-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Peng Huo <penghuo@gmail.com>
- Loading branch information
Showing
36 changed files
with
1,349 additions
and
214 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 0 additions & 24 deletions
24
core/src/main/java/org/opensearch/sql/executor/DefaultQueryManager.java
This file was deleted.
Oops, something went wrong.
30 changes: 30 additions & 0 deletions
30
core/src/main/java/org/opensearch/sql/executor/ExecutionContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.sql.executor; | ||
|
||
import java.util.Optional; | ||
import lombok.Getter; | ||
import org.opensearch.sql.storage.split.Split; | ||
|
||
/** | ||
* Execution context hold planning related information. | ||
*/ | ||
public class ExecutionContext { | ||
@Getter | ||
private final Optional<Split> split; | ||
|
||
public ExecutionContext(Split split) { | ||
this.split = Optional.of(split); | ||
} | ||
|
||
private ExecutionContext(Optional<Split> split) { | ||
this.split = split; | ||
} | ||
|
||
public static ExecutionContext emptyExecutionContext() { | ||
return new ExecutionContext(Optional.empty()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 137 additions & 0 deletions
137
core/src/main/java/org/opensearch/sql/executor/execution/StreamingQueryPlan.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.sql.executor.execution; | ||
|
||
import java.time.Duration; | ||
import java.time.Instant; | ||
import java.util.List; | ||
import java.util.concurrent.TimeUnit; | ||
import lombok.RequiredArgsConstructor; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.opensearch.sql.ast.tree.UnresolvedPlan; | ||
import org.opensearch.sql.common.response.ResponseListener; | ||
import org.opensearch.sql.executor.ExecutionEngine; | ||
import org.opensearch.sql.executor.QueryId; | ||
import org.opensearch.sql.executor.QueryService; | ||
import org.opensearch.sql.executor.streaming.DefaultMetadataLog; | ||
import org.opensearch.sql.executor.streaming.MicroBatchStreamingExecution; | ||
import org.opensearch.sql.executor.streaming.StreamingSource; | ||
import org.opensearch.sql.planner.logical.LogicalPlan; | ||
import org.opensearch.sql.planner.logical.LogicalPlanNodeVisitor; | ||
import org.opensearch.sql.planner.logical.LogicalRelation; | ||
|
||
/** | ||
* Streaming Query Plan. | ||
*/ | ||
public class StreamingQueryPlan extends QueryPlan { | ||
|
||
private static final Logger log = LogManager.getLogger(StreamingQueryPlan.class); | ||
|
||
private final ExecutionStrategy executionStrategy; | ||
|
||
private MicroBatchStreamingExecution streamingExecution; | ||
|
||
/** | ||
* constructor. | ||
*/ | ||
public StreamingQueryPlan(QueryId queryId, | ||
UnresolvedPlan plan, | ||
QueryService queryService, | ||
ResponseListener<ExecutionEngine.QueryResponse> listener, | ||
ExecutionStrategy executionStrategy) { | ||
super(queryId, plan, queryService, listener); | ||
|
||
this.executionStrategy = executionStrategy; | ||
} | ||
|
||
@Override | ||
public void execute() { | ||
try { | ||
LogicalPlan logicalPlan = queryService.analyze(plan); | ||
StreamingSource streamingSource = buildStreamingSource(logicalPlan); | ||
streamingExecution = | ||
new MicroBatchStreamingExecution( | ||
streamingSource, | ||
logicalPlan, | ||
queryService, | ||
new DefaultMetadataLog<>(), | ||
new DefaultMetadataLog<>()); | ||
executionStrategy.execute(streamingExecution::execute); | ||
} catch (UnsupportedOperationException | IllegalArgumentException e) { | ||
listener.onFailure(e); | ||
} catch (InterruptedException e) { | ||
log.error(e); | ||
// todo, update async task status. | ||
} | ||
} | ||
|
||
interface ExecutionStrategy { | ||
/** | ||
* execute task. | ||
*/ | ||
void execute(Runnable task) throws InterruptedException; | ||
} | ||
|
||
/** | ||
* execute task with fixed interval. | ||
* if task run time < interval, trigger next task on next interval. | ||
* if task run time >= interval, trigger next task immediately. | ||
*/ | ||
@RequiredArgsConstructor | ||
public static class IntervalTriggerExecution implements ExecutionStrategy { | ||
|
||
private final long intervalInSeconds; | ||
|
||
@Override | ||
public void execute(Runnable runnable) throws InterruptedException { | ||
while (!Thread.currentThread().isInterrupted()) { | ||
try { | ||
Instant start = Instant.now(); | ||
runnable.run(); | ||
Instant end = Instant.now(); | ||
long took = Duration.between(start, end).toSeconds(); | ||
TimeUnit.SECONDS.sleep(intervalInSeconds > took ? intervalInSeconds - took : 0); | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private StreamingSource buildStreamingSource(LogicalPlan logicalPlan) { | ||
return logicalPlan.accept(new StreamingSourceBuilder(), null); | ||
} | ||
|
||
static class StreamingSourceBuilder extends LogicalPlanNodeVisitor<StreamingSource, Void> { | ||
@Override | ||
public StreamingSource visitNode(LogicalPlan plan, Void context) { | ||
List<LogicalPlan> children = plan.getChild(); | ||
if (children.isEmpty()) { | ||
String errorMsg = | ||
String.format( | ||
"Could find relation plan, %s does not have child node.", | ||
plan.getClass().getSimpleName()); | ||
log.error(errorMsg); | ||
throw new IllegalArgumentException(errorMsg); | ||
} | ||
return children.get(0).accept(this, context); | ||
} | ||
|
||
@Override | ||
public StreamingSource visitRelation(LogicalRelation plan, Void context) { | ||
try { | ||
return plan.getTable().asStreamingSource(); | ||
} catch (UnsupportedOperationException e) { | ||
String errorMsg = | ||
String.format( | ||
"table %s could not been used as streaming source.", plan.getRelationName()); | ||
log.error(errorMsg); | ||
throw new UnsupportedOperationException(errorMsg); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 0 additions & 41 deletions
41
core/src/test/java/org/opensearch/sql/executor/DefaultQueryManagerTest.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.