This project has retired. For details please refer to its Attic page.
RequestInfo xref
View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.giraph.comm.netty.handler;
20  
21  import org.apache.giraph.comm.requests.WritableRequest;
22  import org.apache.giraph.time.SystemTime;
23  import org.apache.giraph.time.Time;
24  import io.netty.channel.ChannelFuture;
25  
26  import java.net.InetSocketAddress;
27  import java.util.Date;
28  
29  /**
30   * Help track requests throughout the system
31   */
32  public class RequestInfo {
33    /** Time class to use */
34    private static final Time TIME = SystemTime.get();
35    /** Destination of the request */
36    private final InetSocketAddress destinationAddress;
37    /** When the request was started */
38    private final long startedNanos;
39    /** Request */
40    private final WritableRequest request;
41    /** Future of the write of this request*/
42    private volatile ChannelFuture writeFuture;
43  
44    /**
45     * Constructor.
46     *
47     * @param destinationAddress Destination of the request
48     * @param request Request that is sent
49     */
50    public RequestInfo(InetSocketAddress destinationAddress,
51                       WritableRequest request) {
52      this.destinationAddress = destinationAddress;
53      this.request = request;
54      this.startedNanos = TIME.getNanoseconds();
55    }
56  
57    public InetSocketAddress getDestinationAddress() {
58      return destinationAddress;
59    }
60  
61    /**
62     * Get the started msecs.
63     *
64     * @return Started msecs
65     */
66    public long getStartedMsecs() {
67      return startedNanos / Time.NS_PER_MS;
68    }
69  
70    /**
71     * Get the elapsed nanoseconds since the request started.
72     *
73     * @return Nanoseconds since the request was started
74     */
75    public long getElapsedNanos() {
76      return TIME.getNanoseconds() - startedNanos;
77    }
78  
79    /**
80     * Get the elapsed millseconds since the request started.
81     *
82     * @return Milliseconds since the request was started
83     */
84    public long getElapsedMsecs() {
85      return getElapsedNanos() / Time.NS_PER_MS;
86    }
87  
88  
89    public WritableRequest getRequest() {
90      return request;
91    }
92  
93    public void setWriteFuture(ChannelFuture writeFuture) {
94      this.writeFuture = writeFuture;
95    }
96  
97    public ChannelFuture getWriteFuture() {
98      return writeFuture;
99    }
100 
101   @Override
102   public String toString() {
103     return "(reqId=" + request.getRequestId() +
104         ",destAddr=" + destinationAddress.getHostName() + ":" +
105         destinationAddress.getPort() +
106         ",elapsedNanos=" +
107         getElapsedNanos() +
108         ",started=" + new Date(getStartedMsecs()) +
109         ((writeFuture == null) ? ")" :
110             ",writeDone=" + writeFuture.isDone() +
111                 ",writeSuccess=" + writeFuture.isSuccess() + ")");
112   }
113 }