View Javadoc

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.io.IOException;
21  
22  import org.apache.hadoop.classification.InterfaceAudience;
23  import org.apache.hadoop.conf.Configuration;
24  import org.apache.hadoop.conf.Configured;
25  import org.apache.hadoop.hbase.client.HConnection;
26  import org.apache.hadoop.hbase.util.Bytes;
27  import org.apache.hadoop.hbase.zookeeper.ZKUtil;
28  import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
29  import org.apache.zookeeper.KeeperException;
30  
31  /**
32   * Example class for how to use the table archiving coordinated via zookeeper
33   */
34  @InterfaceAudience.Private
35  public class ZKTableArchiveClient extends Configured {
36  
37    /** Configuration key for the archive node. */
38    private static final String ZOOKEEPER_ZNODE_HFILE_ARCHIVE_KEY = "zookeeper.znode.hfile.archive";
39    private HConnection connection;
40  
41    public ZKTableArchiveClient(Configuration conf, HConnection connection) {
42      super(conf);
43      this.connection = connection;
44    }
45  
46    /**
47     * Turn on backups for all HFiles for the given table.
48     * <p>
49     * All deleted hfiles are moved to the archive directory under the table directory, rather than
50     * being deleted.
51     * <p>
52     * If backups are already enabled for this table, does nothing.
53     * <p>
54     * If the table does not exist, the archiving the table's hfiles is still enabled as a future
55     * table with that name may be created shortly.
56     * @param table name of the table to start backing up
57     * @throws IOException if an unexpected exception occurs
58     * @throws KeeperException if zookeeper can't be reached
59     */
60    public void enableHFileBackupAsync(final byte[] table) throws IOException, KeeperException {
61      createHFileArchiveManager().enableHFileBackup(table).stop();
62    }
63  
64    /**
65     * Disable hfile backups for the given table.
66     * <p>
67     * Previously backed up files are still retained (if present).
68     * <p>
69     * Asynchronous operation - some extra HFiles may be retained, in the archive directory after
70     * disable is called, dependent on the latency in zookeeper to the servers.
71     * @param table name of the table stop backing up
72     * @throws IOException if an unexpected exception occurs
73     * @throws KeeperException if zookeeper can't be reached
74     */
75    public void disableHFileBackup(String table) throws IOException, KeeperException {
76      disableHFileBackup(Bytes.toBytes(table));
77    }
78  
79    /**
80     * Disable hfile backups for the given table.
81     * <p>
82     * Previously backed up files are still retained (if present).
83     * <p>
84     * Asynchronous operation - some extra HFiles may be retained, in the archive directory after
85     * disable is called, dependent on the latency in zookeeper to the servers.
86     * @param table name of the table stop backing up
87     * @throws IOException if an unexpected exception occurs
88     * @throws KeeperException if zookeeper can't be reached
89     */
90    public void disableHFileBackup(final byte[] table) throws IOException, KeeperException {
91      createHFileArchiveManager().disableHFileBackup(table).stop();
92    }
93  
94    /**
95     * Disable hfile backups for all tables.
96     * <p>
97     * Previously backed up files are still retained (if present).
98     * <p>
99     * Asynchronous operation - some extra HFiles may be retained, in the archive directory after
100    * disable is called, dependent on the latency in zookeeper to the servers.
101    * @throws IOException if an unexpected exception occurs
102    * @throws KeeperException if zookeeper can't be reached
103    */
104   public void disableHFileBackup() throws IOException, KeeperException {
105     createHFileArchiveManager().disableHFileBackup().stop();
106   }
107 
108   /**
109    * Determine if archiving is enabled (but not necessarily fully propagated) for a table
110    * @param table name of the table to check
111    * @return <tt>true</tt> if it is, <tt>false</tt> otherwise
112    * @throws IOException if a connection to ZooKeeper cannot be established
113    * @throws KeeperException
114    */
115   public boolean getArchivingEnabled(byte[] table) throws IOException, KeeperException {
116     HFileArchiveManager manager = createHFileArchiveManager();
117     try {
118       return manager.isArchivingEnabled(table);
119     } finally {
120       manager.stop();
121     }
122   }
123 
124   /**
125    * Determine if archiving is enabled (but not necessarily fully propagated) for a table
126    * @param table name of the table to check
127    * @return <tt>true</tt> if it is, <tt>false</tt> otherwise
128    * @throws IOException if an unexpected network issue occurs
129    * @throws KeeperException if zookeeper can't be reached
130    */
131   public boolean getArchivingEnabled(String table) throws IOException, KeeperException {
132     return getArchivingEnabled(Bytes.toBytes(table));
133   }
134 
135   /**
136    * @return A new {@link HFileArchiveManager} to manage which tables' hfiles should be archived
137    *         rather than deleted.
138    * @throws KeeperException if we can't reach zookeeper
139    * @throws IOException if an unexpected network issue occurs
140    */
141   private synchronized HFileArchiveManager createHFileArchiveManager() throws KeeperException,
142       IOException {
143     return new HFileArchiveManager(this.connection, this.getConf());
144   }
145 
146   /**
147    * @param conf conf to read for the base archive node
148    * @param zooKeeper zookeeper to used for building the full path
149    * @return get the znode for long-term archival of a table for
150    */
151   public static String getArchiveZNode(Configuration conf, ZooKeeperWatcher zooKeeper) {
152     return ZKUtil.joinZNode(zooKeeper.baseZNode, conf.get(ZOOKEEPER_ZNODE_HFILE_ARCHIVE_KEY,
153       TableHFileArchiveTracker.HFILE_ARCHIVE_ZNODE_PARENT));
154   }
155 }