You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jira@kafka.apache.org by "showuon (via GitHub)" <gi...@apache.org> on 2023/03/27 09:16:58 UTC
[GitHub] [kafka] showuon opened a new pull request, #13456: KAFKA-14850: introduce InMemoryLeaderEpochCheckpoint
showuon opened a new pull request, #13456:
URL: https://github.com/apache/kafka/pull/13456
Introduce `InMemoryLeaderEpochCheckpoint` to allow quick read/write
### 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
[GitHub] [kafka] divijvaidya commented on a diff in pull request #13456: KAFKA-14850: introduce InMemoryLeaderEpochCheckpoint
Posted by "divijvaidya (via GitHub)" <gi...@apache.org>.
divijvaidya commented on code in PR #13456:
URL: https://github.com/apache/kafka/pull/13456#discussion_r1154306477
##########
storage/src/main/java/org/apache/kafka/storage/internals/checkpoint/InMemoryLeaderEpochCheckpoint.java:
##########
@@ -0,0 +1,58 @@
+/*
+ * 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.kafka.storage.internals.checkpoint;
+
+import org.apache.kafka.server.common.CheckpointFile;
+import org.apache.kafka.storage.internals.log.EpochEntry;
+
+import java.io.BufferedWriter;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.OutputStreamWriter;
+import java.nio.ByteBuffer;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+/**
+ * This class stores a list of EpochEntry(LeaderEpoch + Offsets) to memory
+ */
+public class InMemoryLeaderEpochCheckpoint implements LeaderEpochCheckpoint {
Review Comment:
Thanks for the explanation @showuon. It's clear now.
To rephrase what you mentioned (correct me if I misunderstood) we want to create an in-memory copy of the `LeaderEpochCheckpointCache` so that we can modify it without modifying the original checkpointcache & checkpoint. Is that right?
At the end of the day, RLSM requires `Map<Int, Long> segmentLeaderEpochs()` and the source is `LeaderEpochCheckpointCache`. I was wondering if it would be possible to copy the `LeaderEpochCheckpointCache` into an intermediate data structure (which doesn't have to be of type LeaderEpochCheckpointCache, hence decoupling it from the need to create a dummy `InMemoryLeaderEpochCheckpoint `) and then using that intermediate data structure to extract the required Map (after whatever manipulation we want to do with it).
Similar logic could be employed for the requirement to provide `ByteBuffer leaderEpochIndex` to `LogSegmentData`.
Thoughts?
Having said that I don't want to block this PR on this refactoring discussion. It can be done separately after this is merged. In the scope of this PR, if you add a comment to InMemoryLeaderEpochCheckpoint which could explain future reader of the code about the purpose of this class (the way you described in the comment above), that would be enough to make me happy.
--
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
[GitHub] [kafka] divijvaidya commented on a diff in pull request #13456: KAFKA-14850: introduce InMemoryLeaderEpochCheckpoint
Posted by "divijvaidya (via GitHub)" <gi...@apache.org>.
divijvaidya commented on code in PR #13456:
URL: https://github.com/apache/kafka/pull/13456#discussion_r1151705356
##########
storage/src/main/java/org/apache/kafka/storage/internals/epoch/LeaderEpochFileCache.java:
##########
@@ -383,11 +396,11 @@ public void clear() {
// Visible for testing
public List<EpochEntry> epochEntries() {
- lock.writeLock().lock();
+ lock.readLock().lock();
Review Comment:
Nice!
Could we move this to a separate PR please since I think this is an obvious change that can be shipped earlier than this PR.
##########
storage/src/main/java/org/apache/kafka/storage/internals/epoch/LeaderEpochFileCache.java:
##########
@@ -148,6 +151,16 @@ private List<EpochEntry> removeWhileMatching(Iterator<Map.Entry<Integer, EpochEn
return removedEpochs;
}
+ public LeaderEpochFileCache writeTo(LeaderEpochCheckpoint leaderEpochCheckpoint) {
+ lock.writeLock().lock();
Review Comment:
You don't need write locks here since you are flushing to an entirely new checkpoint. You need read lock on existing cache. (similar to flush() method)
##########
storage/src/main/java/org/apache/kafka/storage/internals/epoch/LeaderEpochFileCache.java:
##########
@@ -148,6 +151,16 @@ private List<EpochEntry> removeWhileMatching(Iterator<Map.Entry<Integer, EpochEn
return removedEpochs;
}
+ public LeaderEpochFileCache writeTo(LeaderEpochCheckpoint leaderEpochCheckpoint) {
+ lock.writeLock().lock();
+ try {
+ leaderEpochCheckpoint.write(epochEntries());
Review Comment:
It seems like we are doing the following:
1. Flush the cache values to the checkpoint.
2. Create a new file cache which would read from the checkpoint created in step 1
which means, we are probably trying to have a copy of this cache backed by this new checkpoint here.
Alternatively, may I suggest the following:
```
public LeaderEpochFileCache cloneWithLeaderEpochCheckpoint(LeaderEpochCheckpoint leaderEpochCheckpoint) {
this.flushToCheckpoint(leaderEpochCheckpoint);
return new LeaderEpochFileCache(this.topicPartition, leaderEpochCheckpoint);
}
```
`flushToCheckpoint()` is similar to existing flush() method.
##########
storage/src/main/java/org/apache/kafka/storage/internals/checkpoint/InMemoryLeaderEpochCheckpoint.java:
##########
@@ -0,0 +1,58 @@
+/*
+ * 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.kafka.storage.internals.checkpoint;
+
+import org.apache.kafka.server.common.CheckpointFile;
+import org.apache.kafka.storage.internals.log.EpochEntry;
+
+import java.io.BufferedWriter;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.OutputStreamWriter;
+import java.nio.ByteBuffer;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+/**
+ * This class stores a list of EpochEntry(LeaderEpoch + Offsets) to memory
+ */
+public class InMemoryLeaderEpochCheckpoint implements LeaderEpochCheckpoint {
Review Comment:
Please help me clarify my understanding here. I don't understand the motivation here.
File based checkpoints are used for faster recovery so that when a restart occurs, we are don't lose the in-memory state and can recover from a checkpoint. The concept of in-memory checkpoint contradicts the entire purpose of having a checkpoint logic! If we are keeping this in-memory, then why even bother checkpointing at all?
##########
core/src/main/scala/kafka/log/remote/RemoteLogManager.scala:
##########
@@ -219,6 +221,29 @@ class RemoteLogManager(rlmConfig: RemoteLogManagerConfig,
}
}
+ /**
+ * Returns the in memory leader epoch checkpoint by truncating with the given start[exclusive] and end[inclusive] offset
+ *
+ * @param log The actual log from where to take the leader-epoch checkpoint
+ * @param startOffset The start offset of the checkpoint file (exclusive in the truncation).
+ * If start offset is 6, then it will retain an entry at offset 6.
+ * @param endOffset The end offset of the checkpoint file (inclusive in the truncation)
+ * If end offset is 100, then it will remove the entries greater than or equal to 100.
+ * @return the truncated leader epoch checkpoint
+ */
+ private[remote] def getLeaderEpochCheckpoint(log: UnifiedLog, startOffset: Long, endOffset: Long): InMemoryLeaderEpochCheckpoint = {
Review Comment:
where we going to use this? The motivation for this PR is unclear 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.
To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [kafka] showuon commented on a diff in pull request #13456: KAFKA-14850: introduce InMemoryLeaderEpochCheckpoint
Posted by "showuon (via GitHub)" <gi...@apache.org>.
showuon commented on code in PR #13456:
URL: https://github.com/apache/kafka/pull/13456#discussion_r1155878999
##########
storage/src/main/java/org/apache/kafka/storage/internals/checkpoint/InMemoryLeaderEpochCheckpoint.java:
##########
@@ -0,0 +1,58 @@
+/*
+ * 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.kafka.storage.internals.checkpoint;
+
+import org.apache.kafka.server.common.CheckpointFile;
+import org.apache.kafka.storage.internals.log.EpochEntry;
+
+import java.io.BufferedWriter;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.OutputStreamWriter;
+import java.nio.ByteBuffer;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+/**
+ * This class stores a list of EpochEntry(LeaderEpoch + Offsets) to memory
+ */
+public class InMemoryLeaderEpochCheckpoint implements LeaderEpochCheckpoint {
Review Comment:
> I was wondering if it would be possible to copy the LeaderEpochCheckpointCache into an intermediate data structure (which doesn't have to be of type LeaderEpochCheckpointCache, hence decoupling it from the need to create a dummy InMemoryLeaderEpochCheckpoint ) and then using that intermediate data structure to extract the required Map (after whatever manipulation we want to do with it).
That's a good idea. https://issues.apache.org/jira/browse/KAFKA-14877 is created for this improvement.
--
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
[GitHub] [kafka] divijvaidya commented on a diff in pull request #13456: KAFKA-14850: introduce InMemoryLeaderEpochCheckpoint
Posted by "divijvaidya (via GitHub)" <gi...@apache.org>.
divijvaidya commented on code in PR #13456:
URL: https://github.com/apache/kafka/pull/13456#discussion_r1154259339
##########
core/src/main/scala/kafka/log/remote/RemoteLogManager.scala:
##########
@@ -25,6 +26,7 @@ import org.apache.kafka.common.record.{RecordBatch, RemoteLogInputStream}
import org.apache.kafka.common.utils.{ChildFirstClassLoader, Utils}
import org.apache.kafka.server.log.remote.metadata.storage.ClassLoaderAwareRemoteLogMetadataManager
import org.apache.kafka.server.log.remote.storage._
+import org.apache.kafka.storage.internals.checkpoint.InMemoryLeaderEpochCheckpoint
Review Comment:
Is this still required here?
##########
storage/src/main/java/org/apache/kafka/storage/internals/epoch/LeaderEpochFileCache.java:
##########
@@ -383,21 +398,25 @@ public void clear() {
// Visible for testing
public List<EpochEntry> epochEntries() {
- lock.writeLock().lock();
+ lock.readLock().lock();
try {
return new ArrayList<>(epochs.values());
} finally {
- lock.writeLock().unlock();
+ lock.readLock().unlock();
}
}
- private void flush() {
+ private void flushTo(LeaderEpochCheckpoint leaderEpochCheckpoint, Collection<EpochEntry> epochEntries) {
lock.readLock().lock();
try {
- checkpoint.write(epochs.values());
+ leaderEpochCheckpoint.write(epochEntries);
} finally {
lock.readLock().unlock();
}
}
+ private void flush() {
+ flushTo(this.checkpoint, epochs.values());
Review Comment:
both invocations for `flushTo()` have the same second parameter.
Could we instead read the entries inside the flushTo method itself? Note that we don't require to acquire locks in flushTo() or in cloneWithLeaderEpochCheckpoint(), since epochEntries() does it for us.
```
private void flushTo(LeaderEpochCheckpoint leaderEpochCheckpoint) {
leaderEpochCheckpoint.write(epochEntries());
}
private void flush() {
flushTo(this.checkpoint);
}
public LeaderEpochFileCache cloneWithLeaderEpochCheckpoint(LeaderEpochCheckpoint leaderEpochCheckpoint) {
flushTo(leaderEpochCheckpoint);
// create a new cache backed by the provided leaderEpochCheckpoint
return new LeaderEpochFileCache(this.topicPartition, leaderEpochCheckpoint);
}
```
##########
storage/src/main/java/org/apache/kafka/storage/internals/checkpoint/InMemoryLeaderEpochCheckpoint.java:
##########
@@ -0,0 +1,58 @@
+/*
+ * 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.kafka.storage.internals.checkpoint;
+
+import org.apache.kafka.server.common.CheckpointFile;
+import org.apache.kafka.storage.internals.log.EpochEntry;
+
+import java.io.BufferedWriter;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.OutputStreamWriter;
+import java.nio.ByteBuffer;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+/**
+ * This class stores a list of EpochEntry(LeaderEpoch + Offsets) to memory
+ */
+public class InMemoryLeaderEpochCheckpoint implements LeaderEpochCheckpoint {
Review Comment:
Thanks for the explanation @showuon. It's clear now.
To rephrase what you mentioned (correct me if I misunderstood) we want to create an in-memory copy of the `LeaderEpochCheckpointCache` so that we can modify it without modifying the original checkpointcache & checkpoint. Is that right?
At the end of the day, RLSM requires `Map<Int, Long> segmentLeaderEpochs()` and the source is `LeaderEpochCheckpointCache`. I was wondering if it would be possible to copy the `LeaderEpochCheckpointCache` into an intermediate data structure (which doesn't have to be of type LeaderEpochCheckpointCache, hence decoupling it from the need to create a dummy `InMemoryLeaderEpochCheckpoint `) and then using that intermediate data structure to extract the required Map (after whatever manipulation we want to do with it).
Similar logic could be employed for the requirement to provide `ByteBuffer leaderEpochIndex` to `LogSegmentData`.
Thoughts?
--
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
[GitHub] [kafka] divijvaidya commented on a diff in pull request #13456: KAFKA-14850: introduce InMemoryLeaderEpochCheckpoint
Posted by "divijvaidya (via GitHub)" <gi...@apache.org>.
divijvaidya commented on code in PR #13456:
URL: https://github.com/apache/kafka/pull/13456#discussion_r1155911890
##########
storage/src/main/java/org/apache/kafka/storage/internals/epoch/LeaderEpochFileCache.java:
##########
@@ -147,6 +152,16 @@ private List<EpochEntry> removeWhileMatching(Iterator<Map.Entry<Integer, EpochEn
return removedEpochs;
}
+ public LeaderEpochFileCache cloneWithLeaderEpochCheckpoint(LeaderEpochCheckpoint leaderEpochCheckpoint) {
+ lock.readLock().lock();
Review Comment:
do we need a read lock here?
`epochEntries()` already takes one when required to read the mutable state (`epochs`) that we are trying to protect here.
--
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
[GitHub] [kafka] showuon commented on a diff in pull request #13456: KAFKA-14850: introduce InMemoryLeaderEpochCheckpoint
Posted by "showuon (via GitHub)" <gi...@apache.org>.
showuon commented on code in PR #13456:
URL: https://github.com/apache/kafka/pull/13456#discussion_r1154172866
##########
storage/src/main/java/org/apache/kafka/storage/internals/checkpoint/InMemoryLeaderEpochCheckpoint.java:
##########
@@ -0,0 +1,58 @@
+/*
+ * 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.kafka.storage.internals.checkpoint;
+
+import org.apache.kafka.server.common.CheckpointFile;
+import org.apache.kafka.storage.internals.log.EpochEntry;
+
+import java.io.BufferedWriter;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.OutputStreamWriter;
+import java.nio.ByteBuffer;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+/**
+ * This class stores a list of EpochEntry(LeaderEpoch + Offsets) to memory
+ */
+public class InMemoryLeaderEpochCheckpoint implements LeaderEpochCheckpoint {
Review Comment:
@divijvaidya, Sorry for not being clear and waste your time to try understanding this PR. My bad!
The motivation for introducing `InMemoryLeaderEpochCheckpoint` is to allow remote log manager to create the `RemoteLogSegmentMetadata`(RLSM) with the correct leader epoch info for a specific segment. To do that, we need to rely on the `LeaderEpochCheckpointCache` to truncate from start and end, to get the epoch info. However, we don't really want to truncate the epochs in cache (and write to checkpoint file in the end). So, we introduce this `InMemoryLeaderEpochCheckpoint` to feed into `LeaderEpochCheckpointCache`, and when we truncate the epoch for RLSM, we can do them in memory without affecting the checkpoint file, and without interacting with file system.
I'll update in the PR description, and remove the methods that not used currently.
--
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
[GitHub] [kafka] showuon merged pull request #13456: KAFKA-14850: introduce InMemoryLeaderEpochCheckpoint
Posted by "showuon (via GitHub)" <gi...@apache.org>.
showuon merged PR #13456:
URL: https://github.com/apache/kafka/pull/13456
--
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
[GitHub] [kafka] showuon commented on a diff in pull request #13456: KAFKA-14850: introduce InMemoryLeaderEpochCheckpoint
Posted by "showuon (via GitHub)" <gi...@apache.org>.
showuon commented on code in PR #13456:
URL: https://github.com/apache/kafka/pull/13456#discussion_r1154193013
##########
storage/src/main/java/org/apache/kafka/storage/internals/epoch/LeaderEpochFileCache.java:
##########
@@ -383,11 +396,11 @@ public void clear() {
// Visible for testing
public List<EpochEntry> epochEntries() {
- lock.writeLock().lock();
+ lock.readLock().lock();
Review Comment:
Opened another PR https://github.com/apache/kafka/pull/13483 to fix this bug.
--
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
[GitHub] [kafka] showuon commented on a diff in pull request #13456: KAFKA-14850: introduce InMemoryLeaderEpochCheckpoint
Posted by "showuon (via GitHub)" <gi...@apache.org>.
showuon commented on code in PR #13456:
URL: https://github.com/apache/kafka/pull/13456#discussion_r1154172866
##########
storage/src/main/java/org/apache/kafka/storage/internals/checkpoint/InMemoryLeaderEpochCheckpoint.java:
##########
@@ -0,0 +1,58 @@
+/*
+ * 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.kafka.storage.internals.checkpoint;
+
+import org.apache.kafka.server.common.CheckpointFile;
+import org.apache.kafka.storage.internals.log.EpochEntry;
+
+import java.io.BufferedWriter;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.OutputStreamWriter;
+import java.nio.ByteBuffer;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+/**
+ * This class stores a list of EpochEntry(LeaderEpoch + Offsets) to memory
+ */
+public class InMemoryLeaderEpochCheckpoint implements LeaderEpochCheckpoint {
Review Comment:
@divijvaidya, Sorry for not being clear and waste your time to try understanding this PR. My bad!
The motivation for introducing `InMemoryLeaderEpochCheckpoint` is to allow remote log manager to create the `RemoteLogSegmentMetadata`(RLSM) with the correct leader epoch info for a specific segment. To do that, we need to rely on the `LeaderEpochCheckpointCache` to truncate from start and end, to get the epoch info. However, we don't really want to truncate the epochs in cache (and write to checkpoint file in the end). So, we introduce this `InMemoryLeaderEpochCheckpoint` to feed into `LeaderEpochCheckpointCache`, and when we truncate the epoch for RLSM, we can do them in memory without affecting the checkpoint file, and without interacting with file system. Does that make sense?
I'll update in the PR description, and remove the methods that not used currently.
--
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
[GitHub] [kafka] showuon commented on a diff in pull request #13456: KAFKA-14850: introduce InMemoryLeaderEpochCheckpoint
Posted by "showuon (via GitHub)" <gi...@apache.org>.
showuon commented on code in PR #13456:
URL: https://github.com/apache/kafka/pull/13456#discussion_r1158148779
##########
storage/src/main/java/org/apache/kafka/storage/internals/epoch/LeaderEpochFileCache.java:
##########
@@ -147,6 +152,16 @@ private List<EpochEntry> removeWhileMatching(Iterator<Map.Entry<Integer, EpochEn
return removedEpochs;
}
+ public LeaderEpochFileCache cloneWithLeaderEpochCheckpoint(LeaderEpochCheckpoint leaderEpochCheckpoint) {
+ lock.readLock().lock();
+ try {
+ flushTo(leaderEpochCheckpoint, epochEntries());
Review Comment:
Yes, since we'll write with a copy of the entries, it'll be safe to use the `epochs.values()` directly now.
--
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
[GitHub] [kafka] showuon commented on pull request #13456: KAFKA-14850: introduce InMemoryLeaderEpochCheckpoint
Posted by "showuon (via GitHub)" <gi...@apache.org>.
showuon commented on PR #13456:
URL: https://github.com/apache/kafka/pull/13456#issuecomment-1497382648
Failed tests are unrelated:
```
Build / JDK 17 and Scala 2.13 / kafka.security.authorizer.AuthorizerTest.testAuthorizeWithPrefixedResource(String).quorum=kraft
Build / JDK 17 and Scala 2.13 / kafka.security.authorizer.AuthorizerTest.testWildCardAcls(String).quorum=kraft
```
--
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
[GitHub] [kafka] showuon commented on a diff in pull request #13456: KAFKA-14850: introduce InMemoryLeaderEpochCheckpoint
Posted by "showuon (via GitHub)" <gi...@apache.org>.
showuon commented on code in PR #13456:
URL: https://github.com/apache/kafka/pull/13456#discussion_r1154310401
##########
storage/src/main/java/org/apache/kafka/storage/internals/epoch/LeaderEpochFileCache.java:
##########
@@ -383,21 +398,25 @@ public void clear() {
// Visible for testing
public List<EpochEntry> epochEntries() {
- lock.writeLock().lock();
+ lock.readLock().lock();
try {
return new ArrayList<>(epochs.values());
} finally {
- lock.writeLock().unlock();
+ lock.readLock().unlock();
}
}
- private void flush() {
+ private void flushTo(LeaderEpochCheckpoint leaderEpochCheckpoint, Collection<EpochEntry> epochEntries) {
lock.readLock().lock();
try {
- checkpoint.write(epochs.values());
+ leaderEpochCheckpoint.write(epochEntries);
} finally {
lock.readLock().unlock();
}
}
+ private void flush() {
+ flushTo(this.checkpoint, epochs.values());
Review Comment:
No, the 2nd parameter is different. For `in memory` one, we need to "clone" the epochs values to avoid change the inner entries.
--
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
[GitHub] [kafka] showuon commented on a diff in pull request #13456: KAFKA-14850: introduce InMemoryLeaderEpochCheckpoint
Posted by "showuon (via GitHub)" <gi...@apache.org>.
showuon commented on code in PR #13456:
URL: https://github.com/apache/kafka/pull/13456#discussion_r1155876324
##########
storage/src/main/java/org/apache/kafka/storage/internals/epoch/LeaderEpochFileCache.java:
##########
@@ -383,21 +398,25 @@ public void clear() {
// Visible for testing
public List<EpochEntry> epochEntries() {
- lock.writeLock().lock();
+ lock.readLock().lock();
try {
return new ArrayList<>(epochs.values());
} finally {
- lock.writeLock().unlock();
+ lock.readLock().unlock();
}
}
- private void flush() {
+ private void flushTo(LeaderEpochCheckpoint leaderEpochCheckpoint, Collection<EpochEntry> epochEntries) {
lock.readLock().lock();
try {
- checkpoint.write(epochs.values());
+ leaderEpochCheckpoint.write(epochEntries);
} finally {
lock.readLock().unlock();
}
}
+ private void flush() {
+ flushTo(this.checkpoint, epochs.values());
Review Comment:
For the "removing" lock for `flushTo`, good point, removed.
--
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
[GitHub] [kafka] showuon commented on a diff in pull request #13456: KAFKA-14850: introduce InMemoryLeaderEpochCheckpoint
Posted by "showuon (via GitHub)" <gi...@apache.org>.
showuon commented on code in PR #13456:
URL: https://github.com/apache/kafka/pull/13456#discussion_r1154178753
##########
core/src/main/scala/kafka/log/remote/RemoteLogManager.scala:
##########
@@ -219,6 +221,29 @@ class RemoteLogManager(rlmConfig: RemoteLogManagerConfig,
}
}
+ /**
+ * Returns the in memory leader epoch checkpoint by truncating with the given start[exclusive] and end[inclusive] offset
+ *
+ * @param log The actual log from where to take the leader-epoch checkpoint
+ * @param startOffset The start offset of the checkpoint file (exclusive in the truncation).
+ * If start offset is 6, then it will retain an entry at offset 6.
+ * @param endOffset The end offset of the checkpoint file (inclusive in the truncation)
+ * If end offset is 100, then it will remove the entries greater than or equal to 100.
+ * @return the truncated leader epoch checkpoint
+ */
+ private[remote] def getLeaderEpochCheckpoint(log: UnifiedLog, startOffset: Long, endOffset: Long): InMemoryLeaderEpochCheckpoint = {
Review Comment:
Will remove it to avoid confustion.
##########
core/src/main/scala/kafka/log/remote/RemoteLogManager.scala:
##########
@@ -219,6 +221,29 @@ class RemoteLogManager(rlmConfig: RemoteLogManagerConfig,
}
}
+ /**
+ * Returns the in memory leader epoch checkpoint by truncating with the given start[exclusive] and end[inclusive] offset
+ *
+ * @param log The actual log from where to take the leader-epoch checkpoint
+ * @param startOffset The start offset of the checkpoint file (exclusive in the truncation).
+ * If start offset is 6, then it will retain an entry at offset 6.
+ * @param endOffset The end offset of the checkpoint file (inclusive in the truncation)
+ * If end offset is 100, then it will remove the entries greater than or equal to 100.
+ * @return the truncated leader epoch checkpoint
+ */
+ private[remote] def getLeaderEpochCheckpoint(log: UnifiedLog, startOffset: Long, endOffset: Long): InMemoryLeaderEpochCheckpoint = {
Review Comment:
Will remove it to avoid confusion.
--
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
[GitHub] [kafka] showuon commented on a diff in pull request #13456: KAFKA-14850: introduce InMemoryLeaderEpochCheckpoint
Posted by "showuon (via GitHub)" <gi...@apache.org>.
showuon commented on code in PR #13456:
URL: https://github.com/apache/kafka/pull/13456#discussion_r1154178160
##########
storage/src/main/java/org/apache/kafka/storage/internals/epoch/LeaderEpochFileCache.java:
##########
@@ -148,6 +151,16 @@ private List<EpochEntry> removeWhileMatching(Iterator<Map.Entry<Integer, EpochEn
return removedEpochs;
}
+ public LeaderEpochFileCache writeTo(LeaderEpochCheckpoint leaderEpochCheckpoint) {
+ lock.writeLock().lock();
+ try {
+ leaderEpochCheckpoint.write(epochEntries());
Review Comment:
Good suggestion!
##########
storage/src/main/java/org/apache/kafka/storage/internals/epoch/LeaderEpochFileCache.java:
##########
@@ -148,6 +151,16 @@ private List<EpochEntry> removeWhileMatching(Iterator<Map.Entry<Integer, EpochEn
return removedEpochs;
}
+ public LeaderEpochFileCache writeTo(LeaderEpochCheckpoint leaderEpochCheckpoint) {
+ lock.writeLock().lock();
Review Comment:
Nice catch!
--
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
[GitHub] [kafka] showuon commented on a diff in pull request #13456: KAFKA-14850: introduce InMemoryLeaderEpochCheckpoint
Posted by "showuon (via GitHub)" <gi...@apache.org>.
showuon commented on code in PR #13456:
URL: https://github.com/apache/kafka/pull/13456#discussion_r1158149051
##########
storage/src/main/java/org/apache/kafka/storage/internals/epoch/LeaderEpochFileCache.java:
##########
@@ -147,6 +152,16 @@ private List<EpochEntry> removeWhileMatching(Iterator<Map.Entry<Integer, EpochEn
return removedEpochs;
}
+ public LeaderEpochFileCache cloneWithLeaderEpochCheckpoint(LeaderEpochCheckpoint leaderEpochCheckpoint) {
+ lock.readLock().lock();
Review Comment:
Good point. Removed 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.
To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [kafka] satishd commented on a diff in pull request #13456: KAFKA-14850: introduce InMemoryLeaderEpochCheckpoint
Posted by "satishd (via GitHub)" <gi...@apache.org>.
satishd commented on code in PR #13456:
URL: https://github.com/apache/kafka/pull/13456#discussion_r1156800195
##########
storage/src/main/java/org/apache/kafka/storage/internals/checkpoint/InMemoryLeaderEpochCheckpoint.java:
##########
@@ -0,0 +1,64 @@
+/*
+ * 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.kafka.storage.internals.checkpoint;
+
+import org.apache.kafka.server.common.CheckpointFile;
+import org.apache.kafka.storage.internals.log.EpochEntry;
+
+import java.io.BufferedWriter;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.OutputStreamWriter;
+import java.nio.ByteBuffer;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+/**
+ * This class stores a list of EpochEntry(LeaderEpoch + Offsets) to memory
+ *
+ * The motivation for this class is to allow remote log manager to create the RemoteLogSegmentMetadata(RLSM)
+ * with the correct leader epoch info for a specific segment. To do that, we need to rely on the LeaderEpochCheckpointCache
+ * to truncate from start and end, to get the epoch info. However, we don't really want to truncate the epochs in cache
+ * (and write to checkpoint file in the end). So, we introduce this InMemoryLeaderEpochCheckpoint to feed into LeaderEpochCheckpointCache,
+ * and when we truncate the epoch for RLSM, we can do them in memory without affecting the checkpoint file, and without interacting with file system.
+ */
+public class InMemoryLeaderEpochCheckpoint implements LeaderEpochCheckpoint {
+ private List<EpochEntry> epochs = new ArrayList<>();
Review Comment:
`epochs` can be initialized as an empty List like `Collections.emptyList()`.
##########
storage/src/main/java/org/apache/kafka/storage/internals/checkpoint/InMemoryLeaderEpochCheckpoint.java:
##########
@@ -0,0 +1,64 @@
+/*
+ * 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.kafka.storage.internals.checkpoint;
+
+import org.apache.kafka.server.common.CheckpointFile;
+import org.apache.kafka.storage.internals.log.EpochEntry;
+
+import java.io.BufferedWriter;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.OutputStreamWriter;
+import java.nio.ByteBuffer;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+/**
+ * This class stores a list of EpochEntry(LeaderEpoch + Offsets) to memory
+ *
+ * The motivation for this class is to allow remote log manager to create the RemoteLogSegmentMetadata(RLSM)
+ * with the correct leader epoch info for a specific segment. To do that, we need to rely on the LeaderEpochCheckpointCache
+ * to truncate from start and end, to get the epoch info. However, we don't really want to truncate the epochs in cache
+ * (and write to checkpoint file in the end). So, we introduce this InMemoryLeaderEpochCheckpoint to feed into LeaderEpochCheckpointCache,
+ * and when we truncate the epoch for RLSM, we can do them in memory without affecting the checkpoint file, and without interacting with file system.
+ */
+public class InMemoryLeaderEpochCheckpoint implements LeaderEpochCheckpoint {
+ private List<EpochEntry> epochs = new ArrayList<>();
+
+ public void write(Collection<EpochEntry> epochs) {
+ this.epochs.addAll(epochs);
+ }
+
+ public List<EpochEntry> read() {
+ return epochs;
Review Comment:
Return an unmodifiable List like below so that the internal list is unmodifiable from out side of this class.
```
Collections.unmodifiableList(epochs)
```
##########
storage/src/main/java/org/apache/kafka/storage/internals/checkpoint/InMemoryLeaderEpochCheckpoint.java:
##########
@@ -0,0 +1,64 @@
+/*
+ * 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.kafka.storage.internals.checkpoint;
+
+import org.apache.kafka.server.common.CheckpointFile;
+import org.apache.kafka.storage.internals.log.EpochEntry;
+
+import java.io.BufferedWriter;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.OutputStreamWriter;
+import java.nio.ByteBuffer;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+/**
+ * This class stores a list of EpochEntry(LeaderEpoch + Offsets) to memory
+ *
+ * The motivation for this class is to allow remote log manager to create the RemoteLogSegmentMetadata(RLSM)
+ * with the correct leader epoch info for a specific segment. To do that, we need to rely on the LeaderEpochCheckpointCache
+ * to truncate from start and end, to get the epoch info. However, we don't really want to truncate the epochs in cache
+ * (and write to checkpoint file in the end). So, we introduce this InMemoryLeaderEpochCheckpoint to feed into LeaderEpochCheckpointCache,
+ * and when we truncate the epoch for RLSM, we can do them in memory without affecting the checkpoint file, and without interacting with file system.
+ */
+public class InMemoryLeaderEpochCheckpoint implements LeaderEpochCheckpoint {
+ private List<EpochEntry> epochs = new ArrayList<>();
+
+ public void write(Collection<EpochEntry> epochs) {
+ this.epochs.addAll(epochs);
Review Comment:
A new ArrayList with the intended collection can be created here and store like below.
```
this.epochs = new ArrayList<EpochEntry>(epochs)
```
##########
storage/src/main/java/org/apache/kafka/storage/internals/epoch/LeaderEpochFileCache.java:
##########
@@ -147,6 +152,16 @@ private List<EpochEntry> removeWhileMatching(Iterator<Map.Entry<Integer, EpochEn
return removedEpochs;
}
+ public LeaderEpochFileCache cloneWithLeaderEpochCheckpoint(LeaderEpochCheckpoint leaderEpochCheckpoint) {
+ lock.readLock().lock();
+ try {
+ flushTo(leaderEpochCheckpoint, epochEntries());
Review Comment:
`epochs.values()` can be used instead of calling `epochEnrties()` which creates a new ArrayList that is not needed here.
--
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
[GitHub] [kafka] satishd commented on pull request #13456: KAFKA-14850: introduce InMemoryLeaderEpochCheckpoint
Posted by "satishd (via GitHub)" <gi...@apache.org>.
satishd commented on PR #13456:
URL: https://github.com/apache/kafka/pull/13456#issuecomment-1493609793
Thanks @showuon for the updated PR, please resolve the conflicts.
--
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
[GitHub] [kafka] ijuma commented on a diff in pull request #13456: KAFKA-14850: introduce InMemoryLeaderEpochCheckpoint
Posted by "ijuma (via GitHub)" <gi...@apache.org>.
ijuma commented on code in PR #13456:
URL: https://github.com/apache/kafka/pull/13456#discussion_r1163074411
##########
core/src/test/scala/unit/kafka/server/checkpoints/InMemoryLeaderEpochCheckpointTest.scala:
##########
@@ -0,0 +1,58 @@
+/**
+ * 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.server.checkpoints
+
+import org.apache.kafka.storage.internals.checkpoint.InMemoryLeaderEpochCheckpoint
+import org.apache.kafka.storage.internals.log.EpochEntry
+import org.junit.jupiter.api.Assertions.assertEquals
+import org.junit.jupiter.api.Test
+
+import java.io.{BufferedReader, ByteArrayInputStream, InputStreamReader}
+import java.nio.charset.StandardCharsets
+
+class InMemoryLeaderEpochCheckpointTest {
Review Comment:
Why did we add this test in `core`? It should be in `storage`, right?
--
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