View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  package org.apache.hadoop.hbase.master.snapshot;
21  
22  import java.io.IOException;
23  import java.util.List;
24  import java.util.concurrent.CancellationException;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.apache.hadoop.classification.InterfaceAudience;
29  import org.apache.hadoop.fs.FileSystem;
30  import org.apache.hadoop.fs.Path;
31  import org.apache.hadoop.hbase.HRegionInfo;
32  import org.apache.hadoop.hbase.HTableDescriptor;
33  import org.apache.hadoop.hbase.catalog.CatalogTracker;
34  import org.apache.hadoop.hbase.catalog.MetaEditor;
35  import org.apache.hadoop.hbase.errorhandling.ForeignException;
36  import org.apache.hadoop.hbase.errorhandling.ForeignExceptionDispatcher;
37  import org.apache.hadoop.hbase.master.MasterFileSystem;
38  import org.apache.hadoop.hbase.master.MasterServices;
39  import org.apache.hadoop.hbase.master.SnapshotSentinel;
40  import org.apache.hadoop.hbase.master.handler.TableEventHandler;
41  import org.apache.hadoop.hbase.master.metrics.MasterMetrics;
42  import org.apache.hadoop.hbase.monitoring.MonitoredTask;
43  import org.apache.hadoop.hbase.monitoring.TaskMonitor;
44  import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.SnapshotDescription;
45  import org.apache.hadoop.hbase.snapshot.RestoreSnapshotException;
46  import org.apache.hadoop.hbase.snapshot.RestoreSnapshotHelper;
47  import org.apache.hadoop.hbase.snapshot.SnapshotDescriptionUtils;
48  import org.apache.hadoop.hbase.util.Bytes;
49  
50  /**
51   * Handler to Restore a snapshot.
52   *
53   * <p>Uses {@link RestoreSnapshotHelper} to replace the table content with the
54   * data available in the snapshot.
55   */
56  @InterfaceAudience.Private
57  public class RestoreSnapshotHandler extends TableEventHandler implements SnapshotSentinel {
58    private static final Log LOG = LogFactory.getLog(RestoreSnapshotHandler.class);
59  
60    private final HTableDescriptor hTableDescriptor;
61    private final SnapshotDescription snapshot;
62  
63    private final ForeignExceptionDispatcher monitor;
64    private final MasterMetrics metricsMaster;
65    private final MonitoredTask status;
66  
67    private volatile boolean stopped = false;
68  
69    public RestoreSnapshotHandler(final MasterServices masterServices,
70        final SnapshotDescription snapshot, final HTableDescriptor htd,
71        final MasterMetrics metricsMaster) throws IOException {
72      super(EventType.C_M_RESTORE_SNAPSHOT, htd.getName(), masterServices, masterServices);
73      this.metricsMaster = metricsMaster;
74  
75      // Snapshot information
76      this.snapshot = snapshot;
77  
78      // Monitor
79      this.monitor = new ForeignExceptionDispatcher();
80  
81      // Check table exists.
82      getTableDescriptor();
83  
84      // This is the new schema we are going to write out as this modification.
85      this.hTableDescriptor = htd;
86  
87      this.status = TaskMonitor.get().createStatus(
88        "Restoring  snapshot '" + snapshot.getName() + "' to table "
89            + hTableDescriptor.getNameAsString());
90    }
91  
92    /**
93     * The restore table is executed in place.
94     *  - The on-disk data will be restored - reference files are put in place without moving data
95     *  -  [if something fail here: you need to delete the table and re-run the restore]
96     *  - META will be updated
97     *  -  [if something fail here: you need to run hbck to fix META entries]
98     * The passed in list gets changed in this method
99     */
100   @Override
101   protected void handleTableOperation(List<HRegionInfo> hris) throws IOException {
102     MasterFileSystem fileSystemManager = masterServices.getMasterFileSystem();
103     CatalogTracker catalogTracker = masterServices.getCatalogTracker();
104     FileSystem fs = fileSystemManager.getFileSystem();
105     Path rootDir = fileSystemManager.getRootDir();
106     byte[] tableName = hTableDescriptor.getName();
107     Path tableDir = HTableDescriptor.getTableDir(rootDir, tableName);
108 
109     try {
110       // 1. Update descriptor
111       this.masterServices.getTableDescriptors().add(hTableDescriptor);
112 
113       // 2. Execute the on-disk Restore
114       LOG.debug("Starting restore snapshot=" + SnapshotDescriptionUtils.toString(snapshot));
115       Path snapshotDir = SnapshotDescriptionUtils.getCompletedSnapshotDir(snapshot, rootDir);
116       RestoreSnapshotHelper restoreHelper = new RestoreSnapshotHelper(
117           masterServices.getConfiguration(), fs,
118           snapshot, snapshotDir, hTableDescriptor, tableDir, monitor, status);
119       RestoreSnapshotHelper.RestoreMetaChanges metaChanges = restoreHelper.restoreHdfsRegions();
120 
121       // 3. Applies changes to .META.
122       hris.clear();
123       status.setStatus("Preparing to restore each region");
124       if (metaChanges.hasRegionsToAdd()) hris.addAll(metaChanges.getRegionsToAdd());
125       if (metaChanges.hasRegionsToRestore()) hris.addAll(metaChanges.getRegionsToRestore());
126       List<HRegionInfo> hrisToRemove = metaChanges.getRegionsToRemove();
127       MetaEditor.mutateRegions(catalogTracker, hrisToRemove, hris);
128 
129       // At this point the restore is complete. Next step is enabling the table.
130       LOG.info("Restore snapshot=" + SnapshotDescriptionUtils.toString(snapshot) + " on table=" +
131         Bytes.toString(tableName) + " completed!");
132     } catch (IOException e) {
133       String msg = "restore snapshot=" + SnapshotDescriptionUtils.toString(snapshot)
134           + " failed. Try re-running the restore command.";
135       LOG.error(msg, e);
136       monitor.receive(new ForeignException(masterServices.getServerName().toString(), e));
137       throw new RestoreSnapshotException(msg, e);
138     } finally {
139       this.stopped = true;
140     }
141   }
142 
143   @Override
144   protected void completed(final Throwable exception) {
145     this.stopped = true;
146     if (exception != null) {
147       status.abort("Restore snapshot '" + snapshot.getName() + "' failed because " +
148           exception.getMessage());
149     } else {
150       status.markComplete("Restore snapshot '"+ snapshot.getName() +"' completed!");
151     }
152     metricsMaster.addSnapshotRestore(status.getCompletionTimestamp() - status.getStartTime());
153     super.completed(exception);
154   }
155 
156   @Override
157   public boolean isFinished() {
158     return this.stopped;
159   }
160 
161   @Override
162   public SnapshotDescription getSnapshot() {
163     return snapshot;
164   }
165 
166   @Override
167   public void cancel(String why) {
168     if (this.stopped) return;
169     this.stopped = true;
170     String msg = "Stopping restore snapshot=" + SnapshotDescriptionUtils.toString(snapshot)
171         + " because: " + why;
172     LOG.info(msg);
173     CancellationException ce = new CancellationException(why);
174     this.monitor.receive(new ForeignException(masterServices.getServerName().toString(), ce));
175   }
176 
177   public ForeignException getExceptionIfFailed() {
178     return this.monitor.getException();
179   }
180 }