public class EndlessConsumer implements Runnable
{
private final ExecutorService executor;
+ private final PartitionStatisticsRepository repository;
private final String bootstrapServer;
private final String groupId;
private final String id;
private KafkaConsumer<String, String> consumer = null;
private Future<?> future = null;
- private final Map<Integer, Map<String, Integer>> seen = new HashMap<>();
+ private final Map<TopicPartition, PartitionStatistics> seen = new HashMap<>();
public EndlessConsumer(
ExecutorService executor,
+ PartitionStatisticsRepository repository,
String bootstrapServer,
String groupId,
String clientId,
String autoOffsetReset)
{
this.executor = executor;
+ this.repository = repository;
this.bootstrapServer = bootstrapServer;
this.groupId = groupId;
this.id = clientId;
props.put("bootstrap.servers", bootstrapServer);
props.put("group.id", groupId);
props.put("client.id", id);
+ props.put("enable.auto.commit", false);
props.put("auto.offset.reset", autoOffsetReset);
props.put("metadata.max.age.ms", "1000");
props.put("key.deserializer", StringDeserializer.class.getName());
partitions.forEach(tp ->
{
log.info("{} - removing partition: {}", id, tp);
- Map<String, Integer> removed = seen.remove(tp.partition());
- for (String key : removed.keySet())
+ PartitionStatistics removed = seen.remove(tp);
+ for (KeyCounter counter : removed.getStatistics())
{
log.info(
"{} - Seen {} messages for partition={}|key={}",
id,
- removed.get(key),
- removed,
- key);
+ counter.getResult(),
+ removed.getPartition(),
+ counter.getKey());
}
+ repository.save(new StatisticsDocument(removed, consumer.position(tp)));
});
}
partitions.forEach(tp ->
{
log.info("{} - adding partition: {}", id, tp);
- seen.put(tp.partition(), new HashMap<>());
+ StatisticsDocument document =
+ repository
+ .findById(tp.toString())
+ .orElse(new StatisticsDocument(tp));
+ consumer.seek(tp, document.offset);
+ seen.put(tp, new PartitionStatistics(document));
});
}
});
record.value()
);
- Integer partition = record.partition();
+ TopicPartition partition = new TopicPartition(record.topic(), record.partition());
String key = record.key() == null ? "NULL" : record.key();
- Map<String, Integer> byKey = seen.get(partition);
-
- if (!byKey.containsKey(key))
- byKey.put(key, 0);
-
- int seenByKey = byKey.get(key);
- seenByKey++;
- byKey.put(key, seenByKey);
+ seen.get(partition).increment(key);
}
}
}
}
}
- public Map<Integer, Map<String, Integer>> getSeen()
+ public Map<TopicPartition, PartitionStatistics> getSeen()
{
return seen;
}