CN112035547A - Serial number generation method and device, electronic equipment and storage medium - Google Patents

Serial number generation method and device, electronic equipment and storage medium Download PDF

Info

Publication number
CN112035547A
CN112035547A CN202010899153.XA CN202010899153A CN112035547A CN 112035547 A CN112035547 A CN 112035547A CN 202010899153 A CN202010899153 A CN 202010899153A CN 112035547 A CN112035547 A CN 112035547A
Authority
CN
China
Prior art keywords
sequence number
serial number
historical
counting
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.)
Pending
Application number
CN202010899153.XA
Other languages
Chinese (zh)
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.)
WeBank Co Ltd
Original Assignee
WeBank 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 WeBank Co Ltd filed Critical WeBank Co Ltd
Priority to CN202010899153.XA priority Critical patent/CN112035547A/en
Publication of CN112035547A publication Critical patent/CN112035547A/en
Pending legal-status Critical Current

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/24Querying
    • G06F16/245Query processing
    • G06F16/2458Special types of queries, e.g. statistical queries, fuzzy queries or distributed queries
    • G06F16/2474Sequence data queries, e.g. querying versioned data
    • 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/24Querying
    • G06F16/245Query processing
    • G06F16/2458Special types of queries, e.g. statistical queries, fuzzy queries or distributed queries
    • G06F16/2471Distributed queries
    • 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
    • G06QINFORMATION AND COMMUNICATION TECHNOLOGY [ICT] SPECIALLY ADAPTED FOR ADMINISTRATIVE, COMMERCIAL, FINANCIAL, MANAGERIAL OR SUPERVISORY PURPOSES; SYSTEMS OR METHODS SPECIALLY ADAPTED FOR ADMINISTRATIVE, COMMERCIAL, FINANCIAL, MANAGERIAL OR SUPERVISORY PURPOSES, NOT OTHERWISE PROVIDED FOR
    • G06Q40/00Finance; Insurance; Tax strategies; Processing of corporate or income taxes
    • G06Q40/04Trading; Exchange, e.g. stocks, commodities, derivatives or currency exchange

Landscapes

  • Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • Physics & Mathematics (AREA)
  • Databases & Information Systems (AREA)
  • General Physics & Mathematics (AREA)
  • Business, Economics & Management (AREA)
  • General Engineering & Computer Science (AREA)
  • Data Mining & Analysis (AREA)
  • Software Systems (AREA)
  • Finance (AREA)
  • Probability & Statistics with Applications (AREA)
  • Mathematical Physics (AREA)
  • Computational Linguistics (AREA)
  • Fuzzy Systems (AREA)
  • Accounting & Taxation (AREA)
  • Computing Systems (AREA)
  • Development Economics (AREA)
  • Economics (AREA)
  • Marketing (AREA)
  • Strategic Management (AREA)
  • Technology Law (AREA)
  • General Business, Economics & Management (AREA)
  • Computer And Data Communications (AREA)

Abstract

The embodiment of the application provides a method and a device for generating a serial number, electronic equipment and a storage medium, and the method comprises the steps of obtaining a serial number generation request initiated by a target service, wherein the serial number generation request comprises a node identifier corresponding to the target service; generating a current counting serial number according to a historical counting serial number, wherein the historical counting serial number is the counting serial number generated in response to the last serial number generation request; and generating a target serial number according to the current timestamp, the current counting serial number and the node identifier, wherein the target serial number is used for uniquely identifying the target service. In the scheme, the counting sequence number is irrelevant to the timestamp, namely when the time rolls back, the counting sequence number is continuously increased and is not reset to the minimum value, so that the sequence number generated by the method can tolerate certain time to roll back.

Description

