Source:
<code>private void initializeStream() { final boolean subscriptionJustInitialized = initializeSubscriptionLocked(getZk(), getContext().getSubscription(), getContext().getTimelineService(), getContext().getCursorConverter()); if (!subscriptionJustInitialized) { final Collection<Session> sessions = getZk().listSessions(); final Partition[] partitions = getZk().getTopology().getPartitions(); <START> if (getZk().listSessions().size() >= partitions.length) { <END> switchState(new CleanupState(new NoStreamingSlotsAvailable(partitions.length))); return; } final List<EventTypePartition> requestedPartitions = getContext().getParameters().getPartitions(); final List<EventTypePartition> conflictPartitions = sessions.stream() .flatMap(s -> s.getRequestedPartitions().stream()) .filter(requestedPartitions::contains) .collect(Collectors.toList()); if (!conflictPartitions.isEmpty()) { switchState(new CleanupState(SubscriptionPartitionConflictException.of(conflictPartitions))); return; } } if (getZk().isCursorResetInProgress()) { switchState(new CleanupState( new NakadiException("Resetting subscription cursors request is still in progress") { @Override protected Response.StatusType getStatus() { return Response.Status.CONFLICT; } })); return; } try { getContext().registerSession(); } catch (Exception ex) { switchState(new CleanupState(ex)); return; } try { getOut().onInitialized(getSessionId()); switchState(new StreamingState()); } catch (final IOException e) { getLog().error("Failed to notify of initialization. Switch to cleanup directly", e); switchState(new CleanupState(e)); } }</code><technical_language>reuse sessions instead of calling getZk().listSessions() a time</technical_language>

Target:
private void initializeStream() { final boolean subscriptionJustInitialized = initializeSubscriptionLocked(getZk(), getContext().getSubscription(), getContext().getTimelineService(), getContext().getCursorConverter()); if (!subscriptionJustInitialized) { final Collection<Session> sessions = getZk().listSessions(); final Partition[] partitions = getZk().getTopology().getPartitions(); final List<EventTypePartition> requestedPartitions = getContext().getParameters().getPartitions(); if (requestedPartitions.isEmpty()) { final long directlyRequestedPartitionsCount = sessions.stream() .flatMap(s -> s.getRequestedPartitions().stream()) .distinct() .count(); final long autoSlotsCount = partitions.length - directlyRequestedPartitionsCount; final long autoBalanceSessionsCount = sessions.stream() .filter(s -> s.getRequestedPartitions().isEmpty()) .count(); if (autoBalanceSessionsCount >= autoSlotsCount) { switchState(new CleanupState(new NoStreamingSlotsAvailable(partitions.length))); return; } } final List<EventTypePartition> conflictPartitions = sessions.stream() .flatMap(s -> s.getRequestedPartitions().stream()) .filter(requestedPartitions::contains) .collect(Collectors.toList()); if (!conflictPartitions.isEmpty()) { switchState(new CleanupState(SubscriptionPartitionConflictException.of(conflictPartitions))); return; } } if (getZk().isCursorResetInProgress()) { switchState(new CleanupState( new NakadiException("Resetting subscription cursors request is still in progress") { @Override protected Response.StatusType getStatus() { return Response.Status.CONFLICT; } })); return; } try { getContext().registerSession(); } catch (Exception ex) { switchState(new CleanupState(ex)); return; } try { getOut().onInitialized(getSessionId()); switchState(new StreamingState()); } catch (final IOException e) { getLog().error("Failed to notify of initialization. Switch to cleanup directly", e); switchState(new CleanupState(e)); } }