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.io.IOException; 024import java.util.Arrays; 025import java.util.List; 026import java.util.concurrent.CompletableFuture; 027import org.apache.hadoop.hbase.ClientMetaTableAccessor; 028import org.apache.hadoop.hbase.HConstants; 029import org.apache.hadoop.hbase.HRegionLocation; 030import org.apache.hadoop.hbase.TableName; 031import org.apache.yetus.audience.InterfaceAudience; 032 033/** 034 * The implementation of AsyncRegionLocator. 035 */ 036@InterfaceAudience.Private 037class AsyncTableRegionLocatorImpl implements AsyncTableRegionLocator { 038 039 private final TableName tableName; 040 041 private final AsyncConnectionImpl conn; 042 043 public AsyncTableRegionLocatorImpl(TableName tableName, AsyncConnectionImpl conn) { 044 this.tableName = tableName; 045 this.conn = conn; 046 } 047 048 @Override 049 public TableName getName() { 050 return tableName; 051 } 052 053 @Override 054 public CompletableFuture<HRegionLocation> getRegionLocation(byte[] row, int replicaId, 055 boolean reload) { 056 return conn.getLocator().getRegionLocation(tableName, row, replicaId, RegionLocateType.CURRENT, 057 reload, -1L); 058 } 059 060 @Override 061 public CompletableFuture<List<HRegionLocation>> getAllRegionLocations() { 062 return tracedFuture(() -> { 063 if (TableName.isMetaTableName(tableName)) { 064 return conn.registry.getMetaRegionLocations() 065 .thenApply(locs -> Arrays.asList(locs.getRegionLocations())); 066 } 067 CompletableFuture<List<HRegionLocation>> future = ClientMetaTableAccessor 068 .getTableHRegionLocations(conn.getTable(TableName.META_TABLE_NAME), tableName); 069 addListener(future, (locs, error) -> locs.forEach(loc -> { 070 // the cache assumes that all locations have a serverName. only add if that's true 071 if (loc.getServerName() != null) { 072 conn.getLocator().getNonMetaRegionLocator().addLocationToCache(loc); 073 } 074 })); 075 return future; 076 }, getClass().getSimpleName() + ".getAllRegionLocations"); 077 } 078 079 @Override 080 public CompletableFuture<List<HRegionLocation>> getRegionLocations(byte[] row, boolean reload) { 081 return conn.getLocator() 082 .getRegionLocations(tableName, row, RegionLocateType.CURRENT, reload, -1L) 083 .thenApply(locs -> Arrays.asList(locs.getRegionLocations())); 084 } 085 086 @Override 087 public CompletableFuture<List<HRegionLocation>> getRegionLocationsPage(byte[] startKey, 088 int limit) { 089 return tracedFuture(() -> { 090 if (TableName.isMetaTableName(tableName)) { 091 CompletableFuture<List<HRegionLocation>> failed = new CompletableFuture<>(); 092 failed.completeExceptionally( 093 new IOException("getRegionLocationsPage(startKey, limit) is not supported for hbase:meta;" 094 + " use getRegionLocation(EMPTY_START_ROW) instead.")); 095 return failed; 096 } 097 int effectiveLimit = limit > 0 098 ? limit 099 : conn.getConfiguration().getInt(HConstants.HBASE_META_SCANNER_CACHING, 100 HConstants.DEFAULT_HBASE_META_SCANNER_CACHING); 101 CompletableFuture<List<HRegionLocation>> future = 102 ClientMetaTableAccessor.getTableHRegionLocations(conn.getTable(TableName.META_TABLE_NAME), 103 tableName, startKey, effectiveLimit); 104 addListener(future, (locs, error) -> { 105 if (error != null || locs == null) { 106 return; 107 } 108 for (HRegionLocation loc : locs) { 109 // the cache assumes that all locations have a serverName. only add if that's true 110 if (loc.getServerName() != null) { 111 conn.getLocator().getNonMetaRegionLocator().addLocationToCache(loc); 112 } 113 } 114 }); 115 return future; 116 }, getClass().getSimpleName() + ".getRegionLocationsPage"); 117 } 118 119 @Override 120 public void clearRegionLocationCache() { 121 conn.getLocator().clearCache(tableName); 122 } 123}