Java Code Examples for org.apache.helix.NotificationContext#setChangeType()

The following examples show how to use org.apache.helix.NotificationContext#setChangeType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CallbackHandler.java    From helix with Apache License 2.0 6 votes vote down vote up
@Override
public void handleDataChange(String dataPath, Object data) {
  if (logger.isDebugEnabled()) {
    logger.debug("Data change callback: paths changed: {}", dataPath);
  }

  try {
    updateNotificationTime(System.nanoTime());
    if (dataPath != null && dataPath.startsWith(_path)) {
      NotificationContext changeContext = new NotificationContext(_manager);
      changeContext.setType(NotificationContext.Type.CALLBACK);
      changeContext.setPathChanged(dataPath);
      changeContext.setChangeType(_changeType);
      enqueueTask(changeContext);
    }
  } catch (Exception e) {
    String msg =
        "exception in handling data-change. path: " + dataPath + ", listener: " + _listener;
    ZKExceptionHandler.getInstance().handle(msg, e);
  }
}
 
Example 2
Source File: CallbackHandler.java    From helix with Apache License 2.0 6 votes vote down vote up
void reset(boolean isShutdown) {
  logger.info("Resetting CallbackHandler: {}. Is resetting for shutdown: {}.", this.toString(),
      isShutdown);
  try {
    _ready = false;
    synchronized (this) {
      if (_batchCallbackProcessor != null) {
        if (isShutdown) {
          _batchCallbackProcessor.shutdown();
          _batchCallbackProcessor = null;
        } else {
          _batchCallbackProcessor.resetEventQueue();
        }
      }
    }
    NotificationContext changeContext = new NotificationContext(_manager);
    changeContext.setType(NotificationContext.Type.FINALIZE);
    changeContext.setChangeType(_changeType);
    invoke(changeContext);
  } catch (Exception e) {
    String msg = "Exception while resetting the listener:" + _listener;
    ZKExceptionHandler.getInstance().handle(msg, e);
  }
}
 
Example 3
Source File: CallbackHandler.java    From helix with Apache License 2.0 5 votes vote down vote up
/**
 * Invoke the listener so that it sets up the initial values from the zookeeper if any
 * exists
 */
public void init() {
  logger.info("initializing CallbackHandler: {}, content: {} ", this.toString(), getContent());

  if (_batchModeEnabled) {
    synchronized (this) {
      if (_batchCallbackProcessor != null) {
        _batchCallbackProcessor.resetEventQueue();
      } else {
        _batchCallbackProcessor = new CallbackProcessor(this);
        _batchCallbackProcessor.start();
      }
    }
  }

  updateNotificationTime(System.nanoTime());
  try {
    NotificationContext changeContext = new NotificationContext(_manager);
    changeContext.setType(NotificationContext.Type.INIT);
    changeContext.setChangeType(_changeType);
    _ready = true;
    invoke(changeContext);
  } catch (Exception e) {
    String msg = "Exception while invoking init callback for listener:" + _listener;
    ZKExceptionHandler.getInstance().handle(msg, e);
  }
}
 
Example 4
Source File: CallbackHandler.java    From helix with Apache License 2.0 5 votes vote down vote up
@Override
public void handleChildChange(String parentPath, List<String> currentChilds) {
  if (logger.isDebugEnabled()) {
    logger.debug("Data change callback: child changed, path: {} , current child count: {}",
        parentPath, currentChilds == null ? 0 : currentChilds.size());
  }

  try {
    updateNotificationTime(System.nanoTime());
    if (parentPath != null && parentPath.startsWith(_path)) {
      if (currentChilds == null && parentPath.equals(_path)) {
        // _path has been removed, remove this listener
        // removeListener will call handler.reset(), which in turn call invoke() on FINALIZE type
        _manager.removeListener(_propertyKey, _listener);
      } else {
        if (!isReady()) {
          // avoid leaking CallbackHandler
          logger.info("Callbackhandler {} with path {} is in reset state. Stop subscription to ZK client to avoid leaking",
              this, parentPath);
          return;
        }
        NotificationContext changeContext = new NotificationContext(_manager);
        changeContext.setType(NotificationContext.Type.CALLBACK);
        changeContext.setPathChanged(parentPath);
        changeContext.setChangeType(_changeType);
        subscribeForChanges(changeContext.getType(), _path, _watchChild);
        enqueueTask(changeContext);
      }
    }
  } catch (Exception e) {
    String msg = "exception in handling child-change. instance: " + _manager.getInstanceName()
        + ", parentPath: " + parentPath + ", listener: " + _listener;
    ZKExceptionHandler.getInstance().handle(msg, e);
  }
}
 
