/** * Convenience utility: creates a "session fail" retry loop calling the given proc * * @param client Zookeeper * @param mode how to handle session failures * @param proc procedure to call with retry * @param <T> return type * @return procedure result * @throws Exception any non-retriable errors */ public static T CallWithRetry <T>(CuratorZookeeperClient client, Mode mode, Func <T> proc) { T result = default(T); SessionFailRetryLoop retryLoop = client.NewSessionFailRetryLoop(mode); retryLoop.Start(); try{ while (retryLoop.ShouldContinue()) { try { client.InternalBlockUntilConnectedOrTimedOut(); result = proc(); retryLoop.MarkComplete(); } catch (Exception e) { retryLoop.TakeException(e); } } } finally{ retryLoop.Dispose(); } return(result); }
public SessionFailRetryLoop(CuratorZookeeperClient client, Mode _mode) { watcher = new tempWatcher <bool>(() => { sessionHasFailed = true; failedSessionThreads.GetOrAdd(ourThread, false); return(true); }); this.client = client; mode = _mode; retryLoop = client.NewRetryLoop(); }
/** * Convenience utility: creates a retry loop calling the given proc and retrying if needed * * @param client Zookeeper * @param proc procedure to call with retry * @param <T> return type * @return procedure result * @throws Exception any non-retriable errors */ public static T CallWithRetry <T>(CuratorZookeeperClient client, Func <T> proc) { T result = default(T); RetryLoop retryLoop = client.NewRetryLoop(); while (retryLoop.ShouldContinue()) { try { client.InternalBlockUntilConnectedOrTimedOut(); result = proc(); retryLoop.MarkComplete(); } catch (Exception e) { retryLoop.TakeException(e); } } return(result); }
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 testBackgroundConnect() { int CONNECTION_TIMEOUT_MS = 4000; using (CuratorZookeeperClient client = new CuratorZookeeperClient(server.GetConnectionString(), 10000, CONNECTION_TIMEOUT_MS, null, new RetryOneTime(TimeSpan.FromSeconds(1)))) { Assert.False(client.IsConnected()); client.Start(); outer: do { for (int i = 0; i < (CONNECTION_TIMEOUT_MS / 1000); ++i) { if (client.IsConnected()) { goto outer; } Thread.Sleep(CONNECTION_TIMEOUT_MS); } Assert.Fail(); } while (false); } }
public void testSimple() { using (CuratorZookeeperClient client = new CuratorZookeeperClient(server.GetConnectionString(), 10000, 10000, null, new RetryOneTime(TimeSpan.FromSeconds(1)))) { client.Start(); client.BlockUntilConnectedOrTimedOut(); var stat = client.GetZooKeeper ().Exists ("/test",false); if (stat != null) { client.GetZooKeeper ().Delete ("/test", -1); } String path = client.GetZooKeeper().Create("/test", new byte[] { 1, 2, 3 }, Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent); Assert.AreEqual(path, "/test"); } }
public void testReconnect() { using (CuratorZookeeperClient client = new CuratorZookeeperClient(server.GetConnectionString(), 10000, 10000, null, new RetryOneTime(TimeSpan.FromSeconds(1)))) { client.Start(); client.BlockUntilConnectedOrTimedOut(); byte[] writtenData = { 1, 2, 3 }; var stat = client.GetZooKeeper ().Exists ("/test", false); if (stat == null) { client.GetZooKeeper ().Create ("/test", writtenData, Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent); } Assert.IsTrue(client.BlockUntilConnectedOrTimedOut()); byte[] readData = client.GetZooKeeper().GetData("/test", false, null); Assert.AreEqual(readData, writtenData); } }