use of org.apache.helix.HelixManager in project helix by apache.
the class TestZkHelixAdmin method initializeHelixManager.
private HelixManager initializeHelixManager(String clusterName, String instanceName, String zkAddress, String stateModelName) {
HelixManager manager = HelixManagerFactory.getZKHelixManager(clusterName, instanceName, InstanceType.PARTICIPANT, zkAddress);
MasterSlaveStateModelFactory stateModelFactory = new MasterSlaveStateModelFactory(instanceName);
StateMachineEngine stateMach = manager.getStateMachineEngine();
stateMach.registerStateModelFactory(stateModelName, stateModelFactory);
return manager;
}
use of org.apache.helix.HelixManager in project helix by apache.
the class TestZKLiveInstanceData method testDataChange.
@Test
public void testDataChange() throws Exception {
// Create an admin and add LiveInstanceChange listener to it
HelixManager adminManager = HelixManagerFactory.getZKHelixManager(clusterName, null, InstanceType.ADMINISTRATOR, ZK_ADDR);
adminManager.connect();
final BlockingQueue<List<LiveInstance>> changeList = new LinkedBlockingQueue<List<LiveInstance>>();
adminManager.addLiveInstanceChangeListener(new LiveInstanceChangeListener() {
@Override
public void onLiveInstanceChange(List<LiveInstance> liveInstances, NotificationContext changeContext) {
// The queue is basically unbounded, so shouldn't throw exception when calling
// "add".
changeList.add(deepCopy(liveInstances));
}
});
// Check the initial condition
List<LiveInstance> instances = changeList.poll(1, TimeUnit.SECONDS);
Assert.assertNotNull(instances, "Expecting a list of live instance");
Assert.assertTrue(instances.isEmpty(), "Expecting an empty list of live instance");
// Join as participant, should trigger a live instance change event
HelixManager manager = HelixManagerFactory.getZKHelixManager(clusterName, "localhost_54321", InstanceType.PARTICIPANT, ZK_ADDR);
manager.connect();
instances = changeList.poll(1, TimeUnit.SECONDS);
Assert.assertNotNull(instances, "Expecting a list of live instance");
Assert.assertEquals(instances.size(), 1, "Expecting one live instance");
Assert.assertEquals(instances.get(0).getInstanceName(), manager.getInstanceName());
// Update data in the live instance node, should trigger another live instance change
// event
HelixDataAccessor helixDataAccessor = manager.getHelixDataAccessor();
PropertyKey propertyKey = helixDataAccessor.keyBuilder().liveInstance(manager.getInstanceName());
LiveInstance instance = helixDataAccessor.getProperty(propertyKey);
Map<String, String> map = new TreeMap<String, String>();
map.put("k1", "v1");
instance.getRecord().setMapField("test", map);
Assert.assertTrue(helixDataAccessor.updateProperty(propertyKey, instance), "Failed to update live instance node");
instances = changeList.poll(1, TimeUnit.SECONDS);
Assert.assertNotNull(instances, "Expecting a list of live instance");
Assert.assertEquals(instances.get(0).getRecord().getMapField("test"), map, "Wrong map data.");
manager.disconnect();
// wait for callback finish
Thread.sleep(1000);
instances = changeList.poll(1, TimeUnit.SECONDS);
Assert.assertNotNull(instances, "Expecting a list of live instance");
Assert.assertTrue(instances.isEmpty(), "Expecting an empty list of live instance");
adminManager.disconnect();
}
use of org.apache.helix.HelixManager in project helix by apache.
the class TestDefaultMessagingService method TestMessageSend.
@Test()
public void TestMessageSend() {
HelixManager manager = new MockHelixManager();
DefaultMessagingService svc = new DefaultMessagingService(manager);
TestMessageHandlerFactory factory = new TestMessageHandlerFactory();
svc.registerMessageHandlerFactory(factory.getMessageType(), factory);
Criteria recipientCriteria = new Criteria();
recipientCriteria.setInstanceName("localhost_12919");
recipientCriteria.setRecipientInstanceType(InstanceType.PARTICIPANT);
recipientCriteria.setSelfExcluded(true);
Message template = new Message(factory.getMessageType(), UUID.randomUUID().toString());
AssertJUnit.assertEquals(0, svc.send(recipientCriteria, template));
recipientCriteria.setSelfExcluded(false);
AssertJUnit.assertEquals(1, svc.send(recipientCriteria, template));
// all instances, all partitions
recipientCriteria.setSelfExcluded(false);
recipientCriteria.setInstanceName("%");
recipientCriteria.setResource("DB");
recipientCriteria.setPartition("%");
AssertJUnit.assertEquals(200, svc.send(recipientCriteria, template));
// all instances, all partitions, use * instead of %
recipientCriteria.setSelfExcluded(false);
recipientCriteria.setInstanceName("*");
recipientCriteria.setResource("DB");
recipientCriteria.setPartition("*");
AssertJUnit.assertEquals(200, svc.send(recipientCriteria, template));
// tail pattern
recipientCriteria.setSelfExcluded(false);
recipientCriteria.setInstanceName("localhost%");
recipientCriteria.setResource("DB");
recipientCriteria.setPartition("%");
AssertJUnit.assertEquals(200, svc.send(recipientCriteria, template));
// exclude this instance, send to all others for all partitions
recipientCriteria.setSelfExcluded(true);
recipientCriteria.setInstanceName("%");
recipientCriteria.setResource("DB");
recipientCriteria.setPartition("%");
AssertJUnit.assertEquals(159, svc.send(recipientCriteria, template));
// single instance, all partitions
recipientCriteria.setSelfExcluded(true);
recipientCriteria.setInstanceName("localhost_12920");
recipientCriteria.setResource("DB");
recipientCriteria.setPartition("%");
AssertJUnit.assertEquals(39, svc.send(recipientCriteria, template));
// single character wildcards
recipientCriteria.setSelfExcluded(true);
recipientCriteria.setInstanceName("l_calhost_12_20");
recipientCriteria.setResource("DB");
recipientCriteria.setPartition("%");
AssertJUnit.assertEquals(39, svc.send(recipientCriteria, template));
// head pattern
recipientCriteria.setSelfExcluded(true);
recipientCriteria.setInstanceName("%12920");
recipientCriteria.setResource("DB");
recipientCriteria.setPartition("%");
AssertJUnit.assertEquals(39, svc.send(recipientCriteria, template));
// middle pattern
recipientCriteria.setSelfExcluded(true);
recipientCriteria.setInstanceName("l%_12920");
recipientCriteria.setResource("DB");
recipientCriteria.setPartition("%");
AssertJUnit.assertEquals(39, svc.send(recipientCriteria, template));
// send to a controller
recipientCriteria.setSelfExcluded(true);
recipientCriteria.setInstanceName("localhost_12920");
recipientCriteria.setRecipientInstanceType(InstanceType.CONTROLLER);
recipientCriteria.setResource("DB");
recipientCriteria.setPartition("%");
AssertJUnit.assertEquals(1, svc.send(recipientCriteria, template));
}
use of org.apache.helix.HelixManager in project helix by apache.
the class TestResourceThreadpoolSize method setResourceThreadPoolSize.
private void setResourceThreadPoolSize(String resourceName, int threadPoolSize) {
HelixManager manager = _participants[0];
ConfigAccessor accessor = manager.getConfigAccessor();
HelixConfigScope scope = new HelixConfigScopeBuilder(HelixConfigScope.ConfigScopeProperty.RESOURCE).forCluster(manager.getClusterName()).forResource(resourceName).build();
accessor.set(scope, HelixTaskExecutor.MAX_THREADS, "" + threadPoolSize);
}
use of org.apache.helix.HelixManager in project helix by apache.
the class TestZKPathDataDumpTask method testCapacityReached.
@Test
public void testCapacityReached() throws Exception {
String className = TestHelper.getTestClassName();
String methodName = TestHelper.getTestMethodName();
String clusterName = className + "_" + methodName;
int n = 1;
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
1, "MasterSlave", // do rebalance
true);
HelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(_gZkClient));
PropertyKey.Builder keyBuilder = accessor.keyBuilder();
BaseDataAccessor<ZNRecord> baseAccessor = accessor.getBaseDataAccessor();
HelixManager manager = mock(HelixManager.class);
when(manager.getHelixDataAccessor()).thenReturn(accessor);
when(manager.getClusterName()).thenReturn(clusterName);
// run dump task without statusUpdates and errors, should not remove any existing
// statusUpdate/error paths
ZKPathDataDumpTask task = new ZKPathDataDumpTask(manager, Long.MAX_VALUE, Long.MAX_VALUE, 1);
task.run();
PropertyKey controllerStatusUpdateKey = keyBuilder.controllerTaskStatuses();
Assert.assertTrue(baseAccessor.exists(controllerStatusUpdateKey.getPath(), 0));
PropertyKey controllerErrorKey = keyBuilder.controllerTaskErrors();
Assert.assertTrue(baseAccessor.exists(controllerErrorKey.getPath(), 0));
PropertyKey statusUpdateKey = keyBuilder.stateTransitionStatus("localhost_12918");
Assert.assertTrue(baseAccessor.exists(statusUpdateKey.getPath(), 0));
PropertyKey errorKey = keyBuilder.stateTransitionErrors("localhost_12918");
Assert.assertTrue(baseAccessor.exists(errorKey.getPath(), 0));
// add participant status updates and errors
statusUpdateKey = keyBuilder.stateTransitionStatus("localhost_12918", "session_0", "TestDB0", "TestDB0_0");
accessor.setProperty(statusUpdateKey, new StatusUpdate(new ZNRecord("statusUpdate")));
errorKey = keyBuilder.stateTransitionError("localhost_12918", "session_0", "TestDB0", "TestDB0_0");
accessor.setProperty(errorKey, new Error(new ZNRecord("error")));
// add controller status updates and errors (one of each, should not trigger anything)
controllerStatusUpdateKey = keyBuilder.controllerTaskStatus("session_0", "TestDB");
accessor.setProperty(controllerStatusUpdateKey, new StatusUpdate(new ZNRecord("controllerStatusUpdate")));
controllerErrorKey = keyBuilder.controllerTaskError("TestDB_error");
accessor.setProperty(controllerErrorKey, new Error(new ZNRecord("controllerError")));
// run dump task, should not remove anything because the threshold is not exceeded
task.run();
Assert.assertTrue(baseAccessor.exists(controllerStatusUpdateKey.getPath(), 0));
Assert.assertTrue(baseAccessor.exists(controllerErrorKey.getPath(), 0));
Assert.assertTrue(baseAccessor.exists(statusUpdateKey.getPath(), 0));
Assert.assertTrue(baseAccessor.exists(errorKey.getPath(), 0));
// add a second set of all status updates and errors
statusUpdateKey = keyBuilder.stateTransitionStatus("localhost_12918", "session_0", "TestDB0", "TestDB0_1");
accessor.setProperty(statusUpdateKey, new StatusUpdate(new ZNRecord("statusUpdate")));
errorKey = keyBuilder.stateTransitionError("localhost_12918", "session_0", "TestDB0", "TestDB0_1");
accessor.setProperty(errorKey, new Error(new ZNRecord("error")));
controllerStatusUpdateKey = keyBuilder.controllerTaskStatus("session_0", "TestDB1");
accessor.setProperty(controllerStatusUpdateKey, new StatusUpdate(new ZNRecord("controllerStatusUpdate")));
controllerErrorKey = keyBuilder.controllerTaskError("TestDB1_error");
accessor.setProperty(controllerErrorKey, new Error(new ZNRecord("controllerError")));
// run dump task, should remove everything since capacities are exceeded
task.run();
Assert.assertFalse(baseAccessor.exists(controllerStatusUpdateKey.getPath(), 0));
Assert.assertFalse(baseAccessor.exists(controllerErrorKey.getPath(), 0));
Assert.assertFalse(baseAccessor.exists(statusUpdateKey.getPath(), 0));
Assert.assertFalse(baseAccessor.exists(errorKey.getPath(), 0));
}
Aggregations