001: package org.apache.lucene.store;
002:
003: /**
004: * Licensed to the Apache Software Foundation (ASF) under one or more
005: * contributor license agreements. See the NOTICE file distributed with
006: * this work for additional information regarding copyright ownership.
007: * The ASF licenses this file to You under the Apache License, Version 2.0
008: * (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: */
019:
020: import java.io.IOException;
021: import java.io.File;
022:
023: /**
024: * Simple standalone tool that forever acquires & releases a
025: * lock using a specific LockFactory. Run without any args
026: * to see usage.
027: *
028: * @see VerifyingLockFactory
029: * @see LockVerifyServer
030: */
031:
032: public class LockStressTest {
033:
034: public static void main(String[] args) throws Exception {
035:
036: if (args.length != 6) {
037: System.out
038: .println("\nUsage: java org.apache.lucene.store.LockStressTest myID verifierHostOrIP verifierPort lockFactoryClassName lockDirName sleepTime\n"
039: + "\n"
040: + " myID = int from 0 .. 255 (should be unique for test process)\n"
041: + " verifierHostOrIP = host name or IP address where LockVerifyServer is running\n"
042: + " verifierPort = port that LockVerifyServer is listening on\n"
043: + " lockFactoryClassName = primary LockFactory class that we will use\n"
044: + " lockDirName = path to the lock directory (only set for Simple/NativeFSLockFactory\n"
045: + " sleepTimeMS = milliseconds to pause betweeen each lock obtain/release\n"
046: + "\n"
047: + "You should run multiple instances of this process, each with its own\n"
048: + "unique ID, and each pointing to the same lock directory, to verify\n"
049: + "that locking is working correctly.\n"
050: + "\n"
051: + "Make sure you are first running LockVerifyServer.\n"
052: + "\n");
053: System.exit(1);
054: }
055:
056: final int myID = Integer.parseInt(args[0]);
057:
058: if (myID < 0 || myID > 255) {
059: System.out.println("myID must be a unique int 0..255");
060: System.exit(1);
061: }
062:
063: final String verifierHost = args[1];
064: final int verifierPort = Integer.parseInt(args[2]);
065: final String lockFactoryClassName = args[3];
066: final String lockDirName = args[4];
067: final int sleepTimeMS = Integer.parseInt(args[5]);
068:
069: Class c;
070: try {
071: c = Class.forName(lockFactoryClassName);
072: } catch (ClassNotFoundException e) {
073: throw new IOException("unable to find LockClass "
074: + lockFactoryClassName);
075: }
076:
077: LockFactory lockFactory;
078: try {
079: lockFactory = (LockFactory) c.newInstance();
080: } catch (IllegalAccessException e) {
081: throw new IOException(
082: "IllegalAccessException when instantiating LockClass "
083: + lockFactoryClassName);
084: } catch (InstantiationException e) {
085: throw new IOException(
086: "InstantiationException when instantiating LockClass "
087: + lockFactoryClassName);
088: } catch (ClassCastException e) {
089: throw new IOException("unable to cast LockClass "
090: + lockFactoryClassName
091: + " instance to a LockFactory");
092: }
093:
094: File lockDir = new File(lockDirName);
095:
096: if (lockFactory instanceof NativeFSLockFactory) {
097: ((NativeFSLockFactory) lockFactory).setLockDir(lockDir);
098: } else if (lockFactory instanceof SimpleFSLockFactory) {
099: ((SimpleFSLockFactory) lockFactory).setLockDir(lockDir);
100: }
101:
102: lockFactory.setLockPrefix("test");
103:
104: LockFactory verifyLF = new VerifyingLockFactory((byte) myID,
105: lockFactory, verifierHost, verifierPort);
106:
107: Lock l = verifyLF.makeLock("test.lock");
108:
109: while (true) {
110:
111: boolean obtained = false;
112:
113: try {
114: obtained = l.obtain(10);
115: } catch (LockObtainFailedException e) {
116: System.out.print("x");
117: }
118:
119: if (obtained) {
120: System.out.print("l");
121: l.release();
122: }
123: Thread.sleep(sleepTimeMS);
124: }
125: }
126: }
|