Skip to content
This repository was archived by the owner on May 12, 2021. It is now read-only.

Commit 682da44

Browse files
Implementing repeatExecutions and adding more tests
1 parent 56b16f1 commit 682da44

2 files changed

Lines changed: 66 additions & 50 deletions

File tree

vxquery-rest/src/main/java/org/apache/vxquery/rest/service/VXQueryService.java

Lines changed: 55 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,8 @@
5252
import org.apache.vxquery.exceptions.VXQueryRuntimeException;
5353
import org.apache.vxquery.rest.request.QueryRequest;
5454
import org.apache.vxquery.rest.request.QueryResultRequest;
55-
import org.apache.vxquery.rest.response.APIResponse;
55+
import org.apache.vxquery.rest.response.*;
5656
import org.apache.vxquery.rest.response.Error;
57-
import org.apache.vxquery.rest.response.QueryResponse;
58-
import org.apache.vxquery.rest.response.QueryResultResponse;
59-
import org.apache.vxquery.rest.response.SyncQueryResponse;
6057
import org.apache.vxquery.result.ResultUtils;
6158
import org.apache.vxquery.xmlquery.ast.ModuleNode;
6259
import org.apache.vxquery.xmlquery.query.Module;
@@ -67,12 +64,7 @@
6764
import java.io.OutputStream;
6865
import java.io.PrintWriter;
6966
import java.io.StringReader;
70-
import java.util.ArrayList;
71-
import java.util.Arrays;
72-
import java.util.Date;
73-
import java.util.EnumSet;
74-
import java.util.List;
75-
import java.util.Map;
67+
import java.util.*;
7668
import java.util.concurrent.ConcurrentHashMap;
7769
import java.util.concurrent.atomic.AtomicLong;
7870
import java.util.logging.Level;
@@ -81,9 +73,7 @@
8173
import java.util.regex.Pattern;
8274

8375
import static java.util.logging.Level.SEVERE;
84-
import static org.apache.vxquery.rest.Constants.ErrorCodes.NOT_FOUND;
85-
import static org.apache.vxquery.rest.Constants.ErrorCodes.PROBLEM_WITH_QUERY;
86-
import static org.apache.vxquery.rest.Constants.ErrorCodes.UNFORSEEN_PROBLEM;
76+
import static org.apache.vxquery.rest.Constants.ErrorCodes.*;
8777

