public override IndexInputSlicer CreateSlicer(string name, IOContext context)
        {
            lock (this)
            {
                EnsureOpen();
                if (VERBOSE)
                {
                    Console.WriteLine("nrtdir.openInput name=" + name);
                }
#pragma warning disable 612, 618
                if (cache.FileExists(name))
#pragma warning restore 612, 618
                {
                    if (VERBOSE)
                    {
                        Console.WriteLine("  from cache");
                    }
                    return(cache.CreateSlicer(name, context));
                }
                else
                {
                    return(@delegate.CreateSlicer(name, context));
                }
            }
        }
 public override void Sync(ICollection <string> fileNames)
 {
     if (VERBOSE)
     {
         Console.WriteLine("nrtdir.sync files=" + fileNames);
     }
     foreach (string fileName in fileNames)
     {
         UnCache(fileName);
     }
     @delegate.Sync(fileNames);
 }
        public override void DeleteFile(string name)
        {
            lock (this)
            {
                if (VERBOSE)
                {
                    Console.WriteLine("nrtdir.deleteFile name=" + name);
                }
#pragma warning disable 612, 618
                if (cache.FileExists(name))
#pragma warning restore 612, 618
                {
                    cache.DeleteFile(name);
                }
                else
                {
                    @delegate.DeleteFile(name);
                }
            }
        }
        private void UnCache(string fileName)
        {
            // Only let one thread uncache at a time; this only
            // happens during commit() or close():
            lock (uncacheLock)
            {
                if (VERBOSE)
                {
                    Console.WriteLine("nrtdir.unCache name=" + fileName);
                }
#pragma warning disable 612, 618
                if (!cache.FileExists(fileName))
#pragma warning restore 612, 618
                {
                    // Another thread beat us...
                    return;
                }
                IOContext   context = IOContext.DEFAULT;
                IndexOutput @out    = @delegate.CreateOutput(fileName, context);
                IndexInput  @in     = null;
                try
                {
                    @in = cache.OpenInput(fileName, context);
                    @out.CopyBytes(@in, @in.Length);
                }
                finally
                {
                    IOUtils.Dispose(@in, @out);
                }

                // Lock order: uncacheLock -> this
                lock (this)
                {
                    // Must sync here because other sync methods have
                    // if (cache.fileExists(name)) { ... } else { ... }:
                    cache.DeleteFile(fileName);
                }
            }
        }
        public override IndexOutput CreateOutput(string name, IOContext context)
        {
            if (VERBOSE)
            {
                Console.WriteLine("nrtdir.createOutput name=" + name);
            }
            if (DoCacheWrite(name, context))
            {
                if (VERBOSE)
                {
                    Console.WriteLine("  to cache");
                }
                try
                {
                    @delegate.DeleteFile(name);
                }
#pragma warning disable 168
                catch (IOException ioe)
#pragma warning restore 168
                {
                    // this is fine: file may not exist
                }
                return(cache.CreateOutput(name, context));
            }
            else
            {
                try
                {
                    cache.DeleteFile(name);
                }
#pragma warning disable 168
                catch (IOException ioe)
#pragma warning restore 168
                {
                    // this is fine: file may not exist
                }
                return(@delegate.CreateOutput(name, context));
            }
        }
Esempio n. 6
0
        public static void Main(string[] args)
        {
            if (args.Length != 7)
            {
                // LUCENENET specific - our wrapper console shows the correct usage
                throw new ArgumentException();
                //Console.WriteLine("Usage: java Lucene.Net.Store.LockStressTest myID verifierHost verifierPort lockFactoryClassName lockDirName sleepTimeMS count\n" +
                //    "\n" +
                //    "  myID = int from 0 .. 255 (should be unique for test process)\n" +
                //    "  verifierHost = hostname that LockVerifyServer is listening on\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" +
                //    "  count = number of locking tries\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.");
                //Environment.FailFast("1");
            }

            int arg  = 0;
            int myID = Convert.ToInt32(args[arg++], CultureInfo.InvariantCulture);

            if (myID < 0 || myID > 255)
            {
                throw new ArgumentException("ID must be a unique int 0..255");
                //Console.WriteLine("myID must be a unique int 0..255");
                //Environment.Exit(1);
            }

            string verifierHost         = args[arg++];
            int    verifierPort         = Convert.ToInt32(args[arg++], CultureInfo.InvariantCulture);
            string lockFactoryClassName = args[arg++];
            string lockDirName          = args[arg++];
            int    sleepTimeMS          = Convert.ToInt32(args[arg++], CultureInfo.InvariantCulture);
            int    count = Convert.ToInt32(args[arg++], CultureInfo.InvariantCulture);

            IPAddress[] addresses = Dns.GetHostAddressesAsync(verifierHost).Result;
            IPAddress   addr      = addresses.FirstOrDefault();

            Type c;

            try
            {
                c = Type.GetType(lockFactoryClassName);
                if (c == null)
                {
                    // LUCENENET: try again, this time with the Store namespace
                    c = Type.GetType("Lucene.Net.Store." + lockFactoryClassName);
                }
            }
            catch (Exception)
            {
                throw new IOException("unable to find LockClass " + lockFactoryClassName);
            }

            LockFactory lockFactory;

            try
            {
                lockFactory = (LockFactory)Activator.CreateInstance(c);
            }
            catch (UnauthorizedAccessException e)
            {
                throw new IOException("Cannot instantiate lock factory " + lockFactoryClassName, e);
            }
            catch (InvalidCastException e)
            {
                throw new IOException("unable to cast LockClass " + lockFactoryClassName + " instance to a LockFactory", e);
            }
            catch (Exception e)
            {
                throw new IOException("InstantiationException when instantiating LockClass " + lockFactoryClassName, e);
            }

            DirectoryInfo lockDir = new DirectoryInfo(lockDirName);

            if (lockFactory is FSLockFactory)
            {
                ((FSLockFactory)lockFactory).SetLockDir(lockDir);
            }

            Console.WriteLine("Connecting to server " + addr + " and registering as client " + myID + "...");
            using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
            {
                socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
                socket.Connect(verifierHost, verifierPort);

                using (Stream stream = new NetworkStream(socket))
                {
                    BinaryReader intReader = new BinaryReader(stream);
                    BinaryWriter intWriter = new BinaryWriter(stream);

                    intWriter.Write(myID);
                    stream.Flush();

                    lockFactory.LockPrefix = "test";
                    LockFactory verifyLF = new VerifyingLockFactory(lockFactory, stream);
                    Lock        l        = verifyLF.MakeLock("test.lock");
                    Random      rnd      = new Random();

                    // wait for starting gun
                    if (intReader.ReadInt32() != 43)
                    {
                        throw new IOException("Protocol violation");
                    }

                    for (int i = 0; i < count; i++)
                    {
                        bool obtained = false;

                        try
                        {
                            obtained = l.Obtain(rnd.Next(100) + 10);
                        }
#pragma warning disable 168
                        catch (LockObtainFailedException e)
#pragma warning restore 168
                        {
                        }

                        if (obtained)
                        {
                            Thread.Sleep(sleepTimeMS);
                            l.Dispose();
                        }

                        if (i % 500 == 0)
                        {
                            Console.WriteLine((i * 100.0 / count) + "% done.");
                        }

                        Thread.Sleep(sleepTimeMS);
                    }
                }
            }

            Console.WriteLine("Finished " + count + " tries.");
        }