Source:
public Mono<Map<String, Integer>> getEstimatedLag() { Map<String, Integer> earlyResult = new ConcurrentHashMap<>(); if (this.leaseStoreManager == null || this.feedContextClient == null) { return Mono.just(earlyResult); } return this.leaseStoreManager.getAllLeases() .flatMap(lease -> { ChangeFeedOptions options = new ChangeFeedOptions() .setMaxItemCount(1) .setPartitionKeyRangeId(lease.getLeaseToken()) .setStartFromBeginning(true) .setRequestContinuation(lease.getContinuationToken()); return this.feedContextClient.createDocumentChangeFeedQuery(this.feedContextClient.getContainerClient(), options) .take(1) .map(feedResponse -> { String ownerValue = lease.getOwner(); String sessionTokenLsn = feedResponse.getSessionToken(); String parsedSessionToken = sessionTokenLsn.substring(sessionTokenLsn.indexOf(PK_RANGE_ID_SEPARATOR)); String[] segments = parsedSessionToken.split(SEGMENT_SEPARATOR); String latestLsn = segments[0]; if (segments.length >= 2) { latestLsn = segments[1]; } if (ownerValue == null) { ownerValue = ""; } if (feedResponse.getResults() == null || feedResponse.getResults().size() == 0) { return Pair.of(ownerValue + "_" + lease.getLeaseToken(), 0); } Integer currentLsn = 0; Integer estimatedLag = 0; try { currentLsn = Integer.valueOf(feedResponse.getResults().get(0).get(PROPERTY_NAME_LSN).asText("0")); estimatedLag = Integer.valueOf(latestLsn); estimatedLag = estimatedLag - currentLsn + 1; } catch (NumberFormatException ex) { logger.warn("Unexpected Cosmos LSN found", ex); estimatedLag = -1; } return Pair.of(ownerValue + "_" + lease.getLeaseToken() + "_" + currentLsn + "_" + latestLsn, estimatedLag); }); }) .collectList() .map(valueList -> { Map<String, Integer> result = new ConcurrentHashMap<>(); for (Pair<String, Integer> pair : valueList) { result.put(pair.getKey(), pair.getValue()); } return result; }); }

Target:
public Mono<Map<String, Integer>> getEstimatedLag() { Map<String, Integer> earlyResult = new ConcurrentHashMap<>(); if (this.leaseStoreManager == null || this.feedContextClient == null) { return Mono.just(earlyResult); } return this.leaseStoreManager.getAllLeases() .flatMap(lease -> { ChangeFeedOptions options = new ChangeFeedOptions() .setMaxItemCount(1) .setPartitionKeyRangeId(lease.getLeaseToken()) .setStartFromBeginning(true) .setRequestContinuation(lease.getContinuationToken()); return this.feedContextClient.createDocumentChangeFeedQuery(this.feedContextClient.getContainerClient(), options) .take(1) .map(feedResponse -> { String ownerValue = lease.getOwner(); String sessionTokenLsn = feedResponse.getSessionToken(); String parsedSessionToken = sessionTokenLsn.substring(sessionTokenLsn.indexOf(PK_RANGE_ID_SEPARATOR)); String[] segments = StringUtils.split(parsedSessionToken, SEGMENT_SEPARATOR); String latestLsn = segments[0]; if (segments.length >= 2) { latestLsn = segments[1]; } if (ownerValue == null) { ownerValue = ""; } if (feedResponse.getResults() == null || feedResponse.getResults().size() == 0) { return Pair.of(ownerValue + "_" + lease.getLeaseToken(), 0); } Integer currentLsn = 0; Integer estimatedLag = 0; try { currentLsn = Integer.valueOf(feedResponse.getResults().get(0).get(PROPERTY_NAME_LSN).asText("0")); estimatedLag = Integer.valueOf(latestLsn); estimatedLag = estimatedLag - currentLsn + 1; } catch (NumberFormatException ex) { logger.warn("Unexpected Cosmos LSN found", ex); estimatedLag = -1; } return Pair.of(ownerValue + "_" + lease.getLeaseToken() + "_" + currentLsn + "_" + latestLsn, estimatedLag); }); }) .collectList() .map(valueList -> { Map<String, Integer> result = new ConcurrentHashMap<>(); for (Pair<String, Integer> pair : valueList) { result.put(pair.getKey(), pair.getValue()); } return result; }); }