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.util;
019
020import java.io.IOException;
021import org.apache.hadoop.conf.Configuration;
022import org.apache.hadoop.fs.FileSystem;
023import org.apache.hadoop.fs.Path;
024import org.apache.hadoop.hbase.TableName;
025import org.apache.hadoop.hbase.client.Admin;
026import org.apache.hadoop.hbase.client.RegionInfo;
027import org.apache.hadoop.hbase.client.RegionReplicaUtil;
028import org.apache.hadoop.hbase.io.HFileLink;
029import org.apache.hadoop.hbase.io.Reference;
030import org.apache.hadoop.hbase.regionserver.HRegion;
031import org.apache.hadoop.hbase.regionserver.StoreFileInfo;
032import org.apache.hadoop.hbase.regionserver.storefiletracker.StoreFileTracker;
033import org.apache.yetus.audience.InterfaceAudience;
034
035/**
036 * Similar to {@link RegionReplicaUtil} but for the server side
037 */
038@InterfaceAudience.Private
039public class ServerRegionReplicaUtil extends RegionReplicaUtil {
040
041  /**
042   * Whether asynchronous WAL replication to the secondary region replicas is enabled or not. If
043   * this is enabled, a replication peer named "region_replica_replication" will be created which
044   * will tail the logs and replicate the mutatations to region replicas for tables that have region
045   * replication > 1. If this is enabled once, disabling this replication also requires disabling
046   * the replication peer using shell or {@link Admin} java class. Replication to secondary region
047   * replicas works over standard inter-cluster replication.ยท
048   */
049  public static final String REGION_REPLICA_REPLICATION_CONF_KEY =
050    "hbase.region.replica.replication.enabled";
051  private static final boolean DEFAULT_REGION_REPLICA_REPLICATION = false;
052
053  /**
054   * @deprecated Since 3.0.0, leave here only for implementing compatibility code.
055   */
056  @Deprecated
057  public static final String REGION_REPLICA_REPLICATION_PEER = "region_replica_replication";
058
059  /**
060   * Same as for {@link #REGION_REPLICA_REPLICATION_CONF_KEY} but for catalog replication.
061   */
062  public static final String REGION_REPLICA_REPLICATION_CATALOG_CONF_KEY =
063    "hbase.region.replica.replication.catalog.enabled";
064  private static final boolean DEFAULT_REGION_REPLICA_REPLICATION_CATALOG = false;
065
066  /**
067   * Enables or disables refreshing store files of secondary region replicas when the memory is
068   * above the global memstore lower limit. Refreshing the store files means that we will do a file
069   * list of the primary regions store files, and pick up new files. Also depending on the store
070   * files, we can drop some memstore contents which will free up memory.
071   */
072  public static final String REGION_REPLICA_STORE_FILE_REFRESH =
073    "hbase.region.replica.storefile.refresh";
074  private static final boolean DEFAULT_REGION_REPLICA_STORE_FILE_REFRESH = true;
075
076  /**
077   * The multiplier to use when we want to refresh a secondary region instead of flushing a primary
078   * region. Default value assumes that for doing the file refresh, the biggest secondary should be
079   * 4 times bigger than the biggest primary.
080   */
081  public static final String REGION_REPLICA_STORE_FILE_REFRESH_MEMSTORE_MULTIPLIER =
082    "hbase.region.replica.storefile.refresh.memstore.multiplier";
083  private static final double DEFAULT_REGION_REPLICA_STORE_FILE_REFRESH_MEMSTORE_MULTIPLIER = 4;
084
085  /**
086   * Returns the regionInfo object to use for interacting with the file system.
087   * @return An RegionInfo object to interact with the filesystem
088   */
089  public static RegionInfo getRegionInfoForFs(RegionInfo regionInfo) {
090    if (regionInfo == null) {
091      return null;
092    }
093    return RegionReplicaUtil.getRegionInfoForDefaultReplica(regionInfo);
094  }
095
096  /**
097   * Returns whether this region replica can accept writes.
098   * @param region the HRegion object
099   * @return whether the replica is read only
100   */
101  public static boolean isReadOnly(HRegion region) {
102    return region.getTableDescriptor().isReadOnly() || !isDefaultReplica(region.getRegionInfo());
103  }
104
105  /**
106   * Returns whether to replay the recovered edits to flush the results. Currently secondary region
107   * replicas do not replay the edits, since it would cause flushes which might affect the primary
108   * region. Primary regions even opened in read only mode should replay the edits.
109   * @param region the HRegion object
110   * @return whether recovered edits should be replayed.
111   */
112  public static boolean shouldReplayRecoveredEdits(HRegion region) {
113    return isDefaultReplica(region.getRegionInfo());
114  }
115
116  /**
117   * Returns a StoreFileInfo from the given FileStatus. Secondary replicas refer to the files of the
118   * primary region, so an HFileLink is used to construct the StoreFileInfo. This way ensures that
119   * the secondary will be able to continue reading the store files even if they are moved to
120   * archive after compaction
121   */
122  public static StoreFileInfo getStoreFileInfo(Configuration conf, FileSystem fs,
123    RegionInfo regionInfo, RegionInfo regionInfoForFs, String familyName, Path path,
124    StoreFileTracker tracker) throws IOException {
125
126    // if this is a primary region, just return the StoreFileInfo constructed from path
127    if (RegionInfo.COMPARATOR.compare(regionInfo, regionInfoForFs) == 0) {
128      return tracker.getStoreFileInfo(path, true);
129    }
130
131    // else create a store file link. The link file does not exists on filesystem though.
132    if (HFileLink.isHFileLink(path) || StoreFileInfo.isHFile(path)) {
133      HFileLink link = HFileLink.build(conf, regionInfoForFs.getTable(),
134        regionInfoForFs.getEncodedName(), familyName, path.getName());
135      return new StoreFileInfo(conf, fs, link.getFileStatus(fs), link);
136    } else if (StoreFileInfo.isReference(path)) {
137      Reference reference = tracker.readReference(path);
138      Path referencePath = StoreFileInfo.getReferredToFile(path);
139      if (HFileLink.isHFileLink(referencePath)) {
140        // HFileLink Reference
141        HFileLink link = HFileLink.buildFromHFileLinkPattern(conf, referencePath);
142        return new StoreFileInfo(conf, fs, link.getFileStatus(fs), reference, link);
143      } else {
144        // Reference
145        HFileLink link = HFileLink.build(conf, regionInfoForFs.getTable(),
146          regionInfoForFs.getEncodedName(), familyName, path.getName());
147        return new StoreFileInfo(conf, fs, link.getFileStatus(fs), reference);
148      }
149    } else {
150      throw new IOException("path=" + path + " doesn't look like a valid StoreFile");
151    }
152  }
153
154  /**
155   * @return True if Region Read Replica is enabled for <code>tn</code> (whether hbase:meta or
156   *         user-space tables).
157   */
158  public static boolean isRegionReplicaReplicationEnabled(Configuration conf, TableName tn) {
159    return isMetaRegionReplicaReplicationEnabled(conf, tn)
160      || isRegionReplicaReplicationEnabled(conf);
161  }
162
163  /** Returns True if Region Read Replica is enabled for user-space tables. */
164  private static boolean isRegionReplicaReplicationEnabled(Configuration conf) {
165    return conf.getBoolean(REGION_REPLICA_REPLICATION_CONF_KEY, DEFAULT_REGION_REPLICA_REPLICATION);
166  }
167
168  /** Returns True if hbase:meta Region Read Replica is enabled. */
169  public static boolean isMetaRegionReplicaReplicationEnabled(Configuration conf, TableName tn) {
170    return TableName.isMetaTableName(tn) && conf.getBoolean(
171      REGION_REPLICA_REPLICATION_CATALOG_CONF_KEY, DEFAULT_REGION_REPLICA_REPLICATION_CATALOG);
172  }
173
174  /** Returns True if wait for primary to flush is enabled for user-space tables. */
175  public static boolean isRegionReplicaWaitForPrimaryFlushEnabled(Configuration conf) {
176    return conf.getBoolean(REGION_REPLICA_WAIT_FOR_PRIMARY_FLUSH_CONF_KEY,
177      DEFAULT_REGION_REPLICA_WAIT_FOR_PRIMARY_FLUSH);
178  }
179
180  /** Returns True if we are to refresh user-space hfiles in Region Read Replicas. */
181  public static boolean isRegionReplicaStoreFileRefreshEnabled(Configuration conf) {
182    return conf.getBoolean(REGION_REPLICA_STORE_FILE_REFRESH,
183      DEFAULT_REGION_REPLICA_STORE_FILE_REFRESH);
184  }
185
186  public static double getRegionReplicaStoreFileRefreshMultiplier(Configuration conf) {
187    return conf.getDouble(REGION_REPLICA_STORE_FILE_REFRESH_MEMSTORE_MULTIPLIER,
188      DEFAULT_REGION_REPLICA_STORE_FILE_REFRESH_MEMSTORE_MULTIPLIER);
189  }
190
191}