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