stateRepository
.findById(Integer.toString(partition))
.orElse(new StateDocument(partition));
- recordHandler.addPartition(partition, document.state);
+ log.info(
+ "{} - Offset of next unseen message for partition {}: {}",
+ id,
+ partition,
+ document.offset);
+ recordHandler.addPartition(partition, document.state, document.offset);
for (String user : document.state.keySet())
{
log.info(
Integer partition = tp.partition();
log.info("{} - removing partition: {}", id, partition);
this.partitions.remove(partition);
- Map<String, AdderResult> state = recordHandler.removePartition(partition);
- for (String user : state.keySet())
+ ApplicationState state = recordHandler.removePartition(partition);
+ log.info(
+ "{} - offset of next unseen message for partition {} is {}",
+ id,
+ partition,
+ state.getOffset());
+ for (String user : state.getAdderState().keySet())
{
log.info(
"{} - Saved state for partition={}|user={}: {}",
id,
partition,
user,
- state.get(user));
+ state.getAdderState().get(user));
}
Map<String, List<AdderResult>> results = adderResults.removePartition(partition);
- stateRepository.save(new StateDocument(partition, state, results));
+ stateRepository.save(
+ new StateDocument(
+ partition,
+ state.getAdderState(),
+ results,
+ state.getOffset()));
});
}
}
private final String id;
private final Map<Integer, AdderBusinessLogic> state = new HashMap<>();
+ private final Map<Integer, Long> next = new HashMap<>();
@KafkaHandler
public void addNumber(
@Header(KafkaHeaders.RECEIVED_PARTITION_ID)
Integer partition,
+ @Header(KafkaHeaders.OFFSET)
+ Long offset,
@Header(KafkaHeaders.RECEIVED_MESSAGE_KEY)
String user,
@Payload
MessageAddNumber message)
{
+ if (alreadySeen(partition, offset))
+ return;
log.debug("{} - Received {} for {} on {}", id, message, user, partition);
state.get(partition).addToSum(user, message.getNext());
+ rememberMessage(partition, offset);
throttle();
}
public void calculateSum(
@Header(KafkaHeaders.RECEIVED_PARTITION_ID)
Integer partition,
+ @Header(KafkaHeaders.OFFSET)
+ Long offset,
@Header(KafkaHeaders.RECEIVED_MESSAGE_KEY)
String user,
@Payload
MessageCalculateSum message)
{
+ if (alreadySeen(partition, offset))
+ return;
AdderResult result = state.get(partition).calculate(user);
log.info("{} - New result for {}: {}", id, user, result);
results.addResults(partition, user, result);
+ rememberMessage(partition, offset);
throttle();
}
+ private boolean alreadySeen(Integer partition, Long offset)
+ {
+ if (offset < next.get(partition))
+ {
+ log.warn(
+ "{}- Dropping duplicate message: offset={} < next={}",
+ id,
+ offset,
+ next.get(partition));
+
+ return true;
+ }
+ else
+ {
+ return false;
+ }
+ }
+
+ private void rememberMessage(Integer partition, Long offset)
+ {
+ next.put(partition, offset + 1);
+ }
+
private void throttle()
{
if (throttle.isPresent())
}
}
- protected void addPartition(Integer partition, Map<String, AdderResult> state)
+ protected void addPartition(Integer partition, Map<String, AdderResult> state, Long offset)
{
this.state.put(partition, new AdderBusinessLogic(state));
+ this.next.put(partition, offset);
}
- protected Map<String, AdderResult> removePartition(Integer partition)
+ protected ApplicationState removePartition(Integer partition)
{
- return this.state.remove(partition).getState();
+ ApplicationState state =
+ new ApplicationState(
+ this.next.get(partition),
+ this.state.remove(partition).getState());
+
+ this.next.remove(partition);
+
+ return state;
}
}