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); } }
/// <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; } } }
/// <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)); } } }
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 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); }