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