Lucene.Net  3.0.3
Lucene.Net is a port of the Lucene search engine library, written in C# and targeted at .NET runtime users.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
LockStressTest.cs
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 using System;
19 
20 namespace Lucene.Net.Store
21 {
22 
23  /// <summary> Simple standalone tool that forever acquires &amp; releases a
24  /// lock using a specific LockFactory. Run without any args
25  /// to see usage.
26  ///
27  /// </summary>
28  /// <seealso cref="VerifyingLockFactory">
29  /// </seealso>
30  /// <seealso cref="LockVerifyServer">
31  /// </seealso>
32 
33  public class LockStressTest
34  {
35 
36  [STAThread]
37  public static void Main(System.String[] args)
38  {
39 
40  if (args.Length != 6)
41  {
42  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");
43  System.Environment.Exit(1);
44  }
45 
46  int myID = System.Int32.Parse(args[0]);
47 
48  if (myID < 0 || myID > 255)
49  {
50  System.Console.Out.WriteLine("myID must be a unique int 0..255");
51  System.Environment.Exit(1);
52  }
53 
54  System.String verifierHost = args[1];
55  int verifierPort = System.Int32.Parse(args[2]);
56  System.String lockFactoryClassName = args[3];
57  System.String lockDirName = args[4];
58  int sleepTimeMS = System.Int32.Parse(args[5]);
59 
60  System.Type c;
61  try
62  {
63  c = System.Type.GetType(lockFactoryClassName);
64  }
65  catch (System.Exception)
66  {
67  throw new System.IO.IOException("unable to find LockClass " + lockFactoryClassName);
68  }
69 
70  LockFactory lockFactory;
71  try
72  {
73  lockFactory = (LockFactory) System.Activator.CreateInstance(c);
74  }
75  catch (System.UnauthorizedAccessException)
76  {
77  throw new System.IO.IOException("IllegalAccessException when instantiating LockClass " + lockFactoryClassName);
78  }
79  catch (System.InvalidCastException)
80  {
81  throw new System.IO.IOException("unable to cast LockClass " + lockFactoryClassName + " instance to a LockFactory");
82  }
83  catch (System.Exception)
84  {
85  throw new System.IO.IOException("InstantiationException when instantiating LockClass " + lockFactoryClassName);
86  }
87 
88  System.IO.DirectoryInfo lockDir = new System.IO.DirectoryInfo(lockDirName);
89 
90  if (lockFactory is NativeFSLockFactory)
91  {
92  ((NativeFSLockFactory) lockFactory).LockDir = lockDir;
93  }
94  else if (lockFactory is SimpleFSLockFactory)
95  {
96  ((SimpleFSLockFactory) lockFactory).LockDir = lockDir;
97  }
98 
99  lockFactory.LockPrefix = "test";
100 
101  LockFactory verifyLF = new VerifyingLockFactory((sbyte) myID, lockFactory, verifierHost, verifierPort);
102 
103  Lock l = verifyLF.MakeLock("test.lock");
104 
105  while (true)
106  {
107 
108  bool obtained = false;
109 
110  try
111  {
112  obtained = l.Obtain(10);
113  }
115  {
116  System.Console.Out.Write("x");
117  }
118 
119  if (obtained)
120  {
121  System.Console.Out.Write("l");
122  l.Release();
123  }
124  System.Threading.Thread.Sleep(new System.TimeSpan((System.Int64) 10000 * sleepTimeMS));
125  }
126  }
127  }
128 }