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    HRegionLocation oldLoc = cachedLocationSupplier.apply(loc);
056    if (LOG.isDebugEnabled()) {
057      LOG.debug("Try updating {} , the old value is {}, error={}", loc, oldLoc,
058        exception != null ? exception.toString() : "none");
059    }
060    if (!canUpdateOnError(loc, oldLoc)) {
061      return;
062    }
063    Throwable cause = findException(exception);
064    if (LOG.isDebugEnabled()) {
065      LOG.debug("The actual exception when updating {} is {}", loc,
066        cause != null ? cause.toString() : "none");
067    }
068    if (cause == null || !isMetaClearingException(cause)) {
069      LOG.debug("Will not update {} because the exception is null or not the one we care about",
070        loc);
071      return;
072    }
073    if (cause instanceof RegionMovedException) {
074      RegionMovedException rme = (RegionMovedException) cause;
075      HRegionLocation newLoc =
076        new HRegionLocation(loc.getRegion(), rme.getServerName(), rme.getLocationSeqNum());
077      LOG.debug("Try updating {} with the new location {} constructed by {}", loc, newLoc,
078        rme.toString());
079      addToCache.accept(newLoc);
080    } else {
081      LOG.debug("Try removing {} from cache", loc);
082      removeFromCache.accept(loc);
083    }
084  }
085
086  static RegionLocations createRegionLocations(HRegionLocation loc) {
087    int replicaId = loc.getRegion().getReplicaId();
088    HRegionLocation[] locs = new HRegionLocation[replicaId + 1];
089    locs[replicaId] = loc;
090    return new RegionLocations(locs);
091  }
092
093  /**
094   * Create a new {@link RegionLocations} based on the given {@code oldLocs}, and replace the
095   * location for the given {@code replicaId} with the given {@code loc}.
096   * <p/>
097   * All the {@link RegionLocations} in async locator related class are immutable because we want to
098   * access them concurrently, so here we need to create a new one, instead of calling
099   * {@link RegionLocations#updateLocation(HRegionLocation, boolean, boolean)}.
100   */
101  static RegionLocations replaceRegionLocation(RegionLocations oldLocs, HRegionLocation loc) {
102    int replicaId = loc.getRegion().getReplicaId();
103    HRegionLocation[] locs = oldLocs.getRegionLocations();
104    locs = Arrays.copyOf(locs, Math.max(replicaId + 1, locs.length));
105    locs[replicaId] = loc;
106    return new RegionLocations(locs);
107  }
108
109  /**
110   * Create a new {@link RegionLocations} based on the given {@code oldLocs}, and remove the
111   * location for the given {@code replicaId}.
112   * <p/>
113   * All the {@link RegionLocations} in async locator related class are immutable because we want to
114   * access them concurrently, so here we need to create a new one, instead of calling
115   * {@link RegionLocations#remove(int)}.
116   */
117  static RegionLocations removeRegionLocation(RegionLocations oldLocs, int replicaId) {
118    HRegionLocation[] locs = oldLocs.getRegionLocations();
119    if (locs.length < replicaId + 1) {
120      // Here we do not modify the oldLocs so it is safe to return it.
121      return oldLocs;
122    }
123    locs = Arrays.copyOf(locs, locs.length);
124    locs[replicaId] = null;
125    if (ObjectUtils.firstNonNull(locs) != null) {
126      return new RegionLocations(locs);
127    } else {
128      // if all the locations are null, just return null
129      return null;
130    }
131  }
132
133  static boolean isGood(RegionLocations locs, int replicaId) {
134    if (locs == null) {
135      return false;
136    }
137    HRegionLocation loc = locs.getRegionLocation(replicaId);
138    return loc != null && loc.getServerName() != null;
139  }
140}