@@ -147,12 +147,13 @@ public static void checkOtherInstanceRunning(boolean toCheck) {
147147
148148 private final BalancerProtocols namenode ;
149149 /**
150- * If set balancerShouldRequestStandby true, Balancer will getBlocks from
150+ * If set requestToStandby true, Balancer will getBlocks from
151151 * Standby NameNode only and it can reduce the performance impact of Active
152152 * NameNode, especially in a busy HA mode cluster.
153153 */
154- private boolean balancerShouldRequestStandby ;
155- private NamenodeProtocol standbyNameNode ;
154+ private boolean requestToStandby ;
155+ private String nsId ;
156+ private Configuration config ;
156157 private final KeyManager keyManager ;
157158 final AtomicBoolean fallbackToSimpleAuth = new AtomicBoolean (false );
158159
@@ -188,10 +189,10 @@ public NameNodeConnector(String name, URI nameNodeUri, Path idPath,
188189
189190 this .namenode = NameNodeProxies .createProxy (conf , nameNodeUri ,
190191 BalancerProtocols .class , fallbackToSimpleAuth ).getProxy ();
191- this .balancerShouldRequestStandby = conf .getBoolean (
192+ this .requestToStandby = conf .getBoolean (
192193 DFSConfigKeys .DFS_HA_ALLOW_STALE_READ_KEY ,
193194 DFSConfigKeys .DFS_HA_ALLOW_STALE_READ_DEFAULT );
194- this .standbyNameNode = null ;
195+ this .config = conf ;
195196
196197 this .fs = (DistributedFileSystem )FileSystem .get (nameNodeUri , conf );
197198
@@ -216,24 +217,7 @@ public NameNodeConnector(String name, URI nameNodeUri, String nsId,
216217 Configuration conf , int maxNotChangedIterations )
217218 throws IOException {
218219 this (name , nameNodeUri , idPath , targetPaths , conf , maxNotChangedIterations );
219- if (nsId != null && HAUtil .isHAEnabled (conf , nsId )) {
220- List <ClientProtocol > namenodes =
221- HAUtil .getProxiesForAllNameNodesInNameservice (conf , nsId );
222- for (ClientProtocol proxy : namenodes ) {
223- try {
224- if (proxy .getHAServiceState ().equals (
225- HAServiceProtocol .HAServiceState .STANDBY )) {
226- this .standbyNameNode = NameNodeProxies .createNonHAProxy (
227- conf , RPC .getServerAddress (proxy ), NamenodeProtocol .class ,
228- UserGroupInformation .getCurrentUser (), false ).getProxy ();
229- break ;
230- }
231- } catch (Exception e ) {
232- //Ignore the exception while connecting to a namenode.
233- LOG .debug ("Error while connecting to namenode" , e );
234- }
235- }
236- }
220+ this .nsId = nsId ;
237221 }
238222
239223 public DistributedFileSystem getDistributedFileSystem () {
@@ -255,23 +239,43 @@ public BlocksWithLocations getBlocks(DatanodeInfo datanode, long size, long
255239 if (getBlocksRateLimiter != null ) {
256240 getBlocksRateLimiter .acquire ();
257241 }
258- boolean isRequestStandby = true ;
242+ boolean isRequestStandby = false ;
243+ NamenodeProtocol nnproxy = null ;
259244 try {
260- if (balancerShouldRequestStandby && standbyNameNode != null ) {
261- return standbyNameNode .getBlocks (datanode , size , minBlockSize );
245+ if (requestToStandby && nsId != null
246+ && HAUtil .isHAEnabled (config , nsId )) {
247+ List <ClientProtocol > namenodes =
248+ HAUtil .getProxiesForAllNameNodesInNameservice (config , nsId );
249+ for (ClientProtocol proxy : namenodes ) {
250+ try {
251+ if (proxy .getHAServiceState ().equals (
252+ HAServiceProtocol .HAServiceState .STANDBY )) {
253+ NamenodeProtocol sbn = NameNodeProxies .createNonHAProxy (
254+ config , RPC .getServerAddress (proxy ), NamenodeProtocol .class ,
255+ UserGroupInformation .getCurrentUser (), false ).getProxy ();
256+ nnproxy = sbn ;
257+ isRequestStandby = true ;
258+ break ;
259+ }
260+ } catch (Exception e ) {
261+ // Ignore the exception while connecting to a namenode.
262+ LOG .debug ("Error while connecting to namenode" , e );
263+ }
264+ }
265+ if (nnproxy == null ) {
266+ LOG .warn ("Request #getBlocks to Standby NameNode but meet exception,"
267+ + " will fallback to normal way." );
268+ nnproxy = namenode ;
269+ }
262270 } else {
263- isRequestStandby = false ;
271+ nnproxy = namenode ;
264272 }
265- } catch (Exception e ) {
266- LOG .warn ("Request #getBlocks to Standby NameNode but meet exception, " +
267- "will fallback to normal way" , e );
268- isRequestStandby = false ;
273+ return nnproxy .getBlocks (datanode , size , minBlockSize );
269274 } finally {
270275 if (isRequestStandby ) {
271276 LOG .info ("Request #getBlocks to Standby NameNode success." );
272277 }
273278 }
274- return namenode .getBlocks (datanode , size , minBlockSize );
275279 }
276280
277281 /**
0 commit comments