Typical use might look like:
new Lock.With(directory.makeLock("my.lock")) { public Object doBody() { ... code to execute while locked ... } }.run();
public CheckedLock(VerifyingLockFactory enclosingInstance, Lock lock_Renamed) { InitBlock(enclosingInstance); this.lock_Renamed = lock_Renamed; }
public MultiIndexLock(Lock dirMaster, Lock dirChild) { _dirMaster = dirMaster; _dirChild = dirChild; }
/// <summary>Constructs an executor that will grab the named lock. </summary> public With(Lock lock_Renamed, long lockWaitTimeout) { this.lock_Renamed = lock_Renamed; this.lockWaitTimeout = lockWaitTimeout; }
/// <summary>Constructs an executor that will grab the named lock. </summary> protected With(Lock lock_Renamed, long lockWaitTimeout) { this.lock_Renamed = lock_Renamed; this.lockWaitTimeout = lockWaitTimeout; }
/// <summary>Constructs an executor that will grab the named lock. /// Defaults lockWaitTimeout to Lock.COMMIT_LOCK_TIMEOUT. /// </summary> /// <deprecated> Kept only to avoid breaking existing code. /// </deprecated> public With(Lock lock_Renamed) : this(lock_Renamed, IndexWriter.COMMIT_LOCK_TIMEOUT) { }
public CheckedLock(VerifyingLockFactory outerInstance, Lock @lock) { this.OuterInstance = outerInstance; this.@lock = @lock; }
public virtual void TestDirectInstantiation() { DirectoryInfo path = CreateTempDir("testDirectInstantiation"); byte[] largeBuffer = new byte[Random.Next(256 * 1024)], largeReadBuffer = new byte[largeBuffer.Length]; for (int i = 0; i < largeBuffer.Length; i++) { largeBuffer[i] = (byte)i; // automatically loops with modulo } var dirs = new FSDirectory[] { new SimpleFSDirectory(path, null), new NIOFSDirectory(path, null), new MMapDirectory(path, null) }; for (int i = 0; i < dirs.Length; i++) { FSDirectory dir = dirs[i]; dir.EnsureOpen(); string fname = "foo." + i; string lockname = "foo" + i + ".lck"; IndexOutput @out = dir.CreateOutput(fname, NewIOContext(Random)); @out.WriteByte((byte)i); @out.WriteBytes(largeBuffer, largeBuffer.Length); @out.Dispose(); for (int j = 0; j < dirs.Length; j++) { FSDirectory d2 = dirs[j]; d2.EnsureOpen(); Assert.IsTrue(SlowFileExists(d2, fname)); Assert.AreEqual(1 + largeBuffer.Length, d2.FileLength(fname)); // LUCENENET specific - unmap hack not needed //// don't do read tests if unmapping is not supported! //if (d2 is MMapDirectory && !((MMapDirectory)d2).UseUnmap) //{ // continue; //} IndexInput input = d2.OpenInput(fname, NewIOContext(Random)); Assert.AreEqual((byte)i, input.ReadByte()); // read array with buffering enabled Arrays.Fill(largeReadBuffer, (byte)0); input.ReadBytes(largeReadBuffer, 0, largeReadBuffer.Length, true); Assert.AreEqual(largeBuffer, largeReadBuffer); // read again without using buffer input.Seek(1L); Arrays.Fill(largeReadBuffer, (byte)0); input.ReadBytes(largeReadBuffer, 0, largeReadBuffer.Length, false); Assert.AreEqual(largeBuffer, largeReadBuffer); input.Dispose(); } // delete with a different dir dirs[(i + 1) % dirs.Length].DeleteFile(fname); for (int j = 0; j < dirs.Length; j++) { FSDirectory d2 = dirs[j]; Assert.IsFalse(SlowFileExists(d2, fname)); } Lock @lock = dir.MakeLock(lockname); Assert.IsTrue(@lock.Obtain()); for (int j = 0; j < dirs.Length; j++) { FSDirectory d2 = dirs[j]; Lock lock2 = d2.MakeLock(lockname); try { Assert.IsFalse(lock2.Obtain(1)); } #pragma warning disable 168 catch (LockObtainFailedException e) #pragma warning restore 168 { // OK } } @lock.Dispose(); // now lock with different dir @lock = dirs[(i + 1) % dirs.Length].MakeLock(lockname); Assert.IsTrue(@lock.Obtain()); @lock.Dispose(); } for (int i = 0; i < dirs.Length; i++) { FSDirectory dir = dirs[i]; dir.EnsureOpen(); dir.Dispose(); Assert.IsFalse(dir.IsOpen); } }
public static void Main(string[] args) { if (args.Length != 7) { // LUCENENET specific - our wrapper console shows the correct usage throw new ArgumentException(); //Console.WriteLine("Usage: java Lucene.Net.Store.LockStressTest myID verifierHost verifierPort lockFactoryClassName lockDirName sleepTimeMS count\n" + // "\n" + // " myID = int from 0 .. 255 (should be unique for test process)\n" + // " verifierHost = hostname that LockVerifyServer is listening on\n" + // " verifierPort = port that LockVerifyServer is listening on\n" + // " lockFactoryClassName = primary LockFactory class that we will use\n" + // " lockDirName = path to the lock directory (only set for Simple/NativeFSLockFactory\n" + // " sleepTimeMS = milliseconds to pause betweeen each lock obtain/release\n" + // " count = number of locking tries\n" + // "\n" + // "You should run multiple instances of this process, each with its own\n" + // "unique ID, and each pointing to the same lock directory, to verify\n" + // "that locking is working correctly.\n" + // "\n" + // "Make sure you are first running LockVerifyServer."); //Environment.FailFast("1"); } int arg = 0; int myID = Convert.ToInt32(args[arg++], CultureInfo.InvariantCulture); if (myID < 0 || myID > 255) { throw new ArgumentException("ID must be a unique int 0..255"); //Console.WriteLine("myID must be a unique int 0..255"); //Environment.Exit(1); } string verifierHost = args[arg++]; int verifierPort = Convert.ToInt32(args[arg++], CultureInfo.InvariantCulture); string lockFactoryClassName = args[arg++]; string lockDirName = args[arg++]; int sleepTimeMS = Convert.ToInt32(args[arg++], CultureInfo.InvariantCulture); int count = Convert.ToInt32(args[arg++], CultureInfo.InvariantCulture); IPAddress[] addresses = Dns.GetHostAddressesAsync(verifierHost).Result; IPAddress addr = addresses.FirstOrDefault(); Type c; try { c = Type.GetType(lockFactoryClassName); if (c == null) { // LUCENENET: try again, this time with the Store namespace c = Type.GetType("Lucene.Net.Store." + lockFactoryClassName); } } catch (Exception) { throw new IOException("unable to find LockClass " + lockFactoryClassName); } LockFactory lockFactory; try { lockFactory = (LockFactory)Activator.CreateInstance(c); } catch (UnauthorizedAccessException e) { throw new IOException("Cannot instantiate lock factory " + lockFactoryClassName, e); } catch (InvalidCastException e) { throw new IOException("unable to cast LockClass " + lockFactoryClassName + " instance to a LockFactory", e); } catch (Exception e) { throw new IOException("InstantiationException when instantiating LockClass " + lockFactoryClassName, e); } DirectoryInfo lockDir = new DirectoryInfo(lockDirName); if (lockFactory is FSLockFactory) { ((FSLockFactory)lockFactory).SetLockDir(lockDir); } Console.WriteLine("Connecting to server " + addr + " and registering as client " + myID + "..."); using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) { socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1); socket.Connect(verifierHost, verifierPort); using (Stream stream = new NetworkStream(socket)) { BinaryReader intReader = new BinaryReader(stream); BinaryWriter intWriter = new BinaryWriter(stream); intWriter.Write(myID); stream.Flush(); lockFactory.LockPrefix = "test"; LockFactory verifyLF = new VerifyingLockFactory(lockFactory, stream); Lock l = verifyLF.MakeLock("test.lock"); Random rnd = new Random(); // wait for starting gun if (intReader.ReadInt32() != 43) { throw new IOException("Protocol violation"); } for (int i = 0; i < count; i++) { bool obtained = false; try { obtained = l.Obtain(rnd.Next(100) + 10); } #pragma warning disable 168 catch (LockObtainFailedException e) #pragma warning restore 168 { } if (obtained) { Thread.Sleep(sleepTimeMS); l.Dispose(); } if (i % 500 == 0) { Console.WriteLine((i * 100.0 / count) + "% done."); } Thread.Sleep(sleepTimeMS); } } } Console.WriteLine("Finished " + count + " tries."); }
public virtual void TestDirectInstantiation() { System.IO.FileInfo path = new System.IO.FileInfo(SupportClass.AppSettings.Get("tempDir", System.IO.Path.GetTempPath())); int sz = 2; Directory[] dirs = new Directory[sz]; dirs[0] = new SimpleFSDirectory(path, null); // dirs[1] = new NIOFSDirectory(path, null); System.Console.WriteLine("Skipping NIOFSDirectory() test under Lucene.Net"); dirs[1] = new MMapDirectory(path, null); for (int i = 0; i < sz; i++) { Directory dir = dirs[i]; dir.EnsureOpen(); System.String fname = "foo." + i; System.String lockname = "foo" + i + ".lck"; IndexOutput out_Renamed = dir.CreateOutput(fname); out_Renamed.WriteByte((byte)i); out_Renamed.Close(); for (int j = 0; j < sz; j++) { Directory d2 = dirs[j]; d2.EnsureOpen(); Assert.IsTrue(d2.FileExists(fname)); Assert.AreEqual(1, d2.FileLength(fname)); // don't test read on MMapDirectory, since it can't really be // closed and will cause a failure to delete the file. if (d2 is MMapDirectory) { continue; } IndexInput input = d2.OpenInput(fname); Assert.AreEqual((byte)i, input.ReadByte()); input.Close(); } // delete with a different dir dirs[(i + 1) % sz].DeleteFile(fname); for (int j = 0; j < sz; j++) { Directory d2 = dirs[j]; Assert.IsFalse(d2.FileExists(fname)); } Lock lock_Renamed = dir.MakeLock(lockname); Assert.IsTrue(lock_Renamed.Obtain()); for (int j = 0; j < sz; j++) { Directory d2 = dirs[j]; Lock lock2 = d2.MakeLock(lockname); try { Assert.IsFalse(lock2.Obtain(1)); } catch (LockObtainFailedException e) { // OK } } lock_Renamed.Release(); // now lock with different dir lock_Renamed = dirs[(i + 1) % sz].MakeLock(lockname); Assert.IsTrue(lock_Renamed.Obtain()); lock_Renamed.Release(); } for (int i = 0; i < sz; i++) { Directory dir = dirs[i]; dir.EnsureOpen(); dir.Close(); Assert.IsFalse(dir.isOpen_ForNUnit); } }