Beispiel #1
0
        public Future <Tuple2 <T, T2> > Join <T2> (Future <T2> future)
        {
            var f = new FutureImpl <Tuple2 <T, T2> >();

            this.OnComplete(f1 => {
                if (future.Done)
                {
                    if (future.Success && this.Success)
                    {
                        f.SetResult(Tuples.Tuple(this.result, future.Get()));
                    }
                    else if (future.Success && this.HasError)
                    {
                        f.SetError(this.error);
                    }
                    else if (future.HasError && this.Success)
                    {
                        f.SetError(this.error);
                    }
                    else
                    {
                        f.SetError(new MultiException(this.error, future.Error));
                    }
                }
            });
            future.OnComplete(f2 => {
                if (this.Done)
                {
                    if (future.Success && this.Success)
                    {
                        f.SetResult(Tuples.Tuple(this.result, future.Get()));
                    }
                    else if (future.Success && this.HasError)
                    {
                        f.SetError(this.error);
                    }
                    else if (future.HasError && this.Success)
                    {
                        f.SetError(this.error);
                    }
                    else
                    {
                        f.SetError(new MultiException(this.error, future.Error));
                    }
                }
            });

            return(f);
        }
Beispiel #2
0
 public virtual void Run()
 {
     for (int i = 0; i < n; i++)
     {
         bool isAllocate = DFSUtil.GetRandom().Next(NumRunners) < p;
         if (isAllocate)
         {
             SubmitAllocate();
         }
         else
         {
             try
             {
                 Future <byte[]> f = RemoveFirst();
                 if (f != null)
                 {
                     SubmitRecycle(f.Get());
                 }
             }
             catch (Exception e)
             {
                 Sharpen.Runtime.PrintStackTrace(e);
                 NUnit.Framework.Assert.Fail(this + " has " + e);
             }
         }
         if ((i & unchecked ((int)(0xFF))) == 0)
         {
             SleepMs(100);
         }
     }
 }
 /// <summary>Create the home path for a user if it does not exist.</summary>
 /// <remarks>
 /// Create the home path for a user if it does not exist.
 /// This uses
 /// <see cref="InitUserRegistryAsync(string)"/>
 /// and then waits for the
 /// result ... the code path is the same as the async operation; this just
 /// picks up and relays/converts exceptions
 /// </remarks>
 /// <param name="username">username</param>
 /// <returns>the path created</returns>
 /// <exception cref="System.IO.IOException">any failure</exception>
 public virtual string InitUserRegistry(string username)
 {
     try
     {
         Future <bool> future = InitUserRegistryAsync(username);
         future.Get();
     }
     catch (Exception e)
     {
         throw (ThreadInterruptedException)(Sharpen.Extensions.InitCause(new ThreadInterruptedException
                                                                             (e.ToString()), e));
     }
     catch (ExecutionException e)
     {
         Exception cause = e.InnerException;
         if (cause is IOException)
         {
             throw (IOException)(cause);
         }
         else
         {
             throw new IOException(cause.ToString(), cause);
         }
     }
     return(HomeDir(username));
 }
Beispiel #4
0
        public virtual void ConcurrentRepack()
        {
            //
            // leave the syncPoint in broken state so any awaiting
            // threads and any threads that call await in the future get
            // the BrokenBarrierException
            //
            CyclicBarrier syncPoint = new CyclicBarrier(1);
            RevBlob       a         = tr.Blob("a");

            tr.LightweightTag("t", a);
            ExecutorService pool = Executors.NewFixedThreadPool(2);

            try
            {
                _T187790690 repack1 = new _T187790690(this)
                {
                    syncPoint = syncPoint
                };
                //_T187790690 repack2 = new _T187790690(this) { syncPoint = syncPoint };
                Future <int> result1 = pool.Submit(repack1);
                //Future<int> result2 = pool.Submit(repack2);
                NUnit.Framework.Assert.AreEqual(0, result1.Get());                // + result2.Get());
            }
            finally
            {
                pool.Shutdown();
                pool.AwaitTermination(long.MaxValue, TimeUnit.SECONDS);
            }
        }
