From b3112742902d0145779f9ac094857c0a3e9b1d79 Mon Sep 17 00:00:00 2001
From: gbellato <gbellato@localhost>
Date: Mon, 29 Jan 2007 17:21:53 +0000
Subject: [PATCH] - Fix issue 1160 : synchronization is flushing the msgQueue to the database too often The synchronization server is flushing all the queues of the messages received from a LDAP server each time a server needs to retrieve some changes that are not in memory anymore.

---
 opends/src/server/org/opends/server/synchronization/changelog/DbHandler.java |   35 +++++++++++++++++++++++++++++++----
 1 files changed, 31 insertions(+), 4 deletions(-)

diff --git a/opends/src/server/org/opends/server/synchronization/changelog/DbHandler.java b/opends/src/server/org/opends/server/synchronization/changelog/DbHandler.java
index 0fb35e6..90132ac 100644
--- a/opends/src/server/org/opends/server/synchronization/changelog/DbHandler.java
+++ b/opends/src/server/org/opends/server/synchronization/changelog/DbHandler.java
@@ -35,6 +35,7 @@
 import java.util.Date;
 import java.util.List;
 import java.util.LinkedList;
+import java.util.NoSuchElementException;
 
 import org.opends.server.api.DirectoryThread;
 import org.opends.server.api.MonitorProvider;
@@ -206,11 +207,37 @@
                            throws DatabaseException, Exception
   {
     /*
-     * make sure to flush some changes in the database so that
-     * we don't create the iterator on an empty database when the
-     * dbHandler has just been started.
+     * When we create an iterator we need to make sure that we
+     * don't miss some changes because the iterator is created
+     * close to the limit of the changed that have not yet been
+     * flushed to the database.
+     * We detect this by comparing the date of the changeNumber where
+     * we want to start with the date of the first ChangeNumber
+     * of the msgQueue.
+     * If this is the case we flush the queue to the database.
      */
-    flush();
+    ChangeNumber recentChangeNumber = null;
+
+    if (changeNumber == null)
+      flush();
+
+    synchronized (msgQueue)
+    {
+      try
+      {
+        UpdateMessage msg = msgQueue.getFirst();
+        recentChangeNumber = msg.getChangeNumber();
+      }
+      catch (NoSuchElementException e)
+      {}
+    }
+
+    if ( (recentChangeNumber != null) &&
+         (recentChangeNumber.getTimeSec() - changeNumber.getTimeSec() < 2))
+    {
+      flush();
+    }
+
     return new ChangelogIterator(serverId, db, changeNumber);
   }
 

--
Gitblit v1.10.0