1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.io.hfile;
19
20 import java.io.ByteArrayOutputStream;
21 import java.io.IOException;
22 import java.nio.ByteBuffer;
23 import java.util.zip.Checksum;
24
25 import org.apache.hadoop.fs.Path;
26 import org.apache.hadoop.io.DataOutputBuffer;
27 import org.apache.hadoop.hbase.HConstants;
28 import org.apache.hadoop.hbase.util.Bytes;
29 import org.apache.hadoop.hbase.util.ChecksumFactory;
30 import org.apache.hadoop.hbase.util.ChecksumType;
31
32
33
34
35 public class ChecksumUtil {
36
37
38 private static byte[] DUMMY_VALUE = new byte[128 * HFileBlock.CHECKSUM_SIZE];
39
40
41
42
43
44
45
46
47 private static boolean generateExceptions = false;
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63 static void generateChecksums(byte[] indata,
64 int startOffset, int endOffset,
65 byte[] outdata, int outOffset,
66 ChecksumType checksumType,
67 int bytesPerChecksum) throws IOException {
68
69 if (checksumType == ChecksumType.NULL) {
70 return;
71 }
72
73 Checksum checksum = checksumType.getChecksumObject();
74 int bytesLeft = endOffset - startOffset;
75 int chunkNum = 0;
76
77 while (bytesLeft > 0) {
78
79 checksum.reset();
80 int count = Math.min(bytesLeft, bytesPerChecksum);
81 checksum.update(indata, startOffset, count);
82
83
84 int cksumValue = (int)checksum.getValue();
85 outOffset = Bytes.putInt(outdata, outOffset, cksumValue);
86 chunkNum++;
87 startOffset += count;
88 bytesLeft -= count;
89 }
90 }
91
92
93
94
95
96
97
98
99
100
101 static boolean validateBlockChecksum(Path path, HFileBlock block,
102 byte[] data, int hdrSize) throws IOException {
103
104
105
106
107
108
109
110 if (block.getMinorVersion() < HFileBlock.MINOR_VERSION_WITH_CHECKSUM) {
111 return false;
112 }
113
114
115
116
117
118 ChecksumType cktype = ChecksumType.codeToType(block.getChecksumType());
119 if (cktype == ChecksumType.NULL) {
120 return true;
121 }
122 Checksum checksumObject = cktype.getChecksumObject();
123 checksumObject.reset();
124
125
126 int bytesPerChecksum = block.getBytesPerChecksum();
127
128
129 if (bytesPerChecksum < hdrSize) {
130 String msg = "Unsupported value of bytesPerChecksum. " +
131 " Minimum is " + hdrSize +
132 " but the configured value is " + bytesPerChecksum;
133 HFile.LOG.warn(msg);
134 return false;
135 }
136
137 ByteBuffer hdr = block.getBufferWithHeader();
138 checksumObject.update(hdr.array(), hdr.arrayOffset(), hdrSize);
139
140 int off = hdrSize;
141 int consumed = hdrSize;
142 int bytesLeft = block.getOnDiskDataSizeWithHeader() - off;
143 int cksumOffset = block.getOnDiskDataSizeWithHeader();
144
145
146 while (bytesLeft > 0) {
147 int thisChunkSize = bytesPerChecksum - consumed;
148 int count = Math.min(bytesLeft, thisChunkSize);
149 checksumObject.update(data, off, count);
150
151 int storedChecksum = Bytes.toInt(data, cksumOffset);
152 if (storedChecksum != (int)checksumObject.getValue()) {
153 String msg = "File " + path +
154 " Stored checksum value of " + storedChecksum +
155 " at offset " + cksumOffset +
156 " does not match computed checksum " +
157 checksumObject.getValue() +
158 ", total data size " + data.length +
159 " Checksum data range offset " + off + " len " + count +
160 HFileBlock.toStringHeader(block.getBufferReadOnly());
161 HFile.LOG.warn(msg);
162 if (generateExceptions) {
163 throw new IOException(msg);
164 } else {
165 return false;
166 }
167 }
168 cksumOffset += HFileBlock.CHECKSUM_SIZE;
169 bytesLeft -= count;
170 off += count;
171 consumed = 0;
172 checksumObject.reset();
173 }
174 return true;
175 }
176
177
178
179
180
181
182
183
184 static long numBytes(long datasize, int bytesPerChecksum) {
185 return numChunks(datasize, bytesPerChecksum) *
186 HFileBlock.CHECKSUM_SIZE;
187 }
188
189
190
191
192
193
194
195
196 static long numChunks(long datasize, int bytesPerChecksum) {
197 long numChunks = datasize/bytesPerChecksum;
198 if (datasize % bytesPerChecksum != 0) {
199 numChunks++;
200 }
201 return numChunks;
202 }
203
204
205
206
207
208
209
210
211
212 static void reserveSpaceForChecksums(ByteArrayOutputStream baos,
213 int numBytes, int bytesPerChecksum) throws IOException {
214 long numChunks = numChunks(numBytes, bytesPerChecksum);
215 long bytesLeft = numChunks * HFileBlock.CHECKSUM_SIZE;
216 while (bytesLeft > 0) {
217 long count = Math.min(bytesLeft, DUMMY_VALUE.length);
218 baos.write(DUMMY_VALUE, 0, (int)count);
219 bytesLeft -= count;
220 }
221 }
222
223
224
225
226
227
228
229 public static void generateExceptionForChecksumFailureForTest(boolean value) {
230 generateExceptions = value;
231 }
232 }
233