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.FSDataInputStream;
27  import org.apache.hadoop.fs.FileStatus;
28  import org.apache.hadoop.fs.FileSystem;
29  import org.apache.hadoop.fs.Path;
30  import org.apache.hadoop.fs.PathFilter;
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.generated.HBaseProtos.SnapshotDescription;
36  import org.apache.hadoop.hbase.regionserver.HRegion;
37  import org.apache.hadoop.hbase.regionserver.HRegionFileSystem;
38  import org.apache.hadoop.hbase.regionserver.StoreFileInfo;
39  import org.apache.hadoop.hbase.exceptions.CorruptedSnapshotException;
40  import org.apache.hadoop.hbase.snapshot.SnapshotDescriptionUtils;
41  import org.apache.hadoop.hbase.snapshot.TakeSnapshotUtils;
42  import org.apache.hadoop.hbase.util.Bytes;
43  import org.apache.hadoop.hbase.util.FSTableDescriptors;
44  import org.apache.hadoop.hbase.util.FSUtils;
45  import org.apache.hadoop.hbase.util.HFileArchiveUtil;
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   * <ul>
67   * <li>Matching regions in the snapshot as currently in the table</li>
68   * <li>{@link HRegionInfo} matches the current and stored regions</li>
69   * <li>All referenced hfiles have valid names</li>
70   * <li>All the hfiles are present (either in .archive directory in the region)</li>
71   * <li>All recovered.edits files are present (by name) and have the correct file size</li>
72   * </ul>
73   * </ol>
74   */
75  @InterfaceAudience.Private
76  @InterfaceStability.Unstable
77  public final class MasterSnapshotVerifier {
78  
79    private SnapshotDescription snapshot;
80    private FileSystem fs;
81    private Path rootDir;
82    private String tableName;
83    private MasterServices services;
84  
85    /**
86     * @param services services for the master
87     * @param snapshot snapshot to check
88     * @param rootDir root directory of the hbase installation.
89     */
90    public MasterSnapshotVerifier(MasterServices services, SnapshotDescription snapshot, Path rootDir) {
91      this.fs = services.getMasterFileSystem().getFileSystem();
92      this.services = services;
93      this.snapshot = snapshot;
94      this.rootDir = rootDir;
95      this.tableName = 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 ServerName} of the servers that are involved in the snapshot
102    * @throws CorruptedSnapshotException if the snapshot is invalid
103    * @throws IOException if there is an unexpected connection issue to the filesystem
104    */
105   public void verifySnapshot(Path snapshotDir, Set<String> snapshotServers)
106       throws CorruptedSnapshotException, IOException {
107     // verify snapshot info matches
108     verifySnapshotDescription(snapshotDir);
109 
110     // check that tableinfo is a valid table description
111     verifyTableInfo(snapshotDir);
112 
113     // check that each region is valid
114     verifyRegions(snapshotDir);
115   }
116 
117   /**
118    * Check that the snapshot description written in the filesystem matches the current snapshot
119    * @param snapshotDir snapshot directory to check
120    */
121   private void verifySnapshotDescription(Path snapshotDir) throws CorruptedSnapshotException {
122     SnapshotDescription found = SnapshotDescriptionUtils.readSnapshotInfo(fs, snapshotDir);
123     if (!this.snapshot.equals(found)) {
124       throw new CorruptedSnapshotException("Snapshot read (" + found
125           + ") doesn't equal snapshot we ran (" + snapshot + ").", snapshot);
126     }
127   }
128 
129   /**
130    * Check that the table descriptor for the snapshot is a valid table descriptor
131    * @param snapshotDir snapshot directory to check
132    */
133   private void verifyTableInfo(Path snapshotDir) throws IOException {
134     FSTableDescriptors.getTableDescriptor(fs, snapshotDir);
135   }
136 
137   /**
138    * Check that all the regions in the snapshot are valid, and accounted for.
139    * @param snapshotDir snapshot directory to check
140    * @throws IOException if we can't reach .META. or read the files from the FS
141    */
142   private void verifyRegions(Path snapshotDir) throws IOException {
143     List<HRegionInfo> regions = MetaReader.getTableRegions(this.services.getCatalogTracker(),
144       Bytes.toBytes(tableName));
145     for (HRegionInfo region : regions) {
146       // if offline split parent, skip it
147       if (region.isOffline() && (region.isSplit() || region.isSplitParent())) {
148         continue;
149       }
150 
151       verifyRegion(fs, snapshotDir, region);
152     }
153   }
154 
155   /**
156    * Verify that the region (regioninfo, hfiles) are valid
157    * @param fs the FileSystem instance
158    * @param snapshotDir snapshot directory to check
159    * @param region the region to check
160    */
161   private void verifyRegion(FileSystem fs, Path snapshotDir, HRegionInfo region) throws IOException {
162     // make sure we have region in the snapshot
163     Path regionDir = new Path(snapshotDir, region.getEncodedName());
164     if (!fs.exists(regionDir)) {
165       // could happen due to a move or split race.
166       throw new CorruptedSnapshotException("No region directory found for region:" + region,
167           snapshot);
168     }
169     // make sure we have the region info in the snapshot
170     Path regionInfo = new Path(regionDir, HRegionFileSystem.REGION_INFO_FILE);
171     // make sure the file exists
172     if (!fs.exists(regionInfo)) {
173       throw new CorruptedSnapshotException("No region info found for region:" + region, snapshot);
174     }
175 
176     HRegionInfo found = HRegionFileSystem.loadRegionInfoFileContent(fs, regionDir);
177     if (!region.equals(found)) {
178       throw new CorruptedSnapshotException("Found region info (" + found
179         + ") doesn't match expected region:" + region, snapshot);
180     }
181 
182     // make sure we have the expected recovered edits files
183     TakeSnapshotUtils.verifyRecoveredEdits(fs, snapshotDir, found, snapshot);
184 
185     // check for the existance of each hfile
186     PathFilter familiesDirs = new FSUtils.FamilyDirFilter(fs);
187     FileStatus[] columnFamilies = FSUtils.listStatus(fs, regionDir, familiesDirs);
188     // should we do some checking here to make sure the cfs are correct?
189     if (columnFamilies == null) return;
190 
191     // setup the suffixes for the snapshot directories
192     Path tableNameSuffix = new Path(tableName);
193     Path regionNameSuffix = new Path(tableNameSuffix, region.getEncodedName());
194 
195     // get the potential real paths
196     Path archivedRegion = new Path(HFileArchiveUtil.getArchivePath(services.getConfiguration()),
197         regionNameSuffix);
198     Path realRegion = new Path(rootDir, regionNameSuffix);
199 
200     // loop through each cf and check we can find each of the hfiles
201     for (FileStatus cf : columnFamilies) {
202       FileStatus[] hfiles = FSUtils.listStatus(fs, cf.getPath(), null);
203       // should we check if there should be hfiles?
204       if (hfiles == null || hfiles.length == 0) continue;
205 
206       Path realCfDir = new Path(realRegion, cf.getPath().getName());
207       Path archivedCfDir = new Path(archivedRegion, cf.getPath().getName());
208       for (FileStatus hfile : hfiles) {
209         // make sure the name is correct
210         if (!StoreFileInfo.validateStoreFileName(hfile.getPath().getName())) {
211           throw new CorruptedSnapshotException("HFile: " + hfile.getPath()
212               + " is not a valid hfile name.", snapshot);
213         }
214 
215         // check to see if hfile is present in the real table
216         String fileName = hfile.getPath().getName();
217         Path file = new Path(realCfDir, fileName);
218         Path archived = new Path(archivedCfDir, fileName);
219         if (!fs.exists(file) && !file.equals(archived)) {
220           throw new CorruptedSnapshotException("Can't find hfile: " + hfile.getPath()
221               + " in the real (" + realCfDir + ") or archive (" + archivedCfDir
222               + ") directory for the primary table.", snapshot);
223         }
224       }
225     }
226   }
227 }