private List<LiveInstance> deepCopy(List<LiveInstance> instances) { List<LiveInstance> result = new ArrayList<LiveInstance>(); for (LiveInstance instance : instances) { result.add(new LiveInstance(instance.getRecord())); } return result; } }
/* package */ LiveInstance createLiveInstance(String[] resourceTypes, String[] resourceCapacity, String instancename) { LiveInstance li = new LiveInstance(instancename); if (resourceCapacity != null && resourceTypes != null) { Map<String, String> resMap = new HashMap<>(); for (int i = 0; i < resourceCapacity.length; i++) { resMap.put(resourceTypes[i], resourceCapacity[i]); } li.setResourceCapacityMap(resMap); } return li; }
List<LiveInstance> liveInstanceList = new ArrayList<>(); for (String instance : _instanceConfigMap.keySet()) { LiveInstance liveInstance = new LiveInstance(instance); liveInstanceList.add(liveInstance);
List<LiveInstance> liveInstanceList = new ArrayList<>(); for (String instance : _instanceConfigMap.keySet()) { LiveInstance liveInstance = new LiveInstance(instance); liveInstanceList.add(liveInstance);
@Test public void testInvalidInitialization() { try { AssignableInstance ai = new AssignableInstance(null, null, null); Assert.fail("Expecting IllegalArgumentException"); } catch (IllegalArgumentException e) { Assert.assertTrue(e.getMessage().contains("cannot be null")); } try { ClusterConfig clusterConfig = new ClusterConfig("testCluster"); InstanceConfig instanceConfig = new InstanceConfig("instance"); LiveInstance liveInstance = new LiveInstance("another-instance"); AssignableInstance ai = new AssignableInstance(clusterConfig, instanceConfig, liveInstance); Assert.fail("Expecting IllegalArgumentException"); } catch (IllegalArgumentException e) { Assert.assertTrue(e.getMessage().contains("don't match")); } }
liveInstances.put("localhost_0", new LiveInstance("localhost_0")); liveInstances.put("localhost_1", new LiveInstance("localhost_1"));
@Test public void testUpdateAssignableInstances() { Map<String, LiveInstance> newLiveInstances = new HashMap<>(); Map<String, InstanceConfig> newInstanceConfigs = new HashMap<>(); // A brand new set of LiveInstances for (int i = NUM_PARTICIPANTS; i < NUM_PARTICIPANTS + 3; i++) { String instanceName = INSTANCE_PREFIX + i; newLiveInstances.put(instanceName, new LiveInstance(instanceName)); newInstanceConfigs.put(instanceName, new InstanceConfig(instanceName)); } _assignableInstanceManager.updateAssignableInstances(_clusterConfig, newLiveInstances, newInstanceConfigs); // Check that the assignable instance map contains new instances and there are no // TaskAssignResults due to previous live instances being removed Assert.assertEquals(_assignableInstanceManager.getTaskAssignResultMap().size(), 0); Assert.assertEquals(_assignableInstanceManager.getAssignableInstanceMap().size(), newLiveInstances.size()); for (String instance : newLiveInstances.keySet()) { Assert .assertTrue(_assignableInstanceManager.getAssignableInstanceMap().containsKey(instance)); } }
@Test public void testMasterXfer() { System.out.println("START testMasterXfer at " + new Date(System.currentTimeMillis())); Map<String, LiveInstance> liveInstances = new HashMap<String, LiveInstance>(); liveInstances.put("localhost_0", new LiveInstance("localhost_0")); liveInstances.put("localhost_1", new LiveInstance("localhost_1")); Map<String, String> currentStates = new HashMap<String, String>(); currentStates.put("localhost_0", "SLAVE"); currentStates.put("localhost_1", "MASTER"); Map<String, Message> pendingMessages = new HashMap<String, Message>(); List<Message> messages = new ArrayList<Message>(); messages.add(TestHelper.createMessage("msgId_0", "SLAVE", "MASTER", "localhost_0", "TestDB", "TestDB_0")); messages.add(TestHelper.createMessage("msgId_1", "MASTER", "SLAVE", "localhost_1", "TestDB", "TestDB_0")); Map<String, Bounds> stateConstraints = new HashMap<String, Bounds>(); stateConstraints.put("MASTER", new Bounds(0, 1)); stateConstraints.put("SLAVE", new Bounds(0, 2)); Map<String, Integer> stateTransitionPriorities = new HashMap<String, Integer>(); stateTransitionPriorities.put("MASTER-SLAVE", 0); stateTransitionPriorities.put("SLAVE-MASTER", 1); List<Message> selectedMsg = new MessageSelectionStage().selectMessages(liveInstances, currentStates, pendingMessages, messages, Collections.<Message>emptyList(), stateConstraints, stateTransitionPriorities, BuiltInStateModelDefinitions.MasterSlave.getStateModelDefinition(), false); Assert.assertEquals(selectedMsg.size(), 1); Assert.assertEquals(selectedMsg.get(0).getMsgId(), "msgId_1"); System.out.println("END testMasterXfer at " + new Date(System.currentTimeMillis())); }
private void setupMockCluster() { List<LiveInstance> liveInstanceList = new ArrayList<>(); Map<String, InstanceConfig> instanceConfigs = new HashMap<>(); for (String instance : instanceNames) { LiveInstance liveInstance = new LiveInstance(instance); liveInstanceList.add(liveInstance); InstanceConfig config = new InstanceConfig(instance); instanceConfigs.put(instance, config); } cache.setLiveInstances(liveInstanceList); cache.setInstanceConfigMap(instanceConfigs); ClusterConfig clusterConfig = new ClusterConfig("test"); clusterConfig.setTopologyAwareEnabled(false); cache.setClusterConfig(clusterConfig); states.put("OFFLINE", 0); states.put(topState, nReplicas); }
protected List<String> setupLiveInstances(int numLiveInstances) { List<String> instances = new ArrayList<>(); for (int i = 0; i < numLiveInstances; i++) { LiveInstance liveInstance = new LiveInstance(HOSTNAME_PREFIX + i); liveInstance.setSessionId(SESSION_PREFIX + i); Builder keyBuilder = accessor.keyBuilder(); accessor.setProperty(keyBuilder.liveInstance(HOSTNAME_PREFIX + i), liveInstance); instances.add(liveInstance.getInstanceName()); } return instances; }
Builder keyBuilder = accessor.keyBuilder(); LiveInstance newLeader = new LiveInstance(manager.getInstanceName()); newLeader.setLiveInstance(ManagementFactory.getRuntimeMXBean().getName()); newLeader.setSessionId(manager.getSessionId());
private boolean tryUpdateController(HelixManager manager) { HelixDataAccessor accessor = manager.getHelixDataAccessor(); Builder keyBuilder = accessor.keyBuilder(); LiveInstance leader = new LiveInstance(manager.getInstanceName()); try { leader.setLiveInstance(ManagementFactory.getRuntimeMXBean().getName()); leader.setSessionId(manager.getSessionId()); leader.setHelixVersion(manager.getVersion()); boolean success = accessor.createControllerLeader(leader); if (success) { return true; } else { LOG.info("Unable to become leader probably because some other controller becames the leader"); } } catch (Exception e) { LOG.error( "Exception when trying to updating leader record in cluster:" + manager.getClusterName() + ". Need to check again whether leader node has been created or not", e); } leader = accessor.getProperty(keyBuilder.controllerLeader()); if (leader != null) { String leaderSessionId = leader.getSessionId(); LOG.info("Leader exists for cluster: " + manager.getClusterName() + ", currentLeader: " + leader.getInstanceName() + ", leaderSessionId: " + leaderSessionId); if (leaderSessionId != null && leaderSessionId.equals(manager.getSessionId())) { return true; } } return false; }
for (int i = 0; i < instanceNames.size(); i++) { String instance = instanceNames.get(i); LiveInstance liveInstance = new LiveInstance(instance); liveInstanceList.add(liveInstance); InstanceConfig config = new InstanceConfig(instance);
LiveInstance liveInstance = new LiveInstance(instanceName); InstanceConfig instanceConfig = new InstanceConfig(instanceName); _liveInstances.put(instanceName, liveInstance);
private void setLeader(HelixManager manager) throws Exception { System.out.println("Setting controller " + manager.getInstanceName() + " as leader"); HelixDataAccessor accessor = manager.getHelixDataAccessor(); final LiveInstance leader = new LiveInstance(manager.getInstanceName()); leader.setLiveInstance(ManagementFactory.getRuntimeMXBean().getName()); leader.setSessionId(manager.getSessionId()); leader.setHelixVersion(manager.getVersion()); // Delete the current controller leader node so it will trigger leader election while (!manager.isLeader()) { accessor.getBaseDataAccessor() .remove(PropertyPathBuilder.controllerLeader(manager.getClusterName()), AccessOption.EPHEMERAL); Thread.sleep(50); } }
Builder keyBuilder = accessor.keyBuilder(); LiveInstance newLiveIns = new LiveInstance("newLiveInstance"); newLiveIns.setHelixVersion("0.6.0"); newLiveIns.setSessionId("randomSessionId");
accessor.setProperty(keyBuilder.clusterConfig(), clusterConfig); for (String node : _liveNodes) { LiveInstance liveInstance = new LiveInstance(node); liveInstance.setSessionId("testSession"); accessor.setProperty(keyBuilder.liveInstance(node), liveInstance);
protected void setupLiveInstances(String clusterName, int[] liveInstances) { ZKHelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(_gZkClient)); Builder keyBuilder = accessor.keyBuilder(); for (int i = 0; i < liveInstances.length; i++) { String instance = "localhost_" + liveInstances[i]; LiveInstance liveInstance = new LiveInstance(instance); liveInstance.setSessionId("session_" + liveInstances[i]); liveInstance.setHelixVersion("0.0.0"); accessor.setProperty(keyBuilder.liveInstance(instance), liveInstance); } }
new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(_gZkClient)); Builder keyBuilder = new Builder(clusterName); LiveInstance liveInstance = new LiveInstance("localhost_12918"); liveInstance.setSessionId("session_0"); liveInstance.setHelixVersion("version_0");
record.setSimpleField(LiveInstanceProperty.HELIX_VERSION.toString(), participantVersion); LiveInstance liveInstance = new LiveInstance(record); liveInstance.setSessionId("session_0"); accessor.setProperty(keyBuilder.liveInstance("localhost_0"), liveInstance);