use of org.apache.helix.model.LiveInstance in project helix by apache.
the class TestDisableCustomCodeRunner method test.
@Test
public void test() throws Exception {
String className = TestHelper.getTestClassName();
String methodName = TestHelper.getTestMethodName();
String clusterName = className + "_" + methodName;
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
PARTITION_NUM, // number of nodes
N, // replicas
2, "MasterSlave", // do rebalance
true);
ClusterControllerManager controller = new ClusterControllerManager(ZK_ADDR, clusterName, "controller");
controller.syncStart();
// start participants
Map<String, MockParticipantManager> participants = new HashMap<String, MockParticipantManager>();
Map<String, HelixCustomCodeRunner> customCodeRunners = new HashMap<String, HelixCustomCodeRunner>();
Map<String, DummyCallback> callbacks = new HashMap<String, DummyCallback>();
for (int i = 0; i < N; i++) {
String instanceName = "localhost_" + (12918 + i);
participants.put(instanceName, new MockParticipantManager(ZK_ADDR, clusterName, instanceName));
customCodeRunners.put(instanceName, new HelixCustomCodeRunner(participants.get(instanceName), ZK_ADDR));
callbacks.put(instanceName, new DummyCallback());
customCodeRunners.get(instanceName).invoke(callbacks.get(instanceName)).on(ChangeType.LIVE_INSTANCE).usingLeaderStandbyModel("TestParticLeader").start();
participants.get(instanceName).syncStart();
}
boolean result = ClusterStateVerifier.verifyByZkCallback(new ClusterStateVerifier.BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName));
Assert.assertTrue(result);
// Make sure callback is registered
BaseDataAccessor<ZNRecord> baseAccessor = new ZkBaseDataAccessor<ZNRecord>(_gZkClient);
final HelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, baseAccessor);
PropertyKey.Builder keyBuilder = accessor.keyBuilder();
final String customCodeRunnerResource = customCodeRunners.get("localhost_12918").getResourceName();
ExternalView extView = accessor.getProperty(keyBuilder.externalView(customCodeRunnerResource));
Map<String, String> instanceStates = extView.getStateMap(customCodeRunnerResource + "_0");
String leader = null;
for (String instance : instanceStates.keySet()) {
String state = instanceStates.get(instance);
if ("LEADER".equals(state)) {
leader = instance;
break;
}
}
Assert.assertNotNull(leader);
for (String instance : callbacks.keySet()) {
DummyCallback callback = callbacks.get(instance);
if (instance.equals(leader)) {
Assert.assertTrue(callback.isInitTypeInvoked());
} else {
Assert.assertFalse(callback.isInitTypeInvoked());
}
callback.reset();
}
// Disable custom-code runner resource
HelixAdmin admin = new ZKHelixAdmin(_gZkClient);
admin.enableResource(clusterName, customCodeRunnerResource, false);
// Verify that states of custom-code runner are all OFFLINE
result = TestHelper.verify(new TestHelper.Verifier() {
@Override
public boolean verify() throws Exception {
PropertyKey.Builder keyBuilder = accessor.keyBuilder();
ExternalView extView = accessor.getProperty(keyBuilder.externalView(customCodeRunnerResource));
if (extView == null) {
return false;
}
Set<String> partitionSet = extView.getPartitionSet();
if (partitionSet == null || partitionSet.size() != PARTITION_NUM) {
return false;
}
for (String partition : partitionSet) {
Map<String, String> instanceStates = extView.getStateMap(partition);
for (String state : instanceStates.values()) {
if (!"OFFLINE".equals(state)) {
return false;
}
}
}
return true;
}
}, 10 * 1000);
Assert.assertTrue(result);
// Change live-instance should not invoke any custom-code runner
LiveInstance fakeInstance = new LiveInstance("fakeInstance");
fakeInstance.setSessionId("fakeSessionId");
fakeInstance.setHelixVersion("0.6");
accessor.setProperty(keyBuilder.liveInstance("fakeInstance"), fakeInstance);
Thread.sleep(1000);
for (Map.Entry<String, DummyCallback> e : callbacks.entrySet()) {
String instance = e.getKey();
DummyCallback callback = e.getValue();
Assert.assertFalse(callback.isInitTypeInvoked());
Assert.assertFalse(callback.isCallbackTypeInvoked());
// Ensure that we were told that a leader stopped being the leader
if (instance.equals(leader)) {
Assert.assertTrue(callback.isFinalizeTypeInvoked());
}
}
// Remove fake instance
accessor.removeProperty(keyBuilder.liveInstance("fakeInstance"));
// Re-enable custom-code runner
admin.enableResource(clusterName, customCodeRunnerResource, true);
result = ClusterStateVerifier.verifyByZkCallback(new ClusterStateVerifier.BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName));
Assert.assertTrue(result);
// Verify that custom-invoke is invoked again
extView = accessor.getProperty(keyBuilder.externalView(customCodeRunnerResource));
instanceStates = extView.getStateMap(customCodeRunnerResource + "_0");
leader = null;
for (String instance : instanceStates.keySet()) {
String state = instanceStates.get(instance);
if ("LEADER".equals(state)) {
leader = instance;
break;
}
}
Assert.assertNotNull(leader);
for (String instance : callbacks.keySet()) {
DummyCallback callback = callbacks.get(instance);
if (instance.equals(leader)) {
Assert.assertTrue(callback.isInitTypeInvoked());
} else {
Assert.assertFalse(callback.isInitTypeInvoked());
}
callback.reset();
}
// Add a fake instance should invoke custom-code runner
accessor.setProperty(keyBuilder.liveInstance("fakeInstance"), fakeInstance);
Thread.sleep(1000);
for (String instance : callbacks.keySet()) {
DummyCallback callback = callbacks.get(instance);
if (instance.equals(leader)) {
Assert.assertTrue(callback.isCallbackTypeInvoked());
} else {
Assert.assertFalse(callback.isCallbackTypeInvoked());
}
}
// Clean up
controller.syncStop();
for (MockParticipantManager participant : participants.values()) {
participant.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 TestDistributedCMMain method testDistributedCMMain.
@Test
public void testDistributedCMMain() throws Exception {
String className = TestHelper.getTestClassName();
String methodName = TestHelper.getTestMethodName();
String clusterNamePrefix = className + "_" + methodName;
final int n = 5;
final int clusterNb = 10;
System.out.println("START " + clusterNamePrefix + " at " + new Date(System.currentTimeMillis()));
// setup 10 clusters
for (int i = 0; i < clusterNb; i++) {
String clusterName = clusterNamePrefix + "0_" + i;
String participantName = "localhost" + i;
String resourceName = "TestDB" + i;
// participant port
TestHelper.setupCluster(// participant port
clusterName, // participant port
ZK_ADDR, // participant port
12918, // participant name prefix
participantName, // resource name prefix
resourceName, // resources
1, // partitions per resource
8, // number of nodes
n, // replicas
3, "MasterSlave", // do rebalance
true);
}
// setup controller cluster
final String controllerClusterName = "CONTROLLER_" + clusterNamePrefix;
// controller
TestHelper.setupCluster(// controller
"CONTROLLER_" + clusterNamePrefix, // controller
ZK_ADDR, // controller
0, // participant name prefix
"controller", // resource name prefix
clusterNamePrefix, // resources
1, // partitions per resource
clusterNb, // number of nodes
n, // replicas
3, "LeaderStandby", // do rebalance
true);
// start distributed cluster controllers
ClusterDistributedController[] controllers = new ClusterDistributedController[n + n];
for (int i = 0; i < n; i++) {
controllers[i] = new ClusterDistributedController(ZK_ADDR, controllerClusterName, "controller_" + i);
controllers[i].syncStart();
}
boolean result = ClusterStateVerifier.verifyByZkCallback(new ClusterStateVerifier.BestPossAndExtViewZkVerifier(ZK_ADDR, controllerClusterName), 30000);
Assert.assertTrue(result, "Controller cluster NOT in ideal state");
// start first cluster
MockParticipantManager[] participants = new MockParticipantManager[n];
final String firstClusterName = clusterNamePrefix + "0_0";
for (int i = 0; i < n; i++) {
String instanceName = "localhost0_" + (12918 + i);
participants[i] = new MockParticipantManager(ZK_ADDR, firstClusterName, instanceName);
participants[i].syncStart();
}
result = ClusterStateVerifier.verifyByZkCallback(new BestPossAndExtViewZkVerifier(ZK_ADDR, firstClusterName));
Assert.assertTrue(result, "first cluster NOT in ideal state");
// add more controllers to controller cluster
ClusterSetup setupTool = new ClusterSetup(ZK_ADDR);
for (int i = 0; i < n; i++) {
String controller = "controller_" + (n + i);
setupTool.addInstanceToCluster(controllerClusterName, controller);
}
setupTool.rebalanceStorageCluster(controllerClusterName, clusterNamePrefix + "0", 6);
for (int i = n; i < 2 * n; i++) {
controllers[i] = new ClusterDistributedController(ZK_ADDR, controllerClusterName, "controller_" + i);
controllers[i].syncStart();
}
// verify controller cluster
result = ClusterStateVerifier.verifyByZkCallback(new ClusterStateVerifier.BestPossAndExtViewZkVerifier(ZK_ADDR, controllerClusterName));
Assert.assertTrue(result, "Controller cluster NOT in ideal state");
// verify first cluster
result = ClusterStateVerifier.verifyByZkCallback(new BestPossAndExtViewZkVerifier(ZK_ADDR, firstClusterName));
Assert.assertTrue(result, "first cluster NOT in ideal state");
// stop controller_0-5
ZkBaseDataAccessor<ZNRecord> baseAccessor = new ZkBaseDataAccessor<ZNRecord>(_gZkClient);
ZKHelixDataAccessor accessor = new ZKHelixDataAccessor(controllerClusterName, baseAccessor);
Builder keyBuilder = accessor.keyBuilder();
for (int i = 0; i < n; i++) {
LiveInstance leader = accessor.getProperty(keyBuilder.controllerLeader());
String leaderName = leader.getId();
int j = Integer.parseInt(leaderName.substring(leaderName.lastIndexOf('_') + 1));
controllers[j].syncStop();
result = ClusterStateVerifier.verifyByZkCallback(new ClusterStateVerifier.BestPossAndExtViewZkVerifier(ZK_ADDR, controllerClusterName));
Assert.assertTrue(result, "Controller cluster NOT in ideal state");
result = ClusterStateVerifier.verifyByZkCallback(new BestPossAndExtViewZkVerifier(ZK_ADDR, firstClusterName));
Assert.assertTrue(result, "first cluster NOT in ideal state");
}
// clean up
// wait for all zk callbacks done
System.out.println("Cleaning up...");
for (int i = 0; i < 5; i++) {
result = ClusterStateVerifier.verifyByZkCallback(new ClusterStateVerifier.BestPossAndExtViewZkVerifier(ZK_ADDR, controllerClusterName));
controllers[i].syncStop();
}
for (int i = 0; i < 5; i++) {
participants[i].syncStop();
}
System.out.println("END " + clusterNamePrefix + " at " + new Date(System.currentTimeMillis()));
}
use of org.apache.helix.model.LiveInstance in project helix by apache.
the class TestDistributedClusterController method testDistributedClusterController.
@Test
public void testDistributedClusterController() throws Exception {
String className = TestHelper.getTestClassName();
String methodName = TestHelper.getTestMethodName();
String clusterNamePrefix = className + "_" + methodName;
final int n = 5;
final int clusterNb = 10;
System.out.println("START " + clusterNamePrefix + " at " + new Date(System.currentTimeMillis()));
// setup 10 clusters
for (int i = 0; i < clusterNb; i++) {
String clusterName = clusterNamePrefix + "0_" + i;
String participantName = "localhost" + i;
String resourceName = "TestDB" + i;
// participant port
TestHelper.setupCluster(// participant port
clusterName, // participant port
ZK_ADDR, // participant port
12918, // participant name prefix
participantName, // resource name prefix
resourceName, // resources
1, // partitions per resource
8, // number of nodes
n, // replicas
3, "MasterSlave", // do rebalance
true);
}
// setup controller cluster
final String controllerClusterName = "CONTROLLER_" + clusterNamePrefix;
// controller
TestHelper.setupCluster(// controller
"CONTROLLER_" + clusterNamePrefix, // controller
ZK_ADDR, // controller
0, // participant name prefix
"controller", // resource name prefix
clusterNamePrefix, // resources
1, // partitions per resource
clusterNb, // number of nodes
n, // replicas
3, "LeaderStandby", // do rebalance
true);
// start distributed cluster controllers
ClusterDistributedController[] controllers = new ClusterDistributedController[n];
for (int i = 0; i < n; i++) {
controllers[i] = new ClusterDistributedController(ZK_ADDR, controllerClusterName, "controller_" + i);
controllers[i].syncStart();
}
boolean result = ClusterStateVerifier.verifyByZkCallback(new ClusterStateVerifier.BestPossAndExtViewZkVerifier(ZK_ADDR, controllerClusterName), 30000);
Assert.assertTrue(result, "Controller cluster NOT in ideal state");
// start first cluster
MockParticipantManager[] participants = new MockParticipantManager[n];
final String firstClusterName = clusterNamePrefix + "0_0";
for (int i = 0; i < n; i++) {
String instanceName = "localhost0_" + (12918 + i);
participants[i] = new MockParticipantManager(ZK_ADDR, firstClusterName, instanceName);
participants[i].syncStart();
}
result = ClusterStateVerifier.verifyByZkCallback(new BestPossAndExtViewZkVerifier(ZK_ADDR, firstClusterName));
Assert.assertTrue(result, "first cluster NOT in ideal state");
// stop current leader in controller cluster
ZkBaseDataAccessor<ZNRecord> baseAccessor = new ZkBaseDataAccessor<ZNRecord>(_gZkClient);
ZKHelixDataAccessor accessor = new ZKHelixDataAccessor(controllerClusterName, baseAccessor);
Builder keyBuilder = accessor.keyBuilder();
LiveInstance leader = accessor.getProperty(keyBuilder.controllerLeader());
String leaderName = leader.getId();
int j = Integer.parseInt(leaderName.substring(leaderName.lastIndexOf('_') + 1));
controllers[j].syncStop();
// setup the second cluster
MockParticipantManager[] participants2 = new MockParticipantManager[n];
final String secondClusterName = clusterNamePrefix + "0_1";
for (int i = 0; i < n; i++) {
String instanceName = "localhost1_" + (12918 + i);
participants2[i] = new MockParticipantManager(ZK_ADDR, secondClusterName, instanceName);
participants2[i].syncStart();
}
result = ClusterStateVerifier.verifyByZkCallback(new BestPossAndExtViewZkVerifier(ZK_ADDR, secondClusterName));
Assert.assertTrue(result, "second cluster NOT in ideal state");
// clean up
// wait for all zk callbacks done
System.out.println("Cleaning up...");
for (int i = 0; i < 5; i++) {
result = ClusterStateVerifier.verifyByZkCallback(new BestPossAndExtViewZkVerifier(ZK_ADDR, controllerClusterName));
controllers[i].syncStop();
}
for (int i = 0; i < 5; i++) {
participants[i].syncStop();
}
System.out.println("END " + clusterNamePrefix + " at " + new Date(System.currentTimeMillis()));
}
use of org.apache.helix.model.LiveInstance in project helix by apache.
the class TestReelectedPipelineCorrectness method testReelection.
@Test
public void testReelection() throws Exception {
final int NUM_CONTROLLERS = 2;
final int NUM_PARTICIPANTS = 4;
final int NUM_PARTITIONS = 8;
final int NUM_REPLICAS = 2;
String className = TestHelper.getTestClassName();
String methodName = TestHelper.getTestMethodName();
String clusterName = className + "_" + methodName;
System.out.println("START " + clusterName + " at " + new Date(System.currentTimeMillis()));
ClusterSetup setupTool = new ClusterSetup(ZK_ADDR);
// Set up cluster
// 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
NUM_PARTITIONS, // number of nodes
NUM_PARTICIPANTS, // replicas
NUM_REPLICAS, "MasterSlave", RebalanceMode.FULL_AUTO, // do rebalance
true);
// configure distributed controllers
String controllerCluster = clusterName + "_controllers";
setupTool.addCluster(controllerCluster, true);
for (int i = 0; i < NUM_CONTROLLERS; i++) {
setupTool.addInstanceToCluster(controllerCluster, "controller_" + i);
}
setupTool.activateCluster(clusterName, controllerCluster, true);
// start participants
MockParticipantManager[] participants = new MockParticipantManager[NUM_PARTICIPANTS];
for (int i = 0; i < NUM_PARTICIPANTS; i++) {
final String instanceName = "localhost_" + (12918 + i);
participants[i] = new MockParticipantManager(ZK_ADDR, clusterName, instanceName);
participants[i].syncStart();
}
// start controllers
ClusterDistributedController[] controllers = new ClusterDistributedController[NUM_CONTROLLERS];
for (int i = 0; i < NUM_CONTROLLERS; i++) {
controllers[i] = new ClusterDistributedController(ZK_ADDR, controllerCluster, "controller_" + i);
controllers[i].syncStart();
}
Thread.sleep(1000);
// Ensure a balanced cluster
boolean result = ClusterStateVerifier.verifyByZkCallback(new BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName));
Assert.assertTrue(result);
// Disable the leader, resulting in a leader election
HelixDataAccessor accessor = participants[0].getHelixDataAccessor();
LiveInstance leader = accessor.getProperty(accessor.keyBuilder().controllerLeader());
int totalWait = 0;
while (leader == null && totalWait < CHECK_TIMEOUT) {
Thread.sleep(CHECK_INTERVAL);
totalWait += CHECK_INTERVAL;
leader = accessor.getProperty(accessor.keyBuilder().controllerLeader());
}
if (totalWait >= CHECK_TIMEOUT) {
Assert.fail("No leader was ever elected!");
}
String leaderId = leader.getId();
String standbyId = (leaderId.equals("controller_0")) ? "controller_1" : "controller_0";
HelixAdmin admin = setupTool.getClusterManagementTool();
admin.enableInstance(controllerCluster, leaderId, false);
// Stop a participant to make sure that the leader election worked
Thread.sleep(500);
participants[0].syncStop();
Thread.sleep(500);
result = ClusterStateVerifier.verifyByZkCallback(new BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName));
Assert.assertTrue(result);
// Disable the original standby (leaving 0 active controllers) and kill another participant
admin.enableInstance(controllerCluster, standbyId, false);
Thread.sleep(500);
participants[1].syncStop();
// Also change the ideal state
IdealState idealState = admin.getResourceIdealState(clusterName, "TestDB0");
idealState.setMaxPartitionsPerInstance(1);
admin.setResourceIdealState(clusterName, "TestDB0", idealState);
Thread.sleep(500);
// Also disable an instance in the main cluster
admin.enableInstance(clusterName, "localhost_12920", false);
// Re-enable the original leader
admin.enableInstance(controllerCluster, leaderId, true);
// Now check that both the ideal state and the live instances are adhered to by the rebalance
Thread.sleep(500);
result = ClusterStateVerifier.verifyByZkCallback(new BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName));
Assert.assertTrue(result);
// cleanup
for (int i = 0; i < NUM_CONTROLLERS; i++) {
controllers[i].syncStop();
}
for (int i = 2; i < NUM_PARTICIPANTS; i++) {
participants[i].syncStop();
}
System.out.println("STOP " + clusterName + " at " + new Date(System.currentTimeMillis()));
}
use of org.apache.helix.model.LiveInstance in project helix by apache.
the class TestResetPartitionState method clearStatusUpdate.
private void clearStatusUpdate(String clusterName, String instance, String resource, String partition) {
// clear status update for error partition so verify() will not fail on old
// errors
ZKHelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(_gZkClient));
Builder keyBuilder = accessor.keyBuilder();
LiveInstance liveInstance = accessor.getProperty(keyBuilder.liveInstance(instance));
accessor.removeProperty(keyBuilder.stateTransitionStatus(instance, liveInstance.getSessionId(), resource, partition));
}
Aggregations