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.LinkedList;
24  import java.util.List;
25  import java.util.concurrent.CancellationException;
26  
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  import org.apache.hadoop.hbase.classification.InterfaceAudience;
30  import org.apache.hadoop.fs.FileSystem;
31  import org.apache.hadoop.fs.Path;
32  import org.apache.hadoop.hbase.HRegionInfo;
33  import org.apache.hadoop.hbase.HTableDescriptor;
34  import org.apache.hadoop.hbase.TableName;
35  import org.apache.hadoop.hbase.MetaTableAccessor;
36  import org.apache.hadoop.hbase.client.Connection;
37  import org.apache.hadoop.hbase.errorhandling.ForeignException;
38  import org.apache.hadoop.hbase.errorhandling.ForeignExceptionDispatcher;
39  import org.apache.hadoop.hbase.executor.EventType;
40  import org.apache.hadoop.hbase.master.AssignmentManager;
41  import org.apache.hadoop.hbase.master.MasterFileSystem;
42  import org.apache.hadoop.hbase.master.MasterServices;
43  import org.apache.hadoop.hbase.master.MetricsSnapshot;
44  import org.apache.hadoop.hbase.master.RegionStates;
45  import org.apache.hadoop.hbase.master.SnapshotSentinel;
46  import org.apache.hadoop.hbase.master.handler.TableEventHandler;
47  import org.apache.hadoop.hbase.monitoring.MonitoredTask;
48  import org.apache.hadoop.hbase.monitoring.TaskMonitor;
49  import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.SnapshotDescription;
50  import org.apache.hadoop.hbase.snapshot.ClientSnapshotDescriptionUtils;
51  import org.apache.hadoop.hbase.snapshot.RestoreSnapshotException;
52  import org.apache.hadoop.hbase.snapshot.RestoreSnapshotHelper;
53  import org.apache.hadoop.hbase.snapshot.SnapshotDescriptionUtils;
54  import org.apache.hadoop.hbase.snapshot.SnapshotManifest;
55  
56  /**
57   * Handler to Restore a snapshot.
58   *
59   * <p>Uses {@link RestoreSnapshotHelper} to replace the table content with the
60   * data available in the snapshot.
61   */
62  @InterfaceAudience.Private
63  public class RestoreSnapshotHandler extends TableEventHandler implements SnapshotSentinel {
64    private static final Log LOG = LogFactory.getLog(RestoreSnapshotHandler.class);
65  
66    private final HTableDescriptor hTableDescriptor;
67    private final SnapshotDescription snapshot;
68    private final boolean restoreAcl;
69  
70    private final ForeignExceptionDispatcher monitor;
71    private final MetricsSnapshot metricsSnapshot = new MetricsSnapshot();
72    private final MonitoredTask status;
73  
74    private volatile boolean stopped = false;
75  
76    public RestoreSnapshotHandler(final MasterServices masterServices,
77        final SnapshotDescription snapshot, final HTableDescriptor htd, final boolean restoreAcl)
78        throws IOException {
79      super(EventType.C_M_RESTORE_SNAPSHOT, htd.getTableName(), masterServices, masterServices);
80  
81      // Snapshot information
82      this.snapshot = snapshot;
83      this.restoreAcl = restoreAcl;
84  
85      // Monitor
86      this.monitor = new ForeignExceptionDispatcher();
87  
88      // Check table exists.
89      getTableDescriptor();
90  
91      // This is the new schema we are going to write out as this modification.
92      this.hTableDescriptor = htd;
93  
94      this.status = TaskMonitor.get().createStatus(
95        "Restoring  snapshot '" + snapshot.getName() + "' to table "
96            + hTableDescriptor.getTableName());
97    }
98  
99    @Override
100   public RestoreSnapshotHandler prepare() throws IOException {
101     return (RestoreSnapshotHandler) super.prepare();
102   }
103 
104   /**
105    * The restore table is executed in place.
106    *  - The on-disk data will be restored - reference files are put in place without moving data
107    *  -  [if something fail here: you need to delete the table and re-run the restore]
108    *  - hbase:meta will be updated
109    *  -  [if something fail here: you need to run hbck to fix hbase:meta entries]
110    * The passed in list gets changed in this method
111    */
112   @Override
113   protected void handleTableOperation(List<HRegionInfo> hris) throws IOException {
114     MasterFileSystem fileSystemManager = masterServices.getMasterFileSystem();
115     Connection conn = masterServices.getConnection();
116     FileSystem fs = fileSystemManager.getFileSystem();
117     Path rootDir = fileSystemManager.getRootDir();
118     TableName tableName = hTableDescriptor.getTableName();
119 
120     try {
121       // 1. Update descriptor
122       this.masterServices.getTableDescriptors().add(hTableDescriptor);
123 
124       // 2. Execute the on-disk Restore
125       LOG.debug("Starting restore snapshot=" + ClientSnapshotDescriptionUtils.toString(snapshot));
126       Path snapshotDir = SnapshotDescriptionUtils.getCompletedSnapshotDir(snapshot, rootDir);
127       SnapshotManifest manifest = SnapshotManifest.open(masterServices.getConfiguration(), fs,
128                                                         snapshotDir, snapshot);
129       RestoreSnapshotHelper restoreHelper = new RestoreSnapshotHelper(
130           masterServices.getConfiguration(), fs, manifest,
131           this.hTableDescriptor, rootDir, monitor, status);
132       RestoreSnapshotHelper.RestoreMetaChanges metaChanges = restoreHelper.restoreHdfsRegions();
133 
134       // 3. Forces all the RegionStates to be offline
135       //
136       // The AssignmentManager keeps all the region states around
137       // with no possibility to remove them, until the master is restarted.
138       // This means that a region marked as SPLIT before the restore will never be assigned again.
139       // To avoid having all states around all the regions are switched to the OFFLINE state,
140       // which is the same state that the regions will be after a delete table.
141       forceRegionsOffline(metaChanges);
142 
143       // 4. Applies changes to hbase:meta
144       status.setStatus("Preparing to restore each region");
145 
146       // 4.1 Removes the current set of regions from META
147       //
148       // By removing also the regions to restore (the ones present both in the snapshot
149       // and in the current state) we ensure that no extra fields are present in META
150       // e.g. with a simple add addRegionToMeta() the splitA and splitB attributes
151       // not overwritten/removed, so you end up with old informations
152       // that are not correct after the restore.
153       List<HRegionInfo> hrisToRemove = new LinkedList<HRegionInfo>();
154       if (metaChanges.hasRegionsToRemove()) hrisToRemove.addAll(metaChanges.getRegionsToRemove());
155       MetaTableAccessor.deleteRegions(conn, hrisToRemove);
156 
157       // 4.2 Add the new set of regions to META
158       //
159       // At this point the old regions are no longer present in META.
160       // and the set of regions present in the snapshot will be written to META.
161       // All the information in hbase:meta are coming from the .regioninfo of each region present
162       // in the snapshot folder.
163       hris.clear();
164       if (metaChanges.hasRegionsToAdd()) hris.addAll(metaChanges.getRegionsToAdd());
165       MetaTableAccessor.addRegionsToMeta(conn, hris, hTableDescriptor.getRegionReplication());
166       if (metaChanges.hasRegionsToRestore()) {
167         MetaTableAccessor.overwriteRegions(conn, metaChanges.getRegionsToRestore(),
168           hTableDescriptor.getRegionReplication());
169       }
170       metaChanges.updateMetaParentRegions(this.server.getConnection(), hris);
171 
172       // 5. restore acl of snapshot into the table.
173       if (restoreAcl && snapshot.hasUsersAndPermissions()
174           && snapshot.getUsersAndPermissions() != null
175           && SnapshotDescriptionUtils.isSecurityAvailable(server.getConfiguration())) {
176         RestoreSnapshotHelper.restoreSnapshotACL(snapshot, tableName, server.getConfiguration());
177       }
178 
179 
180       // At this point the restore is complete. Next step is enabling the table.
181       LOG.info("Restore snapshot=" + ClientSnapshotDescriptionUtils.toString(snapshot) +
182         " on table=" + tableName + " completed!");
183     } catch (IOException e) {
184       String msg = "restore snapshot=" + ClientSnapshotDescriptionUtils.toString(snapshot)
185           + " failed. Try re-running the restore command.";
186       LOG.error(msg, e);
187       IOException rse = new RestoreSnapshotException(msg, e);
188       monitor.receive(new ForeignException(masterServices.getServerName().toString(), rse));
189       throw rse;
190     }
191   }
192 
193   private void forceRegionsOffline(final RestoreSnapshotHelper.RestoreMetaChanges metaChanges) {
194     forceRegionsOffline(metaChanges.getRegionsToAdd());
195     forceRegionsOffline(metaChanges.getRegionsToRestore());
196     forceRegionsOffline(metaChanges.getRegionsToRemove());
197   }
198 
199   private void forceRegionsOffline(final List<HRegionInfo> hris) {
200     AssignmentManager am = this.masterServices.getAssignmentManager();
201     RegionStates states = am.getRegionStates();
202     if (hris != null) {
203       for (HRegionInfo hri: hris) {
204         states.regionOffline(hri);
205       }
206     }
207   }
208 
209   @Override
210   protected void completed(final Throwable exception) {
211     this.stopped = true;
212     if (exception != null) {
213       status.abort("Restore snapshot '" + snapshot.getName() + "' failed because " +
214           exception.getMessage());
215     } else {
216       status.markComplete("Restore snapshot '"+ snapshot.getName() +"'!");
217     }
218     metricsSnapshot.addSnapshotRestore(status.getCompletionTimestamp() - status.getStartTime());
219     super.completed(exception);
220   }
221 
222   @Override
223   public boolean isFinished() {
224     return this.stopped;
225   }
226 
227   @Override
228   public long getCompletionTimestamp() {
229     return this.status.getCompletionTimestamp();
230   }
231 
232   @Override
233   public SnapshotDescription getSnapshot() {
234     return snapshot;
235   }
236 
237   @Override
238   public void cancel(String why) {
239     if (this.stopped) return;
240     this.stopped = true;
241     String msg = "Stopping restore snapshot=" + ClientSnapshotDescriptionUtils.toString(snapshot)
242         + " because: " + why;
243     LOG.info(msg);
244     CancellationException ce = new CancellationException(why);
245     this.monitor.receive(new ForeignException(masterServices.getServerName().toString(), ce));
246   }
247 
248   @Override
249   public ForeignException getExceptionIfFailed() {
250     return this.monitor.getException();
251   }
252 
253   @Override
254   public void rethrowExceptionIfFailed() throws ForeignException {
255     monitor.rethrowException();
256   }
257 }