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.impl;
019
020import static org.apache.hadoop.hbase.backup.BackupRestoreConstants.JOB_NAME_CONF_KEY;
021
022import java.io.IOException;
023import java.util.ArrayList;
024import java.util.List;
025import java.util.TreeSet;
026import org.apache.commons.lang3.StringUtils;
027import org.apache.hadoop.conf.Configuration;
028import org.apache.hadoop.fs.FileSystem;
029import org.apache.hadoop.fs.LocatedFileStatus;
030import org.apache.hadoop.fs.Path;
031import org.apache.hadoop.fs.RemoteIterator;
032import org.apache.hadoop.hbase.TableName;
033import org.apache.hadoop.hbase.backup.BackupType;
034import org.apache.hadoop.hbase.backup.HBackupFileSystem;
035import org.apache.hadoop.hbase.backup.RestoreRequest;
036import org.apache.hadoop.hbase.backup.impl.BackupManifest.BackupImage;
037import org.apache.hadoop.hbase.backup.util.BackupUtils;
038import org.apache.hadoop.hbase.backup.util.RestoreTool;
039import org.apache.hadoop.hbase.client.Admin;
040import org.apache.hadoop.hbase.client.Connection;
041import org.apache.hadoop.hbase.io.hfile.HFile;
042import org.apache.yetus.audience.InterfaceAudience;
043import org.slf4j.Logger;
044import org.slf4j.LoggerFactory;
045
046/**
047 * Restore table implementation
048 */
049@InterfaceAudience.Private
050public class RestoreTablesClient {
051  private static final Logger LOG = LoggerFactory.getLogger(RestoreTablesClient.class);
052
053  private Configuration conf;
054  private Connection conn;
055  private String backupId;
056  private TableName[] sTableArray;
057  private TableName[] tTableArray;
058  private String backupRootDir;
059  private Path restoreRootDir;
060  private boolean isOverwrite;
061
062  private boolean isKeepOriginalSplits;
063
064  public RestoreTablesClient(Connection conn, RestoreRequest request) throws IOException {
065    this.backupRootDir = request.getBackupRootDir();
066    this.backupId = request.getBackupId();
067    this.sTableArray = request.getFromTables();
068    this.tTableArray = request.getToTables();
069    if (tTableArray == null || tTableArray.length == 0) {
070      this.tTableArray = sTableArray;
071    }
072    this.isOverwrite = request.isOverwrite();
073    this.isKeepOriginalSplits = request.isKeepOriginalSplits();
074    this.conn = conn;
075    this.conf = conn.getConfiguration();
076    if (request.getRestoreRootDir() != null) {
077      restoreRootDir = new Path(request.getRestoreRootDir());
078    } else {
079      FileSystem fs = FileSystem.get(conf);
080      this.restoreRootDir = BackupUtils.getTmpRestoreOutputDir(fs, conf);
081    }
082  }
083
084  /**
085   * Validate target tables.
086   * @param tTableArray target tables
087   * @param isOverwrite overwrite existing table
088   * @throws IOException exception
089   */
090  private void checkTargetTables(TableName[] tTableArray, boolean isOverwrite) throws IOException {
091    ArrayList<TableName> existTableList = new ArrayList<>();
092    ArrayList<TableName> disabledTableList = new ArrayList<>();
093
094    // check if the tables already exist
095    try (Admin admin = conn.getAdmin()) {
096      for (TableName tableName : tTableArray) {
097        if (admin.tableExists(tableName)) {
098          existTableList.add(tableName);
099          if (admin.isTableDisabled(tableName)) {
100            disabledTableList.add(tableName);
101          }
102        } else {
103          LOG.info("HBase table " + tableName
104            + " does not exist. It will be created during restore process");
105        }
106      }
107    }
108
109    if (existTableList.size() > 0) {
110      if (!isOverwrite) {
111        LOG.error("Existing table (" + existTableList + ") found in the restore target, please add "
112          + "\"-o\" as overwrite option in the command if you mean"
113          + " to restore to these existing tables");
114        throw new IOException(
115          "Existing table found in target while no \"-o\" " + "as overwrite option found");
116      } else {
117        if (disabledTableList.size() > 0) {
118          LOG.error("Found offline table in the restore target, "
119            + "please enable them before restore with \"-overwrite\" option");
120          LOG.info("Offline table list in restore target: " + disabledTableList);
121          throw new IOException(
122            "Found offline table in the target when restore with \"-overwrite\" option");
123        }
124      }
125    }
126  }
127
128  /**
129   * Restore operation handle each backupImage in array.
130   * @param images           array BackupImage
131   * @param sTable           table to be restored
132   * @param tTable           table to be restored to
133   * @param truncateIfExists truncate table
134   * @throws IOException exception
135   */
136
137  private void restoreImages(BackupImage[] images, TableName sTable, TableName tTable,
138    boolean truncateIfExists, boolean isKeepOriginalSplits) throws IOException {
139    // First image MUST be image of a FULL backup
140    BackupImage image = images[0];
141    String rootDir = image.getRootDir();
142    String backupId = image.getBackupId();
143    Path backupRoot = new Path(rootDir);
144    RestoreTool restoreTool = new RestoreTool(conf, backupRoot, restoreRootDir, backupId);
145    Path tableBackupPath = HBackupFileSystem.getTableBackupPath(sTable, backupRoot, backupId);
146    String lastIncrBackupId = images.length == 1 ? null : images[images.length - 1].getBackupId();
147    // We need hFS only for full restore (see the code)
148    BackupManifest manifest = HBackupFileSystem.getManifest(conf, backupRoot, backupId);
149    if (manifest.getType() == BackupType.FULL) {
150      LOG.info("Restoring '" + sTable + "' to '" + tTable + "' from full" + " backup image "
151        + tableBackupPath.toString());
152      conf.set(JOB_NAME_CONF_KEY, "Full_Restore-" + backupId + "-" + tTable);
153      restoreTool.fullRestoreTable(conn, tableBackupPath, sTable, tTable, truncateIfExists,
154        isKeepOriginalSplits, lastIncrBackupId);
155      conf.unset(JOB_NAME_CONF_KEY);
156    } else { // incremental Backup
157      throw new IOException("Unexpected backup type " + image.getType());
158    }
159
160    if (images.length == 1) {
161      // full backup restore done
162      return;
163    }
164
165    List<Path> dirList = new ArrayList<>();
166    // add full backup path
167    // full backup path comes first
168    for (int i = 1; i < images.length; i++) {
169      BackupImage im = images[i];
170      String fileBackupDir =
171        HBackupFileSystem.getTableBackupDir(im.getRootDir(), im.getBackupId(), sTable);
172      List<Path> list = getFilesRecursively(fileBackupDir);
173      dirList.addAll(list);
174
175    }
176
177    if (dirList.isEmpty()) {
178      LOG.info("No incremental changes since full backup for '" + sTable
179        + "', skipping incremental restore step.");
180      return;
181    }
182
183    String dirs = StringUtils.join(dirList, ",");
184    LOG.info("Restoring '" + sTable + "' to '" + tTable + "' from log dirs: " + dirs);
185    Path[] paths = new Path[dirList.size()];
186    dirList.toArray(paths);
187    conf.set(JOB_NAME_CONF_KEY, "Incremental_Restore-" + backupId + "-" + tTable);
188    restoreTool.incrementalRestoreTable(conn, tableBackupPath, paths, new TableName[] { sTable },
189      new TableName[] { tTable }, lastIncrBackupId, isKeepOriginalSplits);
190    LOG.info(sTable + " has been successfully restored to " + tTable);
191  }
192
193  private List<Path> getFilesRecursively(String fileBackupDir)
194    throws IllegalArgumentException, IOException {
195    FileSystem fs = FileSystem.get(new Path(fileBackupDir).toUri(), new Configuration());
196    List<Path> list = new ArrayList<>();
197    RemoteIterator<LocatedFileStatus> it = fs.listFiles(new Path(fileBackupDir), true);
198    while (it.hasNext()) {
199      Path p = it.next().getPath();
200      if (HFile.isHFileFormat(fs, p)) {
201        list.add(p);
202      }
203    }
204    return list;
205  }
206
207  /**
208   * Restore operation. Stage 2: resolved Backup Image dependency
209   * @param sTableArray The array of tables to be restored
210   * @param tTableArray The array of mapping tables to restore to
211   * @throws IOException exception
212   */
213  private void restore(BackupManifest manifest, TableName[] sTableArray, TableName[] tTableArray,
214    boolean isOverwrite, boolean isKeepOriginalSplits) throws IOException {
215    TreeSet<BackupImage> restoreImageSet = new TreeSet<>();
216
217    for (int i = 0; i < sTableArray.length; i++) {
218      TableName table = sTableArray[i];
219
220      // Get the image list of this backup for restore in time order from old
221      // to new.
222      List<BackupImage> list = new ArrayList<>();
223      list.add(manifest.getBackupImage());
224      TreeSet<BackupImage> set = new TreeSet<>(list);
225      List<BackupImage> depList = manifest.getDependentListByTable(table);
226      set.addAll(depList);
227      BackupImage[] arr = new BackupImage[set.size()];
228      set.toArray(arr);
229      restoreImages(arr, table, tTableArray[i], isOverwrite, isKeepOriginalSplits);
230      restoreImageSet.addAll(list);
231      if (restoreImageSet != null && !restoreImageSet.isEmpty()) {
232        LOG.info("Restore includes the following image(s):");
233        for (BackupImage image : restoreImageSet) {
234          LOG.info("Backup: " + image.getBackupId() + " "
235            + HBackupFileSystem.getTableBackupDir(image.getRootDir(), image.getBackupId(), table));
236        }
237      }
238    }
239    LOG.debug("restoreStage finished");
240  }
241
242  static long getTsFromBackupId(String backupId) {
243    if (backupId == null) {
244      return 0;
245    }
246    return Long.parseLong(backupId.substring(backupId.lastIndexOf("_") + 1));
247  }
248
249  static boolean withinRange(long a, long lower, long upper) {
250    return a >= lower && a <= upper;
251  }
252
253  public void execute() throws IOException {
254    // case VALIDATION:
255    // check the target tables
256    checkTargetTables(tTableArray, isOverwrite);
257
258    // case RESTORE_IMAGES:
259    // check and load backup image manifest for the tables
260    Path rootPath = new Path(backupRootDir);
261    BackupManifest manifest = HBackupFileSystem.getManifest(conf, rootPath, backupId);
262
263    restore(manifest, sTableArray, tTableArray, isOverwrite, isKeepOriginalSplits);
264  }
265}