HDFS-13869. RBF: Handle NPE for NamenodeBeanMetrics#getFederationMetrics. Contributed by Ranith Sardar.

This commit is contained in:
Yiqun Lin 2018-12-17 12:35:07 +08:00 committed by Brahma Reddy Battula
parent 01b4126b4e
commit bbe859177d
3 changed files with 147 additions and 24 deletions

View File

@ -168,8 +168,12 @@ public void close() {
} }
} }
private FederationMetrics getFederationMetrics() { private FederationMetrics getFederationMetrics() throws IOException {
return this.router.getMetrics(); FederationMetrics metrics = getRouter().getMetrics();
if (metrics == null) {
throw new IOException("Federated metrics is not initialized");
}
return metrics;
} }
///////////////////////////////////////////////////////// /////////////////////////////////////////////////////////
@ -188,22 +192,42 @@ public String getSoftwareVersion() {
@Override @Override
public long getUsed() { public long getUsed() {
return getFederationMetrics().getUsedCapacity(); try {
return getFederationMetrics().getUsedCapacity();
} catch (IOException e) {
LOG.debug("Failed to get the used capacity", e.getMessage());
}
return 0;
} }
@Override @Override
public long getFree() { public long getFree() {
return getFederationMetrics().getRemainingCapacity(); try {
return getFederationMetrics().getRemainingCapacity();
} catch (IOException e) {
LOG.debug("Failed to get remaining capacity", e.getMessage());
}
return 0;
} }
@Override @Override
public long getTotal() { public long getTotal() {
return getFederationMetrics().getTotalCapacity(); try {
return getFederationMetrics().getTotalCapacity();
} catch (IOException e) {
LOG.debug("Failed to Get total capacity", e.getMessage());
}
return 0;
} }
@Override @Override
public long getProvidedCapacity() { public long getProvidedCapacity() {
return getFederationMetrics().getProvidedSpace(); try {
return getFederationMetrics().getProvidedSpace();
} catch (IOException e) {
LOG.debug("Failed to get provided capacity", e.getMessage());
}
return 0;
} }
@Override @Override
@ -261,39 +285,79 @@ public float getPercentBlockPoolUsed() {
@Override @Override
public long getTotalBlocks() { public long getTotalBlocks() {
return getFederationMetrics().getNumBlocks(); try {
return getFederationMetrics().getNumBlocks();
} catch (IOException e) {
LOG.debug("Failed to get number of blocks", e.getMessage());
}
return 0;
} }
@Override @Override
public long getNumberOfMissingBlocks() { public long getNumberOfMissingBlocks() {
return getFederationMetrics().getNumOfMissingBlocks(); try {
return getFederationMetrics().getNumOfMissingBlocks();
} catch (IOException e) {
LOG.debug("Failed to get number of missing blocks", e.getMessage());
}
return 0;
} }
@Override @Override
@Deprecated @Deprecated
public long getPendingReplicationBlocks() { public long getPendingReplicationBlocks() {
return getFederationMetrics().getNumOfBlocksPendingReplication(); try {
return getFederationMetrics().getNumOfBlocksPendingReplication();
} catch (IOException e) {
LOG.debug("Failed to get number of blocks pending replica",
e.getMessage());
}
return 0;
} }
@Override @Override
public long getPendingReconstructionBlocks() { public long getPendingReconstructionBlocks() {
return getFederationMetrics().getNumOfBlocksPendingReplication(); try {
return getFederationMetrics().getNumOfBlocksPendingReplication();
} catch (IOException e) {
LOG.debug("Failed to get number of blocks pending replica",
e.getMessage());
}
return 0;
} }
@Override @Override
@Deprecated @Deprecated
public long getUnderReplicatedBlocks() { public long getUnderReplicatedBlocks() {
return getFederationMetrics().getNumOfBlocksUnderReplicated(); try {
return getFederationMetrics().getNumOfBlocksUnderReplicated();
} catch (IOException e) {
LOG.debug("Failed to get number of blocks under replicated",
e.getMessage());
}
return 0;
} }
@Override @Override
public long getLowRedundancyBlocks() { public long getLowRedundancyBlocks() {
return getFederationMetrics().getNumOfBlocksUnderReplicated(); try {
return getFederationMetrics().getNumOfBlocksUnderReplicated();
} catch (IOException e) {
LOG.debug("Failed to get number of blocks under replicated",
e.getMessage());
}
return 0;
} }
@Override @Override
public long getPendingDeletionBlocks() { public long getPendingDeletionBlocks() {
return getFederationMetrics().getNumOfBlocksPendingDeletion(); try {
return getFederationMetrics().getNumOfBlocksPendingDeletion();
} catch (IOException e) {
LOG.debug("Failed to get number of blocks pending deletion",
e.getMessage());
}
return 0;
} }
@Override @Override
@ -471,7 +535,12 @@ public String getJournalTransactionInfo() {
@Override @Override
public long getNNStartedTimeInMillis() { public long getNNStartedTimeInMillis() {
return this.router.getStartTime(); try {
return getRouter().getStartTime();
} catch (IOException e) {
LOG.debug("Failed to get the router startup time", e.getMessage());
}
return 0;
} }
@Override @Override
@ -527,7 +596,12 @@ public long getProvidedCapacityTotal() {
@Override @Override
public long getFilesTotal() { public long getFilesTotal() {
return getFederationMetrics().getNumFiles(); try {
return getFederationMetrics().getNumFiles();
} catch (IOException e) {
LOG.debug("Failed to get number of files", e.getMessage());
}
return 0;
} }
@Override @Override
@ -537,12 +611,22 @@ public int getTotalLoad() {
@Override @Override
public int getNumLiveDataNodes() { public int getNumLiveDataNodes() {
return this.router.getMetrics().getNumLiveNodes(); try {
return getFederationMetrics().getNumLiveNodes();
} catch (IOException e) {
LOG.debug("Failed to get number of live nodes", e.getMessage());
}
return 0;
} }
@Override @Override
public int getNumDeadDataNodes() { public int getNumDeadDataNodes() {
return this.router.getMetrics().getNumDeadNodes(); try {
return getFederationMetrics().getNumDeadNodes();
} catch (IOException e) {
LOG.debug("Failed to get number of dead nodes", e.getMessage());
}
return 0;
} }
@Override @Override
@ -552,17 +636,35 @@ public int getNumStaleDataNodes() {
@Override @Override
public int getNumDecomLiveDataNodes() { public int getNumDecomLiveDataNodes() {
return this.router.getMetrics().getNumDecomLiveNodes(); try {
return getFederationMetrics().getNumDecomLiveNodes();
} catch (IOException e) {
LOG.debug("Failed to get the number of live decommissioned datanodes",
e.getMessage());
}
return 0;
} }
@Override @Override
public int getNumDecomDeadDataNodes() { public int getNumDecomDeadDataNodes() {
return this.router.getMetrics().getNumDecomDeadNodes(); try {
return getFederationMetrics().getNumDecomDeadNodes();
} catch (IOException e) {
LOG.debug("Failed to get the number of dead decommissioned datanodes",
e.getMessage());
}
return 0;
} }
@Override @Override
public int getNumDecommissioningDataNodes() { public int getNumDecommissioningDataNodes() {
return this.router.getMetrics().getNumDecommissioningNodes(); try {
return getFederationMetrics().getNumDecommissioningNodes();
} catch (IOException e) {
LOG.debug("Failed to get number of decommissioning nodes",
e.getMessage());
}
return 0;
} }
@Override @Override
@ -702,4 +804,11 @@ public int getNumEncryptionZones() {
public String getVerifyECWithTopologyResult() { public String getVerifyECWithTopologyResult() {
return null; return null;
} }
private Router getRouter() throws IOException {
if (this.router == null) {
throw new IOException("Router is not initialized");
}
return this.router;
}
} }

View File

@ -586,11 +586,11 @@ public FederationMetrics getMetrics() {
* *
* @return Namenode metrics. * @return Namenode metrics.
*/ */
public NamenodeBeanMetrics getNamenodeMetrics() { public NamenodeBeanMetrics getNamenodeMetrics() throws IOException {
if (this.metrics != null) { if (this.metrics == null) {
return this.metrics.getNamenodeMetrics(); throw new IOException("Namenode metrics is not initialized");
} }
return null; return this.metrics.getNamenodeMetrics();
} }
/** /**

View File

@ -203,4 +203,18 @@ public void testRouterIDInRouterRpcClient() throws Exception {
router.stop(); router.stop();
router.close(); router.close();
} }
@Test
public void testRouterMetricsWhenDisabled() throws Exception {
Router router = new Router();
router.init(new RouterConfigBuilder(conf).rpc().build());
router.start();
intercept(IOException.class, "Namenode metrics is not initialized",
() -> router.getNamenodeMetrics().getCacheCapacity());
router.stop();
router.close();
}
} }