Skip to content

Commit

Permalink
Use random string instead of container ID in Kafka client id.
Browse files Browse the repository at this point in the history
Reliably getting the container ID is too expensive.
Having the K8s pod name in the Kafka client id should be enough in
most cases to associate a client id with the place where it is used.

Signed-off-by: Carsten Lohmann <carsten.lohmann@bosch.io>
  • Loading branch information
calohmn committed Sep 21, 2023
1 parent 2975700 commit 3db6ca6
Showing 1 changed file with 10 additions and 9 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, 2022 Contributors to the Eclipse Foundation
* Copyright (c) 2021, 2023 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
Expand All @@ -21,7 +21,6 @@
import java.util.concurrent.atomic.AtomicInteger;

import org.apache.kafka.clients.CommonClientConfigs;
import org.eclipse.hono.util.KubernetesContainerUtil;
import org.eclipse.hono.util.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -33,6 +32,8 @@ public abstract class AbstractKafkaConfigProperties {

private static final AtomicInteger ID_COUNTER = new AtomicInteger();

private static final String COMPONENT_UID_DEFAULT = getK8sComponentUId();

/**
* A logger to be shared with subclasses.
*/
Expand All @@ -50,15 +51,15 @@ public abstract class AbstractKafkaConfigProperties {
* Creates a new instance.
*/
protected AbstractKafkaConfigProperties() {
this.componentUId = getComponentUIdFromEnv();
this.componentUId = COMPONENT_UID_DEFAULT;
}

private String getComponentUIdFromEnv() {
final String k8sContainerId = KubernetesContainerUtil.getContainerId();
if (k8sContainerId != null && k8sContainerId.length() >= 12) {
private static String getK8sComponentUId() {
if (System.getenv("KUBERNETES_SERVICE_HOST") != null) {
// running in Kubernetes: use HOSTNAME env var containing the pod name
final String podName = System.getenv("HOSTNAME");
return String.format("%s_%s", podName, k8sContainerId.substring(0, 12));
final String random = UUID.randomUUID().toString().replaceAll("-", "").substring(0, 12);
return String.format("%s_%s", podName, random);
}
return null;
}
Expand Down Expand Up @@ -88,7 +89,7 @@ protected final void setSpecificClientConfig(final Map<String, String> specificC
* Sets the component unique ID to be included in the client ID along with a counter value.
* <p>
* This overrides the unique ID determined automatically in case this application is running in Kubernetes
* (consisting of Pod/Container ID then).
* (containing the Pod name then).
* <p>
* Setting {@code null} here means that a UUID will be used in the client ID instead of component unique ID and
* counter value.
Expand Down Expand Up @@ -133,7 +134,7 @@ public String getBootstrapServers() {
* has been applied.
* <p>
* It is ensured that the returned map contains a unique {@code client.id}. The client ID will be created from the
* given client name, followed by a unique ID (containing component identifiers if running in Kubernetes).
* given client name, followed by a unique ID (containing a component identifier if running in Kubernetes).
* An already set {@code client.id} property value will be used as prefix for the client ID.
*
* @param clientName A name for the client to include in the added {@code client.id} property.
Expand Down

0 comments on commit 3db6ca6

Please sign in to comment.