private void setCurSequence(Application application) { List<Node> nodes = application.getSubscribedNodeManager().getNodeList(NodeType.JOB_TRACKER); totalNodes = CollectionUtils.sizeOf(nodes); if (totalNodes == 0) { curSequence = 0; } else if (totalNodes == 1) { curSequence = 0; } else { List<Node> copy = new ArrayList<Node>(nodes); Collections.sort( copy, new Comparator<Node>() { @Override public int compare(Node left, Node right) { return left.getCreateTime().compareTo(right.getCreateTime()); } }); int index = 0; for (Node node : copy) { if (node.getIdentity().equals(application.getConfig().getIdentity())) { // 当前节点 curSequence = index; break; } index++; } } }
public AbstractPreLoader(final Application application) { if (start.compareAndSet(false, true)) { scheduledFuture = LOAD_EXECUTOR_SERVICE.scheduleWithFixedDelay( new Runnable() { @Override public void run() { for (String loadTaskTrackerNodeGroup : LOAD_SIGNAL) { BlockingQueue<JobPo> queue = JOB_MAP.get(loadTaskTrackerNodeGroup); if (queue.size() / step < factor) { // load List<JobPo> loads = load(loadTaskTrackerNodeGroup, curSequence * step); // 加入到内存中 if (CollectionUtils.isNotEmpty(loads)) { queue.addAll(loads); } } LOAD_SIGNAL.remove(loadTaskTrackerNodeGroup); } } }, 500, 500, TimeUnit.MILLISECONDS); } application .getEventCenter() .subscribe( new EventSubscriber( application.getConfig().getIdentity() + "_preLoader", new Observer() { @Override public void onObserved(EventInfo eventInfo) { setCurSequence(application); } }), EcTopic.NODE_ADD, EcTopic.NODE_REMOVE); setCurSequence(application); }