--- /dev/null
+package de.juplo.kafka;
+
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+
+
+public class AdderResults
+{
+ private final Map<Integer, Map<String, List<AdderResult>>> results = new HashMap<>();
+
+
+ public void addResults(Integer partition, String user, AdderResult result)
+ {
+ Map<String, List<AdderResult>> resultsByUser = this.results.get(partition);
+
+ List<AdderResult> results = resultsByUser.get(user);
+ if (results == null)
+ {
+ results = new LinkedList<>();
+ resultsByUser.put(user, results);
+ }
+
+ results.add(result);
+ }
+
+ protected void addPartition(Integer partition, Map<String, List<AdderResult>> results)
+ {
+ this.results.put(partition, results);
+ }
+
+ protected Map<String, List<AdderResult>> removePartition(Integer partition)
+ {
+ return this.results.remove(partition);
+ }
+
+ public Map<Integer, Map<String, List<AdderResult>>> getState()
+ {
+ return results;
+ }
+
+ public Map<String, List<AdderResult>> getState(Integer partition)
+ {
+ return results.get(partition);
+ }
+}
public class ApplicationConfiguration
{
@Bean
- public ApplicationRecordHandler recordHandler()
+ public ApplicationRecordHandler recordHandler(AdderResults adderResults)
{
- return new ApplicationRecordHandler();
+ return new ApplicationRecordHandler(adderResults);
+ }
+
+ @Bean
+ public AdderResults adderResults()
+ {
+ return new AdderResults();
}
@Bean
public ApplicationRebalanceListener rebalanceListener(
ApplicationRecordHandler recordHandler,
+ AdderResults adderResults,
StateRepository stateRepository,
Consumer<String, String> consumer,
ApplicationProperties properties)
{
return new ApplicationRebalanceListener(
recordHandler,
+ adderResults,
stateRepository,
properties.getClientId(),
properties.getTopic(),
import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
-import java.util.Collection;
-import java.util.Map;
+import java.util.*;
@RequiredArgsConstructor
public class ApplicationRebalanceListener implements PollIntervalAwareConsumerRebalanceListener
{
private final ApplicationRecordHandler recordHandler;
+ private final AdderResults adderResults;
private final StateRepository stateRepository;
private final String id;
private final String topic;
private final Duration commitInterval;
private final Consumer<String, String> consumer;
+ private final Set<Integer> partitions = new HashSet<>();
+
private Instant lastCommit = Instant.EPOCH;
private boolean commitsEnabled = true;
partitions.forEach(tp ->
{
Integer partition = tp.partition();
+ this.partitions.add(partition);
StateDocument document =
stateRepository
.findById(Integer.toString(partition))
consumer.seek(tp, document.offset);
}
recordHandler.addPartition(partition, document.state);
+ adderResults.addPartition(partition, document.results);
});
}
partitions.forEach(tp ->
{
Integer partition = tp.partition();
+ this.partitions.remove(partition);
Long offset = consumer.position(tp);
log.info(
"{} - removing partition: {}, offset of next message {})",
offset);
if (commitsEnabled)
{
- Map<String, AdderResult> removed = recordHandler.removePartition(partition);
- stateRepository.save(new StateDocument(partition, removed, offset));
+ Map<String, AdderResult> state = recordHandler.removePartition(partition);
+ Map<String, List<AdderResult>> results = adderResults.removePartition(partition);
+ stateRepository.save(new StateDocument(partition, state, results, offset));
}
else
{
if (lastCommit.plus(commitInterval).isBefore(clock.instant()))
{
log.debug("Storing data and offsets, last commit: {}", lastCommit);
- recordHandler.getState().forEach((partiton, adder) -> stateRepository.save(
+ partitions.forEach(partition -> stateRepository.save(
new StateDocument(
- partiton,
- adder.getState(),
- consumer.position(new TopicPartition(topic, partiton)))));
+ partition,
+ recordHandler.getState(partition).getState(),
+ adderResults.getState(partition),
+ consumer.position(new TopicPartition(topic, partition)))));
lastCommit = clock.instant();
}
}
package de.juplo.kafka;
+import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import java.util.Map;
+@RequiredArgsConstructor
@Slf4j
public class ApplicationRecordHandler implements RecordHandler<String, String>
{
+ private final AdderResults results;
+
private final Map<Integer, AdderBusinessLogic> state = new HashMap<>();
{
AdderResult result = state.get(partition).calculate(user);
log.info("New result for {}: {}", user, result);
+ results.addResults(partition, user, result);
return;
}
{
return state;
}
+
+ public AdderBusinessLogic getState(Integer partition)
+ {
+ return state.get(partition);
+ }
}
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
+import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ExecutionException;
{
private final EndlessConsumer consumer;
private final ApplicationRecordHandler recordHandler;
+ private final AdderResults results;
@PostMapping("start")
}
@GetMapping("state/{user}")
- public ResponseEntity<Long> seen(@PathVariable String user)
+ public ResponseEntity<Long> state(@PathVariable String user)
{
for (AdderBusinessLogic adder : recordHandler.getState().values())
{
return ResponseEntity.notFound().build();
}
+ @GetMapping("results")
+ public Map<Integer, Map<String, List<AdderResult>>> results()
+ {
+ return results.getState();
+ }
+
+ @GetMapping("results/{user}")
+ public ResponseEntity<List<AdderResult>> results(@PathVariable String user)
+ {
+ for (Map<String, List<AdderResult>> resultsByUser : this.results.getState().values())
+ {
+ List<AdderResult> results = resultsByUser.get(user);
+ if (results != null)
+ return ResponseEntity.ok(results);
+ }
+
+ return ResponseEntity.notFound().build();
+ }
+
@ExceptionHandler
@ResponseStatus(HttpStatus.BAD_REQUEST)
import org.springframework.data.mongodb.core.mapping.Document;
import java.util.HashMap;
+import java.util.List;
import java.util.Map;
public String id;
public long offset = -1l;
public Map<String, AdderResult> state;
+ public Map<String, List<AdderResult>> results;
public StateDocument()
{
{
this.id = Integer.toString(partition);
this.state = new HashMap<>();
+ this.results = new HashMap<>();
}
public StateDocument(
Integer partition,
Map<String, AdderResult> state,
+ Map<String, List<AdderResult>> results,
long offset)
{
this.id = Integer.toString(partition);
this.state = state;
+ this.results = results;
this.offset = offset;
}
}