TGridClientNodePtr GridClientPartitionAffinity::findNode(const GridUuid& id, const TNodesSet& nodes) const { TNodesSet::const_iterator iter = find_if(nodes.begin(), nodes.end(), [&id] (const GridClientNode& node) { return node.getNodeId() == id; }); assert(iter != nodes.end()); return TGridClientNodePtr(new GridClientNode(*iter)); }
TGridClientNodePtr GridClientPartitionAffinity::getNode(const TNodesSet& nodes, const GridHasheableObject& key) { set<GridUuid> newNodes; for (auto iter = nodes.begin(); iter != nodes.end(); ++iter) newNodes.insert(iter->getNodeId()); GridUuid nodeId; { boost::lock_guard<boost::mutex> lock(mux); if (addedNodes != newNodes) { // Recreate the consistent hash ring. addedNodes.clear(); nodeHash.clear(); for (auto iter = nodes.begin(); iter != nodes.end(); ++iter) { GridClientNode n = *iter; add(n, getReplicas(n)); } } int32_t part = abs(key.hashCode() % parts); std::set<NodeInfo> nInfos; for (TNodesSet::const_iterator i = nodes.begin(); i != nodes.end(); i++) nInfos.insert(NodeInfo(i->getNodeId(), std::shared_ptr<GridHasheableObject>(new GridClientVariantHasheableObject(hashIdResolver(*i))))); nodeId = nodeHash.node(GridInt32Hasheable(part), nInfos).id(); } return findNode(nodeId, nodes); }
TGridClientNodeList GridClientComputeProjectionImpl::nodes(std::function<bool(const GridClientNode&)> filter) const { TGridClientNodeList nodes; TNodesSet ns; subProjectionNodes(ns); for (auto it = ns.begin(); it != ns.end(); ++it) { GridClientNode* gcn = new GridClientNode(*it); TGridClientNodePtr p = TGridClientNodePtr(gcn); if (filter(*p)) nodes.push_back(p); } return nodes; }
/** * Gets nodes that passes the filter. If this compute instance is a projection, then only * nodes that passes projection criteria will be passed to the filter. * * @param filter Node filter. * @return Collection of nodes that satisfy provided filter. */ TGridClientNodeList GridClientComputeProjectionImpl::nodes(const std::vector<GridUuid>& ids) const { TGridClientNodeList nodes; std::set<GridUuid> nodeIds(ids.begin(), ids.end()); TNodesSet ns; subProjectionNodes(ns); for (auto it = ns.begin(); it != ns.end(); ++it) { if (nodeIds.find(it->getNodeId()) == nodeIds.end()) continue; nodes.push_back(TGridClientNodePtr(new GridClientNode(*it))); } return nodes; }
void GridClientImpl::fireTopologyEvents(const TNodesSet& updNodes, const TNodesSet& prevNodes) { ClientNodeComparator nodeComp; TNodesSet newNodes; // Calculate new nodes. std::set_difference(updNodes.begin(), updNodes.end(), prevNodes.begin(), prevNodes.end(), std::inserter(newNodes, newNodes.begin()), nodeComp); // File onNodeAdded() events. for (auto i = newNodes.begin(); i != newNodes.end(); i++) fireNodeAdded(*i); TNodesSet leftNodes; // Calculate left nodes. std::set_difference(prevNodes.begin(), prevNodes.end(), updNodes.begin(), updNodes.end(), std::inserter(leftNodes, leftNodes.begin()), nodeComp); // Fire onNodeRemoved() events. for (auto i = leftNodes.begin(); i != leftNodes.end(); i++) fireNodeRemoved(*i); }