-
Notifications
You must be signed in to change notification settings - Fork 116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce Kafka as backend store implementation #359
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
src/main/java/com/purbon/kafka/topology/backend/KafkaBackend.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package com.purbon.kafka.topology.backend; | ||
|
||
import com.purbon.kafka.topology.Configuration; | ||
import com.purbon.kafka.topology.backend.kafka.KafkaBackendConsumer; | ||
import com.purbon.kafka.topology.backend.kafka.KafkaBackendProducer; | ||
import com.purbon.kafka.topology.backend.kafka.RecordReceivedCallback; | ||
import java.io.IOException; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
import lombok.SneakyThrows; | ||
import org.apache.kafka.clients.consumer.ConsumerRecord; | ||
import org.apache.kafka.common.errors.WakeupException; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
|
||
public class KafkaBackend implements Backend, RecordReceivedCallback { | ||
|
||
private static final Logger LOGGER = LogManager.getLogger(KafkaBackend.class); | ||
|
||
private boolean isCompleted; | ||
|
||
private KafkaBackendConsumer consumer; | ||
private KafkaBackendProducer producer; | ||
|
||
private AtomicReference<BackendState> latest; | ||
private String instanceId; | ||
private Thread thread; | ||
|
||
public KafkaBackend() { | ||
isCompleted = false; | ||
} | ||
|
||
private static class JulieKafkaConsumerThread implements Runnable { | ||
private KafkaBackend callback; | ||
private KafkaBackendConsumer consumer; | ||
|
||
public JulieKafkaConsumerThread(KafkaBackend callback, KafkaBackendConsumer consumer) { | ||
this.callback = callback; | ||
this.consumer = consumer; | ||
} | ||
|
||
public void run() { | ||
consumer.start(); | ||
try { | ||
consumer.retrieve(callback); | ||
} catch (WakeupException ex) { | ||
LOGGER.trace(ex); | ||
} | ||
} | ||
} | ||
|
||
@SneakyThrows | ||
@Override | ||
public void configure(Configuration config) { | ||
instanceId = config.getJulieInstanceId(); | ||
latest = new AtomicReference<>(); | ||
|
||
consumer = new KafkaBackendConsumer(config); | ||
consumer.configure(); | ||
|
||
var topics = consumer.listTopics(); | ||
if (!topics.containsKey(config.getJulieKafkaConfigTopic())) { | ||
throw new IOException( | ||
"The internal julie kafka configuration topic topic " | ||
+ config.getJulieKafkaConfigTopic() | ||
+ " should exist in the cluster"); | ||
} | ||
producer = new KafkaBackendProducer(config); | ||
producer.configure(); | ||
|
||
thread = new Thread(new JulieKafkaConsumerThread(this, consumer), "kafkaJulieConsumer"); | ||
thread.start(); | ||
waitForCompletion(); | ||
} | ||
|
||
public synchronized void waitForCompletion() throws InterruptedException { | ||
while (!isCompleted) { | ||
wait(30000); | ||
} | ||
} | ||
|
||
public synchronized void complete() { | ||
isCompleted = true; | ||
notify(); | ||
} | ||
|
||
@Override | ||
public void save(BackendState state) throws IOException { | ||
producer.save(state); | ||
} | ||
|
||
@Override | ||
public BackendState load() throws IOException { | ||
return latest == null ? new BackendState() : latest.get(); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
consumer.stop(); | ||
producer.stop(); | ||
try { | ||
thread.join(); | ||
} catch (InterruptedException e) { | ||
LOGGER.error(e); | ||
} | ||
latest = null; | ||
thread = null; | ||
} | ||
|
||
@Override | ||
public void apply(ConsumerRecord<String, BackendState> record) { | ||
if (instanceId.equals(record.key()) && latest != null) { | ||
latest.set(record.value()); | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/main/java/com/purbon/kafka/topology/backend/kafka/JsonDeserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.purbon.kafka.topology.backend.kafka; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.purbon.kafka.topology.backend.BackendState; | ||
import java.io.IOException; | ||
import org.apache.kafka.common.errors.SerializationException; | ||
import org.apache.kafka.common.serialization.Deserializer; | ||
|
||
public class JsonDeserializer<T> implements Deserializer<BackendState> { | ||
|
||
private final ObjectMapper objectMapper = new ObjectMapper(); | ||
|
||
private Class<T> tClass; | ||
|
||
public JsonDeserializer() {} | ||
|
||
public JsonDeserializer(Class<T> tClass) { | ||
this.tClass = tClass; | ||
} | ||
|
||
@Override | ||
public BackendState deserialize(String s, byte[] bytes) { | ||
if (bytes == null) { | ||
return null; | ||
} | ||
BackendState data; | ||
try { | ||
data = objectMapper.readValue(bytes, BackendState.class); | ||
} catch (IOException e) { | ||
throw new SerializationException(e); | ||
} | ||
return data; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/com/purbon/kafka/topology/backend/kafka/JsonSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.purbon.kafka.topology.backend.kafka; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import java.util.Map; | ||
import org.apache.kafka.common.errors.SerializationException; | ||
import org.apache.kafka.common.serialization.Serializer; | ||
|
||
public class JsonSerializer<T> implements Serializer<T> { | ||
|
||
private final ObjectMapper objectMapper = new ObjectMapper(); | ||
|
||
@Override | ||
public void configure(Map<String, ?> props, boolean isKey) { | ||
// nothing to do | ||
} | ||
|
||
@Override | ||
public byte[] serialize(String topic, T data) { | ||
if (data == null) return null; | ||
|
||
try { | ||
return objectMapper.writeValueAsBytes(data); | ||
} catch (Exception e) { | ||
throw new SerializationException("Error serializing JSON message", e); | ||
} | ||
} | ||
|
||
@Override | ||
public void close() { | ||
// nothing to do | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
src/main/java/com/purbon/kafka/topology/backend/kafka/KafkaBackendConsumer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package com.purbon.kafka.topology.backend.kafka; | ||
|
||
import static org.apache.kafka.clients.consumer.ConsumerConfig.GROUP_ID_CONFIG; | ||
|
||
import com.purbon.kafka.topology.Configuration; | ||
import com.purbon.kafka.topology.backend.BackendState; | ||
import com.purbon.kafka.topology.backend.KafkaBackend; | ||
import java.time.Duration; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Properties; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
import org.apache.kafka.clients.consumer.ConsumerConfig; | ||
import org.apache.kafka.clients.consumer.ConsumerRecord; | ||
import org.apache.kafka.clients.consumer.ConsumerRecords; | ||
import org.apache.kafka.clients.consumer.KafkaConsumer; | ||
import org.apache.kafka.common.PartitionInfo; | ||
import org.apache.kafka.common.serialization.Serdes; | ||
|
||
public class KafkaBackendConsumer { | ||
|
||
private Configuration config; | ||
private KafkaConsumer<String, BackendState> consumer; | ||
|
||
private AtomicBoolean running; | ||
|
||
public KafkaBackendConsumer(Configuration config) { | ||
this.config = config; | ||
this.running = new AtomicBoolean(false); | ||
} | ||
|
||
public void configure() { | ||
Properties consumerProperties = config.asProperties(); | ||
consumerProperties.put( | ||
ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, Serdes.String().deserializer().getClass()); | ||
var serde = new JsonDeserializer<>(BackendState.class); | ||
consumerProperties.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, serde.getClass()); | ||
consumerProperties.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest"); | ||
consumerProperties.put( | ||
GROUP_ID_CONFIG, | ||
consumerProperties.get(GROUP_ID_CONFIG).toString() + System.currentTimeMillis()); | ||
consumer = new KafkaConsumer<>(consumerProperties); | ||
consumer.subscribe(Collections.singletonList(config.getJulieKafkaConfigTopic())); | ||
} | ||
|
||
public void retrieve(KafkaBackend callback) { | ||
|
||
while (running.get()) { | ||
ConsumerRecords<String, BackendState> records = consumer.poll(Duration.ofSeconds(1)); | ||
callback.complete(); | ||
for (ConsumerRecord<String, BackendState> record : records) { | ||
callback.apply(record); | ||
} | ||
consumer.commitAsync(); | ||
} | ||
} | ||
|
||
public void stop() { | ||
running.set(false); | ||
consumer.wakeup(); | ||
} | ||
|
||
public void start() { | ||
running.set(true); | ||
} | ||
|
||
public Map<String, List<PartitionInfo>> listTopics() { | ||
return consumer.listTopics(); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could be improved by allowing ENV overwrite