Example #1
0
        public IFuture <HttpQueryResponse> ProcessQuery(string name,
                                                        IDictionary <string, string> options, AsyncCallback callback, object state)
        {
            QueryRequestMessage request = new QueryRequestMessage.Builder()
                                          .SetName(name)
                                          .AddRangeOptions(GetQueryOptions(options))
                                          .Build();

            RubyMessagePacket packet = GetMessagePacket(GetNextRequestId(),
                                                        request.ToByteString());

            try {
                QueryFuture future = new SettableFuture <HttpQueryResponse>(state);
                Tuple <AsyncCallback, QueryFuture> tuple = Tuple.Create(callback, future);
                futures_.Add(request_id_, tuple);

                // Send the request and wait for the response. The request should
                // follow the REQ/REP pattern, which contains the following parts:
                //   1. [EMPTY FRAME]
                //   2. [MESSAGE]
                //
                socket_.SendMore();
                socket_.Send(packet.ToByteArray());
                return(future);
            } catch (ZMQ.Exception zmqe) {
                return
                    (Futures.ImmediateFuture(
                         GetExceptionResponse(name, HttpStatusCode.InternalServerError, zmqe)));
            } catch (System.Exception e) {
                return
                    (Futures.ImmediateFuture(
                         GetExceptionResponse(name, HttpStatusCode.InternalServerError, e)));
            }
        }
        public IFuture <T> LoadFuture(string key, CacheLoader <T> loader)
        {
            stopwatch_.Start();
            T previous_value = old_value_.Value;

            try {
                // If the T is a value type and its value is default(T) it will never
                // be reloaded and always loaded from the cache loader. We could
                // make the parameter T nullable, but this makes the code much more
                // complicated and we do not want that.
                if (typeof(T).IsValueType || (object)previous_value == null)
                {
                    T new_value = loader.Load(key);
                    return(Set(new_value) ? this : Futures.ImmediateFuture(new_value));
                }
                else
                {
                    IFuture <T> new_value_future = loader.Reload(key, previous_value);
                    return(new_value_future ?? Futures.ImmediateFuture(default(T)));
                }
            } catch (Exception exception) {
                return(SetException(exception)
          ? this
          : Futures.ImmediateFailedFuture <T>(exception));
            }
        }
Example #3
0
 public override Future <FileStatus> Load(Path path)
 {
     try
     {
         FileSystem fs = path.GetFileSystem(conf);
         return(Futures.ImmediateFuture(fs.GetFileStatus(path)));
     }
     catch (Exception th)
     {
         // report failures so it can be memoized
         return(Futures.ImmediateFailedFuture(th));
     }
 }
Example #4
0
        public IAsyncResult BeginProcessRequest(HttpContext context,
                                                AsyncCallback callback, object state)
        {
            string name = context.Request.QueryString["name"];

            if (string.IsNullOrEmpty(name))
            {
                return(Futures.ImmediateFuture(
                           new HttpQueryResponse {
                    Name = string.Empty,
                    Response = string.Empty,
                    StatusCode = HttpStatusCode.NotFound
                }));
            }

            IDictionary <string, string> parameters = GetParameters(context);
            var app = context
                      .Application[Strings.kApplicationKey] as HttpQueryApplication;
            var async_state = new AsyncState(context.GetHashCode(), context);
            IFuture <HttpQueryResponse> result = app.ProcessQuery(name, parameters,
                                                                  callback, async_state);


            // Waits the processing to finish. NOTE that we cannot finish the
            // request synchrnously, because that is no way to tell ASP.NET
            // that the request has been completed. If we do this a null reference
            // exception will be raised when OnAsyncHandlerCompletion runs, because
            // the HttpContext associated with the request is already released.
            HttpQueryResponse response;

            if (result.TryGet(0, TimeUnit.Seconds, out response))
            {
                callback(result);
                return(Futures.ImmediateFuture(0));
            }

            pending_request_[async_state.ID] = name;
            ThreadPool.RegisterWaitForSingleObject(result.AsyncWaitHandle,
                                                   (o, @out) => Timeout(@out, callback, result), null,
                                                   app.Settings.ResponseTimeout, true);
            return(result);
        }
Example #5
0
 /// <summary>
 /// Computes or retrieves a replacement value corresponding to an
 /// already-cached <paramref name="key"/>.
 /// </summary>
 /// <param name="key">
 /// The non-null key whose value should be loaded.
 /// </param>
 /// <param name="old_value">
 /// The non-null old value corresponding to <see cref="key"/>.
 /// </param>
 /// <returns>The future new value associated with <see cref="key"/>;
 /// must not be or return null(for reference types).</returns>
 /// <remarks>
 /// This method is called when an existing cache entry is refreshed by
 /// <see cref="CacheBuilder{T}.RefreshAfterWrite"/>, or through a call to
 /// cache refresh method.
 /// <para>
 /// This implementation synchronously delegates to <see cref="Load"/>. It
 /// is recommended that it be overridden with an asynchronous
 /// implementation when using
 /// <see cref="CacheBuilder{T}.RefreshAfterWrite"/>.
 /// </para>
 /// </remarks>
 public virtual IFuture <T> Reload(string key, T old_value)
 {
     return(Futures.ImmediateFuture(Load(key)));
 }
        internal static Stubber FutureReturns <V>(V value)
        {
            ListenableFuture <V> ret = Futures.ImmediateFuture(value);

            return(Org.Mockito.Mockito.DoReturn(ret));
        }