RBFMetrics implements RouterMBean, FederationMBean { @Override public String getNamenodes() { final Map<String, Map<String, Object>> info = new LinkedHashMap<>(); if (membershipStore == null) { return "{}"; } try { GetNamenodeRegistrationsRequest request = GetNamenodeRegistrationsRequest.newInstance(); GetNamenodeRegistrationsResponse response = membershipStore.getNamenodeRegistrations(request); final List<MembershipState> namenodes = response.getNamenodeMemberships(); if (namenodes == null || namenodes.size() == 0) { return JSON.toString(info); } List<MembershipState> namenodesOrder = new ArrayList<>(namenodes); Collections.sort(namenodesOrder, MembershipState.NAME_COMPARATOR); for (MembershipState namenode : namenodesOrder) { Map<String, Object> innerInfo = new HashMap<>(); Map<String, Object> map = getJson(namenode); innerInfo.putAll(map); long dateModified = namenode.getDateModified(); long lastHeartbeat = getSecondsSince(dateModified); innerInfo.put("lastHeartbeat", lastHeartbeat); MembershipStats stats = namenode.getStats(); long used = stats.getTotalSpace() - stats.getAvailableSpace(); innerInfo.put("used", used); info.put(namenode.getNamenodeKey(), Collections.unmodifiableMap(innerInfo)); } } catch (IOException e) { LOG.error("Enable to fetch json representation of namenodes {}", e.getMessage()); return "{}"; } return JSON.toString(info); } RBFMetrics(Router router); void close(); @Override String getNamenodes(); @Override String getNameservices(); @Override String getMountTable(); @Override String getRouters(); @Override long getTotalCapacity(); @Override long getRemainingCapacity(); @Override long getProvidedSpace(); @Override long getUsedCapacity(); @Override int getNumNameservices(); @Override int getNumNamenodes(); @Override int getNumExpiredNamenodes(); @Override int getNumLiveNodes(); @Override int getNumDeadNodes(); @Override int getNumStaleNodes(); @Override int getNumDecommissioningNodes(); @Override int getNumDecomLiveNodes(); @Override int getNumDecomDeadNodes(); @Override int getNumInMaintenanceLiveDataNodes(); @Override int getNumInMaintenanceDeadDataNodes(); @Override int getNumEnteringMaintenanceDataNodes(); @Override // NameNodeMXBean String getNodeUsage(); @Override long getNumBlocks(); @Override long getNumOfMissingBlocks(); @Override long getNumOfBlocksPendingReplication(); @Override long getNumOfBlocksUnderReplicated(); @Override long getNumOfBlocksPendingDeletion(); @Override long getNumFiles(); @Override String getRouterStarted(); @Override String getVersion(); @Override String getCompiledDate(); @Override String getCompileInfo(); @Override String getHostAndPort(); @Override String getRouterId(); @Override String getClusterId(); @Override String getBlockPoolId(); @Override String getRouterStatus(); @Override long getCurrentTokensCount(); @Override String getTopTokenRealOwners(); @Override boolean isSecurityEnabled(); @Override int getCorruptFilesCount(); @Override long getScheduledReplicationBlocks(); @Override long getNumberOfMissingBlocksWithReplicationFactorOne(); @Override long getHighestPriorityLowRedundancyReplicatedBlocks(); @Override long getHighestPriorityLowRedundancyECBlocks(); @Override String getSafemode();  }
@Test public void testNamenodeStatsDataSource() throws IOException, JSONException { RBFMetrics metrics = getRouter().getMetrics(); String jsonString = metrics.getNamenodes(); JSONObject jsonObject = new JSONObject(jsonString); Iterator<?> keys = jsonObject.keys(); int nnsFound = 0; while (keys.hasNext()) { JSONObject json = jsonObject.getJSONObject((String) keys.next()); String nameserviceId = json.getString("nameserviceId"); String namenodeId = json.getString("namenodeId"); MembershipState mockEntry = this.findMockNamenode(nameserviceId, namenodeId); assertNotNull(mockEntry); assertEquals(json.getString("state"), mockEntry.getState().toString()); MembershipStats stats = mockEntry.getStats(); assertEquals(json.getLong("numOfActiveDatanodes"), stats.getNumOfActiveDatanodes()); assertEquals(json.getLong("numOfDeadDatanodes"), stats.getNumOfDeadDatanodes()); assertEquals(json.getLong("numOfStaleDatanodes"), stats.getNumOfStaleDatanodes()); assertEquals(json.getLong("numOfDecommissioningDatanodes"), stats.getNumOfDecommissioningDatanodes()); assertEquals(json.getLong("numOfDecomActiveDatanodes"), stats.getNumOfDecomActiveDatanodes()); assertEquals(json.getLong("numOfDecomDeadDatanodes"), stats.getNumOfDecomDeadDatanodes()); assertEquals(json.getLong("numOfInMaintenanceLiveDataNodes"), stats.getNumOfInMaintenanceLiveDataNodes()); assertEquals(json.getLong("numOfInMaintenanceDeadDataNodes"), stats.getNumOfInMaintenanceDeadDataNodes()); assertEquals(json.getLong("numOfEnteringMaintenanceDataNodes"), stats.getNumOfEnteringMaintenanceDataNodes()); assertEquals(json.getLong("numOfBlocks"), stats.getNumOfBlocks()); assertEquals(json.getString("rpcAddress"), mockEntry.getRpcAddress()); assertEquals(json.getString("webScheme"), mockEntry.getWebScheme()); assertEquals(json.getString("webAddress"), mockEntry.getWebAddress()); nnsFound++; } assertEquals(getActiveMemberships().size() + getStandbyMemberships().size(), nnsFound); }