Minjae Lee created KAFKA-21616:
----------------------------------

             Summary: Connect: altered/reset source connector offsets can be 
overwritten by a task that is still being stopped on another worker
                 Key: KAFKA-21616
                 URL: https://issues.apache.org/jira/browse/KAFKA-21616
             Project: Kafka
          Issue Type: Bug
          Components: connect
    Affects Versions: 4.3.1
            Reporter: Minjae Lee


With incremental cooperative rebalancing and exactly-once source support 
disabled, a request to alter or reset the offsets of a stopped source connector 
(KIP-875) can succeed while a task of that connector is still running on a 
non-leader worker. When that task is stopped a moment later, its final offset 
commit overwrites the offsets written by the request.

h3. How to reproduce

* 1 broker, 3 distributed Connect workers (apache/kafka:4.3.1), 
connect.protocol=sessioned (default), exactly.once.source.support=disabled 
(default)
* A FileStreamSourceConnector reading a file that is continuously appended to, 
with its task running on a worker that is not the leader
* Against the leader:
** PUT /connectors/\{name\}/stop
** PATCH /connectors/\{name\}/offsets with position 0, immediately after the 
stop request returns (retry on 409)
** GET /connectors/\{name\}/offsets a few seconds later

Result on 4.3.1: the PATCH returns 200 after 40-50 ms without a single 409, a 
GET right after it returns position 0, but 8 seconds later the stored position 
is the one committed by the stopped task. This happened in 10 out of 10 runs. 
When the task is on the leader the problem does not occur, because the leader 
stops its own tasks on the herder thread before it processes the request.

h3. Cause

{{DistributedHerder#modifyConnectorOffsetsChecks}} accepts the request when no 
rebalance is pending, the target state is STOPPED and the connector has zero 
task configs. The comment there says that under these conditions "we can be 
sure that the tasks have at least been attempted to be stopped".

That does not hold for tasks owned by other workers. With incremental 
cooperative rebalancing, the rebalance that follows the stop request only 
delivers the revocation to the owning worker. The worker stops the revoked 
tasks after it has received the assignment and then rejoins the group. On the 
leader, {{needsReconfigRebalance}} is already cleared at that point and no 
rebalance is pending until the worker rejoins, so the check passes while the 
task is still running.

With exactly-once source support enabled this is covered by the round of zombie 
fencing that precedes the offset write. Without it there is nothing that keeps 
the still-running task from committing.

h3. Proposed fix

The leader already knows which tasks it asked workers to revoke in the last 
rebalance 
({{IncrementalCooperativeAssignor.ClusterAssignment#newlyRevokedTasks}}). A 
worker that was asked to revoke tasks rejoins only after it has stopped them, 
and that rejoin triggers the next rebalance, which replaces the leader state.

So the leader can keep the set of connectors that had tasks revoked in the most 
recent rebalance in {{WorkerCoordinator.LeaderState}} and reject offset 
modification requests for those source connectors with a 
{{RebalanceNeededException}} (HTTP 409) until the follow-up rebalance has 
completed. Sink connectors do not need this, because altering consumer group 
offsets already fails while a sink task's consumer is still a member of the 
group. Clients already have to retry 409 responses from this endpoint.

With a prototype of this change on 4.3.1 the same test gets 409 for about 3 
seconds (26 retries at 100 ms), then 200, and the altered offsets were still in 
place after 8 seconds in 10 out of 10 runs.

This does not need a KIP as far as I can tell: no public API or configuration 
changes, only an additional case in which an existing 409 is returned.

h3. Not covered

A task that does not stop within {{task.shutdown.graceful.timeout.ms}} is 
abandoned by its worker, which then rejoins. Such a task can still commit 
offsets later. That is a separate problem (related: KAFKA-14910, KAFKA-19633).

I would like to work on this and can open a pull request.



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to