rpuch commented on code in PR #3475: URL: https://github.com/apache/ignite-3/pull/3475#discussion_r1537360226
########## modules/table/src/main/java/org/apache/ignite/internal/table/distributed/LowWatermarkCandidate.java: ########## @@ -0,0 +1,41 @@ +/* + * 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.table.distributed; + +import java.util.concurrent.CompletableFuture; +import org.apache.ignite.internal.hlc.HybridTimestamp; + +/** Internal class for {@link LowWatermarkImpl}. */ +final class LowWatermarkCandidate { + private final HybridTimestamp lowWatermark; + + private final CompletableFuture<Void> updateFuture; Review Comment: Please add a comment explaining when this future gets completed ########## modules/table/src/main/java/org/apache/ignite/internal/table/distributed/LowWatermarkImpl.java: ########## @@ -292,9 +272,87 @@ private void setLowWatermark(@Nullable HybridTimestamp newLowWatermark) { updateLowWatermarkLock.writeLock().lock(); try { + assert newLowWatermark == null || lowWatermark == null || newLowWatermark.compareTo(lowWatermark) > 0 : + "Low watermark should only grow: [cur=" + lowWatermark + ", new=" + newLowWatermark + "]"; + lowWatermark = newLowWatermark; } finally { updateLowWatermarkLock.writeLock().unlock(); } } + + void onReceiveNetworkMessage(NetworkMessage message, ClusterNode sender, @Nullable Long correlationId) { + inBusyLock(busyLock, () -> { + if (!(message instanceof GetLowWatermarkRequest)) { + return; + } + + assert correlationId != null : sender; + + messagingService.respond( + sender, + MESSAGES_FACTORY.getLowWatermarkResponse().lowWatermark(hybridTimestampToLong(lowWatermark)).build(), + correlationId + ); + }); + } + + @Override + public void updateLowWatermark(HybridTimestamp newLowWatermark) { + inBusyLock(busyLock, () -> { + LowWatermarkCandidate newLowWatermarkCandidate = new LowWatermarkCandidate(newLowWatermark, new CompletableFuture<>()); + LowWatermarkCandidate oldLowWatermarkCandidate; + + do { + oldLowWatermarkCandidate = lowWatermarkCandidate.get(); + + // If another candidate contains a larger low watermark, then there is no need to update. Review Comment: ```suggestion // If another candidate contains a higher low watermark, then there is no need to update. ``` ########## modules/table/src/main/java/org/apache/ignite/internal/table/distributed/LowWatermarkImpl.java: ########## @@ -292,9 +272,87 @@ private void setLowWatermark(@Nullable HybridTimestamp newLowWatermark) { updateLowWatermarkLock.writeLock().lock(); try { + assert newLowWatermark == null || lowWatermark == null || newLowWatermark.compareTo(lowWatermark) > 0 : Review Comment: Do we allow new water mark to be `null` when old WM is not-null? ########## modules/table/src/main/java/org/apache/ignite/internal/table/distributed/LowWatermarkImpl.java: ########## @@ -292,9 +272,87 @@ private void setLowWatermark(@Nullable HybridTimestamp newLowWatermark) { updateLowWatermarkLock.writeLock().lock(); try { + assert newLowWatermark == null || lowWatermark == null || newLowWatermark.compareTo(lowWatermark) > 0 : + "Low watermark should only grow: [cur=" + lowWatermark + ", new=" + newLowWatermark + "]"; + lowWatermark = newLowWatermark; } finally { updateLowWatermarkLock.writeLock().unlock(); } } + + void onReceiveNetworkMessage(NetworkMessage message, ClusterNode sender, @Nullable Long correlationId) { + inBusyLock(busyLock, () -> { + if (!(message instanceof GetLowWatermarkRequest)) { + return; + } + + assert correlationId != null : sender; + + messagingService.respond( + sender, + MESSAGES_FACTORY.getLowWatermarkResponse().lowWatermark(hybridTimestampToLong(lowWatermark)).build(), + correlationId + ); + }); + } + + @Override + public void updateLowWatermark(HybridTimestamp newLowWatermark) { + inBusyLock(busyLock, () -> { + LowWatermarkCandidate newLowWatermarkCandidate = new LowWatermarkCandidate(newLowWatermark, new CompletableFuture<>()); + LowWatermarkCandidate oldLowWatermarkCandidate; + + do { + oldLowWatermarkCandidate = lowWatermarkCandidate.get(); + + // If another candidate contains a larger low watermark, then there is no need to update. + if (oldLowWatermarkCandidate.lowWatermark().compareTo(newLowWatermark) >= 0) { + return; + } + } while (!lowWatermarkCandidate.compareAndSet(oldLowWatermarkCandidate, newLowWatermarkCandidate)); + + // We will start the update as soon as the previous one finishes. + oldLowWatermarkCandidate.updateFuture() + .thenComposeAsync(unused -> updateAndNotify(newLowWatermark), scheduledThreadPool) + .whenComplete((unused, throwable) -> { + if (throwable != null) { + newLowWatermarkCandidate.updateFuture().completeExceptionally(throwable); Review Comment: I suggest to create methods `completeSuccessfully()` and `completeExceptionally(Throwable)` on `LowWatermarkCandidate`; it will become a little easier to find the place where the future gets completed. Also, `updateFuture()` might have `CompletionStage` type, to encapsulate the completion fully (that is, the update future completion will be controlled by the `LowWatermarkCandidate` class itself). ########## modules/table/src/main/java/org/apache/ignite/internal/table/distributed/raft/snapshot/PartitionAccess.java: ########## @@ -214,4 +214,11 @@ public interface PartitionAccess { * @throws StorageException If failed to set the row ID. */ void setNextRowIdToBuildIndex(Map<Integer, RowId> nextRowIdToBuildByIndexId); + + /** + * Updates the low watermark if it is larger than the current one. Review Comment: ```suggestion * Updates the low watermark if it is higher than the current one. ``` -- 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]
