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