예제 #1
0
        public ICollection <CuratorTransactionResult> commit()
        {
            CheckAlreadyCommited();
            isCommitted = true;

            AtomicBoolean   firstTime  = new AtomicBoolean(true);
            List <OpResult> resultList = RetryLoop.callWithRetry
                                         (
                client.getZookeeperClient(),
                CallableUtils.FromFunc(() => doOperation(firstTime)));

            if (resultList.Count != transaction.metadataSize())
            {
                throw new InvalidOperationException(String.Format("Result size ({0}) doesn't match input size ({1})",
                                                                  resultList.Count,
                                                                  transaction.metadataSize()));
            }

            var builder = new ReadOnlyCollectionBuilder <CuratorTransactionResult>();

            for (int i = 0; i < resultList.Count; ++i)
            {
                OpResult opResult = resultList[i];
                CuratorMultiTransactionRecord.TypeAndPath metadata = transaction.getMetadata(i);
                CuratorTransactionResult curatorResult             = makeCuratorResult(opResult, metadata);
                builder.Add(curatorResult);
            }

            return(builder.ToReadOnlyCollection());
        }
예제 #2
0
        private void submitJoinRunnable(CloseableExecutorService service,
                                        CountdownEvent startLatch,
                                        CountdownEvent latch)
        {
            CancellationTokenSource token = new CancellationTokenSource();

            service.submit(CallableUtils.FromFunc <object>(() =>
            {
                try
                {
                    startLatch.Signal();
                    Console.WriteLine(startLatch.CurrentCount);
                    int sleepTime = 100000;
                    while (!token.Token.IsCancellationRequested && sleepTime >= 0)
                    {
                        sleepTime -= 100;
                        if (Thread.CurrentThread.Join(100))
                        {
                            break;
                        }
                    }
                    if (token.Token.IsCancellationRequested)
                    {
                        Console.WriteLine("Stopped by cancel request");
                    }
                }
                finally
                {
                    latch.Signal();
                }
                return(null);
            }), token);
        }
예제 #3
0
        internal String fixForNamespace(String path, bool isSequential)
        {
            if (ensurePathNeeded.get())
            {
                try
                {
                    CuratorZookeeperClient zookeeperClient = client.getZookeeperClient();
                    RetryLoop.callWithRetry
                    (
                        zookeeperClient,
                        CallableUtils.FromFunc <object>(() =>
                    {
                        ZKPaths.mkdirs(zookeeperClient.getZooKeeper(),
                                       ZKPaths.makePath("/", @namespace),
                                       true,
                                       client.getAclProvider(),
                                       true);
                        return(null);
                    })
                    );
                    ensurePathNeeded.set(false);
                }
                catch (Exception e)
                {
                    ThreadUtils.checkInterrupted(e);
                    client.logError("Ensure path threw exception", e);
                }
            }

            return(ZKPaths.fixForNamespace(@namespace, path, isSequential));
        }
예제 #4
0
 public void ensure(CuratorZookeeperClient client,
                    string path,
                    bool makeLastNode)
 {
     lock (this)
     {
         if (!isSet)
         {
             RetryLoop.callWithRetry
             (
                 client,
                 CallableUtils.FromFunc <object>(() =>
             {
                 ZKPaths.mkdirs(client.getZooKeeper(),
                                path,
                                makeLastNode,
                                _ensurePath.aclProvider,
                                _ensurePath.asContainers());
                 _ensurePath.helper.Set(doNothingHelper);
                 isSet = true;
                 return(null);
             })
             );
         }
     }
 }
예제 #5
0
        public void testBasic()
        {
            CuratorZookeeperClient client = new CuratorZookeeperClient(ZkDefaultHosts,
                                                                       DefaultSessionTimeout,
                                                                       DefaultConnectionTimeout,
                                                                       null,
                                                                       new RetryOneTime(1));
            SessionFailRetryLoop retryLoop = client.newSessionFailRetryLoop(SessionFailRetryLoop.Mode.FAIL);

            retryLoop.start();
            try
            {
                client.start();
                try
                {
                    while (retryLoop.shouldContinue())
                    {
                        try
                        {
                            RetryLoop.callWithRetry
                            (
                                client,
                                CallableUtils.FromFunc <object>(() =>
                            {
                                Task <Stat> existsTask = client.getZooKeeper().existsAsync("/foo/bar", false);
                                existsTask.Wait();
                                Assert.Null(existsTask.Result);
                                KillSession.kill(client.getZooKeeper(), ZkDefaultHosts, DefaultSessionTimeout);

                                client.getZooKeeper();
                                client.blockUntilConnectedOrTimedOut();
                                existsTask = client.getZooKeeper().existsAsync("/foo/bar", false);
                                existsTask.Wait();
                                Assert.Null(existsTask.Result);
                                return(null);
                            }
                                                                ));
                        }
                        catch (Exception e)
                        {
                            retryLoop.takeException(e);
                        }
                    }
                    Assert.Fail();
                }
                catch (SessionFailRetryLoop.SessionFailedException dummy)
                {
                    // correct
                }
            }
            finally
            {
                retryLoop.Dispose();
                CloseableUtils.closeQuietly(client);
            }
        }
