Search in sources :

Example 1 with Fragment

use of org.apache.ignite.internal.sql.engine.prepare.Fragment in project ignite-3 by apache.

the class ExecutionServiceImpl method mapAndExecutePlan.

private SqlCursor<List<?>> mapAndExecutePlan(RootQuery<RowT> qry, MultiStepPlan plan) {
    qry.mapping();
    plan.init(mappingSrvc, new MappingQueryContext(locNodeId, topologyVersion()));
    List<Fragment> fragments = plan.fragments();
    // Local execution
    Fragment fragment = first(fragments);
    if (IgniteUtils.assertionsEnabled()) {
        assert fragment != null;
        FragmentMapping mapping = plan.mapping(fragment);
        assert mapping != null;
        List<String> nodes = mapping.nodeIds();
        assert nodes != null && nodes.size() == 1 && first(nodes).equals(locNodeId);
    }
    FragmentDescription fragmentDesc = new FragmentDescription(fragment.fragmentId(), plan.mapping(fragment), plan.target(fragment), plan.remotes(fragment));
    ExecutionContext<RowT> ectx = new ExecutionContext<>(qry.context(), taskExecutor, qry.id(), locNodeId, locNodeId, topologyVersion(), fragmentDesc, handler, Commons.parametersMap(qry.parameters()));
    Node<RowT> node = new LogicalRelImplementor<>(ectx, affSrvc, mailboxRegistry, exchangeSrvc).go(fragment.root());
    qry.run(ectx, plan, node);
    // start remote execution
    for (int i = 1; i < fragments.size(); i++) {
        fragment = fragments.get(i);
        fragmentDesc = new FragmentDescription(fragment.fragmentId(), plan.mapping(fragment), plan.target(fragment), plan.remotes(fragment));
        Throwable ex = null;
        for (String nodeId : fragmentDesc.nodeIds()) {
            if (ex != null) {
                qry.onResponse(nodeId, fragment.fragmentId(), ex);
            } else {
                try {
                    QueryStartRequest req = FACTORY.queryStartRequest().queryId(qry.id()).fragmentId(fragment.fragmentId()).schema(qry.context().schemaName()).root(fragment.serialized()).topologyVersion(ectx.topologyVersion()).fragmentDescription(fragmentDesc).parameters(qry.parameters()).build();
                    msgSrvc.send(nodeId, req);
                } catch (Throwable e) {
                    qry.onResponse(nodeId, fragment.fragmentId(), ex = e);
                }
            }
        }
    }
    return Commons.createCursor(new TransformingIterator<>(iteratorsHolder.iterator(qry.iterator()), row -> {
        int rowSize = ectx.rowHandler().columnCount(row);
        List<Object> res = new ArrayList<>(rowSize);
        for (int i = 0; i < rowSize; i++) {
            res.add(ectx.rowHandler().get(i, row));
        }
        return res;
    }), plan);
}
Also used : MappingServiceImpl(org.apache.ignite.internal.sql.engine.metadata.MappingServiceImpl) RunningFragment(org.apache.ignite.internal.sql.engine.RunningFragment) Frameworks(org.apache.calcite.tools.Frameworks) PlanningContext(org.apache.ignite.internal.sql.engine.prepare.PlanningContext) ErrorMessage(org.apache.ignite.internal.sql.engine.message.ErrorMessage) MappingQueryContext(org.apache.ignite.internal.sql.engine.prepare.MappingQueryContext) IgniteLogger(org.apache.ignite.lang.IgniteLogger) Collections.singletonList(java.util.Collections.singletonList) Contexts(org.apache.calcite.plan.Contexts) MappingService(org.apache.ignite.internal.sql.engine.metadata.MappingService) QueryRegistry(org.apache.ignite.internal.sql.engine.QueryRegistry) QueryState(org.apache.ignite.internal.sql.engine.QueryState) DdlPlan(org.apache.ignite.internal.sql.engine.prepare.DdlPlan) AffinityService(org.apache.ignite.internal.sql.engine.metadata.AffinityService) SqlSchemaManager(org.apache.ignite.internal.sql.engine.schema.SqlSchemaManager) Outbox(org.apache.ignite.internal.sql.engine.exec.rel.Outbox) FragmentMapping(org.apache.ignite.internal.sql.engine.metadata.FragmentMapping) MultiStepPlan(org.apache.ignite.internal.sql.engine.prepare.MultiStepPlan) Context(org.apache.calcite.plan.Context) Fragment(org.apache.ignite.internal.sql.engine.prepare.Fragment) ResultSetMetadataImpl(org.apache.ignite.internal.sql.engine.prepare.ResultSetMetadataImpl) ClusterNode(org.apache.ignite.network.ClusterNode) Objects(java.util.Objects) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) ResultSetMetadataInternal(org.apache.ignite.internal.sql.engine.prepare.ResultSetMetadataInternal) IgniteInternalCheckedException(org.apache.ignite.lang.IgniteInternalCheckedException) RelJsonReader.fromJson(org.apache.ignite.internal.sql.engine.externalize.RelJsonReader.fromJson) IgniteInternalException(org.apache.ignite.lang.IgniteInternalException) FragmentPlan(org.apache.ignite.internal.sql.engine.prepare.FragmentPlan) QueryStartRequest(org.apache.ignite.internal.sql.engine.message.QueryStartRequest) Node(org.apache.ignite.internal.sql.engine.exec.rel.Node) SqlQueryMessagesFactory(org.apache.ignite.internal.sql.engine.message.SqlQueryMessagesFactory) RootQuery(org.apache.ignite.internal.sql.engine.RootQuery) RunningQuery(org.apache.ignite.internal.sql.engine.RunningQuery) QueryPlanCache(org.apache.ignite.internal.sql.engine.prepare.QueryPlanCache) ArrayList(java.util.ArrayList) Query(org.apache.ignite.internal.sql.engine.Query) Inbox(org.apache.ignite.internal.sql.engine.exec.rel.Inbox) IgniteUtils(org.apache.ignite.internal.util.IgniteUtils) SqlCursor(org.apache.ignite.internal.sql.engine.SqlCursor) RemoteException(org.apache.ignite.internal.sql.engine.metadata.RemoteException) CacheKey(org.apache.ignite.internal.sql.engine.prepare.CacheKey) TableManager(org.apache.ignite.internal.table.distributed.TableManager) DdlCommandHandler(org.apache.ignite.internal.sql.engine.exec.ddl.DdlCommandHandler) RelDataType(org.apache.calcite.rel.type.RelDataType) NodeLeaveHandler(org.apache.ignite.internal.sql.engine.util.NodeLeaveHandler) TopologyService(org.apache.ignite.network.TopologyService) ExplainPlan(org.apache.ignite.internal.sql.engine.prepare.ExplainPlan) BaseQueryContext(org.apache.ignite.internal.sql.engine.util.BaseQueryContext) TypeUtils(org.apache.ignite.internal.sql.engine.util.TypeUtils) Commons(org.apache.ignite.internal.sql.engine.util.Commons) FragmentDescription(org.apache.ignite.internal.sql.engine.metadata.FragmentDescription) QueryPlan(org.apache.ignite.internal.sql.engine.prepare.QueryPlan) TransformingIterator(org.apache.ignite.internal.sql.engine.util.TransformingIterator) FRAMEWORK_CONFIG(org.apache.ignite.internal.sql.engine.util.Commons.FRAMEWORK_CONFIG) QueryStartResponse(org.apache.ignite.internal.sql.engine.message.QueryStartResponse) SqlQueryMessageGroup(org.apache.ignite.internal.sql.engine.message.SqlQueryMessageGroup) CollectionUtils.first(org.apache.ignite.internal.util.CollectionUtils.first) Collections(java.util.Collections) MessageService(org.apache.ignite.internal.sql.engine.message.MessageService) MappingQueryContext(org.apache.ignite.internal.sql.engine.prepare.MappingQueryContext) RunningFragment(org.apache.ignite.internal.sql.engine.RunningFragment) Fragment(org.apache.ignite.internal.sql.engine.prepare.Fragment) FragmentMapping(org.apache.ignite.internal.sql.engine.metadata.FragmentMapping) QueryStartRequest(org.apache.ignite.internal.sql.engine.message.QueryStartRequest) Collections.singletonList(java.util.Collections.singletonList) List(java.util.List) ArrayList(java.util.ArrayList) FragmentDescription(org.apache.ignite.internal.sql.engine.metadata.FragmentDescription)

