use of org.apache.helix.model.LiveInstance in project helix by apache.
the class TestControllerLeadershipChange method setLeader.
private void setLeader(HelixManager manager) {
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
accessor.getBaseDataAccessor().remove(PropertyPathBuilder.controllerLeader(manager.getClusterName()), AccessOption.EPHEMERAL);
// No matter who gets leadership, force the given manager to become leader
// Note there is theoretically a racing condition that GenericHelixController.onControllerChange()
// will not catch this new value when it's double checking leadership, but it's stable enough
accessor.getBaseDataAccessor().set(PropertyPathBuilder.controllerLeader(manager.getClusterName()), leader.getRecord(), AccessOption.EPHEMERAL);
}
use of org.apache.helix.model.LiveInstance in project helix by apache.
the class TestDistributedControllerManager method simpleIntegrationTest.
@Test
public void simpleIntegrationTest() 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);
HelixManager[] distributedControllers = new HelixManager[n];
for (int i = 0; i < n; i++) {
int port = 12918 + i;
distributedControllers[i] = new ZKHelixManager(clusterName, "localhost_" + port, InstanceType.CONTROLLER_PARTICIPANT, ZK_ADDR);
distributedControllers[i].getStateMachineEngine().registerStateModelFactory("MasterSlave", new MockMSModelFactory());
distributedControllers[i].connect();
}
boolean result = ClusterStateVerifier.verifyByZkCallback(new BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName));
Assert.assertTrue(result);
// disconnect first distributed-controller, and verify second takes leadership
distributedControllers[0].disconnect();
// verify leader changes to localhost_12919
Thread.sleep(100);
result = ClusterStateVerifier.verifyByZkCallback(new BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName));
Assert.assertTrue(result);
ZKHelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(_gZkClient));
PropertyKey.Builder keyBuilder = accessor.keyBuilder();
Assert.assertNull(accessor.getProperty(keyBuilder.liveInstance("localhost_12918")));
LiveInstance leader = accessor.getProperty(keyBuilder.controllerLeader());
Assert.assertNotNull(leader);
Assert.assertEquals(leader.getId(), "localhost_12919");
// clean up
distributedControllers[1].disconnect();
Assert.assertNull(accessor.getProperty(keyBuilder.liveInstance("localhost_12919")));
Assert.assertNull(accessor.getProperty(keyBuilder.controllerLeader()));
System.out.println("START " + clusterName + " at " + new Date(System.currentTimeMillis()));
}
use of org.apache.helix.model.LiveInstance in project helix by apache.
the class TestDistributedControllerManager method expireController.
/**
* expire a controller and make sure the other takes the leadership
* @param expireController
* @param newController
* @throws Exception
*/
void expireController(ClusterDistributedController expireController, ClusterDistributedController newController) throws Exception {
String clusterName = expireController.getClusterName();
LOG.info("Expiring distributedController: " + expireController.getInstanceName() + ", session: " + expireController.getSessionId() + " ...");
String oldSessionId = expireController.getSessionId();
ZkTestHelper.expireSession(expireController.getZkClient());
String newSessionId = expireController.getSessionId();
LOG.debug("Expired distributedController: " + expireController.getInstanceName() + ", oldSessionId: " + oldSessionId + ", newSessionId: " + newSessionId);
boolean result = ClusterStateVerifier.verifyByPolling(new ClusterStateVerifier.BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName));
Assert.assertTrue(result);
// verify leader changes to localhost_12919
ZKHelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(_gZkClient));
PropertyKey.Builder keyBuilder = accessor.keyBuilder();
Assert.assertNotNull(accessor.getProperty(keyBuilder.liveInstance(expireController.getInstanceName())));
LiveInstance leader = accessor.getProperty(keyBuilder.controllerLeader());
Assert.assertNotNull(leader);
Assert.assertEquals(leader.getId(), newController.getInstanceName());
// check expired-controller has 2 handlers: message and data-accessor
LOG.debug(expireController.getInstanceName() + " handlers: " + TestHelper.printHandlers(expireController));
List<CallbackHandler> handlers = expireController.getHandlers();
Assert.assertEquals(handlers.size(), 2, "Distributed controller should have 1 handler (message) after lose leadership, but was " + handlers.size());
}
use of org.apache.helix.model.LiveInstance in project helix by apache.
the class TestCleanupExternalView method test.
@Test
public void test() 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
2, // number of nodes
n, // replicas
2, "MasterSlave", // do rebalance
true);
ClusterControllerManager controller = new ClusterControllerManager(ZK_ADDR, clusterName, "controller_0");
controller.syncStart();
// start participants
MockParticipantManager[] participants = new MockParticipantManager[n];
for (int i = 0; i < n; i++) {
String instanceName = "localhost_" + (12918 + i);
participants[i] = new MockParticipantManager(ZK_ADDR, clusterName, instanceName);
participants[i].syncStart();
}
boolean result = ClusterStateVerifier.verifyByZkCallback(new ClusterStateVerifier.BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName));
Assert.assertTrue(result);
// disable controller
ZKHelixAdmin admin = new ZKHelixAdmin(_gZkClient);
admin.enableCluster(clusterName, false);
// wait all pending zk-events being processed, otherwise remove current-state will cause
// controller send O->S message
ZkTestHelper.tryWaitZkEventsCleaned(controller.getZkClient());
// System.out.println("paused controller");
// drop resource
admin.dropResource(clusterName, "TestDB0");
// delete current-state manually, controller shall remove external-view when cluster is enabled
// again
ZKHelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(_gZkClient));
PropertyKey.Builder keyBuilder = accessor.keyBuilder();
// System.out.println("remove current-state");
LiveInstance liveInstance = accessor.getProperty(keyBuilder.liveInstance("localhost_12918"));
accessor.removeProperty(keyBuilder.currentState("localhost_12918", liveInstance.getSessionId(), "TestDB0"));
liveInstance = accessor.getProperty(keyBuilder.liveInstance("localhost_12919"));
accessor.removeProperty(keyBuilder.currentState("localhost_12919", liveInstance.getSessionId(), "TestDB0"));
// re-enable controller shall remove orphan external-view
// System.out.println("re-enabling controller");
admin.enableCluster(clusterName, true);
ExternalView externalView = null;
for (int i = 0; i < 10; i++) {
Thread.sleep(100);
externalView = accessor.getProperty(keyBuilder.externalView("TestDB0"));
// System.out.println("externalView: " + externalView);
if (externalView == null) {
break;
}
}
Assert.assertNull(externalView, "external-view for TestDB0 should be removed, but was: " + externalView);
// clean up
controller.syncStop();
for (int i = 0; i < n; i++) {
participants[i].syncStop();
}
System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis()));
}
use of org.apache.helix.model.LiveInstance in project helix by apache.
the class TestHelixCustomCodeRunner method testCustomCodeRunner.
@Test
public void testCustomCodeRunner() throws Exception {
System.out.println("START " + _clusterName + " at " + new Date(System.currentTimeMillis()));
// participant name
TestHelper.setupCluster(// participant name
_clusterName, // participant name
ZK_ADDR, // participant name
_startPort, // participant name
"localhost", // resource name prefix
"TestDB", // resourceNb
1, // partitionNb
5, // nodesNb
_nodeNb, // replica
_nodeNb, "MasterSlave", true);
ClusterControllerManager controller = new ClusterControllerManager(ZK_ADDR, _clusterName, "controller_0");
controller.syncStart();
MockParticipantManager[] participants = new MockParticipantManager[5];
for (int i = 0; i < _nodeNb; i++) {
String instanceName = "localhost_" + (_startPort + i);
participants[i] = new MockParticipantManager(ZK_ADDR, _clusterName, instanceName);
registerCustomCodeRunner(participants[i]);
participants[i].syncStart();
}
boolean result = ClusterStateVerifier.verifyByPolling(new ClusterStateVerifier.BestPossAndExtViewZkVerifier(ZK_ADDR, _clusterName));
Assert.assertTrue(result);
// wait for the INIT type callback to finish
Thread.sleep(1000);
Assert.assertTrue(_callback._isCallbackInvoked);
_callback._isCallbackInvoked = false;
// add a new live instance
HelixDataAccessor accessor = new ZKHelixDataAccessor(_clusterName, new ZkBaseDataAccessor<ZNRecord>(_gZkClient));
Builder keyBuilder = accessor.keyBuilder();
LiveInstance newLiveIns = new LiveInstance("newLiveInstance");
newLiveIns.setHelixVersion("0.6.0");
newLiveIns.setSessionId("randomSessionId");
accessor.setProperty(keyBuilder.liveInstance("newLiveInstance"), newLiveIns);
// wait for the CALLBACK type callback to finish
Thread.sleep(1000);
Assert.assertTrue(_callback._isCallbackInvoked);
// clean up
controller.syncStop();
for (int i = 0; i < _nodeNb; i++) {
participants[i].syncStop();
}
System.out.println("END " + _clusterName + " at " + new Date(System.currentTimeMillis()));
}
Aggregations