Beispiel #5
0
        /// <exception cref="System.Exception"></exception>
        /// <exception cref="Sharpen.ExecutionException"></exception>
        /// <exception cref="Apache.Http.Conn.ConnectionPoolTimeoutException"></exception>
        protected internal virtual HttpClientConnection LeaseConnection(Future <CPoolEntry
                                                                                > future, long timeout, TimeUnit tunit)
        {
            CPoolEntry entry;

            try
            {
                entry = future.Get(timeout, tunit);
                if (entry == null || future.IsCancelled())
                {
                    throw new Exception();
                }
                Asserts.Check(entry.GetConnection() != null, "Pool entry with no connection");
                if (this.log.IsDebugEnabled())
                {
                    this.log.Debug("Connection leased: " + Format(entry) + FormatStats(entry.GetRoute
                                                                                           ()));
                }
                return(CPoolProxy.NewProxy(entry));
            }
            catch (TimeoutException)
            {
                throw new ConnectionPoolTimeoutException("Timeout waiting for connection from pool"
                                                         );
            }
        }
        public virtual void TestAddResourceRefAddResourceConcurrency()
        {
            StartEmptyStore();
            string        key      = "key1";
            string        fileName = "foo.jar";
            string        user     = "******";
            ApplicationId id       = CreateAppId(1, 1L);
            // add the resource and add the resource ref at the same time
            ExecutorService   exec           = Executors.NewFixedThreadPool(2);
            CountDownLatch    start          = new CountDownLatch(1);
            Callable <string> addKeyTask     = new _Callable_240(this, start, key, fileName);
            Callable <string> addAppIdTask   = new _Callable_246(this, start, key, id, user);
            Future <string>   addAppIdFuture = exec.Submit(addAppIdTask);
            Future <string>   addKeyFuture   = exec.Submit(addKeyTask);

            // start them at the same time
            start.CountDown();
            // get the results
            string addKeyResult   = addKeyFuture.Get();
            string addAppIdResult = addAppIdFuture.Get();

            NUnit.Framework.Assert.AreEqual(fileName, addKeyResult);
            System.Console.Out.WriteLine("addAppId() result: " + addAppIdResult);
            // it may be null or the fileName depending on the timing
            NUnit.Framework.Assert.IsTrue(addAppIdResult == null || addAppIdResult.Equals(fileName
                                                                                          ));
            exec.Shutdown();
        }
        /// <summary>
        /// Test that we get an AsynchronousCloseException when the DomainSocket
        /// we're using is closed during a read or write operation.
        /// </summary>
        /// <exception cref="System.IO.IOException"/>
        /// <exception cref="System.Exception"/>
        private void TestAsyncCloseDuringIO(bool closeDuringWrite)
        {
            string TestPath = new FilePath(sockDir.GetDir(), "testAsyncCloseDuringIO(" + closeDuringWrite
                                           + ")").GetAbsolutePath();
            DomainSocket    serv           = DomainSocket.BindAndListen(TestPath);
            ExecutorService exeServ        = Executors.NewFixedThreadPool(2);
            Callable <Void> serverCallable = new _Callable_180(serv, closeDuringWrite);
            // The server just continues either writing or reading until someone
            // asynchronously closes the client's socket.  At that point, all our
            // reads return EOF, and writes get a socket error.
            Future <Void>   serverFuture   = exeServ.Submit(serverCallable);
            DomainSocket    clientConn     = DomainSocket.Connect(serv.GetPath());
            Callable <Void> clientCallable = new _Callable_213(closeDuringWrite, clientConn);
            // The client writes or reads until another thread
            // asynchronously closes the socket.  At that point, we should
            // get ClosedChannelException, or possibly its subclass
            // AsynchronousCloseException.
            Future <Void> clientFuture = exeServ.Submit(clientCallable);

            Thread.Sleep(500);
            clientConn.Close();
            serv.Close();
            clientFuture.Get(2, TimeUnit.Minutes);
            serverFuture.Get(2, TimeUnit.Minutes);
        }
Beispiel #8
0
        /// <exception cref="System.Exception"/>
        public virtual void TestUniqueDestinationPath()
        {
            Configuration conf    = new Configuration();
            FileContext   files   = FileContext.GetLocalFSFileContext(conf);
            Path          basedir = files.MakeQualified(new Path("target", typeof(TestFSDownload).Name
                                                                 ));

            files.Mkdir(basedir, null, true);
            conf.SetStrings(typeof(TestFSDownload).FullName, basedir.ToString());
            ExecutorService   singleThreadedExec = Executors.NewSingleThreadExecutor();
            LocalDirAllocator dirs = new LocalDirAllocator(typeof(TestFSDownload).FullName);
            Path destPath          = dirs.GetLocalPathForWrite(basedir.ToString(), conf);

            destPath = new Path(destPath, System.Convert.ToString(uniqueNumberGenerator.IncrementAndGet
                                                                      ()));
            Path p = new Path(basedir, "dir" + 0 + ".jar");
            LocalResourceVisibility vis  = LocalResourceVisibility.Private;
            LocalResource           rsrc = CreateJar(files, p, vis);
            FSDownload fsd = new FSDownload(files, UserGroupInformation.GetCurrentUser(), conf
                                            , destPath, rsrc);
            Future <Path> rPath = singleThreadedExec.Submit(fsd);

            singleThreadedExec.Shutdown();
            while (!singleThreadedExec.AwaitTermination(1000, TimeUnit.Milliseconds))
            {
            }
            NUnit.Framework.Assert.IsTrue(rPath.IsDone());
            // Now FSDownload will not create a random directory to localize the
            // resource. Therefore the final localizedPath for the resource should be
            // destination directory (passed as an argument) + file name.
            NUnit.Framework.Assert.AreEqual(destPath, rPath.Get().GetParent());
        }