Example 2 with Fragment

use of org.apache.ignite.internal.sql.engine.prepare.Fragment in project ignite-3 by apache.

the class RootQuery method run.

/**
 * Starts execution phase for the query and setup remote fragments.
 */
public void run(ExecutionContext<RowT> ctx, MultiStepPlan plan, Node<RowT> root) {
    synchronized (mux) {
        if (state == QueryState.CLOSED) {
            throw new IgniteInternalException("The query was cancelled while executing.");
        }
        RootNode<RowT> rootNode = new RootNode<>(ctx, plan.metadata().rowType(), this::tryClose);
        rootNode.register(root);
        addFragment(new RunningFragment<>(rootNode, ctx));
        this.root = rootNode;
        for (int i = 1; i < plan.fragments().size(); i++) {
            Fragment fragment = plan.fragments().get(i);
            List<String> nodes = plan.mapping(fragment).nodeIds();
            remotes.addAll(nodes);
            for (String node : nodes) {
                waiting.add(new RemoteFragmentKey(node, fragment.fragmentId()));
            }
        }
        state = QueryState.EXECUTING;
    }
}
Also used : RootNode(org.apache.ignite.internal.sql.engine.exec.rel.RootNode) IgniteInternalException(org.apache.ignite.lang.IgniteInternalException) Fragment(org.apache.ignite.internal.sql.engine.prepare.Fragment)

