5252import org .apache .vxquery .exceptions .VXQueryRuntimeException ;
5353import org .apache .vxquery .rest .request .QueryRequest ;
5454import org .apache .vxquery .rest .request .QueryResultRequest ;
55- import org .apache .vxquery .rest .response .APIResponse ;
55+ import org .apache .vxquery .rest .response .* ;
5656import 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 ;
6057import org .apache .vxquery .result .ResultUtils ;
6158import org .apache .vxquery .xmlquery .ast .ModuleNode ;
6259import org .apache .vxquery .xmlquery .query .Module ;
6764import java .io .OutputStream ;
6865import java .io .PrintWriter ;
6966import 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 .*;
7668import java .util .concurrent .ConcurrentHashMap ;
7769import java .util .concurrent .atomic .AtomicLong ;
7870import java .util .logging .Level ;
8173import java .util .regex .Pattern ;
8274
8375import 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 );
0 commit comments