This is an automated email from the ASF dual-hosted git repository.

markap14 pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/nifi.git


The following commit(s) were added to refs/heads/main by this push:
     new e60cbd4bbb NIFI-10768 Filter empty messages in ListenSyslog (#6624)
e60cbd4bbb is described below

commit e60cbd4bbbfd760cda76e72a4f9afb9864baf70e
Author: exceptionfactory <[email protected]>
AuthorDate: Mon Nov 7 14:05:40 2022 -0600

    NIFI-10768 Filter empty messages in ListenSyslog (#6624)
    
    - Added FilteringStrategy argument to Server Factory for ListenSyslog
    - Set quick shutdown quiet period for ListenSyslog and TestListenSyslog
    - Added test method for batched messages with empty message included
---
 .../ByteArrayMessageNettyEventServerFactory.java   | 33 +++++++++++++-
 .../event/transport/netty/FilteringStrategy.java   | 28 ++++++++++++
 .../FilteringByteArrayMessageChannelHandler.java   | 52 ++++++++++++++++++++++
 .../nifi/processors/standard/ListenSyslog.java     |  5 ++-
 .../nifi/processors/standard/TestListenSyslog.java | 31 +++++++++++++
 5 files changed, 147 insertions(+), 2 deletions(-)

diff --git 
a/nifi-nar-bundles/nifi-extension-utils/nifi-event-transport/src/main/java/org/apache/nifi/event/transport/netty/ByteArrayMessageNettyEventServerFactory.java
 
b/nifi-nar-bundles/nifi-extension-utils/nifi-event-transport/src/main/java/org/apache/nifi/event/transport/netty/ByteArrayMessageNettyEventServerFactory.java
index 3073926391..32a4380648 100644
--- 
a/nifi-nar-bundles/nifi-extension-utils/nifi-event-transport/src/main/java/org/apache/nifi/event/transport/netty/ByteArrayMessageNettyEventServerFactory.java
+++ 
b/nifi-nar-bundles/nifi-extension-utils/nifi-event-transport/src/main/java/org/apache/nifi/event/transport/netty/ByteArrayMessageNettyEventServerFactory.java
@@ -22,6 +22,7 @@ import io.netty.handler.codec.bytes.ByteArrayDecoder;
 import org.apache.nifi.event.transport.configuration.TransportProtocol;
 import org.apache.nifi.event.transport.message.ByteArrayMessage;
 import 
org.apache.nifi.event.transport.netty.channel.ByteArrayMessageChannelHandler;
+import 
org.apache.nifi.event.transport.netty.channel.FilteringByteArrayMessageChannelHandler;
 import 
org.apache.nifi.event.transport.netty.codec.DatagramByteArrayMessageDecoder;
 import 
org.apache.nifi.event.transport.netty.channel.LogExceptionChannelHandler;
 import 
org.apache.nifi.event.transport.netty.codec.SocketByteArrayMessageDecoder;
@@ -55,9 +56,39 @@ public class ByteArrayMessageNettyEventServerFactory extends 
NettyEventServerFac
                                                    final byte[] delimiter,
                                                    final int maxFrameLength,
                                                    final 
BlockingQueue<ByteArrayMessage> messages) {
+        this(log, address, port, protocol, delimiter, maxFrameLength, 
messages, FilteringStrategy.DISABLED);
+    }
+
+
+    /**
+     * Netty Event Server Factory with configurable delimiter and queue of 
Byte Array Messages
+     *
+     * @param log Component Log
+     * @param address Listen Address
+     * @param port Listen Port Number
+     * @param protocol Channel Protocol
+     * @param delimiter Message Delimiter
+     * @param maxFrameLength Maximum Frame Length for delimited TCP messages
+     * @param messages Blocking Queue for events received
+     * @param filteringStrategy Message Filtering Strategy
+     */
+    public ByteArrayMessageNettyEventServerFactory(final ComponentLog log,
+                                                   final InetAddress address,
+                                                   final int port,
+                                                   final TransportProtocol 
protocol,
+                                                   final byte[] delimiter,
+                                                   final int maxFrameLength,
+                                                   final 
BlockingQueue<ByteArrayMessage> messages,
+                                                   final FilteringStrategy 
filteringStrategy) {
         super(address, port, protocol);
         final LogExceptionChannelHandler logExceptionChannelHandler = new 
LogExceptionChannelHandler(log);
-        final ByteArrayMessageChannelHandler byteArrayMessageChannelHandler = 
new ByteArrayMessageChannelHandler(messages);
+
+        final ByteArrayMessageChannelHandler byteArrayMessageChannelHandler;
+        if (FilteringStrategy.EMPTY == filteringStrategy) {
+            byteArrayMessageChannelHandler = new 
FilteringByteArrayMessageChannelHandler(messages);
+        } else {
+            byteArrayMessageChannelHandler = new 
ByteArrayMessageChannelHandler(messages);
+        }
 
         if (TransportProtocol.UDP.equals(protocol)) {
             setHandlerSupplier(() -> Arrays.asList(
diff --git 
a/nifi-nar-bundles/nifi-extension-utils/nifi-event-transport/src/main/java/org/apache/nifi/event/transport/netty/FilteringStrategy.java
 
b/nifi-nar-bundles/nifi-extension-utils/nifi-event-transport/src/main/java/org/apache/nifi/event/transport/netty/FilteringStrategy.java
new file mode 100644
index 0000000000..e0c3ddc105
--- /dev/null
+++ 
b/nifi-nar-bundles/nifi-extension-utils/nifi-event-transport/src/main/java/org/apache/nifi/event/transport/netty/FilteringStrategy.java
@@ -0,0 +1,28 @@
+/*
+ * 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.event.transport.netty;
+
+/**
+ * Message Filtering Strategy
+ */
+public enum FilteringStrategy {
+    /** Filtering is disabled */
+    DISABLED,
+
+    /** Filtering is enabled for messages containing zero bytes */
+    EMPTY
+}
diff --git 
a/nifi-nar-bundles/nifi-extension-utils/nifi-event-transport/src/main/java/org/apache/nifi/event/transport/netty/channel/FilteringByteArrayMessageChannelHandler.java
 
b/nifi-nar-bundles/nifi-extension-utils/nifi-event-transport/src/main/java/org/apache/nifi/event/transport/netty/channel/FilteringByteArrayMessageChannelHandler.java
new file mode 100644
index 0000000000..036f4a11f4
--- /dev/null
+++ 
b/nifi-nar-bundles/nifi-extension-utils/nifi-event-transport/src/main/java/org/apache/nifi/event/transport/netty/channel/FilteringByteArrayMessageChannelHandler.java
@@ -0,0 +1,52 @@
+/*
+ * 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.event.transport.netty.channel;
+
+import io.netty.channel.ChannelHandler;
+import io.netty.channel.ChannelHandlerContext;
+import org.apache.nifi.event.transport.message.ByteArrayMessage;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.concurrent.BlockingQueue;
+
+/**
+ * Channel Handler for queuing bytes received as Byte Array Messages and 
filtering empty messages
+ */
[email protected]
+public class FilteringByteArrayMessageChannelHandler extends 
ByteArrayMessageChannelHandler {
+    private static final Logger logger = 
LoggerFactory.getLogger(FilteringByteArrayMessageChannelHandler.class);
+
+    public FilteringByteArrayMessageChannelHandler(final 
BlockingQueue<ByteArrayMessage> messages) {
+        super(messages);
+    }
+
+    /**
+     * Read Channel message and offer to queue for external processing
+     *
+     * @param channelHandlerContext Channel Handler Context
+     * @param message Byte Array Message for processing
+     */
+    @Override
+    protected void channelRead0(final ChannelHandlerContext 
channelHandlerContext, final ByteArrayMessage message) {
+        if (message.getMessage().length == 0) {
+            logger.debug("Empty Message Received from Remote Address [{}] ", 
message.getSender());
+        } else {
+            super.channelRead0(channelHandlerContext, message);
+        }
+    }
+}
diff --git 
a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/ListenSyslog.java
 
b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/ListenSyslog.java
index 74409e836a..3a5b4020b9 100644
--- 
a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/ListenSyslog.java
+++ 
b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/ListenSyslog.java
@@ -31,9 +31,11 @@ import org.apache.nifi.components.PropertyValue;
 import org.apache.nifi.components.ValidationContext;
 import org.apache.nifi.components.ValidationResult;
 import org.apache.nifi.event.transport.EventServer;
+import org.apache.nifi.event.transport.configuration.ShutdownQuietPeriod;
 import org.apache.nifi.event.transport.configuration.TransportProtocol;
 import org.apache.nifi.event.transport.message.ByteArrayMessage;
 import 
org.apache.nifi.event.transport.netty.ByteArrayMessageNettyEventServerFactory;
+import org.apache.nifi.event.transport.netty.FilteringStrategy;
 import org.apache.nifi.flowfile.FlowFile;
 import org.apache.nifi.flowfile.attributes.CoreAttributes;
 import org.apache.nifi.processor.DataUnit;
@@ -287,7 +289,8 @@ public class ListenSyslog extends AbstractSyslogProcessor {
 
         final InetAddress address = getListenAddress(networkInterfaceName);
         final ByteArrayMessageNettyEventServerFactory factory = new 
ByteArrayMessageNettyEventServerFactory(getLogger(),
-                address, port, transportProtocol, messageDemarcatorBytes, 
receiveBufferSize, syslogEvents);
+                address, port, transportProtocol, messageDemarcatorBytes, 
receiveBufferSize, syslogEvents, FilteringStrategy.EMPTY);
+        
factory.setShutdownQuietPeriod(ShutdownQuietPeriod.QUICK.getDuration());
         factory.setThreadNamePrefix(String.format("%s[%s]", 
ListenSyslog.class.getSimpleName(), getIdentifier()));
         final int maxConnections = 
context.getProperty(MAX_CONNECTIONS).asLong().intValue();
         factory.setWorkerThreads(maxConnections);
diff --git 
a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestListenSyslog.java
 
b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestListenSyslog.java
index 02ef0d2c3e..c7bc29ab4f 100644
--- 
a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestListenSyslog.java
+++ 
b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestListenSyslog.java
@@ -18,6 +18,8 @@ package org.apache.nifi.processors.standard;
 
 import org.apache.nifi.event.transport.EventSender;
 import org.apache.nifi.event.transport.configuration.LineEnding;
+import org.apache.nifi.event.transport.configuration.ShutdownQuietPeriod;
+import org.apache.nifi.event.transport.configuration.ShutdownTimeout;
 import org.apache.nifi.event.transport.configuration.TransportProtocol;
 import org.apache.nifi.event.transport.netty.StringNettyEventSenderFactory;
 import org.apache.nifi.flowfile.attributes.CoreAttributes;
@@ -83,6 +85,33 @@ public class TestListenSyslog {
         assertSendSuccess(protocol, port);
     }
 
+    @Test
+    public void testRunTcpBatchParseDisabled() throws Exception {
+        final int port = NetworkUtils.getAvailableTcpPort();
+        final TransportProtocol protocol = TransportProtocol.TCP;
+        runner.setProperty(ListenSyslog.PROTOCOL, protocol.toString());
+        runner.setProperty(ListenSyslog.PORT, Integer.toString(port));
+        runner.setProperty(ListenSyslog.SOCKET_KEEP_ALIVE, 
Boolean.FALSE.toString());
+        runner.setProperty(ListenSyslog.PARSE_MESSAGES, 
Boolean.FALSE.toString());
+        runner.setProperty(ListenSyslog.MAX_BATCH_SIZE, "2");
+
+        runner.run(1, STOP_ON_FINISH_DISABLED);
+
+        final String batchedWithEmptyMessages = String.format("%s\n\n%s\n", 
VALID_MESSAGE, VALID_MESSAGE);
+        sendMessages(protocol, port, LineEnding.NONE, 
batchedWithEmptyMessages);
+        runner.run(1, STOP_ON_FINISH_DISABLED, INITIALIZE_DISABLED);
+
+        runner.assertTransferCount(ListenSyslog.REL_INVALID, 0);
+
+        final List<MockFlowFile> successFlowFiles = 
runner.getFlowFilesForRelationship(ListenSyslog.REL_SUCCESS);
+        assertEquals(1, successFlowFiles.size(), "Success FlowFiles not 
matched");
+
+        final MockFlowFile flowFile = successFlowFiles.iterator().next();
+
+        final String batchedMessages = String.format("%s\n%s", VALID_MESSAGE, 
VALID_MESSAGE);
+        flowFile.assertContentEquals(batchedMessages);
+    }
+
     @Test
     public void testRunUdp() throws Exception {
         final int port = NetworkUtils.getAvailableUdpPort();
@@ -181,6 +210,8 @@ public class TestListenSyslog {
 
     private void sendMessages(final TransportProtocol protocol, final int 
port, final LineEnding lineEnding, final String... messages) throws Exception {
         final StringNettyEventSenderFactory eventSenderFactory = new 
StringNettyEventSenderFactory(runner.getLogger(), LOCALHOST_ADDRESS, port, 
protocol, CHARSET, lineEnding);
+        
eventSenderFactory.setShutdownQuietPeriod(ShutdownQuietPeriod.QUICK.getDuration());
+        
eventSenderFactory.setShutdownTimeout(ShutdownTimeout.QUICK.getDuration());
         eventSenderFactory.setTimeout(SENDER_TIMEOUT);
         try (final EventSender<String> eventSender = 
eventSenderFactory.getEventSender()) {
             for (final String message : messages) {

Reply via email to