예제 #6
0
        private IFuture <object> submitJoinRunnable(CloseableExecutorService service,
                                                    CountdownEvent startLatch)
        {
            CancellationTokenSource token = new CancellationTokenSource();

            return(service.submit(CallableUtils.FromFunc <object>(() =>
            {
                startLatch.Signal();
                Console.WriteLine(startLatch.CurrentCount);
                Thread.CurrentThread.Join();
                return null;
            }), token));
        }
예제 #7
0
 /**
  * Start the manager
  */
 public void start()
 {
     if (state.CompareAndSet(LATENT, STARTED) != LATENT)
     {
         throw new InvalidOperationException("Cannot be started more than once");
     }
     service.submit
     (
         new FutureTask <object>(CallableUtils.FromFunc <object>(() =>
     {
         processEvents();
         return(null);
     }))
     );
 }
예제 #8
0
        private Stat pathInForeground(string path, byte[] data)
        {
            TimeTrace trace      = client.getZookeeperClient().startTracer("SetDataBuilderImpl-Foreground");
            Stat      resultStat = RetryLoop.callWithRetry
                                   (
                client.getZookeeperClient(),
                CallableUtils.FromFunc(() =>
            {
                Task <Stat> task = client.getZooKeeper().setDataAsync(path, data, version);
                task.Wait();
                return(task.Result);
            })
                                   );

            trace.commit();
            return(resultStat);
        }
예제 #9
0
        public void testPartialRunnable()
        {
            CountdownEvent outsideLatch = new CountdownEvent(1);

            executorService.submit
            (
                new FutureTask <object>(CallableUtils.FromFunc <object>(() =>
            {
                try
                {
                    Thread.CurrentThread.Join();
                }
                finally
                {
                    outsideLatch.Signal();
                }
                return(null);
            }))
            );

            CloseableExecutorService service    = new CloseableExecutorService(executorService);
            CountdownEvent           startLatch = new CountdownEvent(QTY);
            CountdownEvent           latch      = new CountdownEvent(QTY);

            for (int i = 0; i < QTY; ++i)
            {
                submitRunnable(service, startLatch, latch);
            }

            while (service.size() < QTY)
            {
                Thread.Sleep(100);
            }

            Assert.True(startLatch.Wait(TimeSpan.FromSeconds(15)));
            service.Dispose();
            Assert.True(latch.Wait(TimeSpan.FromSeconds(15)));
            Assert.AreEqual(1, outsideLatch.CurrentCount);
        }
        public void Setup(int maxTasksInPool, int tasksToFork)
        {
            AtomicInteger                atomicVal   = new AtomicInteger();
            IExecutorService             execService = new LimitedTaskExecutorService(maxTasksInPool);
            ICollection <IFuture <int> > futures     = new List <IFuture <int> >(tasksToFork);

            for (int i = 0; i < tasksToFork; i++)
            {
                var task = new FutureTask <int>(CallableUtils.FromFunc(() =>
                {
                    Thread.Yield();
                    Thread.Sleep(7);
                    int value = atomicVal.IncrementAndGet();
                    Thread.Yield();
                    return(value);
                }));
                IFuture <int> future = execService.submit(task);
                futures.Add(future);
            }
            var results = new List <int>();

            foreach (IFuture <int> future in futures)
            {
                int value = future.get();
                results.Add(value);
            }
            results.Sort();
            int prevValue = results[0];

            Console.WriteLine(prevValue);
            for (int i = 1; i < results.Count; i++)
            {
                Console.WriteLine(results[i]);
                Assert.AreEqual(prevValue + 1, results[i]);
                prevValue = results[i];
            }
            Assert.AreEqual(atomicVal.Get(), tasksToFork);
        }
예제 #11
0
        public byte[] forPath(String path)
        {
            String localPath = client.fixForNamespace(path);

            TimeTrace trace = client.getZookeeperClient().startTracer("GetDataBuilderImpl-Foreground");

            byte[]
            responseData = RetryLoop.callWithRetry
                           (
                client.getZookeeperClient(),
                CallableUtils.FromFunc(() =>
            {
                var dataAsync = client.getZooKeeper().getDataAsync(localPath, false);
                dataAsync.Wait();
                responseStat = dataAsync.Result.Stat;
                return(dataAsync.Result.Data);
            })
                           );
            trace.commit();

            return(decompress
                    ? client.getCompressionProvider().decompress(path, responseData)
                    : responseData);
        }