Beispiel #9
0
            /// <exception cref="System.Exception"/>
            public Future <Path> Answer(InvocationOnMock invoc)
            {
                Future <Path> done = Org.Mockito.Mockito.Mock <Future>();

                Org.Mockito.Mockito.When(done.IsDone()).ThenReturn(true);
                TestContainerLocalizer.FakeDownload d = (TestContainerLocalizer.FakeDownload)invoc
                                                        .GetArguments()[0];
                Org.Mockito.Mockito.When(done.Get()).ThenReturn(d.Call());
                return(done);
            }
Beispiel #10
0
 public int Compare(Future <int> left, Future <int> right)
 {
     try
     {
         return(left.Get() - right.Get());
     }
     catch (Exception e)
     {
         throw new RuntimeException(e);
     }
 }
Beispiel #11
0
        /// <summary>
        /// Test that if one thread is blocking in a read or write operation, another
        /// thread can close the socket and stop the accept.
        /// </summary>
        /// <exception cref="System.IO.IOException"/>
        /// <exception cref="System.Exception"/>
        public virtual void TestSocketAcceptAndClose()
        {
            string TestPath = new FilePath(sockDir.GetDir(), "test_sock_accept_and_close").GetAbsolutePath
                                  ();
            DomainSocket    serv     = DomainSocket.BindAndListen(TestPath);
            ExecutorService exeServ  = Executors.NewSingleThreadExecutor();
            Callable <Void> callable = new _Callable_149(serv);
            Future <Void>   future   = exeServ.Submit(callable);

            Thread.Sleep(500);
            serv.Close();
            future.Get(2, TimeUnit.Minutes);
        }
        private static Connection TryToConnect(Future <Connection> connectionFuture)
        {
            var connection = connectionFuture.Get(RuntimeConfigDefaults.ConnectionTimeout);

            if (!connection.HasValue || !connection.Value.IsConnected)
            {
                throw new ConnectionFailedException("Failed to connect to SpatialOS.",
                                                    ConnectionErrorReason.CannotEstablishConnection);
            }

            Debug.Log("Successfully connected to SpatialOS!");

            return(connection.Value);
        }
Beispiel #13
0
        /// <summary>Test that we get a read result of -1 on EOF.</summary>
        /// <exception cref="System.IO.IOException"/>
        /// <exception cref="System.Exception"/>
        public virtual void TestSocketReadEof()
        {
            string TestPath = new FilePath(sockDir.GetDir(), "testSocketReadEof").GetAbsolutePath
                                  ();
            DomainSocket    serv     = DomainSocket.BindAndListen(TestPath);
            ExecutorService exeServ  = Executors.NewSingleThreadExecutor();
            Callable <Void> callable = new _Callable_109(serv);
            Future <Void>   future   = exeServ.Submit(callable);
            DomainSocket    conn     = DomainSocket.Connect(serv.GetPath());

            Thread.Sleep(50);
            conn.Close();
            serv.Close();
            future.Get(2, TimeUnit.Minutes);
        }
Beispiel #14
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException
        public virtual List <Future <T> > invokeAll <T, T1>(Collection <T1> tasks) where T1 : Callable <T>
        {
            if (tasks == null)
            {
                throw new NullPointerException();
            }
            List <Future <T> > futures = new List <Future <T> >(tasks.Size());
            bool done = false;

            try
            {
                foreach (Callable <T> t in tasks)
                {
                    RunnableFuture <T> f = NewTaskFor(t);
                    futures.Add(f);
                    Execute(f);
                }
                for (int i = 0, size = futures.Size(); i < size; i++)
                {
                    Future <T> f = futures.Get(i);
                    if (!f.Done)
                    {
                        try
                        {
                            f.Get();
                        }
                        catch (CancellationException)
                        {
                        }
                        catch (ExecutionException)
                        {
                        }
                    }
                }
                done = true;
                return(futures);
            }
            finally
            {
                if (!done)
                {
                    for (int i = 0, size = futures.Size(); i < size; i++)
                    {
                        futures.Get(i).Cancel(true);
                    }
                }
            }
        }
