CN111949730A - Distributed global sequence number implementation method and sequence distributor - Google Patents

Distributed global sequence number implementation method and sequence distributor Download PDF

Info

Publication number
CN111949730A
CN111949730A CN202010807317.1A CN202010807317A CN111949730A CN 111949730 A CN111949730 A CN 111949730A CN 202010807317 A CN202010807317 A CN 202010807317A CN 111949730 A CN111949730 A CN 111949730A
Authority
CN
China
Prior art keywords
sequence
value
database
generated
equal
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.)
Granted
Application number
CN202010807317.1A
Other languages
Chinese (zh)
Other versions
CN111949730B (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.)
Bank of China Ltd
Original Assignee
Bank of China 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 Bank of China Ltd filed Critical Bank of China Ltd
Priority to CN202010807317.1A priority Critical patent/CN111949730B/en
Publication of CN111949730A publication Critical patent/CN111949730A/en
Application granted granted Critical
Publication of CN111949730B publication Critical patent/CN111949730B/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/27Replication, distribution or synchronisation of data between databases or within a distributed database system; Distributed database system architectures therefor
    • 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/21Design, administration or maintenance of databases

Abstract

The invention discloses a distributed global sequence number realization method and a sequence distributor, wherein the method is applied to at least one sequence distributor in a distributed system, the distributed system also comprises a database and a sequence client, the sequence distributor is respectively connected with the database and the sequence client, and the method comprises the following steps: when the sequence distributor does not obtain the sequence from the database for the first time, according to the ID of the sequence which is distributed and completed for the last time, obtaining the sequence attribute and the sequence current value of the sequence with the same ID from the database, wherein the sequence attribute comprises the maximum value, the minimum value, the step length, the buffer quantity and whether circulation is supported or not; generating a sequence number according to the sequence attribute and a sequence current value; and when receiving a serial number distribution request of the sequence client, distributing the generated serial number to the sequence client. The invention can provide a global serial number implementation scheme with less coding and configuration workload.

Description