Serial number generation method and device, electronic equipment and storage medium
Technical Field
The embodiment of the application relates to the technical field of computers, in particular to a serial number generation method and device, electronic equipment and a storage medium.
Background
With the rapid development of computer technology, more and more technologies are applied in the financial field, and the traditional financial industry is gradually changing to financial technology (Fintech), but higher requirements are also put forward on the technologies due to the requirements of the financial industry on safety and real-time performance.
For a distributed computer system for supporting various financial services, a globally unique serial number needs to be generated to uniquely identify a large amount of transaction information in the distributed computer system. The common serial number generation method comprises a Snowflake (Snowflake) algorithm.
However, the existing snowflake algorithm has the problem that the generated serial number is not unique when time rollback is encountered.
Disclosure of Invention
The embodiment of the application provides a serial number generation method, a serial number generation device, electronic equipment and a storage medium, so as to provide a serial number generation scheme supporting rollback within a certain time and improve the generation efficiency under the high concurrency condition.
In a first aspect, the present application provides a method for generating a serial number, including:
acquiring a sequence number generation request initiated by a target service, wherein the sequence number generation request comprises a node identifier corresponding to the target service;
generating a current counting serial number according to a historical counting serial number, wherein the historical counting serial number is the counting serial number generated in response to the last serial number generation request;
and generating a target serial number according to the current timestamp, the current counting serial number and the node identifier, wherein the target serial number is used for uniquely identifying the target service.
Optionally, before generating the current count serial number according to the historical count serial number, the method further includes:
selecting a target sequence number generator from a plurality of sequence number generators according to a thread identifier, wherein the thread identifier is an identifier of a thread processing a sequence number generation request;
and acquiring the historical counting sequence number generated by the target sequence number generator.
Optionally, selecting a target sequence number generator from the plurality of sequence number generators according to the thread identifier specifically includes:
performing hash operation on the thread identifier to obtain a thread hash value;
and taking the sequence number generator with the number being the thread hash value as a target sequence number generator.
Optionally, performing hash calculation on the thread identifier to obtain a thread hash value, specifically including:
determining a hash parameter according to the total number of the sequence number generators;
and performing remainder operation on the thread identifier and the hash parameter to determine a thread hash value.
Optionally, the total number of sequence number generators is 2nWherein n is an integer.
Optionally, a plurality of sequence number generators are stored in the ring buffer.
In the scheme, the target sequence number generator is selected from the sequence number generators through the thread identifier, so that the multiple threads are respectively distributed to the corresponding sequence number generators to be processed, and the number of threads competing for the same sequence number generator is reduced.
Optionally, the historical count sequence number includes a thread hash value and an incremented sequence number.
Optionally, generating the current count serial number according to the historical count serial number specifically includes:
when the maximum value of the increment number is 2nWhen the number is-1, performing incremental operation on the historical incremental sequence number to obtain a middle incremental sequence number;
carrying out bitwise AND operation on the intermediate incremental sequence number and the maximum value of the incremental sequence number to obtain the current incremental sequence number; wherein n is not less than 0 and n is an integer.
In the scheme, the middle increment sequence number and the maximum value of the increment sequence number are subjected to AND calculation, the increment sequence number does not need to be reset, and automatic circulation of the increment sequence number can be realized.
Optionally, generating the current count serial number according to the historical count serial number specifically includes:
judging whether the historical increasing sequence number reaches the maximum value of the increasing sequence number;
if so, the current incremental sequence number is the minimum value of the incremental sequence numbers;
if not, carrying out incremental operation on the historical incremental sequence number to obtain the current incremental sequence number.
Optionally, performing an increment operation on the historical increment sequence number specifically includes:
carrying out incremental operation on the historical incremental sequence number by using a self-increment instruction in an atomic operation class;
wherein the target sequence number generator comprises a selfjoin instruction of the atomic operation class.
In the scheme, the current counting sequence number is generated by using the atomic operation, so that the efficiency of generating the sequence number under the condition of high concurrent requests is improved.
Optionally, the node identifier includes a machine identifier and a program identifier corresponding to the target service.
In a second aspect, the present application provides a serial number generation apparatus, including:
the system comprises an acquisition module, a processing module and a processing module, wherein the acquisition module is used for acquiring a serial number generation request initiated by a target service, and the serial number generation request comprises a node identifier corresponding to the target service;
the processing module is used for generating a current counting serial number according to a historical counting serial number, wherein the historical counting serial number is the counting serial number generated in response to the last serial number generation request;
the processing module is further used for generating a target serial number according to the current timestamp, the current counting serial number and the node identifier, and the target serial number is used for uniquely identifying the target service.
In a third aspect, the present application provides an electronic device, comprising:
a memory for storing a program;
a processor for executing the program stored in the memory, the processor being adapted to perform the method of generating a serial number according to the first aspect and the alternative when the program is executed.
In a fourth aspect, the present application provides a computer-readable storage medium having computer-executable instructions stored thereon, which when executed by a processor, are configured to implement the serial number generation method according to the first aspect and the alternative.
The embodiment of the application provides a serial number generation method, a serial number generation device, an electronic device and a storage medium. The application makes the counting sequence number irrelevant with the time stamp, namely the counting sequence number can be continuously increased when the time rolls back, but not reset to the minimum value, and the generated sequence number can tolerate a certain time roll back. In addition, a target sequence number generator is selected from the sequence number generators through the thread identifier, the multiple threads are respectively distributed to the corresponding sequence number generators to be processed, and the number of threads competing for the same sequence number generator is reduced.
Drawings
Fig. 1 is a schematic structural diagram of a serial number generation system according to an embodiment of the present application;
fig. 2 is a schematic flowchart of a serial number generation method according to another embodiment of the present application;
FIG. 3 is a schematic diagram of a time-tolerant rollback according to another embodiment of the present application;
FIG. 4 is a schematic diagram illustrating a thread identifier ring drop according to another embodiment of the present disclosure;
fig. 5 is a schematic flowchart of a serial number generation method according to another embodiment of the present application;
fig. 6 is a schematic structural diagram of a serial number generation apparatus according to another embodiment of the present application;
fig. 7 is a schematic structural diagram of an electronic device according to another embodiment of the present application.
Detailed Description
In order to make the objects, technical solutions and advantages of the embodiments of the present application clearer, the technical solutions in the embodiments of the present application will be clearly and completely described below with reference to the drawings in the embodiments of the present application, and it is obvious that the described embodiments are some embodiments of the present application, but not all embodiments. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present application.
With the development of computer technology, distributed computer systems have gone deep into various industries, such as: computer systems used in the financial industry to support various financial transactions.
In a distributed computer system, a globally unique serial number needs to be generated to uniquely identify a large amount of information in the computer system. Taking a computer system for financial transactions as an example, the computer system needs to generate a large number of trade orders and, correspondingly, a large number of serial numbers to uniquely identify the trade orders. When a server in the computer system receives a request for generating a trade order, the server also correspondingly receives a request for generating a serial number. The server may trigger generation of the request for generating the serial number after receiving the request for generating the trade order. After receiving the serial number generation request, the server generates the serial number through a corresponding serial number generation method.
Common methods of generating sequence numbers include the Snowflake (Snowflake) algorithm. The generated sequence number includes a timestamp, a machine identification, and a count sequence number. When the snowflake algorithm is operated, judging whether the current time stamp is the same as the time stamp of the last operation, if so, increasing 1 to the counting serial number of the last operation, and determining the current technical serial number. If not, the counting serial number is set to be 1.
However, when the time is rolled back, that is, the time is adjusted by a synchronous clock or the like, the current timestamp is rolled back to a certain historical timestamp, the current timestamp is different from the timestamp in the last operation, the count sequence number is set to 1, and the sequence number of the count sequence number below the historical timestamp, which is 1, is already used, so that the problem of sequence number duplication occurs.
The embodiment of the application provides a serial number generation method and device, electronic equipment and a storage medium. The sequence number generation scheme is used for supporting rollback within a certain time, and generation efficiency under a high concurrency condition is improved. The invention conception of the application is as follows: the counting sequence number is unrelated to the timestamp, namely the counting sequence number is continuously increased when the time rolls back and is not reset to the minimum value, so that the sequence number generating method provided by the application can tolerate the roll back within a certain time. In addition, the target sequence number generator is selected from the sequence number generators through the thread identifier, the multiple threads are respectively distributed to the corresponding sequence number generators to be processed, the number of threads competing for the same sequence number generator is reduced, and the efficiency of generating the sequence number under the condition of high concurrent requests is improved.
As shown in fig. 1, an embodiment of the present application provides a distributed computer system, which is applied to the serial number generation method described in the following embodiment. The serial number generation system 100 includes a plurality of clients 101 and a plurality of servers 102. When a program of a client 101 (i.e., a node) in the distributed computer system needs to obtain a serial number, the program threads compete, and the thread that successfully competes enters the synchronization method, which triggers a serial number generation request. The server 102 receives the sequence number generation request and generates a corresponding target sequence number in response to the sequence number generation request.
As shown in fig. 2, another embodiment of the present application provides a serial number generation method, which includes the following steps:
s201, the server obtains a serial number generation request initiated by the target service.
When a program corresponding to a certain target service in a certain node in the distributed computer system needs to acquire a serial number, competition is performed through threads of the program, the threads which succeed in the competition enter a synchronization method, and at the moment, a serial number generation request is triggered. The sequence number generation request can be generated by triggering after the server receives the corresponding business document generation request, or the corresponding business document generation request and the sequence number generation request can be simultaneously received. The business document generation request can be sent by a client or a server.
The sequence number generation request is used for requesting generation of a target sequence number. The sequence number generation request comprises a node identification corresponding to the target service. I.e. the node identity of the node where the target service corresponding program is located. The node identification is used to represent the uniqueness of the distributed hierarchy. The node identification is configuration information that is confirmed when the distributed computer system is deployed. The node identification comprises machine logic coding and application logic coding. The machine logic is encoded to represent a client device. The application logic encoding is used to identify an application instance. The generation strategies of the machine logic code and the application logic code are not interfered with each other and are not perceived mutually.
To illustrate the node identification, the machine logic is encoded as 4 bits, the first bit represents the environment, if the first bit is a number, the production environment, if the first bit is a letter, the test environment, and the second and third bits represent the machine room and machine number, respectively. The last bit is 0, which represents the physical machine, and the last bit is any number from 1 to 9, which represents the virtual machine.
The application logic code can be applied according to the system, such as a payment system with a 3-start followed by a 4-bit serial number, an intermediate service system with a 4-start followed by a 4-bit serial number, and the like. The machine logic code is 1S50, the application logic code is 31234, and the node is identified as 1S 5031234.
S202, the server generates a current counting sequence number according to the historical counting sequence number.
The counting sequence number is a sequence number which is increased in a cycle, and when the counting sequence number is increased to the maximum value of the counting sequence number, the counting sequence number is circulated to the minimum value of the counting sequence number. For example: the range of the counting serial number is 0-1023, when the counting serial number is 0-1022, the counting serial number obtained next time is increased by 1 on the current counting serial number, and when the counting serial number is 1023, the counting serial number obtained next time is 0.
The history count sequence number is generated in response to the last sequence number generation request. And when the historical counting sequence number is the maximum value of the counting sequence number, setting the current counting sequence number as the minimum value of the counting sequence number. And when the historical counting serial number is smaller than the maximum value of the counting serial number, the current counting serial number is the value obtained by adding 1 to the historical counting serial number.
For example: the range of the counting serial number is 0-1023, and when the historical counting serial number is 1012, the current counting serial number is 1013. When the history count number is 1023, the current count number is 0.
And S204, the server generates a target sequence number according to the current timestamp, the current counting sequence number and the node identification.
Where the current timestamp is represented using yyymmddhhmmss, yyyy represents a 4-bit year, MM represents a 2-bit month, dd represents a 2-bit date, HH represents a 2-bit clock, MM represents a 2-bit minute, and ss represents a 2-bit second. The use of yyyyMMddHHmmss indicates increased readability of the sequence numbers, which may also be fragmented on the time axis.
And after the current timestamp, the current counting sequence number and the node identification are obtained, combining the current timestamp, the current counting sequence number and the node identification to obtain a target sequence number.
Preferably, the node identifier, the current timestamp and the current count sequence number are sequentially spliced into a target sequence number. For example: the node identifier is 1S5031234, the current timestamp is 20200824103000, and the current count sequence number is 1013, then the target sequence number is 1S 5031234-20200824103000-. "-" indicates a separator, not belonging to the composition of the sequence number.
Since the count sequence number is independent of the timestamp, i.e., when the time rolls back, the count sequence number continues to increment rather than being reset to a minimum value, generating the sequence number can tolerate a certain time roll back. Wherein the maximum rollback time tolerated by the generated target sequence number is obtained according to the following formula.
Maximum rollback time-count number maximum/throughput per second
The following illustrates the maximum rollback time, and as shown in fig. 3, it is assumed that the maximum value of the count number is 1000, the throughput per second is 25, and the maximum rollback time is 40 s.
When the rollback time is 20s, that is, the time is rolled back to 20s at 40s, the count number is 751, and the count number is 251 at the last 20s, no duplication occurs. When the rollback time is 40s, that is, when the time rolls back to 0s at 40s, the count number is 751, and when the count number is 751 at the previous 0s, repetition occurs.
In the sequence number generation method provided in the embodiment of the present application, since the count sequence number is not related to the timestamp, that is, when the time rolls back, the count sequence number continues to be incremented instead of being reset to the minimum value, so that the generated sequence number can tolerate a certain time of rolling back.
Another embodiment of the present application provides a method for generating a serial number, including the steps of:
s301, the server obtains a sequence number generation request initiated by the target service.
When a program corresponding to a certain target service in a client needs to acquire a serial number, competition is performed through threads of the program, the threads which succeed in the competition enter a synchronization method, and at the moment, a serial number generation request is triggered. The sequence number generation request includes a thread identification to process the sequence number generation request. When processing a sequence number generation request, the identification of the thread that processes the sequence number generation request can be obtained from the instruction thread.
S302, the server obtains a history counting sequence number.
Wherein the count number is generated using a number generator. The sequence number generator is composed of a plurality of instructions. And is provided with multiple sequence number generators to accommodate high concurrency scenarios. The plurality of clients send sequence number generation requests, and when responding to the sequence number generation request sent by each client, a target sequence number generator needs to be selected from the plurality of sequence number generators to generate a corresponding current counting sequence number. That is, there is a case where the same sequence number generator is contended in order to respond to a plurality of sequence number generation requests.
The following describes a procedure of acquiring the history count number. A target sequence number generator is selected from the plurality of sequence number generators based on the thread identification. And acquiring the historical counting sequence number generated by the target sequence number generator.
Wherein, selecting a target sequence number generator from the plurality of sequence number generators according to the thread identifier specifically comprises the following steps: and performing remainder hash calculation on the thread identifier to obtain a thread hash value. And taking the sequence number generator with the number being the thread hash value as a target sequence number generator.
To identify the count sequence number generated by each sequence number generator, the number of the corresponding sequence number generator, i.e., the thread hash value, is incremented in each count sequence number. Thus, the historical count sequence number includes the thread hash value and the incremented sequence number.
When the thread identification is subjected to remainder hash calculation, hash parameters are generated according to the total number of the sequence number generators, and then the thread identification and the hash parameters are subjected to remainder calculation to obtain thread hash values.
Preferably, the total number of sequence number generators is 2nCan directly represent a numerical value of 2nAnd bitwise AND-ing the binary code of the-1 and the thread identifier to obtain a thread hash value.
Preferably, a plurality of sequence number generators are stored in the ring buffer.
The following illustrates the process of obtaining the target sequence number generator, as shown in fig. 4, 8 instances of the generated sequence number generator are placed in an array of length 8, with the sequence number generator numbering from 0. The array is stored in the ring cache, and then the array can be regarded as a ring which is connected end to end. And generating a thread of the request aiming at different processing serial numbers, and performing ring dropping according to the thread identification. That is, the thread hash value is generated according to the following instruction thread. Taking the thread identifiers 1, 5, 11, and 22 as examples, the hash values of the generated threads are 1, 5, 3, and 6, respectively, and correspond to the 1 st, 5 th, 3 rd, and 6 th sequence number generators, respectively.
S303, the server generates a current counting sequence number according to the historical counting sequence number.
After the target sequence number generator is determined, the target sequence number generator generates a current counting sequence number according to the historical counting sequence number.
Preferably, the thread processing the target sequence number generator is located in a different cache line than the threads processing other sequence number generators to eliminate the false sharing problem.
The current counting sequence number also comprises a thread hash value and a current increment sequence number. Incrementing the increment sequence number in the next cycle may be accomplished by resetting the increment sequence number to the increment sequence number minimum when the increment sequence number exceeds the increment sequence number maximum. Specifically, the current count number is generated from the historical count number in the following manner. And judging whether the historical increasing sequence number reaches the maximum value of the increasing sequence number, and setting the current increasing sequence number as the minimum value of the increasing sequence number when the historical increasing sequence number reaches the maximum value of the increasing sequence number. And when the historical increasing sequence number is smaller than the maximum value of the counting sequence number, carrying out increasing operation on the historical increasing sequence number to obtain the current increasing sequence number. And combining the increment sequence number and the thread hash value to obtain the current counting sequence number.
Preferably, when the maximum value of the increment number is 2nAt-1, the increment sequence number can be advanced to the next cycle in a non-boundary polling mode. Wherein n is not less than 0 and n is an integer.
Specifically, the current count number is generated from the historical count number in the following manner. And carrying out incremental operation on the historical incremental sequence number to obtain an intermediate incremental sequence number, and carrying out bitwise AND operation on the intermediate incremental sequence number and the maximum value of the incremental sequence number to obtain the current incremental sequence number.
Preferably, the target sequence number generator includes a self-increment instruction of the atomic operation class, and the self-increment instruction in the atomic operation class is used for performing incremental operation on the historical incremental sequence number, so as to improve the efficiency of generating the current incremental sequence number and further improve the efficiency of generating the sequence number.
The following describes a process for implementing non-boundary-crossing polling, assuming that the maximum value of the increment sequence number val is 1023, and the atomic integer. The following description of the atomic operation class of the add-on instruction generates the three cases in which the intermediate increment sequence number is less than 1023, between 1023 and the overflow boundary value, and the overflow boundary. The middle increment number is 32-bit signed long integer.
When the history increment number is less than 1023, for example: the history increment number is 3, and the generation intermediate increment number is 4. And the maximum value of the intermediate increment sequence number and the increment sequence number is subjected to AND calculation. 1023 the binary code is 0b11_1111_1111, 0b representing a binary. The binary code of 4 is 0b 0100. The phase and calculation is performed before the intermediate increment sequence number.
When the middle increment order number is equal to 1024, the binary code is 0b0100_0000_0000, and the and operation results in 0. I.e. to implement an automatic loop to the minimum value of the incremented sequence number.
When the middle increment index is an overflow boundary, 0b0111_1111_1111_1111_1111_1111, and the and operation results in 1023.
When the middle increment sequence number overflows, 0b1000_0000_0000_0000_0000_0000_0000_0000, and operation results in 0.
So in binary terms the increment of the number is a continuous loop in successive low bits, with no effect even if overflow occurs.
In actual use of the sequence number, the length of the sequence number is required, and in order to express more sequence numbers by using a shorter number of bits, the circularly-increasing sequence number can use a 16-system number, a 32-system number, and the like. For example, only five bits of the whole serial number can represent an ascending serial number, and at most one hundred thousand orders of magnitude can be represented by 10, and millions can be represented by hexadecimal.
S304, the server generates a target sequence number according to the current timestamp, the current counting sequence number and the node identification.
The current counting sequence number comprises a thread hash value and a current increasing sequence number, so that the current increasing sequence number in the current counting sequence number is identified, and a target sequence number generator for generating the increasing sequence number can be inquired from the generated sequence number.
In the serial number generation method provided in the embodiment of the present application, a thread identifier for processing a serial number generation request is subjected to hash processing, so as to obtain a thread hash value. The thread hash value can perform fragmentation processing on threads, reduce the number of threads competing for the same sequence number generator, and improve the generation efficiency under the condition of high concurrency. And the node identifier, the current timestamp, the thread hash value, and the current count sequence number may identify a sequence number generator that generates the count sequence number. The number of sequence number generators is set to 2nAnd the threads for processing the sequence number generation request are conveniently and quickly fragmented.
As shown in fig. 5, taking a computer system for financial transaction as an example, another embodiment of the present application provides a serial number generation method, which includes the following steps:
s401, the server receives a trading order generation request sent by the client.
The transaction order generation request comprises transaction data. The transaction data includes transaction time, transaction type, transaction amount, and the like.
S402, the server obtains a serial number generation request initiated by the target service.
After the server receives a generation request of a transaction order, the generation request of a serial number is triggered. The sequence number generation request includes the node identifier corresponding to the target service, that is, the identifier of the node that initiated the transaction order request. For example: the node is identified as 1S 5031234.
S403, the server acquires a history counting sequence number.
The method comprises the steps of determining the identifier of a thread processing a serial number generation request in a server, and carrying out hash operation on the thread identifier to obtain a thread hash value. And distributing corresponding target sequence number generators according to the thread hash value, and acquiring historical counting sequence numbers of the target sequence number generators.
For example: the hash operation is a remainder operation, the hash parameter is 7, the thread identifier is 1, and the thread hash value is also 1. The sequence number generator assigned the number 1 generates the current count sequence number, and the history increment sequence number of this sequence number generator is 0X3F 4. Accordingly, the historical count number is 7-0X3F 4.
S404, the server generates a current counting sequence number according to the historical counting sequence number.
And the target sequence number generator performs incremental operation on the historical incremental sequence number to obtain the current incremental sequence number. The history increment sequence number of the sequence number generator numbered 1 is 0X3F4, and is 0X3F5 after the increment processing.
S405, the server generates a target sequence number according to the current timestamp, the current counting sequence number and the node identification.
Wherein, the current timestamp is 20200824103000, the current increment sequence number is 0X3F5, the thread hash value is 1, and the node identifier 1S 5031234. The target sequence number is 1S5031234-20200824103000-1-3F 5.
S406, the server generates a trade order according to the trade data and the target serial number.
The server analyzes the transaction order request to obtain transaction data, verifies the transaction data, generates a corresponding transaction order according to the transaction data and the target serial number, and stores the transaction order in the database.
And S407, the server returns a prompt message to the client.
And after the server successfully generates the trading order, the server returns prompt information of successful creation to the client.
In the serial number generation method provided by the embodiment of the application, the client sends a transaction order generation request to the server, the server responds to the request to generate a corresponding target serial number and generates a transaction order according to the target serial number and transaction data, the counting serial number is irrelevant to the timestamp, and the serial number generated by using the method can be rolled back within a certain time.
As shown in fig. 6, another embodiment of the present application provides a serial number generation apparatus 400, which includes:
an obtaining module 501, configured to obtain a sequence number generation request initiated by a target service, where the sequence number generation request includes a node identifier corresponding to the target service;
a processing module 502, configured to generate a current count serial number according to a historical count serial number, where the historical count serial number is a count serial number generated in response to a last serial number generation request;
the processing module 502 is further configured to generate a target sequence number according to the current timestamp, the current counting sequence number, and the node identifier, where the target sequence number is used to uniquely identify the target service.
Optionally, the processing module 502 is further configured to:
selecting a target sequence number generator from a plurality of sequence number generators according to a thread identifier, wherein the thread identifier is an identifier of a thread processing a sequence number generation request;
and acquiring the historical counting sequence number generated by the target sequence number generator.
Optionally, the processing module 502 is specifically configured to:
performing hash operation on the thread identifier to obtain a thread hash value;
and taking the sequence number generator with the number being the thread hash value as a target sequence number generator.
Optionally, the processing module 502 is specifically configured to:
determining a hash parameter according to the total number of the sequence number generators;
and performing remainder operation on the thread identifier and the hash parameter to determine a thread hash value.
Optionally, the total number of sequence number generators is 2nWherein n is an integer.
Optionally, a plurality of sequence number generators are stored in the ring buffer.
Optionally, the historical count sequence number includes a thread hash value and an incremented sequence number.
Optionally, the processing module 502 is specifically configured to:
when the maximum value of the increment number is 2nWhen the number is-1, performing incremental operation on the historical incremental sequence number to obtain a middle incremental sequence number;
carrying out bitwise AND operation on the intermediate incremental sequence number and the maximum value of the incremental sequence number to obtain the current incremental sequence number; wherein n is not less than 0 and n is an integer.
Optionally, the processing module 502 is specifically configured to:
judging whether the historical increasing sequence number reaches the maximum value of the increasing sequence number;
if so, the current incremental sequence number is the minimum value of the incremental sequence numbers;
if not, carrying out incremental operation on the historical incremental sequence number to obtain the current incremental sequence number.
Optionally, the processing module 502 is specifically configured to:
carrying out incremental operation on the historical incremental sequence number by using a self-increment instruction in an atomic operation class;
wherein the target sequence number generator comprises a selfjoin instruction of the atomic operation class.
Optionally, the node identifier includes a machine identifier and a program identifier corresponding to the target service.
As shown in fig. 7, an electronic device 600 provided in another embodiment of the present application includes: a transmitter 601, a receiver 602, a memory 603, and a processor 604.
A transmitter 601 for transmitting instructions and data;
a receiver 602 for receiving instructions and data;
a memory 603 for storing computer-executable instructions;
processor 604 for executing computer executable instructions stored in the memory to implement the steps performed by the serial number generation method in the above embodiments. Specifically, reference may be made to the related description in the foregoing serial number generation method embodiment.
Alternatively, the memory 603 may be separate or integrated with the processor 604. When the memory 603 is separately provided, the electronic device further includes a bus for connecting the memory 603 and the processor 604.
An embodiment of the present application further provides a computer-readable storage medium, where a computer executing instruction is stored in the computer-readable storage medium, and when the processor executes the computer executing instruction, the serial number generation method executed by the electronic device is implemented as above.
Those of ordinary skill in the art will understand that: all or a portion of the steps of implementing the above-described method embodiments may be performed by hardware associated with program instructions. The program may be stored in a computer-readable storage medium. When executed, the program performs steps comprising the method embodiments described above; and the aforementioned storage medium includes: various media that can store program codes, such as ROM, RAM, magnetic or optical disks.
Finally, it should be noted that: the above embodiments are only used for illustrating the technical solutions of the present application, and not for limiting the same; although the present application has been described in detail with reference to the foregoing embodiments, it should be understood by those of ordinary skill in the art that: the technical solutions described in the foregoing embodiments may still be modified, or some or all of the technical features may be equivalently replaced; and the modifications or the substitutions do not make the essence of the corresponding technical solutions depart from the scope of the technical solutions of the embodiments of the present application.

