/** * Returns the hashes covered by the member. If the hash of the routing key matches with one of the returned * hashes and the member is capable of handling the command then it will be selected as a target for the * command. * * @return the hashes covered by this member */ public Set<String> hashes() { return IntStream.range(0, segmentCount) .mapToObj(i -> hash(name() + " #" + i)) .collect(Collectors.toSet()); }
/** * Returns the collection of nodes, represented as {@link ConsistentHashMember}, in the order they would be * considered for the given routing key. Whether a CommandMessage would be forwarded to each of the candidates, * depends on the Command Filter of each node. * * @param routingKey The routing key to select ordering * @return A collection containing each of the nodes, in the order they would be considered */ public Collection<ConsistentHashMember> getEligibleMembers(String routingKey) { String hash = hash(routingKey); Collection<ConsistentHashMember> tail = hashToMember.tailMap(hash).values(); Collection<ConsistentHashMember> head = hashToMember.headMap(hash).values(); LinkedHashSet<ConsistentHashMember> combined = new LinkedHashSet<>(tail); combined.addAll(head); return combined; }
/** * Returns the member instance to which the given {@code message} should be routed. If no suitable member could be * found an empty Optional is returned. * * @param routingKey the routing that should be used to select a member * @param commandMessage the command message to find a member for * @return the member that should handle the message or an empty Optional if no suitable member was found */ public Optional<Member> getMember(String routingKey, CommandMessage<?> commandMessage) { String hash = hash(routingKey); Optional<Member> foundMember = findSuitableMember(commandMessage, hashToMember.tailMap(hash).values()); if (!foundMember.isPresent()) { foundMember = findSuitableMember(commandMessage, hashToMember.headMap(hash).values()); } return foundMember; }
/** * Returns the collection of nodes, represented as {@link ConsistentHashMember}, in the order they would be * considered for the given routing key. Whether a CommandMessage would be forwarded to each of the candidates, * depends on the Command Filter of each node. * * @param routingKey The routing key to select ordering * @return A collection containing each of the nodes, in the order they would be considered */ public Collection<ConsistentHashMember> getEligibleMembers(String routingKey) { String hash = hash(routingKey); Collection<ConsistentHashMember> tail = hashToMember.tailMap(hash).values(); Collection<ConsistentHashMember> head = hashToMember.headMap(hash).values(); LinkedHashSet<ConsistentHashMember> combined = new LinkedHashSet<>(tail); combined.addAll(head); return combined; }
/** * Returns the collection of nodes, represented as {@link ConsistentHashMember}, in the order they would be * considered for the given routing key. Whether a CommandMessage would be forwarded to each of the candidates, * depends on the Command Filter of each node. * * @param routingKey The routing key to select ordering * @return A collection containing each of the nodes, in the order they would be considered */ public Collection<ConsistentHashMember> getEligibleMembers(String routingKey) { String hash = hash(routingKey); Collection<ConsistentHashMember> tail = hashToMember.tailMap(hash).values(); Collection<ConsistentHashMember> head = hashToMember.headMap(hash).values(); LinkedHashSet<ConsistentHashMember> combined = new LinkedHashSet<>(tail); combined.addAll(head); return combined; }
/** * Returns the hashes covered by the member. If the hash of the routing key matches with one of the returned * hashes and the member is capable of handling the command then it will be selected as a target for the * command. * * @return the hashes covered by this member */ public Set<String> hashes() { Set<String> newHashes = new TreeSet<>(); for (int t = 0; t < segmentCount; t++) { String hash = hash(name() + " #" + t); newHashes.add(hash); } return newHashes; }
/** * Returns the hashes covered by the member. If the hash of the routing key matches with one of the returned * hashes and the member is capable of handling the command then it will be selected as a target for the * command. * * @return the hashes covered by this member */ public Set<String> hashes() { Set<String> newHashes = new TreeSet<>(); for (int t = 0; t < segmentCount; t++) { String hash = hash(name() + " #" + t); newHashes.add(hash); } return newHashes; }
/** * Returns the member instance to which the given {@code message} should be routed. If no suitable member could be * found an empty Optional is returned. * * @param routingKey the routing that should be used to select a member * @param commandMessage the command message to find a member for * @return the member that should handle the message or an empty Optional if no suitable member was found */ public Optional<Member> getMember(String routingKey, CommandMessage<?> commandMessage) { String hash = hash(routingKey); SortedMap<String, ConsistentHashMember> tailMap = hashToMember.tailMap(hash); Iterator<Map.Entry<String, ConsistentHashMember>> tailIterator = tailMap.entrySet().iterator(); Optional<Member> foundMember = findSuitableMember(commandMessage, tailIterator); if (!foundMember.isPresent()) { Iterator<Map.Entry<String, ConsistentHashMember>> headIterator = hashToMember.headMap(hash).entrySet().iterator(); foundMember = findSuitableMember(commandMessage, headIterator); } return foundMember; }
/** * Returns the member instance to which the given {@code message} should be routed. If no suitable member could be * found an empty Optional is returned. * * @param routingKey the routing that should be used to select a member * @param commandMessage the command message to find a member for * @return the member that should handle the message or an empty Optional if no suitable member was found */ public Optional<Member> getMember(String routingKey, CommandMessage<?> commandMessage) { String hash = hash(routingKey); SortedMap<String, ConsistentHashMember> tailMap = hashToMember.tailMap(hash); Iterator<Map.Entry<String, ConsistentHashMember>> tailIterator = tailMap.entrySet().iterator(); Optional<Member> foundMember = findSuitableMember(commandMessage, tailIterator); if (!foundMember.isPresent()) { Iterator<Map.Entry<String, ConsistentHashMember>> headIterator = hashToMember.headMap(hash).entrySet().iterator(); foundMember = findSuitableMember(commandMessage, headIterator); } return foundMember; }