Files
IM-server-new/broker/src/main/java/win/liyufan/im/MessageShardingUtil.java
T

131 lines
4.1 KiB
Java
Raw Normal View History

2019-03-16 17:04:23 +08:00
/*
* This file is part of the Wildfire Chat package.
* (c) Heavyrain2012 <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
package win.liyufan.im;
2019-05-06 08:18:01 +08:00
import java.util.Calendar;
2019-03-16 17:04:23 +08:00
import java.util.Date;
public class MessageShardingUtil {
private static SpinLock mLock = new SpinLock();
2020-07-01 11:04:10 +08:00
private static volatile int rotateId = 0;
2020-08-24 09:30:59 +08:00
private static volatile long timeId = 0;
2019-03-16 17:04:23 +08:00
private static int nodeId = 0;
private static int rotateIdWidth = 15;
private static int rotateIdMask = 0x7FFF;
private static int nodeIdWidth = 6;
private static int nodeIdMask = 0x3F;
private static final long T201801010000 = 1514736000000L;
public static void setNodeId(int nodeId) {
MessageShardingUtil.nodeId = nodeId;
}
/**
* ID = timestamp(43) + nodeId(6) + rotateId(15)
* 所以时间限制是到2157/5/15(2的42次幂代表的时间 + (2018-1970))。节点数限制是小于64,每台服务器每毫秒最多发送32768条消息
* @return
*/
2020-08-24 09:30:59 +08:00
public static long generateId() throws Exception {
2019-03-16 17:04:23 +08:00
mLock.lock();
rotateId = (rotateId + 1)&rotateIdMask;
mLock.unLock();
long id = System.currentTimeMillis() - T201801010000;
2020-08-24 09:30:59 +08:00
if (id > timeId) {
timeId = id;
rotateId = 1;
} else if(id == timeId) {
if (rotateId == (rotateIdMask - 1)) { //当前空间已经用完,等待下一个空间打开
while (id <= timeId) {
id = System.currentTimeMillis() - T201801010000;
}
mLock.unLock();
return generateId();
}
} else { //id < timeId;
if (rotateId > (rotateIdMask -1)*9/10) { //空间已经接近用完
if (timeId - id < 3000) { //时间回拨小于3秒,等待时间赶上回拨之前记录
while (id < timeId) {
id = System.currentTimeMillis() - T201801010000;
}
mLock.unLock();
return generateId();
} else { //时间回拨大于3秒,抛出异常,这段时间消息将不可用。
mLock.unLock();
throw new Exception("Time turn back " + (timeId - id) + " ms, it too long!!!");
}
} else {
id = timeId;
}
}
2019-03-16 17:04:23 +08:00
id <<= nodeIdWidth;
id += (nodeId & nodeIdMask);
id <<= rotateIdWidth;
id += rotateId;
return id;
}
public static String getMessageTable(long mid) {
if (DBUtil.IsEmbedDB) {
return "t_messages";
}
2019-05-06 08:18:01 +08:00
Calendar calendar = Calendar.getInstance();
2020-10-23 11:24:44 +08:00
if (mid != Long.MAX_VALUE && mid != 0) {
2019-07-05 08:47:44 +08:00
mid >>= (nodeIdWidth + rotateIdWidth);
Date date = new Date(mid + T201801010000);
calendar.setTime(date);
} else {
Date date = new Date(System.currentTimeMillis());
calendar.setTime(date);
}
2019-05-06 08:18:01 +08:00
int month = calendar.get(Calendar.MONTH);
int year = calendar.get(Calendar.YEAR);
2019-03-16 17:04:23 +08:00
year %= 3;
return "t_messages_" + (year * 12 + month);
}
2019-04-19 08:15:27 +08:00
public static String getPreviousMessageTable(long mid) {
2020-10-23 11:24:44 +08:00
return getMessageTable(mid, -1);
}
public static String getMessageTable(long mid, int offset) {
2019-04-19 08:15:27 +08:00
if (DBUtil.IsEmbedDB) {
return null;
}
mid >>= (nodeIdWidth + rotateIdWidth);
Date date = new Date(mid + T201801010000);
2019-05-06 08:18:01 +08:00
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int month = calendar.get(Calendar.MONTH);
int year = calendar.get(Calendar.YEAR);
2019-04-19 08:15:27 +08:00
year %= 3;
2020-10-23 11:24:44 +08:00
month = month + offset;
while (month < 0) {
month += 12;
2019-05-06 08:18:01 +08:00
year = (year + 3 - 1)%3;
2019-04-19 08:15:27 +08:00
}
2020-10-23 11:24:44 +08:00
while (month >= 12) {
month -= 12;
year = (year + 1)%3;
}
2019-04-19 08:15:27 +08:00
return "t_messages_" + (year * 12 + month);
}
2019-03-16 17:04:23 +08:00
}