Skip to content

Commit

Permalink
Merge branch 'master' into support-customize-twepoch
Browse files Browse the repository at this point in the history
  • Loading branch information
thelight1 authored Oct 8, 2019
2 parents 1f75828 + 26e3965 commit ac90ea2
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 14 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,7 @@ insert into leaf_alloc(biz_tag, max_id, step, description) values('leaf-segment-
### Leaf Core

当然,为了追求更高的性能,需要通过RPC Server来部署Leaf 服务,那仅需要引入leaf-core的包,把生成ID的API封装到指定的RPC框架中即可。

#### 注意事项
注意现在leaf使用snowflake模式的情况下 其获取ip的逻辑直接取首个网卡ip【特别对于会更换ip的服务要注意】避免浪费workId

Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ public void updateSegmentFromDb(String key, Segment segment) {
} else if (buffer.getUpdateTimestamp() == 0) {
leafAlloc = dao.updateMaxIdAndGetLeafAlloc(key);
buffer.setUpdateTimestamp(System.currentTimeMillis());
buffer.setStep(leafAlloc.getStep());
buffer.setMinStep(leafAlloc.getStep());//leafAlloc中的step为DB中的step
} else {
long duration = System.currentTimeMillis() - buffer.getUpdateTimestamp();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,29 @@ public boolean init() {
return true;
}

static private final Logger LOGGER = LoggerFactory.getLogger(SnowflakeIDGenImpl.class);
private static final Logger LOGGER = LoggerFactory.getLogger(SnowflakeIDGenImpl.class);

private final long twepoch;
private final long workerIdBits = 10L;
private final long maxWorkerId = -1L ^ (-1L << workerIdBits);//最大能够分配的workerid =1023
private final long maxWorkerId = ~(-1L << workerIdBits);//最大能够分配的workerid =1023
private final long sequenceBits = 12L;
private final long workerIdShift = sequenceBits;
private final long timestampLeftShift = sequenceBits + workerIdBits;
private final long sequenceMask = -1L ^ (-1L << sequenceBits);
private final long sequenceMask = ~(-1L << sequenceBits);
private long workerId;
private long sequence = 0L;
private long lastTimestamp = -1L;
public boolean initFlag = false;
private static final Random RANDOM = new Random();
private int port;

public SnowflakeIDGenImpl(String zkAddress, int port) {
//Thu Nov 04 2010 09:42:54 GMT+0800 (中国标准时间) 美团默认时间戳
//Thu Nov 04 2010 09:42:54 GMT+0800 (中国标准时间)
this(zkAddress, port, 1288834974657L);
}

/**
* @param zkAddress zk地址
* @param port snowflake监听端口
* @param twepoch 我们定义的起始的时间戳,这样可以让时间戳存储时间更久
* @param twepoch 起始的时间戳
*/
public SnowflakeIDGenImpl(String zkAddress, int port, long twepoch) {
this.twepoch = twepoch;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ public boolean init() {
//有自己的节点,zk_AddressNode=ip:port
zk_AddressNode = PATH_FOREVER + "/" + realNode.get(listenAddress);
workerID = workerid;//启动worder时使用会使用
if (!checkInitTimeStamp(curator, zk_AddressNode))
if (!checkInitTimeStamp(curator, zk_AddressNode)) {
throw new CheckLastTimeException("init timestamp check error,forever node timestamp gt this node time");
}
//准备创建临时节点
doService(curator);
updateLocalWorkerID(workerID);
Expand Down Expand Up @@ -186,24 +187,24 @@ private Endpoint deBuildData(String json) throws IOException {
* @param workerID
*/
private void updateLocalWorkerID(int workerID) {
File LeafconfFile = new File(PROP_PATH.replace("{port}", port));
boolean exists = LeafconfFile.exists();
File leafConfFile = new File(PROP_PATH.replace("{port}", port));
boolean exists = leafConfFile.exists();
LOGGER.info("file exists status is {}", exists);
if (exists) {
try {
FileUtils.writeStringToFile(LeafconfFile, "workerID=" + workerID, false);
FileUtils.writeStringToFile(leafConfFile, "workerID=" + workerID, false);
LOGGER.info("update file cache workerID is {}", workerID);
} catch (IOException e) {
LOGGER.error("update file cache error ", e);
}
} else {
//不存在文件,父目录页肯定不存在
try {
boolean mkdirs = LeafconfFile.getParentFile().mkdirs();
boolean mkdirs = leafConfFile.getParentFile().mkdirs();
LOGGER.info("init local file cache create parent dis status is {}, worker id is {}", mkdirs, workerID);
if (mkdirs) {
if (LeafconfFile.createNewFile()) {
FileUtils.writeStringToFile(LeafconfFile, "workerID=" + workerID, false);
if (leafConfFile.createNewFile()) {
FileUtils.writeStringToFile(leafConfFile, "workerID=" + workerID, false);
LOGGER.info("local file cache workerID is {}", workerID);
}
} else {
Expand Down
1 change: 1 addition & 0 deletions leaf-server/deploy/run.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env bash



Expand Down

0 comments on commit ac90ea2

Please sign in to comment.