View Javadoc

1   /**
2    * Copyright 2010 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase;
21  
22  import java.io.DataInput;
23  import java.io.DataOutput;
24  import java.io.IOException;
25  import java.net.InetSocketAddress;
26  
27  import org.apache.hadoop.hbase.regionserver.HRegionServer;
28  import org.apache.hadoop.io.VersionedWritable;
29  import org.apache.hadoop.io.WritableComparable;
30  
31  
32  /**
33   * HServerInfo is meta info about an {@link HRegionServer}.  It hosts the
34   * {@link HServerAddress}, its webui port, and its server startcode.  It was
35   * used to pass meta info about a server across an RPC but we've since made
36   * it so regionserver info is up in ZooKeeper and so this class is on its
37   * way out. It used to carry {@link HServerLoad} but as off HBase 0.92.0, the
38   * HServerLoad is passed independent of this class. Also, we now no longer pass
39   * the webui from regionserver to master (TODO: Fix).
40   * @deprecated Use {@link InetSocketAddress} and or {@link ServerName} and or
41   * {@link HServerLoad}
42   */
43  public class HServerInfo extends VersionedWritable
44  implements WritableComparable<HServerInfo> {
45    private static final byte VERSION = 1;
46    private HServerAddress serverAddress = new HServerAddress();
47    private long startCode;
48    private int webuiport;
49  
50    public HServerInfo() {
51      super();
52    }
53  
54    /**
55     * Constructor that creates a HServerInfo with a generated startcode
56     * @param serverAddress
57     * @param webuiport Port the webui runs on.
58     */
59    public HServerInfo(final HServerAddress serverAddress, final int webuiport) {
60      this(serverAddress, System.currentTimeMillis(), webuiport);
61    }
62  
63    public HServerInfo(HServerAddress serverAddress, long startCode,
64        final int webuiport) {
65      this.serverAddress = serverAddress;
66      this.startCode = startCode;
67      this.webuiport = webuiport;
68    }
69  
70    /**
71     * Copy-constructor
72     * @param other
73     */
74    public HServerInfo(HServerInfo other) {
75      this.serverAddress = new HServerAddress(other.getServerAddress());
76      this.startCode = other.getStartCode();
77      this.webuiport = other.getInfoPort();
78    }
79  
80    /** @return the object version number */
81    public byte getVersion() {
82      return VERSION;
83    }
84  
85    public synchronized HServerAddress getServerAddress() {
86      return new HServerAddress(serverAddress);
87    }
88  
89    public synchronized long getStartCode() {
90      return startCode;
91    }
92  
93    public int getInfoPort() {
94      return getWebuiPort();
95    }
96  
97    public int getWebuiPort() {
98      return this.webuiport;
99    }
100 
101   public String getHostname() {
102     return this.serverAddress.getHostname();
103   }
104 
105   /**
106    * @return ServerName and load concatenated.
107    */
108   @Override
109   public synchronized String toString() {
110     return ServerName.getServerName(this.serverAddress.getHostnameAndPort(),
111       this.startCode);
112   }
113 
114   @Override
115   public boolean equals(Object obj) {
116     if (this == obj) return true;
117     if (obj == null) return false;
118     if (getClass() != obj.getClass()) return false;
119     return compareTo((HServerInfo)obj) == 0;
120   }
121 
122   @Override
123   public int hashCode() {
124     int code = this.serverAddress.hashCode();
125     code ^= this.webuiport;
126     code ^= this.startCode;
127     return code;
128   }
129 
130   public void readFields(DataInput in) throws IOException {
131     super.readFields(in);
132     this.serverAddress.readFields(in);
133     this.startCode = in.readLong();
134     this.webuiport = in.readInt();
135   }
136 
137   public void write(DataOutput out) throws IOException {
138     super.write(out);
139     this.serverAddress.write(out);
140     out.writeLong(this.startCode);
141     out.writeInt(this.webuiport);
142   }
143 
144   public int compareTo(HServerInfo o) {
145     int compare = this.serverAddress.compareTo(o.getServerAddress());
146     if (compare != 0) return compare;
147     if (this.webuiport != o.getInfoPort()) return this.webuiport - o.getInfoPort();
148     if (this.startCode != o.getStartCode()) return (int)(this.startCode - o.getStartCode());
149     return 0;
150   }
151 }