Claims (14)

1. A method for generating a sequence number, comprising:
acquiring a sequence number generation request initiated by a target service, wherein the sequence number generation request comprises a node identifier corresponding to the target service;
generating a current counting sequence number according to a historical counting sequence number, wherein the historical counting sequence number is a counting sequence number generated in response to a last sequence number generation request;
and generating a target sequence number according to the current timestamp, the current counting sequence number and the node identifier, wherein the target sequence number is used for uniquely identifying the target service.
2. The method of claim 1, further comprising, prior to generating a current count sequence number from a historical count sequence number:
selecting a target sequence number generator from a plurality of sequence number generators according to a thread identifier, wherein the thread identifier is an identifier of a thread for processing the sequence number generation request;
and acquiring the historical counting sequence number generated by the target sequence number generator.
3. The method of claim 2, wherein selecting a target sequence number generator from a plurality of sequence number generators according to the thread identifier comprises:
performing hash operation on the thread identifier to obtain a thread hash value;
and taking the sequence number generator with the serial number being the thread hash value as the target sequence number generator.
4. The method according to claim 3, wherein performing a hash calculation on the thread identifier to obtain a thread hash value specifically includes:
determining a hash parameter according to the total number of the sequence number generators;
and performing remainder operation on the thread identifier and the hash parameter to determine the thread hash value.
5. Method according to claim 2, characterized in that the total number of sequence number generators is 2nWherein n is not less than 0 and n is an integer.
6. The method of claim 2, wherein the plurality of sequence number generators are stored in a ring buffer.
7. The method of any of claims 2 to 6, wherein the historical count sequence number comprises a thread hash value and an incremented sequence number.
8. The method according to claim 7, wherein generating a current count serial number according to the historical count serial number specifically comprises:
when the maximum value of the increment sequence number is 2nWhen the number is 1, carrying out increment operation on the historical increment sequence number to obtain a middle increment sequence number;
carrying out bitwise AND operation on the intermediate incremental sequence number and the maximum value of the incremental sequence number to obtain the current incremental sequence number; wherein n is not less than 0 and n is an integer.
9. The method according to claim 1, wherein generating a current count serial number according to the historical count serial number specifically comprises:
judging whether the historical increasing sequence number reaches the maximum value of the increasing sequence number;
if so, the current incremental sequence number is the minimum value of the incremental sequence numbers;
if not, carrying out incremental operation on the historical incremental sequence number to obtain the current incremental sequence number.
10. The method according to claim 9, wherein performing an increment operation on the historical increment sequence number specifically comprises:
carrying out incremental operation on the historical incremental sequence number by using a self-increment instruction in an atomic operation class;
wherein the target sequence number generator comprises a selfjoin instruction of the atomic operation class.
11. The method of claim 1, wherein the node identifier comprises a machine identifier and a program identifier corresponding to the target service.
12. A serial number generation apparatus, comprising:
an obtaining module, configured to obtain a sequence number generation request initiated by a target service, where the sequence number generation request includes a node identifier corresponding to the target service;
the processing module is used for generating a current counting serial number according to a historical counting serial number, wherein the historical counting serial number is the counting serial number generated in response to the last serial number generation request;
the processing module is further configured to generate a target sequence number according to the current timestamp, the current counting sequence number, and the node identifier, where the target sequence number is used to uniquely identify the target service.
13. An electronic device, comprising:
a memory for storing a program;
a processor for executing the program stored by the memory, the processor being configured to perform the serial number generation method of any of claims 1 to 11 when the program is executed.
14. A computer-readable storage medium having computer-executable instructions stored therein, which when executed by a processor, are configured to implement the serial number generation method of any one of claims 1 to 11.
CN202010899153.XA 2020-08-31 2020-08-31 Serial number generation method and device, electronic equipment and storage medium Pending CN112035547A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202010899153.XA CN112035547A (en) 2020-08-31 2020-08-31 Serial number generation method and device, electronic equipment and storage medium

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202010899153.XA CN112035547A (en) 2020-08-31 2020-08-31 Serial number generation method and device, electronic equipment and storage medium

