Implements {@link LockFactory} using native OS file locks. Note that because this LockFactory relies on java.nio.* APIs for locking, any problems with those APIs will cause locking to fail. Specifically, on certain NFS environments the java.nio.* locks will fail (the lock can incorrectly be double acquired) whereas {@link SimpleFSLockFactory} worked perfectly in those same environments. For NFS based access to an index, it's recommended that you try {@link SimpleFSLockFactory} first and work around the one limitation that a lock file could be left when the JVM exits abnormally.

The primary benefit of {@link NativeFSLockFactory} is that lock files will be properly removed (by the OS) if the JVM has an abnormal exit.

Note that, unlike {@link SimpleFSLockFactory}, the existence of leftover lock files in the filesystem on exiting the JVM is fine because the OS will free the locks held against these files even though the files still remain.

If you suspect that this or any other LockFactory is not working properly in your environment, you can easily test it by using {@link VerifyingLockFactory}, {@link LockVerifyServer} and {@link LockStressTest}.

Inheritance: Lucene.Net.Store.FSLockFactory
Example #1
0
        // permit subclassing

        /// <summary>Create a new FSDirectory for the named location (ctor for subclasses).</summary>
        /// <param name="path">the path of the directory
        /// </param>
        /// <param name="lockFactory">the lock factory to use, or null for the default
        /// ({@link NativeFSLockFactory});
        /// </param>
        /// <throws>  IOException </throws>
        protected internal FSDirectory(System.IO.DirectoryInfo path, LockFactory lockFactory)
        {
            // new ctors use always NativeFSLockFactory as default:
            if (lockFactory == null)
            {
                lockFactory = new NativeFSLockFactory();
            }
            Init(path, lockFactory);
            refCount = 1;
        }
Example #2
0
        /// <summary>
        /// Create a new <see cref="FSDirectory"/> for the named location (ctor for subclasses). </summary>
        /// <param name="path"> the path of the directory </param>
        /// <param name="lockFactory"> the lock factory to use, or null for the default
        /// (<seealso cref="NativeFSLockFactory"/>); </param>
        /// <exception cref="IOException"> if there is a low-level I/O error </exception>
        protected internal FSDirectory(DirectoryInfo path, LockFactory lockFactory)
        {
            // new ctors use always NativeFSLockFactory as default:
            if (lockFactory == null)
            {
                lockFactory = new NativeFSLockFactory();
            }
            m_directory = new DirectoryInfo(path.GetCanonicalPath());

            if (File.Exists(path.FullName))
            {
                throw new DirectoryNotFoundException("file '" + path.FullName + "' exists but is not a directory");
            }

            SetLockFactory(lockFactory);
        }
Example #3
0
        /// <summary>
        /// Create a new <see cref="FSDirectory"/> for the named location (ctor for subclasses). </summary>
        /// <param name="path"> the path of the directory </param>
        /// <param name="lockFactory"> the lock factory to use, or null for the default
        /// (<seealso cref="NativeFSLockFactory"/>); </param>
        /// <exception cref="IOException"> if there is a low-level I/O error </exception>
        protected internal FSDirectory(DirectoryInfo path, LockFactory lockFactory)
        {
            // new ctors use always NativeFSLockFactory as default:
            if (lockFactory == null)
            {
                lockFactory = new NativeFSLockFactory();
            }
            m_directory = path; // Lucene.NET doesn't need to call GetCanonicalPath since we already have DirectoryInfo handy

            if (File.Exists(path.FullName))
            {
                throw new DirectoryNotFoundException("file '" + path.FullName + "' exists but is not a directory");
            }

            SetLockFactory(lockFactory);
        }
