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.errorhandling.ForeignException;
34  import org.apache.hadoop.hbase.errorhandling.ForeignExceptionDispatcher;
35  import org.apache.hadoop.hbase.exceptions.NotAllMetaRegionsOnlineException;
36  import org.apache.hadoop.hbase.exceptions.RestoreSnapshotException;
37  import org.apache.hadoop.hbase.exceptions.TableExistsException;
38  import org.apache.hadoop.hbase.master.MasterServices;
39  import org.apache.hadoop.hbase.master.MetricsMaster;
40  import org.apache.hadoop.hbase.master.SnapshotSentinel;
41  import org.apache.hadoop.hbase.master.handler.CreateTableHandler;
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.ClientSnapshotDescriptionUtils;
46  import org.apache.hadoop.hbase.snapshot.RestoreSnapshotHelper;
47  import org.apache.hadoop.hbase.snapshot.SnapshotDescriptionUtils;
48  
49  import com.google.common.base.Preconditions;
50  
51  /**
52   * Handler to Clone a snapshot.
53   *
54   * <p>Uses {@link RestoreSnapshotHelper} to create a new table with the same
55   * content of the specified snapshot.
56   */
57  @InterfaceAudience.Private
58  public class CloneSnapshotHandler extends CreateTableHandler implements SnapshotSentinel {
59    private static final Log LOG = LogFactory.getLog(CloneSnapshotHandler.class);
60  
61    private final static String NAME = "Master CloneSnapshotHandler";
62  
63    private final SnapshotDescription snapshot;
64  
65    private final ForeignExceptionDispatcher monitor;
66    private final MetricsMaster metricsMaster;
67    private final MonitoredTask status;
68  
69    private volatile boolean stopped = false;
70  
71    public CloneSnapshotHandler(final MasterServices masterServices,
72        final SnapshotDescription snapshot, final HTableDescriptor hTableDescriptor,
73        final MetricsMaster metricsMaster) {
74      super(masterServices, masterServices.getMasterFileSystem(), hTableDescriptor,
75        masterServices.getConfiguration(), null, masterServices);
76      this.metricsMaster = metricsMaster;
77  
78      // Snapshot information
79      this.snapshot = snapshot;
80  
81      // Monitor
82      this.monitor = new ForeignExceptionDispatcher();
83      this.status = TaskMonitor.get().createStatus("Cloning  snapshot '" + snapshot.getName() +
84        "' to table " + hTableDescriptor.getNameAsString());
85    }
86  
87    @Override
88    public CloneSnapshotHandler prepare() throws NotAllMetaRegionsOnlineException,
89        TableExistsException, IOException {
90      return (CloneSnapshotHandler) super.prepare();
91    }
92  
93    /**
94     * Create the on-disk regions, using the tableRootDir provided by the CreateTableHandler.
95     * The cloned table will be created in a temp directory, and then the CreateTableHandler
96     * will be responsible to add the regions returned by this method to META and do the assignment.
97     */
98    @Override
99    protected List<HRegionInfo> handleCreateHdfsRegions(final Path tableRootDir,
100       final String tableName) throws IOException {
101     status.setStatus("Creating regions for table: " + tableName);
102     FileSystem fs = fileSystemManager.getFileSystem();
103     Path rootDir = fileSystemManager.getRootDir();
104     Path tableDir = new Path(tableRootDir, tableName);
105 
106     try {
107       // 1. Execute the on-disk Clone
108       Path snapshotDir = SnapshotDescriptionUtils.getCompletedSnapshotDir(snapshot, rootDir);
109       RestoreSnapshotHelper restoreHelper = new RestoreSnapshotHelper(conf, fs,
110           snapshot, snapshotDir, hTableDescriptor, tableDir, monitor, status);
111       RestoreSnapshotHelper.RestoreMetaChanges metaChanges = restoreHelper.restoreHdfsRegions();
112 
113       // Clone operation should not have stuff to restore or remove
114       Preconditions.checkArgument(!metaChanges.hasRegionsToRestore(),
115           "A clone should not have regions to restore");
116       Preconditions.checkArgument(!metaChanges.hasRegionsToRemove(),
117           "A clone should not have regions to remove");
118 
119       // At this point the clone is complete. Next step is enabling the table.
120       String msg = "Clone snapshot="+ snapshot.getName() +" on table=" + tableName + " completed!";
121       LOG.info(msg);
122       status.setStatus(msg + " Waiting for table to be enabled...");
123 
124       // 2. let the CreateTableHandler add the regions to meta
125       return metaChanges.getRegionsToAdd();
126     } catch (Exception e) {
127       String msg = "clone snapshot=" + ClientSnapshotDescriptionUtils.toString(snapshot) +
128         " failed because " + e.getMessage();
129       LOG.error(msg, e);
130       IOException rse = new RestoreSnapshotException(msg, e, snapshot);
131 
132       // these handlers aren't futures so we need to register the error here.
133       this.monitor.receive(new ForeignException(NAME, rse));
134       throw rse;
135     }
136   }
137 
138   @Override
139   protected void completed(final Throwable exception) {
140     this.stopped = true;
141     if (exception != null) {
142       status.abort("Snapshot '" + snapshot.getName() + "' clone failed because " +
143           exception.getMessage());
144     } else {
145       status.markComplete("Snapshot '"+ snapshot.getName() +"' clone completed and table enabled!");
146     }
147     metricsMaster.addSnapshotClone(status.getCompletionTimestamp() - status.getStartTime());
148     super.completed(exception);
149   }
150 
151   @Override
152   public boolean isFinished() {
153     return this.stopped;
154   }
155 
156   @Override
157   public long getCompletionTimestamp() {
158     return this.status.getCompletionTimestamp();
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 clone snapshot=" + snapshot + " because: " + why;
171     LOG.info(msg);
172     status.abort(msg);
173     this.monitor.receive(new ForeignException(NAME, new CancellationException(why)));
174   }
175 
176   @Override
177   public ForeignException getExceptionIfFailed() {
178     return this.monitor.getException();
179   }
180 
181   @Override
182   public void rethrowExceptionIfFailed() throws ForeignException {
183     monitor.rethrowException();
184   }
185 }