Exemplo n.º 1
0
        /// <summary>
        /// Serialize <see cref="RequestGetChildren"/>.
        /// </summary>
        /// <param name="request">Request to serialize</param>
        private void SerializeRequestGetChildren(RequestGetChildren request)
        {
            this.SerializeWatcher(request.Watcher);
            bool hasValue = request.RetrievalCondition != null;

            this.binaryWriter.Write((bool)hasValue);
            if (hasValue)
            {
                this.binaryWriter.Write((string)request.RetrievalCondition);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Queue GetChildren requests.
        /// </summary>
        /// <param name="ringMaster">RingMaster client</param>
        /// <param name="rootPath">Path to enumerate</param>
        /// <param name="maxChildren">Maximum number of children to retrieve</param>
        public void QueueRequests(IRingMasterRequestHandler ringMaster, string rootPath, int maxChildren)
        {
            if (ringMaster == null)
            {
                throw new ArgumentNullException(nameof(ringMaster));
            }

            var   random    = new RandomGenerator();
            ulong requestId = 0;

            Trace.TraceInformation($"Queue GetChildren: path={rootPath}");

            while (!this.cancellationToken.IsCancellationRequested)
            {
                string startingChildName  = random.GetRandomName(this.MinNodeNameLength, this.MaxNodeNameLength);
                var    getChildrenRequest = new RequestGetChildren(rootPath, watcher: null, retrievalCondition: $">:{maxChildren}:{startingChildName}", uid: requestId++);

                this.semaphore.Wait();
                var timer = Stopwatch.StartNew();

                ringMaster.Request(getChildrenRequest).ContinueWith(responseTask =>
                {
                    this.semaphore.Release();
                    timer.Stop();

                    try
                    {
                        RequestResponse response = responseTask.Result;
                        if (response.ResultCode == (int)RingMasterException.Code.Ok)
                        {
                            var children = (IReadOnlyList <string>)response.Content;
                            this.instrumentation?.GetChildrenSucceeded(rootPath, children.Count, timer.Elapsed);
                        }
                        else
                        {
                            this.instrumentation?.GetChildrenFailed(rootPath);
                        }
                    }
                    catch (Exception)
                    {
                        this.instrumentation?.GetChildrenFailed(rootPath);
                    }
                });
            }
        }
Exemplo n.º 3
0
        /// <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);
        }
        /// <summary>
        /// Processes the request in regards to the given cache object.
        /// </summary>
        /// <param name="cache">The cache object to use.</param>
        /// <param name="req">The request to process.</param>
        /// <returns><c>a response</c> if the processing was completed, <c>null</c> otherwise.</returns>
        private async Task <RequestResponse> ProcessCacheRequest(IRingMasterClientCache cache, IRingMasterRequest req)
        {
            if (cache != null && req != null)
            {
                RequestResponse cachedResponse = this.GetCachedResponse(cache, req);

                if (cachedResponse != null)
                {
                    return(cachedResponse);
                }

                switch (req.RequestType)
                {
                case RingMasterRequestType.GetData:
                {
                    RequestResponse resp = await this.baseHandler.Request(req);

                    string prefix = this.cachePrefix;

                    if (prefix != null)
                    {
                        cache.SetInfo(prefix, req.Path, CachedKind.NodeData | CachedKind.NodeStats, new PrefixedClientCache.DataEntry()
                            {
                                Data = (byte[])resp.Content, Stat = resp.Stat
                            });
                    }

                    return(resp);
                }

                case RingMasterRequestType.GetChildren:
                {
                    RequestResponse resp = await this.baseHandler.Request(req);

                    string prefix = this.cachePrefix;

                    if (prefix != null)
                    {
                        RequestGetChildren gchil = req as RequestGetChildren;

                        if (string.IsNullOrEmpty(gchil.RetrievalCondition))
                        {
                            cache.SetInfo(prefix, req.Path, CachedKind.NodeChildren | CachedKind.NodeStats, new PrefixedClientCache.DataEntry()
                                {
                                    Children = (IReadOnlyList <string>)resp.Content, Stat = resp.Stat
                                });
                        }
                    }

                    return(resp);
                }

                case RingMasterRequestType.GetAcl:
                {
                    RequestResponse resp = await this.baseHandler.Request(req);

                    string prefix = this.cachePrefix;

                    if (prefix != null)
                    {
                        cache.SetInfo(prefix, req.Path, CachedKind.NodeAcls | CachedKind.NodeStats, new PrefixedClientCache.DataEntry()
                            {
                                Acls = (IReadOnlyList <Acl>)resp.Content, Stat = resp.Stat
                            });
                    }

                    return(resp);
                }

                case RingMasterRequestType.Exists:
                {
                    RequestResponse resp = await this.baseHandler.Request(req);

                    string prefix = this.cachePrefix;

                    if (prefix != null)
                    {
                        cache.SetInfo(prefix, req.Path, CachedKind.NodeStats, new PrefixedClientCache.DataEntry()
                            {
                                Stat = resp.Stat
                            });
                    }

                    return(resp);
                }
                }
            }

            return(await this.baseHandler.Request(req));
        }