Exemple #1
0
            /// <summary>Calls <see cref="DoBody" /> while <i>lock</i> is obtained.  Blocks if lock
            /// cannot be obtained immediately.  Retries to obtain lock once per second
            /// until it is obtained, or until it has tried ten times. Lock is released when
            /// <see cref="DoBody" /> exits.
            /// </summary>
            /// <throws>  LockObtainFailedException if lock could not </throws>
            /// <summary> be obtained
            /// </summary>
            /// <throws>  IOException if <see cref="Lock.Obtain(long)" /> throws IOException </throws>
            public virtual System.Object run()
            {
                bool locked = false;

                try
                {
                    locked = lock_Renamed.Obtain(lockWaitTimeout);
                    return(DoBody());
                }
                finally
                {
                    if (locked)
                    {
                        lock_Renamed.Release();
                    }
                }
            }
Exemple #2
0
		private void  Init(Directory d, Analyzer a, bool create, IndexDeletionPolicy deletionPolicy, int maxFieldLength, DocumentsWriter.IndexingChain indexingChain, IndexCommit commit)
		{
			directory = d;
			analyzer = a;
			SetMessageID(defaultInfoStream);
			this.maxFieldLength = maxFieldLength;
			
			if (indexingChain == null)
				indexingChain = DocumentsWriter.DefaultIndexingChain;
			
			if (create)
			{
				// Clear the write lock in case it's leftover:
				directory.ClearLock(WRITE_LOCK_NAME);
			}
			
			Lock writeLock = directory.MakeLock(WRITE_LOCK_NAME);
			if (!writeLock.Obtain(writeLockTimeout))
			// obtain write lock
			{
				throw new LockObtainFailedException("Index locked for write: " + writeLock);
			}
			this.writeLock = writeLock; // save it

            bool success = false;
			try
			{
				if (create)
				{
					// Try to read first.  This is to allow create
					// against an index that's currently open for
					// searching.  In this case we write the next
					// segments_N file with no segments:
					bool doCommit;
					try
					{
						segmentInfos.Read(directory);
						segmentInfos.Clear();
						doCommit = false;
					}
					catch (System.IO.IOException)
					{
						// Likely this means it's a fresh directory
						doCommit = true;
					}
					
					if (doCommit)
					{
						// Only commit if there is no segments file 
                        // in this dir already.
						segmentInfos.Commit(directory);
                        synced.UnionWith(segmentInfos.Files(directory, true));
					}
					else
					{
						// Record that we have a change (zero out all
						// segments) pending:
						changeCount++;
					}
				}
				else
				{
					segmentInfos.Read(directory);
					
					if (commit != null)
					{
						// Swap out all segments, but, keep metadata in
						// SegmentInfos, like version & generation, to
						// preserve write-once.  This is important if
						// readers are open against the future commit
						// points.
						if (commit.Directory != directory)
							throw new System.ArgumentException("IndexCommit's directory doesn't match my directory");
						SegmentInfos oldInfos = new SegmentInfos();
						oldInfos.Read(directory, commit.SegmentsFileName);
						segmentInfos.Replace(oldInfos);
						changeCount++;
						if (infoStream != null)
							Message("init: loaded commit \"" + commit.SegmentsFileName + "\"");
					}
					
					// We assume that this segments_N was previously
					// properly sync'd:
                    synced.UnionWith(segmentInfos.Files(directory, true));
				}
				
				SetRollbackSegmentInfos(segmentInfos);
				
				docWriter = new DocumentsWriter(directory, this, indexingChain);
				docWriter.SetInfoStream(infoStream);
				docWriter.SetMaxFieldLength(maxFieldLength);
				
				// Default deleter (for backwards compatibility) is
				// KeepOnlyLastCommitDeleter:
				deleter = new IndexFileDeleter(directory, deletionPolicy == null?new KeepOnlyLastCommitDeletionPolicy():deletionPolicy, segmentInfos, infoStream, docWriter, synced);
				
				if (deleter.startingCommitDeleted)
				// Deletion policy deleted the "head" commit point.
				// We have to mark ourself as changed so that if we
				// are closed w/o any further changes we write a new
				// segments_N file.
					changeCount++;
				
				PushMaxBufferedDocs();
				
				if (infoStream != null)
				{
					Message("init: create=" + create);
					MessageState();
				}

                success = true;
			}
			finally
			{
                if (!success)
                {
                    if (infoStream != null)
                    {
                        Message("init: hit exception on init; releasing write lock");
                    }
                    try
                    {
                        writeLock.Release();
                    }
                    catch (Exception)
                    {
                        // don't mask the original exception
                    }
                    writeLock = null;
                }
			}
		}
        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));
            }
        }