Example #4
0
        /// <summary>
        /// Create a new FSDirectory for the named location (ctor for subclasses). </summary>
        /// <param name="path"> the path of the directory </param>
        /// <param name="lockFactory"> the lock factory to use, or null for the default
        /// (<seealso cref="NativeFSLockFactory"/>); </param>
        /// <exception cref="System.IO.IOException"> if there is a low-level I/O error </exception>
        protected internal FSDirectory(DirectoryInfo path, LockFactory lockFactory)
        {
            // new ctors use always NativeFSLockFactory as default:
            if (lockFactory == null)
            {
                lockFactory = new NativeFSLockFactory();
            }
            Directory_Renamed = GetCanonicalPath(path);

            if (File.Exists(path.FullName))
            {
                throw new NoSuchDirectoryException("file '" + path.FullName + "' exists but is not a directory"); //should be NoSuchDirectoryException
            }

            LockFactory = lockFactory;
        }
Example #5
0
        /// <summary>Create a new FSDirectory for the named location (ctor for subclasses).</summary>
        /// <param name="path">the path of the directory
        /// </param>
        /// <param name="lockFactory">the lock factory to use, or null for the default
        /// (<see cref="NativeFSLockFactory" />);
        /// </param>
        /// <throws>  IOException </throws>
        protected internal FSDirectory(System.IO.DirectoryInfo path, LockFactory lockFactory)
        {
            // new ctors use always NativeFSLockFactory as default:
            if (lockFactory == null)
            {
                lockFactory = new NativeFSLockFactory();
            }
            // Set up lockFactory with cascaded defaults: if an instance was passed in,
            // use that; else if locks are disabled, use NoLockFactory; else if the
            // system property Lucene.Net.Store.FSDirectoryLockFactoryClass is set,
            // instantiate that; else, use SimpleFSLockFactory:

            internalDirectory = path;

            // due to differences in how Java & .NET refer to files, the checks are a bit different
            if (!internalDirectory.Exists && System.IO.File.Exists(internalDirectory.FullName))
            {
                throw new NoSuchDirectoryException("file '" + internalDirectory.FullName + "' exists but is not a directory");
            }
            SetLockFactory(lockFactory);

            // for filesystem based LockFactory, delete the lockPrefix, if the locks are placed
            // in index dir. If no index dir is given, set ourselves
            if (lockFactory is FSLockFactory)
            {
                FSLockFactory           lf  = (FSLockFactory)lockFactory;
                System.IO.DirectoryInfo dir = lf.LockDir;
                // if the lock factory has no lockDir set, use the this directory as lockDir
                if (dir == null)
                {
                    lf.LockDir    = this.internalDirectory;
                    lf.LockPrefix = null;
                }
                else if (dir.FullName.Equals(this.internalDirectory.FullName))
                {
                    lf.LockPrefix = null;
                }
            }
        }
Example #6
0
        public virtual void TestNativeFSLockFactory()
        {
            var f = new NativeFSLockFactory(CreateTempDir("testNativeFsLockFactory"));

            f.LockPrefix = "test";
            var l  = f.MakeLock("commit");
            var l2 = f.MakeLock("commit");

            Assert.IsTrue(l.Obtain(), "failed to obtain lock, got exception: {0}", l.FailureReason);
            Assert.IsTrue(!l2.Obtain(), "succeeded in obtaining lock twice");
            l.Dispose();

            Assert.IsTrue(l2.Obtain(), "failed to obtain 2nd lock after first one was freed, got exception: {0}", l2.FailureReason);
            l2.Dispose();

            // Make sure we can obtain first one again, test isLocked():
            Assert.IsTrue(l.Obtain(), "failed to obtain lock, got exception: {0}", l.FailureReason);
            Assert.IsTrue(l.Locked);
            Assert.IsTrue(l2.Locked);
            l.Dispose();
            Assert.IsFalse(l.Locked);
            Assert.IsFalse(l2.Locked);
        }