Example 3 with Fragment

use of org.apache.ignite.internal.sql.engine.prepare.Fragment in project ignite-3 by apache.

the class AbstractPlannerTest method checkSplitAndSerialization.

protected void checkSplitAndSerialization(IgniteRel rel, IgniteSchema publicSchema) {
    assertNotNull(rel);
    rel = Cloner.clone(rel);
    List<Fragment> fragments = new Splitter().go(rel);
    List<String> serialized = new ArrayList<>(fragments.size());
    for (Fragment fragment : fragments) {
        serialized.add(toJson(fragment.root()));
    }
    assertNotNull(serialized);
    List<String> nodes = new ArrayList<>(4);
    for (int i = 0; i < 4; i++) {
        nodes.add(UUID.randomUUID().toString());
    }
    List<RelNode> deserializedNodes = new ArrayList<>();
    Map<UUID, IgniteTable> tableMap = publicSchema.getTableNames().stream().map(publicSchema::getTable).map(IgniteTable.class::cast).collect(Collectors.toMap(IgniteTable::id, Function.identity()));
    for (String s : serialized) {
        RelJsonReader reader = new RelJsonReader(new SqlSchemaManagerImpl(tableMap));
        deserializedNodes.add(reader.read(s));
    }
    List<RelNode> expectedRels = fragments.stream().map(Fragment::root).collect(Collectors.toList());
    assertEquals(expectedRels.size(), deserializedNodes.size(), "Invalid deserialization fragments count");
    for (int i = 0; i < expectedRels.size(); ++i) {
        RelNode expected = expectedRels.get(i);
        RelNode deserialized = deserializedNodes.get(i);
        clearTraits(expected);
        clearTraits(deserialized);
        if (!expected.deepEquals(deserialized)) {
            assertTrue(expected.deepEquals(deserialized), "Invalid serialization / deserialization.\n" + "Expected:\n" + RelOptUtil.toString(expected) + "Deserialized:\n" + RelOptUtil.toString(deserialized));
        }
    }
}
Also used : Splitter(org.apache.ignite.internal.sql.engine.prepare.Splitter) IgniteTable(org.apache.ignite.internal.sql.engine.schema.IgniteTable) InternalIgniteTable(org.apache.ignite.internal.sql.engine.schema.InternalIgniteTable) ArrayList(java.util.ArrayList) Fragment(org.apache.ignite.internal.sql.engine.prepare.Fragment) RelReferentialConstraint(org.apache.calcite.rel.RelReferentialConstraint) AbstractRelNode(org.apache.calcite.rel.AbstractRelNode) RelNode(org.apache.calcite.rel.RelNode) RelJsonReader(org.apache.ignite.internal.sql.engine.externalize.RelJsonReader) UUID(java.util.UUID)

Aggregations

Fragment (org.apache.ignite.internal.sql.engine.prepare.Fragment)3 ArrayList (java.util.ArrayList)2 IgniteInternalException (org.apache.ignite.lang.IgniteInternalException)2 Collections (java.util.Collections)1 Collections.singletonList (java.util.Collections.singletonList)1 List (java.util.List)1 Objects (java.util.Objects)1 UUID (java.util.UUID)1 Context (org.apache.calcite.plan.Context)1 Contexts (org.apache.calcite.plan.Contexts)1 AbstractRelNode (org.apache.calcite.rel.AbstractRelNode)1 RelNode (org.apache.calcite.rel.RelNode)1 RelReferentialConstraint (org.apache.calcite.rel.RelReferentialConstraint)1 RelDataType (org.apache.calcite.rel.type.RelDataType)1 Frameworks (org.apache.calcite.tools.Frameworks)1 Query (org.apache.ignite.internal.sql.engine.Query)1 QueryRegistry (org.apache.ignite.internal.sql.engine.QueryRegistry)1 QueryState (org.apache.ignite.internal.sql.engine.QueryState)1 RootQuery (org.apache.ignite.internal.sql.engine.RootQuery)1 RunningFragment (org.apache.ignite.internal.sql.engine.RunningFragment)1