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.protobuf.generated.HBaseProtos.SnapshotDescription;
36  import org.apache.hadoop.hbase.regionserver.HRegion;
37  import org.apache.hadoop.hbase.regionserver.HRegionFileSystem;
38  import org.apache.hadoop.hbase.snapshot.ClientSnapshotDescriptionUtils;
39  import org.apache.hadoop.hbase.snapshot.CopyRecoveredEditsTask;
40  import org.apache.hadoop.hbase.snapshot.ReferenceRegionHFilesTask;
41  import org.apache.hadoop.hbase.snapshot.SnapshotDescriptionUtils;
42  import org.apache.hadoop.hbase.snapshot.TableInfoCopyTask;
43  import org.apache.hadoop.hbase.snapshot.TakeSnapshotUtils;
44  import org.apache.hadoop.hbase.util.FSUtils;
45  import org.apache.hadoop.hbase.util.Pair;
46  import org.apache.zookeeper.KeeperException;
47  
48  /**
49   * Take a snapshot of a disabled table.
50   * <p>
51   * Table must exist when taking the snapshot, or results are undefined.
52   */
53  @InterfaceAudience.Private
54  @InterfaceStability.Evolving
55  public class DisabledTableSnapshotHandler extends TakeSnapshotHandler {
56    private static final Log LOG = LogFactory.getLog(DisabledTableSnapshotHandler.class);
57    private final TimeoutExceptionInjector timeoutInjector;
58  
59    /**
60     * @param snapshot descriptor of the snapshot to take
61     * @param masterServices master services provider
62     * @throws IOException on unexpected error
63     */
64    public DisabledTableSnapshotHandler(SnapshotDescription snapshot,
65        final MasterServices masterServices) throws IOException {
66      super(snapshot, masterServices);
67  
68      // setup the timer
69      timeoutInjector = TakeSnapshotUtils.getMasterTimerAndBindToMonitor(snapshot, conf, monitor);
70    }
71  
72    @Override
73    public DisabledTableSnapshotHandler prepare() throws Exception {
74      return (DisabledTableSnapshotHandler) super.prepare();
75    }
76  
77    // TODO consider parallelizing these operations since they are independent. Right now its just
78    // easier to keep them serial though
79    @Override
80    public void snapshotRegions(List<Pair<HRegionInfo, ServerName>> regionsAndLocations) throws IOException,
81    KeeperException {
82      try {
83        timeoutInjector.start();
84  
85        Path snapshotDir = SnapshotDescriptionUtils.getWorkingSnapshotDir(snapshot, rootDir);
86  
87        // 1. get all the regions hosting this table.
88  
89        // extract each pair to separate lists
90        Set<String> serverNames = new HashSet<String>();
91        Set<HRegionInfo> regions = new HashSet<HRegionInfo>();
92        for (Pair<HRegionInfo, ServerName> p : regionsAndLocations) {
93          regions.add(p.getFirst());
94          serverNames.add(p.getSecond().toString());
95        }
96  
97        // 2. for each region, write all the info to disk
98        LOG.info("Starting to write region info and WALs for regions for offline snapshot:"
99            + ClientSnapshotDescriptionUtils.toString(snapshot));
100       for (HRegionInfo regionInfo : regions) {
101         // 2.1 copy the regionInfo files to the snapshot
102         HRegionFileSystem regionFs = HRegionFileSystem.createRegionOnFileSystem(conf, fs,
103           snapshotDir, regionInfo);
104 
105         // check for error for each region
106         monitor.rethrowException();
107 
108         // 2.2 for each region, copy over its recovered.edits directory
109         Path regionDir = HRegion.getRegionDir(rootDir, regionInfo);
110         Path snapshotRegionDir = regionFs.getRegionDir();
111         new CopyRecoveredEditsTask(snapshot, monitor, fs, regionDir, snapshotRegionDir).call();
112         monitor.rethrowException();
113 
114         // 2.3 reference all the files in the region
115         new ReferenceRegionHFilesTask(snapshot, monitor, regionDir, fs, snapshotRegionDir).call();
116         monitor.rethrowException();
117       }
118 
119       // 3. write the table info to disk
120       LOG.info("Starting to copy tableinfo for offline snapshot: " +
121       ClientSnapshotDescriptionUtils.toString(snapshot));
122       TableInfoCopyTask tableInfoCopyTask = new TableInfoCopyTask(this.monitor, snapshot, fs,
123           FSUtils.getRootDir(conf));
124       tableInfoCopyTask.call();
125       monitor.rethrowException();
126     } catch (Exception e) {
127       // make sure we capture the exception to propagate back to the client later
128       String reason = "Failed snapshot " + ClientSnapshotDescriptionUtils.toString(snapshot)
129           + " due to exception:" + e.getMessage();
130       ForeignException ee = new ForeignException(reason, e);
131       monitor.receive(ee);
132     } finally {
133       LOG.debug("Marking snapshot" + ClientSnapshotDescriptionUtils.toString(snapshot)
134           + " as finished.");
135 
136       // 6. mark the timer as finished - even if we got an exception, we don't need to time the
137       // operation any further
138       timeoutInjector.complete();
139     }
140   }
141 }