After U8server supports distributed deployment, we have one more problem to solve, which is the unique order number generation problem in distributed environments. The order number generation rule before u8server is a 32-bit timestamp + 32-bit sequence number, resulting in a 64-bit long-type order number.
Before considering, using UUID and so on, but in a comprehensive consideration, we decided to let the order number to use the number type (good sort), while the order number gradually increased, and has a certain self-explanatory semantics (here mainly refers to the production time of the order)
After supporting distributed deployment, we make the following adjustments to the order number generation rules:
1, 32-bit timestamp unchanged (accurate to seconds, up to 64-year order number is supported)
2, Middle plus 10-bit unique U8server instance ID, starting from 0, that is, up to 1024 deployment nodes.
3, after 22 bit serial number (after different seconds, serial number resets, starting from 0)
The specific placeholder descriptions are shown below:
Let's take a look at the order number generation code:
public class Idgenerator {private static idgenerator instance;
private int serverid = 0;
Private long currordersequence = 0L;
Private long lasttimestamp = -1l;
Private Long Sequencemask = (1<<22);
Private Idgenerator () {globalconfig config = (globalconfig) uapplicationcontext.getbean ("GlobalConfig"); if (config = = null) {LOG.E ("GlobalConfig is not exists.
Deployid not config? ");
Return
} ServerID = Config.getdeployid ();
} public static Idgenerator getinstance () {if (instance = = null) {instance = new Idgenerator ();
} return instance;
} Public synchronized Long Nextorderid () {Calendar can = calendar.getinstance ();
int year = Can.get (calendar.year)-2013;
int month = Can.get (calendar.month) + 1;
int day = Can.get (calendar.day_of_month);
int hour = Can.get (Calendar.hour_of_day); int min = can.Get (Calendar.minute);
int sec = Can.get (Calendar.second);
Long req = year; Req = Req << 4 |
Month Req = Req << 5 |
Day Req = Req << 5 |
Hour Req = Req << 6 |
Min Req = Req << 6 |
Sec
if (ServerID >= 1024x768) {log.e ("U8server deploy_id must is in 0 (include) and 1024x768 (exclude)");
return-1;
} Long currtime = req;
if (req = = Lasttimestamp) {this.currordersequence = this.currordersequence + 1;
if (this.currordersequence >= sequencemask) {this.currordersequence = Sequencemask; LOG.E ("WOW!!! U8server had generate more than%s orders per seconds.
I ' m sure you now has enough money to redevelop u8server to fix the problem ", sequencemask);
return-1;
}}else{this.currordersequence = 0L;
Lasttimestamp = Currtime;
}Req = Req << 10|
ServerID; Req = Req << 22|
This.currordersequence;
return req;
}
}
Let's make a simple explanation of the above code.
First take to the current timestamp, that is, the current year, month, day, hour, minute, second. We are only accurate to seconds. For months, days, hours, minutes, seconds a few values, his expression needs the largest number of digits is fixed, for example, the month, the maximum of 12 months a year, so we use 4 bits can be represented (1<<4=16), the same, for the day, one months maximum 31 days, with 5 bits can be expressed; for hours, A maximum of 24 hours a day, 5-bit can be expressed, for minutes and seconds, the maximum is 60, we use 6 bits can be expressed.
So the day of the month and seconds, a few total placeholder for the 4+5+5+6+6=26 bit (total timestamp reservation is 32 bits, so the number of digits of the year is 6 bits, so it says up to 64 years of the order number)
After the timestamp, plus the 10-bit u8server Unique instance ID, this we put in jdbc.properties u8server.deploy_id configuration, each deployment of an instance, this ID can not be duplicated, starting from 0, maximum 1023.
Finally, add a 22-bit serial number. This number is reset every second. That is, in the same second, a single u8server instance can generate up to 4194304 (1<<22) orders, if really exceeded, then really congratulate you ...
This will eventually result in a long, globally unique order number.
Original address: U8SDK Technology Blog