Example 5
Source File: TestHelixTaskExecutor.java    From helix with Apache License 2.0 5 votes vote down vote up
@Test()
public void testNoRetry() throws InterruptedException {
  // String p = "test_";
  // System.out.println(p.substring(p.lastIndexOf('_')+1));
  HelixTaskExecutor executor = new HelixTaskExecutor();
  HelixManager manager = new MockClusterManager();

  CancellableHandlerFactory factory = new CancellableHandlerFactory();
  for (String type : factory.getMessageTypes()) {
    executor.registerMessageHandlerFactory(type, factory);
  }
  NotificationContext changeContext = new NotificationContext(manager);

  List<Message> msgList = new ArrayList<Message>();
  int nMsgs2 = 4;
  // Test the case in which retry = 0
  for (int i = 0; i < nMsgs2; i++) {
    Message msg = new Message(factory.getMessageTypes().get(0), UUID.randomUUID().toString());
    msg.setTgtSessionId("*");
    msg.setTgtName("Localhost_1123");
    msg.setSrcName("127.101.1.23_2234");
    msg.setExecutionTimeout((i + 1) * 600);
    msgList.add(msg);
  }
  changeContext.setChangeType(HelixConstants.ChangeType.MESSAGE);
  executor.onMessage("someInstance", msgList, changeContext);

  Thread.sleep(4000);

  AssertJUnit.assertTrue(factory._handlersCreated == nMsgs2);
  AssertJUnit.assertEquals(factory._timedOutMsgIds.size(), 2);
  // AssertJUnit.assertFalse(msgList.get(0).getRecord().getSimpleFields().containsKey("TimeOut"));
  for (int i = 0; i < nMsgs2 - 2; i++) {
    if (factory.getMessageTypes().contains(msgList.get(i).getMsgType())) {
      AssertJUnit.assertTrue(msgList.get(i).getRecord().getSimpleFields()
          .containsKey("Cancelcount"));
      AssertJUnit.assertTrue(factory._timedOutMsgIds.containsKey(msgList.get(i).getId()));
    }
  }
}
 
Example 6
Source File: TestHelixTaskExecutor.java    From helix with Apache License 2.0 5 votes vote down vote up
@Test()
public void testRetryOnce() throws InterruptedException {
  // Logger.getRootLogger().setLevel(Level.INFO);

  // String p = "test_";
  // System.out.println(p.substring(p.lastIndexOf('_')+1));
  HelixTaskExecutor executor = new HelixTaskExecutor();
  HelixManager manager = new MockClusterManager();

  CancellableHandlerFactory factory = new CancellableHandlerFactory();
  for (String type : factory.getMessageTypes()) {
    executor.registerMessageHandlerFactory(type, factory);
  }
  NotificationContext changeContext = new NotificationContext(manager);

  List<Message> msgList = new ArrayList<Message>();

  // factory.reset();
  // msgList.clear();
  // Test the case that the message are executed for the second time
  int nMsgs2 = 4;
  for (int i = 0; i < nMsgs2; i++) {
    Message msg = new Message(factory.getMessageTypes().get(0), UUID.randomUUID().toString());
    msg.setTgtSessionId("*");
    msg.setTgtName("Localhost_1123");
    msg.setSrcName("127.101.1.23_2234");
    msg.setExecutionTimeout((i + 1) * 600);
    msg.setRetryCount(1);
    msgList.add(msg);
  }
  changeContext.setChangeType(HelixConstants.ChangeType.MESSAGE);
  executor.onMessage("someInstance", msgList, changeContext);
  Thread.sleep(3500);
  AssertJUnit.assertEquals(factory._processedMsgIds.size(), 3);
  AssertJUnit.assertTrue(msgList.get(0).getRecord().getSimpleField("Cancelcount").equals("2"));
  AssertJUnit.assertTrue(msgList.get(1).getRecord().getSimpleField("Cancelcount").equals("1"));
  AssertJUnit.assertEquals(factory._timedOutMsgIds.size(), 2);
  AssertJUnit.assertTrue(executor._taskMap.size() == 0);

}
 
