From 9eb54b5b0e109ecbaee9aa109b32dbf42323bd55 Mon Sep 17 00:00:00 2001
From: gbellato <gbellato@localhost>
Date: Thu, 24 Sep 2009 13:04:28 +0000
Subject: [PATCH] Second try for issue 4223. The first fix was not working because the synchronization code that had been added in the first fix was not called in this case.

---
 opendj-sdk/opends/src/server/org/opends/server/replication/server/DataServerHandler.java                              |    4 -
 opendj-sdk/opends/src/server/org/opends/server/replication/server/MessageHandler.java                                 |    9 ++-
 opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/replication/service/ReplicationDomainTest.java |  102 +++++++++++++++++++++++++++++++++
 opendj-sdk/opends/src/server/org/opends/server/replication/server/ReplicationServerDomain.java                        |    2 
 opendj-sdk/opends/src/server/org/opends/server/replication/server/ECLServerHandler.java                               |    6 +-
 opendj-sdk/opends/src/server/org/opends/server/replication/server/ReplicationServerHandler.java                       |    4 
 6 files changed, 114 insertions(+), 13 deletions(-)

diff --git a/opendj-sdk/opends/src/server/org/opends/server/replication/server/DataServerHandler.java b/opendj-sdk/opends/src/server/org/opends/server/replication/server/DataServerHandler.java
index 1eda9a1..65cfa2c 100644
--- a/opendj-sdk/opends/src/server/org/opends/server/replication/server/DataServerHandler.java
+++ b/opendj-sdk/opends/src/server/org/opends/server/replication/server/DataServerHandler.java
@@ -418,7 +418,7 @@
     heartbeatInterval = serverStartMsg.getHeartbeatInterval();
 
     // generic stuff
-    setServiceIdAndDomain(serverStartMsg.getBaseDn());
+    setServiceIdAndDomain(serverStartMsg.getBaseDn(), true);
     setInitialServerState(serverStartMsg.getServerState());
     setSendWindowSize(serverStartMsg.getWindowSize());
 
@@ -474,8 +474,6 @@
       boolean sessionInitiatorSSLEncryption =
         processStartFromRemote(inServerStartMsg);
 
-      // Get or Create the ReplicationServerDomain
-      replicationServerDomain = getDomain(true, true);
       localGenerationId = replicationServerDomain.getGenerationId();
       oldGenerationId = localGenerationId;
 
