use of org.apache.helix.manager.zk.ZkBaseDataAccessor in project helix by apache.
the class TestDisableResource method checkExternalView.
/**
* Check all partitions are in OFFLINE state
* @param clusterName
* @throws Exception
*/
private void checkExternalView(String clusterName) throws Exception {
BaseDataAccessor<ZNRecord> baseAccessor = new ZkBaseDataAccessor<ZNRecord>(_gZkClient);
final HelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, baseAccessor);
// verify that states of TestDB0 are all OFFLINE
boolean result = TestHelper.verify(new TestHelper.Verifier() {
@Override
public boolean verify() throws Exception {
PropertyKey.Builder keyBuilder = accessor.keyBuilder();
ExternalView extView = accessor.getProperty(keyBuilder.externalView("TestDB0"));
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);
}
use of org.apache.helix.manager.zk.ZkBaseDataAccessor in project helix by apache.
the class TestControllerLiveLock method test.
@Test
public void test() throws Exception {
String className = TestHelper.getTestClassName();
String methodName = TestHelper.getTestMethodName();
String clusterName = className + "_" + methodName;
int n = 12;
final int p = 256;
BaseDataAccessor<ZNRecord> baseAccessor = new ZkBaseDataAccessor<ZNRecord>(_gZkClient);
final HelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, baseAccessor);
final PropertyKey.Builder keyBuilder = accessor.keyBuilder();
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
p, // number of nodes
n, // replicas
1, "LeaderStandby", RebalanceMode.FULL_AUTO, // do rebalance
true);
ClusterControllerManager controller = new ClusterControllerManager(ZK_ADDR, clusterName, "controller_0");
controller.syncStart();
// start participants
Random random = new Random();
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();
Thread.sleep(Math.abs(random.nextInt()) % 500 + 500);
}
boolean result = ClusterStateVerifier.verifyByZkCallback(new ClusterStateVerifier.BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName));
Assert.assertTrue(result);
// make sure all partitions are assigned and no partitions is assigned to STANDBY state
result = TestHelper.verify(new TestHelper.Verifier() {
@Override
public boolean verify() throws Exception {
ExternalView extView = accessor.getProperty(keyBuilder.externalView("TestDB0"));
for (int i = 0; i < p; i++) {
String partition = "TestDB0_" + i;
Map<String, String> map = extView.getRecord().getMapField(partition);
if (map == null || map.size() != 1) {
return false;
}
}
return true;
}
}, 10 * 1000);
if (!result) {
ExternalView extView = accessor.getProperty(keyBuilder.externalView("TestDB0"));
for (int i = 0; i < p; i++) {
String partition = "TestDB0_" + i;
Map<String, String> map = extView.getRecord().getMapField(partition);
if (map == null || map.size() != 1) {
LOG.error(partition + ": " + map);
}
}
}
Assert.assertTrue(result);
// 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.ZkBaseDataAccessor in project helix by apache.
the class TestExternalViewUpdates method testExternalViewUpdates.
@Test
public void testExternalViewUpdates() throws Exception {
System.out.println("START testExternalViewUpdates 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);
// 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();
}
// start controller after participants to trigger rebalance immediate after the controller is ready
ClusterControllerManager controller = new ClusterControllerManager(ZK_ADDR, clusterName, "controller_0");
controller.syncStart();
boolean result = ClusterStateVerifier.verifyByZkCallback(new MasterNbInExtViewVerifier(ZK_ADDR, clusterName));
Assert.assertTrue(result);
result = ClusterStateVerifier.verifyByZkCallback(new BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName));
Assert.assertTrue(result);
// need to verify that each ExternalView's version number is 2
Builder keyBuilder = new Builder(clusterName);
ZkBaseDataAccessor<ZNRecord> accessor = new ZkBaseDataAccessor<ZNRecord>(_gZkClient);
String parentPath = keyBuilder.externalViews().getPath();
List<String> childNames = accessor.getChildNames(parentPath, 0);
List<String> paths = new ArrayList<String>();
for (String name : childNames) {
paths.add(parentPath + "/" + name);
}
// Stat[] stats = accessor.getStats(paths);
for (String path : paths) {
Stat stat = accessor.getStat(path, 0);
Assert.assertTrue(stat.getVersion() <= 2, "ExternalView should be updated at most 2 times");
}
// clean up
controller.syncStop();
for (int i = 0; i < 5; i++) {
participants[i].syncStop();
}
System.out.println("END testExternalViewUpdates at " + new Date(System.currentTimeMillis()));
}
use of org.apache.helix.manager.zk.ZkBaseDataAccessor 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.ZkBaseDataAccessor in project helix by apache.
the class TestClusterSetup method testDisableResource.
@Test
public void testDisableResource() 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
10, // number of nodes
5, // replicas
3, "MasterSlave", // do rebalance
true);
// disable "TestDB0" resource
ClusterSetup.processCommandLineArgs(new String[] { "--zkSvr", ZK_ADDR, "--enableResource", clusterName, "TestDB0", "false" });
BaseDataAccessor<ZNRecord> baseAccessor = new ZkBaseDataAccessor<ZNRecord>(_gZkClient);
HelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, baseAccessor);
PropertyKey.Builder keyBuilder = accessor.keyBuilder();
IdealState idealState = accessor.getProperty(keyBuilder.idealStates("TestDB0"));
Assert.assertFalse(idealState.isEnabled());
// enable "TestDB0" resource
ClusterSetup.processCommandLineArgs(new String[] { "--zkSvr", ZK_ADDR, "--enableResource", clusterName, "TestDB0", "true" });
idealState = accessor.getProperty(keyBuilder.idealStates("TestDB0"));
Assert.assertTrue(idealState.isEnabled());
System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis()));
}
Aggregations