8878
/**
8979
* Main class responsible for handling query requests. This class will first compile, then submit query to hyracks and
@@ -176,33 +166,33 @@ private APIResponse execute(final QueryRequest request, List<String> collections
176166
} catch (HyracksException e) {
177167
LOGGER.log(Level.SEVERE, String.format("Error occurred when obtaining NC info: '%s'", e.getMessage()));
178168
return APIResponse.newErrorResponse(request.getRequestId(), Error.builder().withCode(UNFORSEEN_PROBLEM)
179-
.withMessage("Hyracks connection problem: " + e.getMessage())
180-
.build());
169+
.withMessage("Hyracks connection problem: " + e.getMessage())
170+
.build());
181171
}
182172

183173
// Adding a query compilation listener
184174
VXQueryCompilationListener listener = new VXQueryCompilationListener(response, request.isShowAbstractSyntaxTree(),
185-
request.isShowTranslatedExpressionTree(),
186-
request.isShowOptimizedExpressionTree(),
187-
request.isShowRuntimePlan());
175+
request.isShowTranslatedExpressionTree(),
176+
request.isShowOptimizedExpressionTree(),
177+
request.isShowRuntimePlan());
188178

189179
Date start = new Date();
190180
// Compiling the XQuery given
191181
final XMLQueryCompiler compiler = new XMLQueryCompiler(listener, nodeControllerInfos,
192-
request.getFrameSize(),
193-
vxQueryConfig.getAvailableProcessors(),
194-
vxQueryConfig.getJoinHashSize(),
195-
vxQueryConfig.getMaximumDataSize(),
196-
vxQueryConfig.getHdfsConf());
182+
request.getFrameSize(),
183+
vxQueryConfig.getAvailableProcessors(),
184+
vxQueryConfig.getJoinHashSize(),
185+
vxQueryConfig.getMaximumDataSize(),
186+
vxQueryConfig.getHdfsConf());
197187
CompilerControlBlock compilerControlBlock = new CompilerControlBlock(new StaticContextImpl(RootStaticContextImpl.INSTANCE),
198-
resultSetId, request.getSourceFileMap());
188+
resultSetId, request.getSourceFileMap());
199189
try {
200190
compiler.compile(null, new StringReader(query), compilerControlBlock, request.getOptimization(), collections);
201191
} catch (AlgebricksException e) {
202192
LOGGER.log(Level.SEVERE, String.format("Error occurred when compiling query: '%s' with message: '%s'", query, e.getMessage()));
203193
return APIResponse.newErrorResponse(request.getRequestId(), Error.builder().withCode(PROBLEM_WITH_QUERY)
204-
.withMessage("Query compilation failure: " + e.getMessage())
205-
.build());
194+
.withMessage("Query compilation failure: " + e.getMessage())
195+
.build());
206196
} catch (SystemException e) {
207197
LOGGER.log(Level.SEVERE, String.format("Error occurred when compiling query: '%s' with message: '%s'", query, e.getMessage()));
208198
return APIResponse.newErrorResponse(request.getRequestId(),
@@ -224,32 +214,41 @@ private APIResponse execute(final QueryRequest request, List<String> collections
224214

225215
HyracksJobContext hyracksJobContext;
226216
start = new Date();
227-
try {
228-
JobId jobId = hyracksClientConnection.startJob(js, EnumSet.of(JobFlag.PROFILE_RUNTIME));
229-
hyracksJobContext = new HyracksJobContext(jobId, js.getFrameSize(), resultSetId);
230-
} catch (Exception e) {
231-
LOGGER.log(SEVERE, "Error occurred when submitting job to hyracks for query: " + query, e);
232-
return APIResponse.newErrorResponse(request.getRequestId(), Error.builder().withCode(UNFORSEEN_PROBLEM)
233-
.withMessage("Error occurred when starting hyracks job")
234-
.build());
235-
}
217+
if (!request.isAsync()) {
218+
for (int i = 0; i < request.getRepeatExecutions(); i++) {
219+
try {
220+
hyracksJobContext = executeJob(js, resultSetId, request);
236221

237-
if (request.isAsync()) {
238-
jobContexts.put(resultSetId.getId(), hyracksJobContext);
222+
} catch (Exception e) {
223+
LOGGER.log(SEVERE, "Error occurred when submitting job to hyracks for query: " + query, e);
224+
return APIResponse.newErrorResponse(request.getRequestId(), Error.builder().withCode(UNFORSEEN_PROBLEM)
225+
.withMessage("Error occurred when starting hyracks job")
226+
.build());
227+
}
228+
try {
229+
String results = readResults(hyracksJobContext);
230+
((SyncQueryResponse) response).setResults(results);
231+
} catch (HyracksException e) {
232+
LOGGER.log(Level.SEVERE, "Error occurred when reading results", e);
233+
SystemException se = getSystemException(e);
234+
return APIResponse.newErrorResponse(request.getRequestId(),
235+
new Error(UNFORSEEN_PROBLEM, String.format("Error occurred when reading results: %s", se != null ? se.getCode() : "")));
236+
} catch (Exception e) {
237+
LOGGER.log(Level.SEVERE, "Error occurred when reading results", e);
238+
return APIResponse.newErrorResponse(request.getRequestId(),
239+
new Error(UNFORSEEN_PROBLEM, "Error occurred when reading results: " + e.getMessage()));
240+
}
241+
}
239242
} else {
240243
try {
241-
String results = readResults(hyracksJobContext);
242-
((SyncQueryResponse) response).setResults(results);
243-
} catch (HyracksException e) {
244-
LOGGER.log(Level.SEVERE, "Error occurred when reading results", e);
245-
SystemException se = getSystemException(e);
246-
return APIResponse.newErrorResponse(request.getRequestId(),
247-
new Error(UNFORSEEN_PROBLEM, String.format("Error occurred when reading results: %s", se != null ? se.getCode() : "")));
244+
hyracksJobContext = executeJob(js, resultSetId, request);
248245
} catch (Exception e) {
249-
LOGGER.log(Level.SEVERE, "Error occurred when reading results", e);
250-
return APIResponse.newErrorResponse(request.getRequestId(),
251-
new Error(UNFORSEEN_PROBLEM, "Error occurred when reading results: " + e.getMessage()));
246+
LOGGER.log(SEVERE, "Error occurred when submitting job to hyracks for query: " + query, e);
247+
return APIResponse.newErrorResponse(request.getRequestId(), Error.builder().withCode(UNFORSEEN_PROBLEM)
248+
.withMessage("Error occurred when starting hyracks job")
249+
.build());
252250
}
251+
jobContexts.put(resultSetId.getId(), hyracksJobContext);
253252
}
254253

255254
if (request.isShowMetrics()) {
@@ -259,6 +258,14 @@ private APIResponse execute(final QueryRequest request, List<String> collections
259258
return response;
260259
}
261260

261+
private HyracksJobContext executeJob(JobSpecification js, ResultSetId resultSetId, QueryRequest request) throws Exception {
262+
HyracksJobContext hyracksJobContext;
263+
JobId jobId = hyracksClientConnection.startJob(js, EnumSet.of(JobFlag.PROFILE_RUNTIME));
264+
hyracksJobContext = new HyracksJobContext(jobId, js.getFrameSize(), resultSetId);
265+
266+
return hyracksJobContext;
267+
}
268+
262269
private static SystemException getSystemException(HyracksException e) {
263270
Throwable t = e;
264271
Throwable candidate = t instanceof SystemException ? t : null;
@@ -281,8 +288,6 @@ private static SystemException getSystemException(HyracksException e) {
281288
return null;
282289
}
283290

284-
// TODO: 7/12/17 Allow multiple executions of the same query
285-
286291
/**
287292
* Returns the query results for a given result set id.
288293
*
@@ -444,7 +449,7 @@ public void notifyOptimizedResult(Module module) {
444449
private StringBuilder appendPrettyPlan(StringBuilder sb, Module module) {
445450
try {
446451
ILogicalExpressionVisitor<String, Integer> ev = new VXQueryLogicalExpressionPrettyPrintVisitor(
447-
module.getModuleContext());
452+
module.getModuleContext());
448453
AlgebricksAppendable buffer = new AlgebricksAppendable();
449454
LogicalOperatorPrettyPrintVisitor v = new LogicalOperatorPrettyPrintVisitor(buffer, ev);
450455
PlanPrettyPrinter.printPlan(module.getBody(), v, 0);

vxquery-rest/src/test/java/org/apache/vxquery/rest/SuccessSyncResponseTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,17 @@ public void testSingleParameterCompileOnly() throws Exception {
120120
runTest(CONTENT_TYPE_XML, request);
121121
}
122122

123+
@Test
124+
public void testSingleParameterRepeatExecutions() throws Exception {
125+
QueryRequest request = new QueryRequest("for $x in (1, 2.0, 3) return $x");
126+
request.setRepeatExecutions(5);
127+
request.setAsync(false);
128+
129+
runTest(null, request);
130+
runTest(CONTENT_TYPE_JSON, request);
131+
runTest(CONTENT_TYPE_XML, request);
132+
}
133+
123134
private void runTest(String contentType, QueryRequest request) throws Exception {
124135
runTest(contentType, request, HttpMethod.GET);
125136
runTest(contentType, request, HttpMethod.POST);

0 commit comments

Comments
 (0)