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, rme);
078      addToCache.accept(newLoc);
079    } else {
080      LOG.debug("Try removing {} from cache", loc);
081      removeFromCache.accept(loc);
082    }
083  }
084
085  static RegionLocations createRegionLocations(HRegionLocation loc) {
086    int replicaId = loc.getRegion().getReplicaId();
087    HRegionLocation[] locs = new HRegionLocation[replicaId + 1];
088    locs[replicaId] = loc;
089    return new RegionLocations(locs);
090  }
091
092  /**
093   * Create a new {@link RegionLocations} based on the given {@code oldLocs}, and replace the
094   * location for the given {@code replicaId} with the given {@code loc}.
095   * <p/>
096   * All the {@link RegionLocations} in async locator related class are immutable because we want to
097   * access them concurrently, so here we need to create a new one, instead of calling
098   * {@link RegionLocations#updateLocation(HRegionLocation, boolean, boolean)}.
099   */
100  static RegionLocations replaceRegionLocation(RegionLocations oldLocs, HRegionLocation loc) {
101    int replicaId = loc.getRegion().getReplicaId();
102    HRegionLocation[] locs = oldLocs.getRegionLocations();
103    locs = Arrays.copyOf(locs, Math.max(replicaId + 1, locs.length));
104    locs[replicaId] = loc;
105    return new RegionLocations(locs);
106  }
107
108  /**
109   * Create a new {@link RegionLocations} based on the given {@code oldLocs}, and remove the
110   * location for the given {@code replicaId}.
111   * <p/>
112   * All the {@link RegionLocations} in async locator related class are immutable because we want to
113   * access them concurrently, so here we need to create a new one, instead of calling
114   * {@link RegionLocations#remove(int)}.
115   */
116  static RegionLocations removeRegionLocation(RegionLocations oldLocs, int replicaId) {
117    HRegionLocation[] locs = oldLocs.getRegionLocations();
118    if (locs.length < replicaId + 1) {
119      // Here we do not modify the oldLocs so it is safe to return it.
120      return oldLocs;
121    }
122    locs = Arrays.copyOf(locs, locs.length);
123    locs[replicaId] = null;
124    if (ObjectUtils.firstNonNull(locs) != null) {
125      return new RegionLocations(locs);
126    } else {
127      // if all the locations are null, just return null
128      return null;
129    }
130  }
131
132  static boolean isGood(RegionLocations locs, int replicaId) {
133    if (locs == null) {
134      return false;
135    }
136    HRegionLocation loc = locs.getRegionLocation(replicaId);
137    return loc != null && loc.getServerName() != null;
138  }
139}