activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tab...@apache.org
Subject svn commit: r1065687 - in /activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src: main/csharp/ main/csharp/Transactions/ test/csharp/Transactions/
Date Mon, 31 Jan 2011 17:19:07 GMT
Author: tabish
Date: Mon Jan 31 17:19:07 2011
New Revision: 1065687

URL: http://svn.apache.org/viewvc?rev=1065687&view=rev
Log:
https://issues.apache.org/jira/browse/AMQNET-290

Add test for the Recovery File Logger class and improve the configuration options to add Path
config with the option to create the directory if it doesn't exist.  Clean up the interface
a bit.

Added:
    activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/test/csharp/Transactions/
    activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/test/csharp/Transactions/RecoveryFileLoggerTest.cs
  (with props)
Modified:
    activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/NetTxSession.cs
    activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/TransactionContext.cs
    activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Transactions/IRecoveryLogger.cs
    activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Transactions/RecoveryFileLogger.cs

Modified: activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/NetTxSession.cs
URL: http://svn.apache.org/viewvc/activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/NetTxSession.cs?rev=1065687&r1=1065686&r2=1065687&view=diff
==============================================================================
--- activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/NetTxSession.cs (original)
+++ activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/NetTxSession.cs Mon
Jan 31 17:19:07 2011
@@ -27,7 +27,7 @@ namespace Apache.NMS.ActiveMQ
         public NetTxSession(Connection connection, SessionId id)
             : base(connection, id, AcknowledgementMode.AutoAcknowledge)
         {
-            TransactionContext.CheckForAndRecoverFailedTransactions();
+            TransactionContext.InitializeDtcTxContext();
         }
 
         /// <summary>

Modified: activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/TransactionContext.cs
URL: http://svn.apache.org/viewvc/activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/TransactionContext.cs?rev=1065687&r1=1065686&r2=1065687&view=diff
==============================================================================
--- activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/TransactionContext.cs
(original)
+++ activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/TransactionContext.cs
Mon Jan 31 17:19:07 2011
@@ -511,10 +511,10 @@ namespace Apache.NMS.ActiveMQ
         /// still alive on the Broker it will be recovered, otherwise the stored 
         /// data should be cleared.
         /// </summary>
