korlov42 commented on a change in pull request #213: URL: https://github.com/apache/ignite-3/pull/213#discussion_r670210252
########## File path: modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/ExecutionServiceImpl.java ########## @@ -0,0 +1,864 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ignite.internal.processors.query.calcite.exec; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Set; +import java.util.UUID; +import java.util.concurrent.ConcurrentHashMap; + +import com.google.common.collect.ImmutableList; +import org.apache.calcite.plan.Contexts; +import org.apache.calcite.plan.ConventionTraitDef; +import org.apache.calcite.plan.RelOptUtil; +import org.apache.calcite.plan.RelTraitDef; +import org.apache.calcite.rel.RelCollationTraitDef; +import org.apache.calcite.rel.type.RelDataType; +import org.apache.calcite.sql.SqlDdl; +import org.apache.calcite.sql.SqlExplain; +import org.apache.calcite.sql.SqlExplainLevel; +import org.apache.calcite.sql.SqlNode; +import org.apache.calcite.sql.SqlNodeList; +import org.apache.calcite.sql.parser.SqlParseException; +import org.apache.calcite.sql.type.SqlTypeName; +import org.apache.calcite.tools.Frameworks; +import org.apache.calcite.tools.ValidationException; +import org.apache.ignite.internal.processors.query.calcite.exec.rel.Inbox; +import org.apache.ignite.internal.processors.query.calcite.exec.rel.Node; +import org.apache.ignite.internal.processors.query.calcite.exec.rel.Outbox; +import org.apache.ignite.internal.processors.query.calcite.exec.rel.RootNode; +import org.apache.ignite.internal.processors.query.calcite.message.ErrorMessage; +import org.apache.ignite.internal.processors.query.calcite.message.MessageService; +import org.apache.ignite.internal.processors.query.calcite.message.QueryStartRequest; +import org.apache.ignite.internal.processors.query.calcite.message.QueryStartResponse; +import org.apache.ignite.internal.processors.query.calcite.message.SqlQueryMessageGroup; +import org.apache.ignite.internal.processors.query.calcite.message.SqlQueryMessagesFactory; +import org.apache.ignite.internal.processors.query.calcite.metadata.AffinityService; +import org.apache.ignite.internal.processors.query.calcite.metadata.FragmentDescription; +import org.apache.ignite.internal.processors.query.calcite.metadata.FragmentMapping; +import org.apache.ignite.internal.processors.query.calcite.metadata.MappingService; +import org.apache.ignite.internal.processors.query.calcite.metadata.MappingServiceImpl; +import org.apache.ignite.internal.processors.query.calcite.metadata.RemoteException; +import org.apache.ignite.internal.processors.query.calcite.prepare.CacheKey; +import org.apache.ignite.internal.processors.query.calcite.prepare.DdlPlan; +import org.apache.ignite.internal.processors.query.calcite.prepare.ExplainPlan; +import org.apache.ignite.internal.processors.query.calcite.prepare.FieldsMetadata; +import org.apache.ignite.internal.processors.query.calcite.prepare.FieldsMetadataImpl; +import org.apache.ignite.internal.processors.query.calcite.prepare.Fragment; +import org.apache.ignite.internal.processors.query.calcite.prepare.FragmentPlan; +import org.apache.ignite.internal.processors.query.calcite.prepare.IgnitePlanner; +import org.apache.ignite.internal.processors.query.calcite.prepare.MultiStepDmlPlan; +import org.apache.ignite.internal.processors.query.calcite.prepare.MultiStepPlan; +import org.apache.ignite.internal.processors.query.calcite.prepare.MultiStepQueryPlan; +import org.apache.ignite.internal.processors.query.calcite.prepare.PlanningContext; +import org.apache.ignite.internal.processors.query.calcite.prepare.QueryPlan; +import org.apache.ignite.internal.processors.query.calcite.prepare.QueryPlanCache; +import org.apache.ignite.internal.processors.query.calcite.prepare.QueryTemplate; +import org.apache.ignite.internal.processors.query.calcite.prepare.Splitter; +import org.apache.ignite.internal.processors.query.calcite.prepare.ValidationResult; +import org.apache.ignite.internal.processors.query.calcite.prepare.ddl.DdlSqlToCommandConverter; +import org.apache.ignite.internal.processors.query.calcite.rel.IgniteRel; +import org.apache.ignite.internal.processors.query.calcite.schema.SchemaHolder; +import org.apache.ignite.internal.processors.query.calcite.trait.CorrelationTraitDef; +import org.apache.ignite.internal.processors.query.calcite.trait.DistributionTraitDef; +import org.apache.ignite.internal.processors.query.calcite.trait.RewindabilityTraitDef; +import org.apache.ignite.internal.processors.query.calcite.type.IgniteTypeFactory; +import org.apache.ignite.internal.processors.query.calcite.util.Cancellable; +import org.apache.ignite.internal.processors.query.calcite.util.Commons; +import org.apache.ignite.internal.processors.query.calcite.util.NodeLeaveHandler; +import org.apache.ignite.internal.processors.query.calcite.util.TransformingIterator; +import org.apache.ignite.internal.processors.query.calcite.util.TypeUtils; +import org.apache.ignite.internal.util.Cursor; +import org.apache.ignite.internal.util.IgniteUtils; +import org.apache.ignite.lang.IgniteBiTuple; +import org.apache.ignite.lang.IgniteInternalCheckedException; +import org.apache.ignite.lang.IgniteInternalException; +import org.apache.ignite.lang.IgniteLogger; +import org.apache.ignite.network.ClusterNode; +import org.apache.ignite.network.TopologyService; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import static java.util.Collections.singletonList; +import static org.apache.calcite.rel.type.RelDataType.PRECISION_NOT_SPECIFIED; +import static org.apache.ignite.internal.processors.query.calcite.exec.PlannerHelper.optimize; +import static org.apache.ignite.internal.processors.query.calcite.externalize.RelJsonReader.fromJson; +import static org.apache.ignite.internal.processors.query.calcite.util.Commons.FRAMEWORK_CONFIG; +import static org.apache.ignite.internal.util.CollectionUtils.first; +import static org.apache.ignite.internal.util.CollectionUtils.nullOrEmpty; + +/** + * + */ +public class ExecutionServiceImpl<Row> implements ExecutionService { + private static final IgniteLogger LOG = IgniteLogger.forClass(ExecutionServiceImpl.class); + + private static final SqlQueryMessagesFactory FACTORY = new SqlQueryMessagesFactory(); + + /** */ + private final MessageService msgSrvc; + + /** */ + private final String locNodeId; + + /** */ + private final QueryPlanCache qryPlanCache; + + /** */ + private final SchemaHolder schemaHolder; + + /** */ + private final QueryTaskExecutor taskExecutor; + + /** */ + private final AffinityService affSrvc; + + /** */ + private final MailboxRegistry mailboxRegistry; + + /** */ + private final MappingService mappingSrvc; + + /** */ + private final ExchangeService exchangeSrvc; + + /** */ + private final ClosableIteratorsHolder iteratorsHolder; + + /** */ + private final Map<UUID, QueryInfo> running; + + /** */ + private final RowHandler<Row> handler; + + /** */ + private final DdlSqlToCommandConverter ddlConverter; + + public ExecutionServiceImpl( + TopologyService topSrvc, + MessageService msgSrvc, + QueryPlanCache planCache, + SchemaHolder schemaHolder, + QueryTaskExecutor taskExecutor, + RowHandler<Row> handler + ) { + this.handler = handler; + this.msgSrvc = msgSrvc; + this.schemaHolder = schemaHolder; + this.taskExecutor = taskExecutor; + + locNodeId = topSrvc.localMember().id(); + qryPlanCache = planCache; + running = new ConcurrentHashMap<>(); + ddlConverter = new DdlSqlToCommandConverter(); + iteratorsHolder = new ClosableIteratorsHolder(LOG); + mailboxRegistry = new MailboxRegistryImpl(topSrvc); + exchangeSrvc = new ExchangeServiceImpl(taskExecutor, mailboxRegistry, msgSrvc); + mappingSrvc = new MappingServiceImpl(topSrvc); + // TODO: fix this + affSrvc = cacheId -> Objects::hashCode; + + topSrvc.addEventHandler(new NodeLeaveHandler(this::onNodeLeft)); + + init(); + } + + private void init() { + msgSrvc.register((n, m) -> onMessage(n, (QueryStartRequest) m), SqlQueryMessageGroup.QUERY_START_REQUEST); + msgSrvc.register((n, m) -> onMessage(n, (QueryStartResponse) m), SqlQueryMessageGroup.QUERY_START_RESPONSE); + msgSrvc.register((n, m) -> onMessage(n, (ErrorMessage) m), SqlQueryMessageGroup.ERROR_MESSAGE); + } + + /** {@inheritDoc} */ + @Override public List<Cursor<List<?>>> executeQuery( + String schema, + String qry, + Object[] params + ) { + PlanningContext pctx = createContext(topologyVersion(), locNodeId, schema, qry, params); + + List<QueryPlan> qryPlans = qryPlanCache.queryPlan(pctx, new CacheKey(pctx.schemaName(), pctx.query()), this::prepareQuery); + + return executePlans(qryPlans, pctx); + } + + /** + * Executes prepared plans. + * @param qryPlans Query plans. + * @param pctx Query context. + * @return List of query result cursors. + */ + @NotNull public List<Cursor<List<?>>> executePlans( + Collection<QueryPlan> qryPlans, + PlanningContext pctx + ) { + List<Cursor<List<?>>> cursors = new ArrayList<>(qryPlans.size()); + + for (QueryPlan plan : qryPlans) { + UUID qryId = UUID.randomUUID(); + + Cursor<List<?>> cur = executePlan(qryId, pctx, plan); Review comment: Looks like created cursors will remain open. Hope this place will be reworked under https://issues.apache.org/jira/browse/IGNITE-14749, otherwise I'll file a ticked for this -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
