IGNITE-17385 Skip acquire asyncOp permit for sync singleCache ops (#10160)

diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheAdapter.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheAdapter.java
index 7cae4e7..59a0fe4 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheAdapter.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheAdapter.java
@@ -4298,14 +4298,14 @@
                         }
                     });
 
-                saveFuture(holder, f, /*retry*/false);
+                saveFuture(holder, f, /*asyncOp*/false, /*retry*/false);
 
                 return f;
             }
 
             IgniteInternalFuture<IgniteInternalTx> f = tx.commitNearTxLocalAsync();
 
-            saveFuture(holder, f, /*retry*/false);
+            saveFuture(holder, f, /*asyncOp*/false, /*retry*/false);
 
             ctx.tm().resetContext();
 
@@ -4599,7 +4599,7 @@
                         return resFut;
                     });
 
-                saveFuture(holder, f, retry);
+                saveFuture(holder, f, /*asyncOp*/true, retry);
 
                 return f;
             }
@@ -4621,7 +4621,7 @@
                 ctx.shared().txContextReset();
             }
 
-            saveFuture(holder, f, retry);
+            saveFuture(holder, f, /*asyncOp*/true, retry);
 
             if (tx.implicit())
                 ctx.tm().resetContext();
@@ -4639,8 +4639,10 @@
      *
      * @param holder Future holder.
      * @param fut Future to save.
+     * @param asyncOp Whether operation is instance of AsyncOp.
+     * @param retry {@code true} for retry operations.
      */
-    protected void saveFuture(final FutureHolder holder, IgniteInternalFuture<?> fut, final boolean retry) {
+    protected void saveFuture(final FutureHolder holder, IgniteInternalFuture<?> fut, final boolean asyncOp, final boolean retry) {
         assert holder != null;
         assert fut != null;
         assert holder.holdsLock();
@@ -4650,12 +4652,14 @@
         if (fut.isDone()) {
             holder.future(null);
 
-            asyncOpRelease(retry);
+            if (asyncOp)
+                asyncOpRelease(retry);
         }
         else {
             fut.listen(new CI1<IgniteInternalFuture<?>>() {
                 @Override public void apply(IgniteInternalFuture<?> f) {
-                    asyncOpRelease(retry);
+                    if (asyncOp)
+                        asyncOpRelease(retry);
 
                     if (!holder.tryLock())
                         return;
diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/transactions/TxAsyncOpsSemaphorePermitsExceededTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/transactions/TxAsyncOpsSemaphorePermitsExceededTest.java
new file mode 100644
index 0000000..1fa4c81
--- /dev/null
+++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/transactions/TxAsyncOpsSemaphorePermitsExceededTest.java
@@ -0,0 +1,114 @@
+/*
+ * 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.cache.transactions;
+
+import java.util.Random;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicInteger;
+import org.apache.ignite.configuration.CacheConfiguration;
+import org.apache.ignite.configuration.IgniteConfiguration;
+import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
+import org.apache.ignite.transactions.Transaction;
+import org.junit.Test;
+
+import static org.apache.ignite.cache.CacheAtomicityMode.TRANSACTIONAL;
+
+/** */
+public class TxAsyncOpsSemaphorePermitsExceededTest extends GridCommonAbstractTest {
+    /** Failed flag. */
+    private final AtomicBoolean failed = new AtomicBoolean(false);
+
+    /** */
+    private static final int MAX_NUM_OP_BEFORE_EXCEED = 2;
+
+    /** */
+    private final Random rnd = new Random();
+
+    /** {@inheritDoc} */
+    @Override protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception {
+        IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName);
+
+        cfg.setCacheConfiguration(new CacheConfiguration<>()
+            .setName(DEFAULT_CACHE_NAME)
+            .setAtomicityMode(TRANSACTIONAL)
+            .setBackups(2)
+            .setMaxConcurrentAsyncOperations(Integer.MAX_VALUE - MAX_NUM_OP_BEFORE_EXCEED));
+
+        cfg.setFailureHandler((ignite, ctx) -> {
+            failed.set(true);
+
+            return true;
+        });
+
+        return cfg;
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void beforeTest() throws Exception {
+        startGrids(3);
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void afterTest() throws Exception {
+        stopAllGrids();
+    }
+
+    /** */
+    @Test
+    public void testImplicitAsyncPutOps() throws Exception {
+        runOps(() ->
+            grid(0).cache(DEFAULT_CACHE_NAME).put(rnd.nextInt(), rnd.nextInt())
+        );
+    }
+
+    /** */
+    @Test
+    public void testSyncPutOps() throws Exception {
+        runOps(() -> {
+            try (Transaction tx = grid(0).transactions().txStart()) {
+                grid(0).cache(DEFAULT_CACHE_NAME).put(rnd.nextInt(), rnd.nextInt());
+
+                tx.commit();
+            }
+        });
+    }
+
+    /** */
+    @Test
+    public void testAsyncPutOps() throws Exception {
+        runOps(() -> {
+            try (Transaction tx = grid(0).transactions().txStart()) {
+                grid(0).cache(DEFAULT_CACHE_NAME).putAsync(rnd.nextInt(), rnd.nextInt()).get();
+
+                tx.commit();
+            }
+        });
+    }
+
+    /** */
+    private void runOps(Runnable tx) throws Exception {
+        AtomicInteger cnt = new AtomicInteger();
+
+        multithreadedAsync(() -> {
+            while (!Thread.interrupted() && cnt.incrementAndGet() < MAX_NUM_OP_BEFORE_EXCEED * 100)
+                tx.run();
+        }, MAX_NUM_OP_BEFORE_EXCEED).get();
+
+        assertFalse("Critical failure occurred", failed.get());
+    }
+}
diff --git a/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteCacheTestSuite13.java b/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteCacheTestSuite13.java
index 28a2fc5..7b647bd 100644
--- a/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteCacheTestSuite13.java
+++ b/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteCacheTestSuite13.java
@@ -40,6 +40,7 @@
 import org.apache.ignite.internal.processors.cache.GridCacheVersionGenerationWithCacheStorageTest;
 import org.apache.ignite.internal.processors.cache.distributed.FailBackupOnAtomicOperationTest;
 import org.apache.ignite.internal.processors.cache.distributed.rebalancing.RebalanceStatisticsTest;
+import org.apache.ignite.internal.processors.cache.transactions.TxAsyncOpsSemaphorePermitsExceededTest;
 import org.apache.ignite.internal.processors.cache.transactions.TxRecoveryOnCoordniatorFailTest;
 import org.apache.ignite.internal.processors.cluster.ClusterNameBeforeActivation;
 import org.apache.ignite.testframework.GridTestUtils;
@@ -98,6 +99,8 @@
 
         GridTestUtils.addTestIfNeeded(suite, CacheClearAsyncDeadlockTest.class, ignoredTests);
 
+        GridTestUtils.addTestIfNeeded(suite, TxAsyncOpsSemaphorePermitsExceededTest.class, ignoredTests);
+
         return suite;
     }
 }