Distributed global sequence number implementation method and sequence distributor
Technical Field
The invention relates to the technical field of distributed architectures, in particular to a distributed global sequence number implementation method and a sequence distributor.
Background
This section is intended to provide a background or context to the embodiments of the invention that are recited in the claims. The description herein is not admitted to be prior art by inclusion in this section.
In a distributed architecture, the problem of how to generate the unique serial number is the problem often encountered when designing each application program in a system, especially when the database uses the sub-library and the sub-table. After the database is divided into a plurality of sub-tables, how to make each table data in each sub-table quickly obtain a unique serial number is a problem to be considered.
At present, the mainstream solutions include a snowflake algorithm, a redis global cache and the like, the snowflake algorithm needs to be developed and deployed in combination with an application program requiring a serial number, the redis global cache needs to introduce a redis component and is developed in combination with the application program and the redis component, and the workload of coding and configuration of the two schemes is very large.
Disclosure of Invention
The embodiment of the invention provides a distributed global serial number implementation method, which is used for providing a global serial number implementation scheme with less coding and configuration workload and reducing the intrusiveness of a global serial number to an application program, and is applied to at least one sequence distributor in a distributed system, wherein the distributed system further comprises a database and a sequence client, the sequence distributor is respectively connected with the database and the sequence client, and the method comprises the following steps:
When the sequence distributor does not obtain the sequence from the database for the first time, according to the ID of the sequence which is distributed and completed for the last time, obtaining the sequence attribute and the sequence current value of the sequence with the same ID from the database, wherein the sequence attribute comprises the maximum value, the minimum value, the step length, the buffer quantity and whether circulation is supported or not;
generating a sequence number according to the sequence attribute and a sequence current value;
and when receiving a serial number distribution request of the sequence client, distributing the generated serial number to the sequence client.
The embodiment of the present invention further provides a sequence distributor, which is used to provide a global sequence number implementation scheme with less coding and configuration workload, and reduce the intrusiveness of the global sequence number to an application program at the same time, at least one sequence distributor is arranged in a distributed system, the distributed system further includes a database and a sequence client, the sequence distributor is connected with the database and the sequence client respectively, and the sequence distributor includes:
the acquisition module is used for acquiring sequence attributes and sequence current values of sequences with the same ID from the database according to the ID of the sequence which is distributed and completed at the last time when the sequence distributor does not acquire the sequence from the database for the first time, wherein the sequence attributes comprise a sequence maximum value, a minimum value, a step length, a buffer number and whether circulation is supported or not;
The sequence number generation module is used for generating a sequence number according to the sequence attribute and the sequence current value acquired by the acquisition module;
and the sequence number distributing module is used for distributing the sequence number generated by the sequence number generating module to the sequence client when receiving the sequence number distributing request of the sequence client.
The embodiment of the invention also provides computer equipment which comprises a memory, a processor and a computer program which is stored on the memory and can run on the processor, wherein the processor realizes the distributed global serial number implementation method when executing the computer program.
An embodiment of the present invention further provides a computer-readable storage medium, where a computer program for executing the distributed global serial number implementation method is stored in the computer-readable storage medium.
In the embodiment of the invention, sequence attributes are defined in a database and sequence current values are recorded, a sequence distributor acquires the sequence attributes and the sequence current values from the database, generates a sequence according to the sequence attributes and the sequence current values and then distributes the sequence to a sequence client requesting a sequence number. The sequence distributor is used as an intermediate mechanism, the development of the sequence distributor does not need to combine an application program or additionally refer to other components, the application intrusiveness is low, the acquisition of the global serial number can be realized only by setting a sequence client in the application program, and the workload of coding and configuration is low; and a plurality of sequence distributors can be arranged, and sequence numbers are distributed to the sequence clients at the same time, so that high concurrence, high availability and configurability are realized.
Drawings
In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings used in the description of the embodiments or the prior art will be briefly described below, it is obvious that the drawings in the following description are only some embodiments of the present invention, and for those skilled in the art, other drawings can be obtained according to the drawings without creative efforts. In the drawings:
FIG. 1 is a schematic structural diagram of a distributed system according to an embodiment of the present invention;
FIG. 2 is a flowchart of a distributed global sequence number implementation method according to an embodiment of the present invention;
FIG. 3 is a flow chart of another method for implementing a distributed global sequence number according to an embodiment of the present invention;
FIG. 4 is a flow chart of another method for implementing a distributed global sequence number according to an embodiment of the present invention;
fig. 5 is a schematic structural diagram of a sequence distributor according to an embodiment of the present invention.
Detailed Description
In order to make the objects, technical solutions and advantages of the embodiments of the present invention more apparent, the embodiments of the present invention are further described in detail below with reference to the accompanying drawings. The exemplary embodiments and descriptions of the present invention are provided to explain the present invention, but not to limit the present invention.
The embodiment of the invention provides a distributed global serial number implementation method, which is applied to at least one sequence distributor in a distributed system, as shown in fig. 1, the distributed system further comprises a database and a sequence client, and the sequence distributor is respectively connected with the database and the sequence client. The application program accesses the global sequence acquisition service through the sequence client, the global sequence acquisition service is provided with a plurality of sequence distributors, and the number of the sequence distributors can be increased or decreased, so that dynamic expansion can be conveniently carried out to meet the requirements of high concurrency and high availability. The sequence distributor can obtain a batch of serial numbers for caching according to sequence attributes defined in the database, and finally, different algorithms are adopted to provide services for the sequence client according to whether caching is allowed or not.
As shown in fig. 2, the method includes steps 201 to 203:
step 201, when the sequence distributor does not obtain the sequence from the database for the first time, according to the ID of the sequence which is distributed and completed for the last time, obtaining the sequence attribute and the current value of the sequence of the same ID from the database.
The sequence attributes comprise a sequence maximum value, a sequence minimum value, a step size, a buffer number and whether circulation is supported or not.
The sequence attributes may be stored in the database in a table format, as shown in table one below:
watch 1
Name of field Meaning of a field
SEQUENCE_ID Sequence ID
SEQUENCE_NAME Sequence name
MIN_VALUE Sequence minimum
MAX_VALUE Sequence maximum
INCREMENT_BY Step size
CYCLE Whether or not to support looping
ORDER Whether or not to support ordering
CACHE Buffer memory quantity (greater than or equal to 0)
Wherein, the field "ORDER" in the above table one means whether or not ordering is supported, whether or not the sequence supports ordering is related to the number of caches, and if the number of caches is not equal to 0, it indicates that the sequence is not strictly incremented, i.e. the sequence does not support ordering; if the number of buffers is equal to 0, it indicates that the sequence is strictly increasing, i.e. the sequence supports ordering.
The sequence attribute is set by a developer of the sequence distributor, which can be defined according to the requirements of service scenarios, for example, for some scenarios supporting high concurrency, if the sequence does not support caching, it may be difficult to meet the requirements of the sequence attribute for the sequence number, so the sequence attribute is set to support caching, that is, the number of caches is set to a value greater than 0; for some scenarios with less sequence number requirements, the supporting cache or the non-supporting cache may be set.
The current value of the sequence changes along with the distribution of the sequence, and the current value of the sequence can be saved in a sequence runtime table, as shown in the following table two:
Watch two
Name of field Meaning of a field
SEQUENCE_ID Sequence ID
SERV_IP Distributing service IP addresses
CURRENT Sequence current value of written disk
Wherein, the distribution service IP address is the IP address of the sequence distributor.
In consideration of the existence of the multi-sequence distributor, when the ID of a sequence is acquired, the sequence distributor requests the database to perform locking processing on the sequence corresponding to the ID to prevent the multi-sequence distributor from acquiring a repeated sequence value. Wherein the locking process can be implemented by a locking mechanism in the database.
Step 202, generating a sequence number according to the sequence attribute and the sequence current value.
The sequence current value may not be equal to the sequence maximum value, but may be equal to the sequence maximum value, and for both cases, the sequence number is generated as follows:
1. when the current value of the sequence is not equal to the maximum value of the sequence
If the buffer quantity is not equal to 0, the current value of the sequence is used as an initial value, and the sequence number less than or equal to the buffer quantity is generated according to the step length.
Specifically, when the number of sequence numbers generated from the sequence current value to the sequence maximum value is greater than or equal to the buffer number, the sequence numbers are generated according to the step size from the sequence current value until the number of generated sequence numbers is equal to the buffer number.
Illustratively, the sequence current value is 6, the sequence maximum value is 10, the step size is 1, the buffer number is 2, 4 sequence numbers of 7, 8, 9 and 10 can be generated from 6 to 10, and the sequence number is larger than the buffer number 2, so that 2 sequence numbers of 7 and 8 are generated from 6.
When the number of the sequence numbers generated from the current value of the sequence to the maximum value of the sequence is less than the buffer number, if the sequence supports circulation, the sequence numbers are generated from the minimum value of the sequence until the total number of the generated sequence numbers is equal to the buffer number; if the sequence does not support looping, all sequence numbers from the current value of the sequence to before the maximum value of the sequence are generated by the step size.
Illustratively, the minimum value of the sequence is 3, the current value of the sequence is 8, the maximum value of the sequence is 10, the step size is 1, the number of buffers is 3, 2 sequence numbers of 9 and 10 can be generated from 8 to 10, and the sequence numbers are less than the number of buffers 3, so 2 sequence numbers of 9 and 10 are generated from 8; if the sequence supports looping, then after 9 and 10, sequence number 3 is regenerated; if the sequence does not support looping, only 9 and 10 are generated.
If the number of the cache is equal to 0, taking the current value of the sequence as an initial value, and expanding the current value of the sequence by a specified multiple to obtain a first multiple value; if the first multiple value is smaller than or equal to the maximum value of the sequence, generating the sequence number from the initial value according to the step length until the maximum value in the generated sequence number is smaller than or equal to the first multiple value; and if the first multiple value is larger than the maximum value of the sequence, generating the sequence number from the initial value according to the step length until the maximum value in the generated sequence number is smaller than or equal to the maximum value of the sequence.
Wherein the specified multiple can be set by a developer of the sequence distributor and modified when the sequence distributor stops working.
Illustratively, the specified multiple is 10, the maximum value of the sequence is 800, and the minimum value of the sequence is 50,. When the current value of the sequence is 50, 50 is enlarged by 10 times to 500, and 500 is smaller than 800, then the sequence numbers between 50 and 500 are generated in sequence according to the step size from 50. When the current value of the sequence is 500, the 500 is expanded by 10 times to 5000, and 5000 is larger than 800, and the sequence numbers between 500 and 800 are sequentially generated according to the step size from 500.
It should be noted that, by adopting the mechanism for expanding the current value by the specified multiple, a part of the sequence number may be cached in the content, and it is not necessary to request the database to obtain the current value and the sequence attribute of the sequence each time the sequence number is generated, so that the IO overhead may be reduced, and the performance of the distributed system may be improved.
2. When the current value of the sequence is equal to the maximum value of the sequence
If the sequence supports circulation and the buffer quantity is not equal to 0, the minimum value of the sequence is used as an initial value, and the sequence numbers with the quantity equal to the buffer quantity are generated according to the step length.
And secondly, if the sequence supports circulation and the buffer quantity is equal to 0, taking the minimum value of the sequence as an initial value, expanding the initial value of the sequence by a specified multiple to obtain a second multiplier, and generating the sequence number from the initial value according to the step length until the maximum value in the generated sequence number is less than or equal to the second multiplier.
And thirdly, if the sequence does not support circulation, acquiring the sequence attribute and the sequence current value of the new ID sequence from the database, and generating the sequence number according to the sequence attribute and the sequence current value.
After the sequence numbers are generated according to the sequence attributes and the sequence current values, the sequence number generated at the last of each sequence can be determined as a new sequence current value of the sequence, and the new sequence current value is sent to the database so that the database replaces the sequence current value recorded in the database by the new sequence current value.
The sequence numbers are sequentially generated from an initial value according to the step size, and the last generated sequence number can be recorded into a database as a new sequence current value of the sequence. For a sequence which does not support circulation or a sequence which supports circulation but does not have circulation in the process of generating the sequence at this time, the last sequence number generated, namely the largest sequence number is the current value of a new sequence; and for the sequences which support circulation and circulate in the process of generating the sequence at this time, taking the last sequence number generated after circulation as a new sequence current value. For example, when the sequence numbers 1, 2, and 3 are generated from the sequence minimum value 1 after the sequence numbers 7 and 8 are generated, 3 is set as a new sequence current value.
And step 203, distributing the generated serial number to the sequence client when receiving the serial number distribution request of the sequence client.
Specifically, if the number of the sequence buffers is not equal to 0, the sequence buffers are counted by using AtomicLong, and the generated sequence numbers are sequentially distributed to the sequence client according to the sequence of the sequence numbers increasing from small to large. If the buffer quantity of the sequence is not equal to 0, a sequence number is randomly distributed to the sequence client by using a remove method, and the sequence number stored in the sequence distributor is deleted.
When using AtomicLong for counting, the current value of the sequence may be assigned to the variable atom, counting may be started from the current value of the sequence, and the sum of the current value of the sequence and the step size may be used as the first sequence number generated.
Specifically, the program assigned to the variable atom is as follows:
atomic.set(n)
furthermore, the maximum value of the sequence generated this time, i.e. the first multiple value or the sequence maximum value:
currentMaxId ═ n × M or currentMaxId ═ K
Where n represents the current value of the sequence, M represents the specified multiple, and K represents the maximum value of the sequence.
At this time, when a request is made to acquire the sequence value, whether the maximum value is reached or not is judged, and if the maximum value is not reached, the value is taken down and returned.
atomic.incrementAndGet();
If the maximum value is reached, blocking other request threads, obtaining the sequence attribute and the current sequence value of the next ID sequence by the database, updating the first value and the maximum value of the newly obtained sequence in the memory, updating a sequence running period table in the database, and finally returning the next sequence number.
The remove method is a method for editing a List in JAVA, and acquires and returns a deleted value while randomly deleting. When the sequence supports caching, the generated sequence number may be stored in a List, as follows:
Map:[IP1,list(1,2,3,4,5)]
[IP2,list(6,7,8,9,10)]
[IP3,list(11,12,13,14,15)]
wherein the IP is the IP address of the sequence distributor.
When the embodiment of the present invention is implemented by using AtomicLong and remove methods, a global sequence number implementation mechanism in the embodiment of the present invention is shown in fig. 3. It should be noted that, in fig. 3, whether to strictly increment, that is, whether the number of buffers is equal to 0 or not is determined.
Referring to fig. 4, the embodiment of the present invention can also be implemented as steps 401 to 403:
step 401, when the sequence distributor first obtains the sequence from the database, randomly obtaining the ID, the sequence attribute and the current value of the sequence from the database.
Step 402, generating a sequence number according to the sequence attribute and the sequence current value.
Step 403, when receiving the serial number distribution request from the sequence client, distributing the generated serial number to the sequence client.
It should be noted that the implementation of step 402 and step 403 is the same as that of step 202 and step 203, and is not described again.
In the embodiment of the invention, sequence attributes are defined in a database and sequence current values are recorded, a sequence distributor acquires the sequence attributes and the sequence current values from the database, generates a sequence according to the sequence attributes and the sequence current values and then distributes the sequence to a sequence client requesting a sequence number. The sequence distributor is used as an intermediate mechanism, development of the sequence distributor does not need to combine with an application program or additionally refer to other components, invasion to the application is low, the global sequence number can be obtained only by arranging a sequence client in the application program, a plurality of sequence distributors can be arranged, the sequence numbers are distributed to the sequence client, and high concurrence, high availability and configurability are achieved.
The embodiment of the invention also provides a sequence distributor, which is described in the following embodiment. Referring to fig. 1, at least one sequence distributor is disposed in a distributed system, the distributed system further includes a database and a sequence client, and the sequence distributor is connected to the database and the sequence client, respectively. Because the principle of the sequence distributor for solving the problem is similar to the distributed global sequence number implementation method, the implementation of the device can refer to the implementation of the distributed global sequence number implementation method, and repeated parts are not described again.
As shown in fig. 5, the sequence distributor includes an acquisition module 501, a serial number generation module 502, and a serial number distribution module 503.
The obtaining module 501 is configured to, when the sequence distributor does not obtain the sequence from the database for the first time, obtain, according to the ID of the sequence that is distributed for the last time, a sequence attribute and a sequence current value of the sequence with the same ID from the database, where the sequence attribute includes a sequence maximum value, a sequence minimum value, a step size, a buffer number, and whether to support circulation.
A sequence number generating module 502, configured to generate a sequence number according to the sequence attribute and the sequence current value acquired by the acquiring module 501.
A serial number distributing module 503, configured to distribute the serial number generated by the serial number generating module 502 to the sequence client when receiving the serial number distribution request of the sequence client.
In an implementation manner of the embodiment of the present invention, when the sequence current value is not equal to the sequence maximum value, the sequence number generating module 502 is configured to:
if the buffer quantity is not equal to 0, the current value of the sequence is used as an initial value, and a sequence number less than or equal to the buffer quantity is generated according to the step length;
if the cache number is equal to 0, taking the current value of the sequence as an initial value, and expanding the current value of the sequence by a specified multiple to obtain a first multiple value; if the first multiple value is smaller than or equal to the maximum value of the sequence, generating the sequence number from the initial value according to the step length until the maximum value in the generated sequence number is smaller than or equal to the first multiple value; and if the first multiple value is larger than the maximum value of the sequence, generating the sequence number from the initial value according to the step length until the maximum value in the generated sequence number is smaller than or equal to the maximum value of the sequence.
In an implementation manner of the embodiment of the present invention, the serial number generating module 502 is configured to:
when the number of the sequence numbers generated from the sequence current value to the sequence maximum value is larger than or equal to the cache number, the sequence numbers are generated according to the step length from the sequence current value until the number of the generated sequence numbers is equal to the cache number;
when the number of the sequence numbers generated from the current value of the sequence to the maximum value of the sequence is less than the buffer number, if the sequence supports circulation, the sequence numbers are generated from the minimum value of the sequence until the total number of the generated sequence numbers is equal to the buffer number; if the sequence does not support looping, all sequence numbers from the current value of the sequence to before the maximum value of the sequence are generated by the step size.
In an implementation manner of the embodiment of the present invention, when the sequence current value is equal to the sequence maximum value, the sequence number generating module 502 is configured to:
if the sequence supports circulation and the buffer quantity is not equal to 0, the minimum value of the sequence is used as an initial value, and the sequence numbers with the quantity equal to the buffer quantity are generated according to the step length;
if the sequence supports circulation and the buffer quantity is equal to 0, taking the minimum value of the sequence as an initial value, expanding the initial value of the sequence by a specified multiple to obtain a second multiplier, and generating the sequence number from the initial value according to the step length until the maximum value in the generated sequence number is less than or equal to the second multiplier;
And if the sequence does not support circulation, acquiring the sequence attribute and the sequence current value of the new ID sequence from the database, and generating the sequence number according to the sequence attribute and the sequence current value.
In one implementation of an embodiment of the present invention,
the obtaining module 501 is further configured to randomly obtain the ID, the sequence attribute, and the current value of the sequence from the database when the sequence distributor first obtains the sequence from the database.
The sequence number generation module 502 is further configured to generate a sequence number according to the sequence attribute and the sequence current value.
The sequence number distributing module 503 is further configured to distribute the generated sequence number to the sequence client when receiving a request for distributing the sequence number from the sequence client.
In one implementation manner of the embodiment of the present invention, the sequence distributor 500 further includes:
and a communication module 504, configured to determine the sequence number generated last in each sequence as a new current sequence value of the sequence, and send the new current sequence value to the database, so that the database replaces the current sequence value recorded in the database by the new current sequence value.
In an implementation manner of the embodiment of the present invention, the serial number distribution module 503 is configured to:
If the cache number of the sequence is not equal to 0, counting by using AtomicLong, and sequentially distributing the generated sequence numbers to the sequence client according to the sequence of the sequence numbers increasing from small to large;
if the buffer quantity of the sequence is not equal to 0, a sequence number is randomly distributed to the sequence client by using a remove method, and the sequence number stored in the sequence distributor is deleted.
In an implementation manner of the embodiment of the present invention, the sequence distributor further includes:
and a locking request module 505, configured to, after the obtaining module 501 obtains the ID of the sequence, request the database to perform locking processing on the sequence corresponding to the ID.
In the embodiment of the invention, sequence attributes are defined in a database and sequence current values are recorded, a sequence distributor acquires the sequence attributes and the sequence current values from the database, generates a sequence according to the sequence attributes and the sequence current values and then distributes the sequence to a sequence client requesting a sequence number. The sequence distributor is used as an intermediate mechanism, development of the sequence distributor does not need to combine with an application program or additionally refer to other components, invasion to the application is low, the global sequence number can be obtained only by arranging a sequence client in the application program, a plurality of sequence distributors can be arranged, the sequence numbers are distributed to the sequence client, and high concurrence, high availability and configurability are achieved.
The embodiment of the invention also provides computer equipment which comprises a memory, a processor and a computer program which is stored on the memory and can run on the processor, wherein the processor realizes the distributed global serial number implementation method when executing the computer program.
An embodiment of the present invention further provides a computer-readable storage medium, where a computer program for executing the distributed global serial number implementation method is stored in the computer-readable storage medium.
In the embodiment of the invention, sequence attributes are defined in a database and sequence current values are recorded, a sequence distributor acquires the sequence attributes and the sequence current values from the database, generates a sequence according to the sequence attributes and the sequence current values and then distributes the sequence to a sequence client requesting a sequence number. The sequence distributor is used as an intermediate mechanism, development of the sequence distributor does not need to combine with an application program or additionally refer to other components, invasion to the application is low, the global sequence number can be obtained only by arranging a sequence client in the application program, a plurality of sequence distributors can be arranged, the sequence numbers are distributed to the sequence client, and high concurrence, high availability and configurability are achieved.
As will be appreciated by one skilled in the art, embodiments of the present invention may be provided as a method, system, or computer program product. Accordingly, the present invention may take the form of an entirely hardware embodiment, an entirely software embodiment or an embodiment combining software and hardware aspects. Furthermore, the present invention may take the form of a computer program product embodied on one or more computer-usable storage media (including, but not limited to, disk storage, CD-ROM, optical storage, and the like) having computer-usable program code embodied therein.
The present invention is described with reference to flowchart illustrations and/or block diagrams of methods, apparatus (systems), and computer program products according to embodiments of the invention. It will be understood that each flow and/or block of the flow diagrams and/or block diagrams, and combinations of flows and/or blocks in the flow diagrams and/or block diagrams, can be implemented by computer program instructions. These computer program instructions may be provided to a processor of a general purpose computer, special purpose computer, embedded processor, or other programmable data processing apparatus to produce a machine, such that the instructions, which execute via the processor of the computer or other programmable data processing apparatus, create means for implementing the functions specified in the flowchart flow or flows and/or block diagram block or blocks.
These computer program instructions may also be stored in a computer-readable memory that can direct a computer or other programmable data processing apparatus to function in a particular manner, such that the instructions stored in the computer-readable memory produce an article of manufacture including instruction means which implement the function specified in the flowchart flow or flows and/or block diagram block or blocks.
These computer program instructions may also be loaded onto a computer or other programmable data processing apparatus to cause a series of operational steps to be performed on the computer or other programmable apparatus to produce a computer implemented process such that the instructions which execute on the computer or other programmable apparatus provide steps for implementing the functions specified in the flowchart flow or flows and/or block diagram block or blocks.
The above-mentioned embodiments are intended to illustrate the objects, technical solutions and advantages of the present invention in further detail, and it should be understood that the above-mentioned embodiments are only exemplary embodiments of the present invention, and are not intended to limit the scope of the present invention, and any modifications, equivalent substitutions, improvements and the like made within the spirit and principle of the present invention should be included in the scope of the present invention.

