1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
51
52
53
54
55
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
87
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 }