Juan Hernandez has uploaded a new change for review.

Change subject: tools: Don't send events on startup
......................................................................

tools: Don't send events on startup

Currently the event notifier sends all the pending events during
startup. This can flood the recepients and it is not very useful as old
events are not valuable. This patch adds a new DAYS_TO_SEND_ON_STARTUP
parameter to the notifier. The value of this parameter is the number of
days of old audit log messages that will be sent during startup.
Messages older than that will be marked as processed and not sent.

Change-Id: Id1b8044533d29fa69585a157d3f8409c81a437cd
Signed-off-by: Juan Hernandez <juan.hernan...@redhat.com>
---
M 
backend/manager/tools/engine-notifier/engine-notifier-resources/src/main/resources/notifier.conf
M 
backend/manager/tools/engine-notifier/engine-notifier-service/src/main/java/org/ovirt/engine/core/notifier/NotificationService.java
M 
backend/manager/tools/engine-notifier/engine-notifier-service/src/main/java/org/ovirt/engine/core/notifier/utils/NotificationProperties.java
3 files changed, 89 insertions(+), 28 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/83/11483/1

diff --git 
a/backend/manager/tools/engine-notifier/engine-notifier-resources/src/main/resources/notifier.conf
 
b/backend/manager/tools/engine-notifier/engine-notifier-resources/src/main/resources/notifier.conf
index 075cf83..9498b75 100644
--- 
a/backend/manager/tools/engine-notifier/engine-notifier-resources/src/main/resources/notifier.conf
+++ 
b/backend/manager/tools/engine-notifier/engine-notifier-resources/src/main/resources/notifier.conf
@@ -43,6 +43,13 @@
 # Amount of days to keep dispatched events on history table. If not set, 
events remain on history table.
 #DAYS_TO_KEEP_HISTORY=30
 
+# This parameter specifies how many days of old events are processed and sent
+# when the notifier starts. If set to 2, for example, the notifier will
+# process and send the events of the last two days, older events will just
+# be marked as processed and won't be sent. The default is 0, so no old
+# messages will be sent at all during startup.
+#DAYS_TO_SEND_ON_STARTUP=0
+
 #----------------------------------#
 # Engine Monitoring Configuration: #
 #----------------------------------#
diff --git 
a/backend/manager/tools/engine-notifier/engine-notifier-service/src/main/java/org/ovirt/engine/core/notifier/NotificationService.java
 
b/backend/manager/tools/engine-notifier/engine-notifier-service/src/main/java/org/ovirt/engine/core/notifier/NotificationService.java
index 2e8f035..bc5fa26 100644
--- 
a/backend/manager/tools/engine-notifier/engine-notifier-service/src/main/java/org/ovirt/engine/core/notifier/NotificationService.java
+++ 
b/backend/manager/tools/engine-notifier/engine-notifier-service/src/main/java/org/ovirt/engine/core/notifier/NotificationService.java
@@ -6,6 +6,7 @@
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.sql.Statement;
+import java.sql.Timestamp;
 import java.util.ArrayList;
 import java.util.Calendar;
 import java.util.Date;
@@ -45,12 +46,13 @@
     private DataSource ds;
     private Map<String, String> prop = null;
     private NotificationMethodFactoryMapper methodsMapper = null;
-    private boolean shouldDeleteHistory = false;
-    private int daysToKeepHistory;
+    private int daysToKeepHistory = 0;
+    private int daysToSendOnStartup = 0;
 
     public NotificationService(NotificationConfigurator notificationConf) 
throws NotificationServiceException {
         this.prop = notificationConf.getProperties();
         initConfigurationProperties();
+        startup();
     }
 
     /**
@@ -63,28 +65,35 @@
      *             configuration setting error
      */
     private void initConfigurationProperties() throws 
