예제 #1
0
 /**
  * Get any runnable node that is not one of the excluded nodes
  *
  * @param excluded the list of nodes to ignore
  * @return the runnable node, null if no runnable node can be found
  */
 public ClusterNode getRunnableNodeForAny(Set<String> excluded) {
   double avgLoad = loadManager.getAverageLoad(type);
   // Make two passes over the nodes. In the first pass, try to find a
   // node that has lower than average number of grants on it. If that does
   // not find a node, try looking at all nodes.
   for (int pass = 0; pass < 2; pass++) {
     for (Map.Entry<String, NodeContainer> e : hostToRunnableNodes.entrySet()) {
       NodeContainer nodeContainer = e.getValue();
       if (nodeContainer == null) {
         continue;
       }
       synchronized (nodeContainer) {
         if (nodeContainer.isEmpty()) {
           continue;
         }
         for (ClusterNode node : nodeContainer) {
           if (excluded == null || !excluded.contains(node.getHost())) {
             if (resourceLimit.hasEnoughResource(node)) {
               // When pass == 0, try to average out the load.
               if (pass == 0) {
                 if (node.getGrantCount(type) < avgLoad) {
                   return node;
                 }
               } else {
                 return node;
               }
             }
           }
         }
       }
     }
   }
   return null;
 }
예제 #2
0
 public X getSubTree(X parent) {
   NodeContainer<X> parentContainer = containerNodeDao.getContainerByNodeUID(parent.getUID());
   List<NodeContainer<X>> list =
       containerNodeDao.getNodeContainersBetween(
           parentContainer.getLeft(), parentContainer.getRight());
   return services.getNodeTree(list);
 }
예제 #3
0
    /**
     * Remove the node from the runnable indices
     *
     * @param node node to remove
     */
    public void deleteRunnable(ClusterNode node) {
      String host = node.getHost();

      if (LOG.isDebugEnabled()) {
        LOG.debug(node.getName() + " deleted from runnable list for type: " + type);
      }

      NodeContainer nodeContainer = hostToRunnableNodes.get(host);
      if (nodeContainer != null) {
        synchronized (nodeContainer) {
          if (nodeContainer.removeNode(node)) {
            /**
             * We are not removing the nodeContainer from runnable nodes map since we are
             * synchronizing operations with runnable indices on it
             */
            hostsWithRunnableNodes.decrementAndGet();
          }
        }
      }

      Node rack = node.hostNode.getParent();

      nodeContainer = rackToRunnableNodes.get(rack);
      if (nodeContainer != null) {
        synchronized (nodeContainer) {
          /**
           * We are not removing the nodeContainer from runnable nodes map since we are
           * synchronizing operations with runnable indices on it
           */
          nodeContainer.removeNode(node);
        }
      }
    }
예제 #4
0
    /**
     * Get a runnable node in the given rack that is not present in the excluded list
     *
     * @param requestedNode the node to look up rack locality for
     * @param excluded the list of nodes to ignore
     * @return the runnable node from the rack satisfying conditions, null if the node was not found
     */
    public ClusterNode getRunnableNodeForRack(RequestedNode requestedNode, Set<String> excluded) {

      NodeContainer nodeContainer = requestedNode.getRackNodes();
      getRunnableNodeForRackCounter += 1;
      if (nodeContainer == null) {
        return null;
      }
      synchronized (nodeContainer) {
        if (nodeContainer.isEmpty()) {
          return null;
        }
        if (getRunnableNodeForRackCounter % RACK_SHUFFLE_PERIOD == 0) {
          // This balances more evenly across nodes in a rack
          nodeContainer.shuffle();
        }
        for (ClusterNode node : nodeContainer) {
          if (excluded == null || !excluded.contains(node.getHost())) {
            if (resourceLimit.hasEnoughResource(node)) {
              return node;
            }
          }
        }
      }
      return null;
    }
예제 #5
0
  private void sendNodes() {
    // mark all nodes as required
    ReleasableIterator<WayContainer> wayIterator = allWays.iterate();
    while (wayIterator.hasNext()) {
      Way way = wayIterator.next().getEntity();

      if (!isRequiredWay(way)) continue;

      for (WayNode nodeReference : way.getWayNodes()) {
        long nodeId = nodeReference.getNodeId();
        requiredNodes.set(nodeId);
      }
    }
    wayIterator.release();

    ReleasableIterator<NodeContainer> nodeIterator = allNodes.iterate();
    while (nodeIterator.hasNext()) {
      NodeContainer nodeContainer = nodeIterator.next();
      if (!isRequiredNode(nodeContainer.getEntity())) {
        continue;
      }
      sink.process(nodeContainer);
    }

    nodeIterator.release();
  }
예제 #6
0
 public List<X> getNodePath(X node) {
   NodeContainer<X> nodeContainer = containerNodeDao.getContainerByNodeUID(node.getUID());
   List<X> retList = new ArrayList<X>();
   if (nodeContainer != null) {
     List<NodeContainer<X>> list =
         containerNodeDao.getNodeContainersExternal(
             nodeContainer.getLeft(), nodeContainer.getRight());
     for (NodeContainer<X> nc : list) retList.add(nc.getNode());
   }
   return retList;
 }
예제 #7
0
 /**
  * Get runnable node local to the given host
  *
  * @param requestedNode the requested node that needs local scheduling
  * @return the node that is local to the host, null if there are no runnable nodes local to the
  *     host
  */
 public ClusterNode getRunnableNodeForHost(RequestedNode requestedNode) {
   // there should only be one node per host in the common case
   NodeContainer nodeContainer = requestedNode.getHostNodes();
   if (nodeContainer == null) {
     return null;
   }
   synchronized (nodeContainer) {
     if (nodeContainer.isEmpty()) {
       return null;
     }
     for (ClusterNode node : nodeContainer) {
       if (resourceLimit.hasEnoughResource(node)) {
         return node;
       }
     }
   }
   return null;
 }
