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