Claims (11)

1. A distributed global serial number implementation method is applied to at least one sequence distributor in a distributed system, the distributed system further comprises a database and a sequence client, the sequence distributor is respectively connected with the database and the sequence client, and the method comprises the following steps:
when the sequence distributor does not obtain the sequence from the database for the first time, according to the ID of the sequence which is distributed and completed for the last time, obtaining the sequence attribute and the sequence current value of the sequence with the same ID from the database, wherein the sequence attribute comprises the maximum value, the minimum value, the step length, the buffer quantity and whether circulation is supported or not;
generating a sequence number according to the sequence attribute and a sequence current value;
and when receiving a serial number distribution request of the sequence client, distributing the generated serial number to the sequence client.
2. The method of claim 1, wherein generating a sequence number segment from the sequence attribute and a sequence current value when the sequence current value is not equal to a sequence maximum value comprises:
if the buffer quantity is not equal to 0, the current value of the sequence is used as an initial value, and a sequence number less than or equal to the buffer quantity is generated according to the step length;
If the cache number is equal to 0, taking the current value of the sequence as an initial value, and expanding the current value of the sequence by a specified multiple to obtain a first multiple value; if the first multiple value is smaller than or equal to the maximum value of the sequence, generating the sequence number from the initial value according to the step length until the maximum value in the generated sequence number is smaller than or equal to the first multiple value; and if the first multiple value is larger than the maximum value of the sequence, generating the sequence number from the initial value according to the step length until the maximum value in the generated sequence number is smaller than or equal to the maximum value of the sequence.
3. The method of claim 2, wherein generating sequence numbers equal to or less than the number of buffers in steps comprises:
when the number of the sequence numbers generated from the sequence current value to the sequence maximum value is larger than or equal to the cache number, the sequence numbers are generated according to the step length from the sequence current value until the number of the generated sequence numbers is equal to the cache number;
when the number of the sequence numbers generated from the current value of the sequence to the maximum value of the sequence is less than the buffer number, if the sequence supports circulation, the sequence numbers are generated from the minimum value of the sequence until the total number of the generated sequence numbers is equal to the buffer number; if the sequence does not support looping, all sequence numbers from the current value of the sequence to before the maximum value of the sequence are generated by the step size.
4. The method of claim 1, wherein generating a sequence number segment from the sequence attribute and a sequence current value when the sequence current value is equal to a sequence maximum value comprises:
if the sequence supports circulation and the buffer quantity is not equal to 0, the minimum value of the sequence is used as an initial value, and the sequence numbers with the quantity equal to the buffer quantity are generated according to the step length;
if the sequence supports circulation and the buffer quantity is equal to 0, taking the minimum value of the sequence as an initial value, expanding the initial value of the sequence by a specified multiple to obtain a second multiplier, and generating the sequence number from the initial value according to the step length until the maximum value in the generated sequence number is less than or equal to the second multiplier;
and if the sequence does not support circulation, acquiring the sequence attribute and the sequence current value of the new ID sequence from the database, and generating the sequence number according to the sequence attribute and the sequence current value.
5. The method of claim 1, further comprising:
when the sequence distributor firstly obtains the sequence from the database, randomly obtaining the ID, the sequence attribute and the current value of the sequence from the database;
generating a sequence number according to the sequence attribute and a sequence current value;
And when receiving a serial number distribution request of the sequence client, distributing the generated serial number to the sequence client.
6. The method of any of claims 1 to 5, wherein after generating a sequence number from the sequence attribute and a sequence current value, the method further comprises:
and determining the sequence number generated at the last of each sequence as a new current sequence value of the sequence, and sending the new current sequence value to the database so that the database replaces the current sequence value recorded in the database by the new current sequence value.
7. The method of any of claims 1 to 5, wherein distributing the sequence numbers in the generated sequence number segment to the sequence client comprises:
if the cache number of the sequence is not equal to 0, counting by using AtomicLong, and sequentially distributing the generated sequence numbers to the sequence client according to the sequence of the sequence numbers increasing from small to large;
if the buffer quantity of the sequence is not equal to 0, a sequence number is randomly distributed to the sequence client by using a remove method, and the sequence number stored in the sequence distributor is deleted.
8. The method according to any one of claims 1 to 5, further comprising:
And after the ID of the sequence is acquired, requesting the database to execute locking processing on the sequence corresponding to the ID.
9. A sequence distributor, characterized in that at least one sequence distributor is provided in a distributed system, the distributed system further comprising a database and a sequence client, the sequence distributor being connected to the database and the sequence client, respectively, the sequence distributor comprising:
the acquisition module is used for acquiring sequence attributes and sequence current values of sequences with the same ID from the database according to the ID of the sequence which is distributed and completed at the last time when the sequence distributor does not acquire the sequence from the database for the first time, wherein the sequence attributes comprise a sequence maximum value, a minimum value, a step length, a buffer number and whether circulation is supported or not;
the sequence number generation module is used for generating a sequence number according to the sequence attribute and the sequence current value acquired by the acquisition module;
and the sequence number distributing module is used for distributing the sequence number generated by the sequence number generating module to the sequence client when receiving the sequence number distributing request of the sequence client.
10. A computer device comprising a memory, a processor and a computer program stored on the memory and executable on the processor, wherein the processor implements the method of any one of claims 1 to 8 when executing the computer program.
11. A computer-readable storage medium, characterized in that the computer-readable storage medium stores a computer program for executing the method of any one of claims 1 to 8.
CN202010807317.1A 2020-08-12 2020-08-12 Distributed global sequence number implementation method and sequence distributor Active CN111949730B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202010807317.1A CN111949730B (en) 2020-08-12 2020-08-12 Distributed global sequence number implementation method and sequence distributor

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202010807317.1A CN111949730B (en) 2020-08-12 2020-08-12 Distributed global sequence number implementation method and sequence distributor

