zstan commented on code in PR #13366:
URL: https://github.com/apache/ignite/pull/13366#discussion_r3727158000


##########
modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/ExecutionServiceImpl.java:
##########
@@ -581,6 +598,290 @@ private FieldsQueryCursor<List<?>> 
executeDdl(RootQuery<Row> qry, DdlPlan plan)
         }
     }
 
+    /**
+     * Executes a {@code SELECT ... FOR UPDATE} plan.
+     *
+     * <ol>
+     *   <li>Validates that the current transaction is PESSIMISTIC.</li>
+     *   <li>Runs the inner SELECT with hidden key, value, and version columns 
and materialises all rows.</li>
+     *   <li>Builds cache entries from the hidden columns.</li>
+     *   <li>Creates a savepoint, acquires pessimistic locks via {@code 
lockTxEntries()},
+     *       and releases the savepoint on success (or rolls back on 
failure).</li>
+     *   <li>Repeats the SELECT and lock attempt after a concurrent version 
change while the deadline permits.</li>
+     *   <li>Returns a cursor with only the user-visible columns (the appended 
_KEY is stripped).</li>
+     * </ol>
+     */
+    private FieldsQueryCursor<List<?>> executeForUpdate(RootQuery<Row> qry, 
SelectForUpdatePlan plan) {
+        GridNearTxLocal userTx = Commons.queryTransaction(qry.context(), 
ctx.cache().context());
+
+        if (userTx == null || !userTx.pessimistic())
+            throw new IgniteSQLException(
+                
IgniteResource.INSTANCE.selectForUpdateRequiresPessimisticTx().str(),
+                IgniteQueryErrorCode.UNSUPPORTED_OPERATION);
+
+        long waitMs = waitMillis(plan);
+
+        // Zero means that retries are limited only by the transaction or 
query timeout.
+        long deadline = waitMs > 0
+            ? U.currentTimeMillis() + waitMs
+            : waitMs < 0 ? U.currentTimeMillis() : 0L;
+
+        RootQuery<Row> selectQry = qry;
+
+        while (true) {
+            FieldsQueryCursor<List<?>> cursor = tryExecuteForUpdate(selectQry, 
plan, userTx, waitMs, deadline);
+
+            if (cursor != null)
+                return cursor;
+
+            if (deadline != 0 && U.currentTimeMillis() >= deadline) {
+                throw new IgniteSQLException(
+                    IgniteResource.INSTANCE.selectForUpdateLockFailed().str(),
+                    IgniteQueryErrorCode.CONCURRENT_UPDATE);
+            }
+
+            // The previous query has already been closed after 
materialisation, so retry with a fresh root query.
+            selectQry = qry.retryQuery();
+            qryReg.register(selectQry);
+        }
+    }
+
+    /**
+     * Converts the SQL lock wait value to the internal millisecond 
representation.
+     *
+     * @param plan SELECT FOR UPDATE plan.
+     * @return {@code 0} for the remaining transaction/query timeout, {@code 
-1} for NOWAIT,
+     *      or a positive timeout in milliseconds.
+     */
+    private static long waitMillis(SelectForUpdatePlan plan) {
+        // Convert SQL waitSeconds to the internal lock-wait representation:
+        // null means use the remaining transaction time or the query timeout 
and is encoded as 0;
+        // 0 requests NOWAIT and is encoded as -1; a positive value is 
converted from seconds to milliseconds.
+        Long waitSeconds = plan.waitSeconds();
+
+        if (waitSeconds == null)
+            return 0L;
+        else if (waitSeconds == 0L)
+            return -1L;
+        else
+            return waitSeconds * 1000L;
+    }
+
+    /**
+     * Executes the inner SELECT and attempts to acquire transaction locks for 
the selected row versions.
+     *
+     * @param qry Root query for this execution attempt.
+     * @param plan SELECT FOR UPDATE plan.
+     * @param userTx Transaction that acquires the locks.
+     * @param waitMs Lock wait time in the internal representation.
+     * @param deadline Absolute lock acquisition deadline.
+     * @return Result cursor if all required locks were acquired, or {@code 
null} if at least one lock was not acquired.
+     */
+    @Nullable private FieldsQueryCursor<List<?>> tryExecuteForUpdate(
+        RootQuery<Row> qry,
+        SelectForUpdatePlan plan,
+        GridNearTxLocal userTx,
+        long waitMs,
+        long deadline
+    ) {
+        // Run the inner SELECT (with _KEY, _VAL, _VER appended) and collect 
all rows.
+        ListFieldsQueryCursor<?> innerCursor = mapAndExecutePlan(qry, 
plan.innerPlan());
+        List<List<?>> rows = innerCursor.getAll();

Review Comment:
   Here we can potentially kill each node with OOM, i mean - node can be simple 
killed with just incorrect sql query, probably we need some kind of threshold 
here if memory tracking is disabled, wdyt ?



-- 
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]

Reply via email to