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


##########
core/src/main/scala/kafka/zk/KafkaZkClient.scala:
##########
@@ -1764,11 +1764,53 @@ class KafkaZkClient private[zk] (
     initialState.withMigrationZkVersion(0)
   }
 
+  /**
+   * Reads back the current data of the znode and checks whether its 
migration-relevant fields match the data we
+   * intended to write.
+   *
+   * @return (true, current version) if they match, otherwise (false, current 
version), where the version is
+   *         ZkVersion.UnknownVersion if the znode does not exist
+   * @throws MigrationConflictException if the current data cannot be read, as 
we then cannot tell whether the data
+   *                                    is our own
+   */
+  private def checkMigrationStateZkData(
+                                         zkClient: KafkaZkClient,
+                                         path: String,
+                                         expectedData: Array[Byte]
+                                       ): (Boolean, Int) = {
+    val (writtenDataOpt, writtenStat) = try {
+      zkClient.getDataAndStat(path)
+    } catch {
+      case e: Exception => throw new MigrationConflictException(
+        "Unable to verify whether the update to the KRaft Migration ZNode was 
applied after a BADVERSION response.", e)
+    }
+    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()
+    }
+    val currentVersion = if (writtenDataOpt.isDefined) writtenStat.getVersion 
else ZkVersion.UnknownVersion
+    (succeeded, currentVersion)
+  }
+
   def updateMigrationState(migrationState: ZkMigrationLeadershipState): 
ZkMigrationLeadershipState = {
-    val req = SetDataRequest(MigrationZNode.path, 
MigrationZNode.encode(migrationState), migrationState.migrationZkVersion())
-    val resp = retryRequestUntilConnected(req)
-    resp.maybeThrow()
-    migrationState.withMigrationZkVersion(resp.stat.getVersion)
+    val path = MigrationZNode.path
+    val data = MigrationZNode.encode(migrationState)
+    val expectedVersion = migrationState.migrationZkVersion()
+    val (succeeded, newVersion) = conditionalUpdatePath(

Review Comment:
   I'm wondering if `/controller` has an analogous issue. After a lost ack, the 
retry reads back our own registration, and the `>=` causes 
`ControllerMovedException` instead of "ok, it is me" :(
   
   ```scala
     def tryRegisterKRaftControllerAsActiveController(kraftControllerId: Int, 
kraftControllerEpoch: Int): KRaftRegistrationResult = {
       val timestamp = time.milliseconds()
       val curEpochOpt: Option[(Int, Int)] = getControllerEpoch.map(e => (e._1, 
e._2.getVersion))
       val controllerOpt = getControllerRegistration
   
       // If we have a KRaft epoch registered in /controller, and it is not 
_older_ than the requested epoch, throw an error.
       controllerOpt.flatMap(_.kraftEpoch).foreach { kraftEpochInZk =>
         if (kraftEpochInZk >= kraftControllerEpoch) {
           throw new ControllerMovedException(s"Cannot register KRaft 
controller $kraftControllerId with epoch $kraftControllerEpoch " +
             s"as the current controller register in ZK has the same or newer 
epoch $kraftEpochInZk.")
         }
       }
   ```



-- 
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