Skip to content
11 changes: 7 additions & 4 deletions core/src/main/scala/kafka/cluster/Partition.scala
Original file line number Diff line number Diff line change
Expand Up @@ -678,10 +678,13 @@ class Partition(val topicPartition: TopicPartition,
isr: Set[Int],
addingReplicas: Seq[Int],
removingReplicas: Seq[Int]): Unit = {
remoteReplicasMap.clear()
assignment
.filter(_ != localBrokerId)
.foreach(id => remoteReplicasMap.getAndMaybePut(id, new Replica(id, topicPartition)))
val newRemoteReplicas = assignment.filter(_ != localBrokerId)
val removedReplicas = remoteReplicasMap.keys.filter(!newRemoteReplicas.contains(_))

// due to code paths accessing remoteReplicasMap without a lock,
// first add the new replicas and then remove the old ones
newRemoteReplicas.foreach(id => remoteReplicasMap.getAndMaybePut(id, new Replica(id, topicPartition)))
removedReplicas.foreach(remoteReplicasMap.remove)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I decided to not get fancy with refactorings - this is literally the old code (

removedReplicas.foreach(remoteReplicasMap.remove)
))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would remoteReplicasMap --= removedReplicas work here?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, this is a Pool, so we would have to add a removeAll method. Seems easy enough though since it can call the relevant method in ConcurrentMap.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remoteReplicasMap --= removedReplicas doesn't compile - the remoteReplicasMap is using a Kafka Pool class which itself is using a Java Map and I don't think they support the --= notation


if (addingReplicas.nonEmpty || removingReplicas.nonEmpty)
assignmentState = OngoingReassignmentState(addingReplicas, removingReplicas, assignment)
Expand Down
51 changes: 51 additions & 0 deletions core/src/test/scala/unit/kafka/cluster/PartitionLockTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import org.mockito.ArgumentMatchers
import org.mockito.Mockito.{mock, when}

import scala.jdk.CollectionConverters._
import scala.concurrent.duration._

/**
* Verifies that slow appends to log don't block request threads processing replica fetch requests.
Expand Down Expand Up @@ -116,6 +117,56 @@ class PartitionLockTest extends Logging {
future.get(15, TimeUnit.SECONDS)
}

/**
* Concurrently calling updateAssignmentAndIsr should always ensure that non-lock access
* to the inner remoteReplicaMap (accessed by getReplica) cannot see an intermediate state
* where replicas present both in the old and new assignment are missing
*/
@Test
def testGetReplicaWithUpdateAssignmentAndIsr(): Unit = {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fails incredibly quickly 100/100 times without the Partition.scala changes.

val active = new AtomicBoolean(true)
val replicaToCheck = 3
val firstReplicaSet = Seq[Integer](3, 4, 5).asJava
val secondReplicaSet = Seq[Integer](1, 2, 3).asJava
def partitionState(replicas: java.util.List[Integer]): LeaderAndIsrPartitionState = new LeaderAndIsrPartitionState()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to repeat LeaderAndIsrPartitionState twice.

.setControllerEpoch(1)
.setLeader(replicas.get(0))
.setLeaderEpoch(1)
.setIsr(replicas)
.setZkVersion(1)
.setReplicas(replicas)
.setIsNew(true)
val offsetCheckpoints: OffsetCheckpoints = mock(classOf[OffsetCheckpoints])
// Update replica set synchronously first to avoid race conditions
partition.makeLeader(partitionState(secondReplicaSet), offsetCheckpoints)
assertTrue(s"Expected replica $replicaToCheck to be defined", partition.getReplica(replicaToCheck).isDefined)

var i = 0

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be inside the thread state?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, nice catch

val future = executorService.submit((() => {
// Flip assignment between two replica sets
while (active.get) {
val replicas = if (i % 2 == 0) {
firstReplicaSet
} else {
secondReplicaSet
}

partition.makeLeader(partitionState(replicas), offsetCheckpoints)

i += 1
Thread.sleep(1) // just to avoid tight loop
}
}): Runnable)

val deadline = 5.seconds.fromNow

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

5 seconds is quite a bit. Can it be lower?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so. I opted for 5s as I saw the other tests had up to 15s of waits for futures. Let me see if 1s can go

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lowered to 1s

while(deadline.hasTimeLeft()) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: space missing after while.

assertTrue(s"Expected replica $replicaToCheck to be defined", partition.getReplica(replicaToCheck).isDefined)
}
active.set(false)
future.get(5, TimeUnit.SECONDS)
assertTrue(s"Expected replica $replicaToCheck to be defined", partition.getReplica(replicaToCheck).isDefined)
}

/**
* Perform concurrent appends and replica fetch requests that don't require write lock to
* update follower state. Release sufficient append permits to complete all except one append.
Expand Down