santosh-d3vpl3x commented on code in PR #18843:
URL: https://github.com/apache/druid/pull/18843#discussion_r2901258448


##########
extensions-contrib/consul-extensions/src/main/java/org/apache/druid/consul/discovery/DefaultConsulApiClient.java:
##########
@@ -0,0 +1,283 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.druid.consul.discovery;
+
+import com.ecwid.consul.v1.ConsulClient;
+import com.ecwid.consul.v1.QueryParams;
+import com.ecwid.consul.v1.Response;
+import com.ecwid.consul.v1.agent.model.NewService;
+import com.ecwid.consul.v1.health.HealthServicesRequest;
+import com.ecwid.consul.v1.health.model.HealthService;
+import com.ecwid.consul.v1.kv.model.GetValue;
+import com.ecwid.consul.v1.kv.model.PutParams;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.common.base.Preconditions;
+import org.apache.druid.discovery.DiscoveryDruidNode;
+import org.apache.druid.discovery.NodeRole;
+import org.apache.druid.java.util.common.StringUtils;
+import org.apache.druid.java.util.common.logger.Logger;
+
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.Base64;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Default implementation of {@link ConsulApiClient} using the Ecwid Consul 
client library.
+ */
+public class DefaultConsulApiClient implements ConsulApiClient
+{
+  private static final Logger LOGGER = new 
Logger(DefaultConsulApiClient.class);
+
+  // Consul service metadata has a limit of 512 characters per value
+  // Use a safe limit to avoid edge cases (450 chars leaves room for key name 
overhead)
+  private static final int MAX_METADATA_VALUE_SIZE = 450;
+  private static final long MIN_SESSION_TTL_SECONDS = 30;
+  private static final long MIN_HEALTH_CHECK_INTERVAL_SECONDS = 1;
+
+  private final ConsulClient consulClient;
+  private final ConsulDiscoveryConfig config;
+  private final ObjectMapper jsonMapper;
+
+  public DefaultConsulApiClient(
+      ConsulClient consulClient,
+      ConsulDiscoveryConfig config,
+      ObjectMapper jsonMapper
+  )
+  {
+    this.consulClient = Preconditions.checkNotNull(consulClient, 
"consulClient");
+    this.config = Preconditions.checkNotNull(config, "config");
+    this.jsonMapper = Preconditions.checkNotNull(jsonMapper, "jsonMapper");
+
+    LOGGER.info(
+        "Created DefaultConsulApiClient for [%s:%d] with service prefix [%s]",
+        config.getConnection().getHost(),
+        config.getConnection().getPort(),
+        config.getService().getServicePrefix()
+    );
+  }
+
+  @Override
+  public void registerService(DiscoveryDruidNode node) throws Exception
+  {
+    String serviceId = ConsulServiceIds.serviceId(config, node);
+    String serviceName = ConsulServiceIds.serviceName(config, 
node.getNodeRole());
+
+    NewService service = new NewService();
+    service.setId(serviceId);
+    service.setName(serviceName);
+    service.setAddress(node.getDruidNode().getHost());
+    service.setPort(node.getDruidNode().getPortToUse());
+
+    List<String> tags = new ArrayList<>();
+    tags.add("druid");
+    tags.add("role:" + node.getNodeRole().getJsonName());
+    if (config.getService().getServiceTags() != null) {
+      for (Map.Entry<String, String> e : 
config.getService().getServiceTags().entrySet()) {
+        if (e.getKey() != null && e.getValue() != null) {
+          tags.add(e.getKey() + ":" + e.getValue());
+        }
+      }
+    }
+    service.setTags(tags);
+
+    // Serialize the full DiscoveryDruidNode as metadata
+    String nodeJson = jsonMapper.writeValueAsString(node);
+
+    // Consul service metadata has a 512 character limit per value
+    // If the JSON is too large, store it in Consul KV and reference it from 
metadata
+    Map<String, String> meta = new HashMap<>();
+    if (nodeJson.length() <= MAX_METADATA_VALUE_SIZE) {
+      // Small enough - store directly in metadata
+      meta.put("druid_node", nodeJson);
+    } else {

Review Comment:
   Fixed by checking the serialized metadata size using UTF-8 bytes instead of 
Java string length. The constant and log message now reflect the real 
bytes-on-the-wire limit that Consul enforces.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to