-
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 6 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,33 @@ | ||
| /* | ||
| * 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; | ||
| import java.util.concurrent.Future; | ||
|
|
||
| public interface CloseableOffsetStorageReader extends Closeable, OffsetStorageReader { | ||
|
|
||
| /** | ||
| * {@link Future#cancel(boolean) Cancel} all outstanding offset read requests, and throw an | ||
| * exception in all current and future calls to {@link #offsets(Collection)} and | ||
| * {@link #offset(Map)}. 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 |
|---|---|---|
|
|
@@ -26,27 +26,36 @@ | |
| import java.util.Collection; | ||
| import java.util.Collections; | ||
| import java.util.HashMap; | ||
| import java.util.HashSet; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import java.util.concurrent.CancellationException; | ||
| import java.util.concurrent.Future; | ||
| import java.util.concurrent.atomic.AtomicBoolean; | ||
|
|
||
| /** | ||
| * Implementation of OffsetStorageReader. Unlike OffsetStorageWriter which is implemented | ||
| * directly, the interface is only separate from this implementation because it needs to be | ||
| * included in the public API package. | ||
| */ | ||
| public class OffsetStorageReaderImpl implements OffsetStorageReader { | ||
| public class OffsetStorageReaderImpl implements CloseableOffsetStorageReader { | ||
| private static final Logger log = LoggerFactory.getLogger(OffsetStorageReaderImpl.class); | ||
|
|
||
| private final OffsetBackingStore backingStore; | ||
| private final String namespace; | ||
| private final Converter keyConverter; | ||
| private final Converter valueConverter; | ||
| private final AtomicBoolean closed; | ||
| private final Set<Future<Map<ByteBuffer, ByteBuffer>>> offsetReadFutures; | ||
|
|
||
| public OffsetStorageReaderImpl(OffsetBackingStore backingStore, String namespace, | ||
| Converter keyConverter, Converter valueConverter) { | ||
| this.backingStore = backingStore; | ||
| this.namespace = namespace; | ||
| this.keyConverter = keyConverter; | ||
| this.valueConverter = valueConverter; | ||
| this.closed = new AtomicBoolean(false); | ||
| this.offsetReadFutures = new HashSet<>(); | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -76,7 +85,30 @@ public <T> Map<Map<String, T>, Map<String, Object>> offsets(Collection<Map<Strin | |
| // Get serialized key -> serialized value from backing store | ||
| Map<ByteBuffer, ByteBuffer> raw; | ||
| try { | ||
| raw = backingStore.get(serializedToOriginal.keySet(), null).get(); | ||
| Future<Map<ByteBuffer, ByteBuffer>> offsetReadFuture; | ||
| synchronized (offsetReadFutures) { | ||
| if (closed.get()) { | ||
| throw new ConnectException( | ||
| "Offset reader is closed. This is likely because the task has already been " | ||
| + "scheduled to stop but has taken longer than the graceful shutdown " | ||
| + "period to do so."); | ||
| } | ||
| offsetReadFuture = backingStore.get(serializedToOriginal.keySet()); | ||
| offsetReadFutures.add(offsetReadFuture); | ||
| } | ||
|
|
||
| try { | ||
| raw = offsetReadFuture.get(); | ||
| } catch (CancellationException e) { | ||
| throw new ConnectException( | ||
| "Offset reader closed while attempting to read offsets. This is likely because " | ||
| + "the task was been scheduled to stop but has taken longer than the " | ||
| + "graceful shutdown period to do so."); | ||
| } finally { | ||
| synchronized (offsetReadFutures) { | ||
| offsetReadFutures.remove(offsetReadFuture); | ||
| } | ||
| } | ||
| } catch (Exception e) { | ||
| log.error("Failed to fetch offsets from namespace {}: ", namespace, e); | ||
| throw new ConnectException("Failed to fetch offsets.", e); | ||
|
|
@@ -108,4 +140,14 @@ public <T> Map<Map<String, T>, Map<String, Object>> offsets(Collection<Map<Strin | |
|
|
||
| return result; | ||
| } | ||
|
|
||
| public void close() { | ||
| synchronized (offsetReadFutures) { | ||
| closed.set(true); | ||
| for (Future<Map<ByteBuffer, ByteBuffer>> offsetReadFuture : offsetReadFutures) { | ||
| offsetReadFuture.cancel(true); | ||
|
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. It is now possible with your changes for
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. Ack, will address. |
||
| } | ||
| offsetReadFutures.clear(); | ||
| } | ||
| } | ||
| } | ||
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 first check to see if this is closed, and if so simply return.
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.
Ack, will address.