Search in sources :

Example 21 with ResourceWorkerSlot

use of com.alibaba.jstorm.schedule.default_assign.ResourceWorkerSlot in project jstorm by alibaba.

the class TopologyAssign method getTaskStartTimes.

public static Map<Integer, Integer> getTaskStartTimes(TopologyAssignContext context, NimbusData nimbusData, String topologyId, Assignment existingAssignment, Set<ResourceWorkerSlot> workers) throws Exception {
    Map<Integer, Integer> startTimes = new TreeMap<>();
    if (context.getAssignType() == TopologyAssignContext.ASSIGN_TYPE_NEW) {
        int nowSecs = TimeUtils.current_time_secs();
        for (ResourceWorkerSlot worker : workers) {
            for (Integer changedTaskId : worker.getTasks()) {
                startTimes.put(changedTaskId, nowSecs);
            }
        }
        return startTimes;
    }
    Set<ResourceWorkerSlot> oldWorkers = new HashSet<>();
    if (existingAssignment != null) {
        Map<Integer, Integer> taskStartTimeSecs = existingAssignment.getTaskStartTimeSecs();
        if (taskStartTimeSecs != null) {
            startTimes.putAll(taskStartTimeSecs);
        }
        if (existingAssignment.getWorkers() != null) {
            oldWorkers = existingAssignment.getWorkers();
        }
    }
    Set<Integer> changedTaskIds = getNewOrChangedTaskIds(oldWorkers, workers);
    int nowSecs = TimeUtils.current_time_secs();
    for (Integer changedTaskId : changedTaskIds) {
        startTimes.put(changedTaskId, nowSecs);
        NimbusUtils.removeTopologyTaskHb(nimbusData, topologyId, changedTaskId);
    }
    Set<Integer> removedTaskIds = getRemovedTaskIds(oldWorkers, workers);
    for (Integer removedTaskId : removedTaskIds) {
        startTimes.remove(removedTaskId);
        NimbusUtils.removeTopologyTaskHb(nimbusData, topologyId, removedTaskId);
    }
    LOG.info("Task assignment has been changed: " + changedTaskIds + ", removed tasks " + removedTaskIds);
    return startTimes;
}
Also used : TreeMap(java.util.TreeMap) ResourceWorkerSlot(com.alibaba.jstorm.schedule.default_assign.ResourceWorkerSlot) HashSet(java.util.HashSet)

Example 22 with ResourceWorkerSlot

use of com.alibaba.jstorm.schedule.default_assign.ResourceWorkerSlot in project jstorm by alibaba.

the class RefreshEvent method doRefreshTopologies.

/**
 * refresh metric settings of topologies and sync metric meta from local cache
 */
