Source:
<code>RecordFlow evictAckedEventAndScheduleNextEvent(RecordsDeliveryAck recordsDeliveryAck) { final RecordsRetrievedContext recordsRetrievedContext = recordsDeliveryQueue.peek(); RecordFlow flowToBeReturned = null; final RecordsRetrieved recordsRetrieved = recordsRetrievedContext != null ? recordsRetrievedContext.getRecordsOrShutDownEvent() <START> .map(recordsEvent -> recordsEvent, shutDownEvent -> null) : null; <END> if (recordsRetrieved != null && recordsRetrieved.batchUniqueIdentifier() .equals(recordsDeliveryAck.batchUniqueIdentifier())) { recordsDeliveryQueue.poll(); takeDelayedDeliveryActionIfRequired(shardId, recordsRetrievedContext.getEnqueueTimestamp(), log); currentSequenceNumber = ((FanoutRecordsRetrieved)recordsRetrieved).continuationSequenceNumber(); flowToBeReturned = recordsRetrievedContext.getRecordFlow(); if (!recordsDeliveryQueue.isEmpty()) { recordsDeliveryQueue.peek().getRecordsOrShutDownEvent() .apply(recordsEvent -> scheduleNextEvent(recordsEvent), shutDownEvent -> shutDownEvent.getSubscriptionShutDownAction().run()); } } else { if (flow != null && recordsDeliveryAck.batchUniqueIdentifier().getFlowIdentifier() .equals(flow.getSubscribeToShardId())) { log.error( "{}: Received unexpected ack for the active subscription {}. Throwing. ", shardId, recordsDeliveryAck.batchUniqueIdentifier().getFlowIdentifier()); throw new IllegalStateException("Unexpected ack for the active subscription"); } else { log.info("{}: Publisher received an ack for stale subscription {}. Ignoring.", shardId, recordsDeliveryAck.batchUniqueIdentifier().getFlowIdentifier()); } } return flowToBeReturned; }</code><technical_language>move this logic out RecordsRetrievedContext. getRecordsRetrieved returns null records depending event type stored in context</technical_language>

Target:
RecordFlow evictAckedEventAndScheduleNextEvent(RecordsDeliveryAck recordsDeliveryAck) { final RecordsRetrievedContext recordsRetrievedContext = recordsDeliveryQueue.peek(); RecordFlow flowToBeReturned = null; final RecordsRetrieved recordsRetrieved = recordsRetrievedContext != null ? recordsRetrievedContext.getRecordsRetrieved() : null; if (recordsRetrieved != null && recordsRetrieved.batchUniqueIdentifier() .equals(recordsDeliveryAck.batchUniqueIdentifier())) { recordsDeliveryQueue.poll(); takeDelayedDeliveryActionIfRequired(shardId, recordsRetrievedContext.getEnqueueTimestamp(), log); currentSequenceNumber = ((FanoutRecordsRetrieved)recordsRetrieved).continuationSequenceNumber(); flowToBeReturned = recordsRetrievedContext.getRecordFlow(); if (!recordsDeliveryQueue.isEmpty()) { recordsDeliveryQueue.peek().executeEventAction(subscriber); } } else { if (flow != null && recordsDeliveryAck.batchUniqueIdentifier().getFlowIdentifier() .equals(flow.getSubscribeToShardId())) { log.error( "{}: Received unexpected ack for the active subscription {}. Throwing. ", shardId, recordsDeliveryAck.batchUniqueIdentifier().getFlowIdentifier()); throw new IllegalStateException("Unexpected ack for the active subscription"); } else { log.info("{}: Publisher received an ack for stale subscription {}. Ignoring.", shardId, recordsDeliveryAck.batchUniqueIdentifier().getFlowIdentifier()); } } return flowToBeReturned; }