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> /// Perform the given operation, retrying if the connection fails /// </summary> /// <param name="operation">Operation for executing</param> /// <returns></returns> public T RetryOperation <T>(Func <T> operation) { KeeperException exception = null; for (var i = 0; i < _retryCount; i++) { try { return(operation()); } catch (KeeperException.ConnectionLossException e) { if (exception == null) { exception = e; } if (!Thread.CurrentThread.IsAlive) { Thread.CurrentThread.Interrupt(); throw new ThreadInterruptedException(); } if (i != (int)_retryCount - 1) { RetryDelay(i); } } } throw exception; }
private object RetryUntilConnected(Func <object> command) { while (true) { KeeperException keeperException = null; try { return(command()); } catch (AggregateException ex) { ex.Flatten().Handle(e => { if (e is KeeperException.ConnectionLossException) { keeperException = (KeeperException.ConnectionLossException)e; return(true); } else if (e is KeeperException.SessionExpiredException) { keeperException = (KeeperException.SessionExpiredException)e; return(true); } else if (e is KeeperException.NoNodeException) { keeperException = (KeeperException.NoNodeException)e; Console.WriteLine(e.ToString()); return(true); } else if (e is KeeperException.NodeExistsException) { keeperException = (KeeperException.NodeExistsException)e; Console.WriteLine(e.ToString()); return(true); } return(false); }); } switch (keeperException) { case KeeperException ex when ex is KeeperException.ConnectionLossException: WaitForSyncConnected(); break; case KeeperException ex when ex is KeeperException.SessionExpiredException: WaitForSyncConnected(); break; case KeeperException ex when ex is KeeperException.NoNodeException: return(null); case KeeperException ex when ex is KeeperException.NodeExistsException: return(null); } Thread.Sleep(1000); } }
/// <summary> /// Perform the given operation, retrying if the connection fails </summary> /// <returns> object. it needs to be cast to the callee's expected /// return type. </returns> protected async Task <bool> retryOperation(ZooKeeperOperation operation) { KeeperException exception = null; for (int i = 0; i < retryCount; i++) { try { return(await operation.execute()); } catch (KeeperException.SessionExpiredException e) { LOG.warn("Session expired for: " + zookeeper + " so reconnecting due to: " + e, e); throw; } catch (KeeperException.ConnectionLossException e) { if (exception == null) { exception = e; } LOG.debug("Attempt " + i + " failed with connection loss so " + "attempting to reconnect: " + e, e); } await retryDelay(i); } throw exception; }
public static ZKException Create(KeeperException e) { switch (e.ErrorCode) { // case DATAINCONSISTENCY: // return new DataInconsistencyException(); // case CONNECTIONLOSS: // return new ConnectionLossException(); case KeeperException.Code.NONODE: return(new ZKNoNodeException(e)); // case NOAUTH: // return new ZKNoAuthException(); case KeeperException.Code.BADVERSION: return(new ZKBadVersionException(e)); // case NOCHILDRENFOREPHEMERALS: // return new NoChildrenForEphemeralsException(); case KeeperException.Code.NODEEXISTS: return(new ZKNodeExistsException(e)); // case INVALIDACL: // return new ZKInvalidACLException(); // case AUTHFAILED: // return new AuthFailedException(); // case NOTEMPTY: // return new NotEmptyException(); // case SESSIONEXPIRED: // return new SessionExpiredException(); // case INVALIDCALLBACK: // return new InvalidCallbackException(); default: return(new ZKException(e)); } }
/** * Perform the given operation, retrying if the connection fails * @return object. it needs to be cast to the callee's expected * return type. */ protected object RetryOperation(Func <object> operation) { KeeperException exception = null; for (int i = 0; i < RetryCount; i++) { try { return(operation()); } catch (KeeperException.SessionExpiredException e) { LOG.Warn("Session expired for: " + Zookeeper + " so reconnecting due to: " + e, e); throw e; } catch (KeeperException.ConnectionLossException e) { if (exception == null) { exception = e; } LOG.Debug("Attempt " + i + " failed with connection loss so " + "attempting to reconnect: " + e, e); DoRetryDelay(i); } } throw exception; }
private List <OpResult> doOperation(AtomicBoolean firstTime) { bool localFirstTime = firstTime.getAndSet(false); if (!localFirstTime) { } List <OpResult> opResults = client.getZooKeeper().multi(transaction); if (opResults.Count > 0) { OpResult firstResult = opResults[0]; if (firstResult is OpResult.ErrorResult) { OpResult.ErrorResult error = (OpResult.ErrorResult)firstResult; KeeperException.Code code = KeeperException.Code.get(error.getErr()); if (code == null) { code = KeeperException.Code.UNIMPLEMENTED; } throw KeeperException.create(code); } } return(opResults); }
/** * Utility - return true if the given Zookeeper result code is retry-able * * @param rc result code * @return true/false */ public static bool shouldRetry(KeeperException keeperException) { return((keeperException is KeeperException.ConnectionLossException) || (keeperException is KeeperException.OperationTimeoutException) || (keeperException is KeeperException.SessionMovedException) || (keeperException is KeeperException.SessionExpiredException)); }
/// <summary> /// <para> /// Start the election process. This method will create a leader offer, /// determine its status, and either become the leader or become ready. If an /// instance of <seealso cref="ZooKeeper" /> has not yet been configured by the user, a /// new instance is created using the connectString and sessionTime specified. /// </para> /// <para> /// Any (anticipated) failures result in a failed event being sent to all /// listeners. /// </para> /// </summary> public async Task start() { using (await lockable.LockAsync().ConfigureAwait(false)) { state = State.START; await dispatchEvent(ElectionEventType.START).ConfigureAwait(false); logger.info("Starting leader election support"); if (ZooKeeper == null) { throw new InvalidOperationException("No instance of zookeeper provided. Hint: use setZooKeeper()"); } if (HostName == null) { throw new InvalidOperationException("No hostname provided. Hint: use setHostName()"); } KeeperException ke = null; try { await makeOffer().ConfigureAwait(false); await determineElectionStatus().ConfigureAwait(false); } catch (KeeperException e) { ke = e; } if (ke != null) { await becomeFailed(ke).ConfigureAwait(false); } } }
/// <summary> /// Stops all election services, revokes any outstanding leader offers, and /// disconnects from ZooKeeper. /// </summary> public async Task stop() { using (await lockable.LockAsync().ConfigureAwait(false)) { state = State.STOP; await dispatchEvent(ElectionEventType.STOP_START).ConfigureAwait(false); logger.info("Stopping leader election support"); if (leaderOffer != null) { KeeperException ke = null; try { await ZooKeeper.deleteAsync(leaderOffer.NodePath).ConfigureAwait(false); logger.debugFormat("Removed leader offer {0}", leaderOffer.NodePath); } catch (KeeperException e) { ke = e; } if (ke != null) { await becomeFailed(ke).ConfigureAwait(false); } } await dispatchEvent(ElectionEventType.STOP_COMPLETE).ConfigureAwait(false); } }
public void performBackgroundOperation(OperationAndData <PathAndBytes> operationAndData) { try { TimeTrace trace = client.getZookeeperClient().startTracer("SetDataBuilderImpl-Background"); object taskCtx = backgrounding.getContext(); Task <Stat> task = client.getZooKeeper().setDataAsync ( operationAndData.getData().getPath(), operationAndData.getData().getData(), version //, // new AsyncCallback.StatCallback() // { // public void processResult(int rc, String path, Object ctx, Stat stat) // { // trace.commit(); // ICuratorEvent @event = new CuratorEventImpl(client, CuratorEventType.SET_DATA, rc, path, null, ctx, stat, null, null, null, null); // client.processBackgroundOperation(operationAndData, @event); // }; // }, // backgrounding.getContext() ); task.ContinueWith(statTask => { trace.commit(); int errorCode = 0; if (statTask.IsFaulted) { if (!(statTask.Exception.InnerException is KeeperException)) { throw new Exception($"{nameof(SetDataBuilderImpl)}." + $"{nameof(performBackgroundOperation)} operation failed " + $"with unexpected exception of type " + $"{statTask.Exception.InnerException.GetType().FullName}." + $"Expected type {nameof(KeeperException)}"); } KeeperException keeperException = (KeeperException)statTask.Exception.InnerException; errorCode = (int)keeperException.getCode(); } ICuratorEvent @event = new CuratorEventImpl(client, CuratorEventType.SET_DATA, errorCode, operationAndData.getData().getPath(), null, taskCtx, statTask.Result, null, null, null, null); client.processBackgroundOperation(operationAndData, @event); }); } catch (Exception e) { backgrounding.checkError(e); } }
/** * Utility - return true if the given exception is retry-able * * @param exception exception to check * @return true/false */ public static bool IsRetryException(Exception exception) { if (exception.GetType() == typeof(KeeperException)) { KeeperException keeperException = (KeeperException)exception; return(ShouldRetry((int)keeperException.GetCode())); } return(false); }
/** * Utility - return true if the given exception is retry-able * * @param exception exception to check * @return true/false */ public static bool isRetryException(Exception exception) { KeeperException keeperException = exception as KeeperException; if (keeperException != null) { return(shouldRetry(keeperException)); } return(false); }
public static ZkException Create(KeeperException e) { switch (e.GetCode()) { case KeeperException.Code.NONODE: return new ZkNoNodeException(e.Message, e); case KeeperException.Code.BADVERSION: return new ZkBadVersionException(e.Message, e); case KeeperException.Code.NODEEXISTS: return new ZkNodeExistsException(e.Message, e); default: return new ZkException(e.Message, e); } }
/// <summary> /// Update the state /// </summary> /// <param name="key">Key to identify the state. Must start with / </param> /// <param name="newValue"></param> public override void Update(string key, object newValue) { try { ZooKeeperClient.setDataAsync(key, Serialize(newValue)).Wait(); } catch (AggregateException ex) { KeeperException keeperException = ex.InnerException as KeeperException; if (keeperException != null && keeperException.getCode() == KeeperException.Code.NONODE) { CreateIfNotExist(key, Serialize(newValue)); } } }
/// <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(); }
public static ZkException Create(KeeperException e) { switch (e.ErrorCode) { //节点不存在异常 case KeeperException.Code.NONODE: return(new ZkNoNodeException(e.Message, e)); //非法版本异常 case KeeperException.Code.BADVERSION: return(new ZkBadVersionException(e.Message, e)); //节点已存在异常 case KeeperException.Code.NODEEXISTS: return(new ZkNodeExistsException(e.Message, e)); default: return(new ZkException(e.Message, e)); } }
public bool TryGetString(string property, out string value) { try { var dataTask = _zooKeeper.getDataAsync(_basePath + "/" + property, true); dataTask.Wait(); byte[] data = dataTask.Result.Data; value = Encoding.UTF8.GetString(data); return(true); } catch (AggregateException ex) { KeeperException keeperException = ex.InnerException as KeeperException; if (keeperException != null && keeperException.getCode() == KeeperException.Code.NONODE) { value = null; return(false); } throw; } }
/// <summary> /// Get the value of the state /// </summary> /// <param name="key">Key to identify the state. Must start with / </param> /// <returns></returns> public override object GetValue(string key) { object state = null; try { var readTask = ZooKeeperClient.getDataAsync(key, true); readTask.Wait(); state = Deserialize(readTask.Result.Data); } catch (Exception ex) { KeeperException keeperException = ex.InnerException as KeeperException; if (keeperException != null && keeperException.getCode() == KeeperException.Code.CONNECTIONLOSS) { throw ex; } } return(state); }
public async override Task process(WatchedEvent @event) { if (@event.get_Type().Equals(Event.EventType.NodeDeleted)) { if ([email protected]_Type().ToString().Equals(les.leaderOffer.NodePath) && les.state != State.STOP) { logger.debugFormat("Node {0} deleted. Need to run through the election process.", @event.getPath()); KeeperException ke = null; try { await les.determineElectionStatus().ConfigureAwait(false); } catch (KeeperException e) { ke = e; } if (ke != null) { await les.becomeFailed(ke).ConfigureAwait(false); } } } }
public static ZooKeeperStatus ToZooKeeperStatus(this KeeperException exception) { switch (exception.getCode()) { case KeeperException.Code.CONNECTIONLOSS: return(ZooKeeperStatus.ConnectionLoss); case KeeperException.Code.OPERATIONTIMEOUT: return(ZooKeeperStatus.Timeout); case KeeperException.Code.BADARGUMENTS: return(ZooKeeperStatus.BadArguments); case KeeperException.Code.NONODE: return(ZooKeeperStatus.NodeNotFound); case KeeperException.Code.BADVERSION: return(ZooKeeperStatus.VersionsMismatch); case KeeperException.Code.NOCHILDRENFOREPHEMERALS: return(ZooKeeperStatus.ChildrenForEphemeralAreNotAllowed); case KeeperException.Code.NODEEXISTS: return(ZooKeeperStatus.NodeAlreadyExists); case KeeperException.Code.NOTEMPTY: return(ZooKeeperStatus.NodeHasChildren); case KeeperException.Code.SESSIONEXPIRED: return(ZooKeeperStatus.SessionExpired); case KeeperException.Code.SESSIONMOVED: return(ZooKeeperStatus.SessionMoved); case KeeperException.Code.NOTREADONLY: return(ZooKeeperStatus.NotReadonlyOperation); } return(ZooKeeperStatus.UnknownError); }
/// <summary> /// Create of not exist the nodes of the path /// </summary> /// <param name="path">Node Path</param> /// <param name="data">Array of bytes that represents the value to be stored</param> private void CreateIfNotExist(string path, byte[] data) { try { var createTask = ZooKeeperClient.createAsync(path, data, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); createTask.Wait(); } catch (AggregateException ex) { KeeperException keeperException = ex.InnerException as KeeperException; if (keeperException != null && keeperException.getCode() == KeeperException.Code.NONODE) { int pos = path.LastIndexOf("/"); String parentPath = path.Substring(0, pos); CreateIfNotExist(parentPath, new byte[0]); CreateIfNotExist(path, data); } else if (keeperException == null || keeperException != null && keeperException.getCode() != KeeperException.Code.NODEEXISTS) { throw ex; } } }
/** * 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(); }
public ZKNodeExistsException(string message, KeeperException ex) : base(message, ex) { }
public ZKNodeExistsException(KeeperException ex) : base(ex) { }
internal Watcher.Event.KeeperState codeToState(KeeperException code) { if (code is KeeperException.AuthFailedException || code is KeeperException.NoAuthException) { return Watcher.Event.KeeperState.AuthFailed; } if (code is KeeperException.ConnectionLossException || code is KeeperException.OperationTimeoutException) { return Watcher.Event.KeeperState.Disconnected; } if (code is KeeperException.SessionExpiredException) { return Watcher.Event.KeeperState.Expired; } if (code is KeeperException.SessionMovedException) { return Watcher.Event.KeeperState.SyncConnected; } throw new InvalidOperationException(); return Watcher.Event.KeeperState.Disconnected; }
public ZKBadVersionException(KeeperException ex) : base(ex) { }
public ZKBadVersionException(string message, KeeperException ex) : base(message, ex) { }
private void LogKeeperException(KeeperException error) { Log.Warn("Operation failed: " + error.getMessage()); }
public CuratorConnectionLossException(KeeperException.ConnectionLossException connectionLossException) : base("",connectionLossException) { }