@SuppressWarnings("unchecked")
private void doRefreshTopologies() {
    for (String topology : JStormMetrics.SYS_TOPOLOGIES) {
        if (!context.getTopologyMetricContexts().containsKey(topology)) {
            LOG.info("adding {} to metric context.", topology);
            Map conf = new HashMap();
            if (topology.equals(JStormMetrics.CLUSTER_METRIC_KEY)) {
                // there's no need to consider sample rate when cluster metrics merge
                conf.put(ConfigExtension.TOPOLOGY_METRIC_SAMPLE_RATE, 1.0);
            }
            Set<ResourceWorkerSlot> workerSlot = Sets.newHashSet(new ResourceWorkerSlot());
            TopologyMetricContext metricContext = new TopologyMetricContext(topology, workerSlot, conf);
            context.getTopologyMetricContexts().putIfAbsent(topology, metricContext);
            syncMetaFromCache(topology, context.getTopologyMetricContexts().get(topology));
        }
        // in the first 10 min, sync every 1 min
        if (context.getNimbusData().uptime() < INIT_SYNC_REMOTE_META_TIME_SEC) {
            syncMetaFromRemote(topology, context.getTopologyMetricContexts().get(topology), Lists.newArrayList(MetaType.TOPOLOGY, MetaType.WORKER, MetaType.NIMBUS));
        } else {
            // after that, sync every 5 min
            checkTimeAndSyncMetaFromRemote(topology, context.getTopologyMetricContexts().get(topology), Lists.newArrayList(MetaType.values()));
        }
    }
    Map<String, Assignment> assignMap;
    try {
        assignMap = Cluster.get_all_assignment(context.getStormClusterState(), null);
        for (Entry<String, Assignment> entry : assignMap.entrySet()) {
            String topology = entry.getKey();
            Assignment assignment = entry.getValue();
            TopologyMetricContext metricContext = context.getTopologyMetricContexts().get(topology);
            if (metricContext == null) {
                metricContext = new TopologyMetricContext(assignment.getWorkers());
                metricContext.setTaskNum(NimbusUtils.getTopologyTaskNum(assignment));
                syncMetaFromCache(topology, metricContext);
                LOG.info("adding {} to metric context.", topology);
                context.getTopologyMetricContexts().put(topology, metricContext);
            } else {
                boolean modify = false;
                if (metricContext.getTaskNum() != NimbusUtils.getTopologyTaskNum(assignment)) {
                    modify = true;
                    metricContext.setTaskNum(NimbusUtils.getTopologyTaskNum(assignment));
                }
                if (!assignment.getWorkers().equals(metricContext.getWorkerSet())) {
                    modify = true;
                    metricContext.setWorkerSet(assignment.getWorkers());
                }
                // for normal topologies, only sync topology/component level metrics
                checkTimeAndSyncMetaFromRemote(topology, metricContext, Lists.newArrayList(MetaType.TOPOLOGY, MetaType.COMPONENT));
                // we may need to sync meta when task num/workers change
                metricContext.setSyncMeta(!modify);
            }
        }
    } catch (Exception e1) {
        LOG.warn("Failed to get assignments");
        return;
    }
    List<String> removing = new ArrayList<>();
    for (String topology : context.getTopologyMetricContexts().keySet()) {
        if (!JStormMetrics.SYS_TOPOLOGY_SET.contains(topology) && !assignMap.containsKey(topology)) {
            removing.add(topology);
        }
    }
    for (String topology : removing) {
        LOG.info("removing topology:{}", topology);
        RemoveTopologyEvent.pushEvent(topology);
        syncRemoteTimes.remove(topology);
    }
}
Also used : HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) TopologyMetricContext(com.alibaba.jstorm.metric.TopologyMetricContext) ArrayList(java.util.ArrayList) Assignment(com.alibaba.jstorm.schedule.Assignment) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ResourceWorkerSlot(com.alibaba.jstorm.schedule.default_assign.ResourceWorkerSlot)

Example 23 with ResourceWorkerSlot

use of com.alibaba.jstorm.schedule.default_assign.ResourceWorkerSlot in project jstorm by alibaba.

the class ServiceHandler method getTopologyWorkersToSupervisorIds.

@Override
public Map<String, Map<String, String>> getTopologyWorkersToSupervisorIds(String topologyName) throws NotAliveException, TException {
    StormClusterState stormClusterState = data.getStormClusterState();
    String topologyId = getTopologyId(topologyName);
    Map<String, Map<String, String>> ret = new HashMap<>();
    try {
        Assignment assignment = stormClusterState.assignment_info(topologyId, null);
        Set<ResourceWorkerSlot> workers = assignment.getWorkers();
        for (ResourceWorkerSlot worker : workers) {
            String supervisorId = worker.getNodeId();
            if (!ret.containsKey(worker.getHostname()))
                ret.put(worker.getHostname(), new HashMap<String, String>());
            ret.get(worker.getHostname()).put(String.valueOf(worker.getPort()), supervisorId);
        }
    } catch (Exception ex) {
        LOG.error("Error:", ex);
    }
    return ret;
}
Also used : Assignment(com.alibaba.jstorm.schedule.Assignment) StormClusterState(com.alibaba.jstorm.cluster.StormClusterState) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) TimeCacheMap(com.alibaba.jstorm.utils.TimeCacheMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) FailedAssignTopologyException(com.alibaba.jstorm.utils.FailedAssignTopologyException) KeyNotFoundException(backtype.storm.generated.KeyNotFoundException) TException(org.apache.thrift.TException) IOException(java.io.IOException) AlreadyAliveException(backtype.storm.generated.AlreadyAliveException) TopologyAssignException(backtype.storm.generated.TopologyAssignException) FileNotFoundException(java.io.FileNotFoundException) NotAliveException(backtype.storm.generated.NotAliveException) InvalidTopologyException(backtype.storm.generated.InvalidTopologyException) KeyAlreadyExistsException(backtype.storm.generated.KeyAlreadyExistsException) ResourceWorkerSlot(com.alibaba.jstorm.schedule.default_assign.ResourceWorkerSlot)

Example 24 with ResourceWorkerSlot

use of com.alibaba.jstorm.schedule.default_assign.ResourceWorkerSlot in project jstorm by alibaba.

