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.Set;
23  
24  import org.apache.hadoop.classification.InterfaceAudience;
25  import org.apache.hadoop.classification.InterfaceStability;
26  import org.apache.hadoop.fs.FileStatus;
27  import org.apache.hadoop.fs.FileSystem;
28  import org.apache.hadoop.fs.Path;
29  import org.apache.hadoop.fs.PathFilter;
30  import org.apache.hadoop.hbase.TableName;
31  import org.apache.hadoop.hbase.HRegionInfo;
32  import org.apache.hadoop.hbase.ServerName;
33  import org.apache.hadoop.hbase.catalog.MetaReader;
34  import org.apache.hadoop.hbase.master.MasterServices;
35  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
36  import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.SnapshotDescription;
37  import org.apache.hadoop.hbase.regionserver.HRegionFileSystem;
38  import org.apache.hadoop.hbase.regionserver.StoreFileInfo;
39  import org.apache.hadoop.hbase.snapshot.CorruptedSnapshotException;
40  import org.apache.hadoop.hbase.snapshot.SnapshotDescriptionUtils;
41  import org.apache.hadoop.hbase.snapshot.TakeSnapshotUtils;
42  import org.apache.hadoop.hbase.util.FSTableDescriptors;
43  import org.apache.hadoop.hbase.util.FSUtils;
44  import org.apache.hadoop.hbase.util.HFileArchiveUtil;
45  
46  /**
47   * General snapshot verification on the master.
48   * <p>
49   * This is a light-weight verification mechanism for all the files in a snapshot. It doesn't
50   * attempt to verify that the files are exact copies (that would be paramount to taking the
51   * snapshot again!), but instead just attempts to ensure that the files match the expected
52   * files and are the same length.
53   * <p>
54   * Taking an online snapshots can race against other operations and this is an last line of
55   * defense.  For example, if meta changes between when snapshots are taken not all regions of a
56   * table may be present.  This can be caused by a region split (daughters present on this scan,
57   * but snapshot took parent), or move (snapshots only checks lists of region servers, a move could
58   * have caused a region to be skipped or done twice).
59   * <p>
60   * Current snapshot files checked:
61   * <ol>
62   * <li>SnapshotDescription is readable</li>
63   * <li>Table info is readable</li>
64   * <li>Regions</li>
65   * <ul>
66   * <li>Matching regions in the snapshot as currently in the table</li>
67   * <li>{@link HRegionInfo} matches the current and stored regions</li>
68   * <li>All referenced hfiles have valid names</li>
69   * <li>All the hfiles are present (either in .archive directory in the region)</li>
70   * <li>All recovered.edits files are present (by name) and have the correct file size</li>
71   * </ul>
72   * </ol>
73   */
74  @InterfaceAudience.Private
75  @InterfaceStability.Unstable
76  public final class MasterSnapshotVerifier {
77  
78    private SnapshotDescription snapshot;
79    private FileSystem fs;
80    private Path rootDir;
81    private TableName tableName;
82    private MasterServices services;
83  
84    /**
85     * @param services services for the master
86     * @param snapshot snapshot to check
87     * @param rootDir root directory of the hbase installation.
88     */
89    public MasterSnapshotVerifier(MasterServices services, SnapshotDescription snapshot, Path rootDir) {
90      this.fs = services.getMasterFileSystem().getFileSystem();
91      this.services = services;
92      this.snapshot = snapshot;
93      this.rootDir = rootDir;
94      this.tableName = TableName.valueOf(snapshot.getTable());
95    }
96  
97    /**
98     * Verify that the snapshot in the directory is a valid snapshot
99     * @param snapshotDir snapshot directory to check
100    * @param snapshotServers {@link ServerName} of the servers that are involved in the snapshot
101    * @throws CorruptedSnapshotException if the snapshot is invalid
102    * @throws IOException if there is an unexpected connection issue to the filesystem
103    */
104   public void verifySnapshot(Path snapshotDir, Set<String> snapshotServers)
105       throws CorruptedSnapshotException, IOException {
106     // verify snapshot info matches
107     verifySnapshotDescription(snapshotDir);
108 
109     // check that tableinfo is a valid table description
110     verifyTableInfo(snapshotDir);
111 
112     // check that each region is valid
113     verifyRegions(snapshotDir);
114   }
115 
116   /**
117    * Check that the snapshot description written in the filesystem matches the current snapshot
118    * @param snapshotDir snapshot directory to check
119    */
120   private void verifySnapshotDescription(Path snapshotDir) throws CorruptedSnapshotException {
121     SnapshotDescription found = SnapshotDescriptionUtils.readSnapshotInfo(fs, snapshotDir);
122     if (!this.snapshot.equals(found)) {
123       throw new CorruptedSnapshotException("Snapshot read (" + found
124           + ") doesn't equal snapshot we ran (" + snapshot + ").", snapshot);
125     }
126   }
127 
128   /**
129    * Check that the table descriptor for the snapshot is a valid table descriptor
130    * @param snapshotDir snapshot directory to check
131    */
132   private void verifyTableInfo(Path snapshotDir) throws IOException {
133     FSTableDescriptors.getTableDescriptorFromFs(fs, snapshotDir);
134   }
135 
136   /**
137    * Check that all the regions in the snapshot are valid, and accounted for.
138    * @param snapshotDir snapshot directory to check
139    * @throws IOException if we can't reach .META. or read the files from the FS
140    */
141   private void verifyRegions(Path snapshotDir) throws IOException {
142     List<HRegionInfo> regions = MetaReader.getTableRegions(this.services.getCatalogTracker(),
143         tableName);
144     for (HRegionInfo region : regions) {
145       // if offline split parent, skip it
146       if (region.isOffline() && (region.isSplit() || region.isSplitParent())) {
147         continue;
148       }
149 
150       verifyRegion(fs, snapshotDir, region);
151     }
152   }
153 
154   /**
155    * Verify that the region (regioninfo, hfiles) are valid
156    * @param fs the FileSystem instance
157    * @param snapshotDir snapshot directory to check
158    * @param region the region to check
159    */
160   private void verifyRegion(FileSystem fs, Path snapshotDir, HRegionInfo region) throws IOException {
161     // make sure we have region in the snapshot
162     Path regionDir = new Path(snapshotDir, region.getEncodedName());
163     if (!fs.exists(regionDir)) {
164       // could happen due to a move or split race.
165       throw new CorruptedSnapshotException("No region directory found for region:" + region,
166           snapshot);
167     }
168     // make sure we have the region info in the snapshot
169     Path regionInfo = new Path(regionDir, HRegionFileSystem.REGION_INFO_FILE);
170     // make sure the file exists
171     if (!fs.exists(regionInfo)) {
172       throw new CorruptedSnapshotException("No region info found for region:" + region, snapshot);
173     }
174 
175     HRegionInfo found = HRegionFileSystem.loadRegionInfoFileContent(fs, regionDir);
176     if (!region.equals(found)) {
177       throw new CorruptedSnapshotException("Found region info (" + found
178         + ") doesn't match expected region:" + region, snapshot);
179     }
180 
181     // make sure we have the expected recovered edits files
182     TakeSnapshotUtils.verifyRecoveredEdits(fs, snapshotDir, found, snapshot);
183 
184     // check for the existance of each hfile
185     PathFilter familiesDirs = new FSUtils.FamilyDirFilter(fs);
186     FileStatus[] columnFamilies = FSUtils.listStatus(fs, regionDir, familiesDirs);
187     // should we do some checking here to make sure the cfs are correct?
188     if (columnFamilies == null) return;
189 
190     // setup the suffixes for the snapshot directories
191     Path tableNameSuffix = FSUtils.getTableDir(new Path("./"), tableName);
192     Path regionNameSuffix = new Path(tableNameSuffix, region.getEncodedName());
193 
194     // get the potential real paths
195     Path archivedRegion = new Path(HFileArchiveUtil.getArchivePath(services.getConfiguration()),
196         regionNameSuffix);
197     Path realRegion = new Path(rootDir, regionNameSuffix);
198 
199     // loop through each cf and check we can find each of the hfiles
200     for (FileStatus cf : columnFamilies) {
201       FileStatus[] hfiles = FSUtils.listStatus(fs, cf.getPath(), null);
202       // should we check if there should be hfiles?
203       if (hfiles == null || hfiles.length == 0) continue;
204 
205       Path realCfDir = new Path(realRegion, cf.getPath().getName());
206       Path archivedCfDir = new Path(archivedRegion, cf.getPath().getName());
207       for (FileStatus hfile : hfiles) {
208         // make sure the name is correct
209         if (!StoreFileInfo.validateStoreFileName(hfile.getPath().getName())) {
210           throw new CorruptedSnapshotException("HFile: " + hfile.getPath()
211               + " is not a valid hfile name.", snapshot);
212         }
213 
214         // check to see if hfile is present in the real table
215         String fileName = hfile.getPath().getName();
216         Path file = new Path(realCfDir, fileName);
217         Path archived = new Path(archivedCfDir, fileName);
218         if (!fs.exists(file) && !fs.exists(archived)) {
219           throw new CorruptedSnapshotException("Can't find hfile: " + hfile.getPath()
220               + " in the real (" + realCfDir + ") or archive (" + archivedCfDir
221               + ") directory for the primary table.", snapshot);
222         }
223       }
224     }
225   }
226 }