saintstack commented on a change in pull request #941: HBASE-23326 Implement a ProcedureStore which stores procedures in a H… URL: https://github.com/apache/hbase/pull/941#discussion_r361027108
########## File path: hbase-server/src/main/java/org/apache/hadoop/hbase/procedure2/store/region/RegionProcedureStore.java ########## @@ -0,0 +1,576 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.hbase.procedure2.store.region; + +import static org.apache.hadoop.hbase.HConstants.EMPTY_BYTE_ARRAY; +import static org.apache.hadoop.hbase.HConstants.HREGION_LOGDIR_NAME; +import static org.apache.hadoop.hbase.HConstants.NO_NONCE; + +import java.io.IOException; +import java.io.UncheckedIOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import org.apache.commons.lang3.mutable.MutableLong; +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.fs.FileStatus; +import org.apache.hadoop.fs.FileSystem; +import org.apache.hadoop.fs.Path; +import org.apache.hadoop.hbase.Cell; +import org.apache.hadoop.hbase.HBaseIOException; +import org.apache.hadoop.hbase.Server; +import org.apache.hadoop.hbase.TableName; +import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder; +import org.apache.hadoop.hbase.client.Delete; +import org.apache.hadoop.hbase.client.Mutation; +import org.apache.hadoop.hbase.client.Put; +import org.apache.hadoop.hbase.client.RegionInfo; +import org.apache.hadoop.hbase.client.RegionInfoBuilder; +import org.apache.hadoop.hbase.client.Scan; +import org.apache.hadoop.hbase.client.TableDescriptor; +import org.apache.hadoop.hbase.client.TableDescriptorBuilder; +import org.apache.hadoop.hbase.log.HBaseMarkers; +import org.apache.hadoop.hbase.procedure2.Procedure; +import org.apache.hadoop.hbase.procedure2.ProcedureUtil; +import org.apache.hadoop.hbase.procedure2.store.LeaseRecovery; +import org.apache.hadoop.hbase.procedure2.store.ProcedureStoreBase; +import org.apache.hadoop.hbase.procedure2.store.ProcedureTree; +import org.apache.hadoop.hbase.procedure2.store.wal.WALProcedureStore; +import org.apache.hadoop.hbase.regionserver.HRegion; +import org.apache.hadoop.hbase.regionserver.HRegionFileSystem; +import org.apache.hadoop.hbase.regionserver.RegionScanner; +import org.apache.hadoop.hbase.regionserver.wal.AbstractFSWAL; +import org.apache.hadoop.hbase.util.Bytes; +import org.apache.hadoop.hbase.util.CommonFSUtils; +import org.apache.hadoop.hbase.wal.AbstractFSWALProvider; +import org.apache.hadoop.hbase.wal.WAL; +import org.apache.hadoop.hbase.wal.WALFactory; +import org.apache.yetus.audience.InterfaceAudience; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.apache.hbase.thirdparty.com.google.common.annotations.VisibleForTesting; + +import org.apache.hadoop.hbase.shaded.protobuf.generated.ProcedureProtos; + +/** + * A procedure store which uses a region to store all the procedures. + * <p/> + * FileSystem layout: + * + * <pre> + * hbase + * | + * --MasterProcs + * | + * --data + * | | + * | --/master/procedure/<encoded-region-name> <---- The region data + * | | + * | --replay <---- The edits to replay + * | + * --WALs + * | + * --<master-server-name> <---- The WAL dir for active master + * | + * --<master-server-name>-dead <---- The WAL dir dead master + * </pre> + */ [email protected] +public class RegionProcedureStore extends ProcedureStoreBase { + + private static final Logger LOG = LoggerFactory.getLogger(RegionProcedureStore.class); + + static final String MAX_WALS_KEY = "hbase.procedure.store.region.maxwals"; + + private static final int DEFAULT_MAX_WALS = 10; + + static final String USE_HSYNC_KEY = "hbase.procedure.store.region.wal.hsync"; + + static final String MASTER_PROCEDURE_DIR = "MasterProcs"; + + static final String LOGCLEANER_PLUGINS = "hbase.procedure.store.region.logcleaner.plugins"; + + private static final String DATA_DIR = "data"; + + private static final String REPLAY_EDITS_DIR = "replay"; + + private static final String DEAD_WAL_DIR_SUFFIX = "-dead"; + + private static final TableName TABLE_NAME = TableName.valueOf("master:procedure"); + + private static final byte[] FAMILY = Bytes.toBytes("info"); + + private static final byte[] PROC_QUALIFIER = Bytes.toBytes("proc"); + + private static final TableDescriptor TABLE_DESC = TableDescriptorBuilder.newBuilder(TABLE_NAME) + .setColumnFamily(ColumnFamilyDescriptorBuilder.of(FAMILY)).build(); + + private final Server server; + + private final LeaseRecovery leaseRecovery; + + private WALFactory walFactory; + + @VisibleForTesting + HRegion region; + + private RegionFlusherAndCompactor flusherAndCompactor; + + @VisibleForTesting + RegionProcedureStoreWALRoller walRoller; + + private int numThreads; + + public RegionProcedureStore(Server server, LeaseRecovery leaseRecovery) { + this.server = server; + this.leaseRecovery = leaseRecovery; + } + + @Override + public void start(int numThreads) throws IOException { + if (!setRunning(true)) { + return; + } + LOG.info("Starting the Region Procedure Store..."); + this.numThreads = numThreads; + } + + private void shutdownWAL() { + if (walFactory != null) { + try { + walFactory.shutdown(); + } catch (IOException e) { + LOG.warn("Failed to shutdown WAL", e); + } + } + } + + private void closeRegion(boolean abort) { + if (region != null) { + try { + region.close(abort); + } catch (IOException e) { + LOG.warn("Failed to close region", e); + } + } + + } + + @Override + public void stop(boolean abort) { + if (!setRunning(false)) { + return; + } + LOG.info("Stopping the Region Procedure Store, isAbort={}", abort); + if (flusherAndCompactor != null) { + flusherAndCompactor.close(); + } + // if abort, we shutdown wal first to fail the ongoing updates to the region, and then close the + // region, otherwise there will be dead lock. + if (abort) { + shutdownWAL(); + closeRegion(true); + } else { + closeRegion(false); + shutdownWAL(); + } + + if (walRoller != null) { + walRoller.close(); + } + } + + @Override + public int getNumThreads() { + return numThreads; + } + + @Override + public int setRunningProcedureCount(int count) { + // useless for region based storage. + return count; + } + + private WAL createWAL(FileSystem fs, Path rootDir, RegionInfo regionInfo) throws IOException { + String logName = AbstractFSWALProvider.getWALDirectoryName(server.getServerName().toString()); + Path walDir = new Path(rootDir, logName); + LOG.debug("WALDir={}", walDir); + if (fs.exists(walDir)) { + throw new HBaseIOException( + "Master procedure store has already created directory at " + walDir); + } + if (!fs.mkdirs(walDir)) { + throw new IOException("Can not create master procedure wal directory " + walDir); + } + WAL wal = walFactory.getWAL(regionInfo); + walRoller.addWAL(wal); + return wal; + } + + private HRegion bootstrap(Configuration conf, FileSystem fs, Path rootDir, Path dataDir) + throws IOException { + RegionInfo regionInfo = RegionInfoBuilder.newBuilder(TABLE_NAME).build(); Review comment: hard code regionid so the regioninfo name is always the same in all clusters? setRegionId(0) or 1 or something? ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
