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