use of org.apache.helix.manager.zk.ZKHelixDataAccessor 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()));
}
use of org.apache.helix.manager.zk.ZKHelixDataAccessor in project helix by apache.
the class TestPartitionLevelTransitionConstraint 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
1, // number of nodes
n, // replicas
2, "MasterSlave", // do not rebalance
false);
// setup semi-auto ideal-state
BaseDataAccessor<ZNRecord> baseAccessor = new ZkBaseDataAccessor<ZNRecord>(_gZkClient);
HelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, baseAccessor);
StateModelDefinition stateModelDef = defineStateModel();
accessor.setProperty(accessor.keyBuilder().stateModelDef("Bootstrap"), stateModelDef);
IdealState idealState = accessor.getProperty(accessor.keyBuilder().idealStates("TestDB0"));
idealState.setStateModelDefRef("Bootstrap");
idealState.setReplicas("2");
idealState.getRecord().setListField("TestDB0_0", Arrays.asList("localhost_12919", "localhost_12918"));
accessor.setProperty(accessor.keyBuilder().idealStates("TestDB0"), idealState);
// setup partition-level constraint
ConstraintItemBuilder constraintItemBuilder = new ConstraintItemBuilder();
constraintItemBuilder.addConstraintAttribute(ConstraintAttribute.MESSAGE_TYPE.toString(), "STATE_TRANSITION").addConstraintAttribute(ConstraintAttribute.PARTITION.toString(), ".*").addConstraintAttribute(ConstraintAttribute.CONSTRAINT_VALUE.toString(), "1");
HelixAdmin admin = new ZKHelixAdmin(_gZkClient);
admin.setConstraint(clusterName, ConstraintType.MESSAGE_CONSTRAINT, "constraint1", constraintItemBuilder.build());
ClusterControllerManager controller = new ClusterControllerManager(ZK_ADDR, clusterName, "controller");
controller.syncStart();
// start 1st participant
MockParticipantManager[] participants = new MockParticipantManager[n];
String instanceName1 = "localhost_12918";
participants[0] = new MockParticipantManager(ZK_ADDR, clusterName, instanceName1);
participants[0].getStateMachineEngine().registerStateModelFactory("Bootstrap", new BootstrapStateModelFactory());
participants[0].syncStart();
boolean result = ClusterStateVerifier.verifyByZkCallback(new ClusterStateVerifier.BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName));
Assert.assertTrue(result);
// start 2nd participant which will be the master for Test0_0
String instanceName2 = "localhost_12919";
participants[1] = new MockParticipantManager(ZK_ADDR, clusterName, instanceName2);
participants[1].getStateMachineEngine().registerStateModelFactory("Bootstrap", new BootstrapStateModelFactory());
participants[1].syncStart();
result = ClusterStateVerifier.verifyByZkCallback(new ClusterStateVerifier.BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName));
Assert.assertTrue(result);
// check we received the message in the right order
Assert.assertEquals(_msgOrderList.size(), 7);
Message[] _msgOrderArray = _msgOrderList.toArray(new Message[0]);
assertMessage(_msgOrderArray[0], "OFFLINE", "BOOTSTRAP", instanceName1);
assertMessage(_msgOrderArray[1], "BOOTSTRAP", "SLAVE", instanceName1);
assertMessage(_msgOrderArray[2], "SLAVE", "MASTER", instanceName1);
// after we start the 2nd instance, the messages should be received in the following order:
// 1) offline->bootstrap for localhost_12919
// 2) bootstrap->slave for localhost_12919
// 3) master->slave for localhost_12918
// 4) slave->master for localhost_12919
assertMessage(_msgOrderArray[3], "OFFLINE", "BOOTSTRAP", instanceName2);
assertMessage(_msgOrderArray[4], "BOOTSTRAP", "SLAVE", instanceName2);
assertMessage(_msgOrderArray[5], "MASTER", "SLAVE", instanceName1);
assertMessage(_msgOrderArray[6], "SLAVE", "MASTER", instanceName2);
// clean up
// wait for all zk callbacks done
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.manager.zk.ZKHelixDataAccessor in project helix by apache.
the class TestRenamePartition method testRenamePartitionAutoIS.
@Test()
public void testRenamePartitionAutoIS() throws Exception {
String clusterName = "CLUSTER_" + getShortClassName() + "_auto";
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
"TestDB", // resources
1, // partitions per resource
10, // number of nodes
5, // replicas
3, "MasterSlave", // do rebalance
true);
startAndVerify(clusterName);
// rename partition name TestDB0_0 tp TestDB0_100
ZKHelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(_gZkClient));
Builder keyBuilder = accessor.keyBuilder();
IdealState idealState = accessor.getProperty(keyBuilder.idealStates("TestDB0"));
List<String> prioList = idealState.getRecord().getListFields().remove("TestDB0_0");
idealState.getRecord().getListFields().put("TestDB0_100", prioList);
accessor.setProperty(keyBuilder.idealStates("TestDB0"), idealState);
boolean result = ClusterStateVerifier.verifyByPolling(new ClusterStateVerifier.BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName));
Assert.assertTrue(result);
stop(clusterName);
System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis()));
}
use of org.apache.helix.manager.zk.ZKHelixDataAccessor in project helix by apache.
the class TestResourceWithSamePartitionKey method test.
@Test
public void test() throws Exception {
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, "OnlineOffline", RebalanceMode.CUSTOMIZED, // do rebalance
false);
HelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(_gZkClient));
PropertyKey.Builder keyBuilder = accessor.keyBuilder();
IdealState idealState = accessor.getProperty(keyBuilder.idealStates("TestDB0"));
idealState.setReplicas("2");
idealState.setPartitionState("0", "localhost_12918", "ONLINE");
idealState.setPartitionState("0", "localhost_12919", "ONLINE");
idealState.setPartitionState("1", "localhost_12918", "ONLINE");
idealState.setPartitionState("1", "localhost_12919", "ONLINE");
accessor.setProperty(keyBuilder.idealStates("TestDB0"), idealState);
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);
// add a second resource with the same partition-key
IdealState newIdealState = new IdealState("TestDB1");
newIdealState.getRecord().setSimpleFields(idealState.getRecord().getSimpleFields());
newIdealState.setPartitionState("0", "localhost_12918", "ONLINE");
newIdealState.setPartitionState("0", "localhost_12919", "ONLINE");
newIdealState.setPartitionState("1", "localhost_12918", "ONLINE");
newIdealState.setPartitionState("1", "localhost_12919", "ONLINE");
accessor.setProperty(keyBuilder.idealStates("TestDB1"), newIdealState);
result = ClusterStateVerifier.verifyByZkCallback(new ClusterStateVerifier.BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName));
Assert.assertTrue(result);
// assert no ERROR
for (int i = 0; i < n; i++) {
String instanceName = "localhost_" + (12918 + i);
List<String> errs = accessor.getChildNames(keyBuilder.errors(instanceName));
Assert.assertTrue(errs.isEmpty());
}
// 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.manager.zk.ZKHelixDataAccessor in project helix by apache.
the class TestClusterSetup method testDropInstance.
@Test
public void testDropInstance() throws Exception {
// drop without stop, should throw 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
10, // number of nodes
5, // replicas
3, "MasterSlave", // do rebalance
true);
// add fake liveInstance
ZKHelixDataAccessor accessor = 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");
accessor.setProperty(keyBuilder.liveInstance("localhost_12918"), liveInstance);
// drop without stop the process, should throw exception
try {
ClusterSetup.processCommandLineArgs(new String[] { "--zkSvr", ZK_ADDR, "--dropNode", clusterName, "localhost:12918" });
Assert.fail("Should throw exception since localhost_12918 is still in LIVEINSTANCES/");
} catch (Exception e) {
// OK
}
accessor.removeProperty(keyBuilder.liveInstance("localhost_12918"));
// drop without disable, should throw exception
try {
ClusterSetup.processCommandLineArgs(new String[] { "--zkSvr", ZK_ADDR, "--dropNode", clusterName, "localhost:12918" });
Assert.fail("Should throw exception since localhost_12918 is enabled");
} catch (Exception e) {
// e.printStackTrace();
// OK
}
// drop it
ClusterSetup.processCommandLineArgs(new String[] { "--zkSvr", ZK_ADDR, "--enableInstance", clusterName, "localhost_12918", "false" });
ClusterSetup.processCommandLineArgs(new String[] { "--zkSvr", ZK_ADDR, "--dropNode", clusterName, "localhost:12918" });
Assert.assertNull(accessor.getProperty(keyBuilder.instanceConfig("localhost_12918")), "Instance config should be dropped");
Assert.assertFalse(_gZkClient.exists(PropertyPathBuilder.instance(clusterName, "localhost_12918")), "Instance/host should be dropped");
System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis()));
}
Aggregations