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.fs.Path;
29  import org.apache.hadoop.hbase.HRegionInfo;
30  import org.apache.hadoop.hbase.ServerName;
31  import org.apache.hadoop.hbase.errorhandling.ForeignException;
32  import org.apache.hadoop.hbase.master.MasterServices;
33  import org.apache.hadoop.hbase.master.MetricsMaster;
34  import org.apache.hadoop.hbase.procedure.Procedure;
35  import org.apache.hadoop.hbase.procedure.ProcedureCoordinator;
36  import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.SnapshotDescription;
37  import org.apache.hadoop.hbase.snapshot.HBaseSnapshotException;
38  import org.apache.hadoop.hbase.snapshot.SnapshotDescriptionUtils;
39  import org.apache.hadoop.hbase.util.Pair;
40  
41  import com.google.common.collect.Lists;
42  
43  /**
44   * Handle the master side of taking a snapshot of an online table, regardless of snapshot type.
45   * Uses a {@link Procedure} to run the snapshot across all the involved region servers.
46   * @see ProcedureCoordinator
47   */
48  @InterfaceAudience.Private
49  public class EnabledTableSnapshotHandler extends TakeSnapshotHandler {
50  
51    private static final Log LOG = LogFactory.getLog(EnabledTableSnapshotHandler.class);
52    private final ProcedureCoordinator coordinator;
53  
54    public EnabledTableSnapshotHandler(SnapshotDescription snapshot, MasterServices master,
55        final SnapshotManager manager) {
56      super(snapshot, master);
57      this.coordinator = manager.getCoordinator();
58    }
59  
60    @Override
61    public EnabledTableSnapshotHandler prepare() throws Exception {
62      return (EnabledTableSnapshotHandler) super.prepare();
63    }
64  
65    // TODO consider switching over to using regionnames, rather than server names. This would allow
66    // regions to migrate during a snapshot, and then be involved when they are ready. Still want to
67    // enforce a snapshot time constraints, but lets us be potentially a bit more robust.
68  
69    /**
70     * This method kicks off a snapshot procedure.  Other than that it hangs around for various
71     * phases to complete.
72     */
73    @Override
74    protected void snapshotRegions(List<Pair<HRegionInfo, ServerName>> regions)
75        throws HBaseSnapshotException, IOException {
76      Set<String> regionServers = new HashSet<String>(regions.size());
77      for (Pair<HRegionInfo, ServerName> region : regions) {
78        if (region != null && region.getFirst() != null && region.getSecond() != null) {
79          HRegionInfo hri = region.getFirst();
80          if (hri.isOffline() && (hri.isSplit() || hri.isSplitParent())) continue;
81          regionServers.add(region.getSecond().toString());
82        }
83      }
84  
85      // start the snapshot on the RS
86      Procedure proc = coordinator.startProcedure(this.monitor, this.snapshot.getName(),
87        this.snapshot.toByteArray(), Lists.newArrayList(regionServers));
88      if (proc == null) {
89        String msg = "Failed to submit distributed procedure for snapshot '"
90            + snapshot.getName() + "'";
91        LOG.error(msg);
92        throw new HBaseSnapshotException(msg);
93      }
94  
95      try {
96        // wait for the snapshot to complete.  A timer thread is kicked off that should cancel this
97        // if it takes too long.
98        proc.waitForCompleted();
99        LOG.info("Done waiting - online snapshot for " + this.snapshot.getName());
100 
101       // Take the offline regions as disabled
102       Path snapshotDir = SnapshotDescriptionUtils.getWorkingSnapshotDir(snapshot, rootDir);
103       for (Pair<HRegionInfo, ServerName> region : regions) {
104         HRegionInfo regionInfo = region.getFirst();
105         if (regionInfo.isOffline() && (regionInfo.isSplit() || regionInfo.isSplitParent())) {
106           if (!fs.exists(new Path(snapshotDir, regionInfo.getEncodedName()))) {
107             LOG.info("Take disabled snapshot of offline region=" + regionInfo);
108             snapshotDisabledRegion(regionInfo);
109           }
110         }
111       }
112     } catch (InterruptedException e) {
113       ForeignException ee =
114           new ForeignException("Interrupted while waiting for snapshot to finish", e);
115       monitor.receive(ee);
116       Thread.currentThread().interrupt();
117     } catch (ForeignException e) {
118       monitor.receive(e);
119     }
120   }
121 }