|
38 | 38 | import java.util.concurrent.ConcurrentLinkedQueue;
|
39 | 39 | import java.util.concurrent.atomic.AtomicBoolean;
|
40 | 40 | import java.util.concurrent.atomic.AtomicInteger;
|
| 41 | +import java.util.concurrent.atomic.AtomicLong; |
41 | 42 | import java.util.stream.Collectors;
|
42 | 43 |
|
43 | 44 | /**
|
@@ -77,6 +78,8 @@ public class FilePulseSourceTask extends SourceTask {
|
77 | 78 |
|
78 | 79 | private final Map<String, Schema> valueSchemas = new HashMap<>();
|
79 | 80 |
|
| 81 | + private final AtomicLong taskThreadId = new AtomicLong(0); |
| 82 | + |
80 | 83 | /**
|
81 | 84 | * {@inheritDoc}
|
82 | 85 | */
|
@@ -120,9 +123,10 @@ public void onCompleted(final FileContext context) {
|
120 | 123 | fileURIProvider = taskConfig.getFileURIProvider();
|
121 | 124 |
|
122 | 125 | running.set(true);
|
| 126 | + taskThreadId.set(Thread.currentThread().getId()); |
123 | 127 | LOG.info("Started FilePulse source task");
|
124 | 128 | } catch (final Throwable t) {
|
125 |
| - // This task has failed, so close any resources (may be reopened if needed) before throwing |
| 129 | + // This task has failed, so close any resources (maybe reopened if needed) before throwing |
126 | 130 | closeResources();
|
127 | 131 | throw t;
|
128 | 132 | }
|
@@ -302,9 +306,22 @@ private SourceRecord buildSourceRecord(final FileContext context,
|
302 | 306 | @Override
|
303 | 307 | public void stop() {
|
304 | 308 | LOG.info("Stopping FilePulse source task");
|
| 309 | + |
| 310 | + // In earlier versions of Kafka Connect, 'SourceTask::stop()' was not called from the task thread. |
| 311 | + // In this case, resources should be closed at the end of 'SourceTask::poll()' |
| 312 | + // when no longer running or if there is an error. |
305 | 313 | running.set(false);
|
306 |
| - synchronized (this) { |
307 |
| - notify(); |
| 314 | + |
| 315 | + // Since https://issues.apache.org/jira/browse/KAFKA-10792 the SourceTask::stop() |
| 316 | + // is called from the source task's dedicated thread |
| 317 | + if (taskThreadId.longValue() == Thread.currentThread().getId()) { |
| 318 | + closeResources(); |
| 319 | + LOG.info("Stopped FilePulse source task."); |
| 320 | + } else { |
| 321 | + // For backward-compatibility with earlier versions of Kafka Connect. |
| 322 | + synchronized (this) { |
| 323 | + notify(); |
| 324 | + } |
308 | 325 | }
|
309 | 326 | }
|
310 | 327 |
|
|
0 commit comments