keith-turner commented on code in PR #5398: URL: https://github.com/apache/accumulo/pull/5398#discussion_r2023607675
########## core/src/main/java/org/apache/accumulo/core/rpc/AccumuloProtocolFactory.java: ########## @@ -0,0 +1,238 @@ +/* + * 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 org.checkerframework.checker.nullness.qual.NonNull; + +import io.opentelemetry.api.trace.Span; +import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator; +import io.opentelemetry.context.Context; +import io.opentelemetry.context.Scope; +import io.opentelemetry.context.propagation.TextMapGetter; + +/** + * 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(); Review Comment: why use `this.` here? also used in some other places in this class ########## core/src/main/java/org/apache/accumulo/core/rpc/AccumuloProtocolFactory.java: ########## @@ -0,0 +1,238 @@ +/* + * 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 org.checkerframework.checker.nullness.qual.NonNull; + +import io.opentelemetry.api.trace.Span; +import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator; +import io.opentelemetry.context.Context; +import io.opentelemetry.context.Scope; +import io.opentelemetry.context.propagation.TextMapGetter; + +/** + * 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 (this.isClient && span != null && span.getSpanContext().isValid()) { Review Comment: This method is only called when isClient is true, so the check seems misleading and unnecessary. ########## core/src/main/java/org/apache/accumulo/core/rpc/AccumuloProtocolFactory.java: ########## @@ -0,0 +1,238 @@ +/* + * 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 org.checkerframework.checker.nullness.qual.NonNull; + +import io.opentelemetry.api.trace.Span; +import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator; +import io.opentelemetry.context.Context; +import io.opentelemetry.context.Scope; +import io.opentelemetry.context.propagation.TextMapGetter; + +/** + * 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 (this.isClient && span != null && span.getSpanContext().isValid()) { + super.writeBool(HEADER_HAS_TRACE); + traceHeaders.clear(); + + W3CTraceContextPropagator.getInstance().inject(Context.current(), traceHeaders, Review Comment: Assuming this code was copied from TraceUtil? Was there any significant change you needed to make to it to use it here? ########## core/src/test/java/org/apache/accumulo/core/rpc/AccumuloProtocolTest.java: ########## @@ -0,0 +1,120 @@ +/* + * 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 static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.apache.thrift.TException; +import org.apache.thrift.protocol.TCompactProtocol; +import org.apache.thrift.transport.TMemoryBuffer; +import org.apache.thrift.transport.TTransportException; +import org.junit.jupiter.api.Test; + +public class AccumuloProtocolTest { + + private static final int VALID_MAGIC_NUMBER = + AccumuloProtocolFactory.AccumuloProtocol.MAGIC_NUMBER; + private static final int INVALID_MAGIC_NUMBER = 0x12345678; + private static final byte VALID_VERSION = + AccumuloProtocolFactory.AccumuloProtocol.PROTOCOL_VERSION; + private static final byte INVALID_VERSION = 99; + + /** + * Test that a valid header does not throw an exception + */ + @Test + public void testValidHeader() throws TException { + try (TMemoryBuffer transport = new TMemoryBuffer(100)) { + + TCompactProtocol protocol = new TCompactProtocol(transport); + protocol.writeI32(VALID_MAGIC_NUMBER); + protocol.writeByte(VALID_VERSION); + protocol.writeBool(false); + + var serverProtocol = (AccumuloProtocolFactory.AccumuloProtocol) AccumuloProtocolFactory + .serverFactory().getProtocol(transport); + + assertDoesNotThrow(serverProtocol::validateHeader); Review Comment: Would be good to verify all the data was read from `transport`. Experimented w/ this and found the following seems to work. There are some other methods on `transport` that sound promising based on their name, but do not actually verify the data was read. ```suggestion assertDoesNotThrow(serverProtocol::validateHeader); // All data should have been read from the transport following should read zero bytes. assertEquals(0, transport.read(new byte[10],0,10)); ``` -- 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