예제 #12
0
        public void testRetryStatic()
        {
            CuratorZookeeperClient client = new CuratorZookeeperClient(ZkDefaultHosts,
                                                                       DefaultSessionTimeout,
                                                                       DefaultConnectionTimeout,
                                                                       null,
                                                                       new RetryOneTime(1));
            SessionFailRetryLoop retryLoop = client.newSessionFailRetryLoop(SessionFailRetryLoop.Mode.RETRY);

            retryLoop.start();
            try
            {
                client.start();
                AtomicBoolean secondWasDone = new AtomicBoolean(false);
                AtomicBoolean firstTime     = new AtomicBoolean(true);
                SessionFailRetryLoop.callWithRetry
                (
                    client,
                    SessionFailRetryLoop.Mode.RETRY,
                    CallableUtils.FromFunc <object>(() =>
                {
                    RetryLoop.callWithRetry(
                        client,
                        CallableUtils.FromFunc <object>(() =>
                    {
                        Task <Stat> existsTask;
                        if (firstTime.compareAndSet(true, false))
                        {
                            existsTask = client.getZooKeeper().existsAsync("/foo/bar", false);
                            existsTask.Wait();
                            Assert.Null(existsTask.Result);
                            KillSession.kill(client.getZooKeeper(), ZkDefaultHosts, DefaultSessionTimeout);
                            client.getZooKeeper();
                            client.blockUntilConnectedOrTimedOut();
                        }
                        existsTask = client.getZooKeeper().existsAsync("/foo/bar", false);
                        existsTask.Wait();
                        Assert.Null(existsTask.Result);
                        return(null);
                    }
                                                        ));

                    RetryLoop.callWithRetry
                    (
                        client,
                        CallableUtils.FromFunc <object>(() =>
                    {
                        Assert.False(firstTime.get());
                        Task <Stat> existsTask = client.getZooKeeper().existsAsync("/foo/bar", false);
                        existsTask.Wait();
                        Assert.Null(existsTask.Result);
                        secondWasDone.set(true);
                        return(null);
                    }
                                                        ));
                    return(null);
                }
                                                    ));

                Assert.True(secondWasDone.get());
            }
            finally
            {
                retryLoop.Dispose();
                CloseableUtils.closeQuietly(client);
            }
        }
예제 #13
0
        public void testExpiredSession()
        {
            // WARN: test requires that this must be address of one ZK host,
            // not a connection string to many nodes
            Barrier expiresBarrier        = new Barrier(2);
            Watcher watcher               = new ExpiredWatcher(expiresBarrier);
            CuratorZookeeperClient client = new CuratorZookeeperClient(ZkDefaultHosts,
                                                                       DefaultSessionTimeout,
                                                                       DefaultConnectionTimeout,
                                                                       watcher,
                                                                       new RetryOneTime(2));

            client.start();
            try
            {
                AtomicBoolean firstTime = new AtomicBoolean(true);
                RetryLoop.callWithRetry(
                    client,
                    CallableUtils.FromFunc <object>(() =>
                {
                    if (firstTime.compareAndSet(true, false))
                    {
                        try
                        {
                            Task <string> createTask = client.getZooKeeper()
                                                       .createAsync("/foo",
                                                                    new byte[0],
                                                                    ZooDefs.Ids.OPEN_ACL_UNSAFE,
                                                                    CreateMode.PERSISTENT);
                            createTask.Wait();
                        }
                        catch (AggregateException e)
                        {
                            if (e.InnerException is KeeperException.NodeExistsException)
                            {
                                // ignore
                            }
                            else
                            {
                                throw e;
                            }
                        }

                        KillSession.kill(client.getZooKeeper(), ZkDefaultHosts, DefaultSessionTimeout * 3);
                        Assert.True(expiresBarrier.SignalAndWait(DefaultSessionTimeout));
                    }
                    ZooKeeper zooKeeper = client.getZooKeeper();
                    client.blockUntilConnectedOrTimedOut();
                    Task <Stat> task = zooKeeper.existsAsync("/foo", false);
                    task.Wait();
                    Stat stat = task.Result;
                    Assert.NotNull(stat);
                    Assert.Greater(stat.getCzxid(), 0);
                    return(null);
                })
                    );
            }
            finally
            {
                client.Dispose();
            }
        }