Beispiel #15
0
        private string GetContentLength(Future <string> future)
        {
            string request = null;

            try
            {
                request = future.Get(2, TimeUnit.Seconds);
            }
            catch (Exception e)
            {
                NUnit.Framework.Assert.Fail(e.ToString());
            }
            Matcher matcher = contentLengthPattern.Matcher(request);

            return(matcher.Find() ? matcher.Group(2) : null);
        }
        /// <summary>Create the payload for the HeartBeat.</summary>
        /// <remarks>
        /// Create the payload for the HeartBeat. Mainly the list of
        /// <see cref="Org.Apache.Hadoop.Yarn.Server.Nodemanager.Api.Protocolrecords.LocalResourceStatus
        ///     "/>
        /// es
        /// </remarks>
        /// <returns>
        /// a
        /// <see cref="Org.Apache.Hadoop.Yarn.Server.Nodemanager.Api.Protocolrecords.LocalizerStatus
        ///     "/>
        /// that can be sent via heartbeat.
        /// </returns>
        /// <exception cref="System.Exception"/>
        private LocalizerStatus CreateStatus()
        {
            IList <LocalResourceStatus> currentResources = new AList <LocalResourceStatus>();

            // TODO: Synchronization??
            for (IEnumerator <LocalResource> i = pendingResources.Keys.GetEnumerator(); i.HasNext
                     ();)
            {
                LocalResource       rsrc = i.Next();
                LocalResourceStatus stat = recordFactory.NewRecordInstance <LocalResourceStatus>();
                stat.SetResource(rsrc);
                Future <Path> fPath = pendingResources[rsrc];
                if (fPath.IsDone())
                {
                    try
                    {
                        Path localPath = fPath.Get();
                        stat.SetLocalPath(ConverterUtils.GetYarnUrlFromPath(localPath));
                        stat.SetLocalSize(FileUtil.GetDU(new FilePath(localPath.GetParent().ToUri())));
                        stat.SetStatus(ResourceStatusType.FetchSuccess);
                    }
                    catch (ExecutionException e)
                    {
                        stat.SetStatus(ResourceStatusType.FetchFailure);
                        stat.SetException(SerializedException.NewInstance(e.InnerException));
                    }
                    catch (CancellationException e)
                    {
                        stat.SetStatus(ResourceStatusType.FetchFailure);
                        stat.SetException(SerializedException.NewInstance(e));
                    }
                    // TODO shouldn't remove until ACK
                    i.Remove();
                }
                else
                {
                    stat.SetStatus(ResourceStatusType.FetchPending);
                }
                currentResources.AddItem(stat);
            }
            LocalizerStatus status = recordFactory.NewRecordInstance <LocalizerStatus>();

            status.SetLocalizerId(localizerId);
            status.AddAllResources(currentResources);
            return(status);
        }
Beispiel #17
0
            /// <exception cref="System.Exception"/>
            internal virtual void Recycle()
            {
                Future <byte[]> f = RemoveFirst();

                if (f != null)
                {
                    Printf("randomRecycler: ");
                    try
                    {
                        Recycle(f.Get(10, TimeUnit.Milliseconds));
                    }
                    catch (TimeoutException)
                    {
                        Recycle(new byte[maxArrayLength]);
                        Printf("timeout, new byte[%d]\n", maxArrayLength);
                    }
                }
            }
        public TestAccount getAccountAsynchronous(TestAccount account)
        {
            StandardTransferObject to = transferObjectFactory.createTransferObject();

            to.setCalleeClass("com.qileyuan.tatala.example.proxy.TestServerProxy");
            to.setCalleeMethod("getAccount");
            to.registerReturnType(TransferObject.DATATYPE_WRAPPER);

            TestAccountWrapper accountWrapper = new TestAccountWrapper(account);

            to.putWrapper("account", accountWrapper);
            to.setAsynchronous(true);

            Future future = (Future)ServerExecutor.execute(to);

            accountWrapper = (TestAccountWrapper)future.Get();
            TestAccount returnAccount = accountWrapper.getAccount();

            return(returnAccount);
        }
Beispiel #19
0
        public virtual void ConcurrentPackRefs_onlyOneWritesPackedRefs()
        {
            RevBlob a = tr.Blob("a");

            tr.LightweightTag("t", a);
            CyclicBarrier   syncPoint = new CyclicBarrier(2);
            Callable <int>  packRefs  = new _Callable_131(this, syncPoint);
            ExecutorService pool      = Executors.NewFixedThreadPool(2);

            try
            {
                Future <int> p1 = pool.Submit(packRefs);
                Future <int> p2 = pool.Submit(packRefs);
                NUnit.Framework.Assert.AreEqual(1, p1.Get() + p2.Get());
            }
            finally
            {
                pool.Shutdown();
                pool.AwaitTermination(long.MaxValue, TimeUnit.SECONDS);
            }
        }
