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.Collection;
022import java.util.List;
023import org.apache.hadoop.conf.Configuration;
024import org.apache.hadoop.hbase.regionserver.StoreContext;
025import org.apache.hadoop.hbase.regionserver.StoreFileInfo;
026import org.apache.yetus.audience.InterfaceAudience;
027
028import org.apache.hbase.thirdparty.com.google.common.base.Preconditions;
029
030/**
031 * A store file tracker used for migrating between store file tracker implementations.
032 */
033@InterfaceAudience.Private
034class MigrationStoreFileTracker extends StoreFileTrackerBase {
035
036  public static final String SRC_IMPL = "hbase.store.file-tracker.migration.src.impl";
037
038  public static final String DST_IMPL = "hbase.store.file-tracker.migration.dst.impl";
039
040  private final StoreFileTrackerBase src;
041
042  private final StoreFileTrackerBase dst;
043
044  public MigrationStoreFileTracker(Configuration conf, boolean isPrimaryReplica, StoreContext ctx) {
045    super(conf, isPrimaryReplica, ctx);
046    this.src = StoreFileTrackerFactory.createForMigration(conf, SRC_IMPL, isPrimaryReplica, ctx);
047    this.dst = StoreFileTrackerFactory.createForMigration(conf, DST_IMPL, isPrimaryReplica, ctx);
048    Preconditions.checkArgument(!src.getClass().equals(dst.getClass()),
049      "src and dst is the same: %s", src.getClass());
050  }
051
052  @Override
053  public boolean requireWritingToTmpDirFirst() {
054    // Returns true if either of the two StoreFileTracker returns true.
055    // For example, if we want to migrate from a tracker implementation which can ignore the broken
056    // files under data directory to a tracker implementation which can not, if we still allow
057    // writing in tmp directory directly, we may have some broken files under the data directory and
058    // then after we finally change the implementation which can not ignore the broken files, we
059    // will be in trouble.
060    return src.requireWritingToTmpDirFirst() || dst.requireWritingToTmpDirFirst();
061  }
062
063  @Override
064  protected List<StoreFileInfo> doLoadStoreFiles(boolean readOnly) throws IOException {
065    List<StoreFileInfo> files = src.doLoadStoreFiles(readOnly);
066    if (!readOnly) {
067      dst.doSetStoreFiles(files);
068    }
069    return files;
070  }
071
072  @Override
073  protected void doAddNewStoreFiles(Collection<StoreFileInfo> newFiles) throws IOException {
074    src.doAddNewStoreFiles(newFiles);
075    dst.doAddNewStoreFiles(newFiles);
076  }
077
078  @Override
079  protected void doAddCompactionResults(Collection<StoreFileInfo> compactedFiles,
080    Collection<StoreFileInfo> newFiles) throws IOException {
081    src.doAddCompactionResults(compactedFiles, newFiles);
082    dst.doAddCompactionResults(compactedFiles, newFiles);
083  }
084
085  @Override
086  protected void doSetStoreFiles(Collection<StoreFileInfo> files) throws IOException {
087    throw new UnsupportedOperationException(
088      "Should not call this method on " + getClass().getSimpleName());
089  }
090
091  static Class<? extends StoreFileTracker> getSrcTrackerClass(Configuration conf) {
092    return StoreFileTrackerFactory.getStoreFileTrackerClassForMigration(conf, SRC_IMPL);
093  }
094
095  static Class<? extends StoreFileTracker> getDstTrackerClass(Configuration conf) {
096    return StoreFileTrackerFactory.getStoreFileTrackerClassForMigration(conf, DST_IMPL);
097  }
098}