Search in sources :

Example 1 with Callback

use of com.alibaba.jstorm.callback.Callback in project jstorm by alibaba.

the class StatusTransition method transitionLock.

/**
     * Changing status
     *
     * @param args -- will be used in the status changing callback
     */
public <T> void transitionLock(String topologyId, boolean errorOnNoTransition, StatusType changeStatus, T... args) throws Exception {
    // get ZK's topology node's data, which is StormBase
    StormBase stormbase = data.getStormClusterState().storm_base(topologyId, null);
    if (stormbase == null) {
        LOG.error("Cannot apply event: changing status " + topologyId + " -> " + changeStatus.getStatus() + ", cause: failed to get StormBase from ZK");
        return;
    }
    StormStatus currentStatus = stormbase.getStatus();
    if (currentStatus == null) {
        LOG.error("Cannot apply event: changing status " + topologyId + " -> " + changeStatus.getStatus() + ", cause: topologyStatus is null in ZK");
        return;
    }
    // <currentStatus, Map<changingStatus, callback>>
    Map<StatusType, Map<StatusType, Callback>> callbackMap = stateTransitions(topologyId, currentStatus);
    // get current changingCallbacks
    Map<StatusType, Callback> changingCallbacks = callbackMap.get(currentStatus.getStatusType());
    if (changingCallbacks == null || !changingCallbacks.containsKey(changeStatus) || changingCallbacks.get(changeStatus) == null) {
        String msg = "No transition for event: changing status:" + changeStatus.getStatus() + ", current status: " + currentStatus.getStatusType() + ", topology-id: " + topologyId;
        LOG.info(msg);
        if (errorOnNoTransition) {
            throw new RuntimeException(msg);
        }
        return;
    }
    Callback callback = changingCallbacks.get(changeStatus);
    Object obj = callback.execute(args);
    if (obj != null && obj instanceof StormStatus) {
        StormStatus newStatus = (StormStatus) obj;
        // update status to ZK
        data.getStormClusterState().update_storm(topologyId, newStatus);
        LOG.info("Successfully updated " + topologyId + " to status " + newStatus);
    }
    LOG.info("Successfully apply event: changing status " + topologyId + " -> " + changeStatus.getStatus());
}
Also used : StormStatus(com.alibaba.jstorm.cluster.StormStatus) Callback(com.alibaba.jstorm.callback.Callback) StormBase(com.alibaba.jstorm.cluster.StormBase) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap)

Example 2 with Callback

use of com.alibaba.jstorm.callback.Callback in project jstorm by alibaba.

the class NimbusServer method initFollowerThread.

private void initFollowerThread(Map conf) {
    // when this nimbus become leader, we will execute this callback, to init some necessary data/thread
    Callback leaderCallback = new Callback() {

        public <T> Object execute(T... args) {
            try {
                init(data.getConf());
            } catch (Exception e) {
                LOG.error("Nimbus init error after becoming a leader", e);
                throw new RuntimeException(e);
            }
            return null;
        }
    };
    follower = new FollowerRunnable(data, 5000, leaderCallback);
    Thread thread = new Thread(follower);
    thread.setDaemon(true);
    thread.start();
    LOG.info("Successfully init Follower thread");
}
Also used : RunnableCallback(com.alibaba.jstorm.callback.RunnableCallback) Callback(com.alibaba.jstorm.callback.Callback) FollowerRunnable(com.alibaba.jstorm.schedule.FollowerRunnable) TTransportException(org.apache.thrift.transport.TTransportException) TException(org.apache.thrift.TException) IOException(java.io.IOException) AsyncLoopThread(com.alibaba.jstorm.callback.AsyncLoopThread)

Aggregations

Callback (com.alibaba.jstorm.callback.Callback)2 AsyncLoopThread (com.alibaba.jstorm.callback.AsyncLoopThread)1 RunnableCallback (com.alibaba.jstorm.callback.RunnableCallback)1 StormBase (com.alibaba.jstorm.cluster.StormBase)1 StormStatus (com.alibaba.jstorm.cluster.StormStatus)1 FollowerRunnable (com.alibaba.jstorm.schedule.FollowerRunnable)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 TException (org.apache.thrift.TException)1 TTransportException (org.apache.thrift.transport.TTransportException)1