You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@solr.apache.org by st...@apache.org on 2023/09/15 04:00:44 UTC

[solr] branch branch_9x updated: SOLR-16950 SimpleTracer propagation for manual transaction ids (#1906)

This is an automated email from the ASF dual-hosted git repository.

stillalex pushed a commit to branch branch_9x
in repository https://gitbox.apache.org/repos/asf/solr.git


The following commit(s) were added to refs/heads/branch_9x by this push:
     new 83b13872a9c SOLR-16950 SimpleTracer propagation for manual transaction ids (#1906)
83b13872a9c is described below

commit 83b13872a9cb31907ff85722fb102594823d428f
Author: Alex D <st...@apache.org>
AuthorDate: Thu Sep 14 19:07:38 2023 -0700

    SOLR-16950 SimpleTracer propagation for manual transaction ids (#1906)
---
 solr/CHANGES.txt                                   |   2 +
 .../TestSimplePropagatorDistributedTracing.java    | 141 +++++++++++++++------
 .../org/apache/solr/client/solrj/SolrRequest.java  |  10 ++
 .../solr/client/solrj/request/UpdateRequest.java   |   1 +
 4 files changed, 117 insertions(+), 37 deletions(-)

diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index 3e200832508..7435126e5ab 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -70,6 +70,8 @@ Improvements
 
 * SOLR-16938: Auto configure tracer without a <tracerConfig> tag in solr.xml (Alex Deparvu)
 
+* SOLR-16950: SimpleTracer propagation for manual transaction ids
+
 Optimizations
 ---------------------
 
diff --git a/solr/core/src/test/org/apache/solr/util/tracing/TestSimplePropagatorDistributedTracing.java b/solr/core/src/test/org/apache/solr/util/tracing/TestSimplePropagatorDistributedTracing.java
index a08d3ade5b1..6a8dee6d27d 100644
--- a/solr/core/src/test/org/apache/solr/util/tracing/TestSimplePropagatorDistributedTracing.java
+++ b/solr/core/src/test/org/apache/solr/util/tracing/TestSimplePropagatorDistributedTracing.java
@@ -18,12 +18,18 @@ package org.apache.solr.util.tracing;
 
 import io.opentracing.util.GlobalTracer;
 import java.io.IOException;
+import java.util.List;
+import java.util.Optional;
 import java.util.concurrent.TimeUnit;
+import org.apache.solr.client.solrj.SolrClient;
 import org.apache.solr.client.solrj.SolrQuery;
 import org.apache.solr.client.solrj.SolrServerException;
+import org.apache.solr.client.solrj.impl.CloudHttp2SolrClient;
+import org.apache.solr.client.solrj.impl.CloudLegacySolrClient;
 import org.apache.solr.client.solrj.impl.CloudSolrClient;
 import org.apache.solr.client.solrj.request.CollectionAdminRequest;
 import org.apache.solr.client.solrj.request.QueryRequest;
+import org.apache.solr.client.solrj.request.UpdateRequest;
 import org.apache.solr.cloud.SolrCloudTestCase;
 import org.apache.solr.common.util.SuppressForbidden;
 import org.apache.solr.common.util.TimeSource;
@@ -56,49 +62,95 @@ public class TestSimplePropagatorDistributedTracing extends SolrCloudTestCase {
   }
 
   @Test
-  public void test() throws IOException, SolrServerException {
+  public void testQueryRequest() throws IOException, SolrServerException {
     CloudSolrClient cloudClient = cluster.getSolrClient();
+    cloudClient.add(COLLECTION, sdoc("id", "1"));
+    cloudClient.add(COLLECTION, sdoc("id", "2"));
+    cloudClient.add(COLLECTION, sdoc("id", "3"));
 
-    // Indexing has trace ids
-    try (LogListener reqLog = LogListener.info(LogUpdateProcessorFactory.class.getName())) {
-      // verify all indexing events have trace id present
-      cloudClient.add(COLLECTION, sdoc("id", "1"));
-      cloudClient.add(COLLECTION, sdoc("id", "2"));
-      cloudClient.add(COLLECTION, sdoc("id", "3"));
-      var queue = reqLog.getQueue();
-      assertFalse(queue.isEmpty());
-      while (!queue.isEmpty()) {
-        var reqEvent = queue.poll();
-        String evTraceId = reqEvent.getContextData().getValue(MDCLoggingContext.TRACE_ID);
-        assertNotNull(evTraceId);
+    try (LogListener reqLog = LogListener.info(SolrCore.class.getName() + ".Request")) {
+
+      try (SolrClient testClient = newCloudLegacySolrClient()) {
+        // verify all query events have the same auto-generated trace id
+        var r1 = testClient.query(COLLECTION, new SolrQuery("*:*"));
+        assertEquals(0, r1.getStatus());
+        assertSameTraceId(reqLog, null);
+
+        // verify all query events have the same 'custom' trace id
+        String traceId = "tidTestQueryRequest1";
+        var q = new QueryRequest(new SolrQuery("*:*"));
+        q.addHeader(SimplePropagator.TRACE_ID, traceId);
+        var r2 = q.process(testClient, COLLECTION);
+        assertEquals(0, r2.getStatus());
+        assertSameTraceId(reqLog, traceId);
       }
 
-      // TODO this doesn't work due to solr client creating the UpdateRequest without headers
-      //      // verify all events have the same 'custom' traceid
-      //      String traceId = "tidTestSimplePropagatorDistributedTracing0";
-      //      var doc = sdoc("id", "4");
-      //      UpdateRequest u = new UpdateRequest();
-      //      u.add(doc);
-      //      u.addHeader(SimplePropagator.TRACE_ID, traceId);
-      //      var r1 = u.process(cloudClient, COLLECTION);
-      //      assertEquals(0, r1.getStatus());
-      //      assertSameTraceId(reqLog, traceId);
+      try (SolrClient testClient = newCloudHttp2SolrClient()) {
+        // verify all query events have the same auto-generated trace id
+        var r1 = testClient.query(COLLECTION, new SolrQuery("*:*"));
+        assertEquals(0, r1.getStatus());
+        assertSameTraceId(reqLog, null);
+
+        // verify all query events have the same 'custom' trace id
+        String traceId = "tidTestQueryRequest2";
+        var q = new QueryRequest(new SolrQuery("*:*"));
+        q.addHeader(SimplePropagator.TRACE_ID, traceId);
+        var r2 = q.process(testClient, COLLECTION);
+        assertEquals(0, r2.getStatus());
+        assertSameTraceId(reqLog, traceId);
+      }
     }
+  }
 
-    // Searching has trace ids
-    try (LogListener reqLog = LogListener.info(SolrCore.class.getName() + ".Request")) {
-      // verify all query events have the same auto-generated traceid
-      var r1 = cloudClient.query(COLLECTION, new SolrQuery("*:*"));
-      assertEquals(0, r1.getStatus());
-      assertSameTraceId(reqLog, null);
-
-      // verify all query events have the same 'custom' traceid
-      String traceId = "tidTestSimplePropagatorDistributedTracing1";
-      var q = new QueryRequest(new SolrQuery("*:*"));
-      q.addHeader(SimplePropagator.TRACE_ID, traceId);
-      var r2 = q.process(cloudClient, COLLECTION);
-      assertEquals(0, r2.getStatus());
-      assertSameTraceId(reqLog, traceId);
+  @Test
+  public void testUpdateRequest() throws IOException, SolrServerException {
+    try (LogListener reqLog = LogListener.info(LogUpdateProcessorFactory.class.getName())) {
+
+      try (SolrClient testClient = newCloudLegacySolrClient()) {
+        // verify all indexing events have trace id present
+        testClient.add(COLLECTION, sdoc("id", "1"));
+        testClient.add(COLLECTION, sdoc("id", "3"));
+        var queue = reqLog.getQueue();
+        assertFalse(queue.isEmpty());
+        while (!queue.isEmpty()) {
+          var reqEvent = queue.poll();
+          String evTraceId = reqEvent.getContextData().getValue(MDCLoggingContext.TRACE_ID);
+          assertNotNull(evTraceId);
+        }
+
+        // verify all events have the same 'custom' trace id
+        String traceId = "tidTestUpdateRequest1";
+        UpdateRequest u = new UpdateRequest();
+        u.add(sdoc("id", "5"));
+        u.add(sdoc("id", "7"));
+        u.addHeader(SimplePropagator.TRACE_ID, traceId);
+        var r1 = u.process(testClient, COLLECTION);
+        assertEquals(0, r1.getStatus());
+        assertSameTraceId(reqLog, traceId);
+      }
+
+      try (SolrClient testClient = newCloudHttp2SolrClient()) {
+        // verify all indexing events have trace id present
+        testClient.add(COLLECTION, sdoc("id", "2"));
+        testClient.add(COLLECTION, sdoc("id", "4"));
+        var queue = reqLog.getQueue();
+        assertFalse(queue.isEmpty());
+        while (!queue.isEmpty()) {
+          var reqEvent = queue.poll();
+          String evTraceId = reqEvent.getContextData().getValue(MDCLoggingContext.TRACE_ID);
+          assertNotNull(evTraceId);
+        }
+
+        // verify all events have the same 'custom' trace id
+        String traceId = "tidTestUpdateRequest2";
+        UpdateRequest u = new UpdateRequest();
+        u.add(sdoc("id", "6"));
+        u.add(sdoc("id", "8"));
+        u.addHeader(SimplePropagator.TRACE_ID, traceId);
+        var r1 = u.process(testClient, COLLECTION);
+        assertEquals(0, r1.getStatus());
+        assertSameTraceId(reqLog, traceId);
+      }
     }
   }
 
@@ -115,4 +167,19 @@ public class TestSimplePropagatorDistributedTracing extends SolrCloudTestCase {
       assertEquals(traceId, evTraceId);
     }
   }
+
+  private CloudSolrClient newCloudLegacySolrClient() {
+    return new CloudLegacySolrClient.Builder(
+            List.of(cluster.getZkServer().getZkAddress()), Optional.empty())
+        .build();
+  }
+
+  private CloudHttp2SolrClient newCloudHttp2SolrClient() {
+    var builder =
+        new CloudHttp2SolrClient.Builder(
+            List.of(cluster.getZkServer().getZkAddress()), Optional.empty());
+    var client = builder.build();
+    client.connect();
+    return client;
+  }
 }
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/SolrRequest.java b/solr/solrj/src/java/org/apache/solr/client/solrj/SolrRequest.java
index 269c74ff04e..f433b6c34d0 100644
--- a/solr/solrj/src/java/org/apache/solr/client/solrj/SolrRequest.java
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/SolrRequest.java
@@ -270,6 +270,16 @@ public abstract class SolrRequest<T extends SolrResponse> implements Serializabl
     headers.put(key, value);
   }
 
+  public void addHeaders(Map<String, String> headers) {
+    if (headers == null) {
+      return;
+    }
+    if (this.headers == null) {
+      this.headers = new HashMap<>();
+    }
+    this.headers.putAll(headers);
+  }
+
   public Map<String, String> getHeaders() {
     if (headers == null) return null;
     return Collections.unmodifiableMap(headers);
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/request/UpdateRequest.java b/solr/solrj/src/java/org/apache/solr/client/solrj/request/UpdateRequest.java
index 3534558bba2..c04990accee 100644
--- a/solr/solrj/src/java/org/apache/solr/client/solrj/request/UpdateRequest.java
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/request/UpdateRequest.java
@@ -271,6 +271,7 @@ public class UpdateRequest extends AbstractUpdateRequest {
           updateRequest.setPath(getPath());
           updateRequest.setBasicAuthCredentials(getBasicAuthUser(), getBasicAuthPassword());
           updateRequest.setResponseParser(getResponseParser());
+          updateRequest.addHeaders(getHeaders());
           request = reqSupplier.get(updateRequest, urls);
           routes.put(leaderUrl, request);
         }