/// <summary> /// Serialize <see cref="RequestMulti"/>. /// </summary> /// <param name="request">Request to serialize</param> private void SerializeRequestMulti(RequestMulti request) { this.binaryWriter.Write((ushort)request.Requests.Count); foreach (IRingMasterRequest req in request.Requests) { this.SerializeRingmasterRequest(req); } this.binaryWriter.Write((bool)request.CompleteSynchronously); if (this.versionToUse >= SerializationFormatVersions.Version20) { this.binaryWriter.WriteNullableString(request.ScheduledName); } }
/// <summary> /// Apply a set of delete operations as a multi. /// </summary> /// <param name="ringMaster">Interface to RingMaster</param> /// <param name="deleteOperations">Delete operations to apply as a batch</param> /// <returns>Async task to indicate the completion of the request</returns> private async Task DeleteMulti(IRingMasterRequestHandler ringMaster, IReadOnlyList <Op> deleteOperations) { var timer = Stopwatch.StartNew(); var multiRequest = new RequestMulti(deleteOperations, completeSynchronously: true); var response = await ringMaster.Request(multiRequest); if (response.ResultCode == (int)RingMasterException.Code.Ok) { this.deletedCount += deleteOperations.Count; this.instrumentation?.DeleteMultiSucceeded(this.deletedCount, deleteOperations.Count, timer.Elapsed); } else { this.instrumentation?.DeleteMultiFailed(this.deletedCount, deleteOperations.Count, timer.Elapsed); throw RingMasterException.GetException(response); } }
private void IssueMultiRequest(IRingMasterRequestHandler ringMaster, IReadOnlyList <Op> operations, CountdownEvent operationsCompletedEvent) { var multiRequest = new RequestMulti(operations, completeSynchronously: false, uid: 0); this.semaphore.Wait(); int operationsCount = operations.Count; operationsCompletedEvent.AddCount(operationsCount); var timer = Stopwatch.StartNew(); ringMaster.Request(multiRequest).ContinueWith(responseTask => { try { this.semaphore.Release(); timer.Stop(); RequestResponse response = responseTask.Result; if (response.ResultCode == (int)RingMasterException.Code.Ok) { var results = (IReadOnlyList <OpResult>)response.Content; this.instrumentation?.SetDataMultiSucceeded(results.Count, timer.Elapsed); } else { this.instrumentation?.SetDataMultiFailed(operationsCount); } } catch (Exception) { this.instrumentation?.SetDataMultiFailed(operationsCount); } finally { operationsCompletedEvent.Signal(operationsCount); } }); }
/// <summary> /// Deserialize <see cref="IZooKeeperRequest"/> /// </summary> /// <param name="xid">the callid</param> /// <param name="type">type of the call</param> /// <param name="sessionState">The PerSession State</param> /// <param name="ringMasterRequest">The ring master request.</param> /// <exception cref="System.ArgumentException">unknown type + type</exception> /// <returns>The Zookeeper Request</returns> private IZooKeeperRequest DeserializeZooKeeperRequest(int xid, ZooKeeperRequestType type, ZkprPerSessionState sessionState, out IRingMasterRequest ringMasterRequest) { ringMasterRequest = null; switch (type) { case ZooKeeperRequestType.Notification: // "0" for Createing a session ringMasterRequest = null; return(this.DeserializeNotification(xid)); case ZooKeeperRequestType.CreateSession: ZkprProtocolMessages.CreateSession cs = this.DeserializeCreateSession(); ringMasterRequest = new RequestInit((ulong)cs.SessionId, cs.IsNullPassword ? string.Empty : cs.Password); sessionState.ConnectRecieved = true; return(cs); case ZooKeeperRequestType.Exists: ZkprProtocolMessages.Exists ex = this.DeserializeExists(xid); ringMasterRequest = new RequestExists(ex.Path, ex.Watch == false ? null : new Watcher((ulong)xid, WatcherKind.OneUse)); return(ex); case ZooKeeperRequestType.GetChildren: ZkprProtocolMessages.GetChildren gc = this.DeserializeGetChildren(xid); ringMasterRequest = new RequestGetChildren(gc.Path, gc.Watch == false ? null : new Watcher((ulong)xid, WatcherKind.OneUse), null); return(gc); case ZooKeeperRequestType.GetChildren2: ZkprProtocolMessages.GetChildren2 gc2 = this.DeserializeGetChildren2(xid); ringMasterRequest = new RequestGetChildren(gc2.Path, gc2.Watch == false ? null : new Watcher((ulong)xid, WatcherKind.OneUse), null); return(gc2); case ZooKeeperRequestType.GetData: ZkprProtocolMessages.GetData gd = this.DeserializeGetData(xid); ringMasterRequest = new RequestGetData(gd.Path, RequestGetData.GetDataOptions.None, gd.Watch == false ? null : new Watcher((ulong)xid, WatcherKind.OneUse)); return(gd); case ZooKeeperRequestType.Create: ZkprProtocolMessages.Create cr = this.DeserializeCreate(xid); IReadOnlyList <Acl> acls = this.TranslateZkprAclListToRMAclList(cr.Acls); CreateMode cm = this.TranslateZkprCreatFlagsToRmCreateMode(cr.Flags); ringMasterRequest = new RequestCreate(cr.Path, cr.Data, acls, cm); return(cr); case ZooKeeperRequestType.Create2: ZkprProtocolMessages.Create2 cr2 = this.DeserializeCreate2(xid); IReadOnlyList <Acl> acls2 = this.TranslateZkprAclListToRMAclList(cr2.Acls); CreateMode cm2 = this.TranslateZkprCreatFlagsToRmCreateMode(cr2.Flags); ringMasterRequest = new RequestCreate(cr2.Path, cr2.Data, acls2, cm2); return(cr2); case ZooKeeperRequestType.SetData: ZkprProtocolMessages.SetData sd = this.DeserializeSetData(xid); ringMasterRequest = new RequestSetData(sd.Path, sd.Data, sd.Version); return(sd); case ZooKeeperRequestType.Delete: ZkprProtocolMessages.Delete dl = this.DeserializeDelete(xid); ringMasterRequest = new RequestDelete(dl.Path, dl.Version, false); return(dl); case ZooKeeperRequestType.Ping: ringMasterRequest = null; return(this.DeserializePing(xid)); case ZooKeeperRequestType.CloseSession: ringMasterRequest = null; sessionState.ConnectRecieved = false; // Renegotiate the CreateSession return(this.DeserializeCloseSession(xid)); case ZooKeeperRequestType.GetACL: ZkprProtocolMessages.GetACL ga = this.DeserializeGetACL(xid); ringMasterRequest = new RequestGetAcl(ga.Path, null); return(ga); case ZooKeeperRequestType.SetACL: ZkprProtocolMessages.SetACL sa = this.DeserializeSetACL(xid); IReadOnlyList <Acl> sa_acls = this.TranslateZkprAclListToRMAclList(sa.Acls); ringMasterRequest = new RequestSetAcl(sa.Path, sa_acls, sa.Version); return(sa); case ZooKeeperRequestType.Multi: ZkprProtocolMessages.Multi mu = this.DeserializeMulti(xid); IReadOnlyList <Op> rmOps = this.TranslateZkprOpsListToRmOpsList(mu.Ops); ringMasterRequest = new RequestMulti(rmOps, false); return(mu); case ZooKeeperRequestType.Auth: ZkprProtocolMessages.Auth au = this.DeserializeAuth(xid); ringMasterRequest = new RequestSetAuth(au.RmAuthId); return(au); case ZooKeeperRequestType.Check: case ZooKeeperRequestType.Sync: case ZooKeeperRequestType.Reconfig: case ZooKeeperRequestType.SetWatches: case ZooKeeperRequestType.RemoveWatches: case ZooKeeperRequestType.CreateContainer: case ZooKeeperRequestType.DeleteContainer: case ZooKeeperRequestType.Sasl: case ZooKeeperRequestType.Error: default: break; } return(null); }
public void TestIsReadOnly() { Assert.IsTrue(new RequestCheck(path: "/", version: 1).IsReadOnly()); Assert.IsFalse(new RequestCreate(path: "/", data: null, acl: null, createMode: CreateMode.Persistent).IsReadOnly()); Assert.IsFalse(new RequestDelete(path: "/", version: 1, cascade: false).IsReadOnly()); Assert.IsTrue(new RequestExists(path: "/", watcher: null).IsReadOnly()); Assert.IsTrue(new RequestGetAcl(path: "/", stat: null).IsReadOnly()); Assert.IsTrue(new RequestGetChildren(path: "/", watcher: null, retrievalCondition: null).IsReadOnly()); Assert.IsTrue(new RequestGetData(path: "/", options: RequestGetData.GetDataOptions.None, watcher: null).IsReadOnly()); Assert.IsFalse(new RequestInit(sessionId: 0, sessionPwd: "abc", readOnlyInterfaceRequiresLocks: true, redirection: RequestInit.RedirectionPolicy.ServerDefault).IsReadOnly()); Assert.IsFalse(new RequestSetAcl(path: "/", acl: null, version: -1).IsReadOnly()); Assert.IsFalse(new RequestSetAuth(clientId: "abc").IsReadOnly()); Assert.IsFalse(new RequestSetData(path: "/", data: null, version: 1, dataCommand: false).IsReadOnly()); Assert.IsTrue(new RequestSync(path: "/").IsReadOnly()); Assert.IsTrue(new RequestGetSubtree(path: "/", retrievalCondition: null).IsReadOnly()); Assert.IsTrue(new RequestMulti(new IRingMasterRequest[0], completeSynchronously: false).IsReadOnly()); var readMulti = new RequestMulti( new List <Op> { Op.Check("/", 1), Op.GetData("/a", RequestGetData.GetDataOptions.None, Op.Check("/a", 1)) }, completeSynchronously: false); Assert.IsTrue(readMulti.IsReadOnly()); var readWriteMulti = new RequestMulti( new List <Op> { Op.Check("/a", 1), Op.Create("/a/b", null, null, CreateMode.Ephemeral) }, completeSynchronously: true); Assert.IsFalse(readWriteMulti.IsReadOnly()); var nestedReadMulti = new RequestMulti( new IRingMasterRequest[] { readMulti }, completeSynchronously: false); Assert.IsTrue(nestedReadMulti.IsReadOnly()); var nestedReadWriteMulti = new RequestMulti( new IRingMasterRequest[] { readMulti, readWriteMulti }, completeSynchronously: true); Assert.IsFalse(nestedReadWriteMulti.IsReadOnly()); Assert.IsTrue(new RequestBatch(new IRingMasterRequest[0], completeSynchronously: false).IsReadOnly()); var readBatch = new RequestBatch( new List <Op> { Op.Check("/", 1), Op.GetData("/a", RequestGetData.GetDataOptions.None, Op.Check("/a", 1)) }, completeSynchronously: false); Assert.IsTrue(readBatch.IsReadOnly()); var readWriteBatch = new RequestBatch( new List <Op> { Op.Check("/a", 1), Op.Create("/a/b", null, null, CreateMode.Ephemeral) }, completeSynchronously: false); Assert.IsFalse(readWriteBatch.IsReadOnly()); var nestedReadBatch = new RequestBatch( new IRingMasterRequest[] { readMulti, readBatch }, completeSynchronously: false); Assert.IsTrue(nestedReadBatch.IsReadOnly()); var nestedReadWriteBatch = new RequestMulti( new IRingMasterRequest[] { readBatch, readWriteMulti }, completeSynchronously: false); Assert.IsFalse(nestedReadWriteBatch.IsReadOnly()); }