Publications (2)

Publication Number Publication Date
CN111949730A true CN111949730A (en) 2020-11-17
CN111949730B CN111949730B (en) 2023-09-19

Family

ID=73332344

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202010807317.1A Active CN111949730B (en) 2020-08-12 2020-08-12 Distributed global sequence number implementation method and sequence distributor

Country Status (1)

Country Link
CN (1) CN111949730B (en)

Cited By (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN113239088A (en) * 2021-04-12 2021-08-10 上海沐融信息科技有限公司 Asynchronous pre-fetch adjustable sequence number generator acquisition method and device
CN113342851A (en) * 2021-05-28 2021-09-03 北京银行股份有限公司 Sequence capacity expansion method
CN113467753A (en) * 2020-12-31 2021-10-01 易百信息技术(上海)股份有限公司 Distributed non-repetitive random sequence generation method and system
CN113641674A (en) * 2021-08-18 2021-11-12 中国银行股份有限公司 Adaptive global sequence number generation method and device
CN114281872A (en) * 2022-03-07 2022-04-05 广联达科技股份有限公司 Method, device and equipment for generating distributed serial number and readable storage medium

Citations (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN103164418A (en) * 2011-12-12 2013-06-19 阿里巴巴集团控股有限公司 Method and device for processing serial numbers
CN106940712A (en) * 2017-03-01 2017-07-11 星环信息科技(上海)有限公司 Sequence generating method and equipment
CN107247770A (en) * 2017-06-05 2017-10-13 广东亿迅科技有限公司 Global sequence's generation method and device based on zookeeper
CN109753544A (en) * 2019-01-04 2019-05-14 中国银行股份有限公司 Database sequence processing method, system and the node server based on synchrolock
US20200081877A1 (en) * 2017-04-01 2020-03-12 Beijing Jingdong Shangke Information Technology Co., Ltd. Method and apparatus for providing serial number, electronic device and readable storage medium

Patent Citations (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN103164418A (en) * 2011-12-12 2013-06-19 阿里巴巴集团控股有限公司 Method and device for processing serial numbers
CN106940712A (en) * 2017-03-01 2017-07-11 星环信息科技(上海)有限公司 Sequence generating method and equipment
US20200081877A1 (en) * 2017-04-01 2020-03-12 Beijing Jingdong Shangke Information Technology Co., Ltd. Method and apparatus for providing serial number, electronic device and readable storage medium
CN107247770A (en) * 2017-06-05 2017-10-13 广东亿迅科技有限公司 Global sequence's generation method and device based on zookeeper
CN109753544A (en) * 2019-01-04 2019-05-14 中国银行股份有限公司 Database sequence processing method, system and the node server based on synchrolock

Cited By (8)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN113467753A (en) * 2020-12-31 2021-10-01 易百信息技术(上海)股份有限公司 Distributed non-repetitive random sequence generation method and system
CN113239088A (en) * 2021-04-12 2021-08-10 上海沐融信息科技有限公司 Asynchronous pre-fetch adjustable sequence number generator acquisition method and device
CN113342851A (en) * 2021-05-28 2021-09-03 北京银行股份有限公司 Sequence capacity expansion method
CN113342851B (en) * 2021-05-28 2024-04-02 北京银行股份有限公司 Sequence capacity expansion method
CN113641674A (en) * 2021-08-18 2021-11-12 中国银行股份有限公司 Adaptive global sequence number generation method and device
CN113641674B (en) * 2021-08-18 2024-02-23 中国银行股份有限公司 Self-adaptive global sequence number generation method and device
CN114281872A (en) * 2022-03-07 2022-04-05 广联达科技股份有限公司 Method, device and equipment for generating distributed serial number and readable storage medium
CN114281872B (en) * 2022-03-07 2022-05-24 广联达科技股份有限公司 Method, device and equipment for generating distributed serial number and readable storage medium

Also Published As

Publication number Publication date
CN111949730B (en) 2023-09-19

Similar Documents

Publication Publication Date Title
CN111949730A (en) Distributed global sequence number implementation method and sequence distributor
US20140143365A1 (en) Rdma-optimized high-performance distributed cache
US20030217197A1 (en) Preparation for software on demand system
CN107026879B (en) Data caching method and background application system
JP2019521576A (en) Maintaining Messaging System Persistence
US8666958B2 (en) Approaches to reducing lock communications in a shared disk database
CN109388626B (en) Method and apparatus for assigning numbers to services
CN105653198A (en) Data processing method and device
JP2005276198A (en) System and method for balancing multiple memory buffer sizes
CN112988757B (en) Method, device, equipment and medium for generating global unique ID of distributed system
US20150019673A1 (en) Distributed caching in a communication network
CN111371585A (en) Configuration method and device for CDN node
CN106940712B (en) Sequence generation method and device
CN109325194B (en) Page access method, device, equipment and storage medium
CN112948025B (en) Data loading method and device, storage medium, computing equipment and computing system
US8341368B2 (en) Automatic reallocation of structured external storage structures
CN112328693A (en) Block synchronization method, device and storage medium
CN110781137A (en) Directory reading method and device for distributed system, server and storage medium
CN114390069B (en) Data access method, system, equipment and storage medium based on distributed cache
CN105847395A (en) Cache file processing method and device
CN111177109A (en) Method and device for deleting overdue key
CN112115393B (en) Page loading method, system, device and medium
CN112019343B (en) OpenStack token optimization method and system
CN113672591A (en) Data migration method, system, storage medium and electronic device
CN112732667A (en) Usability enhancing method and system for distributed file system

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