You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@nifi.apache.org by GitBox <gi...@apache.org> on 2022/05/17 20:10:40 UTC

[GitHub] [nifi] exceptionfactory commented on a diff in pull request #6040: NIFI-9805 Refactor Distributed Cache Servers using Netty

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


##########
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/MapCacheRequestDecoder.java:
##########
@@ -0,0 +1,153 @@
+/*
+ * 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 org.apache.nifi.distributed.cache.operations.CacheOperation;
+import org.apache.nifi.distributed.cache.operations.MapOperation;
+import org.apache.nifi.distributed.cache.server.protocol.MapCacheRequest;
+import org.apache.nifi.logging.ComponentLog;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Optional;
+import java.util.OptionalInt;
+import java.util.OptionalLong;
+
+/**
+ * Cache Request Decoder processes bytes and decodes cache version and operation requests
+ */
+public class MapCacheRequestDecoder extends CacheRequestDecoder {
+
+    public MapCacheRequestDecoder(
+            final ComponentLog log,
+            final int maxLength,
+            final CacheOperation[] supportedOperations
+    ) {
+        super(log, maxLength, supportedOperations);
+    }
+
+    @Override
+    protected Object readRequest(final CacheOperation cacheOperation, final ByteBuf byteBuf) {
+        final Object request;
+
+        if (MapOperation.CONTAINS_KEY == cacheOperation) {
+            request = readKeyRequest(cacheOperation, byteBuf);
+        } else if (MapOperation.FETCH == cacheOperation) {
+            request = readKeyRequest(cacheOperation, byteBuf);
+        } else if (MapOperation.GET == cacheOperation) {
+            request = readKeyRequest(cacheOperation, byteBuf);
+        } else if (MapOperation.GET_AND_PUT_IF_ABSENT == cacheOperation) {
+            request = readKeyValueRequest(cacheOperation, byteBuf);
+        } else if (MapOperation.KEYSET == cacheOperation) {
+            request = new MapCacheRequest(cacheOperation);
+        } else if (MapOperation.REMOVE == cacheOperation) {
+            request = readKeyRequest(cacheOperation, byteBuf);
+        } else if (MapOperation.REMOVE_BY_PATTERN == cacheOperation) {
+            request = readPatternRequest(cacheOperation, byteBuf);
+        } else if (MapOperation.REMOVE_BY_PATTERN_AND_GET == cacheOperation) {
+            request = readPatternRequest(cacheOperation, byteBuf);
+        } else if (MapOperation.REMOVE_AND_GET == cacheOperation) {
+            request = readKeyRequest(cacheOperation, byteBuf);
+        } else if (MapOperation.REPLACE == cacheOperation) {
+            request = readKeyRevisionValueRequest(cacheOperation, byteBuf);
+        } else if (MapOperation.SUBMAP == cacheOperation) {
+            request = readSubMapRequest(cacheOperation, byteBuf);
+        } else if (MapOperation.PUT == cacheOperation) {
+            request = readKeyValueRequest(cacheOperation, byteBuf);
+        } else if (MapOperation.PUT_IF_ABSENT == cacheOperation) {
+            request = readKeyValueRequest(cacheOperation, byteBuf);
+        } else {
+            request = new MapCacheRequest(cacheOperation);
+        }
+
+        return request;
+    }
+
+    private MapCacheRequest readKeyRequest(final CacheOperation cacheOperation, final ByteBuf byteBuf) {
+        final Optional<byte[]> key = readBytes(byteBuf);
+        return key.map(bytes -> new MapCacheRequest(cacheOperation, bytes)).orElse(null);
+    }
+
+    private MapCacheRequest readKeyValueRequest(final CacheOperation cacheOperation, final ByteBuf byteBuf) {
+        final MapCacheRequest mapCacheRequest;
+
+        final Optional<byte[]> key = readBytes(byteBuf);
+        if (key.isPresent()) {

Review Comment:
   Thanks for catching that issue @greyp9! I pushed an update with a new test method to exercise this condition, and also adjusted `CacheRequestDecoder` to use `ByteBuf.markReaderIndex()` and `ByteBuf.resetReaderIndex()` to avoid partial reads.



-- 
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: issues-unsubscribe@nifi.apache.org

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