Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ private ExecutionContext<?> baseInboxContext(UUID nodeId, UUID qryId, long fragm
NoOpIoTracker.INSTANCE,
0,
ImmutableMap.of(),
null,
null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,15 @@ public class ExecutionContext<Row> extends AbstractQueryContext implements DataC
/** */
private Object[] correlations = new Object[16];

/** Modified entries holder. */
@Nullable private final TxAwareModifiedEntriesHolder modifiedEntriesHolder;

/**
* @param qctx Parent base query context.
* @param qryId Query ID.
* @param fragmentDesc Partitions information.
* @param params Parameters.
* @param mofiedEntriesHolder Modified entries holder.
*/
@SuppressWarnings("AssignmentOrReturnOfFieldWithMutableType")
public ExecutionContext(
Expand All @@ -166,7 +170,8 @@ public ExecutionContext(
IoTracker ioTracker,
long timeout,
Map<String, Object> params,
@Nullable Collection<QueryTxEntry> qryTxEntries
@Nullable Collection<QueryTxEntry> qryTxEntries,
@Nullable TxAwareModifiedEntriesHolder mofiedEntriesHolder
) {
super(qctx);

Expand All @@ -183,6 +188,7 @@ public ExecutionContext(
this.params = params;
this.timeout = timeout;
this.qryTxEntries = qryTxEntries;
this.modifiedEntriesHolder = mofiedEntriesHolder;

startTs = U.currentTimeMillis();

Expand Down Expand Up @@ -421,12 +427,19 @@ public void execute(RunnableX task, Consumer<Throwable> onError) {

executor.execute(qryId, fragmentId(), () -> {
try {
if (modifiedEntriesHolder != null)
modifiedEntriesHolder.store(qryTxEntries);

if (!isCancelled())
task.run();
}
catch (Throwable e) {
onError.accept(e);
}
finally {
if (modifiedEntriesHolder != null)
modifiedEntriesHolder.detach();
}
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.apache.ignite.internal.processors.query.calcite.exec;

import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
Expand All @@ -37,6 +38,7 @@
import org.apache.ignite.cache.query.QueryCancelledException;
import org.apache.ignite.calcite.CalciteQueryEngineConfiguration;
import org.apache.ignite.cluster.ClusterNode;
import org.apache.ignite.configuration.TransactionConfiguration;
import org.apache.ignite.events.CacheQueryReadEvent;
import org.apache.ignite.events.EventType;
import org.apache.ignite.internal.GridKernalContext;
Expand Down Expand Up @@ -78,6 +80,7 @@
import org.apache.ignite.internal.processors.query.calcite.message.MessageType;
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.QueryTxEntry;
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;
Expand Down Expand Up @@ -205,6 +208,13 @@ public class ExecutionServiceImpl<Row> extends AbstractService implements Execut
/** */
private final Map<String, FragmentPlan> fragmentPlanCache = new GridBoundedConcurrentLinkedHashMap<>(1024);

/**
* Transaction modified entries holder.
*
* @see TransactionConfiguration#isTxAwareQueriesEnabled()
*/
private TxAwareModifiedEntriesHolder modifiedEntriesHolder;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use just static ThreadLocal in ExecutionContext? Looks like we don't need this variable outside of execution context.
When execution context created we can check if qryTxEntries == null and get qryTxEntries from thread local.
It's much simplier (just about 5-10 lines of code)


/**
* @param ctx Kernal.
*/
Expand Down Expand Up @@ -477,13 +487,15 @@ public void injectService(InjectResourcesService injectSvc) {
eventManager().addDiscoveryEventListener(discoLsnr, EventType.EVT_NODE_FAILED, EventType.EVT_NODE_LEFT);

iteratorsHolder().init();
modifiedEntriesHolder = new TxAwareModifiedEntriesHolder(U.isTxAwareQueriesEnabled(ctx));
}

/** {@inheritDoc} */
@Override public void tearDown() {
eventManager().removeDiscoveryEventListener(discoLsnr, EventType.EVT_NODE_FAILED, EventType.EVT_NODE_LEFT);

iteratorsHolder().tearDown();
modifiedEntriesHolder = null;
}

/** */
Expand Down Expand Up @@ -515,7 +527,8 @@ private FragmentPlan prepareFragment(BaseQueryContext ctx, String jsonFragment)
case DML:
ListFieldsQueryCursor<?> cur = mapAndExecutePlan(
qry,
(MultiStepPlan)plan
(MultiStepPlan)plan,
modifiedEntriesHolder
);

cur.iterator().hasNext();
Expand All @@ -525,7 +538,8 @@ private FragmentPlan prepareFragment(BaseQueryContext ctx, String jsonFragment)
case QUERY:
return mapAndExecutePlan(
qry,
(MultiStepPlan)plan
(MultiStepPlan)plan,
modifiedEntriesHolder
);

case EXPLAIN:
Expand Down Expand Up @@ -577,7 +591,8 @@ private FieldsQueryCursor<List<?>> executeDdl(RootQuery<Row> qry, DdlPlan plan)
/** */
private ListFieldsQueryCursor<?> mapAndExecutePlan(
RootQuery<Row> qry,
MultiStepPlan plan
MultiStepPlan plan,
TxAwareModifiedEntriesHolder mofiedEntriesHolder
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to pass this parameter, it's a non static method and always passed as this.modifiedEntriesHolder
Also typo in mofiedEntriesHolder

) {
qry.mapping();

Expand Down Expand Up @@ -626,6 +641,8 @@ private ListFieldsQueryCursor<?> mapAndExecutePlan(
MemoryTracker qryMemoryTracker = qry.createMemoryTracker(memoryTracker, cfg.getQueryMemoryQuota());

final GridNearTxLocal userTx = Commons.queryTransaction(qry.context(), ctx.cache().context());
final @Nullable Collection<QueryTxEntry> writeEntries = userTx == null ?
mofiedEntriesHolder.retrieve() : ExecutionContext.transactionChanges(userTx.writeEntries());

ExecutionContext<Row> ectx = new ExecutionContext<>(
qry.context(),
Expand All @@ -641,7 +658,8 @@ private ListFieldsQueryCursor<?> mapAndExecutePlan(
createIoTracker(locNodeId, qry.localQueryId()),
timeout,
qryParams,
userTx == null ? null : ExecutionContext.transactionChanges(userTx.writeEntries()));
writeEntries,
mofiedEntriesHolder);

Node<Row> node = new LogicalRelImplementor<>(ectx, partitionService(), mailboxRegistry(),
exchangeService(), failureProcessor()).go(fragment.root());
Expand Down Expand Up @@ -901,7 +919,8 @@ private void onMessage(UUID nodeId, final QueryStartRequest msg) {
createIoTracker(nodeId, msg.originatingQueryId()),
msg.timeout(),
Commons.parametersMap(msg.parameters()),
msg.queryTransactionEntries()
msg.queryTransactionEntries(),
null
Copy link

Copilot AI Mar 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new TxAwareModifiedEntriesHolder parameter is passed as null when creating ExecutionContext for remote fragments. That means ExecutionContext.execute() on remote nodes will not populate the thread-local with msg.queryTransactionEntries(), so nested queries started from UDFs on those nodes won't be able to pick up tx-aware modified entries via mofiedEntriesHolder.retrieve(). Consider passing the node's mofiedEntriesHolder here instead of null so the behavior is consistent between local and remote fragment execution.

Suggested change
null
modifiedEntriesHolder

Copilot uses AI. Check for mistakes.
);

executeFragment(qry, fragmentPlan, ectx);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* 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.Collection;
import java.util.Collections;
import org.apache.ignite.internal.processors.query.calcite.message.QueryTxEntry;
import org.jetbrains.annotations.Nullable;

/** Per thread modified entries holder. */
public class TxAwareModifiedEntriesHolder {
/** Transaction modified entries holder. */
@Nullable private final ThreadLocal<Collection<QueryTxEntry>> holder;

/** */
public TxAwareModifiedEntriesHolder(boolean txAware) {
if (txAware)
holder = new ThreadLocal<>();
else
holder = null;
}

/** Store entries if applicable. */
public void store(Collection<QueryTxEntry> items) {
if (holder != null)
holder.set(items);
}

/** Retrieve entries if applicable. */
@Nullable public Collection<QueryTxEntry> retrieve() {
if (holder != null)
return holder.get();
else
return Collections.emptyList();
}

/** Detach stored entries. */
public void detach() {
if (holder != null)
holder.remove();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ public class LogicalRelImplementorTest extends GridCommonAbstractTest {
NoOpIoTracker.INSTANCE,
0,
null,
null,
null
) {
@Override public ColocationGroup group(long srcId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ private RuntimeSortedIndex<Object[]> generate(RelDataType rowType, final List<In
NoOpIoTracker.INSTANCE,
0,
null,
null,
null),
RelCollations.of(ImmutableIntList.copyOf(idxCols)),
(o1, o2) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ protected ExecutionContext<Object[]> executionContext(UUID nodeId, UUID qryId, l
NoOpIoTracker.INSTANCE,
0,
ImmutableMap.of(),
null,
null
);
}
Expand Down
Loading
Loading