You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jira@kafka.apache.org by "hni61223 (via GitHub)" <gi...@apache.org> on 2024/03/01 08:54:58 UTC

[PR] Add Deletion Reasons in KRaft Snapshot Deletion Logging [kafka]

hni61223 opened a new pull request, #15450:
URL: https://github.com/apache/kafka/pull/15450

   *More detailed description of your change,
   if necessary. The PR title and PR message become
   the squashed commit message, so use a separate
   comment to ping reviewers.*
   
   *Summary of testing strategy (including rationale)
   for the feature or bug fix. Unit and/or integration
   tests are expected for any behaviour change and
   system tests should be considered for larger changes.*
   
   ### Committer Checklist (excluded from commit message)
   - [ ] Verify design and implementation 
   - [ ] Verify test coverage and CI build status
   - [ ] Verify documentation (including upgrade notes)
   


-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add Deletion Reasons in KRaft Snapshot Deletion Logging [kafka]

Posted by "jsancio (via GitHub)" <gi...@apache.org>.
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add Deletion Reasons in KRaft Snapshot Deletion Logging [kafka]

Posted by "hni61223 (via GitHub)" <gi...@apache.org>.
hni61223 commented on code in PR #15450:
URL: https://github.com/apache/kafka/pull/15450#discussion_r1510985119


##########
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:
   Do you mind give me a hint on how to pass the time difference (i.e., now - timestamp) to the `RetentionMsBreanch` function? I tried to modify the `shouldClean` function to pass a tuple of (Boolean, Long) to pass the time difference as the second factor, but was blocked. Could you give me some hints to unblock? Thanks!



##########
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:
   Do you mind giving me a hint on how to pass the time difference (i.e., now - timestamp) to the `RetentionMsBreanch` function? I tried to modify the `shouldClean` function to pass a tuple of (Boolean, Long) to pass the time difference as the second factor, but was blocked. Could you give me some hints to unblock? Thanks!



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] MINOR; Log reason for deletion of KRaft snapshot [kafka]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #15450:
URL: https://github.com/apache/kafka/pull/15450#issuecomment-2146521360

   This PR is being marked as stale since it has not had any activity in 90 days. If you would like to keep this PR alive, please ask a committer for review. If the PR has  merge conflicts, please update it with the latest from trunk (or appropriate release branch) <p> If this PR is no longer valid or desired, please feel free to close it. If no activity occurs in the next 30 days, it will be automatically closed.


-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org