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)); }
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()); }
internal void processBackgroundOperation <DATA_TYPE>(OperationAndData <DATA_TYPE> operationAndData, ICuratorEvent @event) { bool isInitialExecution = (@event == null); if (isInitialExecution) { performBackgroundOperation(operationAndData); return; } bool doQueueOperation = false; do { if (RetryLoop.shouldRetry(@event.getResultCode())) { doQueueOperation = checkBackgroundRetry(operationAndData, @event); break; } if (operationAndData.getCallback() != null) { sendToBackgroundCallback(operationAndData, @event); break; } processEvent(@event); }while (false); if (doQueueOperation) { queueOperation(operationAndData); } }
public void Execute_invokes_before_retry_func_after_iteration() { RetryLoop loop = new RetryLoop(r => Task.FromResult(false)); loop.Succeeded = r => false; loop.ShouldRetry = r => r.Iteration < 1; int count = 0; int iteration = 0; loop.BeforeRetry = delegate(RetryContext r) { ++count; iteration = r.Iteration; return(Task.FromResult(false)); }; Task <RetryContext> task = loop.ExecuteAsync(); Assert.Equal(TaskStatus.RanToCompletion, task.Status); RetryContext context = task.Result; Assert.Equal(2, context.Iteration); Assert.False(context.Succeeded); Assert.Equal(1, count); Assert.Equal(1, iteration); }
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); }) ); } } }
static string ReadString(AbsoluteFilePath guidPath) { return(RetryLoop.Try(5, () => { using (var stream = File.OpenRead(guidPath.NativePath)) return stream.ReadToEnd(); })); }
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); } }
public void testRetryLoopWithFailure() { CuratorZookeeperClient client = new CuratorZookeeperClient(ZkDefaultHosts, DefaultSessionTimeout, DefaultConnectionTimeout, null, new RetryOneTime(1)); client.start(); try { int loopCount = 0; RetryLoop retryLoop = client.newRetryLoop(); while (retryLoop.shouldContinue()) { ++loopCount; switch (loopCount) { case 1: { // retryLoop.takeException(); break; } case 2: { retryLoop.markComplete(); break; } case 3: case 4: { // ignore break; } default: { Assert.Fail(); break; } } } Assert.True(loopCount >= 2); } finally { client.Dispose(); } }
public void Execute_runs_func_once_returns_context() { int count = 0; RetryLoop loop = new RetryLoop(r => Task.FromResult(++count)); Task<RetryContext> task = loop.ExecuteAsync(); Assert.Equal(TaskStatus.RanToCompletion, task.Status); Assert.Equal(1, count); RetryContext context = task.Result; Assert.Equal(1, context.Iteration); Assert.NotEqual(TimeSpan.Zero, context.ElapsedTime); }
public void Execute_runs_func_once_returns_context() { int count = 0; RetryLoop loop = new RetryLoop(r => Task.FromResult(++count)); Task <RetryContext> task = loop.ExecuteAsync(); Assert.Equal(TaskStatus.RanToCompletion, task.Status); Assert.Equal(1, count); RetryContext context = task.Result; Assert.Equal(1, context.Iteration); Assert.NotEqual(TimeSpan.Zero, context.ElapsedTime); }
public void Execute_runs_func_until_should_not_retry_returns_context() { int count = 0; RetryLoop loop = new RetryLoop(r => Task.FromResult(++count)); loop.ShouldRetry = r => r.Iteration < 1; loop.Succeeded = r => false; Task<RetryContext> task = loop.ExecuteAsync(); Assert.Equal(TaskStatus.RanToCompletion, task.Status); Assert.Equal(2, count); RetryContext context = task.Result; Assert.Equal(2, context.Iteration); }
public async Task testRetryLoop() { CuratorZookeeperClient client = new CuratorZookeeperClient(ZkDefaultHosts, DefaultSessionTimeout, DefaultConnectionTimeout, null, new RetryOneTime(1)); client.start(); try { int loopCount = 0; RetryLoop retryLoop = client.newRetryLoop(); while (retryLoop.shouldContinue()) { if (++loopCount > 2) { Assert.Fail(); break; } try { var path = "/test"; if (await client.getZooKeeper().existsAsync(path, false) != null) { client.getZooKeeper().deleteAsync(path).Wait(); } client.getZooKeeper().createAsync(path, new byte[] { 1, 2, 3 }, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL) .Wait(); retryLoop.markComplete(); } catch (Exception e) { retryLoop.takeException(e); } } Assert.True(loopCount > 0); } finally { client.Dispose(); } }
public void Execute_runs_func_until_should_not_retry_returns_context() { int count = 0; RetryLoop loop = new RetryLoop(r => Task.FromResult(++count)); loop.ShouldRetry = r => r.Iteration < 1; loop.Succeeded = r => false; Task <RetryContext> task = loop.ExecuteAsync(); Assert.Equal(TaskStatus.RanToCompletion, task.Status); Assert.Equal(2, count); RetryContext context = task.Result; Assert.Equal(2, context.Iteration); }
public void Add_async_extension_awaits_and_adds_result() { TaskCompletionSource <string> tcs = new TaskCompletionSource <string>(); RetryLoop loop = new RetryLoop(r => r.AddAsync("Result", tcs.Task)); Task <RetryContext> task = loop.ExecuteAsync(); Assert.False(task.IsCompleted); tcs.SetResult("xyz"); Assert.Equal(TaskStatus.RanToCompletion, task.Status); RetryContext context = task.Result; string result = context.Get <string>("Result"); Assert.Equal("xyz", result); }
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); }
public void Execute_runs_func_returns_context_with_elapsed_time() { TimeSpan funcElapsed = TimeSpan.MaxValue; Func <RetryContext, Task> func = delegate(RetryContext r) { funcElapsed = r.ElapsedTime; return(Task.FromResult(false)); }; RetryLoop loop = new RetryLoop(func); TimeSpan shouldRetryElapsed = TimeSpan.MaxValue; loop.ShouldRetry = delegate(RetryContext r) { shouldRetryElapsed = r.ElapsedTime; return(false); }; TimeSpan succeededElapsed = TimeSpan.MaxValue; loop.Succeeded = delegate(RetryContext r) { succeededElapsed = r.ElapsedTime; return(false); }; ElapsedTimerStub timer = new ElapsedTimerStub(); loop.Timer = timer; timer.ElapsedTimes.Enqueue(TimeSpan.FromSeconds(1.0d)); timer.ElapsedTimes.Enqueue(TimeSpan.FromSeconds(2.5d)); timer.ElapsedTimes.Enqueue(TimeSpan.FromSeconds(4.0d)); Task <RetryContext> task = loop.ExecuteAsync(); Assert.Equal(TaskStatus.RanToCompletion, task.Status); Assert.Equal(TimeSpan.FromSeconds(1.5d), funcElapsed); RetryContext context = task.Result; Assert.Equal(1, context.Iteration); Assert.Equal(TimeSpan.FromSeconds(3.0d), succeededElapsed); Assert.Equal(TimeSpan.FromSeconds(3.0d), shouldRetryElapsed); Assert.Equal(TimeSpan.FromSeconds(3.0d), context.ElapsedTime); }
public void TestExpiredSession() { var timing = new Timing(); var latch = new AutoResetEvent(false); var watcher = new CuratorWatcher((e) => { if (e.State == KeeperState.Expired) { latch.Set(); } }); using (var client = new CuratorZookeeperClient(server.GetConnectionString(), timing.session(), timing.connection(), watcher, new RetryOneTime(TimeSpan.FromMilliseconds(2)))) { client.Start(); bool firstTime = true; RetryLoop.CallWithRetry <bool>(client, () => { if (firstTime) { try { var stat = client.GetZooKeeper().Exists("/foo", false); if (stat == null) { client.GetZooKeeper().Create("/foo", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent); } } catch (KeeperException.NodeExistsException ignore) { } KillSession.kill(client.GetZooKeeper(), server.GetConnectionString()); Assert.IsFalse(timing.awaitLatch(latch)); } IZooKeeper zooKeeper = client.GetZooKeeper(); client.BlockUntilConnectedOrTimedOut(); Assert.IsNotNull(zooKeeper.Exists("/foo", false)); return(true); }); } }
public void Execute_func_throws_async_exception_caught_and_set_in_context() { InvalidTimeZoneException exception = new InvalidTimeZoneException("Expected."); TaskCompletionSource <bool> tcs = new TaskCompletionSource <bool>(); tcs.SetException(exception); RetryLoop loop = new RetryLoop(r => tcs.Task); Task <RetryContext> task = loop.ExecuteAsync(); Assert.Equal(TaskStatus.RanToCompletion, task.Status); RetryContext context = task.Result; Assert.Equal(1, context.Iteration); Assert.False(context.Succeeded); Assert.NotNull(context.Exception); Assert.Equal(1, context.Exception.InnerExceptions.Count); Assert.Same(exception, context.Exception.InnerExceptions[0]); }
public void Assert_Long_Running_Background_Result() { // fake scenario: // we wait for a timed backgroundworker to fill the database with stockItems. RetryLoop.TriesPerSecond(1).ForSeconds(3).Execute(async() => { var httpResponse = await _testContext.OrderCheckingAPIServerFactory .CreateClient() .GetAsync("http://localhost:6001/api/OrderChecking"); Assert.AreEqual(HttpStatusCode.OK, httpResponse.StatusCode); var existingStockItems = httpResponse.Content .ReadAsStringAsync() .Result.FromJsonToType <IEnumerable <StockItem> >(); existingStockItems.ShouldNotBeEmpty(); }); }
public void Execute_func_throws_sync_exception_caught_and_set_in_context() { InvalidTimeZoneException exception = new InvalidTimeZoneException("Expected."); Func <RetryContext, Task> func = delegate(RetryContext r) { throw exception; }; RetryLoop loop = new RetryLoop(func); Task <RetryContext> task = loop.ExecuteAsync(); Assert.Equal(TaskStatus.RanToCompletion, task.Status); RetryContext context = task.Result; Assert.Equal(1, context.Iteration); Assert.False(context.Succeeded); Assert.NotNull(context.Exception); Assert.Equal(1, context.Exception.InnerExceptions.Count); Assert.Same(exception, context.Exception.InnerExceptions[0]); }
public void Execute_runs_func_returns_context_with_elapsed_time() { TimeSpan funcElapsed = TimeSpan.MaxValue; Func<RetryContext, Task> func = delegate(RetryContext r) { funcElapsed = r.ElapsedTime; return Task.FromResult(false); }; RetryLoop loop = new RetryLoop(func); TimeSpan shouldRetryElapsed = TimeSpan.MaxValue; loop.ShouldRetry = delegate(RetryContext r) { shouldRetryElapsed = r.ElapsedTime; return false; }; TimeSpan succeededElapsed = TimeSpan.MaxValue; loop.Succeeded = delegate(RetryContext r) { succeededElapsed = r.ElapsedTime; return false; }; ElapsedTimerStub timer = new ElapsedTimerStub(); loop.Timer = timer; timer.ElapsedTimes.Enqueue(TimeSpan.FromSeconds(1.0d)); timer.ElapsedTimes.Enqueue(TimeSpan.FromSeconds(2.5d)); timer.ElapsedTimes.Enqueue(TimeSpan.FromSeconds(4.0d)); Task<RetryContext> task = loop.ExecuteAsync(); Assert.Equal(TaskStatus.RanToCompletion, task.Status); Assert.Equal(TimeSpan.FromSeconds(1.5d), funcElapsed); RetryContext context = task.Result; Assert.Equal(1, context.Iteration); Assert.Equal(TimeSpan.FromSeconds(3.0d), succeededElapsed); Assert.Equal(TimeSpan.FromSeconds(3.0d), shouldRetryElapsed); Assert.Equal(TimeSpan.FromSeconds(3.0d), context.ElapsedTime); }
public void Execute_exception_cleared_on_next_iteration() { int nullCount = 0; Func <RetryContext, Task> func = delegate(RetryContext r) { if (r.Exception == null) { ++nullCount; } throw new InvalidTimeZoneException("Expected."); }; RetryLoop loop = new RetryLoop(func); loop.ShouldRetry = r => r.Iteration < 2; Task <RetryContext> task = loop.ExecuteAsync(); Assert.Equal(TaskStatus.RanToCompletion, task.Status); Assert.Equal(3, nullCount); }
public void Execute_before_retry_throws_sync_exception_caught_and_set_in_context() { RetryLoop loop = new RetryLoop(r => Task.FromResult(false)); loop.Succeeded = r => false; loop.ShouldRetry = r => r.Iteration < 1; InvalidTimeZoneException exception = new InvalidTimeZoneException("Expected."); loop.BeforeRetry = delegate(RetryContext r) { throw exception; }; Task <RetryContext> task = loop.ExecuteAsync(); Assert.Equal(TaskStatus.RanToCompletion, task.Status); RetryContext context = task.Result; Assert.Equal(2, context.Iteration); Assert.False(context.Succeeded); Assert.NotNull(context.Exception); Assert.Equal(1, context.Exception.InnerExceptions.Count); Assert.Same(exception, context.Exception.InnerExceptions[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); }
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(); } }
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); } }
public void Add_async_extension_awaits_and_adds_result() { TaskCompletionSource<string> tcs = new TaskCompletionSource<string>(); RetryLoop loop = new RetryLoop(r => r.AddAsync("Result", tcs.Task)); Task<RetryContext> task = loop.ExecuteAsync(); Assert.False(task.IsCompleted); tcs.SetResult("xyz"); Assert.Equal(TaskStatus.RanToCompletion, task.Status); RetryContext context = task.Result; string result = context.Get<string>("Result"); Assert.Equal("xyz", result); }
public void Execute_exception_cleared_on_next_iteration() { int nullCount = 0; Func<RetryContext, Task> func = delegate(RetryContext r) { if (r.Exception == null) { ++nullCount; } throw new InvalidTimeZoneException("Expected."); }; RetryLoop loop = new RetryLoop(func); loop.ShouldRetry = r => r.Iteration < 2; Task<RetryContext> task = loop.ExecuteAsync(); Assert.Equal(TaskStatus.RanToCompletion, task.Status); Assert.Equal(3, nullCount); }
public void Execute_before_retry_throws_async_exception_caught_and_set_in_context() { RetryLoop loop = new RetryLoop(r => Task.FromResult(false)); loop.Succeeded = r => false; loop.ShouldRetry = r => r.Iteration < 1; InvalidTimeZoneException exception = new InvalidTimeZoneException("Expected."); TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>(); tcs.SetException(exception); loop.BeforeRetry = r => tcs.Task; Task<RetryContext> task = loop.ExecuteAsync(); Assert.Equal(TaskStatus.RanToCompletion, task.Status); RetryContext context = task.Result; Assert.Equal(2, context.Iteration); Assert.False(context.Succeeded); Assert.NotNull(context.Exception); Assert.Equal(1, context.Exception.InnerExceptions.Count); Assert.Same(exception, context.Exception.InnerExceptions[0]); }
public void Execute_invokes_before_retry_func_after_iteration() { RetryLoop loop = new RetryLoop(r => Task.FromResult(false)); loop.Succeeded = r => false; loop.ShouldRetry = r => r.Iteration < 1; int count = 0; int iteration = 0; loop.BeforeRetry = delegate(RetryContext r) { ++count; iteration = r.Iteration; return Task.FromResult(false); }; Task<RetryContext> task = loop.ExecuteAsync(); Assert.Equal(TaskStatus.RanToCompletion, task.Status); RetryContext context = task.Result; Assert.Equal(2, context.Iteration); Assert.False(context.Succeeded); Assert.Equal(1, count); Assert.Equal(1, iteration); }
public void Execute_func_throws_sync_exception_caught_and_set_in_context() { InvalidTimeZoneException exception = new InvalidTimeZoneException("Expected."); Func<RetryContext, Task> func = delegate(RetryContext r) { throw exception; }; RetryLoop loop = new RetryLoop(func); Task<RetryContext> task = loop.ExecuteAsync(); Assert.Equal(TaskStatus.RanToCompletion, task.Status); RetryContext context = task.Result; Assert.Equal(1, context.Iteration); Assert.False(context.Succeeded); Assert.NotNull(context.Exception); Assert.Equal(1, context.Exception.InnerExceptions.Count); Assert.Same(exception, context.Exception.InnerExceptions[0]); }