You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jira@kafka.apache.org by GitBox <gi...@apache.org> on 2021/03/17 23:24:58 UTC

[GitHub] [kafka] jsancio opened a new pull request #10344: MINOR: Remove use of NoSuchElementException

jsancio opened a new pull request #10344:
URL: https://github.com/apache/kafka/pull/10344


   *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.

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



[GitHub] [kafka] hachikuji commented on pull request #10344: MINOR: Remove use of NoSuchElementException

Posted by GitBox <gi...@apache.org>.
hachikuji commented on pull request #10344:
URL: https://github.com/apache/kafka/pull/10344#issuecomment-801520703


   One other thing maybe we can fix here. In `ReplicatedLog.validateOffsetAndEpoch`, we have the following check:
   ```
           } else if (
                   earliestSnapshotId().isPresent() &&
                   ((offset < startOffset()) ||
                    (offset == startOffset() && epoch != earliestSnapshotId().get().epoch) ||
                    (epoch < earliestSnapshotId().get().epoch))
           ) {
   ```
   We probably should be caching the result of `earliestSnapshotId`. This is probably one reason it was killing performance. 


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

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



[GitHub] [kafka] jsancio commented on a change in pull request #10344: MINOR: Remove use of NoSuchElementException

Posted by GitBox <gi...@apache.org>.
jsancio commented on a change in pull request #10344:
URL: https://github.com/apache/kafka/pull/10344#discussion_r596474584



