use of org.apache.helix.PropertyKey in project helix by apache.
the class HelixTaskExecutor method syncSessionToController.
private void syncSessionToController(HelixManager manager) {
if (_lastSessionSyncTime == null || System.currentTimeMillis() - _lastSessionSyncTime > SESSION_SYNC_INTERVAL) {
// > delay since last sync
HelixDataAccessor accessor = manager.getHelixDataAccessor();
PropertyKey key = new Builder(manager.getClusterName()).controllerMessage(SESSION_SYNC);
if (accessor.getProperty(key) == null) {
LOG.info(String.format("Participant %s syncs session with controller", manager.getInstanceName()));
Message msg = new Message(MessageType.PARTICIPANT_SESSION_CHANGE, SESSION_SYNC);
msg.setSrcName(manager.getInstanceName());
msg.setTgtSessionId("*");
msg.setMsgState(MessageState.NEW);
msg.setMsgId(SESSION_SYNC);
Criteria cr = new Criteria();
cr.setRecipientInstanceType(InstanceType.CONTROLLER);
cr.setSessionSpecific(false);
manager.getMessagingService().send(cr, msg);
_lastSessionSyncTime = System.currentTimeMillis();
}
}
}
use of org.apache.helix.PropertyKey in project helix by apache.
the class TestSyncSessionToController method testSyncSessionToController.
@Test
public void testSyncSessionToController() throws Exception {
System.out.println("START testSyncSessionToController at " + new Date(System.currentTimeMillis()));
String clusterName = getShortClassName();
MockParticipantManager[] participants = new MockParticipantManager[5];
int resourceNb = 10;
// participant port
TestHelper.setupCluster(// participant port
clusterName, // participant port
ZK_ADDR, // participant port
12918, // participant name prefix
"localhost", // resource name prefix
"TestDB", // resources
resourceNb, // partitions per resource
1, // number of nodes
5, // replicas
1, "MasterSlave", // do rebalance
true);
ClusterControllerManager controller = new ClusterControllerManager(ZK_ADDR, clusterName, "controller_0");
controller.syncStart();
// start participants
for (int i = 0; i < 5; i++) {
String instanceName = "localhost_" + (12918 + i);
participants[i] = new MockParticipantManager(ZK_ADDR, clusterName, instanceName);
participants[i].syncStart();
}
ZKHelixManager zkHelixManager = new ZKHelixManager(clusterName, "controllerMessageListener", InstanceType.CONTROLLER, ZK_ADDR);
zkHelixManager.connect();
MockMessageListener mockMessageListener = new MockMessageListener();
zkHelixManager.addControllerMessageListener(mockMessageListener);
PropertyKey.Builder keyBuilder = new PropertyKey.Builder(clusterName);
ZkBaseDataAccessor<ZNRecord> accessor = new ZkBaseDataAccessor<>(_gZkClient);
String path = keyBuilder.liveInstance("localhost_12918").getPath();
Stat stat = new Stat();
ZNRecord data = accessor.get(path, stat, 2);
data.getSimpleFields().put("SESSION_ID", "invalid-id");
accessor.set(path, data, 2);
Thread.sleep(2000);
// Since we always read the content from ephemeral nodes, sync message won't be sent
Assert.assertFalse(mockMessageListener.isSessionSyncMessageSent());
// Even after reconnect, session sync won't happen
ZkTestHelper.expireSession(participants[0].getZkClient());
Assert.assertFalse(mockMessageListener.isSessionSyncMessageSent());
// Inject an invalid session message to trigger sync message
PropertyKey messageKey = keyBuilder.message("localhost_12918", "Mocked Invalid Message");
Message msg = new Message(Message.MessageType.STATE_TRANSITION, "Mocked Invalid Message");
msg.setSrcName(controller.getInstanceName());
msg.setTgtSessionId("invalid-id");
msg.setMsgState(Message.MessageState.NEW);
msg.setMsgId("Mocked Invalid Message");
msg.setTgtName("localhost_12918");
msg.setPartitionName("foo");
msg.setResourceName("bar");
msg.setFromState("SLAVE");
msg.setToState("MASTER");
msg.setSrcSessionId(controller.getSessionId());
msg.setStateModelDef("MasterSlave");
msg.setStateModelFactoryName("DEFAULT");
HelixDataAccessor dataAccessor = new ZKHelixDataAccessor(clusterName, accessor);
dataAccessor.setProperty(messageKey, msg);
Assert.assertTrue(TestHelper.verify(() -> mockMessageListener.isSessionSyncMessageSent(), 1500));
// Cleanup
controller.syncStop();
zkHelixManager.disconnect();
for (int i = 0; i < 5; i++) {
participants[i].syncStop();
}
deleteCluster(clusterName);
}
use of org.apache.helix.PropertyKey in project helix by apache.
the class TestNoThrottleDisabledPartitions method disablePartitionOnInstance.
/**
* Disable select partitions from the first instance to test that these partitions are not subject
* to throttling.
*/
private void disablePartitionOnInstance(MockParticipantManager participant, String resourceName, String partitionName) {
String instanceName = participant.getInstanceName();
PropertyKey key = _accessor.keyBuilder().instanceConfig(instanceName);
InstanceConfig instanceConfig = _accessor.getProperty(key);
instanceConfig.setInstanceEnabledForPartition(resourceName, partitionName, false);
_accessor.setProperty(key, instanceConfig);
}
use of org.apache.helix.PropertyKey in project helix by apache.
the class TestNoThrottleDisabledPartitions method testDisablingPartitionOnInstance.
/**
* Given the following setup for a partition:
* instance 1 : M
* instance 2 : S
* instance 3 : S
* and no throttle config set for recovery balance
* and throttle config of 1 set for load balance,
* Instead of disabling the instance, we disable the partition in the instance config.
* * instance 1 : S (M->S->Offline)
* * instance 2 : M (S->M because it's in recovery)
* * instance 3 : S
* @throws Exception
*/
@Test
public void testDisablingPartitionOnInstance() throws Exception {
int participantCount = 5;
setupEnvironment(participantCount);
// Set the throttling only for load balance
setThrottleConfigForLoadBalance();
// In this setup, TestDB0_2 has a MASTER replica on localhost_12918
disablePartitionOnInstance(_participants[0], _resourceName + "0", "TestDB0_2");
// Resume the controller
ClusterControllerManager controller = new ClusterControllerManager(ZK_ADDR, _clusterName, "controller_0");
controller.syncStart();
Thread.sleep(500L);
// The disabled instance should not hold any top state replicas (MASTER)
PropertyKey liveInstanceKey = _accessor.keyBuilder().liveInstance(_participants[0].getInstanceName());
LiveInstance liveInstance = _accessor.getProperty(liveInstanceKey);
if (liveInstance != null) {
String sessionId = liveInstance.getEphemeralOwner();
List<CurrentState> currentStates = _accessor.getChildValues(_accessor.keyBuilder().currentStates(_participants[0].getInstanceName(), sessionId), true);
for (CurrentState currentState : currentStates) {
for (Map.Entry<String, String> partitionState : currentState.getPartitionStateMap().entrySet()) {
if (partitionState.getKey().equals("TestDB0_2")) {
Assert.assertFalse(partitionState.getValue().equals("MASTER"));
}
}
}
}
// clean up the cluster
controller.syncStop();
for (int i = 0; i < participantCount; i++) {
_participants[i].syncStop();
}
deleteCluster(_clusterName);
}
use of org.apache.helix.PropertyKey in project helix by apache.
the class TestSchemataSM method testSchemataSM.
@Test
public void testSchemataSM() throws Exception {
String className = TestHelper.getTestClassName();
String methodName = TestHelper.getTestMethodName();
String clusterName = className + "_" + methodName;
int n = 5;
MockParticipantManager[] participants = new MockParticipantManager[n];
System.out.println("START " + clusterName + " at " + new Date(System.currentTimeMillis()));
// participant start port
TestHelper.setupCluster(// participant start port
clusterName, // participant start port
ZK_ADDR, // participant start port
12918, // participant name prefix
"localhost", // resource name prefix
"TestSchemata", // resources
1, // partitions per resource
1, // number of nodes
n, // replicas
0, "STORAGE_DEFAULT_SM_SCHEMATA", // don't rebalance
false);
// rebalance ideal-state to use ANY_LIVEINSTANCE for preference list
ZKHelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<>(_gZkClient));
PropertyKey.Builder keyBuilder = accessor.keyBuilder();
PropertyKey key = keyBuilder.idealStates("TestSchemata0");
IdealState idealState = accessor.getProperty(key);
idealState.setReplicas(IdealState.IdealStateConstants.ANY_LIVEINSTANCE.toString());
idealState.getRecord().setListField("TestSchemata0_0", Collections.singletonList(IdealState.IdealStateConstants.ANY_LIVEINSTANCE.toString()));
accessor.setProperty(key, idealState);
ClusterControllerManager controller = new ClusterControllerManager(ZK_ADDR, clusterName, "controller");
controller.syncStart();
// start n-1 participants
for (int i = 1; i < n; i++) {
String instanceName = "localhost_" + (12918 + i);
participants[i] = new MockParticipantManager(ZK_ADDR, clusterName, instanceName);
participants[i].syncStart();
}
boolean result = ClusterStateVerifier.verifyByZkCallback(new BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName));
Assert.assertTrue(result);
// start the remaining 1 participant
participants[0] = new MockParticipantManager(ZK_ADDR, clusterName, "localhost_12918");
participants[0].syncStart();
// make sure we have all participants in MASTER state
result = ClusterStateVerifier.verifyByZkCallback(new BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName));
Assert.assertTrue(result);
key = keyBuilder.externalView("TestSchemata0");
ExternalView externalView = accessor.getProperty(key);
Map<String, String> stateMap = externalView.getStateMap("TestSchemata0_0");
Assert.assertNotNull(stateMap);
Assert.assertEquals(stateMap.size(), n, "all " + n + " participants should be in Master state");
for (int i = 0; i < n; i++) {
String instanceName = "localhost_" + (12918 + i);
Assert.assertNotNull(stateMap.get(instanceName));
Assert.assertEquals(stateMap.get(instanceName), "MASTER");
}
// clean up
controller.syncStop();
for (int i = 0; i < n; i++) {
participants[i].syncStop();
}
deleteCluster(clusterName);
System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis()));
}
Aggregations