From 61aad5b3ed8c70c8fa83494b0b2d3591eb420b81 Mon Sep 17 00:00:00 2001
From: ian.packer <ian.packer@forgerock.com>
Date: Tue, 11 Oct 2016 16:57:48 +0000
Subject: [PATCH] OPENDJ-3237: Make read/writing offline.state files more robust

---
 opendj-server-legacy/src/test/java/org/opends/server/replication/server/changelog/file/ReplicationEnvironmentTest.java |    7 +++++--
 opendj-server-legacy/src/main/java/org/opends/server/replication/server/changelog/file/ReplicationEnvironment.java     |   17 ++++++++++++-----
 2 files changed, 17 insertions(+), 7 deletions(-)

diff --git a/opendj-server-legacy/src/main/java/org/opends/server/replication/server/changelog/file/ReplicationEnvironment.java b/opendj-server-legacy/src/main/java/org/opends/server/replication/server/changelog/file/ReplicationEnvironment.java
index 9d0cedf..d52eb2a 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/replication/server/changelog/file/ReplicationEnvironment.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/replication/server/changelog/file/ReplicationEnvironment.java
@@ -531,7 +531,7 @@
       {
         // Only the last sent offline CSN is kept
         writer.write(offlineCSN.toString());
-        StaticUtils.close(writer);
+        writer.close();
         changelogState.addOfflineReplica(domainDN, offlineCSN);
         commitFile(offlineFile);
       }
@@ -669,11 +669,18 @@
   {
     state.addServerIdToDomain(toServerId(serverIdPath.getName()), domainDN);
 
-    final File offlineFile = new File(serverIdPath, REPLICA_OFFLINE_STATE_FILENAME);
-    if (offlineFile.exists())
+    try
     {
-      final CSN offlineCSN = readOfflineStateFile(offlineFile, domainDN);
-      state.addOfflineReplica(domainDN, offlineCSN);
+      final File offlineFile = new File(serverIdPath, REPLICA_OFFLINE_STATE_FILENAME);
+      if (offlineFile.exists())
+      {
+        final CSN offlineCSN = readOfflineStateFile(offlineFile, domainDN);
+        state.addOfflineReplica(domainDN, offlineCSN);
+      }
+    }
+    catch(ChangelogException ce)
+    {
+      logger.warn(getExceptionMessage(ce));
     }
   }
 
diff --git a/opendj-server-legacy/src/test/java/org/opends/server/replication/server/changelog/file/ReplicationEnvironmentTest.java b/opendj-server-legacy/src/test/java/org/opends/server/replication/server/changelog/file/ReplicationEnvironmentTest.java
index 5f882e6..4dfb4f5 100644
--- a/opendj-server-legacy/src/test/java/org/opends/server/replication/server/changelog/file/ReplicationEnvironmentTest.java
+++ b/opendj-server-legacy/src/test/java/org/opends/server/replication/server/changelog/file/ReplicationEnvironmentTest.java
@@ -172,7 +172,7 @@
     }
   }
 
-  @Test(expectedExceptions=ChangelogException.class)
+  @Test
   public void testReadChangelogStateWithReplicaOfflineStateFileCorrupted() throws Exception
   {
     Log<Long,ChangeNumberIndexRecord> cnDB = null;
@@ -188,7 +188,10 @@
       File offlineStateFile = new File(environment.getServerIdPath("1", 1), REPLICA_OFFLINE_STATE_FILENAME);
       offlineStateFile.createNewFile();
 
-      environment.readOnDiskChangelogState();
+      // If the offline state file is not readable, the best we can do is continue as if the file wasn't there at all
+      final ChangelogState state = environment.readOnDiskChangelogState();
+      assertThat(state.getOfflineReplicas()).isEmpty();
+      assertThat(state.isEqualTo(environment.getChangelogState())).isTrue();
     }
     finally
     {

--
Gitblit v1.10.0