@Override public String apply(ServiceInformation input) { if (input == null) { return null; } return input.getServiceName(); } };
public Builder<Type> addServiceInformation(final ServiceInformation serviceInformation) { Preconditions.checkArgument(serviceInformation != null, "serviceInformation must not be null"); final String serviceName = serviceInformation.getServiceName(); List<ServiceInformation> serviceInformations = Lists.newArrayList(); ConsistentRingGroup currentGroup = newWorldOrder.get(serviceName); if (currentGroup != null) { serviceInformations.addAll(currentGroup.getAll()); } serviceInformations.add(serviceInformation); newWorldOrder.put(serviceName, new ConsistentRingGroup(serviceInformations)); return this; }
@Override public void unannounce(final ServiceInformation serviceInformation) { Preconditions.checkArgument(serviceInformation != null, "serviceInformation must not be null!"); final Map<String, ConsistentRingGroup> worldOrder = getStateOfTheWorldHolder().getState(); ConsistentRingGroup group = worldOrder.get(serviceInformation.getServiceName()); if (group == null) { return; } Set<ServiceInformation> services = Sets.newHashSet(group.getAll()); services.remove(serviceInformation); worldOrder.put(serviceInformation.getServiceName(), new ConsistentRingGroup(services)); }
public ConsistentRingGroup(Collection<ServiceInformation> servers) { totalServers = servers.size(); Map<String, List<ServiceInformation>> serverGroups = Maps.newHashMap(); String serviceName = null; //Sort the servers by type for (ServiceInformation info: servers) { Preconditions.checkArgument(serviceName == null || StringUtils.equals(serviceName, info.getServiceName()), "All services must have the same name: " + servers); serviceName = info.getServiceName(); //Hashmaps allow null keys, so null service types should map correctly. if (!serverGroups.containsKey(info.getServiceType())) { serverGroups.put(info.getServiceType(), new ArrayList<ServiceInformation>()); } serverGroups.get(info.getServiceType()).add(info); } for (Map.Entry<String, List<ServiceInformation>> entry: serverGroups.entrySet()) { rings.put(entry.getKey(), new ConsistentHashRing(entry.getValue())); } }
@Override public void announce(final ServiceInformation serviceInformation) { Preconditions.checkArgument(serviceInformation != null, "serviceInformation must not be null!"); final Map<String, ConsistentRingGroup> worldOrder = getStateOfTheWorldHolder().getState(); final String serviceName = serviceInformation.getServiceName(); List<ServiceInformation> services = Lists.newArrayList(); ConsistentRingGroup currentGroup = worldOrder.get(serviceName); if (currentGroup != null) { services.addAll(currentGroup.getAll()); } if (!services.contains(serviceInformation)) { services.add(serviceInformation); worldOrder.put(serviceName, new ConsistentRingGroup(services)); } }
List<ServiceInformation> services = serviceMap.get(si.getServiceName()); if (services == null) { services = new ArrayList<ServiceInformation>(); serviceMap.put(si.getServiceName(), services);