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