pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / java / org / apache / lucene / store / LockStressTest.java
1 package org.apache.lucene.store;
2
3 /**
4  * Licensed to the Apache Software Foundation (ASF) under one or more
5  * contributor license agreements.  See the NOTICE file distributed with
6  * this work for additional information regarding copyright ownership.
7  * The ASF licenses this file to You under the Apache License, Version 2.0
8  * (the "License"); you may not use this file except in compliance with
9  * the License.  You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 import java.io.IOException;
21 import java.io.File;
22
23 /**
24  * Simple standalone tool that forever acquires & releases a
25  * lock using a specific LockFactory.  Run without any args
26  * to see usage.
27  *
28  * @see VerifyingLockFactory
29  * @see LockVerifyServer
30  */ 
31
32 public class LockStressTest {
33
34   public static void main(String[] args) throws Exception {
35
36     if (args.length != 6) {
37       System.out.println("\nUsage: java org.apache.lucene.store.LockStressTest myID verifierHostOrIP verifierPort lockFactoryClassName lockDirName sleepTime\n" +
38                          "\n" +
39                          "  myID = int from 0 .. 255 (should be unique for test process)\n" +
40                          "  verifierHostOrIP = host name or IP address where LockVerifyServer is running\n" +
41                          "  verifierPort = port that LockVerifyServer is listening on\n" +
42                          "  lockFactoryClassName = primary LockFactory class that we will use\n" +
43                          "  lockDirName = path to the lock directory (only set for Simple/NativeFSLockFactory\n" +
44                          "  sleepTimeMS = milliseconds to pause betweeen each lock obtain/release\n" +
45                          "\n" +
46                          "You should run multiple instances of this process, each with its own\n" +
47                          "unique ID, and each pointing to the same lock directory, to verify\n" +
48                          "that locking is working correctly.\n" +
49                          "\n" +
50                          "Make sure you are first running LockVerifyServer.\n" + 
51                          "\n");
52       System.exit(1);
53     }
54
55     final int myID = Integer.parseInt(args[0]);
56
57     if (myID < 0 || myID > 255) {
58       System.out.println("myID must be a unique int 0..255");
59       System.exit(1);
60     }
61
62     final String verifierHost = args[1];
63     final int verifierPort = Integer.parseInt(args[2]);
64     final String lockFactoryClassName = args[3];
65     final String lockDirName = args[4];
66     final int sleepTimeMS = Integer.parseInt(args[5]);
67
68     LockFactory lockFactory;
69     try {
70       lockFactory = Class.forName(lockFactoryClassName).asSubclass(LockFactory.class).newInstance();          
71     } catch (IllegalAccessException e) {
72       throw new IOException("IllegalAccessException when instantiating LockClass " + lockFactoryClassName);
73     } catch (InstantiationException e) {
74       throw new IOException("InstantiationException when instantiating LockClass " + lockFactoryClassName);
75     } catch (ClassCastException e) {
76       throw new IOException("unable to cast LockClass " + lockFactoryClassName + " instance to a LockFactory");
77     } catch (ClassNotFoundException e) {
78       throw new IOException("unable to find LockClass " + lockFactoryClassName);
79     }
80
81     File lockDir = new File(lockDirName);
82
83     if (lockFactory instanceof FSLockFactory) {
84       ((FSLockFactory) lockFactory).setLockDir(lockDir);
85     }
86
87     lockFactory.setLockPrefix("test");
88     
89     LockFactory verifyLF = new VerifyingLockFactory((byte) myID, lockFactory, verifierHost, verifierPort);
90
91     Lock l = verifyLF.makeLock("test.lock");
92
93     while(true) {
94
95       boolean obtained = false;
96
97       try {
98         obtained = l.obtain(10);
99       } catch (LockObtainFailedException e) {
100         System.out.print("x");
101       }
102
103       if (obtained) {
104         System.out.print("l");
105         l.release();
106       }
107       Thread.sleep(sleepTimeMS);
108     }
109   }
110 }