[QUESTION] Thread safe problem about HandlerState#changeToReadyState #18401
Unanswered
BewareMyPower
asked this question in
Q&A
Replies: 2 comments
-
Is following implementation better? private static boolean notClosed(State state) {
return state == State.Uninitialized || state == State.Connecting || state == State.RegisteringSchema;
}
// moves the state to ready if it wasn't closed
protected boolean changeToReadyState() {
return STATE_UPDATER.getAndUpdate(this, state -> (notClosed(state) ? State.Ready : state)) == State.Ready;
} |
Beta Was this translation helpful? Give feedback.
0 replies
This comment was marked as off-topic.
This comment was marked as off-topic.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'm not sure if it's bug. It's more a question. As we can see,
pulsar/pulsar-client/src/main/java/org/apache/pulsar/client/impl/HandlerState.java
Lines 53 to 56 in 6089292
HandlerState#changeToReadyState
is not an atomic operation. I'm not sure there's a race case like following timelineSTATE_UPDATER.compareAndSet(this, State.Uninitialized, State.Ready)
State.Connecting
State.Connecting
setState(State.Uninitialized)
State.Connecting
State.Uninitialized
STATE_UPDATER.compareAndSet(this, State.Connecting, State.Ready)
State.Uninitialized
State.Uninitialized
STATE_UPDATER.compareAndSet(this, State.RegisteringSchema, State.Ready)
State.Uninitialized
State.Uninitialized
As we can see, there's a time point that the state was changed back to
Uninitialized
fromConnecting
. However, we should expect the state to beReady
because neitherUninitialized
norConnecting
was a closed state.I see references of
changeToReadyState
inProducerImpl
andConsumerImpl
were protected by the lock directly or indirectly, likepulsar/pulsar-client/src/main/java/org/apache/pulsar/client/impl/ConsumerImpl.java
Lines 738 to 739 in 6089292
I'm not sure if the lock works because it requires some
setState
invocations are protected by the lock and I didn't check it in detail.And in
TransactionMetaStoreHandler#connectionOpened
, there's no lock.pulsar/pulsar-client/src/main/java/org/apache/pulsar/client/impl/TransactionMetaStoreHandler.java
Lines 115 to 117 in 6089292
I'm not sure if the thread safety could be guaranteed. IMO, if there's no possibility that the state was changed back to
Connecting
orUninitialized
duringchangeToReadyState
, it will be thread safe. Or this race condition is acceptable?Beta Was this translation helpful? Give feedback.
All reactions