public List <string> GetChildren(string path, bool watch) { if (!Exists(path, false)) { throw KeeperException.Create(Code.NONODE, path); } if (Exists(path, false) && watch) { InstallWatch(_nodeWatches, path); } CheckACL(path, Perms.READ); List <string> children = new List <string>(); string[] directoryStack = path.Split(new string[] { "/" }, StringSplitOptions.RemoveEmptyEntries); foreach (string str in _data.Keys) { if (str.StartsWith(path)) { string[] stack = str.Split(new string[] { "/" }, StringSplitOptions.RemoveEmptyEntries); // is one folder level below the one we loockig for and starts // with path... if (stack.Length == directoryStack.Length + 1) { children.Add(stack[stack.Length - 1]); } } } return(children); }
public void Delete(string path, int version) { lock (_lock) { if (!Exists(path, false)) { throw new KeeperException.NoNodeException(); } string parentPath = GetParentPath(path); CheckACL(parentPath, Perms.DELETE); // If version isn't -1, check that it mateches if (version != -1) { DataAndVersion item; _data.TryGetValue(path, out item); if (item.version != version) { throw KeeperException.Create(Code.BADVERSION); } } _data.Remove(path); _creationTime.Remove(path); CheckWatch(_nodeWatches, path, EventType.NodeDeleted); if (parentPath != null) { CheckWatch(_nodeWatches, parentPath, EventType.NodeChildrenChanged); } } }
/// <summary> /// Retries given delegate until connections is established /// </summary> /// <param name="callback"> /// The delegate to invoke. /// </param> /// <typeparam name="T"> /// Type of data returned by delegate /// </typeparam> /// <returns> /// data returned by delegate /// </returns> public T RetryUntilConnected <T>(Func <T> callback) { Guard.NotNull(callback, "callback"); EnsuresNotDisposed(); if (zooKeeperEventWorker != null && zooKeeperEventWorker == Thread.CurrentThread) { throw new InvalidOperationException("Must not be done in the zookeeper event thread"); } var connectionWatch = Stopwatch.StartNew(); var maxWait = connectionTimeout * 4L; while (connectionWatch.ElapsedMilliseconds < maxWait) { try { return(callback()); } catch (KeeperException e) { if (e.ErrorCode == KeeperException.Code.CONNECTIONLOSS) // KeeperException.ConnectionLossException) { Thread.Yield(); //TODO: is it better than Sleep(1) ? WaitUntilConnected(connection.SessionTimeout); } else if (e.ErrorCode == KeeperException.Code.SESSIONEXPIRED ) // catch (KeeperException.SessionExpiredException) { Thread.Yield(); WaitUntilConnected(connection.SessionTimeout); } else { throw; } } } connectionWatch.Stop(); // exceeded time out, any good callers will handle the exception properly for their situation. throw KeeperException.Create(KeeperException.Code .OPERATIONTIMEOUT); // KeeperException.OperationTimeoutException(); }
/** * Perform the given operation, retrying if the connection fails * @return object. it needs to be cast to the callee's expected * return type. */ protected T RetryOperation <T>(Func <T> operation) { KeeperException exception = null; for (int i = 0; i < RetryCount; i++) { try { return(operation()); } catch (KeeperException.SessionExpiredException e) { LOG.WarnFormat("Session expired for: {0} so reconnecting due to: {1} {2}", Zookeeper, e, e.StackTrace); throw e; } catch (KeeperException.ConnectionLossException e) { if (exception == null) { exception = e; } LOG.DebugFormat("Attempt {0} failed with connection loss so attempting to reconnect: {1} {2}", e, e.StackTrace); DoRetryDelay(i); } catch (TimeoutException e) { if (exception == null) { exception = KeeperException.Create(KeeperException.Code.OPERATIONTIMEOUT); } LOG.DebugFormat("Attempt {0} failed with connection loss so attempting to reconnect: {1} {2}", e, e.StackTrace); DoRetryDelay(i); } } throw exception; }
public void ProcessResult(int rc, string path, object ctx, string name) { if (KeeperException.Code.Ok.IntValue() == rc || KeeperException.Code.Nodeexists.IntValue () == rc) { Org.Apache.Hadoop.Contrib.Bkjournal.BookKeeperJournalManager.Log.Info("Successfully created bookie available path : " + zkAvailablePath); success.Set(true); } else { KeeperException.Code code = KeeperException.Code.Get(rc); Org.Apache.Hadoop.Contrib.Bkjournal.BookKeeperJournalManager.Log.Error("Error : " + KeeperException.Create(code, path).Message + ", failed to create bookie available path : " + zkAvailablePath); } zkPathLatch.CountDown(); }