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