Example 7
Source File: TestHelixTaskExecutor.java    From helix with Apache License 2.0 4 votes vote down vote up
@Test()
public void testDuplicatedMessage() throws InterruptedException {
  System.out.println("START TestHelixTaskExecutor.testDuplicatedMessage()");
  HelixTaskExecutor executor = new HelixTaskExecutor();
  HelixManager manager = new MockClusterManager();
  HelixDataAccessor dataAccessor = manager.getHelixDataAccessor();
  PropertyKey.Builder keyBuilder = dataAccessor.keyBuilder();

  TestStateTransitionHandlerFactory stateTransitionFactory =
      new TestStateTransitionHandlerFactory(Message.MessageType.STATE_TRANSITION.name(), 1000);
  executor.registerMessageHandlerFactory(Message.MessageType.STATE_TRANSITION.name(),
      stateTransitionFactory);

  NotificationContext changeContext = new NotificationContext(manager);
  List<Message> msgList = new ArrayList<Message>();

  int nMsgs = 3;
  String instanceName = manager.getInstanceName();
  for (int i = 0; i < nMsgs; i++) {
    Message msg =
        new Message(Message.MessageType.STATE_TRANSITION.name(), UUID.randomUUID().toString());
    msg.setTgtSessionId(manager.getSessionId());
    msg.setCreateTimeStamp((long) i);
    msg.setTgtName("Localhost_1123");
    msg.setSrcName("127.101.1.23_2234");
    msg.setPartitionName("Partition");
    msg.setResourceName("Resource");
    msg.setStateModelDef("DummyMasterSlave");
    msg.setFromState("SLAVE");
    msg.setToState("MASTER");
    dataAccessor.setProperty(msg.getKey(keyBuilder, instanceName), msg);
    msgList.add(msg);
  }

  AssertJUnit
      .assertEquals(dataAccessor.getChildValues(keyBuilder.messages(instanceName), true).size(),
          nMsgs);

  changeContext.setChangeType(HelixConstants.ChangeType.MESSAGE);
  executor.onMessage(instanceName, msgList, changeContext);

  Thread.sleep(200);

  // only 1 message is left over - state transition takes 1sec
  Assert.assertEquals(dataAccessor.getChildValues(keyBuilder.messages(instanceName), true).size(),
      1);

  // While a state transition message is going on, another state transition message for same
  // resource / partition comes in, it should be discarded by message handler

  // Mock accessor is modifying message state in memory so we set it back to NEW
  msgList.get(2).setMsgState(MessageState.NEW);
  dataAccessor.setProperty(msgList.get(2).getKey(keyBuilder, instanceName), msgList.get(2));
  executor.onMessage(instanceName, Arrays.asList(msgList.get(2)), changeContext);
  Thread.sleep(200);
  Assert.assertEquals(dataAccessor.getChildValues(keyBuilder.messages(instanceName), true).size(),
      1);

  Thread.sleep(1000);
  Assert.assertEquals(dataAccessor.getChildValues(keyBuilder.messages(instanceName), true).size(),
      0);
  System.out.println("END TestHelixTaskExecutor.testDuplicatedMessage()");
}
 
Example 8
Source File: TestHelixTaskExecutor.java    From helix with Apache License 2.0 4 votes vote down vote up
@Test()
public void testCreateHandlerException() throws InterruptedException {
  System.out.println("START TestCMTaskExecutor.testCreateHandlerException()");
  HelixTaskExecutor executor = new HelixTaskExecutor();
  HelixManager manager = new MockClusterManager();

  TestMessageHandlerFactory factory = new TestMessageHandlerFactory();
  for (String type : factory.getMessageTypes()) {
    executor.registerMessageHandlerFactory(type, factory);
  }

  NotificationContext changeContext = new NotificationContext(manager);
  List<Message> msgList = new ArrayList<Message>();

  int nMsgs1 = 5;
  for (int i = 0; i < nMsgs1; i++) {
    Message msg = new Message(factory.getMessageTypes().get(0), UUID.randomUUID().toString());
    msg.setTgtSessionId(manager.getSessionId());
    msg.setTgtName("Localhost_1123");
    msg.setSrcName("127.101.1.23_2234");
    msg.setCorrelationId(UUID.randomUUID().toString());
    msgList.add(msg);
  }
  Message exceptionMsg = new Message(factory.getMessageTypes().get(0), UUID.randomUUID().toString());
  exceptionMsg.setTgtSessionId(manager.getSessionId());
  exceptionMsg.setMsgSubType("EXCEPTION");
  exceptionMsg.setTgtName("Localhost_1123");
  exceptionMsg.setSrcName("127.101.1.23_2234");
  exceptionMsg.setCorrelationId(UUID.randomUUID().toString());
  msgList.add(exceptionMsg);

  changeContext.setChangeType(HelixConstants.ChangeType.MESSAGE);
  executor.onMessage("someInstance", msgList, changeContext);

  Thread.sleep(1000);

  AssertJUnit.assertTrue(factory._processedMsgIds.size() == nMsgs1);
  AssertJUnit.assertTrue(factory._handlersCreated == nMsgs1);

  AssertJUnit.assertTrue(exceptionMsg.getMsgState() == MessageState.UNPROCESSABLE);
  System.out.println("END TestCMTaskExecutor.testCreateHandlerException()");
}
 
