CN112988757B - Method, device, equipment and medium for generating global unique ID of distributed system - Google Patents

Method, device, equipment and medium for generating global unique ID of distributed system Download PDF

Info

Publication number
CN112988757B
CN112988757B CN202110431038.4A CN202110431038A CN112988757B CN 112988757 B CN112988757 B CN 112988757B CN 202110431038 A CN202110431038 A CN 202110431038A CN 112988757 B CN112988757 B CN 112988757B
Authority
CN
China
Prior art keywords
time point
self
value
generating
current
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Active
Application number
CN202110431038.4A
Other languages
Chinese (zh)
Other versions
CN112988757A (en
Inventor
熊超
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Hangzhou Moredian Technology Co ltd
Original Assignee
Hangzhou Moredian Technology Co ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Hangzhou Moredian Technology Co ltd filed Critical Hangzhou Moredian Technology Co ltd
Priority to CN202110431038.4A priority Critical patent/CN112988757B/en
Publication of CN112988757A publication Critical patent/CN112988757A/en
Application granted granted Critical
Publication of CN112988757B publication Critical patent/CN112988757B/en
Active legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F16/00Information retrieval; Database structures therefor; File system structures therefor
    • G06F16/20Information retrieval; Database structures therefor; File system structures therefor of structured data, e.g. relational data
    • G06F16/22Indexing; Data structures therefor; Storage structures
    • G06F16/2291User-Defined Types; Storage management thereof

Landscapes

  • Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • Software Systems (AREA)
  • Data Mining & Analysis (AREA)
  • Databases & Information Systems (AREA)
  • Physics & Mathematics (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • Information Retrieval, Db Structures And Fs Structures Therefor (AREA)

Abstract

The application discloses a method, a device, equipment and a storage medium for generating a global unique ID of a distributed system, belonging to the field of computers, wherein the method comprises the following steps: obtaining format information of the global unique ID according to the time point occupied digit, the group ID occupied digit, the group node ID occupied digit and the serial number self-increment value occupied digit, wherein the value of the time point is obtained by right shifting the current millisecond timestamp; when a request for generating the global unique ID is received, comparing a currently used time point with a current system time point, and updating the current time point to a larger time point, wherein the request carries information of an ID domain and the number of generated global unique IDs; generating a self-increment value of the serial number according to the number aiming at the ID field; and generating a global unique ID from the self-increment value of the serial number, the current time point, the preset group ID and the ID of the nodes in the group according to format information. According to the embodiment of the application, the problem of generated ID repetition can be effectively solved.

Description

Method, device, equipment and medium for generating global unique ID of distributed system
Technical Field
The present application relates to the field of computer technologies, and in particular, to a method, an apparatus, a device, and a storage medium for generating a globally unique ID of a distributed system.
Background
In a computer operating environment, the business requirement for generating a unique record identifier is particularly important, and as far as a global unique ID is concerned, the global unique ID is almost the only requirement of all systems, and the global unique ID has important significance in many aspects such as data searching, storage, retrieval speed acceleration and the like. In order to generate the global unique ID, a snowflake algorithm is usually adopted, however, the snowflake algorithm not only fixes the digits of the global unique ID, is not flexible enough and is difficult to meet the complex business requirements, but also easily causes timestamp rollback due to system time synchronization in reality, thereby causing the problem of generated ID repetition, and for this reason, no effective solution has been proposed in the related art.
Disclosure of Invention
The embodiment of the application provides a distributed system global unique ID generation method, a distributed system global unique ID generation device, distributed system global unique ID generation equipment and a storage medium, and aims to at least solve the problem of repeated IDs generated in the related technology.
In a first aspect, an embodiment of the present application provides a method for generating a globally unique ID of a distributed system, including: obtaining format information of the global unique ID according to the time point occupied digit, the group ID occupied digit, the group node ID occupied digit and the serial number self-increment value occupied digit, wherein the value of the time point is obtained by right shifting the current millisecond timestamp; when a request for generating the global unique ID is received, comparing a currently used time point with a current system time point, and updating the current time point to a larger time point, wherein the request carries information for generating the number of the global unique ID; generating a serial number self-increment value according to the number aiming at the ID domain; and generating the global unique ID by the serial number self-increment value, the current time point, the preset group ID and the group node ID according to the format information.
In some embodiments, in the case that the self-increment value of the sequence number corresponding to the current time point exceeds the maximum value, the time point is borrowed backwards.
In some embodiments, after the updating the current time point to the larger time point, the method further comprises: under the condition that the current time point is updated to the current system time point, if the current time point changes, the changed time point is stored persistently; in the case of borrowing points of time backwards, the points of time borrowed are then persisted.
In some embodiments, in the case that the number is multiple, the generating the self-increment value of the sequence number according to the number includes: generating a starting value and a stopping value of the self-increment of the serial numbers according to the quantity; and generating an array according to the starting and ending values, wherein the array is used for network transmission and storage in a memory.
In some embodiments, before obtaining the format information of the globally unique ID according to the number of occupied bits of the time point, the number of occupied bits of the group ID, the number of occupied bits of the node ID in the group, and the number of occupied bits of the self-increment value of the serial number, the method further includes: acquiring an ID generator object from a memory or persistent data; and under the condition that no ID generator object exists, generating an ID generator object according to the current time point, wherein the attributes of the ID generator object comprise an ID field, the current time point, the current serial number self-increment value and current supplementary information, and the current supplementary information comprises a group ID, an intra-group node ID and the current time point.
In some embodiments, after the generating the globally unique ID, the method further comprises: and carrying out fragment storage on the globally unique ID according to the following formula:
J=(T+I)%M
wherein J is a fragment value, T is the time point, I is the self-increment value of the serial number, M is the fragment number, and M is a value of the power of n of 2.
In a second aspect, an embodiment of the present application provides a distributed system global unique ID generation apparatus, including a calculation module, a time point update module, a serial number self-increment value generation module, and an ID generation module, where the calculation module is configured to obtain format information of a global unique ID according to a time point occupied bit number, a group ID occupied bit number, a node ID occupied bit number in a group, and a serial number self-increment value occupied bit number, where a value of a time point is obtained by right shifting a current millisecond timestamp; the time point updating module is used for comparing a currently used time point with a current system time point and updating the current time point to a larger time point when a request for generating the global unique ID is received, wherein the request carries information of an ID domain and the number of the generated global unique IDs; a serial number self-increment value generation module, configured to generate a serial number self-increment value according to the number for the ID field; and the ID generation module is used for generating the global unique ID from the serial number self-increment value, the current time point, the preset group ID and the group node ID according to the format information.
In some embodiments, in the case that the self-increment value of the sequence number corresponding to the current time point exceeds the maximum value, the time point is borrowed backwards.
In a third aspect, an embodiment of the present application provides an electronic device, including a memory and a processor, where the memory stores a computer program, and the processor is configured to execute the computer program to perform any one of the methods described above.
In a fourth aspect, the present application provides a storage medium having a computer program stored therein, where the computer program is configured to execute any one of the above methods when the computer program runs.
According to the content, the bit occupation positions of the time points, the bit occupation positions of the group IDs, the bit occupation positions of the node IDs in the groups and the bit occupation positions of the self-increment values of the serial numbers are flexibly matched, and more complex service requirements can be met. In addition, the currently used time point is compared with the current system time, and the current time point is updated to a larger value, so that the generated ID repetition caused by time backspacing caused by system time synchronization is avoided, and the problem of generated ID repetition is effectively solved.
Drawings
The accompanying drawings, which are included to provide a further understanding of the application and are incorporated in and constitute a part of this application, illustrate embodiment(s) of the application and together with the description serve to explain the application and not to limit the application. In the drawings:
FIG. 1 is a flow chart of a distributed system globally unique ID generation method according to an embodiment of the present application;
FIG. 2 is a schematic diagram of a format of a globally unique ID according to an embodiment of the present application;
fig. 3 is a block diagram of a distributed system globally unique ID generating apparatus according to an embodiment of the present application;
fig. 4 is an internal structural diagram of an electronic device according to an embodiment of the present application.
Detailed Description
In order to make the objects, technical solutions and advantages of the present application more apparent, the present application will be described and illustrated below with reference to the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are merely illustrative of the present application and are not intended to limit the present application. All other embodiments obtained by a person of ordinary skill in the art based on the embodiments provided in the present application without any inventive step are within the scope of protection of the present application. Moreover, it should be appreciated that in the development of any such actual implementation, as in any engineering or design project, numerous implementation-specific decisions must be made to achieve the developers' specific goals, such as compliance with system-related and business-related constraints, which may vary from one implementation to another.
Reference in the specification to "an embodiment" means that a particular feature, structure, or characteristic described in connection with the embodiment can be included in at least one embodiment of the specification. The appearances of the phrase in various places in the specification are not necessarily all referring to the same embodiment, nor are separate or alternative embodiments mutually exclusive of other embodiments. Those of ordinary skill in the art will explicitly and implicitly appreciate that the embodiments described herein may be combined with other embodiments without conflict.
Unless defined otherwise, technical or scientific terms referred to herein shall have the ordinary meaning as understood by those of ordinary skill in the art to which this application belongs. Reference to "a," "an," "the," and similar words throughout this application are not to be construed as limiting in number, and may refer to the singular or the plural. The present application is directed to the use of the terms "including," "comprising," "having," and any variations thereof, which are intended to cover non-exclusive inclusions; for example, a process, method, system, article, or apparatus that comprises a list of steps or modules (elements) is not limited to the listed steps or elements, but may include other steps or elements not expressly listed or inherent to such process, method, article, or apparatus. Reference to "connected," "coupled," and the like in this application is not intended to be limited to physical or mechanical connections, but may include electrical connections, whether direct or indirect. Reference herein to "a plurality" means greater than or equal to two. "and/or" describes an association relationship of associated objects, meaning that three relationships may exist, for example, "A and/or B" may mean: a exists alone, A and B exist simultaneously, and B exists alone. Reference herein to the terms "first," "second," "third," and the like, are merely to distinguish similar objects and do not denote a particular ordering for the objects.
The inventor of the present application finds that, in the related art, the timestamp of the system is directly used as a part of the globally unique ID, but the time of a general system in reality is not accurate, the time synchronization of the server is very frequent, especially, the time synchronization is performed when the system is restarted, if the time of the system before the restart is greater than the real time, the time of the system will fall back to the real current timestamp, and the current timestamp is generated before the system is restarted, so that the finally generated ID will be repeated.
In order to solve the above problem, an embodiment of the present application provides a method for generating a global unique ID of a distributed system, and fig. 1 is a flowchart of the method for generating a global unique ID of a distributed system according to the embodiment of the present application, as shown in fig. 1, the method includes the following steps:
s100: obtaining format information of the global unique ID according to the time point occupied digit, the group ID occupied digit, the group node ID occupied digit and the serial number self-increment value occupied digit, wherein the value of the time point is obtained by right shifting the current millisecond timestamp;
s200: when a request for generating the global unique ID is received, comparing a currently used time point with a current system time point, and updating the current time point to a larger time point, wherein the request carries information of an ID domain and the number of generated global unique IDs;
s300: generating a self-increment value of the serial number according to the number aiming at the ID field;
s400: and generating the global unique ID by the self-increment of the serial number, the current time point, the preset group ID and the ID of the nodes in the group according to the format information.
Therefore, the globally unique ID generated in the embodiment of the present application does not use the system timestamp as a unique basis, but compares the currently used time point with the current system time point, and updates the current time point to a larger time point, which is equivalent to that, if the currently used time point is larger than the current system time point, the currently used time point is used as the current time point; if the currently used time point is smaller than the current system time point, the current system time point is used as the current time point, so that the time can be ensured to be moved backwards, the current time can not be backed off due to time synchronization of the system, and the problem of repeated ID generated in the related technology can be effectively solved.
Besides the information of the number of globally unique IDs, the requested parameters also have ID fields (namely, the fields to which the IDs belong), the IDs of the same field can ensure global uniqueness, and the IDs of different fields do not interfere with each other.
In addition, the number of occupied bits of the time points, the number of occupied bits of the group ID, the number of occupied bits of the node ID in the group and the number of occupied bits of the serial number self-increment are all flexibly configurable, and corresponding configuration can be carried out according to different service requirements.
As an example, in case the self-increment value of the sequence number corresponding to the current time point exceeds the maximum value, then the time point is borrowed backwards. Preferably, in order to compare the used time point with the system time, when the current time point is updated to the current system time point, if the current time point changes, the changed time point is stored persistently; in the case of borrowing points of time backwards, the points of time borrowed are then persisted. Especially when the system is restarted, the comparison is more convenient for the time point of the persistent storage.
It should be noted that, since the size of the occupied digit of the time point affects the size of the occupied digit of the self-increment value of the serial number, the maximum value of the self-increment value is determined, and the size of a self-increment period is determined, and the self-increment period is preferably larger, which is beneficial to performing persistent storage on the running time point, therefore, the digit of the time point right shift in the embodiment of the present application is configurable.
In order to more clearly illustrate the examples of the present application, the following detailed description is given.
As an example, the number of occupied time points is tl bits, the number of occupied configuration group ID bits is gl bits, and the number of occupied configuration group node IDs bits is ml bits, where the group ID is gid and the group node ID is mid. Then for a 64-bit long type ID, the sequence number self-increment occupies the number of bits il = 64-ml-gl-tl.
Therefore, in the embodiment of the present application, the number of occupied bits at the time point, the number of occupied bits of the group ID, the number of occupied bits of the node ID in the group, and the number of occupied bits of the serial number self-increment may be calculated by only configuring three items of the four items, for example, when gl, ml, and il are configured, the number of occupied bits at the time point tl = 64-gl-ml-il. It should be noted that the value of the time point is calculated by right shifting the current millisecond timestamp, and it is configurable how many bits are right shifted. And the actual calculated time point value may not be as long as 64-gl-ml-il, the calculation of the globally unique ID may require padding the high bits with 0. Therefore, the serial number self-increment value, the current time point, the preset group ID and the intra-group node ID are generated into the global unique ID according to the format information.
As an example, in order to avoid repeated calculation in the subsequent generation of the globally unique ID, the configuration information of tl, gl and ml is read after the system is started, and an initialization calculation is performed to obtain the value of il, so that the format information of the globally unique ID can be obtained. For example, fig. 2 is a schematic diagram illustrating a format of a globally unique ID according to an embodiment of the present application, and as shown in fig. 2, a globally unique ID includes a time point of tl bits (referring to a time point when the globally unique ID is generated), a group ID of gl bits, a group node ID of ml bits, and a sequence number auto-increment of il bits. Thus, each time a new current time point is generated, the current time point needs to be shifted to the left by the number of bits tsift countforid = ml + gl + tl. In addition, the combined node information part needs the added value dmAdd = gid < < (ml + il) + (mid < < il), i.e., gid needs to be shifted left by ml + il bits and mid needs to be shifted left by il bits.
When a request for generating the global unique ID is received, aiming at the ID domain of the request, the serial number is sequentially and automatically increased according to the ID number of the request at the current time point, so that the serial number self-increment value is generated. It should be noted that, according to the number il of occupied bits of the self-increment value of the serial number, the maximum value increamax = (1< < il) -1 of the self-increment value of the serial number can be obtained, that is, the maximum value of the self-increment value of the serial number is obtained when the number of il bits is all 1, and if the self-increment value of the serial number is greater than the maximum value at a current time point, a next time point, that is, a time point is incremented by 1. Therefore, in the embodiment of the present application, when a request for generating a globally unique ID is received, in a case where a current time point is updated to a current system time point, the time point is incremented by 1 each time the current time point goes to a next time point. It should be noted that, if the number of IDs of the requests is small, the time interval corresponding to two requests may span multiple time periods, and the calculated time is not increased by 1. If the number of the requested IDs is large, the self-increment value of the sequence number corresponding to the current time point is used up (namely, the maximum value is exceeded), then the self-increment value of the sequence number is regenerated for the self-increment of the time point (namely, the time point is borrowed backwards), and the used time point is stored in a database in a persistent mode. Therefore, in the embodiment of the application, aiming at the situation that the current time changes according to the time point caused by the fact that the system time normally goes backwards and the situation that the time point changes due to the fact that the self-increment value of the serial number exceeds the maximum value, the changed time point is stored in a persistent mode so as to be convenient for comparing the next time of generating the ID with the current time of the system, then a larger value is selected as the basis of the self-increment of the time point, and the situation that the finally generated ID is repeated due to the fact that the time point of system time synchronization or the time point borrowed backwards is too much and the current system time stamp is used as the basis after the system is restarted is prevented.
If a request for generating a single globally unique ID is received, the entry is named typeName, the typeName refers to an ID domain, in a distributed system, the ID generated according to the embodiment of the application can be guaranteed to be globally unique in one ID domain, and larger throughput of the system can be supported through different typeNames, and the finally generated ID can be guaranteed to be globally unique in the distributed system due to the fact that the group ID and the node ID in the group are different.
If a request for acquiring global unique IDs in batch is received, namely the request needs to acquire a plurality of global unique IDs, the entry parameter comprises typeName and count, wherein the count represents the number of the global unique IDs needing to be acquired. Thus, an amount of data of 8 × count bytes is generated.
In the embodiment of the application, after the group ID and the node ID in the group are configured, the global unique ID is generated according to the time point and the sequence number self-increment. Therefore, preferably, the start and stop values of the serial number self-increment value are generated according to the count, and the start and stop values of the serial number self-increment value are fed back to the interface requesting to acquire the ID in batch, so that the consumption of data transmission and storage can be reduced. For example, if the count value is large and exceeds the available sequence number self-increment value at a time point, the time points are borrowed backwards, and a start-stop value corresponding to each time point is fed back. For example, n time points are used in total, and n start and stop values are fed back, and the start and stop values are even long type data. Optionally, an array is generated according to the start-stop value for network transmission and storage in a memory, so that network transmission and memory consumption can be reduced. For example, n arrays are generated in total in batch, and are represented as [ a1, b1), [ a2, b2), … … [ an, bn), where [ a1, b1) indicates that the self-increment value of the serial number is from a1 to b1, and includes the value that a1 does not include b1, [ a2, b2), … … [ an, bn) are the same, and are not described again.
When the final global unique ID needs to be generated, the serial number auto-increment values of the batches are generated by circulating and auto-incrementing in the interval of the fed back arrays, so that the global unique IDs of the batches can be finally generated according to the serial number auto-increment values of the batches and the corresponding time points, the group IDs and the node IDs in the groups. Compared with the snowflake algorithm in the related art, the method and the device for acquiring the IDs in batches can not support the acquisition of the IDs in batches, and the ID acquisition efficiency is greatly improved.
As an example, the globally unique ID of the embodiment of the present application is generated by an ID generator (Idgenerator) object, which is described in detail below.
For example, the Idgenerator object has the following attributes: string typeName, which represents the ID field of the String type; long currentTimeSegment, which represents the current time point of the Long integer; int currentIncrement, which represents the current serial number self-increment of integer; and long currentAdd representing long and integer type of current supplementary information, which includes gid, mid, and a current time point.
Firstly, inquiring whether an Idgenerator object corresponding to typeName does not exist in a current memory, if not, acquiring the corresponding Idgenerator object from persistent data, and if not, creating the Idgenerator object according to the current time point and initializing data: right-shift the time point, i.e. currenttimeset = timeStamp > > tpShiftCount; initializing the self-increment value of the sequence number to be 0, namely currentIncrement = 0; current supplemental information is initialized, which = current time point + gid + mid, currentAdd = (currentTimeSegment < < tShiftCountForId) + dmAdd. In addition, the Idgenerator object is stored in a persistent mode, the persistent data comprise typeName and currentTimeSegment, and the Idgenerator object is saved in the current memory.
If the corresponding Idgenerator object exists in the persistent data, the Idgenerator object is taken out, and currentTimeSegment of the Idgenerator object is updated to be the maximum time point, so that the currentTimeSegment is ensured to be moved backwards all the time, the generated ID repetition caused by the repeated time point is avoided, the initialization data is the same as the above, and the repeated description is omitted. And then storing the Idgenerator object in a persistent mode, and saving the Idgenerator object to the current memory.
Locking the Idgenerator object, if currentTimeSegment corresponding to the current Idgenerator object is less than the current system timestamp, then the current system timestamp can be used as currentTimeSegment, so as to update the Idgenerator object. currentIncrement and currentAdd are reinitialized (the same as above, which is not described in detail), and the identifier object is persisted.
If currentIncrement has reached increasemax, it indicates that all sequence number self-increment values corresponding to the current time point are used up, and it needs to borrow the next time point, currentTimeSegment + 1, and reinitialize currentIncrement and currentAdd (the same as above, no longer repeated), and persist the identifier object.
Therefore, the Idgenerator object calculates the global unique ID = currentAdd + currentIncrement, unlocks the Idgenerator object, and feeds back the finally generated global unique ID.
It should be noted that, in the case of receiving a request for obtaining a global unique ID in bulk, it needs to calculate whether the self-increment value of the sequence number remaining in the identifier object at the current time point is greater than or equal to the number count of the ID to be generated, if so, obtain an initial ID (for example, currentIncrement1+ currentAdd), add the count to currentIncrement1 to obtain currentIncrement2, and finally obtain a global unique ID of currentIncrement2+ currentAdd. If the self-increment value of the remaining sequence numbers at the current time point is smaller than the count, borrowing the time point backwards, for example, n time points are needed, and then generating the start-stop values of n self-increment values of the sequence numbers, which correspond to n intervals. For the x-th interval (0 < x ≦ n), its time point corresponds to currentTimeSegment + x-1.
The global unique ID generation service of the embodiment of the application can run independently, provides an RPC interface for the outside and can be called by each service end to generate the global unique ID.
In addition, in the related art, the generated IDs are stored in a fragmentation manner by modulo ID% M, where M is the number of fragments, and the inventor of the present application finds that, in general, many IDs are not generated at one time point, and for many service scenarios, most of the IDs generated at one time point are not more than one, and in this case, the result obtained by modulo ID% M is 0, which inevitably falls into the first fragment, which results in that the number of IDs stored in the first fragment is extremely large, and the number of IDs stored in other fragments is extremely small or even 0, so that the role played by the fragments is very small. Therefore, one of the purposes of the embodiments of the present application is to make the globally unique ID fall more uniformly among the slices.
Based on the above, the globally unique ID includes a time point, a group ID, an intra-group node ID, and a sequence number self-increment value. Therefore, in the present application, the time point and the serial number are added to a value, and then M is modulo to obtain a slice value, and the formula is expressed as follows:
J=(T+I)%M
wherein J is a fragmentation value, T is a time point of the globally unique ID, I is a serial number self-increment value of the globally unique ID, and M is a fragmentation number, preferably, M is a value of the power n of 2, which can improve the operation efficiency.
In summary, the embodiments of the present application have the following advantages:
(1) the bit number occupied by the time point, the bit number occupied by the group ID, the bit number occupied by the node ID in the group and the self-increment value occupied bit of the serial number are flexibly matched, so that more complex service requirements can be met;
(2) the used time point is stored persistently and is compared with the current system time, and the current time point is updated to a larger value, so that time rollback caused by system time synchronization is avoided, and the problem of generated ID repetition is solved effectively;
(3) through the ID field of typeName, the larger data throughput of the system is supported, and the generated ID can be ensured to be globally unique in the system;
(4) the method can support the batch acquisition of the IDs, and greatly improves the ID acquisition efficiency;
(5) when the IDs are obtained in batches, a plurality of serial number self-increment starting and ending value intervals are fed back, so that the data volume is reduced, and the consumption of data transmission and memory is reduced;
(6) the time points and the serial numbers are added with the self-increment value and then are subjected to modular extraction, so that the globally unique ID can be more uniformly stored in each fragment.
Fig. 3 is a block diagram of a structure of the distributed system global unique ID generation apparatus according to the embodiment of the present application, and as shown in fig. 3, the apparatus includes a calculation module 1, a time point update module 2, a serial number auto-increment value generation module 3, and an ID generation module 4, where the calculation module 1 is configured to obtain format information of a global unique ID according to a time point occupied bit number, a group ID occupied bit number, an intra-group node ID occupied bit number, and a serial number auto-increment value occupied bit number, and a value of a time point is obtained by right shifting a current millisecond timestamp. The time point updating module 2 is configured to, when receiving a request for generating a globally unique ID, compare a currently used time point with a current system time point, and update the current time point to a larger time point, where the request carries information of an ID field and the number of generated globally unique IDs, and generate a serial number self-increment according to the number. The serial number self-increment generating module 3 is used for generating serial number self-increment according to the number aiming at the ID field. The ID generation module 4 is used for generating a global unique ID from the self-increment value of the serial number, the current time point, the preset group ID and the ID of the nodes in the group according to the format information.
As an example, in the case that the self-increment value of the sequence number corresponding to the current time point reaches the maximum value, then the time point is self-incremented.
The above modules may be functional modules or program modules, and may be implemented by software or hardware. For a module implemented by hardware, the modules may be located in the same processor; or the modules can be respectively positioned in different processors in any combination.
It should be noted that, for specific examples in this embodiment, reference may be made to examples described in the foregoing embodiments and optional implementations, and details of this embodiment are not described herein again.
In addition, in combination with the distributed system global unique ID generation method in the foregoing embodiment, the embodiment of the present application may provide a storage medium to implement. The storage medium having stored thereon a computer program; the computer program, when executed by a processor, implements any one of the distributed system globally unique ID generation methods in the above embodiments.
An embodiment of the present application also provides an electronic device, which may be a terminal. The electronic device comprises a processor, a memory, a network interface, a display screen and an input device which are connected through a system bus. Wherein the processor of the electronic device is configured to provide computing and control capabilities. The memory of the electronic equipment comprises a nonvolatile storage medium and an internal memory. The non-volatile storage medium stores an operating system and a computer program. The internal memory provides an environment for the operation of an operating system and computer programs in the non-volatile storage medium. The network interface of the electronic device is used for connecting and communicating with an external terminal through a network. The computer program is executed by a processor to implement a distributed system globally unique ID generation method. The display screen of the electronic equipment can be a liquid crystal display screen or an electronic ink display screen, and the input device of the electronic equipment can be a touch layer covered on the display screen, a key, a track ball or a touch pad arranged on the shell of the electronic equipment, an external keyboard, a touch pad or a mouse and the like.
In one embodiment, fig. 4 is a schematic diagram of an internal structure of an electronic device according to an embodiment of the present application, and as shown in fig. 4, there is provided an electronic device, which may be a server, and its internal structure diagram may be as shown in fig. 4. The electronic device comprises a processor, a network interface, an internal memory and a non-volatile memory connected by an internal bus, wherein the non-volatile memory stores an operating system, a computer program and a database. The processor is used for providing calculation and control capability, the network interface is used for communicating with an external terminal through network connection, the internal memory is used for providing an environment for an operating system and the running of a computer program, the computer program is executed by the processor to realize a distributed system global unique ID generation method, and the database is used for storing data.
Those skilled in the art will appreciate that the configuration shown in fig. 4 is a block diagram of only a portion of the configuration associated with the present application, and does not constitute a limitation on the electronic device to which the present application is applied, and a particular electronic device may include more or less components than those shown in the drawings, or combine certain components, or have a different arrangement of components.
It will be understood by those skilled in the art that all or part of the processes of the methods of the embodiments described above can be implemented by hardware instructions of a computer program, which can be stored in a non-volatile computer-readable storage medium, and when executed, can include the processes of the embodiments of the methods described above. Any reference to memory, storage, database, or other medium used in the embodiments provided herein may include non-volatile and/or volatile memory, among others. Non-volatile memory can include read-only memory (ROM), Programmable ROM (PROM), Electrically Programmable ROM (EPROM), Electrically Erasable Programmable ROM (EEPROM), or flash memory. Volatile memory can include Random Access Memory (RAM) or external cache memory. By way of illustration and not limitation, RAM is available in a variety of forms such as Static RAM (SRAM), Dynamic RAM (DRAM), Synchronous DRAM (SDRAM), Double Data Rate SDRAM (DDRSDRAM), Enhanced SDRAM (ESDRAM), Synchronous Link DRAM (SLDRAM), Rambus Direct RAM (RDRAM), direct bus dynamic RAM (DRDRAM), and memory bus dynamic RAM (RDRAM).
It should be understood by those skilled in the art that various features of the above-described embodiments can be combined in any combination, and for the sake of brevity, all possible combinations of features in the above-described embodiments are not described in detail, but rather, all combinations of features which are not inconsistent with each other should be construed as being within the scope of the present disclosure.
The above-mentioned embodiments only express several embodiments of the present application, and the description thereof is more specific and detailed, but not construed as limiting the scope of the invention. It should be noted that, for a person skilled in the art, several variations and modifications can be made without departing from the concept of the present application, which falls within the scope of protection of the present application. Therefore, the protection scope of the present patent shall be subject to the appended claims.

Claims (6)

1. A distributed system global unique ID generation method is characterized by comprising the following steps:
obtaining format information of the global unique ID according to the time point occupied digit, the group ID occupied digit, the group node ID occupied digit and the serial number self-increment value occupied digit, wherein the value of the time point is obtained by right shifting the current millisecond timestamp;
when a request for generating the global unique ID is received, under the condition that the self-increment value of the serial number corresponding to the current time point exceeds the maximum value, the time point is borrowed backwards, and under the condition that the current time point is updated to the current system time point, if the current time point changes, the changed time point is stored persistently; under the condition of borrowing time points backwards, the borrowed time points are stored persistently, when the system is restarted, the current time points of the persistent storage and the current time points of the system are compared, and a larger value is selected as the basis of time point self-increment, wherein the request carries information of an ID domain and the number of the generated global unique IDs;
generating a serial number self-increment value according to the number aiming at the ID domain;
generating the global unique ID by the serial number self-increment value, the current time point, a preset group ID and an intra-group node ID according to the format information;
in the case that the number is multiple, the generating the self-increment value of the sequence number according to the number comprises:
generating a starting value and a stopping value of the self-increment of the serial numbers according to the quantity;
and generating an array according to the starting and ending values, wherein the array is used for network transmission and storage in a memory.
2. The method according to claim 1, wherein before obtaining format information of the globally unique ID according to a number of bits occupied at a time point, a number of bits occupied by a group ID, a number of bits occupied by an intra-group node ID, and a number of bits occupied by a self-increment value of a serial number, the method further comprises:
acquiring an ID generator object from a memory or persistent data;
and under the condition that no ID generator object exists, generating an ID generator object according to the current time point, wherein the attributes of the ID generator object comprise an ID field, the current time point, the current serial number self-increment value and current supplementary information, and the current supplementary information comprises a group ID, an intra-group node ID and the current time point.
3. The method of claim 1, wherein after said generating the globally unique ID, the method further comprises:
and carrying out fragment storage on the globally unique ID according to the following formula:
J=(T+I)%M
wherein J is a fragment value, T is the time point, I is the self-increment value of the serial number, M is the fragment number, and M is a value of the power of n of 2.
4. A distributed system globally unique ID generation apparatus, comprising:
the computing module is used for obtaining format information of the global unique ID according to the time point occupied digit, the group ID occupied digit, the intra-group node ID occupied digit and the serial number self-increment value occupied digit, wherein the time point value is obtained by right shifting a current millisecond timestamp;
the time point updating module is used for borrowing time points backwards when a request for generating the global unique ID is received and the self-increment value of the serial number corresponding to the current time point exceeds the maximum value, and persistently storing the changed time points if the current time point changes under the condition of updating the current time point to the current system time point; under the condition of borrowing time points backwards, the borrowed time points are stored persistently, when the system is restarted, the current time points of the persistent storage and the current time points of the system are compared, and a larger value is selected as the basis of time point self-increment, wherein the request carries information of an ID domain and the number of the generated global unique IDs;
a serial number self-increment value generation module, configured to generate a serial number self-increment value according to the number for the ID field;
the ID generation module is used for generating the global unique ID from the serial number self-increment value, the current time point, the preset group ID and the group node ID according to the format information; in the case that the number is multiple, the generating the self-increment value of the sequence number according to the number comprises: generating a starting value and a stopping value of the self-increment of the serial numbers according to the quantity; and generating an array according to the starting and ending values, wherein the array is used for network transmission and storage in a memory.
5. An electronic device comprising a memory and a processor, wherein the memory has stored therein a computer program, and wherein the processor is arranged to execute the computer program to perform the method of any of claims 1 to 3.
6. A storage medium, in which a computer program is stored, wherein the computer program is arranged to perform the method of any of claims 1 to 3 when executed.
CN202110431038.4A 2021-04-21 2021-04-21 Method, device, equipment and medium for generating global unique ID of distributed system Active CN112988757B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202110431038.4A CN112988757B (en) 2021-04-21 2021-04-21 Method, device, equipment and medium for generating global unique ID of distributed system

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202110431038.4A CN112988757B (en) 2021-04-21 2021-04-21 Method, device, equipment and medium for generating global unique ID of distributed system

Publications (2)

Publication Number Publication Date
CN112988757A CN112988757A (en) 2021-06-18
CN112988757B true CN112988757B (en) 2021-10-26

Family

ID=76341545

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202110431038.4A Active CN112988757B (en) 2021-04-21 2021-04-21 Method, device, equipment and medium for generating global unique ID of distributed system

Country Status (1)

Country Link
CN (1) CN112988757B (en)

Families Citing this family (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN113656491A (en) * 2021-06-30 2021-11-16 紫光云技术有限公司 Distributed ID generation method of multi-Region cloud architecture
CN114143285B (en) * 2021-10-29 2023-11-28 青岛海尔科技有限公司 Method, device and server for generating ID in distributed system
CN114281872B (en) * 2022-03-07 2022-05-24 广联达科技股份有限公司 Method, device and equipment for generating distributed serial number and readable storage medium

Family Cites Families (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN107578248B (en) * 2016-06-30 2021-09-03 阿里巴巴集团控股有限公司 Supervision code generation method and device and server
CN109740120A (en) * 2018-12-14 2019-05-10 深圳壹账通智能科技有限公司 The generation method of unique identification, device, computer equipment under distributed environment
CN110147281A (en) * 2019-05-15 2019-08-20 上海淇毓信息科技有限公司 Optimize method, apparatus, the electronic equipment that snowflake algorithm is applied in financial business
CN111930765A (en) * 2020-07-31 2020-11-13 银盛支付服务股份有限公司 Method for generating unique serial number of distributed system

Also Published As

Publication number Publication date
CN112988757A (en) 2021-06-18

Similar Documents

Publication Publication Date Title
CN112988757B (en) Method, device, equipment and medium for generating global unique ID of distributed system
US10657064B1 (en) Extending usages of cached data objects beyond cache expiration periods
CN111078147B (en) Processing method, device and equipment for cache data and storage medium
US10678697B1 (en) Asynchronous cache building and/or rebuilding
US5542087A (en) Linear hashing for distributed records
US20020065870A1 (en) Method and apparatus for heterogeneous distributed computation
CN111274252B (en) Block chain data uplink method and device, storage medium and server
US10824559B2 (en) Counter tracker service
US6792510B1 (en) System and method for updating a cache
CN109299190B (en) Method and device for processing metadata of object in distributed storage system
CN110427386B (en) Data processing method, device and computer storage medium
US11010305B2 (en) Invalidating cached data objects in write-through operations
CN112860592B (en) Data caching method and device based on linked list, electronic equipment and storage medium
US20140195767A1 (en) Lightweight Random Memory Allocation
US10261949B2 (en) Packed row representation for efficient network serialization with direct column indexing in a network switch
CN111522811A (en) Database processing method and device, storage medium and terminal
CN113886496A (en) Data synchronization method and device of block chain, computer equipment and storage medium
Alam et al. GPU-based parallel algorithm for generating massive scale-free networks using the preferential attachment model
CN116860798A (en) Data query method, electronic device and computer readable storage medium
CN116303343A (en) Data slicing method, device, electronic equipment and storage medium
CN115203255A (en) Data query method and device, electronic equipment and storage medium
CN111538718B (en) Entity id generation and positioning method, capacity expansion method and equipment of distributed system
CN112783866A (en) Data reading method and device, computer equipment and storage medium
CN115599532A (en) Index access method and computer cluster
Barszcz Intercube communication for the iPSC/860

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination
GR01 Patent grant
GR01 Patent grant