-
Notifications
You must be signed in to change notification settings - Fork 3.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[fix][client] Add initialization for the OpSendMsg #16256
Conversation
pulsar-client/src/main/java/org/apache/pulsar/client/impl/ProducerImpl.java
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please explain what's the affect of these fields without initial values.
I have explained it in the PR description. Is there something unclear? |
In addition, adding initial values to a recyclable class is dangerous. For a recyclable class, we should make constructors private and only expose some factory methods and set the value in these factory methods. The reason is that if the object was reused from the pool, the initial value should be what was set in For example, @Data
class RecyclableInteger {
private static final Recycler<RecyclableInteger> RECYCLER = new Recycler<RecyclableInteger>() {
@Override
protected RecyclableInteger newObject(Handle<RecyclableInteger> handle) {
return new RecyclableInteger(handle);
}
};
private final Recycler.Handle<RecyclableInteger> handle;
private int x = 100; // initial value: 100
private RecyclableInteger(Recycler.Handle<RecyclableInteger> handle) {
this.handle = handle;
}
public static RecyclableInteger create() {
return RECYCLER.get(); // retrieve an object without modifying the value of `x`
}
public void recycle() {
this.x = 0; // recycled value: 0
handle.recycle(this);
}
} RecyclableInteger i = RecyclableInteger.create();
System.out.println(i.getX()); // 100 as we expected
i.recycle();
i = RecyclableInteger.create();
System.out.println(i.getX()); // 0, which is unexpected |
Sorry at the time I replied, the PR description was not updated. Then, please see my another comment. |
The best practice might be adding an private void initialize() {
x = 100;
}
public static RecyclableInteger create() {
final RecyclableInteger i = RECYCLER.get();
i.initialize();
return i;
} |
Before this PR, there is already an inconsistent issue in the OpSendMsg. The value of the OpSendMsg just initialized is not the same as the OpSendMsg just being recycled. This PR is to fix this inconsistency. I was wondering if we could have the recyle method call some kind of initialization method so we don't need to maintain the same initialization value in two places. |
Yes. |
@BewareMyPower Hi, I have added the initialization for the OpSendMsg, PTAL again. Thanks. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
### Motivation Some parameters in OpSendMsg are not specified with initialized values. These initial values are all relied upon by other codes. Such as here: https://github.com/apache/pulsar/blob/eec46ddcba4d2b4f956e1b4d63154cc43087f507/pulsar-client/src/main/java/org/apache/pulsar/client/impl/ProducerImpl.java#L1409-L1422 ### Modifications * Add the initialization for the OpSendMsg.
### Motivation Some parameters in OpSendMsg are not specified with initialized values. These initial values are all relied upon by other codes. Such as here: https://github.com/apache/pulsar/blob/eec46ddcba4d2b4f956e1b4d63154cc43087f507/pulsar-client/src/main/java/org/apache/pulsar/client/impl/ProducerImpl.java#L1409-L1422 ### Modifications * Add the initialization for the OpSendMsg.
Motivation
Some parameters in OpSendMsg are not specified with initialized values.
These initial values are all relied upon by other codes. Such as here:
pulsar/pulsar-client/src/main/java/org/apache/pulsar/client/impl/ProducerImpl.java
Lines 1409 to 1422 in eec46dd
Modifications
Documentation
Check the box below or label this PR directly.
Need to update docs?
doc-required
(Your PR needs to update docs and you will update later)
doc-not-needed
(Please explain why)
doc
(Your PR contains doc changes)
doc-complete
(Docs have been already added)