1/*2 * Licensed to the Apache Software Foundation (ASF) under one3 * or more contributor license agreements. See the NOTICE file4 * distributed with this work for additional information5 * regarding copyright ownership. The ASF licenses this file6 * to you under the Apache License, Version 2.0 (the7 * "License"); you may not use this file except in compliance8 * with the License. You may obtain a copy of the License at9 *10 * http://www.apache.org/licenses/LICENSE-2.011 *12 * Unless required by applicable law or agreed to in writing, software13 * 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 and16 * limitations under the License.17 */1819package org.apache.giraph.graph;
2021import org.apache.hadoop.io.Writable;
2223import java.io.DataInput;
24import java.io.DataOutput;
25import java.io.IOException;
26import java.net.InetSocketAddress;
2728/**29 * Abstract class for information about any task - worker or master.30 */31publicabstractclassTaskInfoimplements Writable {
32/** Task hostname */33private String hostname;
34/** Port that the IPC server is using */35privateint port;
36/** Task partition id */37privateint taskId = -1;
38/** Task host IP */39private String hostOrIp;
4041/**42 * Constructor43 */44publicTaskInfo() {
45 }
4647/**48 * Get this task's hostname49 *50 * @return Hostname51 */52public String getHostname() {
53return hostname.toLowerCase();
54 }
5556/**57 * Get this task's host address. Could be IP.58 *59 * @return host address60 */61public String getHostOrIp() {
62return hostOrIp;
63 }
6465/**66 * Get port that the IPC server of this task is using67 *68 * @return Port69 */70publicint getPort() {
71return port;
72 }
7374/**75 * Set address that the IPC server of this task is using76 *77 * @param address Address78 * @param host host name or IP79 */80publicvoid setInetSocketAddress(InetSocketAddress address, String host) {
81this.port = address.getPort();
82this.hostname = address.getHostName();
83this.hostOrIp = host;
84 }
8586/**87 * Get a new instance of the InetSocketAddress for this hostname and port88 *89 * @return InetSocketAddress of the hostname and port.90 */91public InetSocketAddress getInetSocketAddress() {
92returnnew InetSocketAddress(hostOrIp, port);
93 }
9495/**96 * Set task partition id of this task97 *98 * @param taskId partition id99 */100publicvoid setTaskId(int taskId) {
101this.taskId = taskId;
102 }
103104/**105 * Get task partition id of this task106 *107 * @return Task partition id of this task108 */109publicint getTaskId() {
110return taskId;
111 }
112113/**114 * Get hostname and task id115 *116 * @return Hostname and task id117 */118public String getHostnameId() {
119return getHostname() + "_" + getTaskId();
120 }
121122 @Override
123publicboolean equals(Object other) {
124if (other instanceof TaskInfo) {
125TaskInfo taskInfo = (TaskInfo) other;
126if (getHostname().equals(taskInfo.getHostname()) &&
127 getHostOrIp().equals(taskInfo.getHostOrIp()) &&
128 (getTaskId() == taskInfo.getTaskId()) &&
129 (port == taskInfo.getPort() &&
130 (taskId == taskInfo.getTaskId()))) {
131returntrue;
132 }
133 }
134return false;
135 }
136137 @Override
138public String toString() {
139return"hostname=" + getHostname() +
140" hostOrIp=" + getHostOrIp() +
141", MRtaskID=" + getTaskId() +
142", port=" + getPort();
143 }
144145 @Override
146publicvoid readFields(DataInput input) throws IOException {
147 hostname = input.readUTF();
148 hostOrIp = input.readUTF();
149 port = input.readInt();
150 taskId = input.readInt();
151 }
152153 @Override
154publicvoid write(DataOutput output) throws IOException {
155 output.writeUTF(hostname);
156 output.writeUTF(hostOrIp);
157 output.writeInt(port);
158 output.writeInt(taskId);
159 }
160161 @Override
162publicint hashCode() {
163int result = 17;
164 result = 37 * result + getPort();
165 result = 37 * result + hostname.hashCode();
166 result = 37 * result + hostOrIp.hashCode();
167 result = 37 * result + getTaskId();
168return result;
169 }
170171 }