DomGarguilo commented on code in PR #5398:
URL: https://github.com/apache/accumulo/pull/5398#discussion_r2042809568


##########
core/src/main/java/org/apache/accumulo/core/rpc/AccumuloProtocolFactory.java:
##########
@@ -0,0 +1,220 @@
+/*
+ * 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
+ *
+ *   https://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.accumulo.core.rpc;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.accumulo.core.trace.TraceUtil;
+import org.apache.thrift.TException;
+import org.apache.thrift.protocol.TCompactProtocol;
+import org.apache.thrift.protocol.TMessage;
+import org.apache.thrift.protocol.TProtocol;
+import org.apache.thrift.transport.TTransport;
+
+import io.opentelemetry.api.trace.Span;
+import io.opentelemetry.context.Context;
+import io.opentelemetry.context.Scope;
+
+/**
+ * Factory for creating instances of the AccumuloProtocol.
+ * <p>
+ * This protocol includes a custom header to ensure compatibility between 
different versions of the
+ * protocol.
+ */
+public class AccumuloProtocolFactory extends TCompactProtocol.Factory {
+
+  private static final long serialVersionUID = 1L;
+
+  private final boolean isClient;
+
+  public static class AccumuloProtocol extends TCompactProtocol {
+
+    static final int MAGIC_NUMBER = 0x41434355; // "ACCU" in ASCII
+    static final byte PROTOCOL_VERSION = 1;
+    private static final boolean HEADER_HAS_TRACE = true;
+
+    private final boolean isClient;
+
+    private Span span = null;
+    private Scope scope = null;
+    private final Map<String,String> traceHeaders = new HashMap<>();
+
+    public AccumuloProtocol(TTransport transport, boolean isClient) {
+      super(transport);
+      this.isClient = isClient;
+    }
+
+    /**
+     * For client calls, add RPC span and write the validation header
+     */
+    @Override
+    public void writeMessageBegin(TMessage message) throws TException {
+      if (!this.isClient) {
+        super.writeMessageBegin(message);
+      } else {
+        span = TraceUtil.startClientRpcSpan(this.getClass(), message.name);
+        scope = span.makeCurrent();
+
+        try {
+          this.writeHeader();
+          super.writeMessageBegin(message);
+        } catch (TException e) {
+          if (span != null) {
+            TraceUtil.setException(span, e, false);
+          }
+          if (scope != null) {
+            scope.close();
+          }
+          if (span != null) {
+            span.end();
+          }
+          throw e;
+        }
+      }
+    }
+
+    /**
+     * Writes the Accumulo protocol header containing version and 
identification info
+     */
+    private void writeHeader() throws TException {
+      super.writeI32(MAGIC_NUMBER);
+      super.writeByte(PROTOCOL_VERSION);
+
+      if (span == null || !span.getSpanContext().isValid()) {
+        super.writeBool(!HEADER_HAS_TRACE);
+        return;
+      }
+
+      super.writeBool(HEADER_HAS_TRACE);
+      traceHeaders.clear();
+
+      TraceUtil.injectTraceContext(traceHeaders);
+
+      super.writeI32(traceHeaders.size());
+
+      for (Map.Entry<String,String> entry : traceHeaders.entrySet()) {
+        super.writeString(entry.getKey());
+        super.writeString(entry.getValue());
+      }
+    }
+
+    @Override
+    public void writeMessageEnd() throws TException {
+      try {
+        super.writeMessageEnd();
+      } finally {
+        if (scope != null) {
+          scope.close();
+          span.end();
+        }
+      }
+    }
+
+    /**
+     * For server calls, validate the header
+     */
+    @Override
+    public TMessage readMessageBegin() throws TException {
+      if (!this.isClient) {
+        this.validateHeader();
+      }
+
+      return super.readMessageBegin();
+    }
+
+    /**
+     * Reads and validates the Accumulo protocol header
+     *
+     * @throws TException if the header is invalid or incompatible
+     */
+    void validateHeader() throws TException {
+      final int magic = super.readI32();
+      if (magic != MAGIC_NUMBER) {
+        throw new TException("Invalid Accumulo protocol: magic number 
mismatch. Expected: 0x"
+            + Integer.toHexString(MAGIC_NUMBER) + ", got: 0x" + 
Integer.toHexString(magic));
+      }
+
+      final byte version = super.readByte();
+      validateProtocolVersion(version);
+
+      final boolean hasTrace = super.readBool();
+
+      if (hasTrace) {
+        final int numHeaders = super.readI32();
+
+        final Map<String,String> headers = new HashMap<>(numHeaders);
+        for (int i = 0; i < numHeaders; i++) {
+          String key = super.readString();
+          String value = super.readString();
+          headers.put(key, value);
+        }
+
+        if (!headers.isEmpty()) {
+          Context extractedContext = TraceUtil.extractTraceContext(headers);
+
+          // Create server span with extracted context as parent
+          span = TraceUtil.startServerRpcSpanFromContext(this.getClass(), 
"handleMessage",
+              extractedContext);
+          scope = span.makeCurrent();
+        }
+      }
+    }
+
+    /**
+     * @throws TException if the given protocol version is incompatible with 
the current version
+     */
+    private void validateProtocolVersion(byte protocolVersion) throws 
TException {
+      if (protocolVersion != PROTOCOL_VERSION) {
+        throw new TException("Incompatible protocol version. Version seen: " + 
protocolVersion
+            + ", expected version: " + PROTOCOL_VERSION);

Review Comment:
   Added this in 713226c. Right now it splits on the last `.` character and 
checks that the string before that is the same on both ends. It could be 
helpful to add some standard way to get the parts of the version number if we 
find that this method does not work like we expect.



-- 
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: notifications-unsubscr...@accumulo.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to