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
040   * register it.
041   * <p>
042   * <b>Note: All previous tables will be removed in favor of these tables.</b>
043   * @param tables add each of the tables to be archived.
044   */
045  public synchronized void setArchiveTables(List<String> tables) {
046    archivedTables.clear();
047    archivedTables.addAll(tables);
048  }
049
050  /**
051   * Add the named table to be those being archived. Attempts to register the
052   * table
053   * @param table name of the table to be registered
054   */
055  public synchronized void addTable(String table) {
056    if (this.shouldArchiveTable(table)) {
057      LOG.debug("Already archiving table: " + table + ", ignoring it");
058      return;
059    }
060    archivedTables.add(table);
061  }
062
063  public synchronized void removeTable(String table) {
064    archivedTables.remove(table);
065  }
066
067  public synchronized void clearArchive() {
068    archivedTables.clear();
069  }
070
071  /**
072   * Determine if the given table should or should not allow its hfiles to be deleted in the archive
073   * @param tableName name of the table to check
074   * @return <tt>true</tt> if its store files should be retained, <tt>false</tt> otherwise
075   */
076  public synchronized boolean shouldArchiveTable(String tableName) {
077    return archivedTables.contains(tableName);
078  }
079}