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  
21  package org.apache.hadoop.hbase.client;
22  
23  import org.apache.hadoop.hbase.HRegionLocation;
24  import org.apache.hadoop.hbase.ipc.HRegionInterface;
25  
26  import java.io.IOException;
27  import java.util.concurrent.Callable;
28  
29  /**
30   * Abstract class that implements Callable, used by retryable actions.
31   * @param <T> the class that the ServerCallable handles
32   */
33  public abstract class ServerCallable<T> implements Callable<T> {
34    protected final HConnection connection;
35    protected final byte [] tableName;
36    protected final byte [] row;
37    protected HRegionLocation location;
38    protected HRegionInterface server;
39  
40    /**
41     * @param connection connection callable is on
42     * @param tableName table name callable is on
43     * @param row row we are querying
44     */
45    public ServerCallable(HConnection connection, byte [] tableName, byte [] row) {
46      this.connection = connection;
47      this.tableName = tableName;
48      this.row = row;
49    }
50  
51    /**
52     *
53     * @param reload set this to true if connection should re-find the region
54     * @throws IOException e
55     */
56    public void instantiateServer(boolean reload) throws IOException {
57      this.location = connection.getRegionLocation(tableName, row, reload);
58      this.server = connection.getHRegionConnection(location.getServerAddress());
59    }
60  
61    /** @return the server name */
62    public String getServerName() {
63      if (location == null) {
64        return null;
65      }
66      return location.getServerAddress().toString();
67    }
68  
69    /** @return the region name */
70    public byte[] getRegionName() {
71      if (location == null) {
72        return null;
73      }
74      return location.getRegionInfo().getRegionName();
75    }
76  
77    /** @return the row */
78    public byte [] getRow() {
79      return row;
80    }
81  }