sanpwc commented on code in PR #4974: URL: https://github.com/apache/ignite-3/pull/4974#discussion_r1898464919
########## modules/client/src/test/java/org/apache/ignite/client/fakes/FakeInternalTable.java: ########## @@ -147,6 +147,7 @@ public CompletableFuture<BinaryRow> get(BinaryRowEx keyRow, @Nullable InternalTr public CompletableFuture<BinaryRow> get( BinaryRowEx keyRow, HybridTimestamp readTimestamp, + @Nullable UUID transactionId, Review Comment: I saw you discussion with @vldpyatkov. Let it be the way you proposing now (readTimestamp + transactionId), but generally I believe that proposal that involves passing transaction is better. It's however out of scope of your PR. I see, you've created https://issues.apache.org/jira/browse/IGNITE-24120 ########## modules/sql-engine/src/integrationTest/java/org/apache/ignite/internal/sql/sqllogic/ItSqlLogicTest.java: ########## @@ -142,6 +143,8 @@ @Tag("sqllogic") @ExtendWith({SystemPropertiesExtension.class, WorkDirectoryExtension.class}) @WithSystemProperty(key = "IMPLICIT_PK_ENABLED", value = "true") +// The following is to make sure we unlock LWM on data nodes promptly so that dropped tables are destroyed fast. +@WithSystemProperty(key = ResourceVacuumManager.RESOURCE_VACUUM_INTERVAL_MILLISECONDS_PROPERTY, value = "1000") Review Comment: I'd rather run corresponding tests multiple times on TC before merge in order to verify that they are stable. I do understand that it's just an interval though. ########## modules/transactions/src/integrationTest/java/org/apache/ignite/internal/tx/readonly/ItReadOnlyTxAndLowWatermarkTest.java: ########## @@ -0,0 +1,251 @@ +/* + * 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.tx.readonly; + +import static java.util.concurrent.TimeUnit.SECONDS; +import static java.util.stream.Collectors.toList; +import static org.apache.ignite.internal.TestWrappers.unwrapIgniteImpl; +import static org.apache.ignite.internal.TestWrappers.unwrapInternalTransaction; +import static org.apache.ignite.internal.testframework.IgniteTestUtils.waitForCondition; +import static org.apache.ignite.internal.testframework.matchers.CompletableFutureMatcher.willCompleteSuccessfully; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.hasToString; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.isA; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assumptions.assumeFalse; + +import java.util.List; +import java.util.stream.IntStream; +import org.apache.ignite.Ignite; +import org.apache.ignite.InitParametersBuilder; +import org.apache.ignite.internal.ClusterPerTestIntegrationTest; +import org.apache.ignite.internal.app.IgniteImpl; +import org.apache.ignite.internal.hlc.HybridTimestamp; +import org.apache.ignite.internal.lowwatermark.LowWatermarkImpl; +import org.apache.ignite.internal.schema.configuration.GcExtensionConfiguration; +import org.apache.ignite.internal.schema.configuration.LowWatermarkConfiguration; +import org.apache.ignite.internal.testframework.WithSystemProperty; +import org.apache.ignite.internal.tx.impl.ResourceVacuumManager; +import org.apache.ignite.lang.ErrorGroups.Transactions; +import org.apache.ignite.lang.IgniteException; +import org.apache.ignite.sql.ResultSet; +import org.apache.ignite.sql.SqlException; +import org.apache.ignite.sql.SqlRow; +import org.apache.ignite.table.KeyValueView; +import org.apache.ignite.tx.Transaction; +import org.apache.ignite.tx.TransactionException; +import org.apache.ignite.tx.TransactionOptions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.EnumSource; +import org.junitpioneer.jupiter.cartesian.CartesianTest; +import org.junitpioneer.jupiter.cartesian.CartesianTest.Enum; +import org.junitpioneer.jupiter.cartesian.CartesianTest.Values; + +class ItReadOnlyTxAndLowWatermarkTest extends ClusterPerTestIntegrationTest { + private static final String TABLE_NAME = "TEST_TABLE"; + + // 100 keys to make sure that at least one key ends up on every of 2 nodes. + private static final int KEY_COUNT = 100; + + private static final long SHORT_DATA_AVAILABILITY_TIME_MS = 1000; + + @Override + protected int initialNodes() { + // 2 nodes to have a non-coordinator node in cluster. + return 2; + } + + @Override + protected void customizeInitParameters(InitParametersBuilder builder) { + builder.clusterConfiguration("ignite.gc.lowWatermark: {\n" + // Update frequently. + + " updateInterval: 100\n" + + "}"); + } + + @BeforeEach + void createTable() { + node(0).sql().executeScript("CREATE TABLE " + TABLE_NAME + " (ID INT PRIMARY KEY, VAL VARCHAR)"); + } + + @ParameterizedTest + @EnumSource(TransactionalReader.class) + void roTransactionNoticesTupleVersionsMissingDueToGcOnDataNodes(TransactionalReader reader) throws Exception { + // TODO: remove the assumption when IGNITE-24119 is fixed. + assumeFalse(reader == TransactionalReader.MULTI_GET); + + updateDataAvailabilityTimeToShortPeriod(); + + Ignite coordinator = node(0); + KeyValueView<Integer, String> kvView = kvView(coordinator); + + insertOriginalValues(KEY_COUNT, kvView); + + Transaction roTx = coordinator.transactions().begin(new TransactionOptions().readOnly(true)); + + updateToNewValues(KEY_COUNT, kvView); + + waitTillLwmTriesToRaiseAndEraseOverrittenVersions(); + + IgniteException ex = assertThrows(IgniteException.class, () -> reader.read(coordinator, roTx)); + assertThat(ex, isA(reader.sql() ? SqlException.class : TransactionException.class)); + assertThat(ex, hasToString(containsString("Read timestamp is not available anymore."))); + assertThat("Wrong error code: " + ex.codeAsString(), ex.code(), is(Transactions.TX_STALE_READ_ONLY_OPERATION_ERR)); + } + + private void updateDataAvailabilityTimeToShortPeriod() { + IgniteImpl igniteImpl = unwrapIgniteImpl(node(0)); + + LowWatermarkConfiguration lwmConfig = igniteImpl.clusterConfiguration() + .getConfiguration(GcExtensionConfiguration.KEY) + .gc() + .lowWatermark(); + + assertThat(lwmConfig.dataAvailabilityTime().update(SHORT_DATA_AVAILABILITY_TIME_MS), willCompleteSuccessfully()); + } + + private static KeyValueView<Integer, String> kvView(Ignite coordinator) { + return coordinator.tables().table(TABLE_NAME).keyValueView(Integer.class, String.class); + } + + private static void insertOriginalValues(int keyCount, KeyValueView<Integer, String> kvView) { + for (int i = 0; i < keyCount; i++) { + kvView.put(null, i, "original-" + i); + } + } + + private static void updateToNewValues(int keyCount, KeyValueView<Integer, String> kvView) { + for (int i = 0; i < keyCount; i++) { + kvView.put(null, i, "updated-" + i); + } + } + + private static void waitTillLwmTriesToRaiseAndEraseOverrittenVersions() throws InterruptedException { + Thread.sleep(2 * SHORT_DATA_AVAILABILITY_TIME_MS); + } + + @CartesianTest + @WithSystemProperty(key = ResourceVacuumManager.RESOURCE_VACUUM_INTERVAL_MILLISECONDS_PROPERTY, value = "100") + void lwmIsAllowedToBeRaisedOnDataNodesAfterRoTransactionFinish( Review Comment: I'm curios how it's expected to work in case of abandoned RO transactions? I mean transactions that lost their coordinators. I believe that we should add test for such scenario. ########## modules/table/src/main/java/org/apache/ignite/internal/table/distributed/replicator/PartitionReplicaListener.java: ########## @@ -3983,14 +3986,65 @@ private CompletableFuture<?> processOperationRequestWithTxRwCounter( } } - return processOperationRequest(senderId, request, isPrimary, opStartTsIfDirectRo, leaseStartTime) - .whenComplete((unused, throwable) -> { - if (request instanceof ReadWriteReplicaRequest) { - txRwOperationTracker.decrementOperationCount( - rwTxActiveCatalogVersion(catalogService, (ReadWriteReplicaRequest) request) - ); - } - }); + UUID txIdLockingLwm = tryToLockLwmIfNeeded(request, opStartTsIfDirectRo); + + try { + return processOperationRequest(senderId, request, isPrimary, opStartTsIfDirectRo, leaseStartTime) + .whenComplete((unused, throwable) -> { + unlockLwmIfNeeded(txIdLockingLwm, request); + + if (request instanceof ReadWriteReplicaRequest) { + txRwOperationTracker.decrementOperationCount( + rwTxActiveCatalogVersion(catalogService, (ReadWriteReplicaRequest) request) + ); + } + }); + } catch (Throwable e) { + try { + unlockLwmIfNeeded(txIdLockingLwm, request); + } catch (Throwable unlockProblem) { + e.addSuppressed(unlockProblem); + } + throw e; + } + } + + private static UUID newFakeTxId() { Review Comment: Could you please add a javadoc or a comment that will explain why we need such method. ########## modules/transactions/src/integrationTest/java/org/apache/ignite/internal/tx/readonly/ItReadOnlyTxAndLowWatermarkTest.java: ########## @@ -0,0 +1,251 @@ +/* + * 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.tx.readonly; + +import static java.util.concurrent.TimeUnit.SECONDS; +import static java.util.stream.Collectors.toList; +import static org.apache.ignite.internal.TestWrappers.unwrapIgniteImpl; +import static org.apache.ignite.internal.TestWrappers.unwrapInternalTransaction; +import static org.apache.ignite.internal.testframework.IgniteTestUtils.waitForCondition; +import static org.apache.ignite.internal.testframework.matchers.CompletableFutureMatcher.willCompleteSuccessfully; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.hasToString; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.isA; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assumptions.assumeFalse; + +import java.util.List; +import java.util.stream.IntStream; +import org.apache.ignite.Ignite; +import org.apache.ignite.InitParametersBuilder; +import org.apache.ignite.internal.ClusterPerTestIntegrationTest; +import org.apache.ignite.internal.app.IgniteImpl; +import org.apache.ignite.internal.hlc.HybridTimestamp; +import org.apache.ignite.internal.lowwatermark.LowWatermarkImpl; +import org.apache.ignite.internal.schema.configuration.GcExtensionConfiguration; +import org.apache.ignite.internal.schema.configuration.LowWatermarkConfiguration; +import org.apache.ignite.internal.testframework.WithSystemProperty; +import org.apache.ignite.internal.tx.impl.ResourceVacuumManager; +import org.apache.ignite.lang.ErrorGroups.Transactions; +import org.apache.ignite.lang.IgniteException; +import org.apache.ignite.sql.ResultSet; +import org.apache.ignite.sql.SqlException; +import org.apache.ignite.sql.SqlRow; +import org.apache.ignite.table.KeyValueView; +import org.apache.ignite.tx.Transaction; +import org.apache.ignite.tx.TransactionException; +import org.apache.ignite.tx.TransactionOptions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.EnumSource; +import org.junitpioneer.jupiter.cartesian.CartesianTest; +import org.junitpioneer.jupiter.cartesian.CartesianTest.Enum; +import org.junitpioneer.jupiter.cartesian.CartesianTest.Values; + +class ItReadOnlyTxAndLowWatermarkTest extends ClusterPerTestIntegrationTest { + private static final String TABLE_NAME = "TEST_TABLE"; + + // 100 keys to make sure that at least one key ends up on every of 2 nodes. Review Comment: Do you assert that prerequisite in test? If not, I'd rather add such assertion. ########## modules/table/src/main/java/org/apache/ignite/internal/table/distributed/replicator/PartitionReplicaListener.java: ########## @@ -534,7 +540,7 @@ private CompletableFuture<?> processRequest(ReplicaRequest request, @Nullable Bo // Don't need to validate schema. if (opTs == null) { assert opTsIfDirectRo == null; - return processOperationRequestWithTxRwCounter(senderId, request, isPrimary, null, leaseStartTime); + return processOperationRequestWithWrappingLogic(senderId, request, isPrimary, null, leaseStartTime); Review Comment: I don't like the postfix "WithWrappingLogic", wrapping is very general term and thus such postfix says nothing. ########## modules/table/src/main/java/org/apache/ignite/internal/table/distributed/replicator/PartitionReplicaListener.java: ########## @@ -3983,14 +3986,65 @@ private CompletableFuture<?> processOperationRequestWithTxRwCounter( } } - return processOperationRequest(senderId, request, isPrimary, opStartTsIfDirectRo, leaseStartTime) - .whenComplete((unused, throwable) -> { - if (request instanceof ReadWriteReplicaRequest) { - txRwOperationTracker.decrementOperationCount( - rwTxActiveCatalogVersion(catalogService, (ReadWriteReplicaRequest) request) - ); - } - }); + UUID txIdLockingLwm = tryToLockLwmIfNeeded(request, opStartTsIfDirectRo); + + try { + return processOperationRequest(senderId, request, isPrimary, opStartTsIfDirectRo, leaseStartTime) + .whenComplete((unused, throwable) -> { + unlockLwmIfNeeded(txIdLockingLwm, request); Review Comment: I don't like the fact that we detect whether we need to do action on different abstraction levels. `unlockLwmIfNeeded` hides `if (request instanceof ReadOnlyDirectReplicaRequest) ` but `if (request instanceof ReadWriteReplicaRequest)` is right in front of us. We should either hide both or expose both. In order to have symmetry with tryToLockLwmIfNeeded, I'd prefer hiding both in given case. ########## modules/transactions/src/main/java/org/apache/ignite/internal/tx/impl/ResourceVacuumManager.java: ########## @@ -105,7 +108,7 @@ public ResourceVacuumManager( transactionInflights ); this.finishedTransactionBatchRequestHandler = - new FinishedTransactionBatchRequestHandler(messagingService, resourceRegistry, resourceVacuumExecutor); + new FinishedTransactionBatchRequestHandler(messagingService, resourceRegistry, lowWatermark, resourceVacuumExecutor); Review Comment: I prefer one at a line in case of >3 params. ########## modules/table/src/main/java/org/apache/ignite/internal/table/distributed/replicator/PartitionReplicaListener.java: ########## @@ -2196,9 +2202,6 @@ private <T> CompletableFuture<T> appendTxCommand( })) ); } - } catch (Exception e) { Review Comment: What will happen if cursor related exception will be thrown? Or it's not possible? ########## modules/table/src/main/java/org/apache/ignite/internal/table/distributed/replicator/PartitionReplicaListener.java: ########## @@ -3983,14 +3986,65 @@ private CompletableFuture<?> processOperationRequestWithTxRwCounter( } } - return processOperationRequest(senderId, request, isPrimary, opStartTsIfDirectRo, leaseStartTime) - .whenComplete((unused, throwable) -> { - if (request instanceof ReadWriteReplicaRequest) { - txRwOperationTracker.decrementOperationCount( - rwTxActiveCatalogVersion(catalogService, (ReadWriteReplicaRequest) request) - ); - } - }); + UUID txIdLockingLwm = tryToLockLwmIfNeeded(request, opStartTsIfDirectRo); + + try { + return processOperationRequest(senderId, request, isPrimary, opStartTsIfDirectRo, leaseStartTime) + .whenComplete((unused, throwable) -> { + unlockLwmIfNeeded(txIdLockingLwm, request); + + if (request instanceof ReadWriteReplicaRequest) { + txRwOperationTracker.decrementOperationCount( + rwTxActiveCatalogVersion(catalogService, (ReadWriteReplicaRequest) request) + ); + } + }); + } catch (Throwable e) { + try { + unlockLwmIfNeeded(txIdLockingLwm, request); + } catch (Throwable unlockProblem) { + e.addSuppressed(unlockProblem); + } + throw e; + } + } + + private static UUID newFakeTxId() { + return UUID.randomUUID(); + } + + private @Nullable UUID tryToLockLwmIfNeeded(ReplicaRequest request, @Nullable HybridTimestamp opStartTsIfDirectRo) { Review Comment: Could you please add a javadoc that will explain when we do lock the LWM and when we unlock it? Basically, explanation that you have in PR, seems perfect here. > When executing an operation in an RO transaction (explicit or implicit), attempt to lock LWM on the data node where it's executed If lock attempt fails, throw an exception with a specific error code When cleaning up after an RO transaction had been finished, unlock LWM on each node where such a cleanup happens For direct RO operations (which happen in implicit RO transactions), unlock LWM right after the read has been done on the data node -- 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: notifications-unsubscr...@ignite.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org