1 /** 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 package org.apache.hadoop.hbase.backup.example; 19 20 import java.util.List; 21 import java.util.Set; 22 import java.util.TreeSet; 23 24 import org.apache.commons.logging.Log; 25 import org.apache.commons.logging.LogFactory; 26 27 /** 28 * Monitor the actual tables for which HFiles are archived for long-term retention (always kept 29 * unless ZK state changes). 30 * <p> 31 * It is internally synchronized to ensure consistent view of the table state. 32 */ 33 public class HFileArchiveTableMonitor { 34 private static final Log LOG = LogFactory.getLog(HFileArchiveTableMonitor.class); 35 private final Set<String> archivedTables = new TreeSet<String>(); 36 37 /** 38 * Set the tables to be archived. Internally adds each table and attempts to 39 * register it. 40 * <p> 41 * <b>Note: All previous tables will be removed in favor of these tables.</b> 42 * @param tables add each of the tables to be archived. 43 */ 44 public synchronized void setArchiveTables(List<String> tables) { 45 archivedTables.clear(); 46 archivedTables.addAll(tables); 47 } 48 49 /** 50 * Add the named table to be those being archived. Attempts to register the 51 * table 52 * @param table name of the table to be registered 53 */ 54 public synchronized void addTable(String table) { 55 if (this.shouldArchiveTable(table)) { 56 LOG.debug("Already archiving table: " + table + ", ignoring it"); 57 return; 58 } 59 archivedTables.add(table); 60 } 61 62 public synchronized void removeTable(String table) { 63 archivedTables.remove(table); 64 } 65 66 public synchronized void clearArchive() { 67 archivedTables.clear(); 68 } 69 70 /** 71 * Determine if the given table should or should not allow its hfiles to be deleted in the archive 72 * @param tableName name of the table to check 73 * @return <tt>true</tt> if its store files should be retained, <tt>false</tt> otherwise 74 */ 75 public synchronized boolean shouldArchiveTable(String tableName) { 76 return archivedTables.contains(tableName); 77 } 78 }