Beispiel #20
0
        /// <summary>trigger a purge operation</summary>
        /// <param name="path">pathn</param>
        /// <param name="id">yarn ID</param>
        /// <param name="policyMatch">policy to match ID on</param>
        /// <param name="purgePolicy">policy when there are children under a match</param>
        /// <param name="callback">optional callback</param>
        /// <returns>the number purged</returns>
        /// <exception cref="System.IO.IOException"/>
        /// <exception cref="Sharpen.ExecutionException"/>
        /// <exception cref="System.Exception"/>
        public virtual int Purge(string path, string id, string policyMatch, RegistryAdminService.PurgePolicy
                                 purgePolicy, BackgroundCallback callback)
        {
            Future <int> future = registry.PurgeRecordsAsync(path, id, policyMatch, purgePolicy
                                                             , callback);

            try
            {
                return(future.Get());
            }
            catch (ExecutionException e)
            {
                if (e.InnerException is IOException)
                {
                    throw (IOException)e.InnerException;
                }
                else
                {
                    throw;
                }
            }
        }
        public virtual void TestParallelThreadsWithDifferentLocales()
        {
            CyclicBarrier barrier = new CyclicBarrier(2);
            // wait for the other thread to set its locale
            ExecutorService pool = Executors.NewFixedThreadPool(2);

            try
            {
                Future <TranslationBundle> root = pool.Submit(new _T879158014(this, NLS.ROOT_LOCALE,
                                                                              barrier));
                Future <TranslationBundle> german = pool.Submit(new _T879158014(this, Sharpen.Extensions.GetGermanCulture(),
                                                                                barrier));
                NUnit.Framework.Assert.AreEqual(NLS.ROOT_LOCALE, root.Get().EffectiveLocale());
                NUnit.Framework.Assert.AreEqual(Sharpen.Extensions.GetGermanCulture(), german.Get
                                                    ().EffectiveLocale());
            }
            finally
            {
                pool.Shutdown();
                pool.AwaitTermination(long.MaxValue, TimeUnit.SECONDS);
            }
        }
Beispiel #22
0
 public void WaitForRows()
 {
     Start();
     while (true)
     {
         try
         {
             queryFuture.Get();
             break;
         }
         catch (Exception e)
         {
             if (e is CancellationException)
             {
                 continue;
             }
             else
             {
                 lastError = e;
                 throw new CouchbaseLiteException(e, Status.InternalServerError);
             }
         }
     }
 }
Beispiel #23
0
        public virtual void TestRetryInterruptible()
        {
            UnreliableInterface unreliable = (UnreliableInterface)RetryProxy.Create <UnreliableInterface
                                                                                     >(unreliableImpl, RetryPolicies.RetryUpToMaximumTimeWithFixedSleep(10, 10, TimeUnit
                                                                                                                                                        .Seconds));
            CountDownLatch           latch        = new CountDownLatch(1);
            AtomicReference <Thread> futureThread = new AtomicReference <Thread
                                                                         >();
            ExecutorService    exec   = Executors.NewSingleThreadExecutor();
            Future <Exception> future = exec.Submit(new _Callable_216(futureThread, latch, unreliable
                                                                      ));

            latch.Await();
            Thread.Sleep(1000);
            // time to fail and sleep
            Assert.True(futureThread.Get().IsAlive());
            futureThread.Get().Interrupt();
            Exception e = future.Get(1, TimeUnit.Seconds);

            // should return immediately
            NUnit.Framework.Assert.IsNotNull(e);
            Assert.Equal(typeof(Exception), e.GetType());
            Assert.Equal("sleep interrupted", e.Message);
        }
Beispiel #24
0
        public virtual void PackRefsWhileRefUpdated_refUpdateSucceeds()
        {
            RevBlob a = tr.Blob("a");

            tr.LightweightTag("t", a);
            RevBlob         b = tr.Blob("b");
            CyclicBarrier   refUpdateLockedRef = new CyclicBarrier(2);
            CyclicBarrier   packRefsDone       = new CyclicBarrier(2);
            ExecutorService pool = Executors.NewFixedThreadPool(2);

            try
            {
                Future <RefUpdate.Result> result = pool.Submit(new _Callable_185(this, b, refUpdateLockedRef
                                                                                 , packRefsDone));
                pool.Submit <object>(new _Callable_210(this, refUpdateLockedRef, packRefsDone));
                NUnit.Framework.Assert.AreEqual(result.Get(), RefUpdate.Result.FORCED);
            }
            finally
            {
                pool.ShutdownNow();
                pool.AwaitTermination(long.MaxValue, TimeUnit.SECONDS);
            }
            NUnit.Framework.Assert.AreEqual(repo.GetRef("refs/tags/t").GetObjectId(), b);
        }
Beispiel #25
0
        internal virtual void RunMonitor()
        {
            Future f = executor.Submit(monitor);

            f.Get();
        }