diff --git a/opendj-sdk/opends/src/server/org/opends/server/replication/server/ECLServerHandler.java b/opendj-sdk/opends/src/server/org/opends/server/replication/server/ECLServerHandler.java
index 56f18fb..456f37f 100644
--- a/opendj-sdk/opends/src/server/org/opends/server/replication/server/ECLServerHandler.java
+++ b/opendj-sdk/opends/src/server/org/opends/server/replication/server/ECLServerHandler.java
@@ -351,7 +351,7 @@
         replicationServer, rcvWindowSize);
     try
     {
-      setServiceIdAndDomain(ServerConstants.DN_EXTERNAL_CHANGELOG_ROOT);
+      setServiceIdAndDomain(ServerConstants.DN_EXTERNAL_CHANGELOG_ROOT, true);
     }
     catch(DirectoryException de)
     {
@@ -381,7 +381,7 @@
         replicationServer, 0);
     try
     {
-      setServiceIdAndDomain(ServerConstants.DN_EXTERNAL_CHANGELOG_ROOT);
+      setServiceIdAndDomain(ServerConstants.DN_EXTERNAL_CHANGELOG_ROOT, true);
     }
     catch(DirectoryException de)
     {
@@ -673,7 +673,7 @@
           // set initial state
           mh.setInitialServerState(newDomainCtxt.startState);
           // set serviceID and domain
-          mh.setServiceIdAndDomain(rsd.getBaseDn());
+          mh.setServiceIdAndDomain(rsd.getBaseDn(), false);
           // register the unconnected into the domain
           rsd.registerHandler(mh);
           newDomainCtxt.mh = mh;
diff --git a/opendj-sdk/opends/src/server/org/opends/server/replication/server/MessageHandler.java b/opendj-sdk/opends/src/server/org/opends/server/replication/server/MessageHandler.java
index 9025357..1ad9574 100644
--- a/opendj-sdk/opends/src/server/org/opends/server/replication/server/MessageHandler.java
+++ b/opendj-sdk/opends/src/server/org/opends/server/replication/server/MessageHandler.java
@@ -717,10 +717,13 @@
   /**
    * Set the serviceId (usually baseDn) for this handler. Expected to be done
    * once and never changed during the handler life.
-   * @param serviceId The provided serviceId.
+   *
+   * @param serviceId       The provided serviceId.
+   * @param isDataServer    The handler is a dataServer
+   *
    * @exception DirectoryException raised when a problem occurs.
    */
-  protected void setServiceIdAndDomain(String serviceId)
+  protected void setServiceIdAndDomain(String serviceId, boolean isDataServer)
   throws DirectoryException
   {
     if (this.serviceId != null)
@@ -737,7 +740,7 @@
     else
     {
       this.serviceId = serviceId;
-      this.replicationServerDomain = getDomain(true);
+      this.replicationServerDomain = getDomain(true, isDataServer);
     }
   }
 
diff --git a/opendj-sdk/opends/src/server/org/opends/server/replication/server/ReplicationServerDomain.java b/opendj-sdk/opends/src/server/org/opends/server/replication/server/ReplicationServerDomain.java
index 5cc04e7..11442a6 100644
--- a/opendj-sdk/opends/src/server/org/opends/server/replication/server/ReplicationServerDomain.java
+++ b/opendj-sdk/opends/src/server/org/opends/server/replication/server/ReplicationServerDomain.java
@@ -1501,7 +1501,7 @@
       } else
       {
         // the targeted server is NOT connected
-        // Let's search for THE changelog server that MAY
+        // Let's search for the replication server that MAY
         // have the targeted server connected.
         if (senderHandler.isDataServer())
         {
diff --git a/opendj-sdk/opends/src/server/org/opends/server/replication/server/ReplicationServerHandler.java b/opendj-sdk/opends/src/server/org/opends/server/replication/server/ReplicationServerHandler.java
index d71d11f..2c61852 100644
--- a/opendj-sdk/opends/src/server/org/opends/server/replication/server/ReplicationServerHandler.java
+++ b/opendj-sdk/opends/src/server/org/opends/server/replication/server/ReplicationServerHandler.java
@@ -93,7 +93,7 @@
       serverAddressURL =
         session.getRemoteAddress() + ":" + serverURL.substring(separator +
             1);
-      setServiceIdAndDomain(inReplServerStartMsg.getBaseDn());
+      setServiceIdAndDomain(inReplServerStartMsg.getBaseDn(), false);
       setInitialServerState(inReplServerStartMsg.getServerState());
       setSendWindowSize(inReplServerStartMsg.getWindowSize());
       if (protocolVersion > ProtocolVersion.REPLICATION_PROTOCOL_V1)
@@ -156,7 +156,7 @@
     // the encryption we will request to the peer as we are the session creator
     this.initSslEncryption = sslEncryption;
 
-    setServiceIdAndDomain(serviceId);
+    setServiceIdAndDomain(serviceId, false);
 
     localGenerationId = replicationServerDomain.getGenerationId();
     oldGenerationId = localGenerationId;
diff --git a/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/replication/service/ReplicationDomainTest.java b/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/replication/service/ReplicationDomainTest.java
index 6ec1f80..773babb 100644
--- a/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/replication/service/ReplicationDomainTest.java
+++ b/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/replication/service/ReplicationDomainTest.java
@@ -279,8 +279,10 @@
         replServer1.remove();
     }
   }
