use of org.apache.helix.model.LiveInstance in project helix by apache.
the class TestInstanceCurrentState method testAddedFieldsInCurrentState.
@Test
public void testAddedFieldsInCurrentState() {
String instanceName = PARTICIPANT_PREFIX + "_" + _startPort;
HelixDataAccessor accessor = _manager.getHelixDataAccessor();
LiveInstance liveInstance = accessor.getProperty(accessor.keyBuilder().liveInstance(instanceName));
CurrentState currentState = accessor.getProperty(accessor.keyBuilder().currentState(instanceName, liveInstance.getSessionId(), WorkflowGenerator.DEFAULT_TGT_DB));
// Test start time should happen after test start time
Assert.assertTrue(currentState.getStartTime(WorkflowGenerator.DEFAULT_TGT_DB + "_0") >= _testStartTime);
// Test end time is always larger than start time
Assert.assertTrue(currentState.getEndTime(WorkflowGenerator.DEFAULT_TGT_DB + "_0") >= currentState.getStartTime(WorkflowGenerator.DEFAULT_TGT_DB + "_0"));
// Final state is MASTER, so SLAVE will be the previous state
Assert.assertEquals(currentState.getPreviousState(WorkflowGenerator.DEFAULT_TGT_DB + "_0"), "SLAVE");
}
use of org.apache.helix.model.LiveInstance in project helix by apache.
the class TestConsecutiveZkSessionExpiry method testDistributedController.
@Test
public void testDistributedController() throws Exception {
// Logger.getRootLogger().setLevel(Level.INFO);
String className = TestHelper.getTestClassName();
String methodName = TestHelper.getTestMethodName();
String clusterName = className + "_" + methodName;
int n = 2;
System.out.println("START " + clusterName + " at " + new Date(System.currentTimeMillis()));
// participant port
TestHelper.setupCluster(// participant port
clusterName, // participant port
ZK_ADDR, // participant port
12918, // participant name prefix
"localhost", // resource name prefix
"TestDB", // resources
1, // partitions per resource
4, // number of nodes
n, // replicas
2, "MasterSlave", // do rebalance
true);
ClusterDistributedController[] distributedControllers = new ClusterDistributedController[n];
CountDownLatch startCountdown = new CountDownLatch(1);
CountDownLatch endCountdown = new CountDownLatch(1);
for (int i = 0; i < n; i++) {
String contrllerName = "localhost_" + (12918 + i);
distributedControllers[i] = new ClusterDistributedController(ZK_ADDR, clusterName, contrllerName);
distributedControllers[i].getStateMachineEngine().registerStateModelFactory("MasterSlave", new MockMSModelFactory());
if (i == 0) {
distributedControllers[i].addPreConnectCallback(new PreConnectTestCallback(contrllerName, startCountdown, endCountdown));
}
distributedControllers[i].connect();
}
boolean result = ClusterStateVerifier.verifyByZkCallback(new BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName));
Assert.assertTrue(result);
// expire the session of distributedController
LOG.info("1st Expiring distributedController session...");
String oldSessionId = distributedControllers[0].getSessionId();
ZkTestHelper.asyncExpireSession(distributedControllers[0].getZkClient());
String newSessionId = distributedControllers[0].getSessionId();
LOG.info("Expried distributedController session. oldSessionId: " + oldSessionId + ", newSessionId: " + newSessionId);
// expire zk session again during HelixManager#handleNewSession()
startCountdown.await();
LOG.info("2nd Expiring distributedController session...");
oldSessionId = distributedControllers[0].getSessionId();
ZkTestHelper.asyncExpireSession(distributedControllers[0].getZkClient());
newSessionId = distributedControllers[0].getSessionId();
LOG.info("Expried distributedController session. oldSessionId: " + oldSessionId + ", newSessionId: " + newSessionId);
endCountdown.countDown();
result = ClusterStateVerifier.verifyByPolling(new ClusterStateVerifier.BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName));
Assert.assertTrue(result);
// verify leader changes to localhost_12919
HelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(_gZkClient));
PropertyKey.Builder keyBuilder = accessor.keyBuilder();
Assert.assertNotNull(pollForProperty(LiveInstance.class, accessor, keyBuilder.liveInstance("localhost_12918"), true));
LiveInstance leader = pollForProperty(LiveInstance.class, accessor, keyBuilder.controllerLeader(), true);
Assert.assertNotNull(leader);
Assert.assertEquals(leader.getId(), "localhost_12919");
// check localhost_12918 has 2 handlers: message and data-accessor
LOG.debug("handlers: " + TestHelper.printHandlers(distributedControllers[0]));
List<CallbackHandler> handlers = distributedControllers[0].getHandlers();
Assert.assertEquals(handlers.size(), 2, "Distributed controller should have 2 handler (message) after lose leadership, but was " + handlers.size());
// clean up
distributedControllers[0].disconnect();
distributedControllers[1].disconnect();
Assert.assertNull(pollForProperty(LiveInstance.class, accessor, keyBuilder.liveInstance("localhost_12918"), false));
Assert.assertNull(pollForProperty(LiveInstance.class, accessor, keyBuilder.liveInstance("localhost_12919"), false));
Assert.assertNull(pollForProperty(LiveInstance.class, accessor, keyBuilder.controllerLeader(), false));
System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis()));
}
use of org.apache.helix.model.LiveInstance in project ambry by linkedin.
the class MockHelixDataAccessor method getProperty.
@Override
public <T extends HelixProperty> List<T> getProperty(List<PropertyKey> keys, boolean throwException) {
List<T> result = new ArrayList<>();
for (PropertyKey key : keys) {
if (key.toString().matches("/Ambry-/INSTANCES/.*/CURRENTSTATES/\\d+/\\d+")) {
// an example for the key: /Ambry-/INSTANCES/localhost_18089/CURRENTSTATES/sessionId/0
String[] segments = key.toString().split("/");
String instanceName = segments[3];
String resourceName = segments[6];
Map<String, Map<String, String>> partitionStateMap = mockHelixAdmin.getPartitionStateMapForInstance(instanceName);
ZNRecord record = new ZNRecord(resourceName);
record.setMapFields(partitionStateMap);
result.add((T) (new CurrentState(record)));
} else if (key.toString().matches("/Ambry-/LIVEINSTANCES/.*_\\d+")) {
String[] segments = key.toString().split("/");
String instanceName = segments[3];
ZNRecord record = new ZNRecord(instanceName);
record.setEphemeralOwner(SESSION_ID);
result.add((T) (new LiveInstance(record)));
} else if (key.toString().matches("/Ambry-/CONFIGS/PARTICIPANT/.*_\\d+")) {
String[] segments = key.toString().split("/");
String instanceName = segments[4];
InstanceConfig instanceConfig = mockHelixAdmin.getInstanceConfigs(clusterName).stream().filter(config -> config.getInstanceName().equals(instanceName)).findFirst().get();
result.add((T) instanceConfig);
} else {
result.add((T) properties.get(key));
}
}
return result;
}
use of org.apache.helix.model.LiveInstance in project ambry by linkedin.
the class MockHelixManager method triggerLiveInstanceNotification.
/**
* Trigger a live instance change notification.
*/
void triggerLiveInstanceNotification(boolean init) {
List<LiveInstance> liveInstances = new ArrayList<>();
for (String instance : mockAdmin.getUpInstances()) {
liveInstances.add(new LiveInstance(instance));
}
NotificationContext notificationContext = new NotificationContext(this);
if (init) {
notificationContext.setType(NotificationContext.Type.INIT);
}
liveInstanceChangeListener.onLiveInstanceChange(liveInstances, notificationContext);
}
use of org.apache.helix.model.LiveInstance in project incubator-gobblin by apache.
the class GobblinHelixMessagingService method generateMessage.
@Override
public Map<InstanceType, List<Message>> generateMessage(final Criteria recipientCriteria, final Message message) {
Map<InstanceType, List<Message>> messagesToSendMap = new HashMap<InstanceType, List<Message>>();
InstanceType instanceType = recipientCriteria.getRecipientInstanceType();
if (instanceType == InstanceType.CONTROLLER) {
List<Message> messages = generateMessagesForController(message);
messagesToSendMap.put(InstanceType.CONTROLLER, messages);
} else if (instanceType == InstanceType.PARTICIPANT) {
List<Message> messages = new ArrayList<Message>();
List<Map<String, String>> matchedList = _gobblinHelixCriteriaEvaluator.evaluateCriteria(recipientCriteria, _manager);
if (!matchedList.isEmpty()) {
Map<String, String> sessionIdMap = new HashMap<String, String>();
if (recipientCriteria.isSessionSpecific()) {
HelixDataAccessor accessor = _manager.getHelixDataAccessor();
PropertyKey.Builder keyBuilder = accessor.keyBuilder();
List<LiveInstance> liveInstances = accessor.getChildValues(keyBuilder.liveInstances());
for (LiveInstance liveInstance : liveInstances) {
sessionIdMap.put(liveInstance.getInstanceName(), liveInstance.getSessionId());
}
}
for (Map<String, String> map : matchedList) {
String id = UUID.randomUUID().toString();
Message newMessage = new Message(message.getRecord(), id);
String srcInstanceName = _manager.getInstanceName();
String tgtInstanceName = map.get("instanceName");
// Don't send message to self
if (recipientCriteria.isSelfExcluded() && srcInstanceName.equalsIgnoreCase(tgtInstanceName)) {
continue;
}
newMessage.setSrcName(srcInstanceName);
newMessage.setTgtName(tgtInstanceName);
newMessage.setResourceName(map.get("resourceName"));
newMessage.setPartitionName(map.get("partitionName"));
if (recipientCriteria.isSessionSpecific()) {
newMessage.setTgtSessionId(sessionIdMap.get(tgtInstanceName));
}
messages.add(newMessage);
}
messagesToSendMap.put(InstanceType.PARTICIPANT, messages);
}
}
return messagesToSendMap;
}
Aggregations