-
Notifications
You must be signed in to change notification settings - Fork 4
/
db2i_ioBuffers.cc
551 lines (457 loc) · 14.5 KB
/
db2i_ioBuffers.cc
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
/*
Licensed Materials - Property of IBM
DB2 Storage Engine Enablement
Copyright IBM Corporation 2007,2008
All rights reserved
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
(a) Redistributions of source code must retain this list of conditions, the
copyright notice in section {d} below, and the disclaimer following this
list of conditions.
(b) Redistributions in binary form must reproduce this list of conditions, the
copyright notice in section (d) below, and the disclaimer following this
list of conditions, in the documentation and/or other materials provided
with the distribution.
(c) The name of IBM may not be used to endorse or promote products derived from
this software without specific prior written permission.
(d) The text of the required copyright notice is:
Licensed Materials - Property of IBM
DB2 Storage Engine Enablement
Copyright IBM Corporation 2007,2008
All rights reserved
THIS SOFTWARE IS PROVIDED BY IBM CORPORATION "AS IS" AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
SHALL IBM CORPORATION BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.
*/
#include "db2i_validatedPointer.h"
#include <sys/stat.h>
#include <fcntl.h>
#include <as400_types.h>
#include "db2i_ioBuffers.h"
// Needed for compilers which do not include fstatx in standard headers.
extern "C" int fstatx(int, struct stat *, int, int);
/**
Request another block of rows
Request the next set of rows from DB2. This must only be called after
newReadRequest().
@param orientation The direction to use when reading through the table.
*/
void IOAsyncReadBuffer::loadNewRows(char orientation)
{
rewind();
maxRows() = rowsToBlock;
DBUG_PRINT("db2i_ioBuffers::loadNewRows", ("Requesting %d rows, async = %d", rowsToBlock, readIsAsync));
rc = getBridge()->expectErrors(QMY_ERR_END_OF_BLOCK, QMY_ERR_LOB_SPACE_TOO_SMALL)
->read(file,
ptr(),
accessIntent,
commitLevel,
orientation,
readIsAsync,
rrnList,
0,
0,
0);
DBUG_PRINT("db2i_ioBuffers::loadNewRows", ("recordsRead: %d, rc: %d", (uint32)rowCount(), rc));
*releaseRowNeeded = true;
if (rc == QMY_ERR_END_OF_BLOCK)
{
// This is really just an informational error, so we ignore it.
rc = 0;
DBUG_PRINT("db2i_ioBuffers::loadNewRows", ("End of block signalled"));
}
else if (rc == QMY_ERR_END_OF_FILE)
{
// If we reach EOF or end-of-key, DB2 guarantees that no rows will be locked.
rc = HA_ERR_END_OF_FILE;
*releaseRowNeeded = false;
}
else if (rc == QMY_ERR_KEY_NOT_FOUND)
{
rc = HA_ERR_KEY_NOT_FOUND;
*releaseRowNeeded = false;
}
if (rc) closePipe();
}
/**
Empty the message pipe to prepare for another read.
*/
void IOAsyncReadBuffer::drainPipe()
{
DBUG_ASSERT(pipeState == PendingFullBufferMsg);
PipeRpy_t msg[32];
int bytes;
PipeRpy_t* lastMsg;
while ((bytes = read(msgPipe, msg, sizeof(msg))) > 0)
{
DBUG_PRINT("db2i_ioBuffers::drainPipe",("Pipe returned %d bytes", bytes));
lastMsg = &msg[bytes / (sizeof(msg[0]))-1];
if (lastMsg->CumRowCnt == maxRows() ||
lastMsg->RtnCod != 0)
{
pipeState = ConsumedFullBufferMsg;
break;
}
}
DBUG_PRINT("db2i_ioBuffers::drainPipe",("rc = %d, rows = %d, max = %d", lastMsg->RtnCod, lastMsg->CumRowCnt, (uint32)maxRows()));
}
/**
Poll the message pipe for async read messages
Only valid in async
@param orientation The direction to use when reading through the table.
*/
void IOAsyncReadBuffer::pollNextRow(char orientation)
{
DBUG_ASSERT(readIsAsync);
// Handle the case in which the buffer is full.
if (rowCount() == maxRows())
{
// If we haven't read to the end, exit here.
if (readCursor < rowCount())
return;
if (pipeState == PendingFullBufferMsg)
drainPipe();
if (pipeState == ConsumedFullBufferMsg)
loadNewRows(orientation);
}
if (!rc)
{
PipeRpy_t* lastMsg = NULL;
while (true)
{
PipeRpy_t msg[32];
int bytes = read(msgPipe, msg, sizeof(msg));
DBUG_PRINT("db2i_ioBuffers::pollNextRow",("Pipe returned %d bytes", bytes));
if (unlikely(bytes < 0))
{
DBUG_PRINT("db2i_ioBuffers::pollNextRow", ("Error"));
rc = errno;
break;
}
else if (bytes == 0)
break;
DBUG_ASSERT(bytes % sizeof(msg[0]) == 0);
lastMsg = &msg[bytes / (sizeof(msg[0]))-1];
if (lastMsg->RtnCod || (lastMsg->CumRowCnt == usedRows()))
{
rc = lastMsg->RtnCod;
break;
}
}
*releaseRowNeeded = true;
if (rc == QMY_ERR_END_OF_BLOCK)
rc = 0;
else if (rc == QMY_ERR_END_OF_FILE)
{
// If we reach EOF or end-of-key, DB2 guarantees that no rows will be locked.
rc = HA_ERR_END_OF_FILE;
*releaseRowNeeded = false;
}
else if (rc == QMY_ERR_KEY_NOT_FOUND)
{
rc = HA_ERR_KEY_NOT_FOUND;
*releaseRowNeeded = false;
}
if (lastMsg)
DBUG_PRINT("db2i_ioBuffers::pollNextRow", ("Good data: rc=%d; rows=%d; usedRows=%d", lastMsg->RtnCod, lastMsg->CumRowCnt, (uint32)usedRows()));
if (lastMsg && likely(!rc))
{
if (lastMsg->CumRowCnt < maxRows())
pipeState = PendingFullBufferMsg;
else
pipeState = ConsumedFullBufferMsg;
DBUG_ASSERT(lastMsg->CumRowCnt <= usedRows());
}
DBUG_ASSERT(rowCount() <= getRowCapacity());
}
DBUG_PRINT("db2i_ioBuffers::pollNextRow", ("filledRows: %d, rc: %d", rowCount(), rc));
if (rc) closePipe();
}
/**
Prepare for the destruction of the row buffer storage.
*/
void IOAsyncReadBuffer::prepForFree()
{
interruptRead();
rewind();
IORowBuffer::prepForFree();
}
/**
Initialize the newly allocated storage.
@param sizeChanged Indicates whether the storage capacity is being changed.
*/
void IOAsyncReadBuffer::initAfterAllocate(bool sizeChanged)
{
rewind();
if (sizeChanged || ((void*)rrnList == NULL))
rrnList.realloc(getRowCapacity() * sizeof(uint32));
}
/**
Send an initial read request
@param infile The file (table/index) being read from
@param orientation The orientation to use for this read request
@param rowsToBuffer The number of rows to request each time
@param useAsync Whether reads should be performed asynchronously.
@param key The key to use (if any)
@param keyLength The length of key (if any)
@param keyParts The number of columns in the key (if any)
*/
void IOAsyncReadBuffer::newReadRequest(FILE_HANDLE infile,
char orientation,
uint32 rowsToBuffer,
bool useAsync,
ILEMemHandle key,
int keyLength,
int keyParts)
{
DBUG_ENTER("db2i_ioBuffers::newReadRequest");
DBUG_ASSERT(rowsToBuffer <= getRowCapacity());
#ifndef DBUG_OFF
if (readCursor < rowCount())
DBUG_PRINT("PERF:",("Wasting %d buffered rows!\n", rowCount() - readCursor));
#endif
int fildes[2];
int ileDescriptor = QMY_REUSE;
interruptRead();
if (likely(useAsync))
{
if (rowsToBuffer == 1)
{
// Async provides little or no benefit for single row reads, so we turn it off
DBUG_PRINT("db2i_ioBuffers::newReadRequest", ("Disabling async"));
useAsync = false;
}
else
{
rc = pipe(fildes);
if (rc) DBUG_VOID_RETURN;
// Translate the pipe write descriptor into the equivalent ILE descriptor
rc = fstatx(fildes[1], (struct stat*)&ileDescriptor, sizeof(ileDescriptor), STX_XPFFD_PASE);
if (rc)
{
close(fildes[0]);
close(fildes[1]);
DBUG_VOID_RETURN;
}
pipeState = Untouched;
msgPipe = fildes[0];
DBUG_PRINT("db2i_ioBuffers::newReadRequest", ("Opened pipe %d", fildes[0]));
}
}
file = infile;
readIsAsync = useAsync;
rowsToBlock = rowsToBuffer;
rewind();
maxRows() = 1;
rc = getBridge()->expectErrors(QMY_ERR_END_OF_BLOCK, QMY_ERR_LOB_SPACE_TOO_SMALL)
->read(file,
ptr(),
accessIntent,
commitLevel,
orientation,
useAsync,
rrnList,
key,
keyLength,
keyParts,
ileDescriptor);
// Having shared the pipe with ILE, we relinquish our claim on the write end
// of the pipe.
if (useAsync)
close(fildes[1]);
// If we reach EOF or end-of-key, DB2 guarantees that no rows will be locked.
if (rc == QMY_ERR_END_OF_FILE)
{
rc = HA_ERR_END_OF_FILE;
*releaseRowNeeded = false;
}
else if (rc == QMY_ERR_KEY_NOT_FOUND)
{
if (rowCount())
rc = HA_ERR_END_OF_FILE;
else
rc = HA_ERR_KEY_NOT_FOUND;
*releaseRowNeeded = false;
}
else
*releaseRowNeeded = true;
DBUG_VOID_RETURN;
}
// was inline
IORowBuffer::IORowBuffer() :
// data,
allocSize(0),
rowCapacity(0),
rowLength(0),
nullOffset(0)
{;}
IORowBuffer::~IORowBuffer() { freeBuf(); }
void IORowBuffer::allocBuf(uint32 rowLen, uint16 nullMapOffset, uint32 size)
{
nullOffset = nullMapOffset;
uint32 newSize = size + sizeof(BufferHdr_t);
// If the internal structure of the row is changing, we need to
// remember this and notify the subclasses via initAfterAllocate();
bool formatChanged = ((size/rowLen) != rowCapacity);
if (newSize > allocSize)
{
this->freeBuf();
data.alloc(newSize);
if (likely((void*)data))
allocSize = newSize;
}
if (likely((void*)data))
{
DBUG_ASSERT((uint64)(void*)data % 16 == 0);
rowLength = rowLen;
rowCapacity = size / rowLength;
initAfterAllocate(formatChanged);
}
else
{
allocSize = 0;
rowCapacity = 0;
}
DBUG_PRINT("db2i_ioBuffers::allocBuf",("rowCapacity = %d", rowCapacity));
}
void IORowBuffer::zeroBuf()
{
memset(data, 0, allocSize);
}
void IORowBuffer::freeBuf()
{
if (likely(allocSize))
{
prepForFree();
DBUG_PRINT("IORowBuffer::freeBuf",("Freeing 0x%p", (char*)data));
data.dealloc();
}
}
char* IORowBuffer::getRowN(uint32 n)
{
if (unlikely(n >= getRowCapacity()))
return NULL;
return (char*)data + sizeof(BufferHdr_t) + (rowLength * n);
}
IOWriteBuffer::IOWriteBuffer() {;}
bool IOWriteBuffer::endOfBuffer() const {return (maxRows() == getRowCapacity());}
char* IOWriteBuffer::addRow()
{
return getRowN(maxRows()++);
}
void IOWriteBuffer::resetAfterWrite()
{
maxRows() = 0;
}
void IOWriteBuffer::deleteRow()
{
--maxRows();
}
uint32 IOWriteBuffer::rowCount() const {return maxRows();}
uint32 IOWriteBuffer::rowsWritten() const {return usedRows()-1;}
void IOWriteBuffer::initAfterAllocate(bool sizeChanged) {maxRows() = 0; usedRows() = 0;}
IOReadBuffer::IOReadBuffer() {;}
IOReadBuffer::IOReadBuffer(uint32 rows, uint32 rowLength)
{
allocBuf(rows, 0, rows * rowLength);
maxRows() = rows;
}
uint32 IOReadBuffer::rowCount() {return usedRows();}
void IOReadBuffer::setRowsToProcess(uint32 rows) { maxRows() = rows; }
IOAsyncReadBuffer::IOAsyncReadBuffer() :
readCursor(0),
rc(0),
// rrnList;,
accessIntent(0),
commitLevel(0),
EOD(0),
readIsAsync(0),
releaseRowNeeded(0),
file(0),
msgPipe(QMY_REUSE),
bridge(NULL),
rowsToBlock(0)
// pipeState
{
}
IOAsyncReadBuffer::~IOAsyncReadBuffer()
{
interruptRead();
rrnList.dealloc();
}
void IOAsyncReadBuffer::endRead()
{
if (readCursor < rowCount())
DBUG_PRINT("PERF:",("Wasting %d buffered rows!\n", rowCount() - readCursor));
interruptRead();
file = 0;
bridge = NULL;
}
void IOAsyncReadBuffer::update(char newAccessIntent,
bool* newReleaseRowNeeded,
char commitLvl)
{
accessIntent = newAccessIntent;
releaseRowNeeded = newReleaseRowNeeded;
commitLevel = commitLvl;
}
char* IOAsyncReadBuffer::readNextRow(char orientation, uint32& rrn)
{
DBUG_PRINT("db2i_ioBuffers::readNextRow", ("readCursor: %d, filledRows: %d, rc: %d", readCursor, rowCount(), rc));
while (readCursor >= rowCount() && !rc)
{
if (!readIsAsync)
loadNewRows(orientation);
else
pollNextRow(orientation);
}
if (readCursor >= rowCount())
return NULL;
rrn = rrnList[readCursor];
return getRowN(readCursor++);
}
int32 IOAsyncReadBuffer::lastrc()
{
return db2i_ileBridge::translateErrorCode(rc);
}
void IOAsyncReadBuffer::rewind()
{
readCursor = 0;
rc = 0;
usedRows() = 0;
}
bool IOAsyncReadBuffer::reachedEOD() { return EOD; }
void IOAsyncReadBuffer::interruptRead()
{
closePipe();
if (file && readIsAsync && (rc == 0) && (rowCount() < getRowCapacity()))
{
DBUG_PRINT("IOReadBuffer::interruptRead", ("PERF: Interrupting %d", (uint32)file));
getBridge()->readInterrupt(file);
}
}
void IOAsyncReadBuffer::closePipe()
{
if (msgPipe != QMY_REUSE)
{
DBUG_PRINT("db2i_ioBuffers::closePipe", ("Closing pipe %d", msgPipe));
close(msgPipe);
msgPipe = QMY_REUSE;
}
}
db2i_ileBridge* IOAsyncReadBuffer::getBridge()
{
if (unlikely(bridge == NULL))
{
bridge = db2i_ileBridge::getBridgeForThread();
}
return bridge;
}
// end was inline