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.regionserver.storefiletracker;
019
020import java.io.IOException;
021import java.util.ArrayList;
022import java.util.Collection;
023import java.util.Collections;
024import java.util.List;
025import org.apache.hadoop.conf.Configuration;
026import org.apache.hadoop.fs.FileStatus;
027import org.apache.hadoop.fs.Path;
028import org.apache.hadoop.hbase.HConstants;
029import org.apache.hadoop.hbase.regionserver.StoreContext;
030import org.apache.hadoop.hbase.regionserver.StoreFileInfo;
031import org.apache.hadoop.hbase.util.CommonFSUtils;
032import org.apache.hadoop.hbase.util.ServerRegionReplicaUtil;
033import org.apache.yetus.audience.InterfaceAudience;
034import org.slf4j.Logger;
035import org.slf4j.LoggerFactory;
036
037/**
038 * The default implementation for store file tracker, where we do not persist the store file list,
039 * and use listing when loading store files.
040 */
041@InterfaceAudience.Private
042class DefaultStoreFileTracker extends StoreFileTrackerBase {
043
044  public DefaultStoreFileTracker(Configuration conf, boolean isPrimaryReplica, StoreContext ctx) {
045    super(conf, isPrimaryReplica, ctx);
046  }
047
048  private static final Logger LOG = LoggerFactory.getLogger(DefaultStoreFileTracker.class);
049
050  @Override
051  public boolean requireWritingToTmpDirFirst() {
052    return true;
053  }
054
055  @Override
056  protected void doAddNewStoreFiles(Collection<StoreFileInfo> newFiles) throws IOException {
057    // NOOP
058  }
059
060  @Override
061  protected void doAddCompactionResults(Collection<StoreFileInfo> compactedFiles,
062    Collection<StoreFileInfo> newFiles) throws IOException {
063    // NOOP
064  }
065
066  @Override
067  protected List<StoreFileInfo> doLoadStoreFiles(boolean readOnly) throws IOException {
068    List<StoreFileInfo> files = getStoreFiles(ctx.getFamily().getNameAsString());
069    return files != null ? files : Collections.emptyList();
070  }
071
072  @Override
073  protected void doSetStoreFiles(Collection<StoreFileInfo> files) throws IOException {
074  }
075
076  /**
077   * Returns the store files available for the family. This methods performs the filtering based on
078   * the valid store files.
079   * @param familyName Column Family Name
080   * @return a set of {@link StoreFileInfo} for the specified family.
081   */
082  public List<StoreFileInfo> getStoreFiles(final String familyName) throws IOException {
083    Path familyDir = ctx.getRegionFileSystem().getStoreDir(familyName);
084    FileStatus[] files =
085      CommonFSUtils.listStatus(ctx.getRegionFileSystem().getFileSystem(), familyDir);
086    if (files == null) {
087      if (LOG.isTraceEnabled()) {
088        LOG.trace("No StoreFiles for: " + familyDir);
089      }
090      return null;
091    }
092
093    ArrayList<StoreFileInfo> storeFiles = new ArrayList<>(files.length);
094    for (FileStatus status : files) {
095      if (!StoreFileInfo.isValid(status)) {
096        // recovered.hfiles directory is expected inside CF path when
097        // hbase.wal.split.to.hfile to
098        // true, refer HBASE-23740
099        if (!HConstants.RECOVERED_HFILES_DIR.equals(status.getPath().getName())) {
100          LOG.warn("Invalid StoreFile: {}", status.getPath());
101        }
102        continue;
103      }
104      StoreFileInfo info = ServerRegionReplicaUtil.getStoreFileInfo(conf,
105        ctx.getRegionFileSystem().getFileSystem(), ctx.getRegionInfo(),
106        ctx.getRegionFileSystem().getRegionInfoForFS(), familyName, status.getPath(), this);
107      storeFiles.add(info);
108
109    }
110    return storeFiles;
111  }
112}