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  import java.util.concurrent.ThreadPoolExecutor;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.apache.hadoop.hbase.HConstants;
29  import org.apache.hadoop.hbase.classification.InterfaceAudience;
30  import org.apache.hadoop.hbase.classification.InterfaceStability;
31  import org.apache.hadoop.hbase.HRegionInfo;
32  import org.apache.hadoop.hbase.ServerName;
33  import org.apache.hadoop.hbase.client.RegionReplicaUtil;
34  import org.apache.hadoop.hbase.errorhandling.ForeignException;
35  import org.apache.hadoop.hbase.master.MasterServices;
36  import org.apache.hadoop.hbase.mob.MobUtils;
37  import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.SnapshotDescription;
38  import org.apache.hadoop.hbase.snapshot.ClientSnapshotDescriptionUtils;
39  import org.apache.hadoop.hbase.snapshot.SnapshotManifest;
40  import org.apache.hadoop.hbase.util.Bytes;
41  import org.apache.hadoop.hbase.util.FSUtils;
42  import org.apache.hadoop.hbase.util.ModifyRegionUtils;
43  import org.apache.hadoop.hbase.util.Pair;
44  import org.apache.zookeeper.KeeperException;
45  
46  /**
47   * Take a snapshot of a disabled table.
48   * <p>
49   * Table must exist when taking the snapshot, or results are undefined.
50   */
51  @InterfaceAudience.Private
52  @InterfaceStability.Evolving
53  public class DisabledTableSnapshotHandler extends TakeSnapshotHandler {
54    private static final Log LOG = LogFactory.getLog(DisabledTableSnapshotHandler.class);
55  
56    /**
57     * @param snapshot descriptor of the snapshot to take
58     * @param masterServices master services provider
59     */
60    public DisabledTableSnapshotHandler(SnapshotDescription snapshot,
61        final MasterServices masterServices, final SnapshotManager snapshotManager) {
62      super(snapshot, masterServices, snapshotManager);
63    }
64  
65    @Override
66    public DisabledTableSnapshotHandler prepare() throws Exception {
67      return (DisabledTableSnapshotHandler) super.prepare();
68    }
69  
70    // TODO consider parallelizing these operations since they are independent. Right now its just
71    // easier to keep them serial though
72    @Override
73    public void snapshotRegions(List<Pair<HRegionInfo, ServerName>> regionsAndLocations)
74        throws IOException, KeeperException {
75      try {
76        // 1. get all the regions hosting this table.
77  
78        // extract each pair to separate lists
79        Set<HRegionInfo> regions = new HashSet<HRegionInfo>();
80        for (Pair<HRegionInfo, ServerName> p : regionsAndLocations) {
81          // Don't include non-default regions
82          HRegionInfo hri = p.getFirst();
83          if (RegionReplicaUtil.isDefaultReplica(hri)) {
84            regions.add(hri);
85          }
86          // if it's the first region, add the mob region
87          if (Bytes.equals(hri.getStartKey(), HConstants.EMPTY_START_ROW)) {
88            HRegionInfo mobRegion = MobUtils.getMobRegionInfo(hri.getTable());
89            regions.add(mobRegion);
90          }
91        }
92  
93        // 2. for each region, write all the info to disk
94        String msg = "Starting to write region info and WALs for regions for offline snapshot:"
95            + ClientSnapshotDescriptionUtils.toString(snapshot);
96        LOG.info(msg);
97        status.setStatus(msg);
98  
99        ThreadPoolExecutor exec = SnapshotManifest.createExecutor(conf, "DisabledTableSnapshot");
100       try {
101         ModifyRegionUtils.editRegions(exec, regions, new ModifyRegionUtils.RegionEditTask() {
102           @Override
103           public void editRegion(final HRegionInfo regionInfo) throws IOException {
104             snapshotManifest.addRegion(FSUtils.getTableDir(rootDir, snapshotTable), regionInfo);
105           }
106         });
107       } finally {
108         exec.shutdown();
109       }
110     } catch (Exception e) {
111       // make sure we capture the exception to propagate back to the client later
112       String reason = "Failed snapshot " + ClientSnapshotDescriptionUtils.toString(snapshot)
113           + " due to exception:" + e.getMessage();
114       ForeignException ee = new ForeignException(reason, e);
115       monitor.receive(ee);
116       status.abort("Snapshot of table: "+ snapshotTable + " failed because " + e.getMessage());
117     } finally {
118       LOG.debug("Marking snapshot" + ClientSnapshotDescriptionUtils.toString(snapshot)
119           + " as finished.");
120     }
121   }
122 }