This project has retired. For details please refer to its Attic page.
TaskInfo 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.graph;
20  
21  import org.apache.hadoop.io.Writable;
22  
23  import java.io.DataInput;
24  import java.io.DataOutput;
25  import java.io.IOException;
26  import java.net.InetSocketAddress;
27  
28  /**
29   * Abstract class for information about any task - worker or master.
30   */
31  public abstract class TaskInfo implements Writable {
32    /** Task hostname */
33    private String hostname;
34    /** Port that the IPC server is using */
35    private int port;
36    /** Task partition id */
37    private int taskId = -1;
38    /** Task host IP */
39    private String hostOrIp;
40  
41    /**
42     * Constructor
43     */
44    public TaskInfo() {
45    }
46  
47    /**
48     * Get this task's hostname
49     *
50     * @return Hostname
51     */
52    public String getHostname() {
53      return hostname.toLowerCase();
54    }
55  
56    /**
57     * Get this task's host address. Could be IP.
58     *
59     * @return host address
60     */
61    public String getHostOrIp() {
62      return hostOrIp;
63    }
64  
65    /**
66     * Get port that the IPC server of this task is using
67     *
68     * @return Port
69     */
70    public int getPort() {
71      return port;
72    }
73  
74    /**
75     * Set address that the IPC server of this task is using
76     *
77     * @param address Address
78     * @param host host name or IP
79     */
80    public void setInetSocketAddress(InetSocketAddress address, String host) {
81      this.port = address.getPort();
82      this.hostname = address.getHostName();
83      this.hostOrIp = host;
84    }
85  
86    /**
87     * Get a new instance of the InetSocketAddress for this hostname and port
88     *
89     * @return InetSocketAddress of the hostname and port.
90     */
91    public InetSocketAddress getInetSocketAddress() {
92      return new InetSocketAddress(hostOrIp, port);
93    }
94  
95    /**
96     * Set task partition id of this task
97     *
98     * @param taskId partition id
99     */
100   public void setTaskId(int taskId) {
101     this.taskId = taskId;
102   }
103 
104   /**
105    * Get task partition id of this task
106    *
107    * @return Task partition id of this task
108    */
109   public int getTaskId() {
110     return taskId;
111   }
112 
113   /**
114    * Get hostname and task id
115    *
116    * @return Hostname and task id
117    */
118   public String getHostnameId() {
119     return getHostname() + "_" + getTaskId();
120   }
121 
122   @Override
123   public boolean equals(Object other) {
124     if (other instanceof TaskInfo) {
125       TaskInfo taskInfo = (TaskInfo) other;
126       if (getHostname().equals(taskInfo.getHostname()) &&
127           getHostOrIp().equals(taskInfo.getHostOrIp()) &&
128           (getTaskId() == taskInfo.getTaskId()) &&
129           (port == taskInfo.getPort() &&
130           (taskId == taskInfo.getTaskId()))) {
131         return true;
132       }
133     }
134     return false;
135   }
136 
137   @Override
138   public String toString() {
139     return "hostname=" + getHostname() +
140         " hostOrIp=" + getHostOrIp() +
141         ", MRtaskID=" + getTaskId() +
142         ", port=" + getPort();
143   }
144 
145   @Override
146   public void readFields(DataInput input) throws IOException {
147     hostname = input.readUTF();
148     hostOrIp = input.readUTF();
149     port = input.readInt();
150     taskId = input.readInt();
151   }
152 
153   @Override
154   public void write(DataOutput output) throws IOException {
155     output.writeUTF(hostname);
156     output.writeUTF(hostOrIp);
157     output.writeInt(port);
158     output.writeInt(taskId);
159   }
160 
161   @Override
162   public int hashCode() {
163     int result = 17;
164     result = 37 * result + getPort();
165     result = 37 * result + hostname.hashCode();
166     result = 37 * result + hostOrIp.hashCode();
167     result = 37 * result + getTaskId();
168     return result;
169   }
170 
171 }