Skip to content
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

SOLR-16950 SimpleTracer propagation for manual transaction ids #1906

Merged
merged 2 commits into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions solr/CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,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
---------------------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,17 @@
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.api.trace.TracerProvider;
import java.io.IOException;
import java.util.List;
import java.util.Optional;
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.core.SolrCore;
Expand Down Expand Up @@ -55,49 +61,95 @@ public static void setupCluster() throws Exception {
}

@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);
}
}
}

Expand All @@ -114,4 +166,19 @@ private void assertSameTraceId(LogListener reqLog, String traceId) {
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;
}
}
10 changes: 10 additions & 0 deletions solr/solrj/src/java/org/apache/solr/client/solrj/SolrRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,16 @@ public void addHeader(String key, String value) {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ private <T extends LBSolrClient.Req> Map<String, T> getRoutes(
updateRequest.setPath(getPath());
updateRequest.setBasicAuthCredentials(getBasicAuthUser(), getBasicAuthPassword());
updateRequest.setResponseParser(getResponseParser());
updateRequest.addHeaders(getHeaders());
request = reqSupplier.get(updateRequest, urls);
routes.put(leaderUrl, request);
}
Expand Down
Loading