Beispiel #26
0
        /// <exception cref="System.Exception"/>
        /// <exception cref="System.IO.IOException"/>
        private void DoCheckpoint()
        {
            System.Diagnostics.Debug.Assert(canceler != null);
            long txid;

            NNStorage.NameNodeFile imageType;
            // Acquire cpLock to make sure no one is modifying the name system.
            // It does not need the full namesystem write lock, since the only thing
            // that modifies namesystem on standby node is edit log replaying.
            namesystem.CpLockInterruptibly();
            try
            {
                System.Diagnostics.Debug.Assert(namesystem.GetEditLog().IsOpenForRead(), "Standby Checkpointer should only attempt a checkpoint when "
                                                + "NN is in standby mode, but the edit logs are in an unexpected state");
                FSImage img = namesystem.GetFSImage();
                long    prevCheckpointTxId = img.GetStorage().GetMostRecentCheckpointTxId();
                long    thisCheckpointTxId = img.GetLastAppliedOrWrittenTxId();
                System.Diagnostics.Debug.Assert(thisCheckpointTxId >= prevCheckpointTxId);
                if (thisCheckpointTxId == prevCheckpointTxId)
                {
                    Log.Info("A checkpoint was triggered but the Standby Node has not " + "received any transactions since the last checkpoint at txid "
                             + thisCheckpointTxId + ". Skipping...");
                    return;
                }
                if (namesystem.IsRollingUpgrade() && !namesystem.GetFSImage().HasRollbackFSImage(
                        ))
                {
                    // if we will do rolling upgrade but have not created the rollback image
                    // yet, name this checkpoint as fsimage_rollback
                    imageType = NNStorage.NameNodeFile.ImageRollback;
                }
                else
                {
                    imageType = NNStorage.NameNodeFile.Image;
                }
                img.SaveNamespace(namesystem, imageType, canceler);
                txid = img.GetStorage().GetMostRecentCheckpointTxId();
                System.Diagnostics.Debug.Assert(txid == thisCheckpointTxId, "expected to save checkpoint at txid="
                                                + thisCheckpointTxId + " but instead saved at txid=" + txid);
                // Save the legacy OIV image, if the output dir is defined.
                string outputDir = checkpointConf.GetLegacyOivImageDir();
                if (outputDir != null && !outputDir.IsEmpty())
                {
                    img.SaveLegacyOIVImage(namesystem, outputDir, canceler);
                }
            }
            finally
            {
                namesystem.CpUnlock();
            }
            // Upload the saved checkpoint back to the active
            // Do this in a separate thread to avoid blocking transition to active
            // See HDFS-4816
            ExecutorService executor = Executors.NewSingleThreadExecutor(uploadThreadFactory);
            Future <Void>   upload   = executor.Submit(new _Callable_204(this, imageType, txid));

            executor.Shutdown();
            try
            {
                upload.Get();
            }
            catch (Exception e)
            {
                // The background thread may be blocked waiting in the throttler, so
                // interrupt it.
                upload.Cancel(true);
                throw;
            }
            catch (ExecutionException e)
            {
                throw new IOException("Exception during image upload: " + e.Message, e.InnerException
                                      );
            }
        }
Beispiel #27
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException
        public virtual List <Future <T> > invokeAll <T, T1>(Collection <T1> tasks, long timeout, TimeUnit unit) where T1 : Callable <T>
        {
            if (tasks == null)
            {
                throw new NullPointerException();
            }
            long nanos = unit.ToNanos(timeout);
            List <Future <T> > futures = new List <Future <T> >(tasks.Size());
            bool done = false;

            try
            {
                foreach (Callable <T> t in tasks)
                {
                    futures.Add(NewTaskFor(t));
                }

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final long deadline = System.nanoTime() + nanos;
                long deadline = System.nanoTime() + nanos;
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int size = futures.size();
                int size = futures.Size();

                // Interleave time checks and calls to execute in case
                // executor doesn't have any/much parallelism.
                for (int i = 0; i < size; i++)
                {
                    Execute((Runnable)futures.Get(i));
                    nanos = deadline - System.nanoTime();
                    if (nanos <= 0L)
                    {
                        return(futures);
                    }
                }

                for (int i = 0; i < size; i++)
                {
                    Future <T> f = futures.Get(i);
                    if (!f.Done)
                    {
                        if (nanos <= 0L)
                        {
                            return(futures);
                        }
                        try
                        {
                            f.Get(nanos, TimeUnit.NANOSECONDS);
                        }
                        catch (CancellationException)
                        {
                        }
                        catch (ExecutionException)
                        {
                        }
                        catch (TimeoutException)
                        {
                            return(futures);
                        }
                        nanos = deadline - System.nanoTime();
                    }
                }
                done = true;
                return(futures);
            }
            finally
            {
                if (!done)
                {
                    for (int i = 0, size = futures.Size(); i < size; i++)
                    {
                        futures.Get(i).Cancel(true);
                    }
                }
            }
        }