NotificationServiceException {
-        String daysHistoryStr = 
prop.get(NotificationProperties.DAYS_TO_KEEP_HISTORY);
-        // verify property of history is well defined
-        if (StringUtils.isNotEmpty(daysHistoryStr)) {
-            try {
-                daysToKeepHistory = Integer.valueOf(daysHistoryStr).intValue();
-                if (daysToKeepHistory < 0) {
-                    throw new 
NumberFormatException(NotificationProperties.DAYS_TO_KEEP_HISTORY
-                            + " value should be a positive number");
-                }
-                daysToKeepHistory = daysToKeepHistory * -1;
-                shouldDeleteHistory = true;
-            } catch (NumberFormatException e) {
-                String err =
-                        String.format("Invalid format of %s: %s",
-                                NotificationProperties.DAYS_TO_KEEP_HISTORY,
-                                daysHistoryStr);
-                log.error(err, e);
-                throw new NotificationServiceException(err,e);
-            }
-        }
+        daysToKeepHistory = 
getNonNegativeIntegerProperty(NotificationProperties.DAYS_TO_KEEP_HISTORY);
+        daysToSendOnStartup = 
getNonNegativeIntegerProperty(NotificationProperties.DAYS_TO_SEND_ON_STARTUP);
         initConnectivity();
         initMethodMapper();
+    }
+
+    private int getNonNegativeIntegerProperty(final String name) throws 
NotificationServiceException {
+        // Get the text of the property:
+        final String text = prop.get(name);
+
+        // Validate it:
+        if (StringUtils.isNotEmpty(text)) {
+            try {
+                int value = Integer.parseInt(text);
+                if (value < 0) {
+                    throw new NumberFormatException(name + " value should be a 
positive number");
+                }
+                return value;
+            }
+            catch (NumberFormatException exception) {
+                String err =
+                        String.format("Invalid format of %s: %s", name, text);
+                log.error(err, exception);
+                throw new NotificationServiceException(err, exception);
+            }
+        }
+
+        // If the property can't be found then return 0 as the value:
+        return 0;
     }
 
     /**
@@ -93,9 +102,8 @@
     public void run() {
         try {
             log.debug("Start event notification service iteration");
-            startup();
             processEvents();
-            if (shouldDeleteHistory) {
+            if (daysToKeepHistory > 0) {
                 deleteObseleteHistoryData();
             }
             log.debug("Finish event notification service iteration");
@@ -112,11 +120,21 @@
      *             specifies which resource not available and a cause
      */
     private void startup() throws NotificationServiceException {
+        // Create the data source:
         try {
             ds = new StandaloneDataSource();
         }
         catch (SQLException exception) {
             throw new NotificationServiceException("Failed to initialize the 
connection helper", exception);
+        }
+
+        // Mark old events as processed so that during startup we don't send
+        // all of them:
+        try {
+            markOldEventsAsProcessed();
+        }
+        catch (SQLException exception) {
+            throw new NotificationServiceException("Failed mark old events as 
processed.", exception);
         }
     }
 
@@ -124,8 +142,8 @@
     private void deleteObseleteHistoryData() throws SQLException {
         Calendar cal = Calendar.getInstance();
         cal.setTime(new Date());
-        cal.add(Calendar.DATE, daysToKeepHistory);
-        java.sql.Timestamp startDeleteFrom = new 
java.sql.Timestamp(cal.getTimeInMillis());
+        cal.add(Calendar.DATE, -daysToKeepHistory);
+        Timestamp startDeleteFrom = new Timestamp(cal.getTimeInMillis());
         Connection connection = null;
         PreparedStatement deleteStmt = null;
         int deletedRecords;
@@ -134,7 +152,8 @@
             deleteStmt = connection.prepareStatement("delete from 
event_notification_hist where sent_at < ?");
             deleteStmt.setTimestamp(1, startDeleteFrom);
             deletedRecords = deleteStmt.executeUpdate();
-        } finally {
+        }
+        finally {
             if (deleteStmt != null) {
                 deleteStmt.close();
             }
@@ -144,7 +163,34 @@
         }
 
         if (deletedRecords > 0) {
-            log.debug(String.valueOf(deletedRecords) + " records were deleted 
from event_notification_hist table");
+            log.debug(deletedRecords + " records were deleted from 
\"event_notification_hist\" table.");
+        }
+    }
+
+    private void markOldEventsAsProcessed() throws SQLException {
+        Calendar calendar = Calendar.getInstance();
+        calendar.add(Calendar.DATE, -daysToSendOnStartup);
+        Timestamp ts = new Timestamp(calendar.getTimeInMillis());
+        Connection connection = null;
+        PreparedStatement statement = null;
+        int updatedRecords;
+        try {
+            connection = ds.getConnection();
+            statement = connection.prepareStatement(
+                "update audit_log set " +
+                    "processed = 'true' " +
+                "where " +
+                    "processed = 'false' " +
+                    "and log_time < ?"
+            );
+            statement.setTimestamp(1, ts);
+            updatedRecords = statement.executeUpdate();
+        } finally {
+            DbUtils.closeQuietly(statement, connection);
+        }
+
+        if (updatedRecords > 0) {
+            log.debug(updatedRecords + " old records were marked as processed 
in the \"audit_log\" table.");
         }
     }
 
diff --git 
a/backend/manager/tools/engine-notifier/engine-notifier-service/src/main/java/org/ovirt/engine/core/notifier/utils/NotificationProperties.java
 
b/backend/manager/tools/engine-notifier/engine-notifier-service/src/main/java/org/ovirt/engine/core/notifier/utils/NotificationProperties.java
index a170e95..a7055fb 100644
--- 
a/backend/manager/tools/engine-notifier/engine-notifier-service/src/main/java/org/ovirt/engine/core/notifier/utils/NotificationProperties.java
+++ 
b/backend/manager/tools/engine-notifier/engine-notifier-service/src/main/java/org/ovirt/engine/core/notifier/utils/NotificationProperties.java
@@ -46,6 +46,14 @@
     public static final String ENGINE_PID = "ENGINE_PID";
     public static final String DEFAULT_ENGINE_PID = 
"/var/run/ovirt-engine.pid";
 
+       /**
+        * This parameter specifies how many days of old events are processed 
and
+        * sent when the notifier starts. If set to 2, for example, the notifier
+        * will process and send the events of the last two days, older events 
will
+        * just be marked as processed and won't be sent.
+        */
+    public static final String DAYS_TO_SEND_ON_STARTUP = 
"DAYS_TO_SEND_ON_STARTUP";
+
     private static final Log log = 
LogFactory.getLog(NotificationProperties.class);
 
     /**


--
To view, visit http://gerrit.ovirt.org/11483
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Id1b8044533d29fa69585a157d3f8409c81a437cd
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine
Gerrit-Branch: master
Gerrit-Owner: Juan Hernandez <juan.hernan...@redhat.com>
_______________________________________________
Engine-patches mailing list
Engine-patches@ovirt.org
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to