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  
19  package org.apache.hadoop.hbase.snapshot;
20  
21  import com.google.protobuf.CodedInputStream;
22  import com.google.protobuf.InvalidProtocolBufferException;
23  import java.io.IOException;
24  import java.io.InterruptedIOException;
25  import java.util.ArrayList;
26  import java.util.List;
27  import java.util.concurrent.Callable;
28  import java.util.concurrent.Executor;
29  import java.util.concurrent.ExecutionException;
30  import java.util.concurrent.ExecutorCompletionService;
31  
32  import org.apache.commons.logging.Log;
33  import org.apache.commons.logging.LogFactory;
34  import org.apache.hadoop.hbase.classification.InterfaceAudience;
35  import org.apache.hadoop.conf.Configuration;
36  import org.apache.hadoop.fs.FSDataInputStream;
37  import org.apache.hadoop.fs.FSDataOutputStream;
38  import org.apache.hadoop.fs.FileStatus;
39  import org.apache.hadoop.fs.FileSystem;
40  import org.apache.hadoop.fs.Path;
41  import org.apache.hadoop.fs.PathFilter;
42  import org.apache.hadoop.hbase.HRegionInfo;
43  import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.SnapshotDescription;
44  import org.apache.hadoop.hbase.protobuf.generated.SnapshotProtos.SnapshotRegionManifest;
45  import org.apache.hadoop.hbase.regionserver.StoreFileInfo;
46  import org.apache.hadoop.hbase.util.ByteStringer;
47  import org.apache.hadoop.hbase.util.FSUtils;
48  
49  /**
50   * DO NOT USE DIRECTLY. USE {@link SnapshotManifest}.
51   *
52   * Snapshot v2 layout format
53   *  - Single Manifest file containing all the information of regions
54   *  - In the online-snapshot case each region will write a "region manifest"
55   *      /snapshotName/manifest.regionName
56   */
57  @InterfaceAudience.Private
58  public final class SnapshotManifestV2 {
59    private static final Log LOG = LogFactory.getLog(SnapshotManifestV2.class);
60  
61    public static final int DESCRIPTOR_VERSION = 2;
62  
63    public static final String SNAPSHOT_MANIFEST_PREFIX = "region-manifest.";
64  
65    private SnapshotManifestV2() {}
66  
67    static class ManifestBuilder implements SnapshotManifest.RegionVisitor<
68                      SnapshotRegionManifest.Builder, SnapshotRegionManifest.FamilyFiles.Builder> {
69      private final Configuration conf;
70      private final Path snapshotDir;
71      private final FileSystem fs;
72  
73      public ManifestBuilder(final Configuration conf, final FileSystem fs, final Path snapshotDir) {
74        this.snapshotDir = snapshotDir;
75        this.conf = conf;
76        this.fs = fs;
77      }
78  
79      public SnapshotRegionManifest.Builder regionOpen(final HRegionInfo regionInfo) {
80        SnapshotRegionManifest.Builder manifest = SnapshotRegionManifest.newBuilder();
81        manifest.setRegionInfo(HRegionInfo.convert(regionInfo));
82        return manifest;
83      }
84  
85      public void regionClose(final SnapshotRegionManifest.Builder region) throws IOException {
86        // we should ensure the snapshot dir exist, maybe it has been deleted by master
87        // see HBASE-16464
88        if (fs.exists(snapshotDir)) {
89          SnapshotRegionManifest manifest = region.build();
90          FSDataOutputStream stream = fs.create(getRegionManifestPath(snapshotDir, manifest));
91          try {
92            manifest.writeTo(stream);
93          } finally {
94            stream.close();
95          }
96        } else {
97          LOG.warn("can't write manifest without parent dir, maybe it has been deleted by master?");
98        }
99      }
100 
101     public SnapshotRegionManifest.FamilyFiles.Builder familyOpen(
102         final SnapshotRegionManifest.Builder region, final byte[] familyName) {
103       SnapshotRegionManifest.FamilyFiles.Builder family =
104           SnapshotRegionManifest.FamilyFiles.newBuilder();
105       family.setFamilyName(ByteStringer.wrap(familyName));
106       return family;
107     }
108 
109     public void familyClose(final SnapshotRegionManifest.Builder region,
110         final SnapshotRegionManifest.FamilyFiles.Builder family) {
111       region.addFamilyFiles(family.build());
112     }
113 
114     public void storeFile(final SnapshotRegionManifest.Builder region,
115         final SnapshotRegionManifest.FamilyFiles.Builder family, final StoreFileInfo storeFile)
116         throws IOException {
117       SnapshotRegionManifest.StoreFile.Builder sfManifest =
118             SnapshotRegionManifest.StoreFile.newBuilder();
119       sfManifest.setName(storeFile.getPath().getName());
120       if (storeFile.isReference()) {
121         sfManifest.setReference(storeFile.getReference().convert());
122       }
123       sfManifest.setFileSize(storeFile.getReferencedFileStatus(fs).getLen());
124       family.addStoreFiles(sfManifest.build());
125     }
126   }
127 
128   static List<SnapshotRegionManifest> loadRegionManifests(final Configuration conf,
129       final Executor executor, final FileSystem fs, final Path snapshotDir,
130       final SnapshotDescription desc, final int manifestSizeLimit) throws IOException {
131     FileStatus[] manifestFiles = FSUtils.listStatus(fs, snapshotDir, new PathFilter() {
132       @Override
133       public boolean accept(Path path) {
134         return path.getName().startsWith(SNAPSHOT_MANIFEST_PREFIX);
135       }
136     });
137 
138     if (manifestFiles == null || manifestFiles.length == 0) return null;
139 
140     final ExecutorCompletionService<SnapshotRegionManifest> completionService =
141       new ExecutorCompletionService<SnapshotRegionManifest>(executor);
142     for (final FileStatus st: manifestFiles) {
143       completionService.submit(new Callable<SnapshotRegionManifest>() {
144         @Override
145         public SnapshotRegionManifest call() throws IOException {
146           FSDataInputStream stream = fs.open(st.getPath());
147           CodedInputStream cin = CodedInputStream.newInstance(stream);
148           cin.setSizeLimit(manifestSizeLimit);
149 
150           try {
151             return SnapshotRegionManifest.parseFrom(cin);
152           } finally {
153             stream.close();
154           }
155         }
156       });
157     }
158 
159     ArrayList<SnapshotRegionManifest> regionsManifest =
160         new ArrayList<SnapshotRegionManifest>(manifestFiles.length);
161     try {
162       for (int i = 0; i < manifestFiles.length; ++i) {
163         regionsManifest.add(completionService.take().get());
164       }
165     } catch (InterruptedException e) {
166       throw new InterruptedIOException(e.getMessage());
167     } catch (ExecutionException e) {
168       Throwable t = e.getCause();
169 
170       if(t instanceof InvalidProtocolBufferException) {
171         throw (InvalidProtocolBufferException)t;
172       } else {
173         IOException ex = new IOException("ExecutionException");
174         ex.initCause(e.getCause());
175         throw ex;
176       }
177     }
178     return regionsManifest;
179   }
180 
181   static void deleteRegionManifest(final FileSystem fs, final Path snapshotDir,
182       final SnapshotRegionManifest manifest) throws IOException {
183     fs.delete(getRegionManifestPath(snapshotDir, manifest), true);
184   }
185 
186   private static Path getRegionManifestPath(final Path snapshotDir,
187       final SnapshotRegionManifest manifest) {
188     String regionName = SnapshotManifest.getRegionNameFromManifest(manifest);
189     return new Path(snapshotDir, SNAPSHOT_MANIFEST_PREFIX + regionName);
190   }
191 }