use of org.apache.helix.integration.manager.MockParticipantManager in project helix by apache.
the class TestJobQueuesResource method test.
@Test
public void test() throws Exception {
String className = TestHelper.getTestClassName();
String methodName = TestHelper.getTestMethodName();
String clusterName = className + "_" + methodName;
final int n = 5;
final int p = 20;
final int r = 3;
System.out.println("START " + clusterName + " at " + new Date(System.currentTimeMillis()));
_gSetupTool.addCluster(clusterName, true);
for (int i = 0; i < n; i++) {
String instanceName = "localhost_" + (12918 + i);
_gSetupTool.addInstanceToCluster(clusterName, instanceName);
}
// Set up target db
_gSetupTool.addResourceToCluster(clusterName, WorkflowGenerator.DEFAULT_TGT_DB, p, "MasterSlave");
_gSetupTool.rebalanceStorageCluster(clusterName, WorkflowGenerator.DEFAULT_TGT_DB, r);
Map<String, TaskFactory> taskFactoryReg = new HashMap<String, TaskFactory>();
taskFactoryReg.put("DummyTask", new TaskFactory() {
@Override
public Task createNewTask(TaskCallbackContext context) {
return new MockTask(context);
}
});
// Start dummy 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);
// Register a Task state model factory.
StateMachineEngine stateMachine = participants[i].getStateMachineEngine();
stateMachine.registerStateModelFactory("Task", new TaskStateModelFactory(participants[i], taskFactoryReg));
participants[i].syncStart();
}
// start controller
String controllerName = "controller";
ClusterControllerManager controller = new ClusterControllerManager(ZK_ADDR, clusterName, controllerName);
controller.syncStart();
boolean result = ClusterStateVerifier.verifyByZkCallback(new ClusterStateVerifier.BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName));
Assert.assertTrue(result);
// Start a queue
String queueName = "myQueue1";
LOG.info("Starting job-queue: " + queueName);
String jobQueueYamlConfig = "name: " + queueName;
String resourceUrl = "http://localhost:" + ADMIN_PORT + "/clusters/" + clusterName + "/jobQueues";
ZNRecord postRet = AdminTestHelper.post(_gClient, resourceUrl, jobQueueYamlConfig);
LOG.info("Started job-queue: " + queueName + ", ret: " + postRet);
LOG.info("Getting all job-queues");
ZNRecord getRet = AdminTestHelper.get(_gClient, resourceUrl);
LOG.info("Got job-queues: " + getRet);
// Enqueue job
resourceUrl = "http://localhost:" + ADMIN_PORT + "/clusters/" + clusterName + "/jobQueues/" + queueName;
WorkflowBean wfBean = new WorkflowBean();
wfBean.name = queueName;
JobBean jBean1 = new JobBean();
jBean1.name = "myJob1";
jBean1.command = "DummyTask";
jBean1.targetResource = WorkflowGenerator.DEFAULT_TGT_DB;
jBean1.targetPartitionStates = Lists.newArrayList("MASTER");
JobBean jBean2 = new JobBean();
jBean2.name = "myJob2";
jBean2.command = "DummyTask";
jBean2.targetResource = WorkflowGenerator.DEFAULT_TGT_DB;
jBean2.targetPartitionStates = Lists.newArrayList("SLAVE");
wfBean.jobs = Lists.newArrayList(jBean1, jBean2);
String jobYamlConfig = new Yaml().dump(wfBean);
LOG.info("Enqueuing jobs: " + jobQueueYamlConfig);
Map<String, String> paraMap = new HashMap<String, String>();
paraMap.put(JsonParameters.MANAGEMENT_COMMAND, TaskDriver.DriverCommand.start.toString());
String postBody = String.format("%s=%s&%s=%s", JsonParameters.JSON_PARAMETERS, ClusterRepresentationUtil.ObjectToJson(paraMap), ResourceUtil.YamlParamKey.NEW_JOB.toString(), jobYamlConfig);
postRet = AdminTestHelper.post(_gClient, resourceUrl, postBody);
LOG.info("Enqueued job, ret: " + postRet);
// Get job
resourceUrl = "http://localhost:" + ADMIN_PORT + "/clusters/" + clusterName + "/jobQueues/" + queueName + "/" + jBean1.name;
getRet = AdminTestHelper.get(_gClient, resourceUrl);
LOG.info("Got job: " + getRet);
// Stop job queue
resourceUrl = "http://localhost:" + ADMIN_PORT + "/clusters/" + clusterName + "/jobQueues/" + queueName;
paraMap.put(JsonParameters.MANAGEMENT_COMMAND, TaskDriver.DriverCommand.stop.toString());
postBody = String.format("%s=%s", JsonParameters.JSON_PARAMETERS, ClusterRepresentationUtil.ObjectToJson(paraMap));
postRet = AdminTestHelper.post(_gClient, resourceUrl, postBody);
LOG.info("Stopped job-queue, ret: " + postRet);
// Delete a job
resourceUrl = "http://localhost:" + ADMIN_PORT + "/clusters/" + clusterName + "/jobQueues/" + queueName + "/" + jBean2.name;
AdminTestHelper.delete(_gClient, resourceUrl);
LOG.info("Delete a job: ");
// Resume job queue
resourceUrl = "http://localhost:" + ADMIN_PORT + "/clusters/" + clusterName + "/jobQueues/" + queueName;
paraMap.put(JsonParameters.MANAGEMENT_COMMAND, TaskDriver.DriverCommand.resume.toString());
postBody = String.format("%s=%s", JsonParameters.JSON_PARAMETERS, ClusterRepresentationUtil.ObjectToJson(paraMap));
postRet = AdminTestHelper.post(_gClient, resourceUrl, postBody);
LOG.info("Resumed job-queue, ret: " + postRet);
// Flush job queue
paraMap.put(JsonParameters.MANAGEMENT_COMMAND, "flush");
postBody = JsonParameters.JSON_PARAMETERS + "=" + ClusterRepresentationUtil.ObjectToJson(paraMap);
postRet = AdminTestHelper.post(_gClient, resourceUrl, postBody);
LOG.info("Flushed job-queue, ret: " + postRet);
// clean up
controller.syncStop();
for (int i = 0; i < n; i++) {
if (participants[i] != null && participants[i].isConnected()) {
participants[i].syncStop();
}
}
System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis()));
}
use of org.apache.helix.integration.manager.MockParticipantManager in project helix by apache.
the class AbstractTestClass method startInstances.
protected Set<String> startInstances(String cluster, Set<String> instances, int numLiveinstances) {
Set<String> liveInstances = new HashSet<>();
int i = 0;
for (String instance : instances) {
MockParticipantManager participant = new MockParticipantManager(ZK_ADDR, cluster, instance);
participant.syncStart();
liveInstances.add(instance);
if (++i > numLiveinstances) {
break;
}
}
return liveInstances;
}
use of org.apache.helix.integration.manager.MockParticipantManager in project helix by apache.
the class TestClusterVerifier method testSleepTransition.
@Test
public void testSleepTransition() throws InterruptedException {
HelixClusterVerifier bestPossibleVerifier = new BestPossibleExternalViewVerifier.Builder(_clusterName).setZkClient(_gZkClient).build();
Assert.assertTrue(bestPossibleVerifier.verify(10000));
HelixClusterVerifier strictMatchVerifier = new StrictMatchExternalViewVerifier.Builder(_clusterName).setZkClient(_gZkClient).build();
Assert.assertTrue(strictMatchVerifier.verify(10000));
// Re-start a new participant with sleeping transition(all state model transition cannot finish)
_participants[0].syncStop();
Thread.sleep(1000);
_participants[0] = new MockParticipantManager(ZK_ADDR, _clusterName, _participants[0].getInstanceName());
_participants[0].setTransition(new SleepTransition(99999999));
_participants[0].syncStart();
// The new participant causes rebalance, but the state transitions are all stuck
Thread.sleep(1000);
Assert.assertFalse(strictMatchVerifier.verify(3000));
}
use of org.apache.helix.integration.manager.MockParticipantManager in project helix by apache.
the class TestHelixAdminCli method testDropAddResource.
@Test
public void testDropAddResource() throws Exception {
String className = TestHelper.getTestClassName();
String methodName = TestHelper.getTestMethodName();
String clusterName = className + "_" + methodName;
String grandClusterName = clusterName + "_grand";
final int n = 6;
System.out.println("START " + clusterName + " at " + new Date(System.currentTimeMillis()));
MockParticipantManager[] participants = new MockParticipantManager[n];
ClusterDistributedController[] controllers = new ClusterDistributedController[2];
setupCluster(clusterName, grandClusterName, n, participants, controllers);
String command = "-zkSvr " + ZK_ADDR + " -activateCluster " + clusterName + " " + grandClusterName + " true";
ClusterSetup.processCommandLineArgs(command.split("\\s+"));
Thread.sleep(500);
// save ideal state
BaseDataAccessor<ZNRecord> baseAccessor = new ZkBaseDataAccessor<ZNRecord>(_gZkClient);
HelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, baseAccessor);
IdealState idealState = accessor.getProperty(accessor.keyBuilder().idealStates("db_11"));
ZNRecordJsonSerializer serializer = new ZNRecordJsonSerializer();
String tmpDir = System.getProperty("java.io.tmpdir");
if (tmpDir == null) {
tmpDir = "/tmp";
}
final String tmpIdealStateFile = tmpDir + "/" + clusterName + "_idealState.log";
FileWriter fos = new FileWriter(tmpIdealStateFile);
PrintWriter pw = new PrintWriter(fos);
pw.write(new String(serializer.serialize(idealState.getRecord())));
pw.close();
command = "-zkSvr " + ZK_ADDR + " -dropResource " + clusterName + " db_11 ";
ClusterSetup.processCommandLineArgs(command.split("\\s+"));
boolean verifyResult = ClusterStateVerifier.verifyByZkCallback(new BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName));
Assert.assertTrue(verifyResult);
command = "-zkSvr " + ZK_ADDR + " -addIdealState " + clusterName + " db_11 " + tmpIdealStateFile;
ClusterSetup.processCommandLineArgs(command.split("\\s+"));
verifyResult = ClusterStateVerifier.verifyByZkCallback(new BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName));
Assert.assertTrue(verifyResult);
IdealState idealState2 = accessor.getProperty(accessor.keyBuilder().idealStates("db_11"));
Assert.assertTrue(idealState2.getRecord().equals(idealState.getRecord()));
// clean up
for (int i = 0; i < controllers.length; i++) {
controllers[i].syncStop();
}
for (int i = 0; i < participants.length; i++) {
participants[i].syncStop();
}
System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis()));
}
use of org.apache.helix.integration.manager.MockParticipantManager in project helix by apache.
the class TestHelixAdminCli method testInstanceOperations.
@Test
public void testInstanceOperations() throws Exception {
String className = TestHelper.getTestClassName();
String methodName = TestHelper.getTestMethodName();
String clusterName = className + "_" + methodName;
String grandClusterName = clusterName + "_grand";
final int n = 6;
System.out.println("START " + clusterName + " at " + new Date(System.currentTimeMillis()));
MockParticipantManager[] participants = new MockParticipantManager[n];
ClusterDistributedController[] controllers = new ClusterDistributedController[2];
setupCluster(clusterName, grandClusterName, n, participants, controllers);
String command = "-zkSvr " + ZK_ADDR + " -activateCluster " + clusterName + " " + grandClusterName + " true";
ClusterSetup.processCommandLineArgs(command.split("\\s+"));
Thread.sleep(500);
// drop node should fail if the node is not disabled
command = "-zkSvr " + ZK_ADDR + " -dropNode " + clusterName + " localhost:1232";
try {
ClusterSetup.processCommandLineArgs(command.split("\\s+"));
Assert.fail("dropNode should fail since the node is not disabled");
} catch (Exception e) {
// OK
}
// disabled node
command = "-zkSvr " + ZK_ADDR + " -enableInstance " + clusterName + " localhost:1232 false";
ClusterSetup.processCommandLineArgs(command.split("\\s+"));
// Cannot dropNode if the node is not disconnected
command = "-zkSvr " + ZK_ADDR + " -dropNode " + clusterName + " localhost:1232";
try {
ClusterSetup.processCommandLineArgs(command.split("\\s+"));
Assert.fail("dropNode should fail since the node is not disconnected");
} catch (Exception e) {
// OK
}
// Cannot swapNode if the node is not disconnected
command = "-zkSvr " + ZK_ADDR + " -swapInstance " + clusterName + " localhost_1232 localhost_12320";
try {
ClusterSetup.processCommandLineArgs(command.split("\\s+"));
Assert.fail("swapInstance should fail since the node is not disconnected");
} catch (Exception e) {
// OK
}
// disconnect localhost_1232
participants[2].syncStop();
// add new node then swap instance
command = "-zkSvr " + ZK_ADDR + " -addNode " + clusterName + " localhost:12320";
ClusterSetup.processCommandLineArgs(command.split("\\s+"));
// swap instance. The instance get swapped out should not exist anymore
command = "-zkSvr " + ZK_ADDR + " -swapInstance " + clusterName + " localhost_1232 localhost_12320";
ClusterSetup.processCommandLineArgs(command.split("\\s+"));
BaseDataAccessor<ZNRecord> baseAccessor = new ZkBaseDataAccessor<ZNRecord>(_gZkClient);
HelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, baseAccessor);
String path = accessor.keyBuilder().instanceConfig("localhost_1232").getPath();
Assert.assertFalse(_gZkClient.exists(path), path + " should not exist since localhost_1232 has been swapped by localhost_12320");
// clean up
for (int i = 0; i < controllers.length; i++) {
controllers[i].syncStop();
}
for (int i = 0; i < participants.length; i++) {
participants[i].syncStop();
}
System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis()));
}
Aggregations