public void Run()
 {
     try
     {
         IList <LocatedBlock> locatedBlocks = cluster.GetNameNode().GetRpcServer().GetBlockLocations
                                                  (TestFile, 0, TestFileLen).GetLocatedBlocks();
         LocatedBlock lblock      = locatedBlocks[0];
         BlockReader  blockReader = null;
         try
         {
             blockReader = BlockReaderTestUtil.GetBlockReader(cluster, lblock, 0, TestFileLen);
             NUnit.Framework.Assert.Fail("expected getBlockReader to fail the first time.");
         }
         catch (Exception t)
         {
             NUnit.Framework.Assert.IsTrue("expected to see 'TCP reads were disabled " + "for testing' in exception "
                                           + t, t.Message.Contains("TCP reads were disabled for testing"));
         }
         finally
         {
             if (blockReader != null)
             {
                 blockReader.Close();
             }
         }
         gotFailureLatch.CountDown();
         shouldRetryLatch.Await();
         try
         {
             blockReader = BlockReaderTestUtil.GetBlockReader(cluster, lblock, 0, TestFileLen);
         }
         catch (Exception t)
         {
             TestBlockReaderFactory.Log.Error("error trying to retrieve a block reader " + "the second time."
                                              , t);
             throw;
         }
         finally
         {
             if (blockReader != null)
             {
                 blockReader.Close();
             }
         }
     }
     catch (Exception t)
     {
         TestBlockReaderFactory.Log.Error("getBlockReader failure", t);
         testFailed.Set(true);
     }
 }
 public void Run()
 {
     try
     {
         while (true)
         {
             BlockReader blockReader = null;
             try
             {
                 blockReader = BlockReaderTestUtil.GetBlockReader(cluster, lblock, 0, TestFileLen);
                 sem.Release();
                 try
                 {
                     blockReader.ReadAll(buf, 0, TestFileLen);
                 }
                 finally
                 {
                     sem.AcquireUninterruptibly();
                 }
             }
             catch (ClosedByInterruptException e)
             {
                 TestBlockReaderFactory.Log.Info("got the expected ClosedByInterruptException", e);
                 sem.Release();
                 break;
             }
             finally
             {
                 if (blockReader != null)
                 {
                     blockReader.Close();
                 }
             }
             TestBlockReaderFactory.Log.Info("read another " + TestFileLen + " bytes.");
         }
     }
     catch (Exception t)
     {
         TestBlockReaderFactory.Log.Error("getBlockReader failure", t);
         testFailed.Set(true);
         sem.Release();
     }
 }
        /// <summary>
        /// When an InterruptedException is sent to a thread calling
        /// FileChannel#read, the FileChannel is immediately closed and the
        /// thread gets an exception.
        /// </summary>
        /// <remarks>
        /// When an InterruptedException is sent to a thread calling
        /// FileChannel#read, the FileChannel is immediately closed and the
        /// thread gets an exception.  This effectively means that we might have
        /// someone asynchronously calling close() on the file descriptors we use
        /// in BlockReaderLocal.  So when unreferencing a ShortCircuitReplica in
        /// ShortCircuitCache#unref, we should check if the FileChannel objects
        /// are still open.  If not, we should purge the replica to avoid giving
        /// it out to any future readers.
        /// This is a regression test for HDFS-6227: Short circuit read failed
        /// due to ClosedChannelException.
        /// Note that you may still get ClosedChannelException errors if two threads
        /// are reading from the same replica and an InterruptedException is delivered
        /// to one of them.
        /// </remarks>
        /// <exception cref="System.Exception"/>
        public virtual void TestPurgingClosedReplicas()
        {
            BlockReaderTestUtil.EnableBlockReaderFactoryTracing();
            AtomicInteger replicasCreated = new AtomicInteger(0);
            AtomicBoolean testFailed      = new AtomicBoolean(false);

            DFSInputStream.tcpReadsDisabledForTesting = true;
            BlockReaderFactory.createShortCircuitReplicaInfoCallback = new _ShortCircuitReplicaCreator_443
                                                                           (replicasCreated);
            TemporarySocketDirectory sockDir = new TemporarySocketDirectory();
            Configuration            conf    = CreateShortCircuitConf("testPurgingClosedReplicas", sockDir);
            MiniDFSCluster           cluster = new MiniDFSCluster.Builder(conf).NumDataNodes(1).Build();

            cluster.WaitActive();
            DistributedFileSystem dfs = cluster.GetFileSystem();
            string TestFile           = "/test_file";
            int    TestFileLen        = 4095;
            int    Seed = unchecked ((int)(0xFADE0));
            DistributedFileSystem fs = (DistributedFileSystem)FileSystem.Get(cluster.GetURI(0
                                                                                            ), conf);

            DFSTestUtil.CreateFile(fs, new Path(TestFile), TestFileLen, (short)1, Seed);
            Semaphore            sem           = Sharpen.Extensions.CreateSemaphore(0);
            IList <LocatedBlock> locatedBlocks = cluster.GetNameNode().GetRpcServer().GetBlockLocations
                                                     (TestFile, 0, TestFileLen).GetLocatedBlocks();
            LocatedBlock lblock = locatedBlocks[0];

            // first block
            byte[]   buf            = new byte[TestFileLen];
            Runnable readerRunnable = new _Runnable_471(cluster, lblock, TestFileLen, sem, buf
                                                        , testFailed);

            Sharpen.Thread thread = new Sharpen.Thread(readerRunnable);
            thread.Start();
            // While the thread is reading, send it interrupts.
            // These should trigger a ClosedChannelException.
            while (thread.IsAlive())
            {
                sem.AcquireUninterruptibly();
                thread.Interrupt();
                sem.Release();
            }
            NUnit.Framework.Assert.IsFalse(testFailed.Get());
            // We should be able to read from the file without
            // getting a ClosedChannelException.
            BlockReader blockReader = null;

            try
            {
                blockReader = BlockReaderTestUtil.GetBlockReader(cluster, lblock, 0, TestFileLen);
                blockReader.ReadFully(buf, 0, TestFileLen);
            }
            finally
            {
                if (blockReader != null)
                {
                    blockReader.Close();
                }
            }
            byte[] expected = DFSTestUtil.CalculateFileContentsFromSeed(Seed, TestFileLen);
            NUnit.Framework.Assert.IsTrue(Arrays.Equals(buf, expected));
            // Another ShortCircuitReplica object should have been created.
            NUnit.Framework.Assert.AreEqual(2, replicasCreated.Get());
            dfs.Close();
            cluster.Shutdown();
            sockDir.Close();
        }