private void CancelJobCommit() { lock (this) { Sharpen.Thread threadCommitting = jobCommitThread; if (threadCommitting != null && threadCommitting.IsAlive()) { Log.Info("Cancelling commit"); threadCommitting.Interrupt(); // wait up to configured timeout for commit thread to finish long now = context.GetClock().GetTime(); long timeoutTimestamp = now + commitThreadCancelTimeoutMs; try { while (jobCommitThread == threadCommitting && now > timeoutTimestamp) { Sharpen.Runtime.Wait(this, now - timeoutTimestamp); now = context.GetClock().GetTime(); } } catch (Exception) { } } } }
/// <exception cref="System.Exception"></exception> private static void RunOnThread(Runnable task) { Sharpen.Thread t = new Sharpen.Thread(task); t.Start(); t.Join(1000); NUnit.Framework.Assert.IsFalse(t.IsAlive(), "thread has stopped"); }
public void Run() { lock (this) { while (!terminated && callingThread.IsAlive()) { try { if (0 < deadline) { long delay = deadline - Now(); if (delay <= 0) { deadline = 0; callingThread.Interrupt(); } else { Sharpen.Runtime.Wait(this, delay); } } else { Sharpen.Runtime.Wait(this, 1000); } } catch (Exception) { } } } }
/// <summary>Interrupt the writing thread and wait until it dies</summary> /// <exception cref="System.IO.IOException">the waiting is interrupted</exception> public virtual void StopWriter(long xceiverStopTimeout) { if (writer != null && writer != Sharpen.Thread.CurrentThread() && writer.IsAlive( )) { writer.Interrupt(); try { writer.Join(xceiverStopTimeout); if (writer.IsAlive()) { string msg = "Join on writer thread " + writer + " timed out"; DataNode.Log.Warn(msg + "\n" + StringUtils.GetStackTrace(writer)); throw new IOException(msg); } } catch (Exception) { throw new IOException("Waiting for writer thread is interrupted."); } } }
/// <exception cref="System.Exception"/> protected override void ServiceStop() { if (drainEventsOnStop) { blockNewEvents = true; Log.Info("AsyncDispatcher is draining to stop, igonring any new events."); long endTime = Runtime.CurrentTimeMillis() + GetConfig().GetLong(YarnConfiguration .DispatcherDrainEventsTimeout, YarnConfiguration.DefaultDispatcherDrainEventsTimeout ); lock (waitForDrained) { while (!drained && eventHandlingThread != null && eventHandlingThread.IsAlive() && Runtime.CurrentTimeMillis() < endTime) { Sharpen.Runtime.Wait(waitForDrained, 1000); Log.Info("Waiting for AsyncDispatcher to drain. Thread state is :" + eventHandlingThread .GetState()); } } } stopped = true; if (eventHandlingThread != null) { eventHandlingThread.Interrupt(); try { eventHandlingThread.Join(); } catch (Exception ie) { Log.Warn("Interrupted Exception while stopping", ie); } } // stop all the components base.ServiceStop(); }
/// <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(); }