Beispiel #28
0
        /// <summary>
        /// Queries datanodes for the blocks specified in <code>datanodeBlocks</code>,
        /// making one RPC to each datanode.
        /// </summary>
        /// <remarks>
        /// Queries datanodes for the blocks specified in <code>datanodeBlocks</code>,
        /// making one RPC to each datanode. These RPCs are made in parallel using a
        /// threadpool.
        /// </remarks>
        /// <param name="datanodeBlocks">Map of datanodes to the blocks present on the DN</param>
        /// <returns>metadatas Map of datanodes to block metadata of the DN</returns>
        /// <exception cref="Org.Apache.Hadoop.Hdfs.Security.Token.Block.InvalidBlockTokenException
        ///     ">if client does not have read access on a requested block</exception>
        internal static IDictionary <DatanodeInfo, HdfsBlocksMetadata> QueryDatanodesForHdfsBlocksMetadata
            (Configuration conf, IDictionary <DatanodeInfo, IList <LocatedBlock> > datanodeBlocks
            , int poolsize, int timeoutMs, bool connectToDnViaHostname)
        {
            IList <BlockStorageLocationUtil.VolumeBlockLocationCallable> callables = CreateVolumeBlockLocationCallables
                                                                                         (conf, datanodeBlocks, timeoutMs, connectToDnViaHostname, Trace.CurrentSpan());
            // Use a thread pool to execute the Callables in parallel
            IList <Future <HdfsBlocksMetadata> > futures = new AList <Future <HdfsBlocksMetadata> >
                                                               ();
            ExecutorService executor = new ScheduledThreadPoolExecutor(poolsize);

            try
            {
                futures = executor.InvokeAll(callables, timeoutMs, TimeUnit.Milliseconds);
            }
            catch (Exception)
            {
            }
            // Swallow the exception here, because we can return partial results
            executor.Shutdown();
            IDictionary <DatanodeInfo, HdfsBlocksMetadata> metadatas = Maps.NewHashMapWithExpectedSize
                                                                           (datanodeBlocks.Count);

            // Fill in metadatas with results from DN RPCs, where possible
            for (int i = 0; i < futures.Count; i++)
            {
                BlockStorageLocationUtil.VolumeBlockLocationCallable callable = callables[i];
                DatanodeInfo datanode = callable.GetDatanodeInfo();
                Future <HdfsBlocksMetadata> future = futures[i];
                try
                {
                    HdfsBlocksMetadata metadata = future.Get();
                    metadatas[callable.GetDatanodeInfo()] = metadata;
                }
                catch (CancellationException e)
                {
                    Log.Info("Cancelled while waiting for datanode " + datanode.GetIpcAddr(false) + ": "
                             + e.ToString());
                }
                catch (ExecutionException e)
                {
                    Exception t = e.InnerException;
                    if (t is InvalidBlockTokenException)
                    {
                        Log.Warn("Invalid access token when trying to retrieve " + "information from datanode "
                                 + datanode.GetIpcAddr(false));
                        throw (InvalidBlockTokenException)t;
                    }
                    else
                    {
                        if (t is NotSupportedException)
                        {
                            Log.Info("Datanode " + datanode.GetIpcAddr(false) + " does not support" + " required #getHdfsBlocksMetadata() API"
                                     );
                            throw (NotSupportedException)t;
                        }
                        else
                        {
                            Log.Info("Failed to query block locations on datanode " + datanode.GetIpcAddr(false
                                                                                                          ) + ": " + t);
                        }
                    }
                    if (Log.IsDebugEnabled())
                    {
                        Log.Debug("Could not fetch information from datanode", t);
                    }
                }
                catch (Exception)
                {
                    // Shouldn't happen, because invokeAll waits for all Futures to be ready
                    Log.Info("Interrupted while fetching HdfsBlocksMetadata");
                }
            }
            return(metadatas);
        }