+
   /**
-   * Test that a ReplicationDomain is able to export and import its database.
+   * Test that a ReplicationDomain is able to export and import its database
+   * When there is only one replication server.
    */
   @Test(enabled=true)
   public void exportAndImport() throws Exception
@@ -363,6 +365,104 @@
   }
 
   /**
+   * Test that a ReplicationDomain is able to export and import its database
+   * across 2 replication servers.
+   */
+  @Test(enabled=true)
+  public void exportAndImportAcross2ReplServers() throws Exception
+  {
+    final int ENTRYCOUNT=5000;
+    String testService = "test";
+    ReplicationServer replServer2 = null;
+    ReplicationServer replServer1 = null;
+    int replServerID = 11;
+    int replServerID2 = 12;
+    FakeReplicationDomain domain1 = null;
+    FakeReplicationDomain domain2 = null;
+
+    try
+    {
+      // find  a free port for the replicationServer
+      ServerSocket socket = TestCaseUtils.bindFreePort();
+      int replServerPort1 = socket.getLocalPort();
+      socket.close();
+
+      socket = TestCaseUtils.bindFreePort();
+      int replServerPort2 = socket.getLocalPort();
+      socket.close();
+
+      TreeSet<String> replserver1 = new TreeSet<String>();
+      replserver1.add("localhost:" + replServerPort1);
+
+      TreeSet<String> replserver2 = new TreeSet<String>();
+      replserver2.add("localhost:" + replServerPort2);
+
+      ReplServerFakeConfiguration conf1 =
+        new ReplServerFakeConfiguration(
+            replServerPort1, "ReplicationDomainTestDb",
+            0, replServerID, 0, 100, null);
+
+      ReplServerFakeConfiguration conf2 =
+        new ReplServerFakeConfiguration(
+            replServerPort2, "ReplicationDomainTestDb",
+            0, replServerID2, 0, 100, replserver1);
+
+      replServer1 = new ReplicationServer(conf1);
+      replServer2 = new ReplicationServer(conf2);
+
+      ArrayList<String> servers1 = new ArrayList<String>(1);
+      servers1.add("localhost:" + replServerPort1);
+
+      ArrayList<String> servers2 = new ArrayList<String>(1);
+      servers2.add("localhost:" + replServerPort2);
+
+      StringBuilder exportedDataBuilder = new StringBuilder();
+      for (int i =0; i<ENTRYCOUNT; i++)
+      {
+        exportedDataBuilder.append("key : value"+i+"\n\n");
+      }
+      String exportedData=exportedDataBuilder.toString();
+      domain1 = new FakeReplicationDomain(
+          testService, (short) 1, servers1,
+          100, 0, exportedData, null, ENTRYCOUNT);
+
+      StringBuilder importedData = new StringBuilder();
+      domain2 = new FakeReplicationDomain(
+          testService, (short) 2, servers2, 100, 0,
+          null, importedData, 0);
+
+      domain2.initializeFromRemote((short)1);
+
+      int count = 0;
+      while ((importedData.length() < exportedData.length()) && (count < 500))
+      {
+        count ++;
+        Thread.sleep(100);
+      }
+      assertTrue(domain2.getLeftEntryCount() == 0,
+          "LeftEntryCount for export is " + domain2.getLeftEntryCount());
+      assertTrue(domain1.getLeftEntryCount() == 0,
+          "LeftEntryCount for import is " + domain1.getLeftEntryCount());
+      assertEquals(importedData.length(), exportedData.length());
+      assertEquals(importedData.toString(), exportedData);
+    }
+    finally
+    {
+      if (domain1 != null)
+        domain1.disableService();
+
+      if (domain2 != null)
+        domain2.disableService();
+
+      if (replServer1 != null)
+        replServer1.remove();
+
+      if (replServer2 != null)
+        replServer2.remove();
+    }
+  }
+
+  /**
    * Sender side of the Total Update Perf test.
    * The goal of this test is to measure the performance
    * of the total update code.

--
Gitblit v1.10.0