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

The following examples show how to use org.apache.helix.NotificationContext#getType() . 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
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: {}, for "
          + "listener: {}", _path, _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();
  }
}
 
Example 2
Source File: DistributedLeaderElection.java    From helix with Apache License 2.0 6 votes vote down vote up
@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);
  }
}
 
Example 3
Source File: GenericHelixController.java    From helix with Apache License 2.0 6 votes vote down vote up
@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() != NotificationContext.Type.FINALIZE) {
    HelixManager manager = changeContext.getManager();
    if (manager != null) {
      HelixDataAccessor dataAccessor = changeContext.getManager().getHelixDataAccessor();
      checkRebalancingTimer(changeContext.getManager(), idealStates,
          (ClusterConfig) dataAccessor.getProperty(dataAccessor.keyBuilder().clusterConfig()));
    }
  }

  logger.info("END: GenericClusterController.onIdealStateChange() for cluster " + _clusterName);
}
 
Example 4
Source File: TestHandleSession.java    From helix with Apache License 2.0 6 votes vote down vote up
@Override
public void onLiveInstanceChange(List<LiveInstance> liveInstances,
    NotificationContext changeContext) {
  if (changeContext.getType() != NotificationContext.Type.FINALIZE) {
    for (LiveInstance liveInstance : liveInstances) {
      if (_expectedLiveInstances.contains(liveInstance.getInstanceName())) {
        try {
          _manager.addCurrentStateChangeListener(
              (CurrentStateChangeListener) (instanceName, statesInfo, currentStateChangeContext) -> {
                // empty callback
              }, liveInstance.getInstanceName(), liveInstance.getEphemeralOwner());
        } catch (Exception e) {
          throw new HelixException("Unexpected exception in the test method.", e);
        }
      }
    }
  }
}
 
Example 5
Source File: GenericHelixController.java    From helix with Apache License 2.0 5 votes vote down vote up
@Override
public void onLiveInstanceChange(List<LiveInstance> liveInstances,
    NotificationContext changeContext) {
  logger.info("START: Generic GenericClusterController.onLiveInstanceChange() for cluster " + _clusterName);
  notifyCaches(changeContext, ChangeType.LIVE_INSTANCE);

  if (liveInstances == null) {
    liveInstances = Collections.emptyList();
  }

  // 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);
}
 
Example 6
Source File: GenericHelixController.java    From helix with Apache License 2.0 5 votes vote down vote up
private void notifyCaches(NotificationContext context, ChangeType changeType) {
  if (context == null || context.getType() != NotificationContext.Type.CALLBACK) {
    requestDataProvidersFullRefresh();
  } else {
    updateDataChangeInProvider(changeType, context.getPathChanged());
  }
}
 
Example 7
Source File: CustomCodeInvoker.java    From helix with Apache License 2.0 5 votes vote down vote up
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);
  }
}
 
Example 8
Source File: TestBucketizedResource.java    From helix with Apache License 2.0 5 votes vote down vote up
@Override
public void onExternalViewChange(List<ExternalView> externalViewList,
    NotificationContext changeContext) {
  if (changeContext.getType() == Type.CALLBACK) {
    cbCnt++;
  }
}
 
Example 9
Source File: ServiceDiscovery.java    From helix with Apache License 2.0 5 votes vote down vote up
private void setupWatcher() throws Exception {

    LiveInstanceChangeListener listener = new LiveInstanceChangeListener() {
      @Override
      public void onLiveInstanceChange(List<LiveInstance> liveInstances,
          NotificationContext changeContext) {
        if (changeContext.getType() != NotificationContext.Type.FINALIZE) {
          refreshCache();
        }
      }
    };
    admin.addLiveInstanceChangeListener(listener);
  }
 
Example 10
Source File: GenericHelixController.java    From helix with Apache License 2.0 4 votes vote down vote up
@Override
public void onControllerChange(NotificationContext changeContext) {
  logger.info("START: GenericClusterController.onControllerChange() for cluster " + _clusterName);

  requestDataProvidersFullRefresh();

  boolean controllerIsLeader;

  if (changeContext == null || changeContext.getType() == NotificationContext.Type.FINALIZE) {
    logger.info(
        "GenericClusterController.onControllerChange() Cluster change type {} for cluster {}. Disable leadership.",
        changeContext == null ? null : changeContext.getType(), _clusterName);
    controllerIsLeader = false;
  } else {
    // double check if this controller is the leader
    controllerIsLeader = changeContext.getManager().isLeader();
  }

  if (controllerIsLeader) {
    HelixManager manager = changeContext.getManager();
    HelixDataAccessor accessor = manager.getHelixDataAccessor();
    Builder keyBuilder = accessor.keyBuilder();
    PauseSignal pauseSignal = accessor.getProperty(keyBuilder.pause());
    MaintenanceSignal maintenanceSignal = accessor.getProperty(keyBuilder.maintenance());
    _paused = updateControllerState(changeContext, pauseSignal, _paused);
    _inMaintenanceMode =
        updateControllerState(changeContext, maintenanceSignal, _inMaintenanceMode);
    enableClusterStatusMonitor(true);
    _clusterStatusMonitor.setEnabled(!_paused);
    _clusterStatusMonitor.setPaused(_paused);
    _clusterStatusMonitor.setMaintenance(_inMaintenanceMode);
  } else {
    enableClusterStatusMonitor(false);
    // Note that onControllerChange is executed in parallel with the event processing thread. It
    // is possible that the current WAGED rebalancer object is in use for handling callback. So
    // mark the rebalancer invalid only, instead of closing it here.
    // This to-be-closed WAGED rebalancer will be reset later on a later event processing if
    // the controller becomes leader again.
    _rebalancerRef.invalidateRebalancer();
  }

  logger.info("END: GenericClusterController.onControllerChange() for cluster " + _clusterName);
}
 
Example 11
Source File: TestDisableCustomCodeRunner.java    From helix with Apache License 2.0 4 votes vote down vote up
@Override
public void onCallback(NotificationContext context) {
  NotificationContext.Type type = context.getType();
  _callbackInvokeMap.put(type, Boolean.TRUE);
}