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.exceptions.ClientExceptionsUtil.findException;
021import static org.apache.hadoop.hbase.exceptions.ClientExceptionsUtil.isMetaClearingException;
022
023import java.util.Arrays;
024import java.util.function.Consumer;
025import java.util.function.Function;
026import org.apache.commons.lang3.ObjectUtils;
027import org.apache.hadoop.hbase.HRegionLocation;
028import org.apache.hadoop.hbase.RegionLocations;
029import org.apache.hadoop.hbase.exceptions.RegionMovedException;
030import org.apache.yetus.audience.InterfaceAudience;
031import org.slf4j.Logger;
032import org.slf4j.LoggerFactory;
033
034/**
035 * Helper class for asynchronous region locator.
036 */
037@InterfaceAudience.Private
038final class AsyncRegionLocatorHelper {
039
040  private static final Logger LOG = LoggerFactory.getLogger(AsyncRegionLocatorHelper.class);
041
042  private AsyncRegionLocatorHelper() {
043  }
044
045  static boolean canUpdateOnError(HRegionLocation loc, HRegionLocation oldLoc) {
046    // Do not need to update if no such location, or the location is newer, or the location is not
047    // the same with us
048    return oldLoc != null && oldLoc.getSeqNum() <= loc.getSeqNum() &&
049      oldLoc.getServerName().equals(loc.getServerName());
050  }
051
052  static void updateCachedLocationOnError(HRegionLocation loc, Throwable exception,
053      Function<HRegionLocation, HRegionLocation> cachedLocationSupplier,
054      Consumer<HRegionLocation> addToCache, Consumer<HRegionLocation> removeFromCache,
055      MetricsConnection metrics) {
056    HRegionLocation oldLoc = cachedLocationSupplier.apply(loc);
057    if (LOG.isDebugEnabled()) {
058      LOG.debug("Try updating {} , the old value is {}, error={}", loc, oldLoc,
059        exception != null ? exception.toString() : "none");
060    }
061    if (!canUpdateOnError(loc, oldLoc)) {
062      return;
063    }
064    Throwable cause = findException(exception);
065    if (LOG.isDebugEnabled()) {
066      LOG.debug("The actual exception when updating {} is {}", loc,
067        cause != null ? cause.toString() : "none");
068    }
069    if (cause == null || !isMetaClearingException(cause)) {
070      LOG.debug("Will not update {} because the exception is null or not the one we care about",
071        loc);
072      return;
073    }
074    if (cause instanceof RegionMovedException) {
075      RegionMovedException rme = (RegionMovedException) cause;
076      HRegionLocation newLoc =
077        new HRegionLocation(loc.getRegion(), rme.getServerName(), rme.getLocationSeqNum());
078      LOG.debug("Try updating {} with the new location {} constructed by {}", loc, newLoc,
079        rme.toString());
080      addToCache.accept(newLoc);
081    } else {
082      LOG.debug("Try removing {} from cache", loc);
083      if (metrics != null) {
084        metrics.incrCacheDroppingExceptions(exception);
085      }
086      removeFromCache.accept(loc);
087    }
088  }
089
090  static RegionLocations createRegionLocations(HRegionLocation loc) {
091    int replicaId = loc.getRegion().getReplicaId();
092    HRegionLocation[] locs = new HRegionLocation[replicaId + 1];
093    locs[replicaId] = loc;
094    return new RegionLocations(locs);
095  }
096
097  /**
098   * Create a new {@link RegionLocations} based on the given {@code oldLocs}, and replace the
099   * location for the given {@code replicaId} with the given {@code loc}.
100   * <p/>
101   * All the {@link RegionLocations} in async locator related class are immutable because we want to
102   * access them concurrently, so here we need to create a new one, instead of calling
103   * {@link RegionLocations#updateLocation(HRegionLocation, boolean, boolean)}.
104   */
105  static RegionLocations replaceRegionLocation(RegionLocations oldLocs, HRegionLocation loc) {
106    int replicaId = loc.getRegion().getReplicaId();
107    HRegionLocation[] locs = oldLocs.getRegionLocations();
108    locs = Arrays.copyOf(locs, Math.max(replicaId + 1, locs.length));
109    locs[replicaId] = loc;
110    return new RegionLocations(locs);
111  }
112
113  /**
114   * Create a new {@link RegionLocations} based on the given {@code oldLocs}, and remove the
115   * location for the given {@code replicaId}.
116   * <p/>
117   * All the {@link RegionLocations} in async locator related class are immutable because we want to
118   * access them concurrently, so here we need to create a new one, instead of calling
119   * {@link RegionLocations#remove(int)}.
120   */
121  static RegionLocations removeRegionLocation(RegionLocations oldLocs, int replicaId) {
122    HRegionLocation[] locs = oldLocs.getRegionLocations();
123    if (locs.length < replicaId + 1) {
124      // Here we do not modify the oldLocs so it is safe to return it.
125      return oldLocs;
126    }
127    locs = Arrays.copyOf(locs, locs.length);
128    locs[replicaId] = null;
129    if (ObjectUtils.firstNonNull(locs) != null) {
130      return new RegionLocations(locs);
131    } else {
132      // if all the locations are null, just return null
133      return null;
134    }
135  }
136
137  static boolean isGood(RegionLocations locs, int replicaId) {
138    if (locs == null) {
139      return false;
140    }
141    HRegionLocation loc = locs.getRegionLocation(replicaId);
142    return loc != null && loc.getServerName() != null;
143  }
144}