1 package org.apache.lucene.store;
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 import java.io.IOException;
24 * Simple standalone tool that forever acquires & releases a
25 * lock using a specific LockFactory. Run without any args
28 * @see VerifyingLockFactory
29 * @see LockVerifyServer
32 public class LockStressTest {
34 public static void main(String[] args) throws Exception {
36 if (args.length != 6) {
37 System.out.println("\nUsage: java org.apache.lucene.store.LockStressTest myID verifierHostOrIP verifierPort lockFactoryClassName lockDirName sleepTime\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" +
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" +
50 "Make sure you are first running LockVerifyServer.\n" +
55 final int myID = Integer.parseInt(args[0]);
57 if (myID < 0 || myID > 255) {
58 System.out.println("myID must be a unique int 0..255");
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]);
68 LockFactory lockFactory;
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);
81 File lockDir = new File(lockDirName);
83 if (lockFactory instanceof FSLockFactory) {
84 ((FSLockFactory) lockFactory).setLockDir(lockDir);
87 lockFactory.setLockPrefix("test");
89 LockFactory verifyLF = new VerifyingLockFactory((byte) myID, lockFactory, verifierHost, verifierPort);
91 Lock l = verifyLF.makeLock("test.lock");
95 boolean obtained = false;
98 obtained = l.obtain(10);
99 } catch (LockObtainFailedException e) {
100 System.out.print("x");
104 System.out.print("l");
107 Thread.sleep(sleepTimeMS);