예제 #8
0
    /**
     * Add a node to the runnable indices
     *
     * @param clusterNode the node to add
     */
    public void addRunnable(ClusterNode clusterNode) {
      String host = clusterNode.getHost();

      if (LOG.isDebugEnabled()) {
        LOG.debug(clusterNode.getName() + " added to runnable list for type: " + type);
      }

      NodeContainer nodeContainer = getOrCreateHostRunnableNode(host);
      synchronized (nodeContainer) {
        nodeContainer.addNode(clusterNode);
        hostsWithRunnableNodes.incrementAndGet();
      }

      Node rack = clusterNode.hostNode.getParent();
      nodeContainer = getOrCreateRackRunnableNode(rack);
      synchronized (nodeContainer) {
        nodeContainer.addNode(clusterNode);
      }
    }
예제 #9
0
  private boolean containsBeginOfFileTag(Node node, String valueToFind) {
    if (node instanceof InlineTag) {
      InlineTag inlineTag = (InlineTag) node;
      InlineTagName name = inlineTag.getName();
      String value = inlineTag.getValue();
      if (name == InlineTagName.BEGINOFFILE && value.equals(valueToFind)) {
        return true;
      }
    }

    if (node instanceof NodeContainer<?> && !(node instanceof Chapter)) {
      NodeContainer<?> nodeContainer = (NodeContainer<?>) node;
      for (Node child : nodeContainer.getChildren()) {
        // recursive call
        if (containsBeginOfFileTag(child, valueToFind)) {
          return true;
        }
      }
    }
    return false;
  }
예제 #10
0
  /** {@inheritDoc} */
  @Override
  public void process(NodeContainer nodeContainer) {
    boolean modified;

    nodeBuilder.initialize(nodeContainer.getEntity());

    modified = false;
    modified = modified || processEntity(nodeBuilder);
    modified = modified || processNode(nodeBuilder);

    if (modified) {
      sink.process(new NodeContainer(nodeBuilder.buildEntity()));
    } else {
      sink.process(nodeContainer);
    }
  }
예제 #11
0
 /**
  * Create a snapshot of runnable nodes.
  *
  * @return The snapshot.
  */
 public NodeSnapshot getNodeSnapshot() {
   int nodeCount = 0;
   Map<String, NodeContainer> hostRunnables = new HashMap<String, NodeContainer>();
   for (Map.Entry<String, NodeContainer> entry : hostToRunnableNodes.entrySet()) {
     NodeContainer value = entry.getValue();
     synchronized (value) {
       if (!value.isEmpty()) {
         hostRunnables.put(entry.getKey(), value.copy());
         nodeCount += value.size();
       }
     }
   }
   Map<Node, NodeContainer> rackRunnables = new HashMap<Node, NodeContainer>();
   for (Map.Entry<Node, NodeContainer> entry : rackToRunnableNodes.entrySet()) {
     NodeContainer value = entry.getValue();
     synchronized (value) {
       if (!value.isEmpty()) {
         rackRunnables.put(entry.getKey(), value.copy());
       }
     }
   }
   return new NodeSnapshot(topologyCache, hostRunnables, rackRunnables, nodeCount);
 }
예제 #12
0
 /**
  * Checks if a node is present as runnable in this index. Should be called while holding the
  * node lock.
  *
  * @param clusterNode The node.
  * @return A boolean indicating if the node is present.
  */
 public boolean hasRunnable(ClusterNode clusterNode) {
   String host = clusterNode.getHost();
   NodeContainer nodeContainer = hostToRunnableNodes.get(host);
   return (nodeContainer != null) && !nodeContainer.isEmpty();
 }
예제 #13
0
 public void saveProduct(X p) throws TreeNodeException {
   /* parent check */
   if (p.getParent() == null) throw new TreeNodeException("Need to append to a parent");
   containerNodeDao.startTransaction();
   final NodeContainer<X> parent = containerNodeDao.getContainerByNodeUID(p.getParent().getUID());
   if (parent == null) throw new TreeNodeException("Need to append to a saved parent");
   /* load nodes to be updated */
   List<NodeContainer<X>> rightMost = containerNodeDao.getRightMostProducts(parent.getRight() - 1);
   List<NodeContainer<X>> leftMost = containerNodeDao.getLeftMostProducts(parent.getRight() - 1);
   List<NodeContainer<X>> toBeUpdated = new ArrayList<NodeContainer<X>>();
   int incr = (1 + services.countDescendants(p)) * 2;
   /* setting increment (total nodes to be added) */
   for (NodeContainer<X> r : rightMost) {
     r.setRight(r.getRight() + incr);
   }
   toBeUpdated.addAll(rightMost);
   for (NodeContainer<X> r : leftMost) {
     int incLeft = r.getLeft() + incr;
     if (toBeUpdated.contains(r)) rightMost.get(rightMost.indexOf(r)).setLeft(incLeft);
     else {
       r.setLeft(incLeft);
       toBeUpdated.add(r);
     }
   }
   containerNodeDao.saveOrUpdateProducts(toBeUpdated);
   /* add new nodes */
   NodeContainer<X> rightDesc = containerNodeDao.getMaxRightDescendant(parent);
   int start = Math.max(parent.getLeft() + 1, (rightDesc != null) ? rightDesc.getRight() : 0 + 1);
   toBeUpdated.addAll(services.getNodeContainerList(p, start));
   containerNodeDao.saveOrUpdateProducts(toBeUpdated);
   containerNodeDao.commitTransaction();
 }
예제 #14
0
 public void integrateInto(NodeContainer pane) {
   pane.add(styleInput);
 }