mwesterby commented on code in PR #23639:
URL: https://github.com/apache/kafka/pull/23639#discussion_r4525003772


##########
core/src/main/scala/kafka/zk/KafkaZkClient.scala:
##########
@@ -1764,11 +1764,46 @@ class KafkaZkClient private[zk] (
     initialState.withMigrationZkVersion(0)
   }
 
+  /**
+   * When there is a ConnectionLossException while updating the /migration 
znode, the update may have actually succeeded
+   * on the server before the connection dropped, so the retried request can 
fail with BadVersionException even though
+   * our own write went through. This checker reads back the current data and, 
if its migration-relevant fields match
+   * what we intended to write, treats it as our own write having succeeded, 
returning the real current version.
+   */
+  private def checkMigrationStateZkData(
+                                         zkClient: KafkaZkClient,
+                                         path: String,
+                                         expectedData: Array[Byte]
+                                       ): (Boolean, Int) = {
+    try {
+      val (writtenDataOpt, writtenStat) = zkClient.getDataAndStat(path)
+      val expected = MigrationZNode.decode(expectedData, 
writtenStat.getVersion, -1)
+      val succeeded = writtenDataOpt.exists { writtenData =>
+        val written = MigrationZNode.decode(writtenData, 
writtenStat.getVersion, -1)
+        expected.kraftControllerId() == written.kraftControllerId() &&
+          expected.kraftControllerEpoch() == written.kraftControllerEpoch() &&
+          expected.kraftMetadataOffset() == written.kraftMetadataOffset() &&
+          expected.kraftMetadataEpoch() == written.kraftMetadataEpoch()
+      }
+      if (succeeded) (true, writtenStat.getVersion) else (false, 
ZkVersion.UnknownVersion)
+    } catch {
+      case _: Exception => (false, ZkVersion.UnknownVersion)

Review Comment:
    Actually, looking at this a bit more, I'm not sure propagating is the right 
thing to do here. If `getDataAndStat` fails, using the previous logic where we 
return a `MigrationClientException` or a `MigrationClientAuthException` would 
only log the error with ("Will retry"). Nothing actually retries the event. 
It's only when another `MetadataChangeEvent` comes along at some point in the 
future does this code path get executed again.
   
   This also leaves the driver's cached `migrationZkVersion` unchanged. If the 
original write did land, ZK is one version ahead. The next 
`MetadataChangeEvent` then builds its state from the stale version plus a newer 
offset, meaning it will hit a `BADVERSION`, and so does `updateMigrationState`. 
At that point `getDataAndStat` sees an older offset than the one being written, 
so the check fails and we throw `MigrationConflictException`. So we still end 
up renouncing, but later at the next metadata change, and it's reported as a 
conflict for what was our own write.
   
   Instead of swallowing the failure into false, we could catch it and throw a 
`MigrationConflictException` with a message like "could not verify whether the 
write was applied" and the original exception as the cause. So we still 
renounce (as we can't trust the local version if we can't verify the write) but 
make it clear that was the cause.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to