From 87d3c1c9bda5853093131b9c81e408a1dda3e543 Mon Sep 17 00:00:00 2001
From: Clebert Suconic <clebertsuconic@apache.org>
Date: Wed, 7 Sep 2016 11:12:20 -0400
Subject: [PATCH] ARTEMIS-716 max replication = 0 should mean do not make
 copies at all

---
 .../core/server/ActiveMQServerLogger.java     |  5 +++
 .../core/server/files/FileMoveManager.java    | 33 ++++++++++++++-----
 .../server/files/FileMoveManagerTest.java     | 32 ++++++++++++++++--
 docs/user-manual/en/configuration-index.md    |  2 +-
 .../cluster/failover/FailoverTestBase.java    |  2 +-
 5 files changed, 61 insertions(+), 13 deletions(-)

diff --git a/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/ActiveMQServerLogger.java b/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/ActiveMQServerLogger.java
index 7696c01187..4ae54424c5 100644
--- a/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/ActiveMQServerLogger.java
+++ b/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/ActiveMQServerLogger.java
@@ -1136,6 +1136,11 @@ public interface ActiveMQServerLogger extends BasicLogger {
       format = Message.Format.MESSAGE_FORMAT)
    void activateSharedStoreSlaveFailed(@Cause Throwable e);
 
