use of io.crate.metadata.RoutingProvider in project crate by crate.
the class PreExecutionBenchmark method measure_parse_analyze_and_plan_simple_select.
@Benchmark
public Plan measure_parse_analyze_and_plan_simple_select() throws Exception {
String sql = "select name from users";
CoordinatorTxnCtx systemTransactionContext = CoordinatorTxnCtx.systemTransactionContext();
Analysis analysis = new Analysis(systemTransactionContext, ParamTypeHints.EMPTY);
AnalyzedStatement analyzedStatement = analyzer.analyzedStatement(SqlParser.createStatement(sql), analysis);
var jobId = UUID.randomUUID();
var routingProvider = new RoutingProvider(Randomness.get().nextInt(), planner.getAwarenessAttributes());
var clusterState = clusterService.state();
var txnCtx = CoordinatorTxnCtx.systemTransactionContext();
var plannerContext = new PlannerContext(clusterState, routingProvider, jobId, txnCtx, nodeCtx, 0, null);
return planner.plan(analyzedStatement, plannerContext);
}
use of io.crate.metadata.RoutingProvider in project crate by crate.
the class Session method singleExec.
@VisibleForTesting
CompletableFuture<?> singleExec(Portal portal, ResultReceiver<?> resultReceiver, int maxRows) {
var activeConsumer = portal.activeConsumer();
if (activeConsumer != null && activeConsumer.suspended()) {
activeConsumer.replaceResultReceiver(resultReceiver, maxRows);
activeConsumer.resume();
return resultReceiver.completionFuture();
}
var jobId = UUIDs.dirtyUUID();
var routingProvider = new RoutingProvider(Randomness.get().nextInt(), planner.getAwarenessAttributes());
var clusterState = executor.clusterService().state();
var txnCtx = new CoordinatorTxnCtx(sessionContext);
var nodeCtx = executor.nodeContext();
var params = new RowN(portal.params().toArray());
var plannerContext = new PlannerContext(clusterState, routingProvider, jobId, txnCtx, nodeCtx, maxRows, params);
var analyzedStmt = portal.analyzedStatement();
String rawStatement = portal.preparedStmt().rawStatement();
if (analyzedStmt == null) {
String errorMsg = "Statement must have been analyzed: " + rawStatement;
jobsLogs.logPreExecutionFailure(jobId, rawStatement, errorMsg, sessionContext.sessionUser());
throw new IllegalStateException(errorMsg);
}
Plan plan;
try {
plan = planner.plan(analyzedStmt, plannerContext);
} catch (Throwable t) {
jobsLogs.logPreExecutionFailure(jobId, rawStatement, SQLExceptions.messageOf(t), sessionContext.sessionUser());
throw t;
}
if (!analyzedStmt.isWriteOperation()) {
resultReceiver = new RetryOnFailureResultReceiver(executor.clusterService(), clusterState, indexName -> executor.clusterService().state().metadata().hasIndex(indexName), resultReceiver, jobId, (newJobId, resultRec) -> retryQuery(newJobId, analyzedStmt, routingProvider, new RowConsumerToResultReceiver(resultRec, maxRows, new JobsLogsUpdateListener(newJobId, jobsLogs)), params, txnCtx, nodeCtx));
}
jobsLogs.logExecutionStart(jobId, rawStatement, sessionContext.sessionUser(), StatementClassifier.classify(plan));
RowConsumerToResultReceiver consumer = new RowConsumerToResultReceiver(resultReceiver, maxRows, new JobsLogsUpdateListener(jobId, jobsLogs));
portal.setActiveConsumer(consumer);
plan.execute(executor, plannerContext, consumer, params, SubQueryResults.EMPTY);
return resultReceiver.completionFuture();
}
use of io.crate.metadata.RoutingProvider in project crate by crate.
the class SysShardsTableInfo method getRouting.
/**
* Retrieves the routing for sys.shards
* <p>
* This routing contains ALL shards of ALL indices.
* Any shards that are not yet assigned to a node will have a NEGATIVE shard id (see {@link UnassignedShard}
*/
public static Routing getRouting(ClusterState clusterState, RoutingProvider routingProvider, SessionContext sessionContext) {
String[] concreteIndices = Arrays.stream(clusterState.metadata().getConcreteAllIndices()).filter(index -> !IndexParts.isDangling(index)).toArray(String[]::new);
User user = sessionContext != null ? sessionContext.sessionUser() : null;
if (user != null) {
List<String> accessibleTables = new ArrayList<>(concreteIndices.length);
for (String indexName : concreteIndices) {
String tableName = RelationName.fqnFromIndexName(indexName);
if (user.hasAnyPrivilege(Privilege.Clazz.TABLE, tableName)) {
accessibleTables.add(indexName);
}
}
concreteIndices = accessibleTables.toArray(new String[0]);
}
Map<String, Map<String, IntIndexedContainer>> locations = new TreeMap<>();
GroupShardsIterator<ShardIterator> groupShardsIterator = clusterState.getRoutingTable().allAssignedShardsGrouped(concreteIndices, true);
for (final ShardIterator shardIt : groupShardsIterator) {
final ShardRouting shardRouting = shardIt.nextOrNull();
processShardRouting(clusterState.getNodes().getLocalNodeId(), locations, shardRouting, shardIt.shardId());
}
return new Routing(locations);
}
use of io.crate.metadata.RoutingProvider in project crate by crate.
the class SQLExecutor method planInternal.
private <T> T planInternal(AnalyzedStatement analyzedStatement, UUID jobId, int fetchSize, Row params) {
RoutingProvider routingProvider = new RoutingProvider(random.nextInt(), emptyList());
PlannerContext plannerContext = new PlannerContext(planner.currentClusterState(), routingProvider, jobId, coordinatorTxnCtx, nodeCtx, fetchSize, null);
Plan plan = planner.plan(analyzedStatement, plannerContext);
if (plan instanceof LogicalPlan) {
return (T) ((LogicalPlan) plan).build(plannerContext, Set.of(), new ProjectionBuilder(nodeCtx), TopN.NO_LIMIT, 0, null, null, params, new SubQueryResults(emptyMap()) {
@Override
public Object getSafe(SelectSymbol key) {
return Literal.of(key.valueType(), null);
}
});
}
return (T) plan;
}
use of io.crate.metadata.RoutingProvider in project crate by crate.
the class SQLIntegrationTestCase method plan.
public PlanForNode plan(String stmt) {
String[] nodeNames = internalCluster().getNodeNames();
String nodeName = nodeNames[randomIntBetween(1, nodeNames.length) - 1];
Analyzer analyzer = internalCluster().getInstance(Analyzer.class, nodeName);
Planner planner = internalCluster().getInstance(Planner.class, nodeName);
NodeContext nodeCtx = internalCluster().getInstance(NodeContext.class, nodeName);
SessionContext sessionContext = new SessionContext(User.CRATE_USER, sqlExecutor.getCurrentSchema());
CoordinatorTxnCtx coordinatorTxnCtx = new CoordinatorTxnCtx(sessionContext);
RoutingProvider routingProvider = new RoutingProvider(Randomness.get().nextInt(), planner.getAwarenessAttributes());
PlannerContext plannerContext = new PlannerContext(planner.currentClusterState(), routingProvider, UUID.randomUUID(), coordinatorTxnCtx, nodeCtx, 0, null);
Plan plan = planner.plan(analyzer.analyze(SqlParser.createStatement(stmt), coordinatorTxnCtx.sessionContext(), ParamTypeHints.EMPTY), plannerContext);
return new PlanForNode(plan, nodeName, plannerContext);
}
Aggregations