use of org.apache.helix.manager.zk.ZKHelixDataAccessor in project helix by apache.
the class TestStatusUpdate method testParticipantStatusUpdates.
// For now write participant StatusUpdates to log4j.
// TODO: Need to investigate another data channel to report to controller and re-enable
// this test
// @Test
public void testParticipantStatusUpdates() throws Exception {
ZkClient zkClient = new ZkClient(ZkIntegrationTestBase.ZK_ADDR);
zkClient.setZkSerializer(new ZNRecordSerializer());
ZKHelixDataAccessor accessor = new ZKHelixDataAccessor(CLUSTER_NAME, new ZkBaseDataAccessor(zkClient));
Builder keyBuilder = accessor.keyBuilder();
List<ExternalView> extViews = accessor.getChildValues(keyBuilder.externalViews());
Assert.assertNotNull(extViews);
for (ExternalView extView : extViews) {
String resourceName = extView.getResourceName();
Set<String> partitionSet = extView.getPartitionSet();
for (String partition : partitionSet) {
Map<String, String> stateMap = extView.getStateMap(partition);
for (String instance : stateMap.keySet()) {
String state = stateMap.get(instance);
StatusUpdateUtil.StatusUpdateContents statusUpdates = StatusUpdateUtil.StatusUpdateContents.getStatusUpdateContents(accessor, instance, resourceName, partition);
Map<String, StatusUpdateUtil.TaskStatus> taskMessages = statusUpdates.getTaskMessages();
List<StatusUpdateUtil.Transition> transitions = statusUpdates.getTransitions();
if (state.equals("MASTER")) {
Assert.assertEquals(transitions.size() >= 2, true, "Invalid number of transitions");
StatusUpdateUtil.Transition lastTransition = transitions.get(transitions.size() - 1);
StatusUpdateUtil.Transition prevTransition = transitions.get(transitions.size() - 2);
Assert.assertEquals(taskMessages.get(lastTransition.getMsgID()), StatusUpdateUtil.TaskStatus.COMPLETED, "Incomplete transition");
Assert.assertEquals(taskMessages.get(prevTransition.getMsgID()), StatusUpdateUtil.TaskStatus.COMPLETED, "Incomplete transition");
Assert.assertEquals(lastTransition.getFromState(), "SLAVE", "Invalid State");
Assert.assertEquals(lastTransition.getToState(), "MASTER", "Invalid State");
} else if (state.equals("SLAVE")) {
Assert.assertEquals(transitions.size() >= 1, true, "Invalid number of transitions");
StatusUpdateUtil.Transition lastTransition = transitions.get(transitions.size() - 1);
Assert.assertEquals(lastTransition.getFromState().equals("MASTER") || lastTransition.getFromState().equals("OFFLINE"), true, "Invalid transition");
Assert.assertEquals(lastTransition.getToState(), "SLAVE", "Invalid State");
}
}
}
}
}
use of org.apache.helix.manager.zk.ZKHelixDataAccessor in project helix by apache.
the class TestDisable method testDisableNodeCustomIS.
@Test
public void testDisableNodeCustomIS() throws Exception {
// Logger.getRootLogger().setLevel(Level.INFO);
String className = TestHelper.getTestClassName();
String methodName = TestHelper.getTestMethodName();
String clusterName = className + "_" + methodName;
final int n = 5;
String disableNode = "localhost_12918";
System.out.println("START " + clusterName + " at " + new Date(System.currentTimeMillis()));
MockParticipantManager[] participants = new MockParticipantManager[n];
// 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
8, // number of nodes
n, // replicas
3, "MasterSlave", // do rebalance
true);
// set ideal state to customized mode
ZkBaseDataAccessor<ZNRecord> baseAccessor = new ZkBaseDataAccessor<ZNRecord>(_gZkClient);
ZKHelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, baseAccessor);
Builder keyBuilder = accessor.keyBuilder();
IdealState idealState = accessor.getProperty(keyBuilder.idealStates("TestDB0"));
idealState.setRebalanceMode(RebalanceMode.CUSTOMIZED);
accessor.setProperty(keyBuilder.idealStates("TestDB0"), idealState);
// start controller
ClusterControllerManager controller = new ClusterControllerManager(ZK_ADDR, clusterName, "controller_0");
controller.syncStart();
// start participants
for (int i = 0; i < n; i++) {
String instanceName = "localhost_" + (12918 + i);
participants[i] = new MockParticipantManager(ZK_ADDR, clusterName, instanceName);
participants[i].syncStart();
}
HelixClusterVerifier _clusterVerifier = new BestPossibleExternalViewVerifier.Builder(clusterName).setZkAddr(ZK_ADDR).build();
Assert.assertTrue(_clusterVerifier.verify());
// disable localhost_12918
String command = "--zkSvr " + ZK_ADDR + " --enableInstance " + clusterName + " " + disableNode + " false";
ClusterSetup.processCommandLineArgs(command.split("\\s+"));
Assert.assertTrue(_clusterVerifier.verify());
// make sure localhost_12918 is in OFFLINE state
Map<String, Map<String, String>> expectStateMap = new HashMap<String, Map<String, String>>();
Map<String, String> expectInstanceStateMap = new HashMap<String, String>();
expectInstanceStateMap.put(disableNode, "OFFLINE");
expectStateMap.put(".*", expectInstanceStateMap);
boolean result = ZkTestHelper.verifyState(_gZkClient, clusterName, "TestDB0", expectStateMap, "==");
Assert.assertTrue(result, disableNode + " should be in OFFLINE");
// re-enable localhost_12918
command = "--zkSvr " + ZK_ADDR + " --enableInstance " + clusterName + " " + disableNode + " true";
ClusterSetup.processCommandLineArgs(command.split("\\s+"));
Assert.assertTrue(_clusterVerifier.verify());
// make sure localhost_12918 is NOT in OFFLINE state
result = ZkTestHelper.verifyState(_gZkClient, clusterName, "TestDB0", expectStateMap, "!=");
Assert.assertTrue(result, disableNode + " should NOT be in OFFLINE");
// clean up
// wait for all zk callbacks done
controller.syncStop();
for (int i = 0; i < 5; 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 TestDisableExternalView method testDisableExternalView.
@Test
public void testDisableExternalView() throws InterruptedException {
ZKHelixDataAccessor accessor = new ZKHelixDataAccessor(CLUSTER_NAME, new ZkBaseDataAccessor<ZNRecord>(_gZkClient));
PropertyKey.Builder keyBuilder = accessor.keyBuilder();
// verify external view for TEST_DB1 does not exist
ExternalView externalView = null;
externalView = accessor.getProperty(keyBuilder.externalView(TEST_DB1));
Assert.assertNull(externalView, "There should be no external-view for " + TEST_DB1 + ", but is: " + externalView);
// verify external view for TEST_DB2 exists
externalView = accessor.getProperty(keyBuilder.externalView(TEST_DB2));
Assert.assertNotNull(externalView, "Could not find external-view for " + TEST_DB2);
// disable external view in IS
IdealState idealState = _admin.getResourceIdealState(CLUSTER_NAME, TEST_DB2);
idealState.setDisableExternalView(true);
_admin.setResourceIdealState(CLUSTER_NAME, TEST_DB2, idealState);
// touch liveinstance to trigger externalview compute stage
String instance = PARTICIPANT_PREFIX + "_" + START_PORT;
HelixProperty liveInstance = accessor.getProperty(keyBuilder.liveInstance(instance));
accessor.setProperty(keyBuilder.liveInstance(instance), liveInstance);
// verify the external view for the db got removed
for (int i = 0; i < 10; i++) {
Thread.sleep(100);
externalView = accessor.getProperty(keyBuilder.externalView(TEST_DB2));
if (externalView == null) {
break;
}
}
Assert.assertNull(externalView, "external-view for " + TEST_DB2 + " should be removed, but was: " + externalView);
}
use of org.apache.helix.manager.zk.ZKHelixDataAccessor 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.ZKHelixDataAccessor in project helix by apache.
the class ZkIntegrationTestBase method getCurrentLeader.
protected String getCurrentLeader(ZkClient zkClient, String clusterName) {
ZKHelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(zkClient));
Builder keyBuilder = accessor.keyBuilder();
LiveInstance leader = accessor.getProperty(keyBuilder.controllerLeader());
if (leader == null) {
return null;
}
return leader.getInstanceName();
}
Aggregations