+   @LogMessage(level = Logger.Level.INFO)
+   @Message(id = 222190, value = "Deleting old data directory {0} as the max folders is set to 0", format = Message.Format.MESSAGE_FORMAT)
+   void backupDeletingData(String oldPath);
+
+
    @LogMessage(level = Logger.Level.WARN)
    @Message(id = 222191,
       value = "Could not find any configured role for user {0}.",
diff --git a/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/files/FileMoveManager.java b/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/files/FileMoveManager.java
index 3e250f6d1f..2cd5632edd 100644
--- a/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/files/FileMoveManager.java
+++ b/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/files/FileMoveManager.java
@@ -98,16 +98,26 @@ public class FileMoveManager {
 
       int whereToMove = getMaxID() + 1;
 
-      File folderTo = getFolder(whereToMove);
-      folderTo.mkdirs();
+      if (maxFolders == 0) {
+         ActiveMQServerLogger.LOGGER.backupDeletingData(folder.getPath());
+         for (String fileMove : files) {
+            File fileFrom = new File(folder, fileMove);
+            logger.tracef("deleting %s", fileFrom);
+            deleteTree(fileFrom);
+         }
+      }
+      else {
+         File folderTo = getFolder(whereToMove);
+         folderTo.mkdirs();
 
-      ActiveMQServerLogger.LOGGER.backupMovingDataAway(folder.getPath(), folderTo.getPath());
+         ActiveMQServerLogger.LOGGER.backupMovingDataAway(folder.getPath(), folderTo.getPath());
 
-      for (String fileMove : files) {
-         File fileFrom = new File(folder, fileMove);
-         File fileTo = new File(folderTo, fileMove);
-         logger.tracef("doMove:: moving %s as %s", fileFrom, fileTo);
-         Files.move(fileFrom.toPath(), fileTo.toPath());
+         for (String fileMove : files) {
+            File fileFrom = new File(folder, fileMove);
+            File fileTo = new File(folderTo, fileMove);
+            logger.tracef("doMove:: moving %s as %s", fileFrom, fileTo);
+            Files.move(fileFrom.toPath(), fileTo.toPath());
+         }
       }
 
    }
@@ -117,9 +127,14 @@ public class FileMoveManager {
    }
 
    private void internalCheckOldFolders(int creating) {
-      if (maxFolders > 0) {
+      if (maxFolders >= 0) {
          int folders = getNumberOfFolders();
 
+         if (folders == 0) {
+            // no folders.. nothing to be done
+            return;
+         }
+
          // We are counting the next one to be created
          int foldersToDelete = folders + creating - maxFolders;
 
diff --git a/artemis-server/src/test/java/org/apache/activemq/artemis/core/server/files/FileMoveManagerTest.java b/artemis-server/src/test/java/org/apache/activemq/artemis/core/server/files/FileMoveManagerTest.java
index 7353b472ad..299f0bc238 100644
--- a/artemis-server/src/test/java/org/apache/activemq/artemis/core/server/files/FileMoveManagerTest.java
+++ b/artemis-server/src/test/java/org/apache/activemq/artemis/core/server/files/FileMoveManagerTest.java
@@ -200,7 +200,7 @@ public class FileMoveManagerTest {
 
       Assert.assertEquals(manager.getMaxFolders(), manager.getNumberOfFolders());
 
-      manager.setMaxFolders(0).checkOldFolders();
+      manager.setMaxFolders(-1).checkOldFolders();
 
       Assert.assertEquals(3, manager.getNumberOfFolders());
 
@@ -255,7 +255,7 @@ public class FileMoveManagerTest {
 
       Assert.assertEquals(manager.getMaxFolders(), manager.getNumberOfFolders());
 
-      manager.setMaxFolders(0).checkOldFolders();
+      manager.setMaxFolders(-1).checkOldFolders();
 
       Assert.assertEquals(3, manager.getNumberOfFolders());
 
@@ -267,6 +267,34 @@ public class FileMoveManagerTest {
       Assert.assertEquals(10, manager.getMinID());
    }
 
+   @Test
+   public void testMaxZero() throws Exception {
+      manager.setMaxFolders(0);
+
+      int NUMBER_OF_FOLDERS = 10;
+      int FILES_PER_FOLDER = 10;
+
+      for (int bkp = 1; bkp <= 10; bkp++) {
+         for (int f = 0; f < NUMBER_OF_FOLDERS; f++) {
+            File folderF = new File(dataLocation, "folder" + f);
+            folderF.mkdirs();
+
+            // FILES_PER_FOLDER + f, I'm just creating more files as f grows.
+            // this is just to make each folder unique somehow
+            for (int i = 0; i < FILES_PER_FOLDER + f; i++) {
+               createFile(folderF, i);
+            }
+         }
+
+         manager.doMove();
+
+         // We will always have maximum of 3 folders
+         Assert.assertEquals(0, manager.getNumberOfFolders());
+      }
+
+      Assert.assertEquals(0, manager.getMaxID());
+   }
+
    @Test
    public void testMoveOverPaging() throws Exception {
       AssertionLoggerHandler.startCapture();
diff --git a/docs/user-manual/en/configuration-index.md b/docs/user-manual/en/configuration-index.md
index b9a49fb235..c47861bb3e 100644
--- a/docs/user-manual/en/configuration-index.md
+++ b/docs/user-manual/en/configuration-index.md
@@ -66,7 +66,7 @@ Name | Description
 [management-address](management.md "Configuring Core Management")   |  the name of the management address to send management messages to. It is prefixed with "jms.queue" so that JMS clients can send messages to it. Default=jms.queue.activemq.management
 [management-notification-address](management.md "Configuring The Core Management Notification Address") |  the name of the address that consumers bind to receive management notifications. Default=activemq.notifications
 [mask-password](configuration-index.md "Using Masked Passwords in Configuration Files")  |  This option controls whether passwords in server configuration need be masked. If set to "true" the passwords are masked. Default=false
-[max-saved-replicated-journals-size]()                                                                |    This specifies how many times a replicated backup server can restart after moving its files on start. Once there are this number of backup journal files the server will stop permanently after if fails back. Default=2
+[max-saved-replicated-journals-size](ha.md#data-replication)                                                                |    This specifies how many times a replicated backup server can restart after moving its files on start. Once there are this number of backup journal files the server will stop permanently after if fails back. -1 Means no Limit, 0 don't keep a copy at all, Default=2
 [max-disk-usage](paging.md#max-disk-usage) | The max percentage of data we should use from disks. The System will block while the disk is full. Default=100
 [memory-measure-interval](perf-tuning.md)                                                             |  frequency to sample JVM memory in ms (or -1 to disable memory sampling). Default=-1
 [memory-warning-threshold](perf-tuning.md)                                                            |  Percentage of available memory which will trigger a warning log. Default=25
diff --git a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/cluster/failover/FailoverTestBase.java b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/cluster/failover/FailoverTestBase.java
index 67123df2c8..aa87b28236 100644
--- a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/cluster/failover/FailoverTestBase.java
+++ b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/cluster/failover/FailoverTestBase.java
@@ -191,7 +191,7 @@ public abstract class FailoverTestBase extends ActiveMQTestBase {
    }
 
    protected void setupHAPolicyConfiguration() {
-      ((ReplicaPolicyConfiguration) backupConfig.getHAPolicyConfiguration()).setMaxSavedReplicatedJournalsSize(0).setAllowFailBack(true);
+      ((ReplicaPolicyConfiguration) backupConfig.getHAPolicyConfiguration()).setMaxSavedReplicatedJournalsSize(-1).setAllowFailBack(true);
       ((ReplicaPolicyConfiguration) backupConfig.getHAPolicyConfiguration()).setRestartBackup(false);
    }