You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nifi.apache.org by pv...@apache.org on 2019/08/05 08:31:25 UTC

[nifi] branch master updated: NIFI-6507 Unsubscribe if WindowsEventLog subscription encounters an error

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 05cbf8b  NIFI-6507 Unsubscribe if WindowsEventLog subscription encounters an error
05cbf8b is described below

commit 05cbf8b24ff6b17c69cc12311d7a786c295d75b4
Author: Koji Kawamura <ij...@apache.org>
AuthorDate: Mon Jul 1 11:39:20 2019 +0900

    NIFI-6507 Unsubscribe if WindowsEventLog subscription encounters an error
    
    Signed-off-by: Pierre Villard <pi...@gmail.com>
    
    This closes #3617.
---
 .../windows/event/log/ConsumeWindowsEventLog.java  | 11 ++++++++++-
 .../jna/EventSubscribeXmlRenderingCallback.java    | 22 ++++++++++++++++++----
 2 files changed, 28 insertions(+), 5 deletions(-)

diff --git a/nifi-nar-bundles/nifi-windows-event-log-bundle/nifi-windows-event-log-processors/src/main/java/org/apache/nifi/processors/windows/event/log/ConsumeWindowsEventLog.java b/nifi-nar-bundles/nifi-windows-event-log-bundle/nifi-windows-event-log-processors/src/main/java/org/apache/nifi/processors/windows/event/log/ConsumeWindowsEventLog.java
index 7dc4236..22b2b34 100644
--- a/nifi-nar-bundles/nifi-windows-event-log-bundle/nifi-windows-event-log-processors/src/main/java/org/apache/nifi/processors/windows/event/log/ConsumeWindowsEventLog.java
+++ b/nifi-nar-bundles/nifi-windows-event-log-bundle/nifi-windows-event-log-processors/src/main/java/org/apache/nifi/processors/windows/event/log/ConsumeWindowsEventLog.java
@@ -240,7 +240,16 @@ public class ConsumeWindowsEventLog extends AbstractSessionFactoryProcessor {
     }
 
     private boolean isSubscribed() {
-        return subscriptionHandle != null && subscriptionHandle.getPointer() != null;
+        final boolean subscribed = subscriptionHandle != null && subscriptionHandle.getPointer() != null;
+        final boolean subscriptionFailed = evtSubscribeCallback != null
+            && ((EventSubscribeXmlRenderingCallback) evtSubscribeCallback).isSubscriptionFailed();
+        final boolean subscribing = subscribed && !subscriptionFailed;
+        getLogger().debug("subscribing? {}, subscribed={}, subscriptionFailed={}", new Object[]{subscribing, subscribed, subscriptionFailed});
+        if (subscriptionFailed) {
+            getLogger().info("Canceling a failed subscription.");
+            unsubscribe();
+        }
+        return subscribing;
     }
 
     @OnScheduled
diff --git a/nifi-nar-bundles/nifi-windows-event-log-bundle/nifi-windows-event-log-processors/src/main/java/org/apache/nifi/processors/windows/event/log/jna/EventSubscribeXmlRenderingCallback.java b/nifi-nar-bundles/nifi-windows-event-log-bundle/nifi-windows-event-log-processors/src/main/java/org/apache/nifi/processors/windows/event/log/jna/EventSubscribeXmlRenderingCallback.java
index 451e446..82c94ee 100644
--- a/nifi-nar-bundles/nifi-windows-event-log-bundle/nifi-windows-event-log-processors/src/main/java/org/apache/nifi/processors/windows/event/log/jna/EventSubscribeXmlRenderingCallback.java
+++ b/nifi-nar-bundles/nifi-windows-event-log-bundle/nifi-windows-event-log-processors/src/main/java/org/apache/nifi/processors/windows/event/log/jna/EventSubscribeXmlRenderingCallback.java
@@ -46,6 +46,7 @@ public class EventSubscribeXmlRenderingCallback implements WEvtApi.EVT_SUBSCRIBE
     private Memory buffer;
     private Memory used;
     private Memory propertyCount;
+    private boolean subscriptionFailed;
 
     public EventSubscribeXmlRenderingCallback(ComponentLog logger, Consumer<String> consumer, int maxBufferSize, WEvtApi wEvtApi, Kernel32 kernel32, ErrorLookup errorLookup) {
         this.logger = logger;
@@ -67,11 +68,20 @@ public class EventSubscribeXmlRenderingCallback implements WEvtApi.EVT_SUBSCRIBE
         }
 
         if (evtSubscribeNotifyAction == WEvtApi.EvtSubscribeNotifyAction.ERROR) {
-            if (eventHandle.getPointer().getInt(0) == WEvtApi.EvtSubscribeErrors.ERROR_EVT_QUERY_RESULT_STALE) {
-                logger.error(MISSING_EVENT_MESSAGE);
-            } else {
-                logger.error(RECEIVED_THE_FOLLOWING_WIN32_ERROR + eventHandle.getPointer().getInt(0));
+            try {
+                final int errorCode = eventHandle.getPointer().getInt(0);
+                if (errorCode == WEvtApi.EvtSubscribeErrors.ERROR_EVT_QUERY_RESULT_STALE) {
+                    logger.error(MISSING_EVENT_MESSAGE);
+                } else {
+                    logger.error(RECEIVED_THE_FOLLOWING_WIN32_ERROR + errorCode);
+                }
+            } catch (final Error e) {
+                logger.error("Failed to get error code onEvent("
+                    + evtSubscribeNotifyAction + ", " + userContext + ", " + eventHandle);
+            } finally {
+                subscriptionFailed = true;
             }
+
         } else if (evtSubscribeNotifyAction == WEvtApi.EvtSubscribeNotifyAction.DELIVER) {
             wEvtApi.EvtRender(null, eventHandle, WEvtApi.EvtRenderFlags.EVENT_XML, size, buffer, used, propertyCount);
 
@@ -104,4 +114,8 @@ public class EventSubscribeXmlRenderingCallback implements WEvtApi.EVT_SUBSCRIBE
         // Ignored, see https://msdn.microsoft.com/en-us/library/windows/desktop/aa385577(v=vs.85).aspx
         return 0;
     }
+
+    public boolean isSubscriptionFailed() {
+        return subscriptionFailed;
+    }
 }