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