Permalink
Browse files

HORNETQ-1083 replicated backup must also persist their nodeID (also a…

…ffects live's doing fail-back)
  • Loading branch information...
1 parent c5ec083 commit 3222579573b8eebbc6d99b9847b577544a0e7c88 Francisco Borges committed Nov 14, 2012
Showing with 323 additions and 273 deletions.
  1. +1 −1 hornetq-server/src/main/java/org/hornetq/core/persistence/impl/journal/PrintData.java
  2. +153 −14 hornetq-server/src/main/java/org/hornetq/core/server/NodeManager.java
  3. +0 −5 hornetq-server/src/main/java/org/hornetq/core/server/cluster/ClusterManager.java
  4. +3 −2 hornetq-server/src/main/java/org/hornetq/core/server/impl/AIOFileLockNodeManager.java
  5. +15 −122 hornetq-server/src/main/java/org/hornetq/core/server/impl/FileLockNodeManager.java
  6. +34 −27 hornetq-server/src/main/java/org/hornetq/core/server/impl/HornetQServerImpl.java
  7. +6 −2 hornetq-server/src/main/java/org/hornetq/core/server/impl/QuorumManager.java
  8. +13 −16 hornetq-server/src/test/java/org/hornetq/core/server/impl/InVMNodeManager.java
  9. +3 −1 hornetq-server/src/test/java/org/hornetq/tests/util/InVMNodeManagerServer.java
  10. +1 −1 tests/integration-tests/src/test/java/org/hornetq/tests/integration/cluster/NodeManagerAction.java
  11. +1 −1 tests/integration-tests/src/test/java/org/hornetq/tests/integration/cluster/NodeManagerTest.java
  12. +5 −5 tests/integration-tests/src/test/java/org/hornetq/tests/integration/cluster/RealNodeManagerTest.java
  13. +2 −2 ...gration-tests/src/test/java/org/hornetq/tests/integration/cluster/bridge/BridgeReconnectTest.java
  14. +9 −1 ...ation-tests/src/test/java/org/hornetq/tests/integration/cluster/distribution/ClusterTestBase.java
  15. +30 −0 ...ion-tests/src/test/java/org/hornetq/tests/integration/cluster/failover/BackupSyncJournalTest.java
  16. +1 −1 ...egration-tests/src/test/java/org/hornetq/tests/integration/cluster/failover/FailBackAutoTest.java
  17. +1 −1 ...ration-tests/src/test/java/org/hornetq/tests/integration/cluster/failover/FailBackManualTest.java
  18. +1 −1 ...tion-tests/src/test/java/org/hornetq/tests/integration/cluster/failover/FailoverListenerTest.java
  19. +7 −7 ...egration-tests/src/test/java/org/hornetq/tests/integration/cluster/failover/FailoverTestBase.java
  20. +1 −1 ...src/test/java/org/hornetq/tests/integration/cluster/failover/GroupingFailoverReplicationTest.java
  21. +1 −1 ...rc/test/java/org/hornetq/tests/integration/cluster/failover/GroupingFailoverSharedServerTest.java
  22. +8 −10 ...-tests/src/test/java/org/hornetq/tests/integration/cluster/failover/GroupingFailoverTestBase.java
  23. +5 −5 ...java/org/hornetq/tests/integration/cluster/failover/MultipleLivesMultipleBackupsFailoverTest.java
  24. +7 −6 ...ests/src/test/java/org/hornetq/tests/integration/cluster/failover/ReplicatedDistributionTest.java
  25. +1 −1 ...tion-tests/src/test/java/org/hornetq/tests/integration/cluster/failover/SecurityFailoverTest.java
  26. +1 −24 ...sts/src/test/java/org/hornetq/tests/integration/cluster/failover/SharedStoreDistributionTest.java
  27. +4 −4 ...st/java/org/hornetq/tests/integration/cluster/failover/SingleLiveMultipleBackupsFailoverTest.java
  28. +1 −5 tests/integration-tests/src/test/java/org/hornetq/tests/integration/discovery/DiscoveryTest.java
  29. +1 −1 ...ration-tests/src/test/java/org/hornetq/tests/integration/jms/cluster/JMSFailoverListenerTest.java
  30. +4 −3 tests/integration-tests/src/test/java/org/hornetq/tests/integration/jms/cluster/JMSFailoverTest.java
  31. +3 −2 tests/unit-tests/src/test/java/org/hornetq/tests/unit/core/server/impl/FileLockTest.java
@@ -45,7 +45,7 @@ public static void main(String arg[])
{
try
{
- FileLockNodeManager fileLock = new FileLockNodeManager(arg[1]);
+ FileLockNodeManager fileLock = new FileLockNodeManager(arg[1], false);
fileLock.start();
System.out.println("********************************************");
System.out.println("Server's ID=" + fileLock.getNodeId().toString());
@@ -13,11 +13,17 @@
package org.hornetq.core.server;
+import java.io.File;
+import java.io.FileNotFoundException;
import java.io.IOException;
+import java.io.RandomAccessFile;
+import java.nio.ByteBuffer;
+import java.nio.channels.FileChannel;
import org.hornetq.api.core.HornetQIllegalStateException;
import org.hornetq.api.core.SimpleString;
import org.hornetq.utils.UUID;
+import org.hornetq.utils.UUIDGenerator;
/**
* @author <a href="mailto:andy.taylor@jboss.com">Andy Taylor</a>
@@ -26,6 +32,28 @@
*/
public abstract class NodeManager implements HornetQComponent
{
+ protected static final byte FIRST_TIME_START = '0';
+ private static final String SERVER_LOCK_NAME = "server.lock";
+ private static final String ACCESS_MODE = "rw";
+
+ protected final boolean replicatedBackup;
+ private final String directory;
+ private final Object nodeIDGuard = new String();
+ private SimpleString nodeID;
+ private UUID uuid;
+ private String nodeGroupName;
+ private boolean isStarted = false;
+
+ protected FileChannel channel;
+
+ public NodeManager(final boolean replicatedBackup, final String directory)
+ {
+ this.directory = directory;
+ this.replicatedBackup = replicatedBackup;
+ }
+
+ // --------------------------------------------------------------------
+
public abstract void awaitLiveNode() throws Exception;
public abstract void startBackup() throws Exception;
@@ -36,27 +64,15 @@
public abstract void crashLiveServer() throws Exception;
- public abstract void stopBackup() throws Exception;
-
public abstract void releaseBackup() throws Exception;
- private boolean isStarted = false;
+ // --------------------------------------------------------------------
- private final Object nodeIDGuard = new Object();
- private SimpleString nodeID;
- private UUID uuid;
- private String nodeGroupName;
-
- public void start() throws Exception
+ public synchronized void start() throws Exception
{
isStarted = true;
}
- public void stop() throws Exception
- {
- isStarted = false;
- }
-
public boolean isStarted()
{
return isStarted;
@@ -123,4 +139,127 @@ public String getNodeGroupName()
public abstract void interrupt();
+ @Override
+ public synchronized void stop() throws Exception
+ {
+ FileChannel channelCopy = channel;
+ if (channelCopy != null)
+ channelCopy.close();
+ isStarted = false;
+ }
+
+ public final void stopBackup() throws Exception
+ {
+ if (replicatedBackup && getNodeId() != null)
+ {
+ setUpServerLockFile();
+ }
+ releaseBackup();
+ }
+
+ /**
+ * Ensures existence of persistent information about the server's nodeID.
+ * <p>
+ * Roughly the different use cases are:
+ * <ol>
+ * <li>old live server restarts: a server.lock file already exists and contains a nodeID.
+ * <li>new live server starting for the first time: no file exists, and we just *create* a new
+ * UUID to use as nodeID
+ * <li>replicated backup received its nodeID from its live: no file exists, we need to persist
+ * the *current* nodeID
+ * </ol>
+ * @throws Exception
+ * @throws FileNotFoundException
+ * @throws IOException
+ */
+ protected synchronized final void setUpServerLockFile() throws FileNotFoundException, IOException
+ {
+ File serverLockFile = newFile(SERVER_LOCK_NAME);
+
+ boolean fileCreated = false;
+
+ if (!serverLockFile.exists())
+ {
+ try
+ {
+ fileCreated = serverLockFile.createNewFile();
+ }
+ catch (RuntimeException e)
+ {
+ HornetQServerLogger.LOGGER.nodeManagerCantOpenFile(e, serverLockFile);
+ throw e;
+ }
+ catch (IOException e)
+ {
+ HornetQServerLogger.LOGGER.nodeManagerCantOpenFile(e, serverLockFile);
+ throw e;
+ }
+ if (!fileCreated)
+ {
+ throw new IllegalStateException("Unable to create server lock file");
+ }
+ }
+
+ @SuppressWarnings("resource")
+ RandomAccessFile raFile = new RandomAccessFile(serverLockFile, ACCESS_MODE);
+
+ channel = raFile.getChannel();
+
+ if (fileCreated)
+ {
+ ByteBuffer id = ByteBuffer.allocateDirect(3);
+ byte[] bytes = new byte[3];
+ bytes[0] = FIRST_TIME_START;
+ bytes[1] = FIRST_TIME_START;
+ bytes[2] = FIRST_TIME_START;
+ id.put(bytes, 0, 3);
+ id.position(0);
+ channel.write(id, 0);
+ channel.force(true);
+ }
+
+ createNodeId();
+ }
+
+ /**
+ * @return
+ */
+ protected final File newFile(final String fileName)
+ {
+ File file = new File(directory, fileName);
+ return file;
+ }
+
+ protected final synchronized void createNodeId() throws IOException
+ {
+ synchronized (nodeIDGuard)
+ {
+ ByteBuffer id = ByteBuffer.allocateDirect(16);
+ int read = channel.read(id, 3);
+ if (replicatedBackup)
+ {
+ id.position(0);
+ id.put(getUUID().asBytes(), 0, 16);
+ id.position(0);
+ channel.write(id, 3);
+ channel.force(true);
+ }
+ else if (read != 16)
+ {
+ setUUID(UUIDGenerator.getInstance().generateUUID());
+ id.put(getUUID().asBytes(), 0, 16);
+ id.position(0);
+ channel.write(id, 3);
+ channel.force(true);
+ }
+ else
+ {
+ byte[] bytes = new byte[16];
+ id.position(0);
+ id.get(bytes);
+ setUUID(new UUID(UUID.TYPE_TIME_BASED, bytes));
+ }
+ }
+ }
+
}
@@ -104,11 +104,6 @@ public ClusterManager(final ExecutorFactory executorFactory,
final Configuration configuration,
final NodeManager nodeManager, final boolean backup)
{
- if (nodeManager.getNodeId() == null)
- {
- throw HornetQMessageBundle.BUNDLE.nodeIdNull();
- }
-
this.executorFactory = executorFactory;
executor = executorFactory.getExecutor();;
@@ -35,10 +35,11 @@
/**
* @param directory
+ * @param replicatingBackup
*/
- public AIOFileLockNodeManager(final String directory)
+ public AIOFileLockNodeManager(final String directory, boolean replicatingBackup)
{
- super(directory);
+ super(directory, replicatingBackup);
}
@Override
Oops, something went wrong.

0 comments on commit 3222579

Please sign in to comment.