import org.apache.kafka.clients.consumer.Consumer;
import org.apache.kafka.common.TopicPartition;
-import java.time.Clock;
-import java.time.Duration;
-import java.time.Instant;
import java.util.*;
private final StateRepository stateRepository;
private final String id;
private final String topic;
- private final Clock clock;
- private final Duration commitInterval;
private final Consumer consumer;
private final Set<Integer> partitions = new HashSet<>();
- private Instant lastCommit = Instant.EPOCH;
- private boolean commitsEnabled = true;
-
@Override
public void onPartitionsAssigned(Collection<TopicPartition> partitions)
{
Integer partition = tp.partition();
log.info("{} - removing partition: {}", id, partition);
this.partitions.remove(partition);
- if (commitsEnabled)
+ Map<String, AdderResult> state = recordHandler.removePartition(partition);
+ Long offset = consumer.position(tp);
+ log.info(
+ "{} - offset of next unseen message for partition {} is {}",
+ id,
+ partition,
+ offset);
+ for (String user : state.keySet())
{
- Map<String, AdderResult> state = recordHandler.removePartition(partition);
- Long offset = consumer.position(tp);
log.info(
- "{} - offset of next unseen message for partition {} is {}",
+ "{} - Saved state for partition={}|user={}: {}",
id,
partition,
- offset);
- for (String user : state.keySet())
- {
- log.info(
- "{} - Saved state for partition={}|user={}: {}",
- id,
- partition,
- user,
- state.get(user));
- }
- Map<String, List<AdderResult>> results = adderResults.removePartition(partition);
- stateRepository.save(new StateDocument(partition, state, results, offset));
- }
- else
- {
- log.info("{} - Offset commits are disabled! Last commit: {}", id, lastCommit);
+ user,
+ state.get(user));
}
+ Map<String, List<AdderResult>> results = adderResults.removePartition(partition);
+ stateRepository.save(new StateDocument(partition, state, results, offset));
});
}
@Override
public void beforeNextPoll()
{
- if (!commitsEnabled)
- {
- log.info("{} - Offset commits are disabled! Last commit: {}", id, lastCommit);
- return;
- }
-
- if (lastCommit.plus(commitInterval).isBefore(clock.instant()))
- {
- partitions
- .stream()
- .forEach(partition ->
+ partitions
+ .stream()
+ .forEach(partition ->
+ {
+ log.info("{} - persisting state & offset for partition: {}", id, partition);
+ Map<String, AdderResult> state = recordHandler.getState(partition).getState();
+ Long offset = consumer.position(new TopicPartition(topic, partition));
+ log.info(
+ "{} - offset of next unseen message for partition {} is {}",
+ id,
+ partition,
+ offset);
+ for (String user : state.keySet())
{
- log.info("{} - persisting state & offset for partition: {}", id, partition);
- Map<String, AdderResult> state = recordHandler.getState(partition).getState();
- Long offset = consumer.position(new TopicPartition(topic, partition));
log.info(
- "{} - offset of next unseen message for partition {} is {}",
+ "{} - Saved state for partition={}|user={}: {}",
id,
partition,
- offset);
- for (String user : state.keySet())
- {
- log.info(
- "{} - Saved state for partition={}|user={}: {}",
- id,
- partition,
- user,
- state.get(user));
- }
- Map<String, List<AdderResult>> results = adderResults.getState(partition);
- stateRepository.save(new StateDocument(partition, state, results, offset));
- });
-
- lastCommit = clock.instant();
- }
- }
-
- @Override
- public void enableCommits()
- {
- commitsEnabled = true;
- }
-
- @Override
- public void disableCommits()
- {
- commitsEnabled = false;
+ user,
+ state.get(user));
+ }
+ Map<String, List<AdderResult>> results = adderResults.getState(partition);
+ stateRepository.save(new StateDocument(partition, state, results, offset));
+ });
}
}