@GET @Path("/{instanceId}/slobrok") @Produces(MediaType.APPLICATION_JSON) public List<SlobrokEntryResponse> getSlobrokEntries( @PathParam("instanceId") String instanceId, @QueryParam("pattern") String pattern) { ApplicationInstanceReference reference = parseInstanceId(instanceId); ApplicationId applicationId = OrchestratorUtil.toApplicationId(reference); if (pattern == null) { pattern = DEFAULT_SLOBROK_PATTERN; } List<Mirror.Entry> entries = slobrokApi.lookup(applicationId, pattern); return entries.stream() .map(entry -> new SlobrokEntryResponse(entry.getName(), entry.getSpec())) .collect(Collectors.toList()); }
/** * Updates the list of matching recipients by querying the extern slobrok. */ private void update() { int upd = mirror.updates(); if (generation != upd) { generation = upd; recipients.clear(); Mirror.Entry[] arr = mirror.lookup(pattern); for (Mirror.Entry entry : arr) { recipients.add(Hop.parse(entry.getSpec() + session)); } } }
/** * Resolve a concrete address from this service. This service may represent multiple remote sessions, so this will * select one that is online. * * @return A concrete service address. */ public RPCServiceAddress resolve() { if (pattern.startsWith("tcp/")) { int pos = pattern.lastIndexOf('/'); if (pos > 0 && pos < pattern.length() - 1) { RPCServiceAddress ret = new RPCServiceAddress(pattern, pattern.substring(0, pos)); if (!ret.isMalformed()) { return ret; } } } else { if (addressGen != mirror.updates()) { addressGen = mirror.updates(); addressList = mirror.lookup(pattern); } if (addressList != null && addressList.length > 0) { addressIdx = ++addressIdx % addressList.length; Mirror.Entry entry = addressList[addressIdx]; return new RPCServiceAddress(entry.getName(), entry.getSpec()); } } return null; }
/** * Updates and returns the cache entry for the given routing context. This method assumes that synchronization is * handled outside of it. * * @param ctx The routing context. * @return The updated cache entry. */ private CacheEntry update(RoutingContext ctx) { String key = getCacheKey(ctx); CacheEntry entry = cache.get(key); if (entry == null) { entry = new CacheEntry(); cache.put(key, entry); } int upd = ctx.getMirror().updates(); if (entry.generation != upd) { entry.generation = upd; entry.recipients.clear(); Mirror.Entry[] arr = ctx.getMirror().lookup(ctx.getHopPrefix() + "*" + ctx.getHopSuffix()); String self = localAddress != null ? localAddress : toAddress(ctx.getMessageBus().getConnectionSpec()); for (Mirror.Entry item : arr) { if (self.equals(toAddress(item.getSpec()))) { entry.recipients.add(Hop.parse(item.getName())); } } } return entry; }
public String getTargetSpec(Integer distributor, RoutingContext context) { Mirror.Entry[] arr = getEntries(patternGenerator.getDistributorHostPattern(distributor), context); if (arr.length == 0) return null; if (distributor != null) { if (arr.length == 1) { return convertSlobrokNameToSessionName(arr[0].getSpec()); } else { log.log(LogLevel.WARNING, "Got " + arr.length + " matches for a distributor."); } } else { return convertSlobrokNameToSessionName(arr[randomizer.nextInt(arr.length)].getSpec()); } return null; } }
@Override public void doSelect(RoutingContext context) { LoadBalancer.Node node = getRecipient(context); if (node != null) { context.setContext(node); Route route = new Route(context.getRoute()); route.setHop(0, Hop.parse(node.entry.getSpec() + "/" + session)); context.addChild(route); } else { context.setError(ErrorCode.NO_ADDRESS_FOR_SERVICE, "Could not resolve any nodes to send to in pattern " + pattern); } }