-
Notifications
You must be signed in to change notification settings - Fork 984
/
AsyncPageReader.java
413 lines (359 loc) · 15.9 KB
/
AsyncPageReader.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.drill.exec.store.parquet.columnreaders;
import com.google.common.base.Stopwatch;
import io.netty.buffer.DrillBuf;
import io.netty.buffer.ByteBufUtil;
import org.apache.drill.common.exceptions.ExecutionSetupException;
import org.apache.drill.common.exceptions.UserException;
import org.apache.hadoop.io.compress.Decompressor;
import org.apache.hadoop.io.compress.DirectDecompressor;
import org.apache.hadoop.io.compress.GzipCodec;
import org.apache.parquet.hadoop.CodecFactory;
import org.apache.parquet.hadoop.codec.SnappyCodec;
import org.apache.parquet.hadoop.metadata.ColumnChunkMetaData;
import org.apache.drill.exec.util.filereader.DirectBufInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.parquet.column.page.DictionaryPage;
import org.apache.parquet.format.PageHeader;
import org.apache.parquet.format.PageType;
import org.apache.parquet.format.Util;
import org.apache.parquet.hadoop.metadata.CompressionCodecName;
import org.xerial.snappy.Snappy;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import static org.apache.parquet.column.Encoding.valueOf;
class AsyncPageReader extends PageReader {
static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(AsyncPageReader.class);
private ExecutorService threadPool;
private Future<ReadStatus> asyncPageRead;
AsyncPageReader(ColumnReader<?> parentStatus, FileSystem fs, Path path,
ColumnChunkMetaData columnChunkMetaData) throws ExecutionSetupException {
super(parentStatus, fs, path, columnChunkMetaData);
if (threadPool == null) {
threadPool = parentColumnReader.parentReader.getOperatorContext().getScanExecutor();
}
asyncPageRead = threadPool.submit(new AsyncPageReaderTask());
}
@Override
protected void loadDictionaryIfExists(final ColumnReader<?> parentStatus,
final ColumnChunkMetaData columnChunkMetaData, final DirectBufInputStream f) throws UserException {
if (columnChunkMetaData.getDictionaryPageOffset() > 0) {
try {
assert(columnChunkMetaData.getDictionaryPageOffset() >= dataReader.getPos() );
dataReader.skip(columnChunkMetaData.getDictionaryPageOffset() - dataReader.getPos());
} catch (IOException e) {
handleAndThrowException(e, "Error Reading dictionary page.");
}
// parent constructor may call this method before the thread pool is set.
if (threadPool == null) {
threadPool = parentColumnReader.parentReader.getOperatorContext().getScanExecutor();
}
asyncPageRead = threadPool.submit(new AsyncPageReaderTask());
readDictionaryPage(asyncPageRead, parentStatus);
asyncPageRead = null; // reset after consuming
}
}
private DrillBuf getDecompressedPageData(ReadStatus readStatus) {
DrillBuf data;
boolean isDictionary = false;
synchronized (this) {
data = readStatus.getPageData();
readStatus.setPageData(null);
isDictionary = readStatus.isDictionaryPage;
}
if (parentColumnReader.columnChunkMetaData.getCodec() != CompressionCodecName.UNCOMPRESSED) {
DrillBuf compressedData = data;
data = decompress(readStatus.getPageHeader(), compressedData);
synchronized (this) {
readStatus.setPageData(null);
}
compressedData.release();
} else {
if (isDictionary) {
stats.totalDictPageReadBytes.addAndGet(readStatus.bytesRead);
} else {
stats.totalDataPageReadBytes.addAndGet(readStatus.bytesRead);
}
}
return data;
}
// Read and decode the dictionary and the header
private void readDictionaryPage(final Future<ReadStatus> asyncPageRead,
final ColumnReader<?> parentStatus) throws UserException {
try {
Stopwatch timer = Stopwatch.createStarted();
ReadStatus readStatus = asyncPageRead.get();
long timeBlocked = timer.elapsed(TimeUnit.NANOSECONDS);
stats.timeDiskScanWait.addAndGet(timeBlocked);
stats.timeDiskScan.addAndGet(readStatus.getDiskScanTime());
stats.numDictPageLoads.incrementAndGet();
stats.timeDictPageLoads.addAndGet(timeBlocked+readStatus.getDiskScanTime());
readDictionaryPageData(readStatus, parentStatus);
} catch (Exception e) {
handleAndThrowException(e, "Error reading dictionary page.");
}
}
// Read and decode the dictionary data
private void readDictionaryPageData(final ReadStatus readStatus, final ColumnReader<?> parentStatus)
throws UserException {
try {
pageHeader = readStatus.getPageHeader();
int uncompressedSize = pageHeader.getUncompressed_page_size();
final DrillBuf dictionaryData = getDecompressedPageData(readStatus);
Stopwatch timer = Stopwatch.createStarted();
allocatedDictionaryBuffers.add(dictionaryData);
DictionaryPage page = new DictionaryPage(asBytesInput(dictionaryData, 0, uncompressedSize),
pageHeader.uncompressed_page_size, pageHeader.dictionary_page_header.num_values,
valueOf(pageHeader.dictionary_page_header.encoding.name()));
this.dictionary = page.getEncoding().initDictionary(parentStatus.columnDescriptor, page);
long timeToDecode = timer.elapsed(TimeUnit.NANOSECONDS);
stats.timeDictPageDecode.addAndGet(timeToDecode);
} catch (Exception e) {
handleAndThrowException(e, "Error decoding dictionary page.");
}
}
private void handleAndThrowException(Exception e, String msg) throws UserException {
UserException ex = UserException.dataReadError(e).message(msg)
.pushContext("Row Group Start: ", this.parentColumnReader.columnChunkMetaData.getStartingPos())
.pushContext("Column: ", this.parentColumnReader.schemaElement.getName())
.pushContext("File: ", this.fileName).build(logger);
throw ex;
}
private DrillBuf decompress(PageHeader pageHeader, DrillBuf compressedData) {
DrillBuf pageDataBuf = null;
Stopwatch timer = Stopwatch.createUnstarted();
long timeToRead;
int compressedSize = pageHeader.getCompressed_page_size();
int uncompressedSize = pageHeader.getUncompressed_page_size();
pageDataBuf = allocateTemporaryBuffer(uncompressedSize);
try {
timer.start();
CompressionCodecName codecName = parentColumnReader.columnChunkMetaData.getCodec();
ByteBuffer input = compressedData.nioBuffer(0, compressedSize);
ByteBuffer output = pageDataBuf.nioBuffer(0, uncompressedSize);
DecompressionHelper decompressionHelper = new DecompressionHelper(codecName);
decompressionHelper.decompress(input, compressedSize, output, uncompressedSize);
pageDataBuf.writerIndex(uncompressedSize);
timeToRead = timer.elapsed(TimeUnit.NANOSECONDS);
this.updateStats(pageHeader, "Decompress", 0, timeToRead, compressedSize, uncompressedSize);
} catch (IOException e) {
handleAndThrowException(e, "Error decompressing data.");
}
return pageDataBuf;
}
@Override protected void nextInternal() throws IOException {
ReadStatus readStatus = null;
try {
Stopwatch timer = Stopwatch.createStarted();
readStatus = asyncPageRead.get();
long timeBlocked = timer.elapsed(TimeUnit.NANOSECONDS);
stats.timeDiskScanWait.addAndGet(timeBlocked);
stats.timeDiskScan.addAndGet(readStatus.getDiskScanTime());
if (readStatus.isDictionaryPage) {
stats.numDictPageLoads.incrementAndGet();
stats.timeDictPageLoads.addAndGet(timeBlocked + readStatus.getDiskScanTime());
} else {
stats.numDataPageLoads.incrementAndGet();
stats.timeDataPageLoads.addAndGet(timeBlocked + readStatus.getDiskScanTime());
}
pageHeader = readStatus.getPageHeader();
// reset this. At the time of calling close, if this is not null then a pending asyncPageRead needs to be consumed
asyncPageRead = null;
} catch (Exception e) {
handleAndThrowException(e, "Error reading page data.");
}
// TODO - figure out if we need multiple dictionary pages, I believe it may be limited to one
// I think we are clobbering parts of the dictionary if there can be multiple pages of dictionary
do {
if (pageHeader.getType() == PageType.DICTIONARY_PAGE) {
readDictionaryPageData(readStatus, parentColumnReader);
// Ugly. Use the Async task to make a synchronous read call.
readStatus = new AsyncPageReaderTask().call();
pageHeader = readStatus.getPageHeader();
}
} while (pageHeader.getType() == PageType.DICTIONARY_PAGE);
if (parentColumnReader.totalValuesRead + readStatus.getValuesRead()
< parentColumnReader.columnChunkMetaData.getValueCount()) {
asyncPageRead = threadPool.submit(new AsyncPageReaderTask());
}
pageHeader = readStatus.getPageHeader();
pageData = getDecompressedPageData(readStatus);
}
@Override public void clear() {
if (asyncPageRead != null) {
try {
final ReadStatus readStatus = asyncPageRead.get();
readStatus.getPageData().release();
} catch (Exception e) {
// Do nothing.
}
}
super.clear();
}
public static class ReadStatus {
private PageHeader pageHeader;
private DrillBuf pageData;
private boolean isDictionaryPage = false;
private long bytesRead = 0;
private long valuesRead = 0;
private long diskScanTime = 0;
public synchronized PageHeader getPageHeader() {
return pageHeader;
}
public synchronized void setPageHeader(PageHeader pageHeader) {
this.pageHeader = pageHeader;
}
public synchronized DrillBuf getPageData() {
return pageData;
}
public synchronized void setPageData(DrillBuf pageData) {
this.pageData = pageData;
}
public synchronized boolean isDictionaryPage() {
return isDictionaryPage;
}
public synchronized void setIsDictionaryPage(boolean isDictionaryPage) {
this.isDictionaryPage = isDictionaryPage;
}
public synchronized long getBytesRead() {
return bytesRead;
}
public synchronized void setBytesRead(long bytesRead) {
this.bytesRead = bytesRead;
}
public synchronized long getValuesRead() {
return valuesRead;
}
public synchronized void setValuesRead(long valuesRead) {
this.valuesRead = valuesRead;
}
public long getDiskScanTime() {
return diskScanTime;
}
public void setDiskScanTime(long diskScanTime) {
this.diskScanTime = diskScanTime;
}
}
private class AsyncPageReaderTask implements Callable<ReadStatus> {
private final AsyncPageReader parent = AsyncPageReader.this;
public AsyncPageReaderTask() {
}
@Override public ReadStatus call() throws IOException {
ReadStatus readStatus = new ReadStatus();
String oldname = Thread.currentThread().getName();
String name = parent.parentColumnReader.columnChunkMetaData.toString();
Thread.currentThread().setName(name);
long bytesRead = 0;
long valuesRead = 0;
Stopwatch timer = Stopwatch.createStarted();
DrillBuf pageData = null;
try {
long s = parent.dataReader.getPos();
PageHeader pageHeader = Util.readPageHeader(parent.dataReader);
long e = parent.dataReader.getPos();
if (logger.isTraceEnabled()) {
logger.trace("[{}]: Read Page Header : ReadPos = {} : Bytes Read = {} ", name, s, e - s);
}
int compressedSize = pageHeader.getCompressed_page_size();
s = parent.dataReader.getPos();
pageData = parent.dataReader.getNext(compressedSize);
e = parent.dataReader.getPos();
bytesRead = compressedSize;
if (logger.isTraceEnabled()) {
DrillBuf bufStart = pageData.slice(0, compressedSize>100?100:compressedSize);
int endOffset = compressedSize>100?compressedSize-100:0;
DrillBuf bufEnd = pageData.slice(endOffset, compressedSize-endOffset);
logger
.trace("[{}]: Read Page Data : ReadPos = {} : Bytes Read = {} : Buf Start = {} : Buf End = {} ",
name, s, e - s, ByteBufUtil.hexDump(bufStart), ByteBufUtil.hexDump(bufEnd));
}
synchronized (parent) {
if (pageHeader.getType() == PageType.DICTIONARY_PAGE) {
readStatus.setIsDictionaryPage(true);
valuesRead += pageHeader.getDictionary_page_header().getNum_values();
} else {
valuesRead += pageHeader.getData_page_header().getNum_values();
}
long timeToRead = timer.elapsed(TimeUnit.NANOSECONDS);
readStatus.setPageHeader(pageHeader);
readStatus.setPageData(pageData);
readStatus.setBytesRead(bytesRead);
readStatus.setValuesRead(valuesRead);
readStatus.setDiskScanTime(timeToRead);
}
} catch (Exception e) {
if (pageData != null) {
pageData.release();
}
throw e;
}
Thread.currentThread().setName(oldname);
return readStatus;
}
}
private class DecompressionHelper {
final CompressionCodecName codecName;
public DecompressionHelper(CompressionCodecName codecName) {
this.codecName = codecName;
}
public void decompress (ByteBuffer input, int compressedSize, ByteBuffer output, int uncompressedSize)
throws IOException {
// GZip != thread_safe, so we go off and do our own thing.
// The hadoop interface does not support ByteBuffer so we incur some
// expensive copying.
if (codecName == CompressionCodecName.GZIP) {
GzipCodec codec = new GzipCodec();
// DirectDecompressor: @see https://hadoop.apache.org/docs/r2.7.2/api/org/apache/hadoop/io/compress/DirectDecompressor.html
DirectDecompressor directDecompressor = codec.createDirectDecompressor();
if (directDecompressor != null) {
logger.debug("Using GZIP direct decompressor.");
directDecompressor.decompress(input, output);
} else {
logger.debug("Using GZIP (in)direct decompressor.");
Decompressor decompressor = codec.createDecompressor();
decompressor.reset();
byte[] inputBytes = new byte[compressedSize];
input.position(0);
input.get(inputBytes);
decompressor.setInput(inputBytes, 0, inputBytes.length);
byte[] outputBytes = new byte[uncompressedSize];
decompressor.decompress(outputBytes, 0, uncompressedSize);
output.clear();
output.put(outputBytes);
}
} else if (codecName == CompressionCodecName.SNAPPY) {
// For Snappy, just call the Snappy decompressor directly instead
// of going thru the DirectDecompressor class.
// The Snappy codec is itself thread safe, while going thru the DirectDecompressor path
// seems to have concurrency issues.
output.clear();
int size = Snappy.uncompress(input, output);
output.limit(size);
} else {
CodecFactory.BytesDecompressor decompressor = codecFactory.getDecompressor(parentColumnReader.columnChunkMetaData.getCodec());
decompressor.decompress(input, compressedSize, output, uncompressedSize);
}
}
}
}