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.backup.example;
019
020import java.util.List;
021import java.util.Set;
022import java.util.TreeSet;
023import org.apache.yetus.audience.InterfaceAudience;
024import org.slf4j.Logger;
025import org.slf4j.LoggerFactory;
026
027/**
028 * Monitor the actual tables for which HFiles are archived for long-term retention (always kept
029 * unless ZK state changes).
030 * <p>
031 * It is internally synchronized to ensure consistent view of the table state.
032 */
033@InterfaceAudience.Private
034public class HFileArchiveTableMonitor {
035  private static final Logger LOG = LoggerFactory.getLogger(HFileArchiveTableMonitor.class);
036  private final Set<String> archivedTables = new TreeSet<>();
037
038  /**
039   * Set the tables to be archived. Internally adds each table and attempts to register it.
040   * <p>
041   * <b>Note: All previous tables will be removed in favor of these tables.</b>
042   * @param tables add each of the tables to be archived.
043   */
044  public synchronized void setArchiveTables(List<String> tables) {
045    archivedTables.clear();
046    archivedTables.addAll(tables);
047  }
048
049  /**
050   * Add the named table to be those being archived. Attempts to register the table
051   * @param table name of the table to be registered
052   */
053  public synchronized void addTable(String table) {
054    if (this.shouldArchiveTable(table)) {
055      LOG.debug("Already archiving table: " + table + ", ignoring it");
056      return;
057    }
058    archivedTables.add(table);
059  }
060
061  public synchronized void removeTable(String table) {
062    archivedTables.remove(table);
063  }
064
065  public synchronized void clearArchive() {
066    archivedTables.clear();
067  }
068
069  /**
070   * Determine if the given table should or should not allow its hfiles to be deleted in the archive
071   * @param tableName name of the table to check
072   * @return <tt>true</tt> if its store files should be retained, <tt>false</tt> otherwise
073   */
074  public synchronized boolean shouldArchiveTable(String tableName) {
075    return archivedTables.contains(tableName);
076  }
077}