You are viewing a plain text version of this content. The canonical link for it is here.
Posted to gitbox@hive.apache.org by GitBox <gi...@apache.org> on 2020/12/09 06:23:37 UTC

[GitHub] [hive] aasha commented on a change in pull request #1710: HIVE-24432: Delete Notification Events in Batches

aasha commented on a change in pull request #1710:
URL: https://github.com/apache/hive/pull/1710#discussion_r539040762



##########
File path: standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/ObjectStore.java
##########
@@ -10800,53 +10801,89 @@ public void addNotificationEvent(NotificationEvent entry) throws MetaException {
 
   @Override
   public void cleanNotificationEvents(int olderThan) {
-    boolean commited = false;
-    Query query = null;
+    final int eventBatchSize = MetastoreConf.getIntVar(conf, MetastoreConf.ConfVars.EVENT_CLEAN_MAX_EVENTS);
+
+    final long ageSec = olderThan;
+    final Instant now = Instant.now();
+
+    final int tooOld = Math.toIntExact(now.getEpochSecond() - ageSec);
+
+    final Optional<Integer> batchSize = (eventBatchSize > 0) ? Optional.of(eventBatchSize) : Optional.empty();
+
+    final long start = System.nanoTime();
+    int deleteCount = doCleanNotificationEvents(tooOld, batchSize);
+
+    if (deleteCount == 0) {
+      LOG.info("No Notification events found to be cleaned with eventTime < {}", tooOld);
+    } else {
+      int batchCount = 0;
+      do {
+        batchCount = doCleanNotificationEvents(tooOld, batchSize);
+        deleteCount += batchCount;
+      } while (batchCount > 0);
+    }
+
+    final long finish = System.nanoTime();
+
+    LOG.info("Deleted {} notification events older than epoch:{} in {}ms", deleteCount, tooOld,
+        TimeUnit.NANOSECONDS.toMillis(finish - start));
+  }
+
+  private int doCleanNotificationEvents(final int ageSec, final Optional<Integer> batchSize) {
+    final Transaction tx = pm.currentTransaction();
+    int eventsCount = 0;
+
     try {
-      openTransaction();
-      long tmp = System.currentTimeMillis() / 1000 - olderThan;
-      int tooOld = (tmp > Integer.MAX_VALUE) ? 0 : (int) tmp;
-      query = pm.newQuery(MNotificationLog.class, "eventTime < tooOld");
-      query.declareParameters("java.lang.Integer tooOld");
+      tx.begin();
 
-      int max_events = MetastoreConf.getIntVar(conf, MetastoreConf.ConfVars.EVENT_CLEAN_MAX_EVENTS);
-      max_events = max_events > 0 ? max_events : Integer.MAX_VALUE;
-      query.setRange(0, max_events);
-      query.setOrdering("eventId ascending");
+      try (Query query = pm.newQuery(MNotificationLog.class, "eventTime < tooOld")) {
+        query.declareParameters("java.lang.Integer tooOld");
+        query.setOrdering("eventId ascending");
+        if (batchSize.isPresent()) {
+          query.setRange(0, batchSize.get());
+        }
 
-      List<MNotificationLog> toBeRemoved = (List) query.execute(tooOld);
-      int iteration = 0;
-      int eventCount = 0;
-      long minEventId = 0;
-      long minEventTime = 0;
-      long maxEventId = 0;
-      long maxEventTime = 0;
-      while (CollectionUtils.isNotEmpty(toBeRemoved)) {
-        int listSize = toBeRemoved.size();
-        if (iteration == 0) {
-          MNotificationLog firstNotification = toBeRemoved.get(0);
-          minEventId = firstNotification.getEventId();
-          minEventTime = firstNotification.getEventTime();
+        List<MNotificationLog> events = (List) query.execute(ageSec);
+        if (CollectionUtils.isNotEmpty(events)) {
+          eventsCount = events.size();
+
+          if (LOG.isDebugEnabled()) {
+            int minEventTime, maxEventTime;
+            long minEventId, maxEventId;
+            Iterator<MNotificationLog> iter = events.iterator();
+            MNotificationLog firstNotification = iter.next();
+
+            minEventTime = maxEventTime = firstNotification.getEventTime();
+            minEventId = maxEventId = firstNotification.getEventId();
+
+            while (iter.hasNext()) {
+              MNotificationLog notification = iter.next();

Review comment:
       comparison is not required. events will always be in ascending order of event id




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

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



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org