use of org.apache.helix.manager.zk.ZKHelixDataAccessor in project helix by apache.
the class TestPauseSignal method testPauseSignal.
@Test()
public void testPauseSignal() throws Exception {
// Logger.getRootLogger().setLevel(Level.INFO);
String className = TestHelper.getTestClassName();
String methodName = TestHelper.getTestMethodName();
final String clusterName = className + "_" + methodName;
System.out.println("START " + clusterName + " at " + new Date(System.currentTimeMillis()));
MockParticipantManager[] participants = new MockParticipantManager[5];
// 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);
// start controller
ClusterControllerManager controller = new ClusterControllerManager(ZK_ADDR, clusterName, "controller_0");
controller.syncStart();
// 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();
}
boolean result = ClusterStateVerifier.verifyByZkCallback(new BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName));
Assert.assertTrue(result);
// pause the cluster and make sure pause is persistent
ZkClient zkClient = new ZkClient(ZK_ADDR);
zkClient.setZkSerializer(new ZNRecordSerializer());
final HelixDataAccessor tmpAccessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(zkClient));
String cmd = "-zkSvr " + ZK_ADDR + " -enableCluster " + clusterName + " false";
ClusterSetup.processCommandLineArgs(cmd.split(" "));
tmpAccessor.setProperty(tmpAccessor.keyBuilder().pause(), new PauseSignal("pause"));
zkClient.close();
// wait for controller to be signaled by pause
Thread.sleep(1000);
// add a new resource group
ClusterSetup setupTool = new ClusterSetup(ZK_ADDR);
setupTool.addResourceToCluster(clusterName, "TestDB1", 10, "MasterSlave");
setupTool.rebalanceStorageCluster(clusterName, "TestDB1", 3);
// make sure TestDB1 external view is empty
TestHelper.verifyWithTimeout("verifyEmptyCurStateAndExtView", 1000, clusterName, "TestDB1", TestHelper.<String>setOf("localhost_12918", "localhost_12919", "localhost_12920", "localhost_12921", "localhost_12922"), ZK_ADDR);
// resume controller
final HelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(_gZkClient));
cmd = "-zkSvr " + ZK_ADDR + " -enableCluster " + clusterName + " true";
ClusterSetup.processCommandLineArgs(cmd.split(" "));
result = ClusterStateVerifier.verifyByZkCallback(new BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName));
Assert.assertTrue(result);
// clean up
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 TestRebalancePipeline method testMsgTriggeredRebalance.
@Test
public void testMsgTriggeredRebalance() throws Exception {
String clusterName = "CLUSTER_" + _className + "_msgTrigger";
System.out.println("START " + clusterName + " at " + new Date(System.currentTimeMillis()));
HelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(_gZkClient));
HelixManager manager = new DummyClusterManager(clusterName, accessor);
ClusterEvent event = new ClusterEvent(ClusterEventType.Unknown);
refreshClusterConfig(clusterName, accessor);
final String resourceName = "testResource_dup";
String[] resourceGroups = new String[] { resourceName };
TestHelper.setupEmptyCluster(_gZkClient, clusterName);
// ideal state: node0 is MASTER, node1 is SLAVE
// replica=2 means 1 master and 1 slave
setupIdealState(clusterName, new int[] { 0, 1 }, resourceGroups, 1, 2);
setupStateModel(clusterName);
setupInstances(clusterName, new int[] { 0, 1 });
setupLiveInstances(clusterName, new int[] { 0, 1 });
ClusterControllerManager controller = new ClusterControllerManager(ZK_ADDR, clusterName, "controller_0");
controller.syncStart();
// round1: controller sends O->S to both node0 and node1
Thread.sleep(1000);
Builder keyBuilder = accessor.keyBuilder();
List<String> messages = accessor.getChildNames(keyBuilder.messages("localhost_0"));
Assert.assertEquals(messages.size(), 1);
messages = accessor.getChildNames(keyBuilder.messages("localhost_1"));
Assert.assertEquals(messages.size(), 1);
// round2: node0 and node1 update current states but not removing messages
// Since controller's rebalancer pipeline will GC pending messages after timeout, and both hosts
// update current states to SLAVE, controller will send out rebalance message to
// have one host to become master
setCurrentState(clusterName, "localhost_0", resourceName, resourceName + "_0", "session_0", "SLAVE", true);
setCurrentState(clusterName, "localhost_1", resourceName, resourceName + "_0", "session_1", "SLAVE", true);
// Controller has 3s timeout, so after 1s, controller should not have GCed message
Thread.sleep(1000);
Assert.assertEquals(accessor.getChildValues(keyBuilder.messages("localhost_0")).size(), 1);
Assert.assertEquals(accessor.getChildValues(keyBuilder.messages("localhost_1")).size(), 1);
// After another 2 second, controller should cleanup messages and continue to rebalance
Thread.sleep(3000);
// Manually trigger another rebalance by touching current state
setCurrentState(clusterName, "localhost_0", resourceName, resourceName + "_0", "session_0", "SLAVE");
Thread.sleep(1000);
List<Message> host0Msg = accessor.getChildValues(keyBuilder.messages("localhost_0"));
List<Message> host1Msg = accessor.getChildValues(keyBuilder.messages("localhost_1"));
List<Message> allMsgs = new ArrayList<>(host0Msg);
allMsgs.addAll(host1Msg);
Assert.assertEquals(allMsgs.size(), 1);
Assert.assertEquals(allMsgs.get(0).getToState(), "MASTER");
Assert.assertEquals(allMsgs.get(0).getFromState(), "SLAVE");
// round3: node0 changes state to master, but failed to delete message,
// controller will clean it up
setCurrentState(clusterName, "localhost_0", resourceName, resourceName + "_0", "session_0", "MASTER", true);
Thread.sleep(3500);
// touch current state to trigger rebalance
setCurrentState(clusterName, "localhost_0", resourceName, resourceName + "_0", "session_0", "MASTER", false);
Thread.sleep(1000);
Assert.assertTrue(accessor.getChildNames(keyBuilder.messages("localhost_0")).isEmpty());
// round4: node0 has duplicated but valid message, i.e. there is a P2P message sent to it
// due to error in the triggered pipeline, controller should remove duplicated message
// immediately as the partition has became master 3 sec ago (there is already a timeout)
Message sourceMsg = allMsgs.get(0);
Message dupMsg = new Message(sourceMsg.getMsgType(), UUID.randomUUID().toString());
dupMsg.getRecord().setSimpleFields(sourceMsg.getRecord().getSimpleFields());
dupMsg.getRecord().setListFields(sourceMsg.getRecord().getListFields());
dupMsg.getRecord().setMapFields(sourceMsg.getRecord().getMapFields());
accessor.setProperty(dupMsg.getKey(accessor.keyBuilder(), dupMsg.getTgtName()), dupMsg);
Thread.sleep(1000);
messages = accessor.getChildNames(keyBuilder.messages("localhost_0"));
Assert.assertTrue(messages.isEmpty());
// round5: node0 has completely invalid message, controller should immediately delete it
dupMsg.setFromState("SLAVE");
dupMsg.setToState("OFFLINE");
accessor.setProperty(dupMsg.getKey(accessor.keyBuilder(), dupMsg.getTgtName()), dupMsg);
Thread.sleep(1000);
messages = accessor.getChildNames(keyBuilder.messages("localhost_0"));
Assert.assertTrue(messages.isEmpty());
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 TestRebalancePipeline method testDuplicateMsg.
@Test
public void testDuplicateMsg() {
String clusterName = "CLUSTER_" + _className + "_dup";
System.out.println("START " + clusterName + " at " + new Date(System.currentTimeMillis()));
HelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(_gZkClient));
refreshClusterConfig(clusterName, accessor);
HelixManager manager = new DummyClusterManager(clusterName, accessor);
ClusterEvent event = new ClusterEvent(ClusterEventType.Unknown);
event.addAttribute(AttributeName.helixmanager.name(), manager);
final String resourceName = "testResource_dup";
String[] resourceGroups = new String[] { resourceName };
// ideal state: node0 is MASTER, node1 is SLAVE
// replica=2 means 1 master and 1 slave
setupIdealState(clusterName, new int[] { 0 }, resourceGroups, 1, 1);
setupLiveInstances(clusterName, new int[] { 0 });
setupStateModel(clusterName);
// cluster data cache refresh pipeline
Pipeline dataRefresh = new Pipeline();
dataRefresh.addStage(new ReadClusterDataStage());
// rebalance pipeline
Pipeline rebalancePipeline = new Pipeline();
rebalancePipeline.addStage(new ResourceComputationStage());
rebalancePipeline.addStage(new CurrentStateComputationStage());
rebalancePipeline.addStage(new BestPossibleStateCalcStage());
rebalancePipeline.addStage(new IntermediateStateCalcStage());
rebalancePipeline.addStage(new MessageGenerationPhase());
rebalancePipeline.addStage(new MessageSelectionStage());
rebalancePipeline.addStage(new MessageThrottleStage());
rebalancePipeline.addStage(new TaskAssignmentStage());
// round1: set node0 currentState to OFFLINE
setCurrentState(clusterName, "localhost_0", resourceName, resourceName + "_0", "session_0", "OFFLINE");
runPipeline(event, dataRefresh);
runPipeline(event, rebalancePipeline);
MessageSelectionStageOutput msgSelOutput = event.getAttribute(AttributeName.MESSAGES_SELECTED.name());
List<Message> messages = msgSelOutput.getMessages(resourceName, new Partition(resourceName + "_0"));
Assert.assertEquals(messages.size(), 1, "Should output 1 message: OFFLINE-SLAVE for node0");
Message message = messages.get(0);
Assert.assertEquals(message.getFromState(), "OFFLINE");
Assert.assertEquals(message.getToState(), "SLAVE");
Assert.assertEquals(message.getTgtName(), "localhost_0");
// round2: updates node0 currentState to SLAVE but keep the
// message, make sure controller should not send S->M until removal is done
setCurrentState(clusterName, "localhost_0", resourceName, resourceName + "_0", "session_1", "SLAVE");
runPipeline(event, dataRefresh);
refreshClusterConfig(clusterName, accessor);
runPipeline(event, rebalancePipeline);
msgSelOutput = event.getAttribute(AttributeName.MESSAGES_SELECTED.name());
messages = msgSelOutput.getMessages(resourceName, new Partition(resourceName + "_0"));
Assert.assertEquals(messages.size(), 0, "Should NOT output 1 message: SLAVE-MASTER for node1");
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 TestStateTransitionThrottle method testTransitionThrottleOnErrorPartition.
@Test
public void testTransitionThrottleOnErrorPartition() throws Exception {
String clusterName = getShortClassName() + "testMaxErrorPartition";
MockParticipantManager[] participants = new MockParticipantManager[participantCount];
System.out.println("START " + clusterName + " at " + new Date(System.currentTimeMillis()));
final ZKHelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(_gZkClient));
setupCluster(clusterName, accessor);
// Set throttle config to enable throttling
PropertyKey.Builder keyBuilder = accessor.keyBuilder();
ClusterConfig clusterConfig = accessor.getProperty(accessor.keyBuilder().clusterConfig());
clusterConfig.setResourcePriorityField("Name");
List<StateTransitionThrottleConfig> throttleConfigs = new ArrayList<>();
throttleConfigs.add(new StateTransitionThrottleConfig(StateTransitionThrottleConfig.RebalanceType.LOAD_BALANCE, StateTransitionThrottleConfig.ThrottleScope.CLUSTER, 100));
throttleConfigs.add(new StateTransitionThrottleConfig(StateTransitionThrottleConfig.RebalanceType.RECOVERY_BALANCE, StateTransitionThrottleConfig.ThrottleScope.CLUSTER, 100));
clusterConfig.setStateTransitionThrottleConfigs(throttleConfigs);
accessor.setProperty(keyBuilder.clusterConfig(), clusterConfig);
// set one partition to be always Error, so load balance won't be triggered
Map<String, Set<String>> errPartitions = new HashMap<>();
errPartitions.put("OFFLINE-SLAVE", TestHelper.setOf(resourceName + "_0"));
// start part of participants
for (int i = 0; i < participantCount - 1; i++) {
participants[i] = new MockParticipantManager(ZK_ADDR, clusterName, "localhost_" + (12918 + i));
if (i == 0) {
participants[i].setTransition(new ErrTransition(errPartitions));
}
participants[i].syncStart();
}
ClusterControllerManager controller = new ClusterControllerManager(ZK_ADDR, clusterName, "controller_0");
controller.syncStart();
BestPossibleExternalViewVerifier verifier = new BestPossibleExternalViewVerifier.Builder(clusterName).setZkClient(_gZkClient).build();
Assert.assertTrue(verifier.verify(3000));
// Adding one more participant.
participants[participantCount - 1] = new MockParticipantManager(ZK_ADDR, clusterName, "localhost_" + (12918 + participantCount - 1));
participants[participantCount - 1].syncStart();
// Since error partition exists, no load balance transition will be done
Assert.assertFalse(pollForPartitionAssignment(accessor, participants[participantCount - 1], resourceName, 5000));
// Update cluster config to tolerate error partition, so load balance transition will be done
clusterConfig = accessor.getProperty(accessor.keyBuilder().clusterConfig());
clusterConfig.setErrorPartitionThresholdForLoadBalance(1);
accessor.setProperty(keyBuilder.clusterConfig(), clusterConfig);
_gSetupTool.rebalanceResource(clusterName, resourceName, 3);
Assert.assertTrue(pollForPartitionAssignment(accessor, participants[participantCount - 1], resourceName, 3000));
// clean up
controller.syncStop();
for (int i = 0; i < participantCount; 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 TestAddStateModelFactoryAfterConnect method testBasic.
@Test
public void testBasic() throws Exception {
// Logger.getRootLogger().setLevel(Level.INFO);
String className = TestHelper.getTestClassName();
String methodName = TestHelper.getTestMethodName();
String clusterName = className + "_" + methodName;
final int n = 5;
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
10, // number of nodes
n, // replicas
3, "MasterSlave", // do rebalance
true);
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();
}
boolean result = ClusterStateVerifier.verifyByZkCallback(new BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName));
Assert.assertTrue(result);
// add a new idealState without registering message handling factory
ClusterSetup setupTool = new ClusterSetup(ZK_ADDR);
setupTool.addResourceToCluster(clusterName, "TestDB1", 16, "MasterSlave");
ZkBaseDataAccessor<ZNRecord> baseAccessor = new ZkBaseDataAccessor<ZNRecord>(_gZkClient);
ZKHelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, baseAccessor);
Builder keyBuilder = accessor.keyBuilder();
IdealState idealState = accessor.getProperty(keyBuilder.idealStates("TestDB1"));
idealState.setStateModelFactoryName("TestDB1_Factory");
accessor.setProperty(keyBuilder.idealStates("TestDB1"), idealState);
setupTool.rebalanceStorageCluster(clusterName, "TestDB1", 3);
// assert that we have received OFFLINE->SLAVE messages for all partitions
int totalMsgs = 0;
for (int retry = 0; retry < 5; retry++) {
Thread.sleep(100);
totalMsgs = 0;
for (int i = 0; i < n; i++) {
List<Message> msgs = accessor.getChildValues(keyBuilder.messages(participants[i].getInstanceName()));
totalMsgs += msgs.size();
}
if (// partition# x replicas
totalMsgs == 48)
break;
}
Assert.assertEquals(totalMsgs, 48, "Should accumulated 48 unprocessed messages (1 O->S per partition per replica) because TestDB1 is added without state-model-factory but was " + totalMsgs);
// Logger.getRootLogger().setLevel(Level.INFO);
for (int i = 0; i < n; i++) {
participants[i].getStateMachineEngine().registerStateModelFactory("MasterSlave", new MockMSModelFactory(), "TestDB1_Factory");
}
result = ClusterStateVerifier.verifyByZkCallback(new BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName));
Assert.assertTrue(result);
// 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()));
}
Aggregations