Apache9 commented on a change in pull request #2584:
URL: https://github.com/apache/hbase/pull/2584#discussion_r512682721
##########
File path:
hbase-client/src/main/java/org/apache/hadoop/hbase/client/AsyncConnectionImpl.java
##########
@@ -181,9 +185,18 @@ public void newDead(ServerName sn) {
this.clusterStatusListener = listener;
}
+ public void startChoreService() {
+ if (this.choreService == null) {
+ choreService = new ChoreService("AsyncConn Chore Service");
+ }
+ }
+
private void spawnRenewalChore(final UserGroupInformation user) {
- authService = new ChoreService("Relogin service");
- authService.scheduleChore(AuthUtil.getAuthRenewalChore(user));
+ choreService.scheduleChore(AuthUtil.getAuthRenewalChore(user));
+ }
+
+ public ChoreService getChoreService() {
Review comment:
Could this be private or at least package private?
##########
File path:
hbase-client/src/main/java/org/apache/hadoop/hbase/client/AsyncConnectionImpl.java
##########
@@ -181,9 +185,18 @@ public void newDead(ServerName sn) {
this.clusterStatusListener = listener;
}
+ public void startChoreService() {
Review comment:
I do not think we need this method? Just do the initialization work in
the getChoreService method?
##########
File path:
hbase-client/src/main/java/org/apache/hadoop/hbase/client/CatalogReplicaLoadBalanceReplicaSelector.java
##########
@@ -0,0 +1,46 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hadoop.hbase.client;
+
+import org.apache.hadoop.hbase.HRegionLocation;
+import org.apache.hadoop.hbase.TableName;
+import org.apache.yetus.audience.InterfaceAudience;
+
+/**
+ * A Catalog replica selector decides which catalog replica to go for read
requests.
+ */
[email protected]
+public interface CatalogReplicaLoadBalanceReplicaSelector {
+
+ /**
+ * This method is called upon when input location is stale.
+ * @param loc location which is stale.
+ * @param fromReplicaId which replica the stale location comes from.
+ */
+ void notifyOnError(HRegionLocation loc, int fromReplicaId);
Review comment:
Do we need the exception?
##########
File path:
hbase-client/src/main/java/org/apache/hadoop/hbase/client/CatalogReplicaLoadBalanceReplicaSimpleSelector.java
##########
@@ -0,0 +1,273 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hadoop.hbase.client;
+
+import static org.apache.hadoop.hbase.client.ConnectionUtils.isEmptyStopRow;
+import static org.apache.hadoop.hbase.util.Bytes.BYTES_COMPARATOR;
+import static org.apache.hadoop.hbase.util.ConcurrentMapUtils.computeIfAbsent;
+import java.io.IOException;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.ConcurrentNavigableMap;
+import java.util.concurrent.ConcurrentSkipListMap;
+import java.util.concurrent.ThreadLocalRandom;
+import org.apache.commons.lang3.builder.ToStringBuilder;
+import org.apache.hadoop.hbase.HRegionLocation;
+import org.apache.hadoop.hbase.ScheduledChore;
+import org.apache.hadoop.hbase.Stoppable;
+import org.apache.hadoop.hbase.TableName;
+import org.apache.hadoop.hbase.util.Bytes;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * CatalogReplicaLoadBalanceReplicaSimpleSelector implements a simple catalog
replica load balancing
+ * algorithm. It maintains a stale location cache for each table. Whenever
client looks up location,
+ * it first check if the row is the stale location cache. If yes, the location
from
+ * catalog replica is stale, it will go to the primary region to look up
update-to-date location;
+ * otherwise, it will randomly pick up a replica region for lookup. When
clients receive
+ * RegionNotServedException from region servers, it will add these region
locations to the stale
+ * location cache. The stale cache will be cleaned up periodically by a chore.
+ *
+ * It follows a simple algorithm to choose a replica to go:
+ *
+ * 1. If there is no stale location entry for rows it looks up, it will
randomly
+ * pick a replica region to do lookup.
+ * 2. If the location from the replica region is stale, client gets
RegionNotServedException
+ * from region server, in this case, it will create
StaleLocationCacheEntry in
+ * CatalogReplicaLoadBalanceReplicaSimpleSelector.
+ * 3. When client tries to do location lookup, it checks StaleLocationCache
first for rows it
+ * tries to lookup, if entry exists, it will go with primary meta region
to do lookup;
+ * otherwise, it will follow step 1.
+ * 4. A chore will periodically run to clean up cache entries in the
StaleLocationCache.
+ */
+class CatalogReplicaLoadBalanceReplicaSimpleSelector implements
+ CatalogReplicaLoadBalanceReplicaSelector {
+ private static final Logger LOG =
+
LoggerFactory.getLogger(CatalogReplicaLoadBalanceReplicaSimpleSelector.class);
+ private final long STALE_CACHE_TIMEOUT_IN_MILLISECONDS = 3000; // 3 seconds
+ private final int STALE_CACHE_CLEAN_CHORE_INTERVAL = 1500; // 1.5 seconds
Review comment:
IN_MILLISECONDS?
##########
File path:
hbase-client/src/main/java/org/apache/hadoop/hbase/client/CatalogReplicaLoadBalanceReplicaSimpleSelector.java
##########
@@ -0,0 +1,273 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hadoop.hbase.client;
+
+import static org.apache.hadoop.hbase.client.ConnectionUtils.isEmptyStopRow;
+import static org.apache.hadoop.hbase.util.Bytes.BYTES_COMPARATOR;
+import static org.apache.hadoop.hbase.util.ConcurrentMapUtils.computeIfAbsent;
+import java.io.IOException;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.ConcurrentNavigableMap;
+import java.util.concurrent.ConcurrentSkipListMap;
+import java.util.concurrent.ThreadLocalRandom;
+import org.apache.commons.lang3.builder.ToStringBuilder;
+import org.apache.hadoop.hbase.HRegionLocation;
+import org.apache.hadoop.hbase.ScheduledChore;
+import org.apache.hadoop.hbase.Stoppable;
+import org.apache.hadoop.hbase.TableName;
+import org.apache.hadoop.hbase.util.Bytes;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * CatalogReplicaLoadBalanceReplicaSimpleSelector implements a simple catalog
replica load balancing
+ * algorithm. It maintains a stale location cache for each table. Whenever
client looks up location,
+ * it first check if the row is the stale location cache. If yes, the location
from
+ * catalog replica is stale, it will go to the primary region to look up
update-to-date location;
+ * otherwise, it will randomly pick up a replica region for lookup. When
clients receive
+ * RegionNotServedException from region servers, it will add these region
locations to the stale
+ * location cache. The stale cache will be cleaned up periodically by a chore.
+ *
+ * It follows a simple algorithm to choose a replica to go:
+ *
+ * 1. If there is no stale location entry for rows it looks up, it will
randomly
+ * pick a replica region to do lookup.
+ * 2. If the location from the replica region is stale, client gets
RegionNotServedException
+ * from region server, in this case, it will create
StaleLocationCacheEntry in
+ * CatalogReplicaLoadBalanceReplicaSimpleSelector.
+ * 3. When client tries to do location lookup, it checks StaleLocationCache
first for rows it
+ * tries to lookup, if entry exists, it will go with primary meta region
to do lookup;
+ * otherwise, it will follow step 1.
+ * 4. A chore will periodically run to clean up cache entries in the
StaleLocationCache.
+ */
+class CatalogReplicaLoadBalanceReplicaSimpleSelector implements
+ CatalogReplicaLoadBalanceReplicaSelector {
+ private static final Logger LOG =
+
LoggerFactory.getLogger(CatalogReplicaLoadBalanceReplicaSimpleSelector.class);
+ private final long STALE_CACHE_TIMEOUT_IN_MILLISECONDS = 3000; // 3 seconds
+ private final int STALE_CACHE_CLEAN_CHORE_INTERVAL = 1500; // 1.5 seconds
+
+ /**
+ * StaleLocationCacheEntry is the entry when a stale location is reported by
an client.
+ */
+ private static final class StaleLocationCacheEntry {
+ // replica id where the stale location comes from.
+ private int fromReplicaId;
+
+ // timestamp in milliseconds
+ private long timestamp;
+
+ private byte[] endKey;
+
+ StaleLocationCacheEntry(final int metaReplicaId, final byte[] endKey) {
+ this.fromReplicaId = metaReplicaId;
+ this.endKey = endKey;
+ timestamp = System.currentTimeMillis();
+ }
+
+ public byte[] getEndKey() {
+ return this.endKey;
+ }
+
+ public int getFromReplicaId() {
+ return this.fromReplicaId;
+ }
+ public long getTimestamp() {
+ return this.timestamp;
+ }
+
+ @Override
+ public String toString() {
+ return new ToStringBuilder(this)
+ .append("endKey", endKey)
+ .append("fromReplicaId", fromReplicaId)
+ .append("timestamp", timestamp)
+ .toString();
+ }
+ }
+
+ private static final class StaleTableCache {
+ private final ConcurrentNavigableMap<byte[], StaleLocationCacheEntry>
cache =
+ new ConcurrentSkipListMap<>(BYTES_COMPARATOR);
+ }
+
+ private final ConcurrentMap<TableName, StaleTableCache> staleCache;
+ private int numOfReplicas;
+ private final AsyncConnectionImpl conn;
+ private TableName tableName;
+
+ CatalogReplicaLoadBalanceReplicaSimpleSelector(TableName tableName,
AsyncConnectionImpl conn) {
+ staleCache = new ConcurrentHashMap<>();
+ this.conn = conn;
+ this.tableName = tableName;
+
+ // This numOfReplicas is going to be lazy initialized.
+ this.numOfReplicas = -1;
+ // Start connection's chore service in case.
+ this.conn.startChoreService();
+ this.conn.getChoreService().scheduleChore(getCacheCleanupChore(this));
+ }
+
+ /**
+ * When a client runs into RegionNotServingException, it will call this
method to
+ * update Selector's internal state.
+ * @param loc the location which causes exception.
+ * @param fromReplicaId the replica id where the stale location comes from.
+ */
+ public void notifyOnError(HRegionLocation loc, int fromReplicaId) {
+ StaleTableCache tableCache =
+ computeIfAbsent(staleCache, loc.getRegion().getTable(),
StaleTableCache::new);
+ byte[] startKey = loc.getRegion().getStartKey();
+ tableCache.cache.putIfAbsent(startKey,
+ new StaleLocationCacheEntry(fromReplicaId, loc.getRegion().getEndKey()));
+ LOG.debug("Add entry to stale cache for table {} with startKey {}, {}",
+ loc.getRegion().getTable(), startKey, loc.getRegion().getEndKey());
+ }
+
+ private int getRandomNonPrimaryReplicaId() {
+ if (numOfReplicas < 0) {
+ try {
+ Admin admin = conn.toConnection().getAdmin();
+ TableDescriptor tableDescriptor = admin.getDescriptor(tableName);
+ this.numOfReplicas = tableDescriptor.getRegionReplication();
+ } catch (IOException ioe) {
+ LOG.error("Failed to get table {}'s region replication, exception ",
tableName, ioe);
+ this.numOfReplicas = 1;
+ }
+ }
+ // In case of no replica configured, return the primary region id.
+ if (this.numOfReplicas <= 1) {
Review comment:
Then the method name should be changed? Here we return the primary
replica id, not 'nonPrimaryReplicaId'
##########
File path:
hbase-client/src/main/java/org/apache/hadoop/hbase/client/CatalogReplicaLoadBalanceReplicaSimpleSelector.java
##########
@@ -0,0 +1,273 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hadoop.hbase.client;
+
+import static org.apache.hadoop.hbase.client.ConnectionUtils.isEmptyStopRow;
+import static org.apache.hadoop.hbase.util.Bytes.BYTES_COMPARATOR;
+import static org.apache.hadoop.hbase.util.ConcurrentMapUtils.computeIfAbsent;
+import java.io.IOException;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.ConcurrentNavigableMap;
+import java.util.concurrent.ConcurrentSkipListMap;
+import java.util.concurrent.ThreadLocalRandom;
+import org.apache.commons.lang3.builder.ToStringBuilder;
+import org.apache.hadoop.hbase.HRegionLocation;
+import org.apache.hadoop.hbase.ScheduledChore;
+import org.apache.hadoop.hbase.Stoppable;
+import org.apache.hadoop.hbase.TableName;
+import org.apache.hadoop.hbase.util.Bytes;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * CatalogReplicaLoadBalanceReplicaSimpleSelector implements a simple catalog
replica load balancing
+ * algorithm. It maintains a stale location cache for each table. Whenever
client looks up location,
+ * it first check if the row is the stale location cache. If yes, the location
from
+ * catalog replica is stale, it will go to the primary region to look up
update-to-date location;
+ * otherwise, it will randomly pick up a replica region for lookup. When
clients receive
+ * RegionNotServedException from region servers, it will add these region
locations to the stale
+ * location cache. The stale cache will be cleaned up periodically by a chore.
+ *
+ * It follows a simple algorithm to choose a replica to go:
+ *
+ * 1. If there is no stale location entry for rows it looks up, it will
randomly
+ * pick a replica region to do lookup.
+ * 2. If the location from the replica region is stale, client gets
RegionNotServedException
+ * from region server, in this case, it will create
StaleLocationCacheEntry in
+ * CatalogReplicaLoadBalanceReplicaSimpleSelector.
+ * 3. When client tries to do location lookup, it checks StaleLocationCache
first for rows it
+ * tries to lookup, if entry exists, it will go with primary meta region
to do lookup;
+ * otherwise, it will follow step 1.
+ * 4. A chore will periodically run to clean up cache entries in the
StaleLocationCache.
+ */
+class CatalogReplicaLoadBalanceReplicaSimpleSelector implements
+ CatalogReplicaLoadBalanceReplicaSelector {
+ private static final Logger LOG =
+
LoggerFactory.getLogger(CatalogReplicaLoadBalanceReplicaSimpleSelector.class);
+ private final long STALE_CACHE_TIMEOUT_IN_MILLISECONDS = 3000; // 3 seconds
+ private final int STALE_CACHE_CLEAN_CHORE_INTERVAL = 1500; // 1.5 seconds
+
+ /**
+ * StaleLocationCacheEntry is the entry when a stale location is reported by
an client.
+ */
+ private static final class StaleLocationCacheEntry {
+ // replica id where the stale location comes from.
+ private int fromReplicaId;
+
+ // timestamp in milliseconds
+ private long timestamp;
+
+ private byte[] endKey;
+
+ StaleLocationCacheEntry(final int metaReplicaId, final byte[] endKey) {
+ this.fromReplicaId = metaReplicaId;
+ this.endKey = endKey;
+ timestamp = System.currentTimeMillis();
+ }
+
+ public byte[] getEndKey() {
+ return this.endKey;
+ }
+
+ public int getFromReplicaId() {
+ return this.fromReplicaId;
+ }
+ public long getTimestamp() {
+ return this.timestamp;
+ }
+
+ @Override
+ public String toString() {
+ return new ToStringBuilder(this)
+ .append("endKey", endKey)
+ .append("fromReplicaId", fromReplicaId)
+ .append("timestamp", timestamp)
+ .toString();
+ }
+ }
+
+ private static final class StaleTableCache {
+ private final ConcurrentNavigableMap<byte[], StaleLocationCacheEntry>
cache =
+ new ConcurrentSkipListMap<>(BYTES_COMPARATOR);
+ }
+
+ private final ConcurrentMap<TableName, StaleTableCache> staleCache;
+ private int numOfReplicas;
+ private final AsyncConnectionImpl conn;
+ private TableName tableName;
+
+ CatalogReplicaLoadBalanceReplicaSimpleSelector(TableName tableName,
AsyncConnectionImpl conn) {
+ staleCache = new ConcurrentHashMap<>();
+ this.conn = conn;
+ this.tableName = tableName;
+
+ // This numOfReplicas is going to be lazy initialized.
+ this.numOfReplicas = -1;
+ // Start connection's chore service in case.
+ this.conn.startChoreService();
+ this.conn.getChoreService().scheduleChore(getCacheCleanupChore(this));
+ }
+
+ /**
+ * When a client runs into RegionNotServingException, it will call this
method to
+ * update Selector's internal state.
+ * @param loc the location which causes exception.
+ * @param fromReplicaId the replica id where the stale location comes from.
+ */
+ public void notifyOnError(HRegionLocation loc, int fromReplicaId) {
+ StaleTableCache tableCache =
+ computeIfAbsent(staleCache, loc.getRegion().getTable(),
StaleTableCache::new);
+ byte[] startKey = loc.getRegion().getStartKey();
+ tableCache.cache.putIfAbsent(startKey,
+ new StaleLocationCacheEntry(fromReplicaId, loc.getRegion().getEndKey()));
+ LOG.debug("Add entry to stale cache for table {} with startKey {}, {}",
+ loc.getRegion().getTable(), startKey, loc.getRegion().getEndKey());
+ }
+
+ private int getRandomNonPrimaryReplicaId() {
+ if (numOfReplicas < 0) {
+ try {
+ Admin admin = conn.toConnection().getAdmin();
+ TableDescriptor tableDescriptor = admin.getDescriptor(tableName);
+ this.numOfReplicas = tableDescriptor.getRegionReplication();
+ } catch (IOException ioe) {
+ LOG.error("Failed to get table {}'s region replication, exception ",
tableName, ioe);
+ this.numOfReplicas = 1;
+ }
+ }
+ // In case of no replica configured, return the primary region id.
+ if (this.numOfReplicas <= 1) {
+ return RegionInfo.DEFAULT_REPLICA_ID;
+ }
+ return 1 + ThreadLocalRandom.current().nextInt(this.numOfReplicas - 1);
+ }
+
+ /**
+ * When it looks up a location, it will call this method to find a replica
replica region to go.
+ * For a normal case, > 99% of region locations from catalog/meta replica
will be up to date.
+ * In extreme cases such as region server crashes, it will depends on how
fast replication
+ * catches up.
+ *
+ * @param tablename table name it looks up
+ * @param row key it looks up.
+ * @param locateType locateType, Only BEFORE and CURRENT will be passed in.
+ * @return catalog replica id
+ */
+ public int selectReplica(final TableName tablename, final byte[] row,
+ final RegionLocateType locateType) {
+ StaleTableCache tableCache = staleCache.get(tablename);
+
+ // If there is no entry in StaleCache, select a random replica id.
+ if (tableCache == null) {
+ return getRandomNonPrimaryReplicaId();
+ }
+
+ Map.Entry<byte[], StaleLocationCacheEntry> entry;
+ boolean isEmptyStopRow = isEmptyStopRow(row);
+ // Only BEFORE and CURRENT are passed in.
+ if (locateType == RegionLocateType.BEFORE) {
+ entry = isEmptyStopRow ? tableCache.cache.lastEntry() :
tableCache.cache.lowerEntry(row);
+ } else {
+ entry = tableCache.cache.floorEntry(row);
+ }
+
+ // It is not in the stale cache, return a random replica id.
+ if (entry == null) {
+ return getRandomNonPrimaryReplicaId();
+ }
+
+ // Check if the entry times out.
+ if ((System.currentTimeMillis() - entry.getValue().getTimestamp()) >=
+ STALE_CACHE_TIMEOUT_IN_MILLISECONDS) {
+ LOG.debug("Entry for table {} with startKey {}, {} times out",
tablename, entry.getKey(),
+ entry);
+ tableCache.cache.remove(entry.getKey());
+ return getRandomNonPrimaryReplicaId();
+ }
+
+ byte[] endKey = entry.getValue().getEndKey();
+
+ // The following logic is borrowed from AsyncNonMetaRegionLocator.
+ if (isEmptyStopRow(endKey)) {
Review comment:
I think the below checks should be moved before the 'Check if the entry
times out' check? Before the below checks, you do not know whether it is the
location you are looking for...
##########
File path:
hbase-client/src/main/java/org/apache/hadoop/hbase/client/CatalogReplicaLoadBalanceReplicaSelector.java
##########
@@ -0,0 +1,46 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hadoop.hbase.client;
+
+import org.apache.hadoop.hbase.HRegionLocation;
+import org.apache.hadoop.hbase.TableName;
+import org.apache.yetus.audience.InterfaceAudience;
+
+/**
+ * A Catalog replica selector decides which catalog replica to go for read
requests.
+ */
[email protected]
+public interface CatalogReplicaLoadBalanceReplicaSelector {
+
+ /**
+ * This method is called upon when input location is stale.
Review comment:
nit: an unnecessary space at the front of the line?
##########
File path:
hbase-client/src/main/java/org/apache/hadoop/hbase/client/CatalogReplicaLoadBalanceReplicaSimpleSelector.java
##########
@@ -0,0 +1,273 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hadoop.hbase.client;
+
+import static org.apache.hadoop.hbase.client.ConnectionUtils.isEmptyStopRow;
+import static org.apache.hadoop.hbase.util.Bytes.BYTES_COMPARATOR;
+import static org.apache.hadoop.hbase.util.ConcurrentMapUtils.computeIfAbsent;
+import java.io.IOException;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.ConcurrentNavigableMap;
+import java.util.concurrent.ConcurrentSkipListMap;
+import java.util.concurrent.ThreadLocalRandom;
+import org.apache.commons.lang3.builder.ToStringBuilder;
+import org.apache.hadoop.hbase.HRegionLocation;
+import org.apache.hadoop.hbase.ScheduledChore;
+import org.apache.hadoop.hbase.Stoppable;
+import org.apache.hadoop.hbase.TableName;
+import org.apache.hadoop.hbase.util.Bytes;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * CatalogReplicaLoadBalanceReplicaSimpleSelector implements a simple catalog
replica load balancing
+ * algorithm. It maintains a stale location cache for each table. Whenever
client looks up location,
+ * it first check if the row is the stale location cache. If yes, the location
from
+ * catalog replica is stale, it will go to the primary region to look up
update-to-date location;
+ * otherwise, it will randomly pick up a replica region for lookup. When
clients receive
+ * RegionNotServedException from region servers, it will add these region
locations to the stale
+ * location cache. The stale cache will be cleaned up periodically by a chore.
+ *
Review comment:
Please use `</p>`, `<ol>`, `<li>` to rewrite the javadoc so it will not
be incorrected formatted by IDE.
##########
File path:
hbase-client/src/main/java/org/apache/hadoop/hbase/client/CatalogReplicaLoadBalanceReplicaSelector.java
##########
@@ -0,0 +1,46 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hadoop.hbase.client;
+
+import org.apache.hadoop.hbase.HRegionLocation;
+import org.apache.hadoop.hbase.TableName;
+import org.apache.yetus.audience.InterfaceAudience;
+
+/**
+ * A Catalog replica selector decides which catalog replica to go for read
requests.
+ */
[email protected]
+public interface CatalogReplicaLoadBalanceReplicaSelector {
+
+ /**
+ * This method is called upon when input location is stale.
+ * @param loc location which is stale.
+ * @param fromReplicaId which replica the stale location comes from.
+ */
+ void notifyOnError(HRegionLocation loc, int fromReplicaId);
+
+ /**
+ * Select a catalog replica region to loop up the input row.
+ *
+ * @param tablename table name.
+ * @param row key to look up
+ * @param locateType locate type.
+ * @return
Review comment:
nit: remove empty javadoc element.
##########
File path:
hbase-client/src/main/java/org/apache/hadoop/hbase/client/CatalogReplicaLoadBalanceReplicaSelectorFactory.java
##########
@@ -0,0 +1,49 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hadoop.hbase.client;
+
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.hbase.TableName;
+import org.apache.hadoop.hbase.util.ReflectionUtils;
+import org.apache.yetus.audience.InterfaceAudience;
+
+/**
+ * Factory to create a {@link CatalogReplicaLoadBalanceReplicaSelector}
+ */
[email protected]
+public final class CatalogReplicaLoadBalanceReplicaSelectorFactory {
Review comment:
Is it possible to make it package private?
##########
File path:
hbase-client/src/main/java/org/apache/hadoop/hbase/client/CatalogReplicaLoadBalanceReplicaSimpleSelector.java
##########
@@ -0,0 +1,273 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hadoop.hbase.client;
+
+import static org.apache.hadoop.hbase.client.ConnectionUtils.isEmptyStopRow;
+import static org.apache.hadoop.hbase.util.Bytes.BYTES_COMPARATOR;
+import static org.apache.hadoop.hbase.util.ConcurrentMapUtils.computeIfAbsent;
+import java.io.IOException;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.ConcurrentNavigableMap;
+import java.util.concurrent.ConcurrentSkipListMap;
+import java.util.concurrent.ThreadLocalRandom;
+import org.apache.commons.lang3.builder.ToStringBuilder;
+import org.apache.hadoop.hbase.HRegionLocation;
+import org.apache.hadoop.hbase.ScheduledChore;
+import org.apache.hadoop.hbase.Stoppable;
+import org.apache.hadoop.hbase.TableName;
+import org.apache.hadoop.hbase.util.Bytes;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * CatalogReplicaLoadBalanceReplicaSimpleSelector implements a simple catalog
replica load balancing
+ * algorithm. It maintains a stale location cache for each table. Whenever
client looks up location,
+ * it first check if the row is the stale location cache. If yes, the location
from
+ * catalog replica is stale, it will go to the primary region to look up
update-to-date location;
+ * otherwise, it will randomly pick up a replica region for lookup. When
clients receive
+ * RegionNotServedException from region servers, it will add these region
locations to the stale
+ * location cache. The stale cache will be cleaned up periodically by a chore.
+ *
+ * It follows a simple algorithm to choose a replica to go:
+ *
+ * 1. If there is no stale location entry for rows it looks up, it will
randomly
+ * pick a replica region to do lookup.
+ * 2. If the location from the replica region is stale, client gets
RegionNotServedException
+ * from region server, in this case, it will create
StaleLocationCacheEntry in
+ * CatalogReplicaLoadBalanceReplicaSimpleSelector.
+ * 3. When client tries to do location lookup, it checks StaleLocationCache
first for rows it
+ * tries to lookup, if entry exists, it will go with primary meta region
to do lookup;
+ * otherwise, it will follow step 1.
+ * 4. A chore will periodically run to clean up cache entries in the
StaleLocationCache.
+ */
+class CatalogReplicaLoadBalanceReplicaSimpleSelector implements
+ CatalogReplicaLoadBalanceReplicaSelector {
+ private static final Logger LOG =
+
LoggerFactory.getLogger(CatalogReplicaLoadBalanceReplicaSimpleSelector.class);
+ private final long STALE_CACHE_TIMEOUT_IN_MILLISECONDS = 3000; // 3 seconds
+ private final int STALE_CACHE_CLEAN_CHORE_INTERVAL = 1500; // 1.5 seconds
+
+ /**
+ * StaleLocationCacheEntry is the entry when a stale location is reported by
an client.
+ */
+ private static final class StaleLocationCacheEntry {
+ // replica id where the stale location comes from.
+ private int fromReplicaId;
+
+ // timestamp in milliseconds
+ private long timestamp;
+
+ private byte[] endKey;
+
+ StaleLocationCacheEntry(final int metaReplicaId, final byte[] endKey) {
+ this.fromReplicaId = metaReplicaId;
+ this.endKey = endKey;
+ timestamp = System.currentTimeMillis();
Review comment:
Use EnviromentEdge so it will be easier for testing?
##########
File path:
hbase-client/src/main/java/org/apache/hadoop/hbase/client/CatalogReplicaLoadBalanceReplicaSimpleSelector.java
##########
@@ -0,0 +1,273 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hadoop.hbase.client;
+
+import static org.apache.hadoop.hbase.client.ConnectionUtils.isEmptyStopRow;
+import static org.apache.hadoop.hbase.util.Bytes.BYTES_COMPARATOR;
+import static org.apache.hadoop.hbase.util.ConcurrentMapUtils.computeIfAbsent;
+import java.io.IOException;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.ConcurrentNavigableMap;
+import java.util.concurrent.ConcurrentSkipListMap;
+import java.util.concurrent.ThreadLocalRandom;
+import org.apache.commons.lang3.builder.ToStringBuilder;
+import org.apache.hadoop.hbase.HRegionLocation;
+import org.apache.hadoop.hbase.ScheduledChore;
+import org.apache.hadoop.hbase.Stoppable;
+import org.apache.hadoop.hbase.TableName;
+import org.apache.hadoop.hbase.util.Bytes;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * CatalogReplicaLoadBalanceReplicaSimpleSelector implements a simple catalog
replica load balancing
+ * algorithm. It maintains a stale location cache for each table. Whenever
client looks up location,
+ * it first check if the row is the stale location cache. If yes, the location
from
+ * catalog replica is stale, it will go to the primary region to look up
update-to-date location;
+ * otherwise, it will randomly pick up a replica region for lookup. When
clients receive
+ * RegionNotServedException from region servers, it will add these region
locations to the stale
+ * location cache. The stale cache will be cleaned up periodically by a chore.
+ *
+ * It follows a simple algorithm to choose a replica to go:
+ *
+ * 1. If there is no stale location entry for rows it looks up, it will
randomly
+ * pick a replica region to do lookup.
+ * 2. If the location from the replica region is stale, client gets
RegionNotServedException
+ * from region server, in this case, it will create
StaleLocationCacheEntry in
+ * CatalogReplicaLoadBalanceReplicaSimpleSelector.
+ * 3. When client tries to do location lookup, it checks StaleLocationCache
first for rows it
+ * tries to lookup, if entry exists, it will go with primary meta region
to do lookup;
+ * otherwise, it will follow step 1.
+ * 4. A chore will periodically run to clean up cache entries in the
StaleLocationCache.
+ */
+class CatalogReplicaLoadBalanceReplicaSimpleSelector implements
+ CatalogReplicaLoadBalanceReplicaSelector {
+ private static final Logger LOG =
+
LoggerFactory.getLogger(CatalogReplicaLoadBalanceReplicaSimpleSelector.class);
+ private final long STALE_CACHE_TIMEOUT_IN_MILLISECONDS = 3000; // 3 seconds
Review comment:
Not configurable?
##########
File path:
hbase-client/src/main/java/org/apache/hadoop/hbase/client/CatalogReplicaLoadBalanceReplicaSimpleSelector.java
##########
@@ -0,0 +1,273 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hadoop.hbase.client;
+
+import static org.apache.hadoop.hbase.client.ConnectionUtils.isEmptyStopRow;
+import static org.apache.hadoop.hbase.util.Bytes.BYTES_COMPARATOR;
+import static org.apache.hadoop.hbase.util.ConcurrentMapUtils.computeIfAbsent;
+import java.io.IOException;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.ConcurrentNavigableMap;
+import java.util.concurrent.ConcurrentSkipListMap;
+import java.util.concurrent.ThreadLocalRandom;
+import org.apache.commons.lang3.builder.ToStringBuilder;
+import org.apache.hadoop.hbase.HRegionLocation;
+import org.apache.hadoop.hbase.ScheduledChore;
+import org.apache.hadoop.hbase.Stoppable;
+import org.apache.hadoop.hbase.TableName;
+import org.apache.hadoop.hbase.util.Bytes;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * CatalogReplicaLoadBalanceReplicaSimpleSelector implements a simple catalog
replica load balancing
+ * algorithm. It maintains a stale location cache for each table. Whenever
client looks up location,
+ * it first check if the row is the stale location cache. If yes, the location
from
+ * catalog replica is stale, it will go to the primary region to look up
update-to-date location;
+ * otherwise, it will randomly pick up a replica region for lookup. When
clients receive
+ * RegionNotServedException from region servers, it will add these region
locations to the stale
+ * location cache. The stale cache will be cleaned up periodically by a chore.
+ *
+ * It follows a simple algorithm to choose a replica to go:
+ *
+ * 1. If there is no stale location entry for rows it looks up, it will
randomly
+ * pick a replica region to do lookup.
+ * 2. If the location from the replica region is stale, client gets
RegionNotServedException
+ * from region server, in this case, it will create
StaleLocationCacheEntry in
+ * CatalogReplicaLoadBalanceReplicaSimpleSelector.
+ * 3. When client tries to do location lookup, it checks StaleLocationCache
first for rows it
+ * tries to lookup, if entry exists, it will go with primary meta region
to do lookup;
+ * otherwise, it will follow step 1.
+ * 4. A chore will periodically run to clean up cache entries in the
StaleLocationCache.
+ */
+class CatalogReplicaLoadBalanceReplicaSimpleSelector implements
+ CatalogReplicaLoadBalanceReplicaSelector {
+ private static final Logger LOG =
+
LoggerFactory.getLogger(CatalogReplicaLoadBalanceReplicaSimpleSelector.class);
+ private final long STALE_CACHE_TIMEOUT_IN_MILLISECONDS = 3000; // 3 seconds
+ private final int STALE_CACHE_CLEAN_CHORE_INTERVAL = 1500; // 1.5 seconds
+
+ /**
+ * StaleLocationCacheEntry is the entry when a stale location is reported by
an client.
+ */
+ private static final class StaleLocationCacheEntry {
+ // replica id where the stale location comes from.
+ private int fromReplicaId;
+
+ // timestamp in milliseconds
+ private long timestamp;
+
+ private byte[] endKey;
+
+ StaleLocationCacheEntry(final int metaReplicaId, final byte[] endKey) {
+ this.fromReplicaId = metaReplicaId;
+ this.endKey = endKey;
+ timestamp = System.currentTimeMillis();
+ }
+
+ public byte[] getEndKey() {
+ return this.endKey;
+ }
+
+ public int getFromReplicaId() {
+ return this.fromReplicaId;
+ }
+ public long getTimestamp() {
+ return this.timestamp;
+ }
+
+ @Override
+ public String toString() {
+ return new ToStringBuilder(this)
+ .append("endKey", endKey)
+ .append("fromReplicaId", fromReplicaId)
+ .append("timestamp", timestamp)
+ .toString();
+ }
+ }
+
+ private static final class StaleTableCache {
Review comment:
There is only one field in this class, then why not use
ConcurrentNavigableMap<byte[], StaleLocationCacheEntry> directly?
##########
File path:
hbase-client/src/main/java/org/apache/hadoop/hbase/client/CatalogReplicaLoadBalanceReplicaSimpleSelector.java
##########
@@ -0,0 +1,273 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hadoop.hbase.client;
+
+import static org.apache.hadoop.hbase.client.ConnectionUtils.isEmptyStopRow;
+import static org.apache.hadoop.hbase.util.Bytes.BYTES_COMPARATOR;
+import static org.apache.hadoop.hbase.util.ConcurrentMapUtils.computeIfAbsent;
+import java.io.IOException;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.ConcurrentNavigableMap;
+import java.util.concurrent.ConcurrentSkipListMap;
+import java.util.concurrent.ThreadLocalRandom;
+import org.apache.commons.lang3.builder.ToStringBuilder;
+import org.apache.hadoop.hbase.HRegionLocation;
+import org.apache.hadoop.hbase.ScheduledChore;
+import org.apache.hadoop.hbase.Stoppable;
+import org.apache.hadoop.hbase.TableName;
+import org.apache.hadoop.hbase.util.Bytes;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * CatalogReplicaLoadBalanceReplicaSimpleSelector implements a simple catalog
replica load balancing
+ * algorithm. It maintains a stale location cache for each table. Whenever
client looks up location,
+ * it first check if the row is the stale location cache. If yes, the location
from
+ * catalog replica is stale, it will go to the primary region to look up
update-to-date location;
+ * otherwise, it will randomly pick up a replica region for lookup. When
clients receive
+ * RegionNotServedException from region servers, it will add these region
locations to the stale
+ * location cache. The stale cache will be cleaned up periodically by a chore.
+ *
+ * It follows a simple algorithm to choose a replica to go:
+ *
+ * 1. If there is no stale location entry for rows it looks up, it will
randomly
+ * pick a replica region to do lookup.
+ * 2. If the location from the replica region is stale, client gets
RegionNotServedException
+ * from region server, in this case, it will create
StaleLocationCacheEntry in
+ * CatalogReplicaLoadBalanceReplicaSimpleSelector.
+ * 3. When client tries to do location lookup, it checks StaleLocationCache
first for rows it
+ * tries to lookup, if entry exists, it will go with primary meta region
to do lookup;
+ * otherwise, it will follow step 1.
+ * 4. A chore will periodically run to clean up cache entries in the
StaleLocationCache.
+ */
+class CatalogReplicaLoadBalanceReplicaSimpleSelector implements
+ CatalogReplicaLoadBalanceReplicaSelector {
+ private static final Logger LOG =
+
LoggerFactory.getLogger(CatalogReplicaLoadBalanceReplicaSimpleSelector.class);
+ private final long STALE_CACHE_TIMEOUT_IN_MILLISECONDS = 3000; // 3 seconds
+ private final int STALE_CACHE_CLEAN_CHORE_INTERVAL = 1500; // 1.5 seconds
+
+ /**
+ * StaleLocationCacheEntry is the entry when a stale location is reported by
an client.
+ */
+ private static final class StaleLocationCacheEntry {
+ // replica id where the stale location comes from.
+ private int fromReplicaId;
+
+ // timestamp in milliseconds
+ private long timestamp;
+
+ private byte[] endKey;
+
+ StaleLocationCacheEntry(final int metaReplicaId, final byte[] endKey) {
+ this.fromReplicaId = metaReplicaId;
+ this.endKey = endKey;
+ timestamp = System.currentTimeMillis();
+ }
+
+ public byte[] getEndKey() {
+ return this.endKey;
+ }
+
+ public int getFromReplicaId() {
+ return this.fromReplicaId;
+ }
+ public long getTimestamp() {
+ return this.timestamp;
+ }
+
+ @Override
+ public String toString() {
+ return new ToStringBuilder(this)
+ .append("endKey", endKey)
+ .append("fromReplicaId", fromReplicaId)
+ .append("timestamp", timestamp)
+ .toString();
+ }
+ }
+
+ private static final class StaleTableCache {
+ private final ConcurrentNavigableMap<byte[], StaleLocationCacheEntry>
cache =
+ new ConcurrentSkipListMap<>(BYTES_COMPARATOR);
+ }
+
+ private final ConcurrentMap<TableName, StaleTableCache> staleCache;
+ private int numOfReplicas;
+ private final AsyncConnectionImpl conn;
+ private TableName tableName;
+
+ CatalogReplicaLoadBalanceReplicaSimpleSelector(TableName tableName,
AsyncConnectionImpl conn) {
+ staleCache = new ConcurrentHashMap<>();
+ this.conn = conn;
+ this.tableName = tableName;
+
+ // This numOfReplicas is going to be lazy initialized.
+ this.numOfReplicas = -1;
+ // Start connection's chore service in case.
+ this.conn.startChoreService();
+ this.conn.getChoreService().scheduleChore(getCacheCleanupChore(this));
+ }
+
+ /**
+ * When a client runs into RegionNotServingException, it will call this
method to
+ * update Selector's internal state.
+ * @param loc the location which causes exception.
+ * @param fromReplicaId the replica id where the stale location comes from.
+ */
+ public void notifyOnError(HRegionLocation loc, int fromReplicaId) {
+ StaleTableCache tableCache =
+ computeIfAbsent(staleCache, loc.getRegion().getTable(),
StaleTableCache::new);
+ byte[] startKey = loc.getRegion().getStartKey();
+ tableCache.cache.putIfAbsent(startKey,
+ new StaleLocationCacheEntry(fromReplicaId, loc.getRegion().getEndKey()));
+ LOG.debug("Add entry to stale cache for table {} with startKey {}, {}",
+ loc.getRegion().getTable(), startKey, loc.getRegion().getEndKey());
+ }
+
+ private int getRandomNonPrimaryReplicaId() {
+ if (numOfReplicas < 0) {
+ try {
+ Admin admin = conn.toConnection().getAdmin();
Review comment:
And do we need to refresh this value? For a long running client, the
HBase cluster could change the meta replica count.
##########
File path:
hbase-client/src/main/java/org/apache/hadoop/hbase/client/CatalogReplicaLoadBalanceReplicaSimpleSelector.java
##########
@@ -0,0 +1,273 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hadoop.hbase.client;
+
+import static org.apache.hadoop.hbase.client.ConnectionUtils.isEmptyStopRow;
+import static org.apache.hadoop.hbase.util.Bytes.BYTES_COMPARATOR;
+import static org.apache.hadoop.hbase.util.ConcurrentMapUtils.computeIfAbsent;
+import java.io.IOException;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.ConcurrentNavigableMap;
+import java.util.concurrent.ConcurrentSkipListMap;
+import java.util.concurrent.ThreadLocalRandom;
+import org.apache.commons.lang3.builder.ToStringBuilder;
+import org.apache.hadoop.hbase.HRegionLocation;
+import org.apache.hadoop.hbase.ScheduledChore;
+import org.apache.hadoop.hbase.Stoppable;
+import org.apache.hadoop.hbase.TableName;
+import org.apache.hadoop.hbase.util.Bytes;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * CatalogReplicaLoadBalanceReplicaSimpleSelector implements a simple catalog
replica load balancing
+ * algorithm. It maintains a stale location cache for each table. Whenever
client looks up location,
+ * it first check if the row is the stale location cache. If yes, the location
from
+ * catalog replica is stale, it will go to the primary region to look up
update-to-date location;
+ * otherwise, it will randomly pick up a replica region for lookup. When
clients receive
+ * RegionNotServedException from region servers, it will add these region
locations to the stale
+ * location cache. The stale cache will be cleaned up periodically by a chore.
+ *
+ * It follows a simple algorithm to choose a replica to go:
+ *
+ * 1. If there is no stale location entry for rows it looks up, it will
randomly
+ * pick a replica region to do lookup.
+ * 2. If the location from the replica region is stale, client gets
RegionNotServedException
+ * from region server, in this case, it will create
StaleLocationCacheEntry in
+ * CatalogReplicaLoadBalanceReplicaSimpleSelector.
+ * 3. When client tries to do location lookup, it checks StaleLocationCache
first for rows it
+ * tries to lookup, if entry exists, it will go with primary meta region
to do lookup;
+ * otherwise, it will follow step 1.
+ * 4. A chore will periodically run to clean up cache entries in the
StaleLocationCache.
+ */
+class CatalogReplicaLoadBalanceReplicaSimpleSelector implements
+ CatalogReplicaLoadBalanceReplicaSelector {
+ private static final Logger LOG =
+
LoggerFactory.getLogger(CatalogReplicaLoadBalanceReplicaSimpleSelector.class);
+ private final long STALE_CACHE_TIMEOUT_IN_MILLISECONDS = 3000; // 3 seconds
+ private final int STALE_CACHE_CLEAN_CHORE_INTERVAL = 1500; // 1.5 seconds
+
+ /**
+ * StaleLocationCacheEntry is the entry when a stale location is reported by
an client.
+ */
+ private static final class StaleLocationCacheEntry {
+ // replica id where the stale location comes from.
+ private int fromReplicaId;
+
+ // timestamp in milliseconds
+ private long timestamp;
+
+ private byte[] endKey;
+
+ StaleLocationCacheEntry(final int metaReplicaId, final byte[] endKey) {
+ this.fromReplicaId = metaReplicaId;
+ this.endKey = endKey;
+ timestamp = System.currentTimeMillis();
+ }
+
+ public byte[] getEndKey() {
+ return this.endKey;
+ }
+
+ public int getFromReplicaId() {
+ return this.fromReplicaId;
+ }
+ public long getTimestamp() {
+ return this.timestamp;
+ }
+
+ @Override
+ public String toString() {
+ return new ToStringBuilder(this)
+ .append("endKey", endKey)
+ .append("fromReplicaId", fromReplicaId)
+ .append("timestamp", timestamp)
+ .toString();
+ }
+ }
+
+ private static final class StaleTableCache {
+ private final ConcurrentNavigableMap<byte[], StaleLocationCacheEntry>
cache =
+ new ConcurrentSkipListMap<>(BYTES_COMPARATOR);
+ }
+
+ private final ConcurrentMap<TableName, StaleTableCache> staleCache;
+ private int numOfReplicas;
+ private final AsyncConnectionImpl conn;
+ private TableName tableName;
+
+ CatalogReplicaLoadBalanceReplicaSimpleSelector(TableName tableName,
AsyncConnectionImpl conn) {
+ staleCache = new ConcurrentHashMap<>();
+ this.conn = conn;
+ this.tableName = tableName;
+
+ // This numOfReplicas is going to be lazy initialized.
+ this.numOfReplicas = -1;
+ // Start connection's chore service in case.
+ this.conn.startChoreService();
+ this.conn.getChoreService().scheduleChore(getCacheCleanupChore(this));
+ }
+
+ /**
+ * When a client runs into RegionNotServingException, it will call this
method to
+ * update Selector's internal state.
+ * @param loc the location which causes exception.
+ * @param fromReplicaId the replica id where the stale location comes from.
+ */
+ public void notifyOnError(HRegionLocation loc, int fromReplicaId) {
+ StaleTableCache tableCache =
+ computeIfAbsent(staleCache, loc.getRegion().getTable(),
StaleTableCache::new);
+ byte[] startKey = loc.getRegion().getStartKey();
+ tableCache.cache.putIfAbsent(startKey,
+ new StaleLocationCacheEntry(fromReplicaId, loc.getRegion().getEndKey()));
+ LOG.debug("Add entry to stale cache for table {} with startKey {}, {}",
+ loc.getRegion().getTable(), startKey, loc.getRegion().getEndKey());
+ }
+
+ private int getRandomNonPrimaryReplicaId() {
+ if (numOfReplicas < 0) {
+ try {
+ Admin admin = conn.toConnection().getAdmin();
Review comment:
I think here we'd better make use of the Locator related API, or use
ConnectionRegistry directly? Usually it is not a good idea to involve admin
related API in the critical read/write path.
##########
File path: hbase-common/src/main/java/org/apache/hadoop/hbase/HConstants.java
##########
@@ -1129,7 +1129,18 @@
public static final int MASTER__META_TRANSITION_HANDLER_COUNT_DEFAULT = 1;
/** Conf key for enabling meta replication */
+ /**
+ * @deprecated Since 2.4.0, will be removed in 4.0.0. Please enable meta
replica HedgedRead mode
+ * by setting "hbase.locator.meta.replicas.mode" to "HedgedRead".
+ */
+ @Deprecated
public static final String USE_META_REPLICAS = "hbase.meta.replicas.use";
Review comment:
Though I do not like this config as I said before its scope is too wide,
but since it is there for a very long time, I do not think the new config could
replace it? The config only controls how we locate regions, but this controls
all the requests to meta table...
##########
File path:
hbase-client/src/main/java/org/apache/hadoop/hbase/client/CatalogReplicaLoadBalanceReplicaSimpleSelector.java
##########
@@ -0,0 +1,273 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hadoop.hbase.client;
+
+import static org.apache.hadoop.hbase.client.ConnectionUtils.isEmptyStopRow;
+import static org.apache.hadoop.hbase.util.Bytes.BYTES_COMPARATOR;
+import static org.apache.hadoop.hbase.util.ConcurrentMapUtils.computeIfAbsent;
+import java.io.IOException;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.ConcurrentNavigableMap;
+import java.util.concurrent.ConcurrentSkipListMap;
+import java.util.concurrent.ThreadLocalRandom;
+import org.apache.commons.lang3.builder.ToStringBuilder;
+import org.apache.hadoop.hbase.HRegionLocation;
+import org.apache.hadoop.hbase.ScheduledChore;
+import org.apache.hadoop.hbase.Stoppable;
+import org.apache.hadoop.hbase.TableName;
+import org.apache.hadoop.hbase.util.Bytes;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * CatalogReplicaLoadBalanceReplicaSimpleSelector implements a simple catalog
replica load balancing
+ * algorithm. It maintains a stale location cache for each table. Whenever
client looks up location,
+ * it first check if the row is the stale location cache. If yes, the location
from
+ * catalog replica is stale, it will go to the primary region to look up
update-to-date location;
+ * otherwise, it will randomly pick up a replica region for lookup. When
clients receive
+ * RegionNotServedException from region servers, it will add these region
locations to the stale
+ * location cache. The stale cache will be cleaned up periodically by a chore.
+ *
+ * It follows a simple algorithm to choose a replica to go:
+ *
+ * 1. If there is no stale location entry for rows it looks up, it will
randomly
+ * pick a replica region to do lookup.
+ * 2. If the location from the replica region is stale, client gets
RegionNotServedException
+ * from region server, in this case, it will create
StaleLocationCacheEntry in
+ * CatalogReplicaLoadBalanceReplicaSimpleSelector.
+ * 3. When client tries to do location lookup, it checks StaleLocationCache
first for rows it
+ * tries to lookup, if entry exists, it will go with primary meta region
to do lookup;
+ * otherwise, it will follow step 1.
+ * 4. A chore will periodically run to clean up cache entries in the
StaleLocationCache.
+ */
+class CatalogReplicaLoadBalanceReplicaSimpleSelector implements
+ CatalogReplicaLoadBalanceReplicaSelector {
+ private static final Logger LOG =
+
LoggerFactory.getLogger(CatalogReplicaLoadBalanceReplicaSimpleSelector.class);
+ private final long STALE_CACHE_TIMEOUT_IN_MILLISECONDS = 3000; // 3 seconds
+ private final int STALE_CACHE_CLEAN_CHORE_INTERVAL = 1500; // 1.5 seconds
+
+ /**
+ * StaleLocationCacheEntry is the entry when a stale location is reported by
an client.
+ */
+ private static final class StaleLocationCacheEntry {
+ // replica id where the stale location comes from.
+ private int fromReplicaId;
+
+ // timestamp in milliseconds
+ private long timestamp;
+
+ private byte[] endKey;
+
+ StaleLocationCacheEntry(final int metaReplicaId, final byte[] endKey) {
+ this.fromReplicaId = metaReplicaId;
+ this.endKey = endKey;
+ timestamp = System.currentTimeMillis();
+ }
+
+ public byte[] getEndKey() {
+ return this.endKey;
+ }
+
+ public int getFromReplicaId() {
+ return this.fromReplicaId;
+ }
+ public long getTimestamp() {
+ return this.timestamp;
+ }
+
+ @Override
+ public String toString() {
+ return new ToStringBuilder(this)
+ .append("endKey", endKey)
+ .append("fromReplicaId", fromReplicaId)
+ .append("timestamp", timestamp)
+ .toString();
+ }
+ }
+
+ private static final class StaleTableCache {
+ private final ConcurrentNavigableMap<byte[], StaleLocationCacheEntry>
cache =
+ new ConcurrentSkipListMap<>(BYTES_COMPARATOR);
+ }
+
+ private final ConcurrentMap<TableName, StaleTableCache> staleCache;
+ private int numOfReplicas;
+ private final AsyncConnectionImpl conn;
+ private TableName tableName;
+
+ CatalogReplicaLoadBalanceReplicaSimpleSelector(TableName tableName,
AsyncConnectionImpl conn) {
+ staleCache = new ConcurrentHashMap<>();
+ this.conn = conn;
+ this.tableName = tableName;
+
+ // This numOfReplicas is going to be lazy initialized.
+ this.numOfReplicas = -1;
+ // Start connection's chore service in case.
+ this.conn.startChoreService();
+ this.conn.getChoreService().scheduleChore(getCacheCleanupChore(this));
+ }
+
+ /**
+ * When a client runs into RegionNotServingException, it will call this
method to
+ * update Selector's internal state.
+ * @param loc the location which causes exception.
+ * @param fromReplicaId the replica id where the stale location comes from.
+ */
+ public void notifyOnError(HRegionLocation loc, int fromReplicaId) {
+ StaleTableCache tableCache =
+ computeIfAbsent(staleCache, loc.getRegion().getTable(),
StaleTableCache::new);
+ byte[] startKey = loc.getRegion().getStartKey();
+ tableCache.cache.putIfAbsent(startKey,
+ new StaleLocationCacheEntry(fromReplicaId, loc.getRegion().getEndKey()));
+ LOG.debug("Add entry to stale cache for table {} with startKey {}, {}",
+ loc.getRegion().getTable(), startKey, loc.getRegion().getEndKey());
+ }
+
+ private int getRandomNonPrimaryReplicaId() {
+ if (numOfReplicas < 0) {
+ try {
+ Admin admin = conn.toConnection().getAdmin();
+ TableDescriptor tableDescriptor = admin.getDescriptor(tableName);
+ this.numOfReplicas = tableDescriptor.getRegionReplication();
+ } catch (IOException ioe) {
+ LOG.error("Failed to get table {}'s region replication, exception ",
tableName, ioe);
+ this.numOfReplicas = 1;
+ }
+ }
+ // In case of no replica configured, return the primary region id.
+ if (this.numOfReplicas <= 1) {
+ return RegionInfo.DEFAULT_REPLICA_ID;
+ }
+ return 1 + ThreadLocalRandom.current().nextInt(this.numOfReplicas - 1);
+ }
+
+ /**
+ * When it looks up a location, it will call this method to find a replica
replica region to go.
+ * For a normal case, > 99% of region locations from catalog/meta replica
will be up to date.
+ * In extreme cases such as region server crashes, it will depends on how
fast replication
+ * catches up.
+ *
+ * @param tablename table name it looks up
+ * @param row key it looks up.
+ * @param locateType locateType, Only BEFORE and CURRENT will be passed in.
+ * @return catalog replica id
+ */
+ public int selectReplica(final TableName tablename, final byte[] row,
+ final RegionLocateType locateType) {
+ StaleTableCache tableCache = staleCache.get(tablename);
+
+ // If there is no entry in StaleCache, select a random replica id.
+ if (tableCache == null) {
+ return getRandomNonPrimaryReplicaId();
+ }
+
+ Map.Entry<byte[], StaleLocationCacheEntry> entry;
+ boolean isEmptyStopRow = isEmptyStopRow(row);
+ // Only BEFORE and CURRENT are passed in.
+ if (locateType == RegionLocateType.BEFORE) {
+ entry = isEmptyStopRow ? tableCache.cache.lastEntry() :
tableCache.cache.lowerEntry(row);
+ } else {
+ entry = tableCache.cache.floorEntry(row);
+ }
+
+ // It is not in the stale cache, return a random replica id.
+ if (entry == null) {
+ return getRandomNonPrimaryReplicaId();
+ }
+
+ // Check if the entry times out.
+ if ((System.currentTimeMillis() - entry.getValue().getTimestamp()) >=
+ STALE_CACHE_TIMEOUT_IN_MILLISECONDS) {
+ LOG.debug("Entry for table {} with startKey {}, {} times out",
tablename, entry.getKey(),
+ entry);
+ tableCache.cache.remove(entry.getKey());
+ return getRandomNonPrimaryReplicaId();
+ }
+
+ byte[] endKey = entry.getValue().getEndKey();
+
+ // The following logic is borrowed from AsyncNonMetaRegionLocator.
+ if (isEmptyStopRow(endKey)) {
+ LOG.debug("Lookup {} goes to primary region", row);
+ return RegionInfo.DEFAULT_REPLICA_ID;
+ }
+
+ if (locateType == RegionLocateType.BEFORE) {
+ if (!isEmptyStopRow && Bytes.compareTo(endKey, row) >= 0) {
+ LOG.debug("Lookup {} goes to primary meta", row);
+ return RegionInfo.DEFAULT_REPLICA_ID;
+ }
+ } else {
+ if (Bytes.compareTo(row, endKey) < 0) {
+ LOG.debug("Lookup {} goes to primary meta", row);
+ return RegionInfo.DEFAULT_REPLICA_ID;
+ }
+ }
+
+ // Not in stale cache, return a random replica id.
+ return getRandomNonPrimaryReplicaId();
+ }
+
+ private void cleanupReplicaReplicaStaleCache() {
+ long curTimeInMills = System.currentTimeMillis();
+ for (StaleTableCache tableCache : staleCache.values()) {
+ Iterator<Map.Entry<byte[], StaleLocationCacheEntry>> it =
+ tableCache.cache.entrySet().iterator();
+ while (it.hasNext()) {
+ Map.Entry<byte[], StaleLocationCacheEntry> entry = it.next();
+ if (curTimeInMills - entry.getValue().getTimestamp() >=
+ STALE_CACHE_TIMEOUT_IN_MILLISECONDS) {
+ LOG.debug("clean entry {}, {} from stale cache", entry.getKey(),
entry.getValue());
+ it.remove();
+ }
+ }
+ }
+ }
+
+ private static Stoppable createDummyStoppable() {
Review comment:
We use 'dummy' in non test code?
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]