use of org.apache.helix.NotificationContext in project helix by apache.
the class TestDistControllerStateModel method testReset.
@Test()
public void testReset() {
Message message = new Message(MessageType.STATE_TRANSITION, "0");
message.setPartitionName(clusterName);
message.setTgtName("controller_0");
try {
stateModel.onBecomeLeaderFromStandby(message, new NotificationContext(null));
} catch (Exception e) {
LOG.error("Exception becoming leader from standby", e);
}
stateModel.reset();
}
use of org.apache.helix.NotificationContext in project helix by apache.
the class ScheduledTaskStateModel method onBecomeCompletedFromOffline.
@Transition(to = "COMPLETED", from = "OFFLINE")
public void onBecomeCompletedFromOffline(Message message, NotificationContext context) throws InterruptedException {
logger.info(_partitionKey + " onBecomeCompletedFromOffline");
// Construct the inner task message from the mapfields of scheduledTaskQueue resource group
Map<String, String> messageInfo = message.getRecord().getMapField(Message.Attributes.INNER_MESSAGE.toString());
ZNRecord record = new ZNRecord(_partitionKey);
record.getSimpleFields().putAll(messageInfo);
Message taskMessage = new Message(record);
if (logger.isDebugEnabled()) {
logger.debug(taskMessage.getRecord().getSimpleFields().toString());
}
MessageHandler handler = _executor.createMessageHandler(taskMessage, new NotificationContext(null));
if (handler == null) {
throw new HelixException("Task message " + taskMessage.getMsgType() + " handler not found, task id " + _partitionKey);
}
// Invoke the internal handler to complete the task
handler.handleMessage();
logger.info(_partitionKey + " onBecomeCompletedFromOffline completed");
}
use of org.apache.helix.NotificationContext in project helix by apache.
the class RoutingTableProvider method shutdown.
/**
* Shutdown current RoutingTableProvider. Once it is shutdown, it should never be reused.
*/
public void shutdown() {
_routerUpdater.shutdown();
if (_helixManager != null) {
PropertyKey.Builder keyBuilder = _helixManager.getHelixDataAccessor().keyBuilder();
switch(_sourceDataType) {
case EXTERNALVIEW:
_helixManager.removeListener(keyBuilder.externalViews(), this);
break;
case TARGETEXTERNALVIEW:
_helixManager.removeListener(keyBuilder.targetExternalViews(), this);
break;
case CURRENTSTATES:
NotificationContext context = new NotificationContext(_helixManager);
context.setType(NotificationContext.Type.FINALIZE);
updateCurrentStatesListeners(Collections.<LiveInstance>emptyList(), context);
break;
default:
break;
}
}
}
use of org.apache.helix.NotificationContext 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.NotificationContext in project helix by apache.
the class TestDefaultControllerMsgHandlerFactory method testDefaultControllerMsgHandlerFactory.
@Test()
public void testDefaultControllerMsgHandlerFactory() {
System.out.println("START TestDefaultControllerMsgHandlerFactory at " + new Date(System.currentTimeMillis()));
DefaultControllerMessageHandlerFactory facotry = new DefaultControllerMessageHandlerFactory();
Message message = new Message(MessageType.NO_OP, "0");
NotificationContext context = new NotificationContext(null);
boolean exceptionCaught = false;
try {
MessageHandler handler = facotry.createHandler(message, context);
} catch (HelixException e) {
exceptionCaught = true;
}
AssertJUnit.assertTrue(exceptionCaught);
message = new Message(MessageType.CONTROLLER_MSG, "1");
exceptionCaught = false;
try {
MessageHandler handler = facotry.createHandler(message, context);
} catch (HelixException e) {
exceptionCaught = true;
}
AssertJUnit.assertFalse(exceptionCaught);
Map<String, String> resultMap = new HashMap<String, String>();
message = new Message(MessageType.NO_OP, "3");
DefaultControllerMessageHandler defaultHandler = new DefaultControllerMessageHandler(message, context);
try {
defaultHandler.handleMessage();
} catch (HelixException e) {
exceptionCaught = true;
} catch (InterruptedException e) {
LOG.error("Interrupted handling message", e);
}
AssertJUnit.assertTrue(exceptionCaught);
message = new Message(MessageType.CONTROLLER_MSG, "4");
defaultHandler = new DefaultControllerMessageHandler(message, context);
exceptionCaught = false;
try {
defaultHandler.handleMessage();
} catch (HelixException e) {
exceptionCaught = true;
} catch (InterruptedException e) {
LOG.error("Interrupted handling message", e);
}
AssertJUnit.assertFalse(exceptionCaught);
System.out.println("END TestDefaultControllerMsgHandlerFactory at " + new Date(System.currentTimeMillis()));
}
Aggregations