View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase;
20  
21  import java.io.DataInput;
22  import java.io.DataOutput;
23  import java.io.IOException;
24  import java.util.List;
25  
26  import org.apache.hadoop.hbase.util.Bytes;
27  import org.apache.hadoop.hbase.util.Strings;
28  import org.apache.hadoop.io.VersionedWritable;
29  import org.apache.hadoop.io.WritableComparable;
30  import org.apache.hadoop.io.WritableUtils;
31  
32  /**
33   * This is a class that extends HServerLoad with last sequence IDs.
34   * It enables us to port 6508 w/o breaking compat.
35   */
36  public class HServerLoadWithSeqIds extends VersionedWritable {
37    private static final byte VERSION = 1;
38  
39    private HServerLoad serverLoad;
40  
41    public HServerLoadWithSeqIds() {
42      super();
43    }
44  
45    public HServerLoadWithSeqIds(HServerLoad serverLoad) {
46      super();
47      this.serverLoad = serverLoad;
48    }
49  
50    /** @return the object version number */
51    @Override
52    public byte getVersion() {
53      return VERSION;
54    }
55  
56    public HServerLoad getServerLoad() {
57      return serverLoad;
58    }
59  
60    @Override
61    public void write(DataOutput out) throws IOException {
62      super.write(out);
63      serverLoad.write(out);
64      // HACK: this loop MUST write regions in the same order as HServerLoad.write.
65      for (HServerLoad.RegionLoad rl: serverLoad.regionLoad.values()) {
66        out.writeLong(rl.getCompleteSequenceId());
67      }
68    }
69  
70    @Override
71    public void readFields(DataInput in) throws IOException {
72      super.readFields(in);
73      if (null == serverLoad) {
74        serverLoad = new HServerLoad();
75      }
76      List<byte[]> regionKeys = serverLoad.readFieldsGetRegionKeys(in);
77      for (int i = 0; i < regionKeys.size(); ++i) {
78        long lastSequenceId = in.readLong();
79        HServerLoad.RegionLoad rl = serverLoad.regionLoad.get(regionKeys.get(i));
80        rl.setCompleteSequenceId(lastSequenceId);
81      }
82    }
83  }