コード例 #1
0
 /// <summary>
 /// Examine the ResultCode in the given <paramref name="response"/> and if the ResultCode is not Ok, throw an exception that corresponds to the code.
 /// </summary>
 /// <param name="response">Response to check for error</param>
 private static void ThrowIfError(RequestResponse response)
 {
     if (response.ResultCode != (int)RingMasterException.Code.Ok)
     {
         Exception exception = RingMasterException.GetException(response);
         throw exception;
     }
 }
コード例 #2
0
 /// <summary>
 /// Examine the ResultCode in the given <paramref name="response"/> and if the ResultCode is not Ok, throw an exception that corresponds to the response.
 /// </summary>
 /// <param name="response">Response to check for error</param>
 private static void ThrowIfError(RequestResponse response)
 {
     if (response.ResultCode != (int)RingMasterException.Code.Ok)
     {
         Exception exception = RingMasterException.GetException(response);
         RingMasterClientEventSource.Log.CompleteWithException(exception.Message);
         throw exception;
     }
 }
コード例 #3
0
        /// <summary>
        /// Queries the <see cref="Stat"/> of the node with the given path.
        /// </summary>
        /// <param name="ringMaster">Interface to ringmaster</param>
        /// <param name="path">Node path</param>
        /// <param name="watcher">Watcher interface that receives notifications for changes to this path or null</param>
        /// <param name="ignoreNonodeError">If set to <c>true</c> an exception is not thrown if no node is found at the given path</param>
        /// <returns>Task that will resolve on success to the <see cref="Stat"/> associated with the node</returns>
        public static async Task <IStat> Exists(this IRingMasterRequestHandler ringMaster, string path, IWatcher watcher, bool ignoreNonodeError)
        {
            RequestResponse response = await ringMaster.Request(
                new RequestExists(
                    path,
                    watcher));

            if (ignoreNonodeError &&
                (RingMasterException.GetCode(response.ResultCode) == RingMasterException.Code.Nonode))
            {
                return(response.Stat);
            }

            ThrowIfError(response);
            return((IStat)response.Content);
        }
コード例 #4
0
        /// <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);
            }
        }
コード例 #5
0
        public static async Task <bool> Delete(this IRingMasterRequestHandler ringMaster, string path, int version, DeleteMode deletemode = DeleteMode.None)
        {
            RequestResponse response = await ringMaster.Request(
                new RequestDelete(
                    path,
                    version,
                    deletemode));

            switch (RingMasterException.GetCode(response.ResultCode))
            {
            case RingMasterException.Code.Ok: return(true);

            case RingMasterException.Code.Nonode: return(false);

            default:
                break;
            }

            ThrowIfError(response);
            return(false);
        }
コード例 #6
0
        public void RetriableRingMasterClientStress()
        {
            int maxRequestCount       = 1000;
            int initiallyWorkingCount = 500;
            var mockRequestHandler    = Substitute.For <IRingMasterRequestHandler>();

            var rnd          = new Random();
            int requestCount = 0;

            mockRequestHandler.Request(Arg.Any <IRingMasterRequest>()).Returns <Task <RequestResponse> >((callInfo2) =>
            {
                Interlocked.Increment(ref requestCount);
                if (requestCount <= initiallyWorkingCount)
                {
                    if (rnd.NextDouble() < 0.1)
                    {
                        return(Task.FromResult(new RequestResponse()
                        {
                            ResultCode = (int)RingMasterException.Code.Operationtimeout
                        }));
                    }
                    else
                    {
                        return(Task.FromResult(new RequestResponse()
                        {
                            ResultCode = (int)RingMasterException.Code.Ok
                        }));
                    }
                }
                else if (rnd.NextDouble() < 0.5)
                {
                    return(Task.FromResult(new RequestResponse()
                    {
                        ResultCode = (int)RingMasterException.Code.Operationtimeout
                    }));
                }
                else
                {
                    throw RingMasterException.GetException(new RequestResponse()
                    {
                        ResultCode = (int)RingMasterException.Code.OperationCancelled
                    });
                }
            });

            var createClientFunc = Substitute.For <Func <string, IRingMasterRequestHandler> >();

            createClientFunc(Arg.Any <string>()).Returns((callInfo) =>
            {
                requestCount = 0;
                return(mockRequestHandler);
            });

            var vegaServiceInfoReader = Substitute.For <IVegaServiceInfoReader>();

            vegaServiceInfoReader.GetVegaServiceInfo().Returns((callInfo) =>
            {
                Thread.Sleep(rnd.Next(1, 5) * 1000);
                return(Tuple.Create(Arg.Any <string>(), Arg.Any <string>()));
            });

            var requestFunc = Substitute.For <Func <IRingMasterRequestHandler, Task <RequestResponse> > >();

            requestFunc(Arg.Any <IRingMasterRequestHandler>()).Returns((callInfo) => mockRequestHandler.Request(Arg.Any <IRingMasterRequest>()));

            RetriableRingMasterClient theClient = new RetriableRingMasterClient(createClientFunc, Arg.Any <string>(), vegaServiceInfoReader, log);

            int taskCount      = 0;
            int exceptionCount = 0;

            for (int i = 0; i < maxRequestCount; i++)
            {
                taskCount++;
                var unused = theClient.Request(requestFunc)
                             .ContinueWith(t =>
                {
                    if (t.Exception != null)
                    {
                        Interlocked.Increment(ref exceptionCount);
                    }

                    Interlocked.Decrement(ref taskCount);
                });
            }

            SpinWait.SpinUntil(() => taskCount == 0);

            Assert.IsTrue(exceptionCount < maxRequestCount - initiallyWorkingCount);
            log($"{exceptionCount}");
        }