From 30cdaded4aa75d49c663640e5d6659228e0ca1a9 Mon Sep 17 00:00:00 2001
From: Matthew Swift <matthew.swift@forgerock.com>
Date: Fri, 31 May 2013 17:25:23 +0000
Subject: [PATCH] Fix for OPENDJ-875: Use of hostnames in replication protocol causes failover problems

---
 opendj-sdk/opends/src/server/org/opends/server/replication/server/ReplicationServer.java |   44 +++++++++++++++++++++++++++++++++++++-------
 1 files changed, 37 insertions(+), 7 deletions(-)

diff --git a/opendj-sdk/opends/src/server/org/opends/server/replication/server/ReplicationServer.java b/opendj-sdk/opends/src/server/org/opends/server/replication/server/ReplicationServer.java
index 4076a1d..629da1c 100644
--- a/opendj-sdk/opends/src/server/org/opends/server/replication/server/ReplicationServer.java
+++ b/opendj-sdk/opends/src/server/org/opends/server/replication/server/ReplicationServer.java
@@ -537,11 +537,7 @@
       dbEnv = new ReplicationDbEnv(getFileForPath(dbDirname).getAbsolutePath(),
           this);
 
-      /*
-       * Open replicationServer socket
-       */
-      String localhostname = InetAddress.getLocalHost().getHostName();
-      serverURL = localhostname + ":" + String.valueOf(replicationPort);
+      setServerURL();
       listenSocket = new ServerSocket();
       listenSocket.bind(new InetSocketAddress(replicationPort));
 
@@ -1038,8 +1034,7 @@
         stopListen = false;
 
         replicationPort = newPort;
-        String localhostname = InetAddress.getLocalHost().getHostName();
-        serverURL = localhostname + ":" + String.valueOf(replicationPort);
+        setServerURL();
         listenSocket = new ServerSocket();
         listenSocket.bind(new InetSocketAddress(replicationPort));
 
@@ -1151,6 +1146,41 @@
     return new ConfigChangeResult(ResultCode.SUCCESS, false);
   }
 
+  /*
+   * Try and set a sensible URL for this replication server. Since we are
+   * listening on all addresses there are a couple of potential candidates: 1) a
+   * matching server url in the replication server's configuration, 2) hostname
+   * local address.
+   */
+  private void setServerURL() throws UnknownHostException
+  {
+    /*
+     * First try the set of configured replication servers to see if one of them
+     * is this replication server (this should always be the case).
+     */
+    for (String rs : replicationServers)
+    {
+      /*
+       * No need validate the string format because the admin framework has
+       * already done it.
+       */
+      final int index = rs.lastIndexOf(":");
+      final String hostname = rs.substring(0, index);
+      final int port = Integer.parseInt(rs.substring(index + 1));
+      if (port == replicationPort && isLocalAddress(hostname))
+      {
+        serverURL = rs;
+        return;
+      }
+    }
+
+    /*
+     * Fall-back to the machine hostname.
+     */
+    serverURL = InetAddress.getLocalHost().getHostName() + ":"
+        + replicationPort;
+  }
+
   /**
    * Broadcast a configuration change that just happened to the whole topology
    * by sending a TopologyMsg to every entity in the topology.

--
Gitblit v1.10.0