-
Notifications
You must be signed in to change notification settings - Fork 229
/
QueryProcessor.java
337 lines (297 loc) · 11.2 KB
/
QueryProcessor.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
/*
* JBoss, Home of Professional Open Source.
* See the COPYRIGHT.txt file distributed with this work for information
* regarding copyright ownership. Some portions may be licensed
* to Red Hat, Inc. under one or more contributor license agreements.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA.
*/
package org.teiid.query.processor;
import java.util.Arrays;
import java.util.List;
import org.teiid.common.buffer.BlockedException;
import org.teiid.common.buffer.BufferManager;
import org.teiid.common.buffer.BufferManager.BufferReserveMode;
import org.teiid.common.buffer.TupleBatch;
import org.teiid.common.buffer.TupleBuffer;
import org.teiid.core.TeiidComponentException;
import org.teiid.core.TeiidException;
import org.teiid.core.TeiidProcessingException;
import org.teiid.core.util.Assertion;
import org.teiid.dqp.internal.process.PreparedPlan;
import org.teiid.dqp.internal.process.RequestWorkItem;
import org.teiid.dqp.internal.process.TupleSourceCache;
import org.teiid.logging.LogConstants;
import org.teiid.logging.LogManager;
import org.teiid.logging.MessageLevel;
import org.teiid.query.QueryPlugin;
import org.teiid.query.metadata.QueryMetadataInterface;
import org.teiid.query.optimizer.capabilities.CapabilitiesFinder;
import org.teiid.query.processor.BatchCollector.BatchProducer;
import org.teiid.query.util.CommandContext;
/**
* Driver for plan processing.
*/
public class QueryProcessor implements BatchProducer {
public static class ExpiredTimeSliceException extends BlockedException {
private static final long serialVersionUID = 4585044674826578060L;
}
private static ExpiredTimeSliceException EXPIRED_TIME_SLICE = new ExpiredTimeSliceException();
public interface ProcessorFactory {
QueryProcessor createQueryProcessor(String query, String recursionGroup, CommandContext commandContext, Object... params) throws TeiidProcessingException, TeiidComponentException;
PreparedPlan getPreparedPlan(String query, String recursionGroup,
CommandContext commandContext, QueryMetadataInterface metadata)
throws TeiidProcessingException, TeiidComponentException;
CapabilitiesFinder getCapabiltiesFinder();
}
private CommandContext context;
private ProcessorDataManager dataMgr;
private BufferManager bufferMgr;
private ProcessorPlan processPlan;
private boolean initialized;
private boolean open;
private int reserved;
/** Flag that marks whether the request has been canceled. */
private volatile boolean requestCanceled;
private static final int DEFAULT_WAIT = 50;
private boolean processorClosed;
private boolean continuous;
private String query; //used only in continuous mode
private PreparedPlan plan;
private int rowOffset = 1;
/**
* Construct a processor with all necessary information to process.
* @param plan The plan to process
* @param context The context that this plan is being processed in
* @param bufferMgr The buffer manager that provides access to tuple sources
* @param dataMgr The data manager that provides access to get data
* @throws TeiidComponentException
*/
public QueryProcessor(ProcessorPlan plan, CommandContext context, BufferManager bufferMgr, final ProcessorDataManager dataMgr) {
this.context = context;
this.context.setTupleSourceCache(new TupleSourceCache());
this.dataMgr = dataMgr;
this.processPlan = plan;
this.bufferMgr = bufferMgr;
}
public CommandContext getContext() {
return context;
}
public ProcessorPlan getProcessorPlan() {
return this.processPlan;
}
public TupleBatch nextBatch()
throws BlockedException, TeiidProcessingException, TeiidComponentException {
while (true) {
long wait = DEFAULT_WAIT;
try {
return nextBatchDirect();
} catch (BlockedException e) {
if (!this.context.isNonBlocking()) {
throw e;
}
if (e == BlockedException.BLOCKED_ON_MEMORY_EXCEPTION) {
continue; //TODO: pass the commandcontext into sortutility
}
}
try {
Thread.sleep(wait);
} catch (InterruptedException err) {
throw new TeiidComponentException(QueryPlugin.Event.TEIID30159, err);
}
}
}
private TupleBatch nextBatchDirect()
throws BlockedException, TeiidProcessingException, TeiidComponentException {
boolean done = false;
TupleBatch result = null;
try {
init();
long currentTime = System.currentTimeMillis();
Assertion.assertTrue(!processorClosed);
//TODO: see if there is pending work before preempting
while(currentTime < context.getTimeSliceEnd() || context.isNonBlocking()) {
if (requestCanceled) {
throw new TeiidProcessingException(QueryPlugin.Event.TEIID30160, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30160, this.context.getRequestId()));
}
if (currentTime > context.getTimeoutEnd()) {
throw new TeiidProcessingException(QueryPlugin.Event.TEIID30161, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30161));
}
result = processPlan.nextBatch();
if (continuous) {
result.setRowOffset(rowOffset);
if (result.getTerminationFlag()) {
result.setTermination(TupleBatch.ITERATION_TERMINATED);
List<Object> terminationTuple = Arrays.asList(new Object[this.getOutputElements().size()]);
result.getTuples().add(terminationTuple);
this.context.getTupleSourceCache().close();
this.processPlan.close();
this.processPlan.reset();
this.context.incrementReuseCount();
this.open = false;
}
rowOffset = result.getEndRow() + 1;
}
if(result.getTermination() != TupleBatch.NOT_TERMINATED) {
if (result.getTerminationFlag()) {
done = true;
}
break;
}
if (result.getRowCount() > 0) {
break;
}
}
} catch (BlockedException e) {
throw e;
} catch (TeiidException e) {
closeProcessing();
if (e instanceof TeiidProcessingException) {
throw (TeiidProcessingException)e;
}
if (e instanceof TeiidComponentException) {
throw (TeiidComponentException)e;
}
throw new TeiidComponentException(QueryPlugin.Event.TEIID30162, e);
}
if(done) {
closeProcessing();
}
if (result == null) {
RequestWorkItem workItem = this.getContext().getWorkItem();
if (workItem != null) {
//if we have a workitem (non-test scenario) then before
//throwing exprired time slice we need to indicate there's more work
workItem.moreWork();
}
throw EXPIRED_TIME_SLICE;
}
return result;
}
public void init() throws TeiidComponentException, TeiidProcessingException {
if (!open) {
if (continuous && context.getReuseCount() > 0) {
//validate the plan prior to the next run
if (this.plan != null && !this.plan.validate()) {
this.plan = null;
}
if (this.plan == null) {
this.plan = context.getQueryProcessorFactory().getPreparedPlan(query, null, context, context.getMetadata());
this.processPlan = this.plan.getPlan().clone();
this.processPlan.initialize(context, dataMgr, bufferMgr);
}
}
// initialize if necessary
if(!initialized) {
reserved = this.bufferMgr.reserveBuffers(this.bufferMgr.getSchemaSize(this.getOutputElements()), BufferReserveMode.FORCE);
this.processPlan.initialize(context, dataMgr, bufferMgr);
initialized = true;
}
// Open the top node for reading
processPlan.open();
open = true;
}
}
/**
* Close processing and clean everything up. Should only be called by the same thread that called process.
*/
public void closeProcessing() {
if (processorClosed) {
return;
}
if (LogManager.isMessageToBeRecorded(LogConstants.CTX_DQP, MessageLevel.DETAIL)) {
LogManager.logDetail(LogConstants.CTX_DQP, "QueryProcessor: closing processor"); //$NON-NLS-1$
}
this.context.getTupleSourceCache().close();
this.bufferMgr.releaseBuffers(reserved);
reserved = 0;
processorClosed = true;
if (initialized) {
try {
processPlan.close();
} catch (TeiidComponentException e1){
LogManager.logDetail(LogConstants.CTX_DQP, e1, "Error closing processor"); //$NON-NLS-1$
}
}
}
@Override
public List getOutputElements() {
return this.processPlan.getOutputElements();
}
public List<Exception> getAndClearWarnings() {
return this.context.getAndClearWarnings();
}
/**
* Asynch shutdown of the QueryProcessor, which may trigger exceptions in the processing thread
*/
public void requestCanceled() {
this.requestCanceled = true;
}
public BatchCollector createBatchCollector() throws TeiidComponentException {
return new BatchCollector(this, this.bufferMgr, this.context, false);
}
public void setNonBlocking(boolean nonBlocking) {
this.context.setNonBlocking(nonBlocking);
}
@Override
public TupleBuffer getBuffer(int maxRows) throws BlockedException, TeiidComponentException, TeiidProcessingException {
while (true) {
long wait = DEFAULT_WAIT;
try {
init();
return this.processPlan.getBuffer(maxRows);
} catch (BlockedException e) {
if (!this.context.isNonBlocking()) {
throw e;
}
if (e == BlockedException.BLOCKED_ON_MEMORY_EXCEPTION) {
continue; //TODO: pass the commandcontext into sortutility
}
} catch (TeiidComponentException e) {
closeProcessing();
throw e;
} catch (TeiidProcessingException e) {
closeProcessing();
throw e;
}
try {
Thread.sleep(wait);
} catch (InterruptedException err) {
throw new TeiidComponentException(QueryPlugin.Event.TEIID30163, err);
}
}
}
@Override
public boolean hasBuffer(boolean requireFinal) {
return !continuous && this.processPlan.hasBuffer(false);
}
public BufferManager getBufferManager() {
return bufferMgr;
}
public void setContinuous(PreparedPlan prepPlan, String query) {
this.continuous = true;
this.plan = prepPlan;
this.query = query;
this.context.setContinuous();
}
@Override
public void close() throws TeiidComponentException {
closeProcessing();
}
public ProcessorDataManager getProcessorDataManager() {
return dataMgr;
}
}