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

The following examples show how to use org.apache.helix.NotificationContext#setType() . 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: GenericHelixController.java    From helix with Apache License 2.0 6 votes vote down vote up
private void forceRebalance(HelixManager manager, ClusterEventType eventType) {
  NotificationContext changeContext = new NotificationContext(manager);
  changeContext.setType(NotificationContext.Type.CALLBACK);
  String uid = UUID.randomUUID().toString().substring(0, 8);
  ClusterEvent event = new ClusterEvent(_clusterName, eventType, uid);
  event.addAttribute(AttributeName.helixmanager.name(), changeContext.getManager());
  event.addAttribute(AttributeName.changeContext.name(), changeContext);
  event.addAttribute(AttributeName.eventData.name(), new ArrayList<>());
  event.addAttribute(AttributeName.AsyncFIFOWorkerPool.name(), _asyncFIFOWorkerPool);

  enqueueEvent(_taskEventQueue, event);
  enqueueEvent(_eventQueue, event.clone(uid));
  logger.info(String
      .format("Controller rebalance pipeline triggered with event type: %s for cluster %s",
          eventType, _clusterName));
}
 
Example 4
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 5
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 6
Source File: GenericHelixController.java    From helix with Apache License 2.0 5 votes vote down vote up
@Override
public void run() {
  try {
    if (_shouldRefreshCacheOption.orElse(
        _clusterEventType.equals(ClusterEventType.PeriodicalRebalance) || _clusterEventType
            .equals(ClusterEventType.OnDemandRebalance))) {
      requestDataProvidersFullRefresh();

      HelixDataAccessor accessor = _manager.getHelixDataAccessor();
      PropertyKey.Builder keyBuilder = accessor.keyBuilder();
      List<LiveInstance> liveInstances =
          accessor.getChildValues(keyBuilder.liveInstances(), true);

      if (liveInstances != null && !liveInstances.isEmpty()) {
        NotificationContext changeContext = new NotificationContext(_manager);
        changeContext.setType(NotificationContext.Type.CALLBACK);
        synchronized (_manager) {
          checkLiveInstancesObservation(liveInstances, changeContext);
        }
      }
    }
    forceRebalance(_manager, _clusterEventType);
  } catch (Throwable ex) {
    logger.error("Time task failed. Rebalance task type: " + _clusterEventType + ", cluster: "
        + _clusterName, ex);
  }
}
 
Example 7
Source File: MockHelixManager.java    From ambry with Apache License 2.0 5 votes vote down vote up
/**
 * Trigger a live instance change notification.
 */
void triggerLiveInstanceNotification(boolean init) {
  List<LiveInstance> liveInstances = new ArrayList<>();
  for (String instance : mockAdmin.getUpInstances()) {
    liveInstances.add(new LiveInstance(instance));
  }
  NotificationContext notificationContext = new NotificationContext(this);
  if (init) {
    notificationContext.setType(NotificationContext.Type.INIT);
  }
  liveInstanceChangeListener.onLiveInstanceChange(liveInstances, notificationContext);
}
 
Example 8
Source File: MockHelixManager.java    From ambry with Apache License 2.0 5 votes vote down vote up
void triggerConfigChangeNotification(boolean init) {
  if (!isConnected) {
    return;
  }
  NotificationContext notificationContext = new NotificationContext(this);
  if (init) {
    notificationContext.setType(NotificationContext.Type.INIT);
  }
  instanceConfigChangeListener.onInstanceConfigChange(mockAdmin.getInstanceConfigs(clusterName), notificationContext);
}
 
Example 9
Source File: MockHelixManager.java    From ambry with Apache License 2.0 5 votes vote down vote up
void triggerIdealStateNotification(boolean init) throws InterruptedException {
  NotificationContext notificationContext = new NotificationContext(this);
  if (init) {
    notificationContext.setType(NotificationContext.Type.INIT);
  }
  idealStateChangeListener.onIdealStateChange(mockAdmin.getIdealStates(), notificationContext);
}
 
Example 10
Source File: MockHelixManager.java    From ambry with Apache License 2.0 5 votes vote down vote up
@Override
public void addExternalViewChangeListener(ExternalViewChangeListener externalViewChangeListener) throws Exception {
  if (beBadException != null) {
    throw beBadException;
  }
  this.externalViewChangeListener = externalViewChangeListener;
  NotificationContext notificationContext = new NotificationContext(this);
  notificationContext.setType(NotificationContext.Type.INIT);
  this.externalViewChangeListener.onExternalViewChange(Collections.emptyList(), notificationContext);
}