FromError() static private method

static private FromError ( ResultCode code, string message, bool nodeOffline ) : RiakResult
code ResultCode
message string
nodeOffline bool
return RiakResult
Exemplo n.º 1
0
        /// <summary>
        /// Executes a delegate function using a <see cref="IRiakConnection"/>, and returns the results.
        /// Can retry up to "<paramref name="retryAttempts"/>" times for <see cref="ResultCode.NoRetries"/> and <see cref="ResultCode.ShuttingDown"/> error states.
        /// This method is used over <see cref="RiakEndPoint.UseConnection"/> to keep a connection open to receive streaming results.
        /// </summary>
        /// <typeparam name="TResult">The type of the result from the <paramref name="useFun"/> parameter.</typeparam>
        /// <param name="useFun">
        /// The delegate function to execute. Takes an <see cref="IRiakConnection"/> and an <see cref="Action"/> continuation as input, and returns a
        /// <see cref="RiakResult{T}"/> containing an <see cref="IEnumerable{TResult}"/> as the results of the operation.
        /// </param>
        /// <param name="retryAttempts">The number of times to retry an operation.</param>
        /// <returns>The results of the <paramref name="useFun"/> delegate.</returns>
        public override RiakResult <IEnumerable <TResult> > UseDelayedConnection <TResult>(Func <IRiakConnection, Action, RiakResult <IEnumerable <TResult> > > useFun, int retryAttempts)
        {
            if (retryAttempts < 0)
            {
                return(RiakResult <IEnumerable <TResult> > .FromError(ResultCode.NoRetries, "Unable to access a connection on the cluster (no more retries).", false));
            }

            if (disposing)
            {
                return(RiakResult <IEnumerable <TResult> > .FromError(ResultCode.ShuttingDown, "System currently shutting down", true));
            }

            var errorMessages = new List <string>();
            var node          = loadBalancer.SelectNode();

            if (node != null)
            {
                var result = node.UseDelayedConnection(useFun);
                if (!result.IsSuccess)
                {
                    errorMessages.Add(result.ErrorMessage);

                    if (result.ResultCode == ResultCode.NoConnections)
                    {
                        Thread.Sleep(RetryWaitTime);
                        return(UseDelayedConnection(useFun, retryAttempts - 1));
                    }

                    if (result.ResultCode == ResultCode.CommunicationError)
                    {
                        MaybeDeactivateNode(result.NodeOffline, node);
                        Thread.Sleep(RetryWaitTime);
                        return(UseDelayedConnection(useFun, retryAttempts - 1));
                    }
                }

                return(result);
            }

            string msg = string.Format("Unable to access functioning Riak node, error(s): {0}", string.Join(", ", errorMessages));

            return(RiakResult <IEnumerable <TResult> > .FromError(ResultCode.ClusterOffline, msg, true));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Executes a delegate function using a <see cref="IRiakConnection"/>, and returns the results.
        /// Can retry up to "<paramref name="retryAttempts"/>" times for <see cref="ResultCode.NoRetries"/> and <see cref="ResultCode.ShuttingDown"/> error states.
        /// This method is used over <see cref="RiakEndPoint.UseConnection"/> to keep a connection open to receive streaming results.
        /// </summary>
        /// <typeparam name="TResult">The type of the result from the <paramref name="useFun"/> parameter.</typeparam>
        /// <param name="useFun">
        /// The delegate function to execute. Takes an <see cref="IRiakConnection"/> and an <see cref="Action"/> continuation as input, and returns a
        /// <see cref="RiakResult{T}"/> containing an <see cref="IEnumerable{TResult}"/> as the results of the operation.
        /// </param>
        /// <param name="retryAttempts">The number of times to retry an operation.</param>
        /// <returns>The results of the <paramref name="useFun"/> delegate.</returns>
        public override RiakResult <IEnumerable <TResult> > UseDelayedConnection <TResult>(Func <IRiakConnection, Action, RiakResult <IEnumerable <TResult> > > useFun, int retryAttempts)
        {
            if (retryAttempts < 0)
            {
                return(RiakResult <IEnumerable <TResult> > .FromError(ResultCode.NoRetries, "Unable to access a connection on the cluster.", false));
            }

            if (disposing)
            {
                return(RiakResult <IEnumerable <TResult> > .FromError(ResultCode.ShuttingDown, "System currently shutting down", true));
            }

            var node = loadBalancer.SelectNode();

            if (node != null)
            {
                var result = node.UseDelayedConnection(useFun);
                if (!result.IsSuccess)
                {
                    if (result.ResultCode == ResultCode.NoConnections)
                    {
                        Thread.Sleep(RetryWaitTime);
                        return(UseDelayedConnection(useFun, retryAttempts - 1));
                    }

                    if (result.ResultCode == ResultCode.CommunicationError)
                    {
                        if (result.NodeOffline)
                        {
                            DeactivateNode(node);
                        }

                        Thread.Sleep(RetryWaitTime);
                        return(UseDelayedConnection(useFun, retryAttempts - 1));
                    }
                }

                return(result);
            }

            return(RiakResult <IEnumerable <TResult> > .FromError(ResultCode.ClusterOffline, "Unable to access functioning Riak node", true));
        }