##########
File path: core/src/main/scala/kafka/raft/KafkaMetadataLog.scala
##########
@@ -37,8 +37,9 @@ final class KafkaMetadataLog private (
   log: Log,
   scheduler: Scheduler,
   // This object needs to be thread-safe because it is used by the snapshotting thread to notify the
-  // polling thread when snapshots are created.
-  snapshotIds: ConcurrentSkipListSet[OffsetAndEpoch],
+  // polling thread when snapshots are created. Using a Map instead of a Set so that there is no
+  // need to handle NoSuchElementException.
+  snapshotIds: ConcurrentSkipListMap[OffsetAndEpoch, Unit],

Review comment:
       > For the sake of having an alternative, what if we replaced the concurrent collection with a TreeSet and a lock? The main contention seems to be around accessing the earliest snapshot id. Perhaps we could use an atomic reference or a volatile for the earliest and latest snapshot ids so that we don't need to go through the lock.
   
   This would work. Yeah, I think every polling cycle of the `KafkaRaftClient` needs to read the **latest** snapshot in `maybeUpdateOldestSnapshotId`. I am tempted to wait until we/I implement https://issues.apache.org/jira/browse/KAFKA-12155 to see what the access pattern looks like. I am working on this next. What do you think?




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

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



[GitHub] [kafka] hachikuji edited a comment on pull request #10344: MINOR: Remove use of NoSuchElementException

Posted by GitBox <gi...@apache.org>.
hachikuji edited a comment on pull request #10344:
URL: https://github.com/apache/kafka/pull/10344#issuecomment-801520703


   One other thing maybe we can fix here. In `ReplicatedLog.validateOffsetAndEpoch`, we have the following check:
   ```
           } else if (
                   earliestSnapshotId().isPresent() &&
                   ((offset < startOffset()) ||
                    (offset == startOffset() && epoch != earliestSnapshotId().get().epoch) ||
                    (epoch < earliestSnapshotId().get().epoch))
           ) {
   ```
   We should be caching the result of `earliestSnapshotId`. This is probably one reason it was killing performance. 


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

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



[GitHub] [kafka] ijuma commented on a change in pull request #10344: MINOR: Remove use of NoSuchElementException

Posted by GitBox <gi...@apache.org>.
ijuma commented on a change in pull request #10344:
URL: https://github.com/apache/kafka/pull/10344#discussion_r596895503



##########
File path: core/src/main/scala/kafka/raft/KafkaMetadataLog.scala
##########
@@ -37,8 +37,9 @@ final class KafkaMetadataLog private (
   log: Log,
   scheduler: Scheduler,
   // This object needs to be thread-safe because it is used by the snapshotting thread to notify the
-  // polling thread when snapshots are created.
-  snapshotIds: ConcurrentSkipListSet[OffsetAndEpoch],
+  // polling thread when snapshots are created. Using a Map instead of a Set so that there is no
+  // need to handle NoSuchElementException.
+  snapshotIds: ConcurrentSkipListMap[OffsetAndEpoch, Unit],

Review comment:
       @jsancio Iterators need to provide consistent behavior or they would not satisfy the Iterator contract. What I mean is: if `hasNext` returns `true`, then `next` has to return an element. It would not be ok for `next` to do something different in that case. So, it seems safe to use here. In any case, not a big deal to use `ConcurrentNavigableMap` since that's what `ConcurrentSkipListSet` uses too. But if we do that, I would suggest creating a simple wrapper that exposes the methods you want.




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

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



[GitHub] [kafka] hachikuji commented on a change in pull request #10344: MINOR: Remove use of NoSuchElementException

Posted by GitBox <gi...@apache.org>.
hachikuji commented on a change in pull request #10344:
URL: https://github.com/apache/kafka/pull/10344#discussion_r596467002



##########
File path: core/src/main/scala/kafka/raft/KafkaMetadataLog.scala
##########
@@ -37,8 +37,9 @@ final class KafkaMetadataLog private (
   log: Log,
   scheduler: Scheduler,
   // This object needs to be thread-safe because it is used by the snapshotting thread to notify the
-  // polling thread when snapshots are created.
-  snapshotIds: ConcurrentSkipListSet[OffsetAndEpoch],
+  // polling thread when snapshots are created. Using a Map instead of a Set so that there is no
+  // need to handle NoSuchElementException.
+  snapshotIds: ConcurrentSkipListMap[OffsetAndEpoch, Unit],

Review comment:
       For the sake of having an alternative, what if we replaced the concurrent collection with a TreeMap and a lock? The main contention seems to be around accessing the earliest snapshot id. Perhaps we could use an atomic reference or a volatile for the earliest and latest snapshot ids so that we don't need to go through the lock.




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

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



[GitHub] [kafka] hachikuji commented on a change in pull request #10344: MINOR: Remove use of NoSuchElementException

Posted by GitBox <gi...@apache.org>.
hachikuji commented on a change in pull request #10344:
URL: https://github.com/apache/kafka/pull/10344#discussion_r596484376



##########
File path: core/src/main/scala/kafka/raft/KafkaMetadataLog.scala
##########
@@ -37,8 +37,9 @@ final class KafkaMetadataLog private (
   log: Log,
   scheduler: Scheduler,
   // This object needs to be thread-safe because it is used by the snapshotting thread to notify the
-  // polling thread when snapshots are created.
-  snapshotIds: ConcurrentSkipListSet[OffsetAndEpoch],
+  // polling thread when snapshots are created. Using a Map instead of a Set so that there is no
+  // need to handle NoSuchElementException.
+  snapshotIds: ConcurrentSkipListMap[OffsetAndEpoch, Unit],

Review comment:
       Sounds fine to me.




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

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



[GitHub] [kafka] jsancio commented on a change in pull request #10344: MINOR: Remove use of NoSuchElementException

Posted by GitBox <gi...@apache.org>.
jsancio commented on a change in pull request #10344:
URL: https://github.com/apache/kafka/pull/10344#discussion_r596455495



##########
File path: core/src/main/scala/kafka/raft/KafkaMetadataLog.scala
##########
@@ -37,8 +37,9 @@ final class KafkaMetadataLog private (
   log: Log,
   scheduler: Scheduler,
   // This object needs to be thread-safe because it is used by the snapshotting thread to notify the
-  // polling thread when snapshots are created.
-  snapshotIds: ConcurrentSkipListSet[OffsetAndEpoch],
+  // polling thread when snapshots are created. Using a Map instead of a Set so that there is no
+  // need to handle NoSuchElementException.
+  snapshotIds: ConcurrentSkipListMap[OffsetAndEpoch, Unit],

Review comment:
       `ConcurrentSkipListMap` has `lastEntry` and `firstEntry` that return `null` if the map is empty. `ConcurrentSkipListSet` only has `last` and `first` which throws if the set is empty. :dizzy: 




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

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



[GitHub] [kafka] hachikuji commented on a change in pull request #10344: MINOR: Remove use of NoSuchElementException

Posted by GitBox <gi...@apache.org>.
hachikuji commented on a change in pull request #10344:
URL: https://github.com/apache/kafka/pull/10344#discussion_r596467002



##########
File path: core/src/main/scala/kafka/raft/KafkaMetadataLog.scala
##########
@@ -37,8 +37,9 @@ final class KafkaMetadataLog private (
   log: Log,
   scheduler: Scheduler,
   // This object needs to be thread-safe because it is used by the snapshotting thread to notify the
-  // polling thread when snapshots are created.
-  snapshotIds: ConcurrentSkipListSet[OffsetAndEpoch],
+  // polling thread when snapshots are created. Using a Map instead of a Set so that there is no
+  // need to handle NoSuchElementException.
+  snapshotIds: ConcurrentSkipListMap[OffsetAndEpoch, Unit],

Review comment:
       For the sake of having an alternative, what if we replaced the concurrent collection with a TreeSet and a lock? The main contention seems to be around accessing the earliest snapshot id. Perhaps we could use an atomic reference or a volatile for the earliest and latest snapshot ids so that we don't need to go through the lock.




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

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



[GitHub] [kafka] jsancio commented on a change in pull request #10344: MINOR: Remove use of NoSuchElementException

Posted by GitBox <gi...@apache.org>.
jsancio commented on a change in pull request #10344:
URL: https://github.com/apache/kafka/pull/10344#discussion_r596467199



##########
File path: core/src/main/scala/kafka/raft/KafkaMetadataLog.scala
##########
@@ -37,8 +37,9 @@ final class KafkaMetadataLog private (
   log: Log,
   scheduler: Scheduler,
   // This object needs to be thread-safe because it is used by the snapshotting thread to notify the
-  // polling thread when snapshots are created.
-  snapshotIds: ConcurrentSkipListSet[OffsetAndEpoch],
+  // polling thread when snapshots are created. Using a Map instead of a Set so that there is no
+  // need to handle NoSuchElementException.
+  snapshotIds: ConcurrentSkipListMap[OffsetAndEpoch, Unit],

Review comment:
       We can probably use iterator and reverse-iterator but iterators require `hasNext` follow by `next`. I didn't want to think/research the concurrency and consistency implications.




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

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



[GitHub] [kafka] ijuma commented on a change in pull request #10344: MINOR: Remove use of NoSuchElementException

Posted by GitBox <gi...@apache.org>.
ijuma commented on a change in pull request #10344:
URL: https://github.com/apache/kafka/pull/10344#discussion_r596465385



##########
File path: core/src/main/scala/kafka/raft/KafkaMetadataLog.scala
##########
@@ -37,8 +37,9 @@ final class KafkaMetadataLog private (
   log: Log,
   scheduler: Scheduler,
   // This object needs to be thread-safe because it is used by the snapshotting thread to notify the
-  // polling thread when snapshots are created.
-  snapshotIds: ConcurrentSkipListSet[OffsetAndEpoch],
+  // polling thread when snapshots are created. Using a Map instead of a Set so that there is no
+  // need to handle NoSuchElementException.
+  snapshotIds: ConcurrentSkipListMap[OffsetAndEpoch, Unit],

Review comment:
       Throwing is definitely a bad idea. You can't iterator to get the desired behavior?




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

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



[GitHub] [kafka] jsancio commented on a change in pull request #10344: MINOR: Remove use of NoSuchElementException

Posted by GitBox <gi...@apache.org>.
jsancio commented on a change in pull request #10344:
URL: https://github.com/apache/kafka/pull/10344#discussion_r596455495



##########
File path: core/src/main/scala/kafka/raft/KafkaMetadataLog.scala
##########
@@ -37,8 +37,9 @@ final class KafkaMetadataLog private (
   log: Log,
   scheduler: Scheduler,
   // This object needs to be thread-safe because it is used by the snapshotting thread to notify the
-  // polling thread when snapshots are created.
-  snapshotIds: ConcurrentSkipListSet[OffsetAndEpoch],
+  // polling thread when snapshots are created. Using a Map instead of a Set so that there is no
+  // need to handle NoSuchElementException.
+  snapshotIds: ConcurrentSkipListMap[OffsetAndEpoch, Unit],

Review comment:
       `ConcurrentSkipListMap` has `lastEntry` and `firstEntry` that return `null` if the map is empty. `ConcurrentSkipListSet` only has `last` and `first` which throws if the set is empty. :dizzy: 
   
   This show up as a performance issue in the simulation test which use `MockLog`. https://github.com/apache/kafka/pull/10323 fixes the issue in `MockLog`.




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

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



[GitHub] [kafka] hachikuji merged pull request #10344: MINOR: Remove use of NoSuchElementException

Posted by GitBox <gi...@apache.org>.
hachikuji merged pull request #10344:
URL: https://github.com/apache/kafka/pull/10344


   


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

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



[GitHub] [kafka] ijuma commented on a change in pull request #10344: MINOR: Remove use of NoSuchElementException

Posted by GitBox <gi...@apache.org>.
ijuma commented on a change in pull request #10344:
URL: https://github.com/apache/kafka/pull/10344#discussion_r596453408



##########
File path: core/src/main/scala/kafka/raft/KafkaMetadataLog.scala
##########
@@ -37,8 +37,9 @@ final class KafkaMetadataLog private (
   log: Log,
   scheduler: Scheduler,
   // This object needs to be thread-safe because it is used by the snapshotting thread to notify the
-  // polling thread when snapshots are created.
-  snapshotIds: ConcurrentSkipListSet[OffsetAndEpoch],
+  // polling thread when snapshots are created. Using a Map instead of a Set so that there is no
+  // need to handle NoSuchElementException.
+  snapshotIds: ConcurrentSkipListMap[OffsetAndEpoch, Unit],

Review comment:
       Why does this have to be a map?




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

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



[GitHub] [kafka] jsancio commented on a change in pull request #10344: MINOR: Remove use of NoSuchElementException

Posted by GitBox <gi...@apache.org>.
jsancio commented on a change in pull request #10344:
URL: https://github.com/apache/kafka/pull/10344#discussion_r596980629



##########
File path: core/src/main/scala/kafka/raft/KafkaMetadataLog.scala
##########
@@ -37,8 +37,9 @@ final class KafkaMetadataLog private (
   log: Log,
   scheduler: Scheduler,
   // This object needs to be thread-safe because it is used by the snapshotting thread to notify the
-  // polling thread when snapshots are created.
-  snapshotIds: ConcurrentSkipListSet[OffsetAndEpoch],
+  // polling thread when snapshots are created. Using a Map instead of a Set so that there is no
+  // need to handle NoSuchElementException.
+  snapshotIds: ConcurrentSkipListMap[OffsetAndEpoch, Unit],

Review comment:
       Sounds good @ijuma . I changed the code to use a `ConcurrentSkipListSet`, ascending iterator and descending iterator.




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

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