smiklosovic commented on code in PR #3374: URL: https://github.com/apache/cassandra/pull/3374#discussion_r1639018610
########## src/java/org/apache/cassandra/service/snapshot/SnapshotWatcher.java: ########## @@ -0,0 +1,186 @@ +/* + * 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.cassandra.service.snapshot; + +import java.io.IOException; +import java.nio.file.ClosedWatchServiceException; +import java.nio.file.FileSystems; +import java.nio.file.Path; +import java.nio.file.WatchEvent; +import java.nio.file.WatchKey; +import java.nio.file.WatchService; +import java.util.Collection; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.apache.cassandra.concurrent.ExecutorFactory; +import org.apache.cassandra.io.util.File; +import org.apache.cassandra.utils.ExecutorUtils; + +import static java.nio.file.StandardWatchEventKinds.ENTRY_DELETE; + +/** + * SnapshotWatcher watches snapshot directories. When a directory is removed from disk, e.g. manually, + * it will clean respective snapshot from SnapshotManager. The result of doing so is that when somebody + * removes a snapshot via other means from e.g. nodetool clearsnapshot, such snapshot will not be present + * in SnapshotManager anymore hence nodetool listsnapshots nor system_views.snapshots will not display it either. + */ +public class SnapshotWatcher implements AutoCloseable +{ + private static final Logger logger = LoggerFactory.getLogger(SnapshotWatcher.class); + + private final SnapshotManager snapshotManager; + private WatchService watchService; + private ExecutorService executor; + private final Map<WatchKey, Path> watchKeyPathMap = new ConcurrentHashMap<>(); + + private Future<?> watcherFuture; + + public SnapshotWatcher(SnapshotManager snapshotManager) + { + this.snapshotManager = snapshotManager; + } + + public void watch(TableSnapshot snapshot) + { + Collection<File> directories = snapshot.getDirectories(); + if (directories == null) + return; + + for (File snapshotDir : directories) + { + Path snapshotPath = snapshotDir.toPath(); + try + { + WatchKey watchKey = snapshotPath.register(watchService, ENTRY_DELETE); + watchKeyPathMap.put(watchKey, snapshotPath); + } + catch (IOException ex) + { + logger.warn("Unable to register watch service for path " + snapshotPath, ex); + } + } + } + + public void unwatch(TableSnapshot snapshot) + { + for (File snapshotDir : snapshot.getDirectories()) + { + Path snapshotPath = snapshotDir.toPath(); + for (Map.Entry<WatchKey, Path> entry : watchKeyPathMap.entrySet()) + { + if (entry.getValue().equals(snapshotPath)) + { + WatchKey watchKey = entry.getKey(); + watchKeyPathMap.remove(watchKey); + } + } + } + } + + public void start() + { + if (watchService != null || executor != null) + return; + + try + { + watchService = FileSystems.getDefault().newWatchService(); + } + catch (IOException ex) + { + throw new RuntimeException("Unable to get watch service", ex); + } + + executor = getExecutorService(); + + watcherFuture = executor.submit(() -> { + while (!Thread.currentThread().isInterrupted()) + { + WatchKey key; + + try + { + key = watchService.take(); + + for (WatchEvent<?> event : key.pollEvents()) + { + if (event.kind() == ENTRY_DELETE) + { + Path dir = watchKeyPathMap.get(key); + + if (dir == null) + continue; + + snapshotManager.findSnapshot(dir).ifPresent(snapshotManager::clearSnapshot); Review Comment: This should not block and clearing of a snapshot should be done asynchronously to process events as fast as possible as doing it by a blocking manner might cause (in theory) the overflowing of deletion events under absolutely extreme conditions but the probability this happens in the environment we use this API is basically equal to zero. Anyway, to be extra sure, we should just populate a queue of snapshots to be cleared and then a thread in SnapshotManager should just go over it, similarly as expired snapshots are cleared. -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]

