Skip to content
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

refact(core): remove lock of globalMasterInfo to optimize perf #2151

Merged
merged 2 commits into from
Mar 14, 2023
Merged

Conversation

zyxxoo
Copy link
Contributor

@zyxxoo zyxxoo commented Mar 9, 2023

No description provided.

@codecov
Copy link

codecov bot commented Mar 9, 2023

Codecov Report

Merging #2151 (5361aeb) into master (31c6c6d) will increase coverage by 15.21%.
The diff coverage is 75.00%.

@@              Coverage Diff              @@
##             master    #2151       +/-   ##
=============================================
+ Coverage     53.71%   68.92%   +15.20%     
- Complexity      652      979      +327     
=============================================
  Files           488      488               
  Lines         40078    40084        +6     
  Branches       5607     5608        +1     
=============================================
+ Hits          21528    27628     +6100     
+ Misses        16284     9799     -6485     
- Partials       2266     2657      +391     
Impacted Files Coverage Δ
...rg/apache/hugegraph/api/filter/RedirectFilter.java 77.77% <50.00%> (+53.96%) ⬆️
...h/masterelection/StandardStateMachineCallback.java 65.11% <50.00%> (+13.95%) ⬆️
...che/hugegraph/masterelection/GlobalMasterInfo.java 100.00% <100.00%> (+9.09%) ⬆️

... and 166 files with indirect coverage changes

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

public synchronized boolean isMaster() {
return this.isMaster;
public void info(boolean isMaster, String url) {
this.info = new Info(isMaster, url);
Copy link
Member

@imbajin imbajin Mar 9, 2023

Choose a reason for hiding this comment

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

volatile Info info = new Info(isMaster, url);

Does volatile guarantee the atomic integrity of Info initialization? Or shall we consider to use final to guarantee to complete initialization like:

final Info info = new Info(isMaster, url);

Update:volatile could guarantee the obj init integrity, I forgot it:) However, if we don't need the obj watchable in all the time, 'final' is enough for these cases(more lightly)

Copy link
Contributor Author

@zyxxoo zyxxoo Mar 9, 2023

Choose a reason for hiding this comment

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

info define with volatile, so as my think, the write operator happens-before read operator, so if write operator happend first, the read operator must after the write finish

Copy link
Member

Choose a reason for hiding this comment

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

info define with volatile, so as my think, the write operator happens-before read operator, so if write operator happend first, the read operator must after the write finish

        public Info(boolean isMaster, String url) {
            this.isMaster = isMaster; // ①
            this.url = url; // ②
        }

new Info include ① & ② step, when ① is finished but ② is not, will we see an inconsistent assignments?

like Info info = new Info(true, "a"), will we get Info(true, "") or Info(false, "a")?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the case don't happend, because method parameter be bound;
when call 1 step, the isMaster and url parameter never be modify by external logic, because the boolean is copy the origin data, and string is final;

this is a interesting question:

      boolean isMaster = global.isMaster(); // 1
      String url = global.url(); //2
      this.info = new Info(isMaster, url); // 3

if the code run in more than one thread, the timing might look like this:
thread1: run 1 step; thread2: modify global.master; thread2: modify global.url; thread1: run 2; this case will have some concurrency problem;
but in our case, only one thread modify global, so here is no problem

Copy link
Contributor Author

Choose a reason for hiding this comment

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

here is a “copy on write” thought;
single thread copy the data and modify it, then multi-thread read data don't consider lock

return;
}

boolean isMaster = Objects.equals(context.node(), master.node());
String url = master.url();
this.globalMasterInfo.set(isMaster, url);
this.globalMasterInfo.info(isMaster, url);
Copy link
Contributor

Choose a reason for hiding this comment

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

how frequently the initGlobalMasterInfo() to be called?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

frequently = 1 / heatbeat

@javeme javeme changed the title optimize: remove lock optimize: remove lock of globalMasterInfo Mar 10, 2023

public synchronized boolean isMaster() {
return this.isMaster;
public final void nodeInfo(boolean isMaster, String url) {
Copy link
Member

Choose a reason for hiding this comment

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

nice but why do we add final for the method?(for design?)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

nice but why do we add final for the method?(for design?)

final method no usefull,only express dont modify

Copy link
Contributor

Choose a reason for hiding this comment

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

looks useful, the subclasses cannot override it

@imbajin imbajin merged commit 6409d92 into master Mar 14, 2023
@imbajin imbajin deleted the zy_dev branch March 14, 2023 10:55
@imbajin imbajin changed the title optimize: remove lock of globalMasterInfo refact(core): remove lock of globalMasterInfo to optimize perf Mar 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants