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

optimize: Modify workerid generation method #2787

Merged
merged 31 commits into from
Jun 24, 2020

Conversation

lj2018110133
Copy link
Contributor

@lj2018110133 lj2018110133 commented Jun 10, 2020

Ⅰ. Describe what this PR did

Modify workerid generation method

Ⅱ. Does this pull request fix one issue?

fix #2785

Ⅲ. Why don't you add test cases (unit test/integration test)?

Ⅳ. Describe how to verify it

Ⅴ. Special notes for reviews

@funky-eyes
Copy link
Contributor

the title prefix optimize:

}
byte[] ipAddressByteArray = address.getAddress();
long workerId = (((ipAddressByteArray[ipAddressByteArray.length - 2] & 0B11) << Byte.SIZE) + (ipAddressByteArray[ipAddressByteArray.length - 1] & 0xFF));
Preconditions.checkArgument(workerId >= 0L && workerId < 1024L);
Copy link
Contributor

Choose a reason for hiding this comment

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

if according to the current practice, will the id be greater than 1023? if yes, it is not advisable to simply throw an exception

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No more than 1023, because according to the snowflake algorithm, the maximum 1111111111 is 1023

server/src/main/java/io/seata/server/ParameterParser.java Outdated Show resolved Hide resolved
@funky-eyes funky-eyes added the first-time contributor first-time contributor label Jun 10, 2020
@lj2018110133 lj2018110133 changed the title Seata workerid optimize: Modify workerid generation method Jun 11, 2020
@codecov-commenter
Copy link

codecov-commenter commented Jun 11, 2020

Codecov Report

Merging #2787 into develop will decrease coverage by 0.03%.
The diff coverage is 7.69%.

Impacted file tree graph

@@              Coverage Diff              @@
##             develop    #2787      +/-   ##
=============================================
- Coverage      50.22%   50.18%   -0.04%     
+ Complexity      2954     2952       -2     
=============================================
  Files            588      588              
  Lines          18929    18938       +9     
  Branches        2286     2287       +1     
=============================================
- Hits            9507     9504       -3     
- Misses          8480     8489       +9     
- Partials         942      945       +3     
Impacted Files Coverage Δ Complexity Δ
...n/src/main/java/io/seata/common/util/IdWorker.java 0.00% <0.00%> (ø) 0.00 <0.00> (ø)
...rc/main/java/io/seata/common/util/NumberUtils.java 35.71% <0.00%> (-9.75%) 2.00 <0.00> (ø)
...main/java/io/seata/server/env/ContainerHelper.java 14.28% <0.00%> (ø) 2.00 <0.00> (ø)
...src/main/java/io/seata/server/ParameterParser.java 45.94% <50.00%> (-2.71%) 8.00 <0.00> (ø)
...o/seata/server/coordinator/DefaultCoordinator.java 54.63% <0.00%> (-0.52%) 28.00% <0.00%> (-1.00%)
...in/java/io/seata/server/session/GlobalSession.java 83.71% <0.00%> (-0.46%) 71.00% <0.00%> (-1.00%)

Copy link
Contributor

@funky-eyes funky-eyes left a comment

Choose a reason for hiding this comment

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

restore the unchanged code format

Copy link
Contributor

@funky-eyes funky-eyes left a comment

Choose a reason for hiding this comment

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

LGTM

try {
address = InetAddress.getLocalHost();
} catch (final UnknownHostException e) {
throw new IllegalStateException("Cannot get LocalHost InetAddress, please check your network!");
Copy link
Member

Choose a reason for hiding this comment

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

the exception information should contain e.

@slievrly slievrly added the module/server server module label Jun 17, 2020
Copy link
Member

@slievrly slievrly left a comment

Choose a reason for hiding this comment

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

LGTM

}
}
}
return idWorker;
}

public static long initWorkerId() {
Copy link
Contributor

@l81893521 l81893521 Jun 19, 2020

Choose a reason for hiding this comment

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

I think it will always return 8 bytes, is it too big for long?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Indeed, it will be dealt with next time

@jsbxyyx
Copy link
Member

jsbxyyx commented Jun 22, 2020

The same machine, multiple servers, how to solve?

Copy link
Contributor

@l81893521 l81893521 left a comment

Choose a reason for hiding this comment

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

LGTM

@funky-eyes funky-eyes merged commit d554fc8 into apache:develop Jun 24, 2020
@slievrly slievrly added this to the 1.3.0 milestone Jun 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
first-time contributor first-time contributor module/server server module
Projects
None yet
Development

Successfully merging this pull request may close these issues.

need to optimize the current workerId generation rule of snowflake id
7 participants