Publications (1)

Publication Number Publication Date
CN112035547A true CN112035547A (en) 2020-12-04

Family

ID=73586533

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202010899153.XA Pending CN112035547A (en) 2020-08-31 2020-08-31 Serial number generation method and device, electronic equipment and storage medium

Country Status (1)

Country Link
CN (1) CN112035547A (en)

Cited By (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN112559908A (en) * 2020-12-16 2021-03-26 北京百度网讯科技有限公司 Data identifier generation method, device, equipment, medium and program product
CN113792351A (en) * 2021-08-16 2021-12-14 珠海天威技术开发有限公司 Consumable chip serial number generation method, consumable chip and consumable container
CN115103024A (en) * 2022-06-21 2022-09-23 联仁健康医疗大数据科技股份有限公司 Serial number generation method and device, electronic equipment and storage medium

Citations (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN110750506A (en) * 2019-09-17 2020-02-04 苏宁云计算有限公司 Data object identification generation method and device, computer equipment and storage medium

Patent Citations (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN110750506A (en) * 2019-09-17 2020-02-04 苏宁云计算有限公司 Data object identification generation method and device, computer equipment and storage medium

Cited By (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN112559908A (en) * 2020-12-16 2021-03-26 北京百度网讯科技有限公司 Data identifier generation method, device, equipment, medium and program product
CN112559908B (en) * 2020-12-16 2024-04-26 北京百度网讯科技有限公司 Data identifier generation method, device, equipment, medium and program product
CN113792351A (en) * 2021-08-16 2021-12-14 珠海天威技术开发有限公司 Consumable chip serial number generation method, consumable chip and consumable container
CN115103024A (en) * 2022-06-21 2022-09-23 联仁健康医疗大数据科技股份有限公司 Serial number generation method and device, electronic equipment and storage medium
CN115103024B (en) * 2022-06-21 2024-03-15 联仁健康医疗大数据科技股份有限公司 Sequence number generation method and device, electronic equipment and storage medium

Similar Documents

Publication Publication Date Title
CN112035547A (en) Serial number generation method and device, electronic equipment and storage medium
US11632441B2 (en) Methods, systems, and devices for electronic note identifier allocation and electronic note generation
CN107301546B (en) Random number generation and application method and device
CN110554732A (en) identification number generation method and device, electronic equipment and storage medium
CN107547572B (en) CAN bus communication method based on pseudo-random number
CN112907369B (en) Block chain-based data consensus method and device, electronic equipment and storage medium
CN110830608B (en) Global unique identifier generation method, device, equipment and storage medium
CN110602158A (en) Global unique ID generation system and method, ID generator and storage medium
CN111416765A (en) Interactive message processing method and device
CN109257396B (en) Distributed lock scheduling method and device
CN106210159B (en) Domain name resolution method and device
CN112436962B (en) Block chain consensus network dynamic expansion method, electronic device, system and medium
CN111752819B (en) Abnormality monitoring method, device, system, equipment and storage medium
CN110781387B (en) Pushing method of stock market snapshot, stock trading system and storage medium
CN117235817A (en) Method and device for generating identification in cluster environment, electronic equipment and storage medium
CN111078413A (en) Timed task execution method and device, computer equipment and storage medium
CN107315693B (en) Data storage method and device
US8572231B2 (en) Variable-length nonce generation
CN105447141A (en) Data processing method and node
CN110460634B (en) Edge computing consensus request management method and system
CN113032188B (en) Method, device, server and storage medium for determining main server
CN110935168B (en) Distributed ID generation method, device and equipment for global uniform architecture
CN112181926A (en) Configuration update synchronization method and device
CN113468508B (en) Information verification method, device, server and storage medium
CN113301002B (en) Information processing method, device, electronic equipment and storage medium

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