Source:
<code>public List<AgentBatchResponseItem> processRequests(List<BaragonRequestBatchItem> batch) throws InterruptedException { Map<String, Optional<BaragonRequest>> requests = new HashMap<>(); Map<String, Collection<UpstreamInfo>> existingUpstreamsForThisBatch = batch.stream() .map(requestItem -> { final Optional<BaragonRequest> maybeRequest = requestDatastore.getRequest(requestItem.getRequestId()); requests.put(requestItem.getRequestId(), maybeRequest); Optional<BaragonService> oldService; if (maybeRequest.isPresent()) { <START> oldService = getOldService(maybeRequest.get()); <END> } else { oldService = Optional.absent(); } return oldService; }) .filter(Optional::isPresent) .map(Optional::get) .distinct() .collect(Collectors.toMap(BaragonService::getServiceId, service -> { try { return stateDatastore.getUpstreams(service.getServiceId()); } catch (Exception e) { LOG.warn("Unable to get upstream information for service {}", service.getServiceId(), e); return new ArrayList<>(); } })); List<AgentBatchResponseItem> responses = new ArrayList<>(batch.size()); int i = 0; for (BaragonRequestBatchItem item : batch) { boolean isLast = i == batch.size() - 1; responses.add(getResponseItem(processRequest(item.getRequestId(), requests.get(item.getRequestId()), existingUpstreamsForThisBatch, actionForBatchItem(item), !isLast, Optional.of(i)), item)); i++; } return responses; }</code><technical_language>save service instead of in processRequest</technical_language>

Target:
public List<AgentBatchResponseItem> processRequests(List<BaragonRequestBatchItem> batch) throws InterruptedException { Map<String, Optional<BaragonRequest>> requests = new HashMap<>(); Map<String, Optional<BaragonService>> services = new HashMap<>(); Map<String, Long> numRequestsByService = batch.stream() .map(requestItem -> { final Optional<BaragonRequest> maybeRequest = requestDatastore.getRequest(requestItem.getRequestId()); requests.put(requestItem.getRequestId(), maybeRequest); Optional<BaragonService> oldService = Optional.absent(); if (maybeRequest.isPresent()) { oldService = services.getOrDefault(maybeRequest.get().getLoadBalancerService().getServiceId(), getOldService(maybeRequest.get())); services.put(maybeRequest.get().getLoadBalancerService().getServiceId(), oldService); } return oldService; }) .filter(Optional::isPresent) .map(Optional::get) .collect(Collectors.groupingBy(BaragonService::getServiceId, Collectors.counting())); LOG.debug("Requests in this batch by service: {}", numRequestsByService); Map<String, Collection<UpstreamInfo>> existingUpstreamsForThisBatch = numRequestsByService.keySet().stream() .collect(Collectors.toMap(Function.identity(), serviceId -> { try { return stateDatastore.getUpstreams(serviceId); } catch (Exception e) { LOG.warn("Unable to get upstream information for service {}", serviceId, e); throw new RuntimeException("Unable to get upstream information for service {}", e); } })); List<AgentBatchResponseItem> responses = new ArrayList<>(batch.size()); int i = 0; for (BaragonRequestBatchItem item : batch) { boolean isLast = i == batch.size() - 1; responses.add(getResponseItem(processRequest(item.getRequestId(), existingUpstreamsForThisBatch, services, requests, actionForBatchItem(item), !isLast, Optional.of(i)), item)); i++; } return responses; }