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 static org.apache.hadoop.hbase.trace.TraceUtil.tracedFuture;
021import static org.apache.hadoop.hbase.util.FutureUtils.addListener;
022
023import java.util.Arrays;
024import java.util.List;
025import java.util.concurrent.CompletableFuture;
026import org.apache.hadoop.hbase.ClientMetaTableAccessor;
027import org.apache.hadoop.hbase.HRegionLocation;
028import org.apache.hadoop.hbase.TableName;
029import org.apache.yetus.audience.InterfaceAudience;
030
031/**
032 * The implementation of AsyncRegionLocator.
033 */
034@InterfaceAudience.Private
035class AsyncTableRegionLocatorImpl implements AsyncTableRegionLocator {
036
037  private final TableName tableName;
038
039  private final AsyncConnectionImpl conn;
040
041  public AsyncTableRegionLocatorImpl(TableName tableName, AsyncConnectionImpl conn) {
042    this.tableName = tableName;
043    this.conn = conn;
044  }
045
046  @Override
047  public TableName getName() {
048    return tableName;
049  }
050
051  @Override
052  public CompletableFuture<HRegionLocation> getRegionLocation(byte[] row, int replicaId,
053    boolean reload) {
054    return conn.getLocator().getRegionLocation(tableName, row, replicaId, RegionLocateType.CURRENT,
055      reload, -1L);
056  }
057
058  @Override
059  public CompletableFuture<List<HRegionLocation>> getAllRegionLocations() {
060    return tracedFuture(() -> {
061      if (TableName.isMetaTableName(tableName)) {
062        return conn.registry.getMetaRegionLocations()
063          .thenApply(locs -> Arrays.asList(locs.getRegionLocations()));
064      }
065      CompletableFuture<List<HRegionLocation>> future = ClientMetaTableAccessor
066        .getTableHRegionLocations(conn.getTable(TableName.META_TABLE_NAME), tableName);
067      addListener(future, (locs, error) -> locs.forEach(loc -> {
068        // the cache assumes that all locations have a serverName. only add if that's true
069        if (loc.getServerName() != null) {
070          conn.getLocator().getNonMetaRegionLocator().addLocationToCache(loc);
071        }
072      }));
073      return future;
074    }, getClass().getSimpleName() + ".getAllRegionLocations");
075  }
076
077  @Override
078  public CompletableFuture<List<HRegionLocation>> getRegionLocations(byte[] row, boolean reload) {
079    return conn.getLocator()
080      .getRegionLocations(tableName, row, RegionLocateType.CURRENT, reload, -1L)
081      .thenApply(locs -> Arrays.asList(locs.getRegionLocations()));
082  }
083
084  @Override
085  public void clearRegionLocationCache() {
086    conn.getLocator().clearCache(tableName);
087  }
088}