jsancio commented on code in PR #15450:
URL: https://github.com/apache/kafka/pull/15450#discussion_r1509262172


##########
core/src/main/scala/kafka/raft/KafkaMetadataLog.scala:
##########
@@ -179,7 +180,7 @@ final class KafkaMetadataLog private (
         (false, mutable.TreeMap.empty[OffsetAndEpoch, 
Option[FileRawSnapshotReader]])
     }
 
-    removeSnapshots(forgottenSnapshots)
+    removeSnapshots(forgottenSnapshots,RetentionMsBreach())

Review Comment:
   Missing space between `,` and `RetentionMsBreach`.



##########
core/src/main/scala/kafka/raft/KafkaMetadataLog.scala:
##########
@@ -501,6 +524,24 @@ final class KafkaMetadataLog private (
     }
   }
 
+  private def removeSnapshots(
+    expiredSnapshots: mutable.TreeMap[OffsetAndEpoch, 
Option[FileRawSnapshotReader]],
+    reason: SnapshotDeletionReason,
+  ): Unit = {
+    expiredSnapshots.foreach { case (snapshotId, _) =>
+      reason.logReason(snapshotId)
+      Snapshots.markForDelete(log.dir.toPath, snapshotId)
+    }
+
+    if (expiredSnapshots.nonEmpty) {
+      scheduler.scheduleOnce(
+        "delete-snapshot-files",
+        () => KafkaMetadataLog.deleteSnapshotFiles(log.dir.toPath, 
expiredSnapshots, this),
+        config.fileDeleteDelayMs
+      )
+    }
+  }

Review Comment:
   This looks like a duplicate of the method 
`removeSnapshots(mutable.TreeMap[_, _])`.



##########
core/src/main/scala/kafka/raft/KafkaMetadataLog.scala:
##########
@@ -677,4 +692,20 @@ object KafkaMetadataLog extends Logging {
       Snapshots.deleteIfExists(logDir, snapshotId)
     }
   }