Example 9
Source File: TestHelixTaskExecutor.java    From helix with Apache License 2.0 4 votes vote down vote up
@Test
public void testStateTransitionCancellationMsg() throws InterruptedException {
  HelixTaskExecutor executor = new HelixTaskExecutor();
  HelixManager manager = new MockClusterManager();

  TestStateTransitionHandlerFactory stateTransitionFactory = new TestStateTransitionHandlerFactory(Message.MessageType.STATE_TRANSITION.name());
  TestStateTransitionHandlerFactory cancelFactory = new TestStateTransitionHandlerFactory(Message.MessageType.STATE_TRANSITION_CANCELLATION
      .name());
  executor.registerMessageHandlerFactory(Message.MessageType.STATE_TRANSITION.name(), stateTransitionFactory);
  executor.registerMessageHandlerFactory(Message.MessageType.STATE_TRANSITION_CANCELLATION.name(), cancelFactory);


  NotificationContext changeContext = new NotificationContext(manager);

  List<Message> msgList = new ArrayList<Message>();
  Message msg1 = new Message(Message.MessageType.STATE_TRANSITION, UUID.randomUUID().toString());
  msg1.setTgtSessionId("*");
  msg1.setPartitionName("P1");
  msg1.setResourceName("R1");
  msg1.setTgtName("Localhost_1123");
  msg1.setSrcName("127.101.1.23_2234");
  msg1.setFromState("SLAVE");
  msg1.setToState("MASTER");
  msgList.add(msg1);

  Message msg2 = new Message(Message.MessageType.STATE_TRANSITION_CANCELLATION, UUID.randomUUID().toString());
  msg2.setTgtSessionId("*");
  msg2.setPartitionName("P1");
  msg2.setResourceName("R1");
  msg2.setTgtName("Localhost_1123");
  msg2.setSrcName("127.101.1.23_2234");
  msg2.setFromState("SLAVE");
  msg2.setToState("MASTER");
  msgList.add(msg2);

  changeContext.setChangeType(HelixConstants.ChangeType.MESSAGE);
  executor.onMessage("someInstance", msgList, changeContext);

  Thread.sleep(3000);
  AssertJUnit.assertEquals(cancelFactory._processedMsgIds.size(), 0);
  AssertJUnit.assertEquals(stateTransitionFactory._processedMsgIds.size(), 0);
}
 
Example 10
Source File: TestHelixTaskExecutor.java    From helix with Apache License 2.0 4 votes vote down vote up
@Test
public void testMessageReadOptimization() throws InterruptedException {
  HelixTaskExecutor executor = new HelixTaskExecutor();
  HelixManager manager = new MockClusterManager();

  TestMessageHandlerFactory factory = new TestMessageHandlerFactory();
  for (String type : factory.getMessageTypes()) {
    executor.registerMessageHandlerFactory(type, factory);
  }

  HelixDataAccessor accessor = manager.getHelixDataAccessor();
  PropertyKey.Builder keyBuilder = accessor.keyBuilder();

  List<String> messageIds = new ArrayList<>();
  int nMsgs1 = 5;
  for (int i = 0; i < nMsgs1; i++) {
    Message msg = new Message(factory.getMessageTypes().get(0), UUID.randomUUID().toString());
    msg.setTgtSessionId(manager.getSessionId());
    msg.setTgtName("Localhost_1123");
    msg.setSrcName("127.101.1.23_2234");
    msg.setCorrelationId(UUID.randomUUID().toString());
    accessor.setProperty(keyBuilder.message("someInstance", msg.getId()), msg);
    messageIds.add(msg.getId());
  }

  NotificationContext changeContext = new NotificationContext(manager);
  changeContext.setChangeType(HelixConstants.ChangeType.MESSAGE);

  // Simulate read message already, then processing message. Should read and handle no message.
  executor._knownMessageIds.addAll(messageIds);
  executor.onMessage("someInstance", Collections.EMPTY_LIST, changeContext);
  Thread.sleep(3000);
  AssertJUnit.assertEquals(0, factory._processedMsgIds.size());
  executor._knownMessageIds.clear();

  // Processing message normally
  executor.onMessage("someInstance", Collections.EMPTY_LIST, changeContext);
  Thread.sleep(3000);
  AssertJUnit.assertEquals(nMsgs1, factory._processedMsgIds.size());
  // After all messages are processed, _knownMessageIds should be empty.
  Assert.assertTrue(executor._knownMessageIds.isEmpty());
}