-        public void CheckForAndRecoverFailedTransactions()
+        public void InitializeDtcTxContext()
         {
             // initialize the logger with the current Resource Manager Id
-            RecoveryLogger.ResourceManagerId = ResourceManagerId;
+            RecoveryLogger.Initialize(ResourceManagerId);
 
             KeyValuePair<XATransactionId, byte[]>[] localRecoverables = RecoveryLogger.GetRecoverables();
             if (localRecoverables.Length == 0)

Modified: activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Transactions/IRecoveryLogger.cs
URL: http://svn.apache.org/viewvc/activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Transactions/IRecoveryLogger.cs?rev=1065687&r1=1065686&r2=1065687&view=diff
==============================================================================
--- activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Transactions/IRecoveryLogger.cs
(original)
+++ activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Transactions/IRecoveryLogger.cs
Mon Jan 31 17:19:07 2011
@@ -30,6 +30,8 @@ namespace Apache.NMS.ActiveMQ.Transactio
     /// </summary>
     public interface IRecoveryLogger
     {
+        void Initialize(string resourceManagerId);
+
         void LogRecoveryInfo(XATransactionId xid, byte[] recoveryInformation);
 
         KeyValuePair<XATransactionId, byte[]>[] GetRecoverables();
@@ -38,7 +40,7 @@ namespace Apache.NMS.ActiveMQ.Transactio
 
         string LoggerType{ get; }
 
-        string ResourceManagerId{ get; set; }
+        string ResourceManagerId{ get; }
 
         void Purge();
     }

Modified: activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Transactions/RecoveryFileLogger.cs
URL: http://svn.apache.org/viewvc/activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Transactions/RecoveryFileLogger.cs?rev=1065687&r1=1065686&r2=1065687&view=diff
==============================================================================
--- activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Transactions/RecoveryFileLogger.cs
(original)
+++ activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Transactions/RecoveryFileLogger.cs
Mon Jan 31 17:19:07 2011
@@ -22,6 +22,7 @@ using System.Collections.Generic;
 using System.Runtime.Serialization;
 using System.Runtime.Serialization.Formatters.Binary;
 
+using Apache.NMS.Util;
 using Apache.NMS.ActiveMQ.Commands;
 
 namespace Apache.NMS.ActiveMQ.Transactions
@@ -29,6 +30,7 @@ namespace Apache.NMS.ActiveMQ.Transactio
     public class RecoveryFileLogger : IRecoveryLogger
     {
         private string location;
+        private bool autoCreateLocation;
         private string resourceManagerId;
         private readonly object syncRoot = new object();
 
@@ -46,7 +48,6 @@ namespace Apache.NMS.ActiveMQ.Transactio
         public string ResourceManagerId
         {
             get { return this.resourceManagerId; }
-            set { this.resourceManagerId = value; }
         }
 
         /// <summary>
@@ -59,6 +60,49 @@ namespace Apache.NMS.ActiveMQ.Transactio
             set { this.location = value; }
         }
 
+        /// <summary>
+        /// Indiciate that the Logger should create and sibdirs of the
+        /// given location that don't currently exist.
+        /// </summary>
+        public bool AutoCreateLocation
+        {
+            get { return this.autoCreateLocation; }
+            set { this.autoCreateLocation = value; }
+        }
+
+        public void Initialize(string resourceManagerId)
+        {
+            this.resourceManagerId = resourceManagerId;
+
+            if(String.IsNullOrEmpty(location))
+            {
+                this.location = Directory.GetCurrentDirectory();
+            }
+            else
+            {
+                // Test if the location configured is valid.
+                if(!Directory.Exists(location))
+                {
+                    if(AutoCreateLocation)
+                    {
+                        try
+                        {
+                            Directory.CreateDirectory(Location);
+                        }
+                        catch(Exception ex)
+                        {
+                            Tracer.Error("Failed to create log directory: " + ex.Message);
+                            throw NMSExceptionSupport.Create(ex);
+                        }
+                    }
+                    else
+                    {
+                        throw new NMSException("Configured Recovery Log Location does not
exist: " + location);
+                    }
+                }
+            }
+        }
+
         public void LogRecoveryInfo(XATransactionId xid, byte[] recoveryInformation)
         {
             if (recoveryInformation == null || recoveryInformation.Length == 0)
@@ -144,7 +188,7 @@ namespace Apache.NMS.ActiveMQ.Transactio
 
         private string Filename
         {
-            get { return Location + ResourceManagerId + ".bin"; }
+            get { return Location + Path.DirectorySeparatorChar + ResourceManagerId + ".bin";
}
         }
 
         [Serializable]
@@ -190,30 +234,29 @@ namespace Apache.NMS.ActiveMQ.Transactio
 
         private RecoveryInformation TryOpenRecoveryInfoFile()
         {
-            string filename = Location + ResourceManagerId + ".bin";
             RecoveryInformation result = null;
 
-            Tracer.Debug("Checking for Recoverable Transactions filename: " + filename);
+            Tracer.Debug("Checking for Recoverable Transactions filename: " + Filename);
 
             lock (syncRoot)
             {
                 try
                 {
-                    if (!File.Exists(filename))
+                    if (!File.Exists(Filename))
                     {
                         return null;
                     }
 
-                    using(FileStream recoveryLog = new FileStream(filename, FileMode.Open,
FileAccess.Read))
+                    using(FileStream recoveryLog = new FileStream(Filename, FileMode.Open,
FileAccess.Read))
                     {
-                        Tracer.Debug("Found Recovery Log File: " + filename);
+                        Tracer.Debug("Found Recovery Log File: " + Filename);
                         IFormatter formatter = new BinaryFormatter();
                         result = formatter.Deserialize(recoveryLog) as RecoveryInformation;
                     }
                 }
                 catch(Exception ex)
                 {
-                    Tracer.InfoFormat("Error while opening Recovery file {0} error message:
{1}", filename, ex.Message);
+                    Tracer.InfoFormat("Error while opening Recovery file {0} error message:
{1}", Filename, ex.Message);
                     // Nothing to restore.
                     return null;
                 }

Added: activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/test/csharp/Transactions/RecoveryFileLoggerTest.cs
URL: http://svn.apache.org/viewvc/activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/test/csharp/Transactions/RecoveryFileLoggerTest.cs?rev=1065687&view=auto
==============================================================================
--- activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/test/csharp/Transactions/RecoveryFileLoggerTest.cs
(added)
+++ activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/test/csharp/Transactions/RecoveryFileLoggerTest.cs
Mon Jan 31 17:19:07 2011
@@ -0,0 +1,224 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+using System;
+using System.IO;
+using System.Collections.Generic;
+
+using NUnit.Framework;
+
+using Apache.NMS.ActiveMQ.Transactions;
+using Apache.NMS.ActiveMQ.Commands;
+
+namespace Apache.NMS.ActiveMQ.Transactions.Test
+{
+    [TestFixture]
+    public class RecoveryFileLoggerTest
+    {
+        private Guid rmId;
+        private string nonExistantPath;
+        private string autoCreatePath;
+        private string nonDefaultLogLocation;
+
+        [SetUp]
+        public void SetUp()
+        {
+            this.rmId = Guid.NewGuid();
+            this.nonExistantPath = Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar
+ Guid.NewGuid();
+            this.nonDefaultLogLocation = Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar
+ Guid.NewGuid();
+            this.autoCreatePath = Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar
+ Guid.NewGuid();
+
+            Directory.CreateDirectory(nonDefaultLogLocation);
+        }
+
+        [TearDown]
+        public void TearDown()
+        {
+            if(Directory.Exists(autoCreatePath))
+            {
+                Directory.Delete(autoCreatePath);
+            }
+
+            Directory.Delete(nonDefaultLogLocation, true);
+        }
+
+        [Test]
+        public void TestInitWithNoLocationSet()
+        {
+            RecoveryFileLogger logger = new RecoveryFileLogger();
+
+            logger.Initialize(rmId.ToString());
+
+            Assert.AreEqual(Directory.GetCurrentDirectory(), logger.Location);
+        }
+
+        [Test]
+        public void TestInitWithNonDefaultLocationSet()
+        {
+            RecoveryFileLogger logger = new RecoveryFileLogger();
+
+            logger.Location = nonDefaultLogLocation;
+            logger.Initialize(rmId.ToString());
+
+            Assert.AreEqual(nonDefaultLogLocation, logger.Location);
+        }
+
+        [Test]
+        public void TestInitWithAutoCreateLocation()
+        {
+            RecoveryFileLogger logger = new RecoveryFileLogger();
+
+            Assert.IsFalse(Directory.Exists(autoCreatePath));
+
+            logger.AutoCreateLocation = true;
+            logger.Location = autoCreatePath;
+            logger.Initialize(rmId.ToString());
+
+            Assert.IsTrue(Directory.Exists(autoCreatePath));
+            Assert.AreEqual(autoCreatePath, logger.Location);
+        }
+
+
+        [Test]
+        public void TestInitWithLocationSetToBadPath()
+        {
+            RecoveryFileLogger logger = new RecoveryFileLogger();
+
+            logger.Location = this.nonExistantPath;
+
+            try
+            {
+                logger.Initialize(rmId.ToString());
+                Assert.Fail("Should have detected an invalid dir and thrown an exception");
+            }
+            catch
+            {
+            }
+        }
+
+        [Test]
+        public void TestNothingToRecover()
+        {
+            RecoveryFileLogger logger = new RecoveryFileLogger();
+
+            logger.Location = nonDefaultLogLocation;
+            logger.Initialize(rmId.ToString());
+
+            Assert.IsTrue(logger.GetRecoverables().Length == 0);
+        }
+
+        [Test]
+        public void TestLogTransactionRecord()
+        {
+            RecoveryFileLogger logger = new RecoveryFileLogger();
+
+            byte[] globalId = new byte[32];
+            byte[] branchQ = new byte[32];
+            byte[] recoveryData = new byte[256];
+
+            Random gen = new Random();
+
+            gen.NextBytes(globalId);
+            gen.NextBytes(branchQ);
+            gen.NextBytes(recoveryData);
+
+            logger.Location = nonDefaultLogLocation;
+            logger.Initialize(rmId.ToString());
+
+            XATransactionId xid = new XATransactionId();
+            xid.GlobalTransactionId = globalId;
+            xid.BranchQualifier = branchQ;
+
+            logger.LogRecoveryInfo(xid, recoveryData);
+
+            Assert.IsTrue(File.Exists(logger.Location + Path.DirectorySeparatorChar + rmId.ToString()
+ ".bin"),
+                          "Recovery File was not created");
+        }
+
+        [Test]
+        public void TestRecoverLoggedRecord()
+        {
+            RecoveryFileLogger logger = new RecoveryFileLogger();
+
+            byte[] globalId = new byte[32];
+            byte[] branchQ = new byte[32];
+            byte[] recoveryData = new byte[256];
+
+            Random gen = new Random();
+
+            gen.NextBytes(globalId);
+            gen.NextBytes(branchQ);
+            gen.NextBytes(recoveryData);
+
+            logger.Location = nonDefaultLogLocation;
+            logger.Initialize(rmId.ToString());
+
+            XATransactionId xid = new XATransactionId();
+            xid.GlobalTransactionId = globalId;
+            xid.BranchQualifier = branchQ;
+
+            logger.LogRecoveryInfo(xid, recoveryData);
+
+            Assert.IsTrue(File.Exists(logger.Location + Path.DirectorySeparatorChar + rmId.ToString()
+ ".bin"),
+                          "Recovery File was not created");
+            Assert.IsTrue(logger.GetRecoverables().Length == 1,
+                          "Did not recover the logged record.");
+
+            KeyValuePair<XATransactionId, byte[]>[] records = logger.GetRecoverables();
+            Assert.AreEqual(1, records.Length);
+
+            Assert.AreEqual(globalId, records[0].Key.GlobalTransactionId, "Incorrect Global
TX Id returned");
+            Assert.AreEqual(branchQ, records[0].Key.BranchQualifier, "Incorrect Branch Qualifier
returned");
+            Assert.AreEqual(recoveryData, records[0].Value, "Incorrect Recovery Information
returned");
+        }
+
+        [Test]
+        public void TestPurgeTransactionRecord()
+        {
+            RecoveryFileLogger logger = new RecoveryFileLogger();
+
+            byte[] globalId = new byte[32];
+            byte[] branchQ = new byte[32];
+            byte[] recoveryData = new byte[256];
+
+            Random gen = new Random();
+
+            gen.NextBytes(globalId);
+            gen.NextBytes(branchQ);
+            gen.NextBytes(recoveryData);
+
+            logger.Location = nonDefaultLogLocation;
+            logger.Initialize(rmId.ToString());
+
+            XATransactionId xid = new XATransactionId();
+            xid.GlobalTransactionId = globalId;
+            xid.BranchQualifier = branchQ;
+
+            logger.LogRecoveryInfo(xid, recoveryData);
+
+            Assert.IsTrue(File.Exists(logger.Location + Path.DirectorySeparatorChar + rmId.ToString()
+ ".bin"),
+                          "Recovery File was not created");
+
+            logger.Purge();
+
+            Assert.IsFalse(File.Exists(logger.Location + Path.DirectorySeparatorChar + rmId.ToString()
+ ".bin"),
+                          "Recovery File was not created");
+        }
+
+    }
+}
+

Propchange: activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/test/csharp/Transactions/RecoveryFileLoggerTest.cs
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message