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.List;
22  import java.util.Map;
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.classification.InterfaceStability;
29  import org.apache.hadoop.fs.FileSystem;
30  import org.apache.hadoop.fs.Path;
31  import org.apache.hadoop.hbase.TableName;
32  import org.apache.hadoop.hbase.HRegionInfo;
33  import org.apache.hadoop.hbase.HTableDescriptor;
34  import org.apache.hadoop.hbase.client.RegionReplicaUtil;
35  import org.apache.hadoop.hbase.MetaTableAccessor;
36  import org.apache.hadoop.hbase.master.MasterServices;
37  import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.SnapshotDescription;
38  import org.apache.hadoop.hbase.protobuf.generated.SnapshotProtos.SnapshotRegionManifest;
39  import org.apache.hadoop.hbase.snapshot.ClientSnapshotDescriptionUtils;
40  import org.apache.hadoop.hbase.snapshot.CorruptedSnapshotException;
41  import org.apache.hadoop.hbase.snapshot.SnapshotDescriptionUtils;
42  import org.apache.hadoop.hbase.snapshot.SnapshotManifest;
43  import org.apache.hadoop.hbase.snapshot.SnapshotReferenceUtil;
44  import org.apache.hadoop.hbase.util.FSUtils;
45  import org.apache.hadoop.hbase.zookeeper.MetaTableLocator;
46  
47  /**
48   * General snapshot verification on the master.
49   * <p>
50   * This is a light-weight verification mechanism for all the files in a snapshot. It doesn't
51   * attempt to verify that the files are exact copies (that would be paramount to taking the
52   * snapshot again!), but instead just attempts to ensure that the files match the expected
53   * files and are the same length.
54   * <p>
55   * Taking an online snapshots can race against other operations and this is an last line of
56   * defense.  For example, if meta changes between when snapshots are taken not all regions of a
57   * table may be present.  This can be caused by a region split (daughters present on this scan,
58   * but snapshot took parent), or move (snapshots only checks lists of region servers, a move could
59   * have caused a region to be skipped or done twice).
60   * <p>
61   * Current snapshot files checked:
62   * <ol>
63   * <li>SnapshotDescription is readable</li>
64   * <li>Table info is readable</li>
65   * <li>Regions</li>
66   * </ol>
67   * <ul>
68   * <li>Matching regions in the snapshot as currently in the table</li>
69   * <li>{@link HRegionInfo} matches the current and stored regions</li>
70   * <li>All referenced hfiles have valid names</li>
71   * <li>All the hfiles are present (either in .archive directory in the region)</li>
72   * <li>All recovered.edits files are present (by name) and have the correct file size</li>
73   * </ul>
74   */
75  @InterfaceAudience.Private
76  @InterfaceStability.Unstable
77  public final class MasterSnapshotVerifier {
78    private static final Log LOG = LogFactory.getLog(MasterSnapshotVerifier.class);
79  
80    private SnapshotDescription snapshot;
81    private FileSystem workingDirFs;
82    private TableName tableName;
83    private MasterServices services;
84  
85    /**
86     * @param services services for the master
87     * @param snapshot snapshot to check
88     * @param workingDirFs the file system containing the temporary snapshot information
89     */
90    public MasterSnapshotVerifier(MasterServices services,
91        SnapshotDescription snapshot, FileSystem workingDirFs) {
92      this.workingDirFs = workingDirFs;
93      this.services = services;
94      this.snapshot = snapshot;
95      this.tableName = TableName.valueOf(snapshot.getTable());
96    }
97  
98    /**
99     * Verify that the snapshot in the directory is a valid snapshot
100    * @param snapshotDir snapshot directory to check
101    * @param snapshotServers {@link org.apache.hadoop.hbase.ServerName} 
102    * of the servers that are involved in the snapshot
103    * @throws CorruptedSnapshotException if the snapshot is invalid
104    * @throws IOException if there is an unexpected connection issue to the filesystem
105    */
106   public void verifySnapshot(Path snapshotDir, Set<String> snapshotServers)
107       throws CorruptedSnapshotException, IOException {
108     SnapshotManifest manifest = SnapshotManifest.open(services.getConfiguration(), workingDirFs,
109                                                       snapshotDir, snapshot);
110     // verify snapshot info matches
111     verifySnapshotDescription(snapshotDir);
112 
113     // check that tableinfo is a valid table description
114     verifyTableInfo(manifest);
115 
116     // check that each region is valid
117     verifyRegions(manifest);
118   }
119 
120   /**
121    * Check that the snapshot description written in the filesystem matches the current snapshot
122    * @param snapshotDir snapshot directory to check
123    */
124   private void verifySnapshotDescription(Path snapshotDir) throws CorruptedSnapshotException {
125     SnapshotDescription found = SnapshotDescriptionUtils.readSnapshotInfo(workingDirFs,
126         snapshotDir);
127     if (!this.snapshot.equals(found)) {
128       throw new CorruptedSnapshotException("Snapshot read (" + found
129           + ") doesn't equal snapshot we ran (" + snapshot + ").", snapshot);
130     }
131   }
132 
133   /**
134    * Check that the table descriptor for the snapshot is a valid table descriptor
135    * @param manifest snapshot manifest to inspect
136    */
137   private void verifyTableInfo(final SnapshotManifest manifest) throws IOException {
138     HTableDescriptor htd = manifest.getTableDescriptor();
139     if (htd == null) {
140       throw new CorruptedSnapshotException("Missing Table Descriptor", snapshot);
141     }
142 
143     if (!htd.getNameAsString().equals(snapshot.getTable())) {
144       throw new CorruptedSnapshotException("Invalid Table Descriptor. Expected "
145         + snapshot.getTable() + " name, got " + htd.getNameAsString(), snapshot);
146     }
147   }
148 
149   /**
150    * Check that all the regions in the snapshot are valid, and accounted for.
151    * @param manifest snapshot manifest to inspect
152    * @throws IOException if we can't reach hbase:meta or read the files from the FS
153    */
154   private void verifyRegions(final SnapshotManifest manifest) throws IOException {
155     List<HRegionInfo> regions;
156     if (TableName.META_TABLE_NAME.equals(tableName)) {
157       regions = new MetaTableLocator().getMetaRegions(services.getZooKeeper());
158     } else {
159       regions = MetaTableAccessor.getTableRegions(services.getZooKeeper(),
160         services.getConnection(), tableName);
161     }
162     // Remove the non-default regions
163     RegionReplicaUtil.removeNonDefaultRegions(regions);
164 
165     Map<String, SnapshotRegionManifest> regionManifests = manifest.getRegionManifestsMap();
166     if (regionManifests == null) {
167       String msg = "Snapshot " + ClientSnapshotDescriptionUtils.toString(snapshot) + " looks empty";
168       LOG.error(msg);
169       throw new CorruptedSnapshotException(msg);
170     }
171 
172     String errorMsg = "";
173     if (regionManifests.size() != regions.size()) {
174       errorMsg = "Regions moved during the snapshot '" +
175                    ClientSnapshotDescriptionUtils.toString(snapshot) + "'. expected=" +
176                    regions.size() + " snapshotted=" + regionManifests.size() + ".";
177       LOG.error(errorMsg);
178     }
179 
180     // Verify HRegionInfo
181     for (HRegionInfo region : regions) {
182       SnapshotRegionManifest regionManifest = regionManifests.get(region.getEncodedName());
183       if (regionManifest == null) {
184         // could happen due to a move or split race.
185         String mesg = " No snapshot region directory found for region:" + region;
186         if (errorMsg.isEmpty()) errorMsg = mesg;
187         LOG.error(mesg);
188         continue;
189       }
190 
191       verifyRegionInfo(region, regionManifest);
192     }
193 
194     if (!errorMsg.isEmpty()) {
195       throw new CorruptedSnapshotException(errorMsg);
196     }
197 
198     // Verify Snapshot HFiles
199     // Requires the root directory file system as HFiles are stored in the root directory
200     SnapshotReferenceUtil.verifySnapshot(services.getConfiguration(),
201         FSUtils.getRootDirFileSystem(services.getConfiguration()), manifest);
202   }
203 
204   /**
205    * Verify that the regionInfo is valid
206    * @param region the region to check
207    * @param manifest snapshot manifest to inspect
208    */
209   private void verifyRegionInfo(final HRegionInfo region,
210       final SnapshotRegionManifest manifest) throws IOException {
211     HRegionInfo manifestRegionInfo = HRegionInfo.convert(manifest.getRegionInfo());
212     if (!region.equals(manifestRegionInfo)) {
213       String msg = "Manifest region info " + manifestRegionInfo +
214                    "doesn't match expected region:" + region;
215       throw new CorruptedSnapshotException(msg, snapshot);
216     }
217   }
218 }