Beispiel #29
0
        /// <summary>
        /// the main mechanics of invokeAny.
        /// </summary>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: private <T> T doInvokeAny(Collection<? extends Callable<T>> tasks, boolean timed, long nanos) throws InterruptedException, ExecutionException, TimeoutException
        private T doInvokeAny <T, T1>(Collection <T1> tasks, bool timed, long nanos) where T1 : Callable <T>
        {
            if (tasks == null)
            {
                throw new NullPointerException();
            }
            int ntasks = tasks.Size();

            if (ntasks == 0)
            {
                throw new IllegalArgumentException();
            }
            List <Future <T> >            futures = new List <Future <T> >(ntasks);
            ExecutorCompletionService <T> ecs     = new ExecutorCompletionService <T>(this);

            // For efficiency, especially in executors with limited
            // parallelism, check to see if previously submitted tasks are
            // done before submitting more of them. This interleaving
            // plus the exception mechanics account for messiness of main
            // loop.

            try
            {
                // Record exceptions so that if we fail to obtain any
                // result, we can throw the last exception we got.
                ExecutionException ee = null;
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final long deadline = timed ? System.nanoTime() + nanos : 0L;
                long deadline = timed ? System.nanoTime() + nanos : 0L;
//JAVA TO C# CONVERTER TODO TASK: Java wildcard generics are not converted to .NET:
//ORIGINAL LINE: Iterator<? extends Callable<T>> it = tasks.iterator();
                Iterator <?> it = tasks.Iterator();

                // Start one task for sure; the rest incrementally
                futures.Add(ecs.Submit(it.Next()));
                --ntasks;
                int active = 1;

                for (;;)
                {
                    Future <T> f = ecs.Poll();
                    if (f == null)
                    {
                        if (ntasks > 0)
                        {
                            --ntasks;
                            futures.Add(ecs.Submit(it.Next()));
                            ++active;
                        }
                        else if (active == 0)
                        {
                            break;
                        }
                        else if (timed)
                        {
                            f = ecs.Poll(nanos, TimeUnit.NANOSECONDS);
                            if (f == null)
                            {
                                throw new TimeoutException();
                            }
                            nanos = deadline - System.nanoTime();
                        }
                        else
                        {
                            f = ecs.Take();
                        }
                    }
                    if (f != null)
                    {
                        --active;
                        try
                        {
                            return(f.Get());
                        }
                        catch (ExecutionException eex)
                        {
                            ee = eex;
                        }
                        catch (RuntimeException rex)
                        {
                            ee = new ExecutionException(rex);
                        }
                    }
                }

                if (ee == null)
                {
                    ee = new ExecutionException();
                }
                throw ee;
            }
            finally
            {
                for (int i = 0, size = futures.Size(); i < size; i++)
                {
                    futures.Get(i).Cancel(true);
                }
            }
        }
        /// <exception cref="System.Exception"/>
        public virtual void TestCancelSaveNamespace()
        {
            Configuration conf = GetConf();

            NameNode.InitMetrics(conf, HdfsServerConstants.NamenodeRole.Namenode);
            DFSTestUtil.FormatNameNode(conf);
            FSNamesystem fsn = FSNamesystem.LoadFromDisk(conf);
            // Replace the FSImage with a spy
            FSImage   image   = fsn.GetFSImage();
            NNStorage storage = image.GetStorage();

            storage.Close();
            // unlock any directories that FSNamesystem's initialization may have locked
            storage.SetStorageDirectories(FSNamesystem.GetNamespaceDirs(conf), FSNamesystem.GetNamespaceEditsDirs
                                              (conf));
            FSNamesystem spyFsn   = Org.Mockito.Mockito.Spy(fsn);
            FSNamesystem finalFsn = spyFsn;

            GenericTestUtils.DelayAnswer delayer = new GenericTestUtils.DelayAnswer(Log);
            BlockIdManager bid = Org.Mockito.Mockito.Spy(spyFsn.GetBlockIdManager());

            Whitebox.SetInternalState(finalFsn, "blockIdManager", bid);
            Org.Mockito.Mockito.DoAnswer(delayer).When(bid).GetGenerationStampV2();
            ExecutorService pool = Executors.NewFixedThreadPool(2);

            try
            {
                DoAnEdit(fsn, 1);
                Canceler canceler = new Canceler();
                // Save namespace
                fsn.SetSafeMode(HdfsConstants.SafeModeAction.SafemodeEnter);
                try
                {
                    Future <Void> saverFuture = pool.Submit(new _Callable_561(image, finalFsn, canceler
                                                                              ));
                    // Wait until saveNamespace calls getGenerationStamp
                    delayer.WaitForCall();
                    // then cancel the saveNamespace
                    Future <Void> cancelFuture = pool.Submit(new _Callable_572(canceler));
                    // give the cancel call time to run
                    Sharpen.Thread.Sleep(500);
                    // allow saveNamespace to proceed - it should check the cancel flag after
                    // this point and throw an exception
                    delayer.Proceed();
                    cancelFuture.Get();
                    saverFuture.Get();
                    NUnit.Framework.Assert.Fail("saveNamespace did not fail even though cancelled!");
                }
                catch (Exception t)
                {
                    GenericTestUtils.AssertExceptionContains("SaveNamespaceCancelledException", t);
                }
                Log.Info("Successfully cancelled a saveNamespace");
                // Check that we have only the original image and not any
                // cruft left over from half-finished images
                FSImageTestUtil.LogStorageContents(Log, storage);
                foreach (Storage.StorageDirectory sd in storage.DirIterable(null))
                {
                    FilePath curDir = sd.GetCurrentDir();
                    GenericTestUtils.AssertGlobEquals(curDir, "fsimage_.*", NNStorage.GetImageFileName
                                                          (0), NNStorage.GetImageFileName(0) + MD5FileUtils.Md5Suffix);
                }
            }
            finally
            {
                fsn.Close();
            }
        }