+
+  trait SnapshotDeletionReason {
+    def logReason(snapshotId: OffsetAndEpoch): Unit
+  }
+
+  final case class RetentionMsBreach() extends SnapshotDeletionReason {
+    override def logReason(snapshotId: OffsetAndEpoch): Unit = {
+      info(s"Marking snapshot $snapshotId for deletion because the age is too 
old")
+    }
+  }
+
+  final case class RetentionSizeBreach() extends SnapshotDeletionReason {
+    override def logReason(snapshotId: OffsetAndEpoch): Unit = {
+      info(s"Marking snapshot $snapshotId for deletion because the size is too 
big")
+    }

Review Comment:
   Similar comment here. The reason should include this information: `log.size 
+ snapshotTotalSize > config.retentionMaxBytes`
   
   From: 
https://github.com/apache/kafka/pull/15450/files#diff-b332f85b04775c821226b6f704e91d51f9647f29ba73dace65b99cf36f6b9ceaR477



##########
core/src/main/scala/kafka/raft/KafkaMetadataLog.scala:
##########
@@ -484,9 +506,10 @@ final class KafkaMetadataLog private (
    * Rename the given snapshots on the log directory. Asynchronously, close 
and delete the
    * given snapshots after some delay.
    */
+
   private def removeSnapshots(
-    expiredSnapshots: mutable.TreeMap[OffsetAndEpoch, 
Option[FileRawSnapshotReader]]
-  ): Unit = {
+     expiredSnapshots: mutable.TreeMap[OffsetAndEpoch, 
Option[FileRawSnapshotReader]],
+ ): Unit = {

Review Comment:
   This indentation doesn't look correct. I think you want to revert this 
change.



##########
core/src/main/scala/kafka/raft/KafkaMetadataLog.scala:
##########
@@ -359,6 +360,27 @@ final class KafkaMetadataLog private (
     deleted
   }
 
+  def deleteBeforeSnapshot(snapshotId: OffsetAndEpoch,reason: 
SnapshotDeletionReason): Boolean = {
+    val (deleted, forgottenSnapshots) = snapshots synchronized {
+      latestSnapshotId().asScala match {
+        case Some(latestSnapshotId) if
+          snapshots.contains(snapshotId) &&
+            startOffset < snapshotId.offset &&
+            snapshotId.offset <= latestSnapshotId.offset &&
+            log.maybeIncrementLogStartOffset(snapshotId.offset, 
LogStartOffsetIncrementReason.SnapshotGenerated) =>
+          // Delete all segments that have a "last offset" less than the log 
start offset
+          val deletedSegments = log.deleteOldSegments()
+          // Remove older snapshots from the snapshots cache
+          val forgottenSnapshots = forgetSnapshotsBefore(snapshotId)
+          (deletedSegments != 0 || forgottenSnapshots.nonEmpty, 
forgottenSnapshots)
+        case _ =>
+          (false, mutable.TreeMap.empty[OffsetAndEpoch, 
Option[FileRawSnapshotReader]])
+      }
+    }
+    removeSnapshots(forgottenSnapshots, reason)
+    deleted
+  }

Review Comment:
   This looks like a duplicate of `deleteBeforeSnapshot(OffsetAndEpoch)`. If 
tests are calling this method directly, lets add an "unknown" reason and the 
test can use that when deleting snapshots.
   
   You should be able to avoid this code duplication with this change:
   ```scala
   override def deleteBeforeSnapshot(snapshotId: OffsetAndEpoch): Boolean = {
     deleteBeforeSnapshot(snapshotId, UnknownReason())
   }
   ```



##########
core/src/main/scala/kafka/raft/KafkaMetadataLog.scala:
##########
@@ -348,9 +349,9 @@ final class KafkaMetadataLog private (
           snapshotId.offset <= latestSnapshotId.offset &&
           log.maybeIncrementLogStartOffset(snapshotId.offset, 
LogStartOffsetIncrementReason.SnapshotGenerated) =>
             // Delete all segments that have a "last offset" less than the log 
start offset
-            log.deleteOldSegments()
-            // Remove older snapshots from the snapshots cache
-            (true, forgetSnapshotsBefore(snapshotId))
+          log.deleteOldSegments()
+          // Remove older snapshots from the snapshots cache
+          (true, forgetSnapshotsBefore(snapshotId))

Review Comment:
   I think this indentation is off by two spaces. I think you want to revert 
this change.



##########
core/src/main/scala/kafka/MetadataLogConfig.scala:
##########
@@ -0,0 +1,49 @@
+/*
+ * 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 kafka.raft
+
+import kafka.server.KafkaConfig
+import org.apache.kafka.common.config.AbstractConfig
+import org.apache.kafka.storage.internals.log.LogConfig
+
+final case class MetadataLogConfig(
+  logSegmentBytes: Int,
+  logSegmentMinBytes: Int,
+  logSegmentMillis: Long,
+  retentionMaxBytes: Long,
+  retentionMillis: Long,
+  maxBatchSizeInBytes: Int,
+  maxFetchSizeInBytes: Int,
+  fileDeleteDelayMs: Long,
+  nodeId: Int
+)
+
+object MetadataLogConfig {
+  def apply(config: AbstractConfig, maxBatchSizeInBytes: Int, 
maxFetchSizeInBytes: Int): MetadataLogConfig = {
+    new MetadataLogConfig(
+      config.getInt(KafkaConfig.MetadataLogSegmentBytesProp),
+      config.getInt(KafkaConfig.MetadataLogSegmentMinBytesProp),
+      config.getLong(KafkaConfig.MetadataLogSegmentMillisProp),
+      config.getLong(KafkaConfig.MetadataMaxRetentionBytesProp),
+      config.getLong(KafkaConfig.MetadataMaxRetentionMillisProp),
+      maxBatchSizeInBytes,
+      maxFetchSizeInBytes,
+      LogConfig.DEFAULT_FILE_DELETE_DELAY_MS,
+      config.getInt(KafkaConfig.NodeIdProp)
+    )
+  }
+}

Review Comment:
   Missing new line.



##########
core/src/main/scala/kafka/raft/KafkaMetadataLog.scala:
##########
@@ -677,4 +692,20 @@ object KafkaMetadataLog extends Logging {
       Snapshots.deleteIfExists(logDir, snapshotId)
     }
   }
+
+  trait SnapshotDeletionReason {
+    def logReason(snapshotId: OffsetAndEpoch): Unit
+  }
+
+  final case class RetentionMsBreach() extends SnapshotDeletionReason {
+    override def logReason(snapshotId: OffsetAndEpoch): Unit = {
+      info(s"Marking snapshot $snapshotId for deletion because the age is too 
old")

Review Comment:
   Should this function returns a `String` instead log calling `info` directly?
   
   The reason should include this information: `now - timestamp > 
config.retentionMillis`. From 
https://github.com/apache/kafka/pull/15450/files#diff-b332f85b04775c821226b6f704e91d51f9647f29ba73dace65b99cf36f6b9ceaR455
   
   For Example,
   `s"the snapshot's timestamp ($timestamp) is now ($now) older than the 
retention (${config.retentionMillis})"`



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

Reply via email to