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 ArgumentOutOfRangeException("ID", "ID must be a unique int 0..255"); // LUCENENET specific - changed from IllegalArgumentException to ArgumentOutOfRangeException (.NET convention) //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.Length > 0 ? addresses[0] : null; Type c; try { c = Type.GetType(lockFactoryClassName); if (c is null) { // LUCENENET: try again, this time with the Store namespace c = Type.GetType("Lucene.Net.Store." + lockFactoryClassName); } } catch (Exception e) { throw new IOException("unable to find LockClass " + lockFactoryClassName, e); } LockFactory lockFactory; try { lockFactory = (LockFactory)Activator.CreateInstance(c); } catch (Exception e) when(e.IsIllegalAccessException() || e.IsInstantiationException() || e.IsClassNotFoundException()) { throw new IOException("Cannot instantiate lock factory " + lockFactoryClassName, e); } // LUCENENET specific - added more explicit exception message in this case catch (Exception e) when(e.IsClassCastException()) { throw new IOException("unable to cast LockClass " + lockFactoryClassName + " instance to a LockFactory", e); } DirectoryInfo lockDir = new DirectoryInfo(lockDirName); if (lockFactory is FSLockFactory fsLockFactory) { fsLockFactory.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 static void MainTMP(System.String[] args) { if (args.Length != 6) { System.Console.Out.WriteLine("\nUsage: java Lucene.Net.Store.LockStressTest myID verifierHostOrIP verifierPort lockFactoryClassName lockDirName sleepTime\n" + "\n" + " myID = int from 0 .. 255 (should be unique for test process)\n" + " verifierHostOrIP = host name or IP address where LockVerifyServer is running\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" + "\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.\n" + "\n"); System.Environment.Exit(1); } int myID = System.Int32.Parse(args[0]); if (myID < 0 || myID > 255) { System.Console.Out.WriteLine("myID must be a unique int 0..255"); System.Environment.Exit(1); } System.String verifierHost = args[1]; int verifierPort = System.Int32.Parse(args[2]); System.String lockFactoryClassName = args[3]; System.String lockDirName = args[4]; int sleepTimeMS = System.Int32.Parse(args[5]); System.Type c; try { c = System.Type.GetType(lockFactoryClassName); } catch (System.Exception) { throw new System.IO.IOException("unable to find LockClass " + lockFactoryClassName); } LockFactory lockFactory; try { lockFactory = (LockFactory)System.Activator.CreateInstance(c); } catch (System.UnauthorizedAccessException) { throw new System.IO.IOException("IllegalAccessException when instantiating LockClass " + lockFactoryClassName); } catch (System.InvalidCastException) { throw new System.IO.IOException("unable to cast LockClass " + lockFactoryClassName + " instance to a LockFactory"); } catch (System.Exception) { throw new System.IO.IOException("InstantiationException when instantiating LockClass " + lockFactoryClassName); } System.IO.DirectoryInfo lockDir = new System.IO.DirectoryInfo(lockDirName); if (lockFactory is NativeFSLockFactory) { ((NativeFSLockFactory)lockFactory).LockDir = lockDir; } else if (lockFactory is SimpleFSLockFactory) { ((SimpleFSLockFactory)lockFactory).LockDir = lockDir; } lockFactory.LockPrefix = "test"; LockFactory verifyLF = new VerifyingLockFactory((sbyte)myID, lockFactory, verifierHost, verifierPort); Lock l = verifyLF.MakeLock("test.lock"); while (true) { bool obtained = false; try { obtained = l.Obtain(10); } catch (LockObtainFailedException) { System.Console.Out.Write("x"); } if (obtained) { System.Console.Out.Write("l"); l.Release(); } System.Threading.Thread.Sleep(new System.TimeSpan((System.Int64) 10000 * sleepTimeMS)); } }
public static void Main(string[] args) { if (args.Length != 7) { 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.Exit(1); } int arg = 0; int myID = Convert.ToInt32(args[arg++]); if (myID < 0 || myID > 255) { Console.WriteLine("myID must be a unique int 0..255"); Environment.Exit(1); } IPHostEntry verifierHost = Dns.GetHostEntry(args[arg++]); int verifierPort = Convert.ToInt32(args[arg++]); IPAddress verifierIp = verifierHost.AddressList[0]; IPEndPoint addr = new IPEndPoint(verifierIp, verifierPort); string lockFactoryClassName = args[arg++]; string lockDirName = args[arg++]; int sleepTimeMS = Convert.ToInt32(args[arg++]); int count = Convert.ToInt32(args[arg++]); Type c; try { c = Type.GetType(lockFactoryClassName); } catch (Exception) { throw new IOException("unable to find LockClass " + lockFactoryClassName); } LockFactory lockFactory; try { lockFactory = (LockFactory)Activator.CreateInstance(c); } catch (UnauthorizedAccessException) { throw new System.IO.IOException("Cannot instantiate lock factory " + lockFactoryClassName); } catch (InvalidCastException) { throw new System.IO.IOException("unable to cast LockClass " + lockFactoryClassName + " instance to a LockFactory"); } catch (Exception) { throw new System.IO.IOException("InstantiationException when instantiating LockClass " + lockFactoryClassName); } DirectoryInfo lockDir = new DirectoryInfo(lockDirName); if (lockFactory is FSLockFactory) { ((FSLockFactory)lockFactory).LockDir = lockDir; } Console.WriteLine("Connecting to server " + addr + " and registering as client " + myID + "..."); using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) { using (Stream @out = new NetworkStream(socket, FileAccess.ReadWrite), @in = new NetworkStream(socket, FileAccess.Read)) { socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1); socket.Connect(verifierIp, 500); BinaryReader intReader = new BinaryReader(@in); BinaryWriter intWriter = new BinaryWriter(@out); intWriter.Write(myID); @out.Flush(); lockFactory.LockPrefix = "test"; LockFactory verifyLF = new VerifyingLockFactory(lockFactory, @in, @out); Lock l = verifyLF.MakeLock("test.lock"); Random rnd = new Random(); // wait for starting gun if (intReader.ReadInt32() != 43) { throw new System.IO.IOException("Protocol violation"); } for (int i = 0; i < count; i++) { bool obtained = false; try { obtained = l.Obtain(rnd.Next(100) + 10); } catch (LockObtainFailedException e) { } if (obtained) { Thread.Sleep(sleepTimeMS); l.Release(); } if (i % 500 == 0) { Console.WriteLine((i * 100.0 / count) + "% done."); } Thread.Sleep(sleepTimeMS); } } } Console.WriteLine("Finished " + count + " tries."); }
public static void Main(System.String[] args) { if (args.Length != 6) { System.Console.Out.WriteLine("\nUsage: java Lucene.Net.Store.LockStressTest myID verifierHostOrIP verifierPort lockFactoryClassName lockDirName sleepTime\n" + "\n" + " myID = int from 0 .. 255 (should be unique for test process)\n" + " verifierHostOrIP = host name or IP address where LockVerifyServer is running\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" + "\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.\n" + "\n"); System.Environment.Exit(1); } int myID = System.Int32.Parse(args[0]); if (myID < 0 || myID > 255) { System.Console.Out.WriteLine("myID must be a unique int 0..255"); System.Environment.Exit(1); } System.String verifierHost = args[1]; int verifierPort = System.Int32.Parse(args[2]); System.String lockFactoryClassName = args[3]; System.String lockDirName = args[4]; int sleepTimeMS = System.Int32.Parse(args[5]); System.Type c; try { c = System.Type.GetType(lockFactoryClassName); } catch (System.Exception) { throw new System.IO.IOException("unable to find LockClass " + lockFactoryClassName); } LockFactory lockFactory; try { lockFactory = (LockFactory) System.Activator.CreateInstance(c); } catch (System.UnauthorizedAccessException) { throw new System.IO.IOException("IllegalAccessException when instantiating LockClass " + lockFactoryClassName); } catch (System.InvalidCastException) { throw new System.IO.IOException("unable to cast LockClass " + lockFactoryClassName + " instance to a LockFactory"); } catch (System.Exception) { throw new System.IO.IOException("InstantiationException when instantiating LockClass " + lockFactoryClassName); } System.IO.DirectoryInfo lockDir = new System.IO.DirectoryInfo(lockDirName); if (lockFactory is NativeFSLockFactory) { ((NativeFSLockFactory) lockFactory).LockDir = lockDir; } else if (lockFactory is SimpleFSLockFactory) { ((SimpleFSLockFactory) lockFactory).LockDir = lockDir; } lockFactory.LockPrefix = "test"; LockFactory verifyLF = new VerifyingLockFactory((sbyte) myID, lockFactory, verifierHost, verifierPort); Lock l = verifyLF.MakeLock("test.lock"); while (true) { bool obtained = false; try { obtained = l.Obtain(10); } catch (LockObtainFailedException) { System.Console.Out.Write("x"); } if (obtained) { System.Console.Out.Write("l"); l.Release(); } System.Threading.Thread.Sleep(new System.TimeSpan((System.Int64) 10000 * sleepTimeMS)); } }