-
Notifications
You must be signed in to change notification settings - Fork 15.4k
KAFKA-9051: Prematurely complete source offset read requests for stopped tasks #7532
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
24ba9bd
f89b454
9bda84b
7f2f21b
746b46b
3828abf
28c156e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| /* | ||
| * 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.connect.storage; | ||
|
|
||
| import java.io.Closeable; | ||
| import java.util.Collection; | ||
| import java.util.Map; | ||
|
|
||
| public interface CloseableOffsetStorageReader extends Closeable, OffsetStorageReader { | ||
|
|
||
| /** | ||
| * Invoke {@link OffsetReadFuture#forceComplete()} on all outstanding offset read requests, and | ||
| * return an empty map and {@code null} in all future calls to {@link #offsets(Collection)} and | ||
| * {@link #offset(Map)}, respectively. This is useful for unblocking task threads which need to shut | ||
| * down but are blocked on offset reads. | ||
| */ | ||
| void close(); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -42,6 +42,7 @@ | |
| import java.util.concurrent.Future; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.concurrent.TimeoutException; | ||
| import java.util.concurrent.atomic.AtomicBoolean; | ||
|
|
||
| /** | ||
| * <p> | ||
|
|
@@ -118,17 +119,8 @@ public void stop() { | |
| } | ||
|
|
||
| @Override | ||
| public Future<Map<ByteBuffer, ByteBuffer>> get(final Collection<ByteBuffer> keys, | ||
| final Callback<Map<ByteBuffer, ByteBuffer>> callback) { | ||
| ConvertingFutureCallback<Void, Map<ByteBuffer, ByteBuffer>> future = new ConvertingFutureCallback<Void, Map<ByteBuffer, ByteBuffer>>(callback) { | ||
| @Override | ||
| public Map<ByteBuffer, ByteBuffer> convert(Void result) { | ||
| Map<ByteBuffer, ByteBuffer> values = new HashMap<>(); | ||
| for (ByteBuffer key : keys) | ||
| values.put(key, data.get(key)); | ||
| return values; | ||
| } | ||
| }; | ||
| public OffsetReadFuture get(final Collection<ByteBuffer> keys) { | ||
| KafkaOffsetReadFuture future = new KafkaOffsetReadFuture(keys); | ||
| // This operation may be relatively (but not too) expensive since it always requires checking end offsets, even | ||
| // if we've already read up to the end. However, it also should not be common (offsets should only be read when | ||
| // resetting a task). Always requiring that we read to the end is simpler than trying to differentiate when it | ||
|
|
@@ -231,5 +223,50 @@ public synchronized Void get(long timeout, TimeUnit unit) throws InterruptedExce | |
| } | ||
| } | ||
|
|
||
| private class KafkaOffsetReadFuture extends ConvertingFutureCallback<Void, Map<ByteBuffer, ByteBuffer>> implements OffsetReadFuture { | ||
| private final Collection<ByteBuffer> keys; | ||
| private final AtomicBoolean completed; | ||
| private transient boolean forceComplete; | ||
|
|
||
| public KafkaOffsetReadFuture(Collection<ByteBuffer> keys) { | ||
| super(null); | ||
| this.keys = keys; | ||
| this.completed = new AtomicBoolean(false); | ||
| this.forceComplete = false; | ||
| } | ||
|
|
||
| @Override | ||
| public Map<ByteBuffer, ByteBuffer> convert(Void result) { | ||
| Map<ByteBuffer, ByteBuffer> values = new HashMap<>(); | ||
| if (!forceComplete) { | ||
| for (ByteBuffer key : keys) | ||
| values.put(key, data.get(key)); | ||
| } | ||
| return values; | ||
| } | ||
|
|
||
| @Override | ||
| public void forceComplete() { | ||
| log.debug("Forcing completion of offset read future"); | ||
| forceComplete = true; | ||
| onCompletion(null, null); | ||
| } | ||
|
|
||
| @Override | ||
| public Map<ByteBuffer, ByteBuffer> get() throws InterruptedException, ExecutionException { | ||
| log.trace("Blocking on offset read request"); | ||
| return super.get(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this would return stale values for a key. if this future was prematurely closed, then we might still return a stale values for a key. we should be careful that this doesn't break any of the existing contracts of OffsetReader.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, this is noted in the description. However, if we want to stick strictly to the API for the |
||
| } | ||
|
|
||
| @Override | ||
| public void onCompletion(Throwable error, Void result) { | ||
| if (!completed.getAndSet(true)) { | ||
| log.trace("Offset request completed (either normally or forcibly)"); | ||
| super.onCompletion(error, result); | ||
| } else { | ||
| log.trace("Ignoring onCompletion invocation as offset read has already completed"); | ||
| } | ||
| } | ||
|
|
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| /* | ||
| * 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.connect.storage; | ||
|
|
||
|
|
||
| import java.nio.ByteBuffer; | ||
| import java.util.Map; | ||
| import java.util.concurrent.Future; | ||
|
|
||
| public interface OffsetReadFuture extends Future<Map<ByteBuffer, ByteBuffer>> { | ||
|
|
||
| /** | ||
| * Without {@link Future#cancel(boolean) cancelling} the operation, immediately return an empty | ||
| * map to any threads which are waiting for this future to complete. | ||
| */ | ||
| void forceComplete(); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should cache the fact that this future has been force stopped, and if so, any subsequent calls to get() should immediately return with either en empty map. right now, it looks like it will re-try to catch up with the remote log.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with the gist of this comment, but I think the right place for that caching behavior is in the offset reader class, not in the backing store (since each task is given its own offset reader, but the backing store is shared among all of them).