View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.master.snapshot;
19  
20  import java.io.IOException;
21  import java.util.HashSet;
22  import java.util.List;
23  import java.util.Set;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.hadoop.classification.InterfaceAudience;
28  import org.apache.hadoop.classification.InterfaceStability;
29  import org.apache.hadoop.fs.Path;
30  import org.apache.hadoop.hbase.HRegionInfo;
31  import org.apache.hadoop.hbase.ServerName;
32  import org.apache.hadoop.hbase.errorhandling.ForeignException;
33  import org.apache.hadoop.hbase.errorhandling.TimeoutExceptionInjector;
34  import org.apache.hadoop.hbase.master.MasterServices;
35  import org.apache.hadoop.hbase.master.MetricsMaster;
36  import org.apache.hadoop.hbase.monitoring.MonitoredTask;
37  import org.apache.hadoop.hbase.monitoring.TaskMonitor;
38  import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.SnapshotDescription;
39  import org.apache.hadoop.hbase.regionserver.HRegion;
40  import org.apache.hadoop.hbase.regionserver.HRegionFileSystem;
41  import org.apache.hadoop.hbase.snapshot.ClientSnapshotDescriptionUtils;
42  import org.apache.hadoop.hbase.snapshot.CopyRecoveredEditsTask;
43  import org.apache.hadoop.hbase.snapshot.ReferenceRegionHFilesTask;
44  import org.apache.hadoop.hbase.snapshot.SnapshotDescriptionUtils;
45  import org.apache.hadoop.hbase.snapshot.TableInfoCopyTask;
46  import org.apache.hadoop.hbase.snapshot.TakeSnapshotUtils;
47  import org.apache.hadoop.hbase.util.FSUtils;
48  import org.apache.hadoop.hbase.util.Pair;
49  import org.apache.zookeeper.KeeperException;
50  
51  /**
52   * Take a snapshot of a disabled table.
53   * <p>
54   * Table must exist when taking the snapshot, or results are undefined.
55   */
56  @InterfaceAudience.Private
57  @InterfaceStability.Evolving
58  public class DisabledTableSnapshotHandler extends TakeSnapshotHandler {
59    private static final Log LOG = LogFactory.getLog(DisabledTableSnapshotHandler.class);
60    private final TimeoutExceptionInjector timeoutInjector;
61  
62    /**
63     * @param snapshot descriptor of the snapshot to take
64     * @param masterServices master services provider
65     */
66    public DisabledTableSnapshotHandler(SnapshotDescription snapshot,
67        final MasterServices masterServices, final MetricsMaster metricsMaster) {
68      super(snapshot, masterServices, metricsMaster);
69  
70      // setup the timer
71      timeoutInjector = TakeSnapshotUtils.getMasterTimerAndBindToMonitor(snapshot, conf, monitor);
72    }
73  
74    @Override
75    public DisabledTableSnapshotHandler prepare() throws Exception {
76      return (DisabledTableSnapshotHandler) super.prepare();
77    }
78  
79    // TODO consider parallelizing these operations since they are independent. Right now its just
80    // easier to keep them serial though
81    @Override
82    public void snapshotRegions(List<Pair<HRegionInfo, ServerName>> regionsAndLocations)
83        throws IOException, KeeperException {
84      try {
85        timeoutInjector.start();
86  
87        Path snapshotDir = SnapshotDescriptionUtils.getWorkingSnapshotDir(snapshot, rootDir);
88  
89        // 1. get all the regions hosting this table.
90  
91        // extract each pair to separate lists
92        Set<String> serverNames = new HashSet<String>();
93        Set<HRegionInfo> regions = new HashSet<HRegionInfo>();
94        for (Pair<HRegionInfo, ServerName> p : regionsAndLocations) {
95          regions.add(p.getFirst());
96          serverNames.add(p.getSecond().toString());
97        }
98  
99        // 2. for each region, write all the info to disk
100       String msg = "Starting to write region info and WALs for regions for offline snapshot:"
101           + ClientSnapshotDescriptionUtils.toString(snapshot);
102       LOG.info(msg);
103       status.setStatus(msg);
104       for (HRegionInfo regionInfo : regions) {
105         // 2.1 copy the regionInfo files to the snapshot
106         HRegionFileSystem regionFs = HRegionFileSystem.createRegionOnFileSystem(conf, fs,
107           snapshotDir, regionInfo);
108 
109         // check for error for each region
110         monitor.rethrowException();
111 
112         // 2.2 for each region, copy over its recovered.edits directory
113         Path regionDir = HRegion.getRegionDir(rootDir, regionInfo);
114         Path snapshotRegionDir = regionFs.getRegionDir();
115         new CopyRecoveredEditsTask(snapshot, monitor, fs, regionDir, snapshotRegionDir).call();
116         monitor.rethrowException();
117         status.setStatus("Completed copying recovered edits for offline snapshot of table: "
118             + snapshotTable);
119 
120         // 2.3 reference all the files in the region
121         new ReferenceRegionHFilesTask(snapshot, monitor, regionDir, fs, snapshotRegionDir).call();
122         monitor.rethrowException();
123         status.setStatus("Completed referencing HFiles for offline snapshot of table: " +
124             snapshotTable);
125       }
126 
127       // 3. write the table info to disk
128       LOG.info("Starting to copy tableinfo for offline snapshot: " +
129       ClientSnapshotDescriptionUtils.toString(snapshot));
130       TableInfoCopyTask tableInfoCopyTask = new TableInfoCopyTask(this.monitor, snapshot, fs,
131           FSUtils.getRootDir(conf));
132       tableInfoCopyTask.call();
133       monitor.rethrowException();
134       status.setStatus("Finished copying tableinfo for snapshot of table: " +
135           snapshotTable);
136     } catch (Exception e) {
137       // make sure we capture the exception to propagate back to the client later
138       String reason = "Failed snapshot " + ClientSnapshotDescriptionUtils.toString(snapshot)
139           + " due to exception:" + e.getMessage();
140       ForeignException ee = new ForeignException(reason, e);
141       monitor.receive(ee);
142       status.abort("Snapshot of table: "+ snapshotTable +
143           " failed because " + e.getMessage());
144     } finally {
145       LOG.debug("Marking snapshot" + ClientSnapshotDescriptionUtils.toString(snapshot)
146           + " as finished.");
147 
148       // 6. mark the timer as finished - even if we got an exception, we don't need to time the
149       // operation any further
150       timeoutInjector.complete();
151     }
152   }
153 }