@Override public void onLiveInstanceChange(List<LiveInstance> liveInstances, NotificationContext changeContext) { if (changeContext.getType() != NotificationContext.Type.FINALIZE) { refreshCache(); } } };
@Override public void onExternalViewChange(List<ExternalView> externalViewList, NotificationContext changeContext) { if (changeContext.getType() == Type.CALLBACK) { cbCnt++; } } }
@Override public void onCallback(NotificationContext context) { NotificationContext.Type type = context.getType(); _callbackInvokeMap.put(type, Boolean.TRUE); }
public void enqueueTask(NotificationContext changeContext) throws Exception { //async mode only applicable to CALLBACK from ZK, During INIT and FINALIZE invoke the callback's immediately. if (_batchModeEnabled && changeContext.getType() == NotificationContext.Type.CALLBACK) { logger.debug("Enqueuing callback"); if (!isReady()) { logger.info( "CallbackHandler is not ready, ignore change callback from path: " + _path + ", for listener: " + _listener); } else { _batchCallbackProcessor.queueEvent(changeContext.getType(), changeContext); } } else { invoke(changeContext); } if (_monitor != null) { _monitor.increaseCallbackUnbatchedCounters(); } }
@Override public void onLiveInstanceChange(List<LiveInstance> liveInstances, NotificationContext changeContext) { logger.info("START: Generic GenericClusterController.onLiveInstanceChange() for cluster " + _clusterName); if (changeContext == null || changeContext.getType() != Type.CALLBACK) { _cache.requireFullRefresh(); _taskCache.requireFullRefresh(); } if (liveInstances == null) { liveInstances = Collections.emptyList(); } _cache.setLiveInstances(liveInstances); _taskCache.setLiveInstances(liveInstances); // Go though the live instance list and make sure that we are observing them // accordingly. The action is done regardless of the paused flag. if (changeContext.getType() == NotificationContext.Type.INIT || changeContext.getType() == NotificationContext.Type.CALLBACK) { checkLiveInstancesObservation(liveInstances, changeContext); } else if (changeContext.getType() == NotificationContext.Type.FINALIZE) { // on finalize, should remove all message/current-state listeners logger.info("remove message/current-state listeners. lastSeenInstances: " + _lastSeenInstances + ", lastSeenSessions: " + _lastSeenSessions); liveInstances = Collections.emptyList(); checkLiveInstancesObservation(liveInstances, changeContext); } pushToEventQueues(ClusterEventType.LiveInstanceChange, changeContext, Collections.<String, Object>singletonMap(AttributeName.eventData.name(), liveInstances)); logger.info( "END: Generic GenericClusterController.onLiveInstanceChange() for cluster " + _clusterName); }
@Override public synchronized void onControllerChange(NotificationContext changeContext) { ControllerManagerHelper controllerHelper = new ControllerManagerHelper(_manager, _controllerTimerTasks); try { switch (changeContext.getType()) { case INIT: case CALLBACK: acquireLeadership(_manager, controllerHelper); break; case FINALIZE: relinquishLeadership(_manager, controllerHelper); break; default: LOG.info("Ignore controller change event {}. Type {}.", changeContext.getEventName(), changeContext.getType().name()); } } catch (Exception e) { LOG.error("Exception when trying to become leader", e); } }
@Override public void onIdealStateChange(List<IdealState> idealState, NotificationContext changeContext) { if (changeContext.getType().equals(NotificationContext.Type.CALLBACK)) { _idealStateChangedCount++; try { Thread.sleep(200); } catch (InterruptedException e) { e.printStackTrace(); } } }
@Override public void onInstanceConfigChange(List<InstanceConfig> instanceConfigs, NotificationContext context) { if (context.getType().equals(NotificationContext.Type.CALLBACK)) { _instanceConfigChangedCount++; try { Thread.sleep(200); } catch (InterruptedException e) { e.printStackTrace(); } } }
@Override public void onCallback(NotificationContext context) { HelixManager manager = context.getManager(); Type type = context.getType(); _isCallbackInvoked = true; // System.out.println(type + ": TestCallback invoked on " + manager.getInstanceName()); }
@Override @PreFetch(enabled = false) public void onIdealStateChange(List<IdealState> idealStates, NotificationContext changeContext) { logger.info( "START: Generic GenericClusterController.onIdealStateChange() for cluster " + _clusterName); notifyCaches(changeContext, ChangeType.IDEAL_STATE); pushToEventQueues(ClusterEventType.IdealStateChange, changeContext, Collections.<String, Object>emptyMap()); if (changeContext.getType() != Type.FINALIZE) { checkRebalancingTimer(changeContext.getManager(), idealStates, _cache.getClusterConfig()); } logger.info("END: GenericClusterController.onIdealStateChange() for cluster " + _clusterName); }
@Override @PreFetch(enabled = false) public void onIdealStateChange(List<IdealState> idealStates, NotificationContext changeContext) { logger.info( "START: Generic GenericClusterController.onIdealStateChange() for cluster " + _clusterName); notifyCaches(changeContext, ChangeType.IDEAL_STATE); pushToEventQueues(ClusterEventType.IdealStateChange, changeContext, Collections.<String, Object>emptyMap()); if (changeContext.getType() != Type.FINALIZE) { checkRebalancingTimer(changeContext.getManager(), idealStates, _cache.getClusterConfig()); } logger.info("END: GenericClusterController.onIdealStateChange() for cluster " + _clusterName); }
public void enqueueTask(NotificationContext changeContext) throws Exception { //async mode only applicable to CALLBACK from ZK, During INIT and FINALIZE invoke the callback's immediately. if (_batchModeEnabled && changeContext.getType() == NotificationContext.Type.CALLBACK) { logger.debug("Enqueuing callback"); if (!isReady()) { logger.info( "CallbackHandler is not ready, ignore change callback from path: " + _path + ", for listener: " + _listener); } else { synchronized (this) { if (_batchCallbackProcessor != null) { _batchCallbackProcessor.queueEvent(changeContext.getType(), changeContext); } else { throw new HelixException("Failed to process callback in batch mode. Batch Callback Processor does not exist."); } } } } else { invoke(changeContext); } if (_monitor != null) { _monitor.increaseCallbackUnbatchedCounters(); } }
@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 { 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); } }
@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 { 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); } }
private void callParticipantCode(NotificationContext context) { // since ZkClient.unsubscribe() does not immediately remove listeners // from zk, it is possible that two listeners exist when leadership transfers // therefore, double check to make sure only one participant invokes the code if (context.getType() == Type.CALLBACK) { HelixManager manager = context.getManager(); // DataAccessor accessor = manager.getDataAccessor(); HelixDataAccessor accessor = manager.getHelixDataAccessor(); Builder keyBuilder = accessor.keyBuilder(); String instance = manager.getInstanceName(); String sessionId = manager.getSessionId(); // get resource name from partition key: "PARTICIPANT_LEADER_XXX_0" String resourceName = _partitionKey.substring(0, _partitionKey.lastIndexOf('_')); CurrentState curState = accessor.getProperty(keyBuilder.currentState(instance, sessionId, resourceName)); if (curState == null) { return; } String state = curState.getState(_partitionKey); if (state == null || !state.equalsIgnoreCase("LEADER")) { return; } } try { _callback.onCallback(context); } catch (Exception e) { LOG.error("Error invoking callback:" + _callback, e); } }
private void callParticipantCode(NotificationContext context) { // since ZkClient.unsubscribe() does not immediately remove listeners // from zk, it is possible that two listeners exist when leadership transfers // therefore, double check to make sure only one participant invokes the code if (context.getType() == Type.CALLBACK) { HelixManager manager = context.getManager(); // DataAccessor accessor = manager.getDataAccessor(); HelixDataAccessor accessor = manager.getHelixDataAccessor(); Builder keyBuilder = accessor.keyBuilder(); String instance = manager.getInstanceName(); String sessionId = manager.getSessionId(); // get resource name from partition key: "PARTICIPANT_LEADER_XXX_0" String resourceName = _partitionKey.substring(0, _partitionKey.lastIndexOf('_')); CurrentState curState = accessor.getProperty(keyBuilder.currentState(instance, sessionId, resourceName)); if (curState == null) { return; } String state = curState.getState(_partitionKey); if (state == null || !state.equalsIgnoreCase("LEADER")) { return; } } try { _callback.onCallback(context); } catch (Exception e) { LOG.error("Error invoking callback:" + _callback, e); } }
private void notifyCaches(NotificationContext context, ChangeType changeType) { if (context == null || context.getType() != Type.CALLBACK) { _cache.requireFullRefresh(); _taskCache.requireFullRefresh(); } else { _cache.notifyDataChange(changeType, context.getPathChanged()); _taskCache.notifyDataChange(changeType, context.getPathChanged()); } }
private void notifyCaches(NotificationContext context, ChangeType changeType) { if (context == null || context.getType() != Type.CALLBACK) { _cache.requireFullRefresh(); _taskCache.requireFullRefresh(); } else { _cache.notifyDataChange(changeType, context.getPathChanged()); _taskCache.notifyDataChange(changeType, context.getPathChanged()); } }
public void queueEvent(NotificationContext context, ClusterEventType eventType, HelixConstants.ChangeType changeType) { ClusterEvent event = new ClusterEvent(_clusterName, eventType); if (context == null || context.getType() != NotificationContext.Type.CALLBACK || context.getType() == NotificationContext.Type.PERIODIC_REFRESH) { _dataCache.requireFullRefresh(); } else { _dataCache.notifyDataChange(changeType, context.getPathChanged()); } // Null check for manager in the following line is done in handleEvent() event.addAttribute(AttributeName.helixmanager.name(), context.getManager()); event.addAttribute(AttributeName.changeContext.name(), context); queueEvent(event); _monitor.increaseCallbackCounters(_eventQueue.size()); } }
public void queueEvent(NotificationContext context, ClusterEventType eventType, HelixConstants.ChangeType changeType) { ClusterEvent event = new ClusterEvent(_clusterName, eventType); if (context == null || context.getType() != NotificationContext.Type.CALLBACK || context.getType() == NotificationContext.Type.PERIODIC_REFRESH) { _dataCache.requireFullRefresh(); } else { _dataCache.notifyDataChange(changeType, context.getPathChanged()); } // Null check for manager in the following line is done in handleEvent() event.addAttribute(AttributeName.helixmanager.name(), context.getManager()); event.addAttribute(AttributeName.changeContext.name(), context); queueEvent(event); _monitor.increaseCallbackCounters(_eventQueue.size()); } }