-
Notifications
You must be signed in to change notification settings - Fork 57
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
1. Kafka: Initial integration changes #1492
Open
khansaad
wants to merge
3
commits into
kruize:mvp_demo
Choose a base branch
from
khansaad:kafka-1
base: mvp_demo
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 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
Large diffs are not rendered by default.
Oops, something went wrong.
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
64 changes: 64 additions & 0 deletions
64
src/main/java/com/autotune/utils/kafka/KruizeKafkaConsumer.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,64 @@ | ||
package com.autotune.utils.kafka; | ||
|
||
import com.autotune.operator.KruizeDeploymentInfo; | ||
import org.apache.kafka.clients.consumer.ConsumerConfig; | ||
import org.apache.kafka.clients.consumer.KafkaConsumer; | ||
import org.apache.kafka.common.serialization.StringDeserializer; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.Properties; | ||
import java.util.Scanner; | ||
|
||
//TODO: This class is not being used for now, will be updated later | ||
public class KruizeKafkaConsumer implements Runnable { | ||
private static KafkaConsumer<String, String> consumer; | ||
private static final Logger LOGGER = LoggerFactory.getLogger(KruizeKafkaConsumer.class); | ||
|
||
@Override | ||
public void run() { | ||
|
||
// Flag to control the loop and terminate when needed | ||
boolean continueListening = true; | ||
|
||
try { | ||
consumer = getKafkaConsumerConfig(); | ||
consumer.subscribe(java.util.Collections.singletonList(KruizeDeploymentInfo.kafka_topic_inbound)); | ||
while (continueListening) { | ||
consumer.poll(java.time.Duration.ofMillis(100)).forEach(record -> { | ||
LOGGER.info("Received Recommendation: JobID={}, Value={}, Partition={}, Offset={}", | ||
record.key(), record.value(), record.partition(), record.offset()); | ||
}); | ||
if (isTerminationSignalReceived()) { | ||
continueListening = false; | ||
} | ||
} | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
private KafkaConsumer<String, String> getKafkaConsumerConfig() { | ||
Properties consumerProps = new Properties(); | ||
consumerProps.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, KruizeDeploymentInfo.kafka_bootstrap_servers); | ||
consumerProps.put(ConsumerConfig.GROUP_ID_CONFIG, KruizeDeploymentInfo.kafka_group_id); | ||
consumerProps.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName()); | ||
consumerProps.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName()); | ||
|
||
return new KafkaConsumer<>(consumerProps); | ||
} | ||
|
||
// Shutdown hook for the consumer | ||
private static void addConsumerShutdownHook() { | ||
Runtime.getRuntime().addShutdownHook(new Thread(() -> { | ||
if (consumer != null) { | ||
consumer.close(); | ||
} | ||
})); | ||
} | ||
|
||
private static boolean isTerminationSignalReceived() { | ||
Scanner scanner = new Scanner(System.in); | ||
return scanner.hasNext(); // This will return true if any input arrives | ||
} | ||
} |
97 changes: 97 additions & 0 deletions
97
src/main/java/com/autotune/utils/kafka/KruizeKafkaProducer.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,97 @@ | ||
package com.autotune.utils.kafka; | ||
|
||
import com.autotune.operator.KruizeDeploymentInfo; | ||
import com.autotune.utils.KruizeConstants; | ||
import org.apache.kafka.clients.producer.KafkaProducer; | ||
import org.apache.kafka.clients.producer.ProducerConfig; | ||
import org.apache.kafka.clients.producer.ProducerRecord; | ||
import org.apache.kafka.clients.producer.RecordMetadata; | ||
import org.apache.kafka.common.serialization.StringSerializer; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.Properties; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.TimeoutException; | ||
|
||
public class KruizeKafkaProducer { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(KruizeKafkaProducer.class); | ||
|
||
// Singleton Kafka Producer Instance | ||
private static final KafkaProducer<String, String> producer = new KafkaProducer<>(getProducerProperties()); | ||
|
||
// Get Kafka producer properties | ||
private static Properties getProducerProperties() { | ||
Properties producerProps = new Properties(); | ||
producerProps.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, KruizeDeploymentInfo.kafka_bootstrap_servers); | ||
producerProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName()); | ||
producerProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName()); | ||
producerProps.put(ProducerConfig.ACKS_CONFIG, "all"); | ||
return producerProps; | ||
} | ||
|
||
// Kafka message producer | ||
private static void sendMessage(String topic, String payload) { | ||
try { | ||
RecordMetadata metadata = producer.send(new ProducerRecord<>(topic, payload)) | ||
.get(5, TimeUnit.SECONDS); //todo : set the timeout value via ENV | ||
// todo: get the status of message whether its delivered or failed | ||
LOGGER.debug("Message sent successfully to topic {} at partition {} and offset {}", | ||
metadata.topic(), metadata.partition(), metadata.offset()); | ||
} catch (TimeoutException te) { | ||
LOGGER.error("Kafka timeout while sending message to topic {}: {}", topic, te.getMessage()); | ||
} catch (Exception e) { | ||
LOGGER.error("Error sending message to Kafka topic {}: {}", topic, e.getMessage(), e); | ||
} | ||
} | ||
|
||
// Send valid recommendation messages | ||
public static class ValidRecommendationMessageProducer implements Runnable { | ||
private final String payload; | ||
|
||
public ValidRecommendationMessageProducer(String payload) { | ||
this.payload = payload; | ||
} | ||
|
||
@Override | ||
public void run() { | ||
sendMessage(KruizeConstants.KAFKA_CONSTANTS.RECOMMENDATIONS_TOPIC, payload); | ||
} | ||
} | ||
|
||
// Send error messages | ||
public static class ErrorMessageProducer implements Runnable { | ||
private final String errorDetails; | ||
|
||
public ErrorMessageProducer(String errorDetails) { | ||
this.errorDetails = errorDetails; | ||
} | ||
|
||
@Override | ||
public void run() { | ||
sendMessage(KruizeConstants.KAFKA_CONSTANTS.ERROR_TOPIC, errorDetails); | ||
} | ||
} | ||
|
||
// Send summary messages | ||
public static class SummaryResponseMessageProducer implements Runnable { | ||
private final String payload; | ||
|
||
public SummaryResponseMessageProducer(String payload) { | ||
this.payload = payload; | ||
} | ||
|
||
@Override | ||
public void run() { | ||
sendMessage(KruizeConstants.KAFKA_CONSTANTS.SUMMARY_TOPIC, payload); | ||
} | ||
} | ||
|
||
// Close the Kafka producer | ||
public static void close() { | ||
if (producer != null) { | ||
producer.close(); | ||
LOGGER.info("Kafka producer closed."); | ||
} | ||
} | ||
} |
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.
is it possible to set via env variable ?
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.
Please create issue to address following
Client Validation Flow:
Authentication: Broker checks client credentials (SASL or TLS certs).
Authorization: Broker verifies the client has the necessary ACLs.
Data Integrity: TLS encryption protects the messages during transmission.
Please verify if this configuration is getting matched with ROS
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.
Created #1497 to track this
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.
@khansaad Are you addressing setting the kafka bootstrap server using env in this PR?