Example #7
0
        public virtual void  TestNativeFSLockFactory()
        {
            NativeFSLockFactory f = new NativeFSLockFactory(SupportClass.AppSettings.Get("tempDir", System.IO.Path.GetTempPath()));

            f.SetLockPrefix("test");
            Lock l  = f.MakeLock("commit");
            Lock l2 = f.MakeLock("commit");

            Assert.IsTrue(l.Obtain(), "failed to obtain lock");
            Assert.IsTrue(!l2.Obtain(), "succeeded in obtaining lock twice");
            l.Release();

            Assert.IsTrue(l2.Obtain(), "failed to obtain 2nd lock after first one was freed");
            l2.Release();

            // Make sure we can obtain first one again, test isLocked():
            Assert.IsTrue(l.Obtain(), "failed to obtain lock");
            Assert.IsTrue(l.IsLocked());
            Assert.IsTrue(l2.IsLocked());
            l.Release();
            Assert.IsFalse(l.IsLocked());
            Assert.IsFalse(l2.IsLocked());
        }
Example #8
0
        public virtual void TestNativeFSLockFactory()
        {
            //NativeFSLockFactory f = new NativeFSLockFactory(CreateTempDir(LuceneTestCase.TestClass.SimpleName));
            NativeFSLockFactory f = new NativeFSLockFactory(AppSettings.Get("tempDir", Path.GetTempPath()));

            f.LockPrefix = "test";
            Lock l  = f.MakeLock("commit");
            Lock l2 = f.MakeLock("commit");

            Assert.IsTrue(l.Obtain(), "failed to obtain lock");
            Assert.IsTrue(!l2.Obtain(), "succeeded in obtaining lock twice");
            l.Dispose();

            Assert.IsTrue(l2.Obtain(), "failed to obtain 2nd lock after first one was freed");
            l2.Dispose();

            // Make sure we can obtain first one again, test isLocked():
            Assert.IsTrue(l.Obtain(), "failed to obtain lock");
            Assert.IsTrue(l.Locked);
            Assert.IsTrue(l2.Locked);
            l.Dispose();
            Assert.IsFalse(l.Locked);
            Assert.IsFalse(l2.Locked);
        }
Example #9
0
 public NativeFSLock(NativeFSLockFactory outerInstance, DirectoryInfo lockDir, string lockFileName)
 {
     this.outerInstance = outerInstance;
     this.lockDir       = lockDir;
     path = new DirectoryInfo(System.IO.Path.Combine(lockDir.FullName, lockFileName));
 }
Example #10
0
        public virtual void TestNativeFSLockFactory()
        {
            var f = new NativeFSLockFactory(CreateTempDir("testNativeFsLockFactory"));

            f.LockPrefix = "test";
            var l = f.MakeLock("commit");
            var l2 = f.MakeLock("commit");

            Assert.IsTrue(l.Obtain(), "failed to obtain lock, got exception: {0}", l.FailureReason);
            Assert.IsTrue(!l2.Obtain(), "succeeded in obtaining lock twice");
            l.Dispose();

            Assert.IsTrue(l2.Obtain(), "failed to obtain 2nd lock after first one was freed, got exception: {0}", l2.FailureReason);
            l2.Dispose();

            // Make sure we can obtain first one again, test isLocked():
            Assert.IsTrue(l.Obtain(), "failed to obtain lock, got exception: {0}", l.FailureReason);
            Assert.IsTrue(l.Locked);
            Assert.IsTrue(l2.Locked);
            l.Dispose();
            Assert.IsFalse(l.Locked);
            Assert.IsFalse(l2.Locked);
        }
Example #11
0
        /// <summary>
        /// Create a new FSDirectory for the named location (ctor for subclasses). </summary>
        /// <param name="path"> the path of the directory </param>
        /// <param name="lockFactory"> the lock factory to use, or null for the default
        /// (<seealso cref="NativeFSLockFactory"/>); </param>
        /// <exception cref="System.IO.IOException"> if there is a low-level I/O error </exception>
        protected internal FSDirectory(DirectoryInfo path, LockFactory lockFactory)
        {
            // new ctors use always NativeFSLockFactory as default:
            if (lockFactory == null)
            {
                lockFactory = new NativeFSLockFactory();
            }
            directory = path; // Lucene.NET doesn't need to call GetCanonicalPath since we already have DirectoryInfo handy

            if (File.Exists(path.FullName))
            {
                throw new NoSuchDirectoryException("file '" + path.FullName + "' exists but is not a directory"); //should be NoSuchDirectoryException
            }

            LockFactory = lockFactory;
        }
