001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.client;
019
020import java.io.IOException;
021import java.util.List;
022import java.util.concurrent.CompletableFuture;
023import java.util.stream.Collectors;
024import org.apache.hadoop.hbase.HRegionLocation;
025import org.apache.hadoop.hbase.TableName;
026import org.apache.hadoop.hbase.util.Pair;
027import org.apache.yetus.audience.InterfaceAudience;
028
029/**
030 * The asynchronous version of RegionLocator.
031 * <p>
032 * Usually the implementations will not throw any exception directly, you need to get the exception
033 * from the returned {@link CompletableFuture}.
034 * @since 2.0.0
035 */
036@InterfaceAudience.Public
037public interface AsyncTableRegionLocator {
038
039  /**
040   * Gets the fully qualified table name instance of the table whose region we want to locate.
041   */
042  TableName getName();
043
044  /**
045   * Finds the region on which the given row is being served. Does not reload the cache.
046   * <p/>
047   * Returns the location of the region to which the row belongs.
048   * @param row Row to find.
049   */
050  default CompletableFuture<HRegionLocation> getRegionLocation(byte[] row) {
051    return getRegionLocation(row, false);
052  }
053
054  /**
055   * Finds the region on which the given row is being served.
056   * <p/>
057   * Returns the location of the region to which the row belongs.
058   * @param row    Row to find.
059   * @param reload true to reload information or false to use cached information
060   */
061  default CompletableFuture<HRegionLocation> getRegionLocation(byte[] row, boolean reload) {
062    return getRegionLocation(row, RegionInfo.DEFAULT_REPLICA_ID, reload);
063  }
064
065  /**
066   * Finds the region with the given <code>replicaId</code> on which the given row is being served.
067   * <p/>
068   * Returns the location of the region with the given <code>replicaId</code> to which the row
069   * belongs.
070   * @param row       Row to find.
071   * @param replicaId the replica id of the region
072   */
073  default CompletableFuture<HRegionLocation> getRegionLocation(byte[] row, int replicaId) {
074    return getRegionLocation(row, replicaId, false);
075  }
076
077  /**
078   * Finds the region with the given <code>replicaId</code> on which the given row is being served.
079   * <p/>
080   * Returns the location of the region with the given <code>replicaId</code> to which the row
081   * belongs.
082   * @param row       Row to find.
083   * @param replicaId the replica id of the region
084   * @param reload    true to reload information or false to use cached information
085   */
086  CompletableFuture<HRegionLocation> getRegionLocation(byte[] row, int replicaId, boolean reload);
087
088  /**
089   * Find all the replicas for the region on which the given row is being served.
090   * @param row Row to find.
091   * @return Locations for all the replicas of the row.
092   */
093  default CompletableFuture<List<HRegionLocation>> getRegionLocations(byte[] row) {
094    return getRegionLocations(row, false);
095  }
096
097  /**
098   * Find all the replicas for the region on which the given row is being served.
099   * @param row    Row to find.
100   * @param reload true to reload information or false to use cached information
101   * @return Locations for all the replicas of the row.
102   */
103  CompletableFuture<List<HRegionLocation>> getRegionLocations(byte[] row, boolean reload);
104
105  /**
106   * Retrieves all of the regions associated with this table.
107   * <p/>
108   * Usually we will go to meta table directly in this method so there is no {@code reload}
109   * parameter.
110   * <p/>
111   * Notice that the location for region replicas other than the default replica are also returned.
112   * @return a {@link List} of all regions associated with this table.
113   */
114  CompletableFuture<List<HRegionLocation>> getAllRegionLocations();
115
116  /**
117   * Gets the starting row key for every region in the currently open table.
118   * <p>
119   * This is mainly useful for the MapReduce integration.
120   * @return Array of region starting row keys
121   * @throws IOException if a remote or network exception occurs
122   */
123  default CompletableFuture<List<byte[]>> getStartKeys() throws IOException {
124    return getStartEndKeys().thenApply(
125      startEndKeys -> startEndKeys.stream().map(Pair::getFirst).collect(Collectors.toList()));
126  }
127
128  /**
129   * Gets the ending row key for every region in the currently open table.
130   * <p>
131   * This is mainly useful for the MapReduce integration.
132   * @return Array of region ending row keys
133   * @throws IOException if a remote or network exception occurs
134   */
135  default CompletableFuture<List<byte[]>> getEndKeys() throws IOException {
136    return getStartEndKeys().thenApply(
137      startEndKeys -> startEndKeys.stream().map(Pair::getSecond).collect(Collectors.toList()));
138  }
139
140  /**
141   * Gets the starting and ending row keys for every region in the currently open table.
142   * <p>
143   * This is mainly useful for the MapReduce integration.
144   * @return Pair of arrays of region starting and ending row keys
145   * @throws IOException if a remote or network exception occurs
146   */
147  default CompletableFuture<List<Pair<byte[], byte[]>>> getStartEndKeys() throws IOException {
148    return getAllRegionLocations().thenApply(
149      locs -> locs.stream().filter(loc -> RegionReplicaUtil.isDefaultReplica(loc.getRegion()))
150        .map(HRegionLocation::getRegion).map(r -> Pair.newPair(r.getStartKey(), r.getEndKey()))
151        .collect(Collectors.toList()));
152  }
153
154  /**
155   * Clear all the entries in the region location cache.
156   * <p/>
157   * This may cause performance issue so use it with caution.
158   */
159  void clearRegionLocationCache();
160}