pauloricardomg commented on a change in pull request #1046:
URL: https://github.com/apache/cassandra/pull/1046#discussion_r666874204



##########
File path: src/java/org/apache/cassandra/db/SnapshotDetails.java
##########
@@ -0,0 +1,68 @@
+/*
+ * 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.db;
+
+import java.time.Instant;
+import java.io.File;
+import java.io.IOException;
+import org.apache.cassandra.io.util.FileUtils;
+import java.util.Map;
+import org.apache.cassandra.config.Duration;
+
+
+
+public class SnapshotDetails {
+    public String tag;
+    public String keyspace;
+    public Instant createdAt;
+    public Instant expiresAt;
+
+    public SnapshotDetails(String tag, String keyspace, File manifestFile) {
+        this.tag = tag;
+        this.keyspace = keyspace;
+        try
+        {
+            Map<String, Object> manifest = 
FileUtils.readFileToJson(manifestFile);

Review comment:
       Don't do file operations inside constructors. Read the manifest outside 
before constructing `SnapshotDetails`.

##########
File path: src/java/org/apache/cassandra/db/SnapshotDetails.java
##########
@@ -0,0 +1,68 @@
+/*
+ * 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.db;
+
+import java.time.Instant;
+import java.io.File;
+import java.io.IOException;
+import org.apache.cassandra.io.util.FileUtils;
+import java.util.Map;
+import org.apache.cassandra.config.Duration;
+
+
+
+public class SnapshotDetails {
+    public String tag;
+    public String keyspace;
+    public Instant createdAt;
+    public Instant expiresAt;
+
+    public SnapshotDetails(String tag, String keyspace, File manifestFile) {
+        this.tag = tag;
+        this.keyspace = keyspace;
+        try
+        {
+            Map<String, Object> manifest = 
FileUtils.readFileToJson(manifestFile);
+            if (manifest.containsKey("created_at")) {
+                this.createdAt = 
Instant.parse((String)manifest.get("created_at"));
+            }
+            if (manifest.containsKey("expires_at")) {
+                this.expiresAt = 
Instant.parse((String)manifest.get("expires_at"));
+            }
+        } catch (IOException e) {
+            //

Review comment:
       Wrap with runtime exception.

##########
File path: src/java/org/apache/cassandra/service/StorageService.java
##########
@@ -3672,14 +3673,18 @@ public int garbageCollect(String tombstoneOptionString, 
int jobs, String keyspac
     public void takeSnapshot(String tag, Map<String, String> options, 
String... entities) throws IOException

Review comment:
       @smiklosovic can you resolve this?

##########
File path: src/java/org/apache/cassandra/db/SnapshotDetails.java
##########
@@ -0,0 +1,68 @@
+/*
+ * 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.db;
+
+import java.time.Instant;
+import java.io.File;
+import java.io.IOException;
+import org.apache.cassandra.io.util.FileUtils;
+import java.util.Map;
+import org.apache.cassandra.config.Duration;
+
+
+
+public class SnapshotDetails {
+    public String tag;
+    public String keyspace;
+    public Instant createdAt;
+    public Instant expiresAt;
+
+    public SnapshotDetails(String tag, String keyspace, File manifestFile) {
+        this.tag = tag;
+        this.keyspace = keyspace;
+        try
+        {
+            Map<String, Object> manifest = 
FileUtils.readFileToJson(manifestFile);
+            if (manifest.containsKey("created_at")) {
+                this.createdAt = 
Instant.parse((String)manifest.get("created_at"));

Review comment:
       extract `created_at` field name to constant on `SnapshotDetails`

##########
File path: src/java/org/apache/cassandra/db/SnapshotDetails.java
##########
@@ -0,0 +1,68 @@
+/*
+ * 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.db;
+
+import java.time.Instant;
+import java.io.File;
+import java.io.IOException;
+import org.apache.cassandra.io.util.FileUtils;
+import java.util.Map;
+import org.apache.cassandra.config.Duration;
+
+
+
+public class SnapshotDetails {
+    public String tag;
+    public String keyspace;
+    public Instant createdAt;
+    public Instant expiresAt;
+
+    public SnapshotDetails(String tag, String keyspace, File manifestFile) {
+        this.tag = tag;
+        this.keyspace = keyspace;
+        try
+        {
+            Map<String, Object> manifest = 
FileUtils.readFileToJson(manifestFile);
+            if (manifest.containsKey("created_at")) {
+                this.createdAt = 
Instant.parse((String)manifest.get("created_at"));
+            }
+            if (manifest.containsKey("expires_at")) {
+                this.expiresAt = 
Instant.parse((String)manifest.get("expires_at"));

Review comment:
       extract `expires_at` field name to constant on `SnapshotDetails`

##########
File path: src/java/org/apache/cassandra/db/SnapshotDetails.java
##########
@@ -0,0 +1,68 @@
+/*
+ * 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.db;
+
+import java.time.Instant;
+import java.io.File;
+import java.io.IOException;
+import org.apache.cassandra.io.util.FileUtils;
+import java.util.Map;
+import org.apache.cassandra.config.Duration;
+
+
+
+public class SnapshotDetails {
+    public String tag;
+    public String keyspace;
+    public Instant createdAt;
+    public Instant expiresAt;
+
+    public SnapshotDetails(String tag, String keyspace, File manifestFile) {
+        this.tag = tag;
+        this.keyspace = keyspace;
+        try
+        {
+            Map<String, Object> manifest = 
FileUtils.readFileToJson(manifestFile);
+            if (manifest.containsKey("created_at")) {
+                this.createdAt = 
Instant.parse((String)manifest.get("created_at"));
+            }
+            if (manifest.containsKey("expires_at")) {
+                this.expiresAt = 
Instant.parse((String)manifest.get("expires_at"));
+            }
+        } catch (IOException e) {
+            //
+        }
+    }
+
+
+    public SnapshotDetails(String tag, String keyspace, Duration ttl) {
+        this.tag = tag;
+        this.keyspace = keyspace;
+        this.createdAt = Instant.now();
+        this.expiresAt = createdAt.plusMillis(ttl.toMilliseconds());
+    }
+
+    public boolean isExpired() {
+        if (createdAt == null || expiresAt == null) {
+            return false;
+        }
+        Instant now = Instant.now();

Review comment:
       no need to use variable since it's used in a single place

##########
File path: src/java/org/apache/cassandra/service/SnapshotManager.java
##########
@@ -0,0 +1,75 @@
+/*
+ * 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;
+
+
+import java.util.concurrent.ScheduledFuture;
+import java.util.concurrent.TimeUnit;
+import org.apache.cassandra.db.SnapshotDetails;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.apache.cassandra.config.Duration;
+import org.apache.cassandra.db.Keyspace;
+
+import java.util.Map;
+import java.util.HashMap;
+import java.util.concurrent.ConcurrentHashMap;
+import org.apache.cassandra.concurrent.ScheduledExecutors;
+
+public class SnapshotManager {
+    private volatile ScheduledFuture snapshotCleanupTrigger;
+    private static final Logger logger = 
LoggerFactory.getLogger(SnapshotManager.class);
+    private Map<String, SnapshotDetails> activeSnapshots = new 
ConcurrentHashMap();

Review comment:
       Let's rename this to `ActiveTtlSnapshots` to show that only contains 
snapshots with ttls.

##########
File path: src/java/org/apache/cassandra/service/SnapshotManager.java
##########
@@ -0,0 +1,75 @@
+/*
+ * 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;
+
+
+import java.util.concurrent.ScheduledFuture;
+import java.util.concurrent.TimeUnit;
+import org.apache.cassandra.db.SnapshotDetails;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.apache.cassandra.config.Duration;
+import org.apache.cassandra.db.Keyspace;
+
+import java.util.Map;
+import java.util.HashMap;
+import java.util.concurrent.ConcurrentHashMap;
+import org.apache.cassandra.concurrent.ScheduledExecutors;
+
+public class SnapshotManager {
+    private volatile ScheduledFuture snapshotCleanupTrigger;
+    private static final Logger logger = 
LoggerFactory.getLogger(SnapshotManager.class);
+    private Map<String, SnapshotDetails> activeSnapshots = new 
ConcurrentHashMap();
+
+    public void addSnapshot(String tag, String keyspace, Duration ttl) {
+        SnapshotDetails snapshot = new SnapshotDetails(tag, keyspace, ttl);
+        activeSnapshots.put(snapshot.keyspace + ":" + snapshot.tag, snapshot);
+    }
+
+    private void uploadSnapshotsFromDisk() {
+        for (Keyspace ks : Keyspace.all()) {
+            for (SnapshotDetails snapshot : ks.getSnapshotDetails()) {
+                activeSnapshots.put(snapshot.keyspace + ":" + snapshot.tag, 
snapshot);
+            }
+        }
+    }
+
+    public class SnapshotCleanupTrigger implements Runnable {
+        private final Logger logger = 
LoggerFactory.getLogger(SnapshotCleanupTrigger.class);
+
+        public void run() {
+            logger.info("start cleanup");
+
+            for (SnapshotDetails snapshot : activeSnapshots.values()) {
+                if (snapshot.isExpired()) {
+                    Keyspace.clearSnapshot(snapshot.tag, snapshot.keyspace);
+                    activeSnapshots.remove(snapshot.keyspace + ":" + 
snapshot.tag);
+                }
+            }
+        }
+    }
+
+    public void startScanning() {
+        logger.info("start scheduling cleanups");
+        SnapshotCleanupTrigger trigger = new SnapshotCleanupTrigger();
+    
+        uploadSnapshotsFromDisk();
+
+        snapshotCleanupTrigger = 
ScheduledExecutors.scheduledTasks.scheduleWithFixedDelay(trigger, 10, 10, 
TimeUnit.SECONDS);

Review comment:
       Let's make the delay configurable: 
`Integer.getInteger("cassandra.ttl_snapshot_cleanup_period_seconds", 60)`

##########
File path: src/java/org/apache/cassandra/service/SnapshotManager.java
##########
@@ -0,0 +1,75 @@
+/*
+ * 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;
+
+
+import java.util.concurrent.ScheduledFuture;
+import java.util.concurrent.TimeUnit;
+import org.apache.cassandra.db.SnapshotDetails;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.apache.cassandra.config.Duration;
+import org.apache.cassandra.db.Keyspace;
+
+import java.util.Map;
+import java.util.HashMap;
+import java.util.concurrent.ConcurrentHashMap;
+import org.apache.cassandra.concurrent.ScheduledExecutors;
+
+public class SnapshotManager {
+    private volatile ScheduledFuture snapshotCleanupTrigger;
+    private static final Logger logger = 
LoggerFactory.getLogger(SnapshotManager.class);
+    private Map<String, SnapshotDetails> activeSnapshots = new 
ConcurrentHashMap();
+
+    public void addSnapshot(String tag, String keyspace, Duration ttl) {

Review comment:
       Let's rename this to `addTtlSnapshot` to show that only contains 
snapshots with ttls.
   Also please add an assertion in the first line of the method to reflect 
this: `assert ttl != null`.




-- 
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]

Reply via email to