Example #12
0
 // permit subclassing
 /// <summary>Create a new FSDirectory for the named location (ctor for subclasses).</summary>
 /// <param name="path">the path of the directory
 /// </param>
 /// <param name="lockFactory">the lock factory to use, or null for the default
 /// ({@link NativeFSLockFactory});
 /// </param>
 /// <throws>  IOException </throws>
 protected internal FSDirectory(System.IO.DirectoryInfo path, LockFactory lockFactory)
 {
     // new ctors use always NativeFSLockFactory as default:
     if (lockFactory == null)
     {
         lockFactory = new NativeFSLockFactory();
     }
     Init(path, lockFactory);
     refCount = 1;
 }
Example #13
0
        /// <summary>Create a new FSDirectory for the named location (ctor for subclasses).</summary>
        /// <param name="path">the path of the directory
        /// </param>
        /// <param name="lockFactory">the lock factory to use, or null for the default
        /// (<see cref="NativeFSLockFactory" />);
        /// </param>
        /// <throws>  IOException </throws>
        protected internal FSDirectory(System.IO.DirectoryInfo path, LockFactory lockFactory)
        {
            // new ctors use always NativeFSLockFactory as default:
            if (lockFactory == null)
            {
                lockFactory = new NativeFSLockFactory();
            }
            // Set up lockFactory with cascaded defaults: if an instance was passed in,
            // use that; else if locks are disabled, use NoLockFactory; else if the
            // system property Lucene.Net.Store.FSDirectoryLockFactoryClass is set,
            // instantiate that; else, use SimpleFSLockFactory:

            internalDirectory = path;

            // due to differences in how Java & .NET refer to files, the checks are a bit different
            if (!internalDirectory.Exists && System.IO.File.Exists(internalDirectory.FullName))
            {
                throw new NoSuchDirectoryException("file '" + internalDirectory.FullName + "' exists but is not a directory");
            }
            SetLockFactory(lockFactory);
            
            // for filesystem based LockFactory, delete the lockPrefix, if the locks are placed
            // in index dir. If no index dir is given, set ourselves
            if (lockFactory is FSLockFactory)
            {
                FSLockFactory lf = (FSLockFactory)lockFactory;
                System.IO.DirectoryInfo dir = lf.LockDir;
                // if the lock factory has no lockDir set, use the this directory as lockDir
                if (dir == null)
                {
                    lf.LockDir = this.internalDirectory;
                    lf.LockPrefix = null;
                }
                else if (dir.FullName.Equals(this.internalDirectory.FullName))
                {
                    lf.LockPrefix = null;
                }
            }
        }
Example #14
0
 public SharingAwareNativeFSLock(NativeFSLockFactory outerInstance, DirectoryInfo lockDir, string path)
 {
     this.outerInstance = outerInstance;
     this.lockDir       = lockDir;
     this.path          = path;
 }
Example #15
0
 // permit subclassing
 /// <summary>Create a new FSDirectory for the named location (ctor for subclasses).</summary>
 /// <param name="path">the path of the directory
 /// </param>
 /// <param name="lockFactory">the lock factory to use, or null for the default
 /// ({@link NativeFSLockFactory});
 /// </param>
 /// <throws>  IOException </throws>
 protected internal FSDirectory(System.IO.FileInfo path, LockFactory lockFactory)
 {
     path = GetCanonicalPath(path);
     // new ctors use always NativeFSLockFactory as default:
     if (lockFactory == null)
     {
         lockFactory = new NativeFSLockFactory();
     }
     Init(path, lockFactory);
     refCount = 1;
 }
