} catch (Exception e) {
Assert.fail("Fail to register ClusterStatusMonitor");
}
// Test #setPerInstanceResourceStatus()
BestPossibleStateOutput bestPossibleStates = new BestPossibleStateOutput();
bestPossibleStates.setState(testDB, new Partition(testDB_0), "localhost_12918", "MASTER");
bestPossibleStates.setState(testDB, new Partition(testDB_0), "localhost_12919", "SLAVE");
bestPossibleStates.setState(testDB, new Partition(testDB_0), "localhost_12920", "SLAVE");
bestPossibleStates.setState(testDB, new Partition(testDB_0), "localhost_12921", "OFFLINE");
bestPossibleStates.setState(testDB, new Partition(testDB_0), "localhost_12922", "DROPPED");
Map<String, InstanceConfig> instanceConfigMap = Maps.newHashMap();
for (int i = 0; i < n; i++) {
String instanceName = "localhost_" + (12918 + i);
InstanceConfig config = new InstanceConfig(instanceName);
instanceConfigMap.put(instanceName, config);
}
Map<String, Resource> resourceMap = Maps.newHashMap();
Resource db = new Resource(testDB);
db.setStateModelDefRef("MasterSlave");
db.addPartition(testDB_0);
resourceMap.put(testDB, db);
Map<String, StateModelDefinition> stateModelDefMap = Maps.newHashMap();
StateModelDefinition msStateModelDef =
new StateModelDefinition(StateModelConfigGenerator.generateConfigForMasterSlave());
stateModelDefMap.put("MasterSlave", msStateModelDef);
monitor.setPerInstanceResourceStatus(bestPossibleStates, instanceConfigMap, resourceMap,
stateModelDefMap);
// localhost_12918 should have 1 partition because it's MASTER
ObjectName objName =
monitor.getObjectName(monitor.getPerInstanceResourceBeanName("localhost_12918", testDB));
Object value = _server.getAttribute(objName, "PartitionGauge");
Assert.assertTrue(value instanceof Long);
Assert.assertEquals((Long) value, new Long(1));
value = _server.getAttribute(objName, "SensorName");
Assert.assertTrue(value instanceof String);
Assert.assertEquals((String) value, String.format("%s.%s.%s.%s.%s",
ClusterStatusMonitor.PARTICIPANT_STATUS_KEY, clusterName, ClusterStatusMonitor.DEFAULT_TAG,
"localhost_12918", testDB));
// localhost_12919 should have 1 partition because it's SLAVE
objName =
monitor.getObjectName(monitor.getPerInstanceResourceBeanName("localhost_12919", testDB));
value = _server.getAttribute(objName, "PartitionGauge");
Assert.assertTrue(value instanceof Long);
Assert.assertEquals((Long) value, new Long(1));
// localhost_12921 should have 0 partition because it's OFFLINE
objName =
monitor.getObjectName(monitor.getPerInstanceResourceBeanName("localhost_12921", testDB));
value = _server.getAttribute(objName, "PartitionGauge");
Assert.assertTrue(value instanceof Long);
Assert.assertEquals((Long) value, new Long(0));
// localhost_12922 should have 0 partition because it's DROPPED
objName =
monitor.getObjectName(monitor.getPerInstanceResourceBeanName("localhost_12922", testDB));
value = _server.getAttribute(objName, "PartitionGauge");
Assert.assertTrue(value instanceof Long);
Assert.assertEquals((Long) value, new Long(0));
// Missing localhost_12918 in best possible ideal-state should remove it from mbean
bestPossibleStates.getInstanceStateMap(testDB, new Partition(testDB_0)).remove(
"localhost_12918");
monitor.setPerInstanceResourceStatus(bestPossibleStates, instanceConfigMap, resourceMap,
stateModelDefMap);
try {
objName =