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
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
e9e7255
release 0.5.1
slievrly Apr 30, 2019
f017c6b
release 0.5.2
slievrly May 17, 2019
7ca3332
Release 0.6.0 (#1106)
zhangthen May 24, 2019
dc088cb
Revert "Release 0.6.0 (#1106)" (#1107)
slievrly May 27, 2019
da96029
re-merging 0.6.0
slievrly May 27, 2019
ca567ef
Release 0.6.1
zhangthen May 31, 2019
1b90f93
release 0.7.0
slievrly Jul 12, 2019
39e4d89
release 0.7.1
slievrly Jul 15, 2019
0945681
release 0.8.0
slievrly Aug 16, 2019
9ffa290
release 0.8.1
slievrly Sep 18, 2019
a174ed5
release 0.9.0
slievrly Oct 16, 2019
5bdbc44
[release] release 1.0.0
slievrly Dec 20, 2019
f990a57
release: release 1.1.0
slievrly Feb 19, 2020
04d0568
Merge pull request #2582 from seata/1.2.0
slievrly Apr 21, 2020
952248d
修改workid生成方式
Jun 10, 2020
ab55797
modify workerid generation method
lj2018110133 Jun 10, 2020
6693048
modify workerid generation method 2th
lj2018110133 Jun 11, 2020
f99be7f
modify workerid generation method 3th
lj2018110133 Jun 11, 2020
53b721b
modify workerid generation method 4th
lj2018110133 Jun 12, 2020
1fade38
Merge branch 'develop' into seata_workerid
slievrly Jun 12, 2020
61d1188
modify workerid generation method 5th
lj2018110133 Jun 12, 2020
b148f7d
Merge branch 'seata_workerid' of https://github.com/lj2018110133/seat…
lj2018110133 Jun 12, 2020
47456e4
contain exception information
lj2018110133 Jun 17, 2020
08d2a8a
Merge branch 'develop' into seata_workerid
slievrly Jun 17, 2020
1ea747c
Merge branch 'master' of https://github.com/lj2018110133/seata into s…
lj2018110133 Jun 19, 2020
15a44aa
Handling conflicts
lj2018110133 Jun 19, 2020
160d41a
Merge branch 'seata_workerid' of https://github.com/lj2018110133/seat…
lj2018110133 Jun 19, 2020
f1a4067
Handling conflicts
lj2018110133 Jun 19, 2020
aa6c17e
Merge branch 'develop' into seata_workerid
l81893521 Jun 19, 2020
af4176a
Merge branch 'develop' into seata_workerid
jsbxyyx Jun 22, 2020
f0d3e8a
Merge branch 'develop' into seata_workerid
funky-eyes Jun 24, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions common/src/main/java/io/seata/common/util/IdWorker.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
* limitations under the License.
*/
package io.seata.common.util;

import java.util.concurrent.ThreadLocalRandom;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
* @author funkye
Expand Down Expand Up @@ -144,13 +144,24 @@ public static IdWorker getInstance() {
if (idWorker == null) {
synchronized (IdWorker.class) {
if (idWorker == null) {
init(ThreadLocalRandom.current().nextLong(1024));
init(initWorkerId());
}
}
}
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

InetAddress address;
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.

}
byte[] ipAddressByteArray = address.getAddress();
return ((ipAddressByteArray[ipAddressByteArray.length - 2] & 0B11) << Byte.SIZE) + (ipAddressByteArray[ipAddressByteArray.length - 1] & 0xFF);
slievrly marked this conversation as resolved.
Show resolved Hide resolved
}

public static void init(Long serverNodeId) {
if (idWorker == null) {
synchronized (IdWorker.class) {
Expand Down
7 changes: 7 additions & 0 deletions common/src/main/java/io/seata/common/util/NumberUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,11 @@ public static Long toLong(String str, final Long defaultValue) {
return defaultValue;
}
}

public static Long toLong(String str) {
if (StringUtils.isNotBlank(str)) {
return Long.valueOf(str);
}
return null;
}
}
14 changes: 8 additions & 6 deletions server/src/main/java/io/seata/server/ParameterParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Stream;

import com.beust.jcommander.JCommander;
import com.beust.jcommander.Parameter;
import com.beust.jcommander.ParameterException;
import io.seata.common.util.IdWorker;
import io.seata.common.util.NumberUtils;
import io.seata.common.util.StringUtils;
import io.seata.config.ConfigurationFactory;
Expand All @@ -49,7 +49,6 @@ public class ParameterParser {

private static final int SERVER_DEFAULT_PORT = 8091;
private static final String SERVER_DEFAULT_STORE_MODE = "file";
private static final Long SERVER_DEFAULT_NODE = ThreadLocalRandom.current().nextLong(1024);

private static final String ENV_SYSTEM_KEY = "SEATA_ENV";
private static final String ENV_SEATA_IP_KEY = "SEATA_IP";
Expand All @@ -68,8 +67,8 @@ public class ParameterParser {
private int port = SERVER_DEFAULT_PORT;
@Parameter(names = {"--storeMode", "-m"}, description = "log store mode : file, db", order = 3)
private String storeMode;
@Parameter(names = {"--serverNode", "-n"}, description = "server node id, such as 1, 2, 3. default is 1", order = 4)
private Long serverNode = SERVER_DEFAULT_NODE;
@Parameter(names = {"--serverNode", "-n"}, description = "server node id, such as 1, 2, 3.it will be generated according to the snowflake by default", order = 4)
private Long serverNode;
@Parameter(names = {"--seataEnv", "-e"}, description = "The name used for multi-configuration isolation.",
order = 5)
private String seataEnv;
Expand All @@ -93,7 +92,7 @@ private void init(String[] args) {

this.seataEnv = StringUtils.trimToNull(System.getenv(ENV_SYSTEM_KEY));
this.host = StringUtils.trimToNull(System.getenv(ENV_SEATA_IP_KEY));
this.serverNode = NumberUtils.toLong(System.getenv(ENV_SERVER_NODE_KEY), SERVER_DEFAULT_NODE);
this.serverNode = NumberUtils.toLong(System.getenv(ENV_SERVER_NODE_KEY));
this.port = NumberUtils.toInt(System.getenv(ENV_SEATA_PORT_KEY), SERVER_DEFAULT_PORT);
this.storeMode = StringUtils.trimToNull(System.getenv(ENV_STORE_MODE_KEY));
} else {
Expand All @@ -105,6 +104,9 @@ private void init(String[] args) {
System.exit(0);
}
}
if (this.serverNode == null) {
this.serverNode = IdWorker.initWorkerId();
}
if (StringUtils.isNotBlank(seataEnv)) {
System.setProperty(ENV_PROPERTY_KEY, seataEnv);
}
Expand Down Expand Up @@ -196,4 +198,4 @@ public String getSeataEnv() {
return seataEnv;
}

}
}