Example #16
0
        public virtual void TestNativeFSLockFactory()
        {
            //NativeFSLockFactory f = new NativeFSLockFactory(CreateTempDir(LuceneTestCase.TestClass.SimpleName));
            NativeFSLockFactory f = new NativeFSLockFactory(AppSettings.Get("tempDir", Path.GetTempPath()));

            f.LockPrefix = "test";
            Lock l = f.MakeLock("commit");
            Lock l2 = f.MakeLock("commit");

            Assert.IsTrue(l.Obtain(), "failed to obtain lock");
            Assert.IsTrue(!l2.Obtain(), "succeeded in obtaining lock twice");
            l.Dispose();

            Assert.IsTrue(l2.Obtain(), "failed to obtain 2nd lock after first one was freed");
            l2.Dispose();

            // Make sure we can obtain first one again, test isLocked():
            Assert.IsTrue(l.Obtain(), "failed to obtain lock");
            Assert.IsTrue(l.Locked);
            Assert.IsTrue(l2.Locked);
            l.Dispose();
            Assert.IsFalse(l.Locked);
            Assert.IsFalse(l2.Locked);
        }
Example #17
0
 public WindowsNativeFSLock(NativeFSLockFactory outerInstance, DirectoryInfo lockDir, string path)
     : base(outerInstance, lockDir, path)
 {
 }
Example #18
0
		public virtual void  TestNativeFSLockFactory()
		{
			
			NativeFSLockFactory f = new NativeFSLockFactory(SupportClass.AppSettings.Get("tempDir", System.IO.Path.GetTempPath()));
			
			f.SetLockPrefix("test");
			Lock l = f.MakeLock("commit");
			Lock l2 = f.MakeLock("commit");
			
			Assert.IsTrue(l.Obtain(), "failed to obtain lock");
			Assert.IsTrue(!l2.Obtain(), "succeeded in obtaining lock twice");
			l.Release();
			
			Assert.IsTrue(l2.Obtain(), "failed to obtain 2nd lock after first one was freed");
			l2.Release();
			
			// Make sure we can obtain first one again, test isLocked():
			Assert.IsTrue(l.Obtain(), "failed to obtain lock");
			Assert.IsTrue(l.IsLocked());
			Assert.IsTrue(l2.IsLocked());
			l.Release();
			Assert.IsFalse(l.IsLocked());
			Assert.IsFalse(l2.IsLocked());
		}
Example #19
0
 public NativeFSLock(NativeFSLockFactory creatingInstance, DirectoryInfo lockDir, string lockFileName)
 {
     _creatingInstance = creatingInstance;
     this.LockDir      = lockDir;
     Path = new DirectoryInfo(System.IO.Path.Combine(lockDir.FullName, lockFileName));
 }
 public NativeFSLock(NativeFSLockFactory creatingInstance, DirectoryInfo lockDir, string lockFileName)
 {
     _creatingInstance = creatingInstance;
     this.LockDir = lockDir;
     Path = new DirectoryInfo(System.IO.Path.Combine(lockDir.FullName, lockFileName));
 }
Example #21
0
        /// <summary>
        /// Create a new FSDirectory for the named location (ctor for subclasses). </summary>
        /// <param name="path"> the path of the directory </param>
        /// <param name="lockFactory"> the lock factory to use, or null for the default
        /// (<seealso cref="NativeFSLockFactory"/>); </param>
        /// <exception cref="System.IO.IOException"> if there is a low-level I/O error </exception>
        protected internal FSDirectory(DirectoryInfo path, LockFactory lockFactory)
        {
            // new ctors use always NativeFSLockFactory as default:
            if (lockFactory == null)
            {
                lockFactory = new NativeFSLockFactory();
            }
            directory = GetCanonicalPath(path);

            if (File.Exists(path.FullName))
            {
                throw new NoSuchDirectoryException("file '" + path.FullName + "' exists but is not a directory"); //should be NoSuchDirectoryException
            }

            LockFactory = lockFactory;
        }