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.master.region;
019
020import java.io.IOException;
021import java.util.concurrent.TimeUnit;
022import org.apache.hadoop.conf.Configuration;
023import org.apache.hadoop.hbase.HConstants;
024import org.apache.hadoop.hbase.Server;
025import org.apache.hadoop.hbase.TableName;
026import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
027import org.apache.hadoop.hbase.client.TableDescriptor;
028import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
029import org.apache.hadoop.hbase.io.encoding.DataBlockEncoding;
030import org.apache.hadoop.hbase.regionserver.BloomType;
031import org.apache.hadoop.hbase.regionserver.storefiletracker.StoreFileTracker;
032import org.apache.hadoop.hbase.regionserver.storefiletracker.StoreFileTrackerFactory;
033import org.apache.hadoop.hbase.util.Bytes;
034import org.apache.hadoop.hbase.util.ReflectionUtils;
035import org.apache.yetus.audience.InterfaceAudience;
036
037/**
038 * The factory class for creating a {@link MasterRegion}.
039 */
040@InterfaceAudience.Private
041public final class MasterRegionFactory {
042
043  // Use the character $ to let the log cleaner know that this is not the normal wal file.
044  public static final String ARCHIVED_WAL_SUFFIX = "$masterlocalwal$";
045
046  // this is a bit trick that in StoreFileInfo.validateStoreFileName, we just test if the file name
047  // contains '-' to determine if it is a valid store file, so here we have to add '-'in the file
048  // name to avoid being processed by normal TimeToLiveHFileCleaner.
049  public static final String ARCHIVED_HFILE_SUFFIX = "$-masterlocalhfile-$";
050
051  private static final String MAX_WALS_KEY = "hbase.master.store.region.maxwals";
052
053  private static final int DEFAULT_MAX_WALS = 10;
054
055  public static final String USE_HSYNC_KEY = "hbase.master.store.region.wal.hsync";
056
057  public static final String MASTER_STORE_DIR = "MasterData";
058
059  private static final String FLUSH_SIZE_KEY = "hbase.master.store.region.flush.size";
060
061  private static final long DEFAULT_FLUSH_SIZE = TableDescriptorBuilder.DEFAULT_MEMSTORE_FLUSH_SIZE;
062
063  private static final String FLUSH_PER_CHANGES_KEY = "hbase.master.store.region.flush.per.changes";
064
065  private static final long DEFAULT_FLUSH_PER_CHANGES = 1_000_000;
066
067  private static final String FLUSH_INTERVAL_MS_KEY = "hbase.master.store.region.flush.interval.ms";
068
069  // default to flush every 15 minutes, for safety
070  private static final long DEFAULT_FLUSH_INTERVAL_MS = TimeUnit.MINUTES.toMillis(15);
071
072  private static final String COMPACT_MIN_KEY = "hbase.master.store.region.compact.min";
073
074  private static final int DEFAULT_COMPACT_MIN = 4;
075
076  private static final String ROLL_PERIOD_MS_KEY = "hbase.master.store.region.walroll.period.ms";
077
078  private static final long DEFAULT_ROLL_PERIOD_MS = TimeUnit.MINUTES.toMillis(15);
079
080  private static final String RING_BUFFER_SLOT_COUNT = "hbase.master.store.ringbuffer.slot.count";
081
082  private static final int DEFAULT_RING_BUFFER_SLOT_COUNT = 128;
083
084  public static final String TRACKER_IMPL = "hbase.master.store.region.file-tracker.impl";
085
086  public static final TableName TABLE_NAME = TableName.valueOf("master:store");
087
088  public static final byte[] PROC_FAMILY = Bytes.toBytes("proc");
089
090  public static final byte[] REGION_SERVER_FAMILY = Bytes.toBytes("rs");
091
092  private static final TableDescriptor TABLE_DESC = TableDescriptorBuilder.newBuilder(TABLE_NAME)
093    .setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(HConstants.CATALOG_FAMILY)
094      .setMaxVersions(HConstants.DEFAULT_HBASE_META_VERSIONS).setInMemory(true)
095      .setBlocksize(HConstants.DEFAULT_HBASE_META_BLOCK_SIZE).setBloomFilterType(BloomType.ROWCOL)
096      .setDataBlockEncoding(DataBlockEncoding.ROW_INDEX_V1).build())
097    .setColumnFamily(ColumnFamilyDescriptorBuilder.of(PROC_FAMILY))
098    .setColumnFamily(ColumnFamilyDescriptorBuilder.of(REGION_SERVER_FAMILY)).build();
099
100  private static TableDescriptor withTrackerConfigs(Configuration conf) {
101    String trackerImpl = conf.get(TRACKER_IMPL, conf.get(StoreFileTrackerFactory.TRACKER_IMPL,
102      StoreFileTrackerFactory.Trackers.DEFAULT.name()));
103    Class<? extends StoreFileTracker> trackerClass =
104      StoreFileTrackerFactory.getTrackerClass(trackerImpl);
105    if (StoreFileTrackerFactory.isMigration(trackerClass)) {
106      throw new IllegalArgumentException("Should not set store file tracker to "
107        + StoreFileTrackerFactory.Trackers.MIGRATION.name() + " for master local region");
108    }
109    StoreFileTracker tracker = ReflectionUtils.newInstance(trackerClass, conf, true, null);
110    return tracker.updateWithTrackerConfigs(TableDescriptorBuilder.newBuilder(TABLE_DESC)).build();
111  }
112
113  public static MasterRegion create(Server server) throws IOException {
114    Configuration conf = server.getConfiguration();
115    MasterRegionParams params = new MasterRegionParams().server(server)
116      .regionDirName(MASTER_STORE_DIR).tableDescriptor(withTrackerConfigs(conf));
117    long flushSize = conf.getLong(FLUSH_SIZE_KEY, DEFAULT_FLUSH_SIZE);
118    long flushPerChanges = conf.getLong(FLUSH_PER_CHANGES_KEY, DEFAULT_FLUSH_PER_CHANGES);
119    long flushIntervalMs = conf.getLong(FLUSH_INTERVAL_MS_KEY, DEFAULT_FLUSH_INTERVAL_MS);
120    int compactMin = conf.getInt(COMPACT_MIN_KEY, DEFAULT_COMPACT_MIN);
121    params.flushSize(flushSize).flushPerChanges(flushPerChanges).flushIntervalMs(flushIntervalMs)
122      .compactMin(compactMin);
123    int maxWals = conf.getInt(MAX_WALS_KEY, DEFAULT_MAX_WALS);
124    params.maxWals(maxWals);
125    if (conf.get(USE_HSYNC_KEY) != null) {
126      params.useHsync(conf.getBoolean(USE_HSYNC_KEY, false));
127    }
128    params.ringBufferSlotCount(conf.getInt(RING_BUFFER_SLOT_COUNT, DEFAULT_RING_BUFFER_SLOT_COUNT));
129    long rollPeriodMs = conf.getLong(ROLL_PERIOD_MS_KEY, DEFAULT_ROLL_PERIOD_MS);
130    params.rollPeriodMs(rollPeriodMs).archivedWalSuffix(ARCHIVED_WAL_SUFFIX)
131      .archivedHFileSuffix(ARCHIVED_HFILE_SUFFIX);
132    return MasterRegion.create(params);
133  }
134}