the class ServiceHandler method getTopologyTasksToSupervisorIds.

@Override
public Map<Integer, String> getTopologyTasksToSupervisorIds(String topologyName) throws TException {
    StormClusterState stormClusterState = data.getStormClusterState();
    String topologyId = getTopologyId(topologyName);
    Map<Integer, String> ret = new HashMap<>();
    try {
        Assignment assignment = stormClusterState.assignment_info(topologyId, null);
        Set<ResourceWorkerSlot> workers = assignment.getWorkers();
        for (ResourceWorkerSlot worker : workers) {
            String supervisorId = worker.getNodeId();
            for (Integer task : worker.getTasks()) {
                ret.put(task, supervisorId);
            }
        }
    } catch (Exception ex) {
        LOG.error("Error:", ex);
    }
    return ret;
}
Also used : Assignment(com.alibaba.jstorm.schedule.Assignment) StormClusterState(com.alibaba.jstorm.cluster.StormClusterState) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) FailedAssignTopologyException(com.alibaba.jstorm.utils.FailedAssignTopologyException) KeyNotFoundException(backtype.storm.generated.KeyNotFoundException) TException(org.apache.thrift.TException) IOException(java.io.IOException) AlreadyAliveException(backtype.storm.generated.AlreadyAliveException) TopologyAssignException(backtype.storm.generated.TopologyAssignException) FileNotFoundException(java.io.FileNotFoundException) NotAliveException(backtype.storm.generated.NotAliveException) InvalidTopologyException(backtype.storm.generated.InvalidTopologyException) KeyAlreadyExistsException(backtype.storm.generated.KeyAlreadyExistsException) ResourceWorkerSlot(com.alibaba.jstorm.schedule.default_assign.ResourceWorkerSlot)

Example 25 with ResourceWorkerSlot

use of com.alibaba.jstorm.schedule.default_assign.ResourceWorkerSlot in project jstorm by alibaba.

the class SyncSupervisorEvent method readMyTasks.

/**
 * get local node's tasks
 *
 * @return Map: [port, LocalAssignment]
 */
@SuppressWarnings("unused")
private Map<Integer, LocalAssignment> readMyTasks(StormClusterState stormClusterState, String topologyId, String supervisorId, Assignment assignmentInfo) throws Exception {
    Map<Integer, LocalAssignment> portTasks = new HashMap<>();
    Set<ResourceWorkerSlot> workers = assignmentInfo.getWorkers();
    if (workers == null) {
        LOG.error("No worker found for assignment {}!", assignmentInfo);
        return portTasks;
    }
    for (ResourceWorkerSlot worker : workers) {
        if (!supervisorId.equals(worker.getNodeId())) {
            continue;
        }
        portTasks.put(worker.getPort(), new LocalAssignment(topologyId, worker.getTasks(), Common.topologyIdToName(topologyId), worker.getMemSize(), worker.getCpu(), worker.getJvm(), assignmentInfo.getTimeStamp()));
    }
    return portTasks;
}
Also used : LocalAssignment(com.alibaba.jstorm.daemon.worker.LocalAssignment) ResourceWorkerSlot(com.alibaba.jstorm.schedule.default_assign.ResourceWorkerSlot)

Aggregations

ResourceWorkerSlot (com.alibaba.jstorm.schedule.default_assign.ResourceWorkerSlot)26 Assignment (com.alibaba.jstorm.schedule.Assignment)13 HashMap (java.util.HashMap)10 HashSet (java.util.HashSet)9 StormClusterState (com.alibaba.jstorm.cluster.StormClusterState)7 ArrayList (java.util.ArrayList)7 Map (java.util.Map)7 FailedAssignTopologyException (com.alibaba.jstorm.utils.FailedAssignTopologyException)6 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)6 SupervisorInfo (com.alibaba.jstorm.daemon.supervisor.SupervisorInfo)5 FileNotFoundException (java.io.FileNotFoundException)5 IOException (java.io.IOException)5 TreeMap (java.util.TreeMap)5 AlreadyAliveException (backtype.storm.generated.AlreadyAliveException)4 InvalidTopologyException (backtype.storm.generated.InvalidTopologyException)4 KeyAlreadyExistsException (backtype.storm.generated.KeyAlreadyExistsException)4 KeyNotFoundException (backtype.storm.generated.KeyNotFoundException)4 NotAliveException (backtype.storm.generated.NotAliveException)4 TopologyAssignException (backtype.storm.generated.TopologyAssignException)4 TException (org.apache.thrift.TException)4