tkalkirill commented on code in PR #3358: URL: https://github.com/apache/ignite-3/pull/3358#discussion_r1523232084
########## modules/index/src/main/java/org/apache/ignite/internal/index/IndexManager.java: ########## @@ -198,6 +198,56 @@ private CompletableFuture<Boolean> onIndexCreate(CreateIndexEventParameters para }); } + private void onIndexRemoved(RemoveIndexEventParameters parameters) { + inBusyLock(busyLock, () -> { + int indexId = parameters.indexId(); + int catalogVersion = parameters.catalogVersion(); + int previousCatalogVersion = catalogVersion - 1; + + // Retrieve descriptor during synchronous call, before the previous catalog version could be concurrently compacted. + CatalogIndexDescriptor indexDescriptor = catalogService.index(indexId, previousCatalogVersion); + assert indexDescriptor != null : "index"; + + int tableId = indexDescriptor.tableId(); + + if (catalogService.table(tableId, catalogVersion) == null) { + // Nothing to do. Index will be destroyed along with the table. + return; + } + + destructionEventsQueue.enqueue(new DestroyIndexEvent(catalogVersion, indexId, tableId)); + }); + } + + private CompletableFuture<Void> onLwmChanged(HybridTimestamp ts) { + return inBusyLockAsync(busyLock, () -> { + int newEarliestCatalogVersion = catalogService.activeCatalogVersion(hybridTimestampToLong(ts)); + + List<DestroyIndexEvent> events = destructionEventsQueue.drainUpTo(newEarliestCatalogVersion); + + if (events.isEmpty()) { + return nullCompletedFuture(); + } + + List<CompletableFuture<Void>> futures = events.stream() + .map(event -> destroyIndexAsync(event.indexId(), event.tableId())) + .collect(toList()); + + return allOf(futures.toArray(CompletableFuture[]::new)); + }); + } + + private CompletableFuture<Void> destroyIndexAsync(int indexId, int tableId) { + return runAsync(() -> inBusyLock(busyLock, () -> { Review Comment: Ok ########## modules/index/src/main/java/org/apache/ignite/internal/index/IndexManager.java: ########## @@ -111,8 +126,11 @@ public IndexManager( public CompletableFuture<Void> start() { LOG.debug("Index manager is about to start"); + recoverDeferredQueue(); Review Comment: Maybe rename to `recoverDestructionEventsQueue();` ? ########## modules/index/src/main/java/org/apache/ignite/internal/index/IndexManager.java: ########## @@ -198,6 +195,48 @@ private CompletableFuture<Boolean> onIndexCreate(CreateIndexEventParameters para }); } + private void onIndexRemoved(RemoveIndexEventParameters parameters) { + inBusyLock(busyLock, () -> { + int indexId = parameters.indexId(); + int catalogVersion = parameters.catalogVersion(); + int previousCatalogVersion = catalogVersion - 1; + + // Retrieve descriptor during synchronous call, before the previous catalog version could be concurrently compacted. + CatalogIndexDescriptor indexDescriptor = catalogService.index(indexId, previousCatalogVersion); + assert indexDescriptor != null : "indexId=" + indexId + ", catalogVersion=" + previousCatalogVersion; + + int tableId = indexDescriptor.tableId(); + + if (catalogService.table(tableId, catalogVersion) == null) { + // Nothing to do. Index will be destroyed along with the table. + return; + } + + destructionEventsQueue.enqueue(new DestroyIndexEvent(catalogVersion, indexId, tableId)); Review Comment: It seems there may be a race, if `onLwmChanged` is executed in parallel, we may skip the index which could be started to be destroyed immediately, we will have to wait for the next lwm update. This is not scary if the sign is updated every second, but if it’s a day, then we can keep the data on disk for quite a long time. We need to somehow fix this race so as not to delay the metastor thread. ########## modules/table/src/main/java/org/apache/ignite/internal/table/SynchronousPriorityQueue.java: ########## @@ -0,0 +1,124 @@ +/* + * 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; + +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; +import java.util.PriorityQueue; +import java.util.function.ToLongFunction; + +/** + * A thread-safe wrapper over {@link java.util.PriorityQueue}, which uses {@code long} value for ordering. + * The implementation provides a method to poll top item up to the given priority. + * + * @param <T> Item type. + */ +public class SynchronousPriorityQueue<T> { + /** A queue. Guarded by itself. */ + private final PriorityQueue<T> queue; + private final ToLongFunction<T> priorityExtractor; + + /** + * Creates a queue. + * + * @param priorityExtractor Priority extractor. + */ + public SynchronousPriorityQueue(ToLongFunction<T> priorityExtractor) { Review Comment: Still don't like the name. It's up to you. ########## modules/table/src/main/java/org/apache/ignite/internal/table/distributed/LowWatermarkImpl.java: ########## @@ -0,0 +1,280 @@ +/* + * 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 static org.apache.ignite.internal.failure.FailureType.CRITICAL_ERROR; +import static org.apache.ignite.internal.util.CompletableFutures.nullCompletedFuture; +import static org.apache.ignite.internal.util.IgniteUtils.inBusyLock; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CopyOnWriteArrayList; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicReference; +import org.apache.ignite.internal.failure.FailureContext; +import org.apache.ignite.internal.failure.FailureProcessor; +import org.apache.ignite.internal.hlc.HybridClock; +import org.apache.ignite.internal.hlc.HybridTimestamp; +import org.apache.ignite.internal.lang.ByteArray; +import org.apache.ignite.internal.lang.NodeStoppingException; +import org.apache.ignite.internal.logger.IgniteLogger; +import org.apache.ignite.internal.logger.Loggers; +import org.apache.ignite.internal.manager.IgniteComponent; +import org.apache.ignite.internal.schema.configuration.LowWatermarkConfiguration; +import org.apache.ignite.internal.thread.NamedThreadFactory; +import org.apache.ignite.internal.tx.TxManager; +import org.apache.ignite.internal.util.ByteUtils; +import org.apache.ignite.internal.util.IgniteSpinBusyLock; +import org.apache.ignite.internal.util.IgniteUtils; +import org.apache.ignite.internal.vault.VaultEntry; +import org.apache.ignite.internal.vault.VaultManager; +import org.jetbrains.annotations.Nullable; + +/** + * Class to manage the low watermark. + * + * <p>Low watermark is the node's local time, which ensures that read-only transactions have completed by this time, and new read-only + * transactions will only be created after this time, and we can safely delete obsolete/garbage data such as: obsolete versions of table + * rows, remote indexes, remote tables, etc. + * + * @see <a href="https://cwiki.apache.org/confluence/display/IGNITE/IEP-91%3A+Transaction+protocol">IEP-91</a> + */ +public class LowWatermarkImpl implements IgniteComponent, LowWatermark { + private static final IgniteLogger LOG = Loggers.forClass(LowWatermarkImpl.class); + + static final ByteArray LOW_WATERMARK_VAULT_KEY = new ByteArray("low-watermark"); + + private final LowWatermarkConfiguration lowWatermarkConfig; + + private final HybridClock clock; + + private final TxManager txManager; + + private final VaultManager vaultManager; + + private final List<LowWatermarkChangedListener> updateListeners = new CopyOnWriteArrayList<>(); + + private final ScheduledExecutorService scheduledThreadPool; + + private final IgniteSpinBusyLock busyLock = new IgniteSpinBusyLock(); + + private final AtomicBoolean closeGuard = new AtomicBoolean(); + + private volatile @Nullable HybridTimestamp lowWatermark; + + private final AtomicReference<ScheduledFuture<?>> lastScheduledTaskFuture = new AtomicReference<>(); + + private final FailureProcessor failureProcessor; + + /** + * Constructor. + * + * @param nodeName Node name. + * @param lowWatermarkConfig Low watermark configuration. + * @param clock A hybrid logical clock. + * @param txManager Transaction manager. + * @param vaultManager Vault manager. + * @param failureProcessor Failure processor tha is used to handle critical errors. + */ + public LowWatermarkImpl( + String nodeName, + LowWatermarkConfiguration lowWatermarkConfig, + HybridClock clock, + TxManager txManager, + VaultManager vaultManager, + FailureProcessor failureProcessor + ) { + this.lowWatermarkConfig = lowWatermarkConfig; + this.clock = clock; + this.txManager = txManager; + this.vaultManager = vaultManager; + this.failureProcessor = failureProcessor; + + scheduledThreadPool = Executors.newSingleThreadScheduledExecutor( + NamedThreadFactory.create(nodeName, "low-watermark-updater", LOG) + ); + } + + /** + * Starts the watermark manager. + */ + @Override + public CompletableFuture<Void> start() { + inBusyLock(busyLock, () -> { + lowWatermark = readLowWatermarkFromVault(); + }); + + return nullCompletedFuture(); + } + + /** + * Schedule watermark updates. + */ + public void scheduleUpdates() { + inBusyLock(busyLock, () -> { + HybridTimestamp lowWatermarkCandidate = lowWatermark; + + if (lowWatermarkCandidate == null) { + LOG.info("Previous value of the low watermark was not found, will schedule to update it"); + + scheduleUpdateLowWatermarkBusy(); + + return; + } + + LOG.info("Low watermark has been scheduled to be updated: {}", lowWatermarkCandidate); + + txManager.updateLowWatermark(lowWatermarkCandidate) + .thenComposeAsync(unused -> inBusyLock(busyLock, () -> notifyListeners(lowWatermarkCandidate)), scheduledThreadPool) + .whenComplete((unused, throwable) -> { + if (throwable == null) { + inBusyLock(busyLock, this::scheduleUpdateLowWatermarkBusy); Review Comment: Okay let's create it. This is one of the reasons to divide the task into several small ones. -- 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