exceptionfactory commented on code in PR #6040:
URL: https://github.com/apache/nifi/pull/6040#discussion_r873947923


##########
nifi-nar-bundles/nifi-standard-services/nifi-distributed-cache-services-bundle/nifi-distributed-cache-server/src/main/java/org/apache/nifi/distributed/cache/server/codec/CacheRequestDecoder.java:
##########
@@ -0,0 +1,183 @@
+/*
+ * 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.nifi.distributed.cache.server.codec;
+
+import io.netty.buffer.ByteBuf;
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.handler.codec.ByteToMessageDecoder;
+import org.apache.nifi.distributed.cache.operations.CacheOperation;
+import org.apache.nifi.distributed.cache.server.protocol.CacheRequest;
+import org.apache.nifi.distributed.cache.server.protocol.CacheVersionRequest;
+import org.apache.nifi.logging.ComponentLog;
+
+import java.nio.charset.StandardCharsets;
+import java.util.Arrays;
+import java.util.List;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.atomic.AtomicReference;
+
+/**
+ * Cache Request Decoder processes bytes and decodes cache version and 
operation requests
+ */
+public class CacheRequestDecoder extends ByteToMessageDecoder {
+    private static final int DEFAULT_LENGTH = 0;
+
+    private static final int HEADER_LENGTH = 4;
+
+    private final AtomicBoolean headerReceived = new AtomicBoolean();
+
+    private final AtomicInteger protocolVersion = new AtomicInteger();
+
+    private final AtomicInteger valueLength = new 
AtomicInteger(DEFAULT_LENGTH);
+
+    private final AtomicReference<CacheOperation> currentOperation = new 
AtomicReference<>();
+
+    private final ComponentLog log;
+
+    private final int maxLength;
+
+    private final CacheOperation[] supportedOperations;
+
+    public CacheRequestDecoder(
+            final ComponentLog log,
+            final int maxLength,
+            final CacheOperation[] supportedOperations
+    ) {
+        this.log = log;
+        this.maxLength = maxLength;
+        this.supportedOperations = supportedOperations;
+    }
+
+    /**
+     * Decode Byte Buffer reading header on initial connection followed by 
protocol version and cache operations
+     *
+     * @param channelHandlerContext Channel Handler Context
+     * @param byteBuf Byte Buffer
+     * @param objects Decoded Objects
+     */
+    @Override
+    protected void decode(final ChannelHandlerContext channelHandlerContext, 
final ByteBuf byteBuf, final List<Object> objects) {
+        if (!headerReceived.get()) {
+            byteBuf.readBytes(HEADER_LENGTH);
+            headerReceived.getAndSet(true);
+            log.debug("Header Received [{}]", 
channelHandlerContext.channel().remoteAddress());
+        }
+
+        if (protocolVersion.get() == 0) {
+            final int clientVersion = byteBuf.readInt();
+            log.debug("Protocol Version [{}] Received [{}]", clientVersion, 
channelHandlerContext.channel().remoteAddress());
+            final CacheVersionRequest cacheVersionRequest = new 
CacheVersionRequest(clientVersion);
+            objects.add(cacheVersionRequest);
+        } else {
+            final CacheOperation cacheOperation = readOperation(byteBuf);
+            final Object cacheRequest = readRequest(cacheOperation, byteBuf);
+            if (cacheRequest == null) {
+                log.debug("Cache Operation [{}] request not processed", 
cacheOperation);
+            } else {
+                objects.add(cacheRequest);
+                // Reset Cache Operation after successful decoding
+                currentOperation.set(null);
+            }
+        }
+    }
+
+    @Override
+    public void exceptionCaught(final ChannelHandlerContext context, final 
Throwable cause) {
+        log.warn("Request Decoding Failed: Closing Connection [{}]", 
context.channel().remoteAddress(), cause);
+        context.close();
+    }
+
+    /**
+     * Set Protocol Version based on version negotiated in other handlers
+     *
+     * @param protocolVersion Protocol Version
+     */
+    public void setProtocolVersion(final int protocolVersion) {
+        this.protocolVersion.getAndSet(protocolVersion);
+    }
+
+    /**
+     * Read Request Object based on Cache Operation
+     *
+     * @param cacheOperation Cache Operation
+     * @param byteBuf Byte Buffer
+     * @return Request Object or null when buffer does not contain sufficient 
bytes
+     */
+    protected Object readRequest(final CacheOperation cacheOperation, final 
ByteBuf byteBuf) {
+        final int readableBytes = byteBuf.readableBytes();
+        final byte[] body = readableBytes == 0 ? new byte[0] : 
readBytes(byteBuf);
+        return body == null ? null : new CacheRequest(cacheOperation, body);
+    }
+
+    /**
+     * Read Bytes from buffer based on length indicated
+     *
+     * @param byteBuf Byte Buffer
+     * @return Bytes read or null when buffer does not contain sufficient bytes
+     */
+    protected byte[] readBytes(final ByteBuf byteBuf) {
+        final int length = readBytesLength(byteBuf);

Review Comment:
   Yes, the Netty `BytesToMessageDecoder` class, which this class extends, 
buffers incoming data in the provided `ByteBuf`. Netty will continue calling 
this decode with the current contents of the buffer. As implemented, this 
method checks for a sufficient number of bytes available before attempting to 
read the buffer contents. With this approach, Netty handles the buffering so 
this class can support both small and large requests.



-- 
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]

Reply via email to