Method, device, equipment, storage medium and program product for generating time series primary key

The method addresses concurrency and availability issues in time series primary key generation by using a client-side token system to check for clock rollbacks and request new tokens, ensuring unique and reliable key generation in distributed systems.

CN114168593BActive Publication Date: 2024-12-24CCB FINTECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202111461637.7
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-12-02
Publication Date
2024-12-24
Estimated Expiration
2041-12-02

AI Technical Summary

Technical Problem

Existing methods for generating time series primary keys in distributed systems face challenges with high concurrency, availability, and clock synchronization issues, leading to potential duplication and single point failures.

Method used

A method and device for generating time series primary keys that involve a client obtaining a primary key generation token, checking for clock rollback, and requesting a new token from a server if necessary, ensuring unique key generation without blocking services.

Benefits of technology

This approach ensures unique primary key generation with high concurrency and availability, avoiding duplication and single point failures by locally generating keys and handling clock rollbacks efficiently.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN114168593B_ABST
    Figure CN114168593B_ABST
Patent Text Reader

Abstract

The present application discloses a method, apparatus, device, storage medium and program product for generating a sequential primary key. When the method is applied to a client, it includes: obtaining a primary key generation token; obtaining the last primary key generation time and the current system time, and judging whether a clock dialback occurs based on the last primary key generation time and the current system time; if a clock dialback occurs, the primary key generation token is judged to be invalid, and a new primary key generation token is applied for from the server; and a sequential primary key of the current data record is generated based on the new primary key generation token. Since the new primary key generation token is different from the original primary key generation token, the sequential primary key generated based on the primary key generation token is also different, which effectively avoids duplication of sequential primary keys. Generating sequential primary keys by the client can avoid single point failures and meet the requirements of high concurrency, high availability and high performance.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of data processing technology, and in particular to a method for generating a time sequence primary key, a device for generating a time sequence primary key, an electronic device, a computer-readable storage medium, and a computer program product. Background Art

[0002] In increasingly complex distributed systems, the amount of data is increasing. In order to facilitate data management, it is necessary to uniquely identify a large amount of time series data and generate a globally unique time series primary key for each data.

[0003] In related technologies, there are two main directions for generating time series primary keys:

[0004] One direction is to generate a time-series primary key outside the application, use distributed cache, database self-increment sequence, and time series to generate the time-series primary key. The disadvantage is that the concurrency is not high. The distributed cache can reach 100,000 levels, and the database self-increment sequence is 10,000 or lower, which cannot meet the high concurrency requirements of fields such as the Internet of Things. On the other hand, due to the use of external primary keys, single point failures are prone to occur. In high concurrency situations, failures in the cache or database that generates the time-series primary key will cause the entire distributed application to be unavailable, which cannot meet high availability requirements.

[0005] Another direction is to use the application host to generate the timing primary key. Its advantage is high concurrency, but because the host system time needs to be synchronized regularly, clock callbacks are prone to occur, resulting in duplicate timing primary keys. The solution in the related technology mainly uses a distributed coordinator to manage the clock callback uniformly. Each time a timing primary key is generated, the distributed coordinator needs to determine the clock callback and handle the clock callback by blocking the service, which leads to problems such as service unavailability after the clock callback occurs. At the same time, since an external distributed coordinator is required to determine the clock callback, the concurrency is limited to the processing capacity of the distributed coordinator and cannot meet high concurrency requirements. Summary of the invention

[0006] The present application provides a method, apparatus, device, storage medium and program product for generating a timing master key, so as to solve the problem of how to avoid duplication of the timing master key caused by clock dialback in the process of generating the timing master key.

[0007] In a first aspect, an embodiment of the present application provides a method for generating a time series primary key, the method being applied in a client, the method comprising:

[0008] Get the primary key generation token;

[0009] Obtain the last primary key generation time and the current system time, and determine whether a clock rollback occurs based on the last primary key generation time and the current system time;

[0010] If the clock is dialed back, the master key generation token is determined to be invalid, and a new master key generation token is requested from the server;

[0011] A temporal primary key of the current data record is generated based on the new primary key generation token.

[0012] In a second aspect, an embodiment of the present application further provides a method for generating a time series primary key, the method being applied in a server, the method comprising:

[0013] Receive the token request sent by the client;

[0014] Generate a token with a primary key corresponding to the client according to the token request;

[0015] The primary key generation token is returned to the client, so that the client generates a temporal primary key of the current data record based on the primary key generation token.

[0016] In a third aspect, an embodiment of the present application further provides a device for generating a time series primary key, the device being applied in a client, and comprising:

[0017] Token acquisition module, used to obtain the primary key to generate the token;

[0018] A clock dial-back judgment module is used to obtain the last master key generation time and the current system time, and judge whether the clock dial-back occurs according to the last master key generation time and the current system time;

[0019] A token update module, used to determine that the master key generation token is invalid if a clock is set back, and apply for a new master key generation token from the server;

[0020] A temporal primary key generation module is used to generate a temporal primary key of a current data record based on the new primary key generation token.

[0021] In a fourth aspect, an embodiment of the present application further provides a device for generating a time series primary key, the device being applied in a server, and comprising:

[0022] A token request receiving module, used for receiving a token request sent by a client;

[0023] A token generation module, used to generate a primary key generation token corresponding to the client according to the token request;

[0024] A token return module is used to return the primary key generation token to the client, so that the client generates a temporal primary key of the current data record based on the primary key generation token.

[0025] In a fifth aspect, an embodiment of the present application further provides an electronic device, the electronic device comprising:

[0026] one or more processors;

[0027] a storage device for storing one or more programs,

[0028] When the one or more programs are executed by the one or more processors, the one or more processors implement the method of the first aspect or the second aspect mentioned above.

[0029] In a sixth aspect, an embodiment of the present application further provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the method of the first aspect or the second aspect mentioned above.

[0030] In a seventh aspect, an embodiment of the present application further provides a computer program product, wherein the computer program product comprises computer executable instructions, and wherein the computer executable instructions are used to implement the method of the first aspect or the second aspect when executed.

[0031] The technical solution provided by this application has the following beneficial effects:

[0032] In this embodiment, during the process of generating a timing primary key, the client automatically obtains a primary key generation token, identifies the host clock callback and applies for a new primary key generation token, and uses the new primary key generation token to generate a timing primary key. Since the new primary key generation token is different from the originally obtained primary key generation token, the timing primary key generated according to the primary key generation token is also different, which effectively avoids duplication of the timing primary key. In addition, this embodiment generates a unique timing primary key locally on the client, so different clients can generate timing primary keys at the same time, which can avoid single point failures and meet the requirements of high concurrency, high availability and high performance. Compared with the service registration mode mentioned in the background technology, and the method of determining the clock callback by registering the host, this embodiment adopts a non-blocking mode, automatically identifies and processes the clock callback, and will not interrupt business operations. BRIEF DESCRIPTION OF THE DRAWINGS

[0033] Figure 1 It is a flowchart of a method embodiment of generating a temporal primary key provided in the first embodiment of the present application;

[0034] Figure 2 It is a flow chart of a method embodiment of generating a temporal primary key provided in Embodiment 2 of the present application;

[0035] Figure 3 This is a schematic diagram of a distributed system framework provided in Example 2 of the present application;

[0036] Figure 4This is a schematic diagram of a client architecture provided in Embodiment 2 of the present application;

[0037] Figure 5 This is a schematic diagram of a time sequence primary key provided in Example 2 of the present application;

[0038] Figure 6 This is a flowchart of a method embodiment of generating a time series primary key provided in Embodiment 3 of the present application;

[0039] Figure 7 This is a schematic diagram of a server architecture provided in Example 3 of the present application;

[0040] Figure 8 This is a schematic diagram of slot status management provided in Embodiment 3 of the present application;

[0041] Fig. 9 It is a structural block diagram of an embodiment of a device for generating a time sequence primary key provided in Embodiment 4 of the present application;

[0042] Fig.10 It is a structural block diagram of an embodiment of a device for generating a time sequence primary key provided in Embodiment 5 of the present application;

[0043] Fig.11 This is a structural diagram of an electronic device provided in Example 6 of the present application. DETAILED DESCRIPTION

[0044] The present application will be further described in detail below in conjunction with the accompanying drawings and embodiments. It is to be understood that the specific embodiments described herein are only used to explain the present application, rather than to limit the present application. It should also be noted that, for ease of description, only the parts related to the present application, rather than all structures, are shown in the accompanying drawings.

[0045] Embodiment 1

[0046] Figure 1 This is a flowchart of a method embodiment of generating a time series primary key provided in the first embodiment of the present application. This embodiment can be applied to a client and may include the following steps:

[0047] Step 110, obtain a primary key generation token.

[0048] Among them, the primary key generation token is a dynamic token used to generate a temporal primary key of a data record in a database. The temporal primary key is used to identify the data record and is the unique identifier of the data record in the database system.

[0049] In implementation, if the client has a primary key generation token stored locally, the primary key generation token can be directly obtained locally. Otherwise, if the client does not have a primary key generation token stored locally, the primary key generation token can be requested from the server.

[0050] It should be noted that this embodiment does not limit the form and length of the primary key generation token. Technical personnel in this field can set its form and length according to actual needs. For example, the length of the primary key generation token can be set to twice the number of global application clients. Assuming that the number of global application clients is n, the length of the primary key generation token is 2n, to prevent the host clock from collectively dialing back under extreme conditions and causing the queue to exceed the limit.

[0051] Step 120, obtaining the last master key generation time and the current system time, and determining whether a clock rollback occurs based on the last master key generation time and the current system time.

[0052] The last primary key generation time is a field name stored in the client. The initialization of the client includes initializing the last primary key generation time to the current system time of the client. The last primary key generation time is a global attribute within a single application. A global lock needs to be added and updated every time a primary key is generated.

[0053] The current system time refers to the real-time system time of the client, or more precisely, the system time of the application server where the current client is located.

[0054] In order to improve the accuracy of preventing the host clock from being rolled back, the current system time and the time when the last primary key was generated can both be millisecond values.

[0055] Clock rollback means that the time of a computer server is earlier than the previous time at a certain moment for some reason. There are many possible causes for the server clock to roll back. For example, the operation and maintenance personnel manually set a time earlier than the current time of the server, the server clock runs slower than the standard time during operation, or the server clock runs faster than the standard clock, and the server clock rolls back after running remote clock synchronization, etc.

[0056] In one embodiment, in step 120, whether a clock rollback occurs may be determined in the following manner:

[0057] The difference between the last master key generation time and the current system time is calculated; if the difference is greater than a preset threshold, it is determined that a clock rollback occurs; if the difference is less than or equal to the preset threshold, it is determined that a clock rollback does not occur.

[0058] In this embodiment, the value of "last primary key generation time" recorded by the client can be read, and the real-time current system time can be obtained, and then the difference between the two can be calculated. The difference is compared with a preset threshold to determine whether a clock dialback occurs. If the difference is not greater than the preset threshold, it is determined that the clock dialback does not occur; if the difference is greater than the preset threshold, it is determined that the clock dialback occurs.

[0059] It should be noted that the preset threshold may be an empirical value, or a value calculated according to a preset concurrency and a preset primary key count capacity, which is not limited in this embodiment.

[0060] Step 130: If the clock is dialed back, it is determined that the master key generation token is invalid, and a new master key generation token is requested from the server.

[0061] If it is determined that the clock has been dialed back, in order to avoid generating duplicate timing primary keys, the primary key generation token obtained in step 110 can be set to invalid, and then a new primary key generation token can be requested from the server to update the locally stored primary key generation token.

[0062] It should be noted that when applying for a new primary key to generate a token from the server, the application corresponding to the current client is in a blocked state.

[0063] Step 140: Generate a temporal primary key of the current data record based on the new primary key generation token.

[0064] After obtaining a new primary key generation token from the server, the temporal primary key of the current data record can be generated according to the new primary key generation token. The difference in the primary key generation token can effectively ensure the uniqueness of the temporal primary key.

[0065] In this embodiment, during the process of generating a timing primary key, the client automatically obtains a primary key generation token, identifies the host clock callback and applies for a new primary key generation token, and uses the new primary key generation token to generate a timing primary key. Since the new primary key generation token is different from the originally obtained primary key generation token, the timing primary key generated according to the primary key generation token is also different, which effectively avoids duplication of the timing primary key. In addition, this embodiment generates a unique timing primary key locally on the client, so different clients can generate timing primary keys at the same time, which can avoid single point failures and meet the requirements of high concurrency, high availability and high performance. Compared with the service registration mode mentioned in the background technology, and the method of determining the clock callback by registering the host, this embodiment adopts a non-blocking mode, automatically identifies and processes the clock callback, and will not interrupt business operations.

[0066] Embodiment 2

[0067] Figure 2 This is a flowchart of a method embodiment of generating a time-series primary key provided in Embodiment 2 of the present application. This embodiment provides a more specific description of the process of generating a time-series primary key based on Embodiment 1. This embodiment can be executed in a client of an application program, which can be a distributed application client, and which is deployed in an application server, wherein multiple clients can be deployed in the same application server, and the application server deployment can be in a cluster mode, which can be expanded horizontally and vertically.

[0068] like Figure 3 As shown in the distributed system framework diagram, the application server can communicate with the time synchronization server and the token registration server. The time synchronization server is used to synchronize time with the application server regularly. The frequency of time synchronization can be determined according to actual business needs, such as once a day. However, continuous time synchronization is not performed to avoid the failure of the detection time threshold k obtained later due to continuous clock dialback. The token registration server is used to generate a primary key generation token and send the primary key generation token to the application client via the application server.

[0069] like Figure 4 As shown in the client architecture diagram, the client may include a local storage unit, a primary key generation unit, a token registration unit, a clock dialback detection unit, etc. Among them, the local storage unit is used to store the primary key generation token, the last primary key generation time and the status information of the primary key generation unit, etc.; the primary key generation unit is used to generate a time-series primary key; the token registration unit is used to register the primary key generation token to the token registration server via the application server; the clock dialback detection unit is used to detect whether a clock dialback occurs.

[0070] like Figure 2 As shown, this embodiment may include the following steps:

[0071] Step 210, search whether a primary key generation token is stored in the local storage unit, if so, execute step 220, if not, execute step 230.

[0072] In one implementation, a token field is stored in the local storage unit, and the value of the field can be initialized to a null value during initialization. Each time a primary key generation token value is obtained from a token registration server, the primary key generation token can be recorded in the token field. Then, each time a sequential primary key needs to be generated, the corresponding token value can be first read from the token field of the local storage unit to determine whether the client already has a primary key generation token. If the token value is a null value, it is determined that the client has not registered a primary key generation token locally, and step 230 can be executed at this time; if the token value is not a null value, it is determined that the client has previously obtained a primary key generation token locally, and step 220 can be executed at this time.

[0073] Step 220 , read the primary key from the local storage unit to generate a token, and then proceed to step 250 .

[0074] When the token value of the token field is not null, the corresponding token value can be read from the token field as the primary key to generate a token.

[0075] Step 230, obtain client parameters, generate a token application request based on the client parameters, send the token application request to the server, and receive a first response message returned by the server based on the token application request, wherein the first response message includes: a primary key to generate a token, and a client identification number generated by the server based on the client parameters.

[0076] When the token value of the token field is null, it means that the client has no primary key to generate a token locally, and then the primary key to generate a token can be applied for from the token registration server through the token registration unit. Specifically, the server mentioned in this step refers to the token registration server. When the token registration unit first applies to the token registration server for a primary key to generate a token, it needs to first collect the client parameters of the client, and then generate a token application request based on the client parameters, and then send the token application request to the application server, and the application server sends the token application request to the token registration server.

[0077] For example, the client parameters may include but are not limited to: host name, application name, physical address of the network card corresponding to the distributed application outbound call, IP address, external service port number of the application instance, etc. The combination of the above client parameters can identify the only application client in the distributed system.

[0078] For the token registration server, after receiving the token application request, it parses the client parameters from it, and generates a client identification number for identifying the client in the server based on the client parameters, and then allocates a primary key to generate a token for the client. The client identification number and the primary key generation token are then combined into a first response message, and the first response message is returned to the application server corresponding to the client, and the application server returns the first response message to the client.

[0079] After the client receives the first response information, it can parse the first response information to obtain the primary key generation token and the client identification number.

[0080] Step 240 , associate and store the primary key generation token and the client identification number in the first response information in the local storage unit, and proceed to step 250 .

[0081] After the client obtains the primary key generation token and the client identification number through step 230, the primary key generation token can be recorded in the token field, and the client identification number can be recorded in the client identification number field. In the token field and the client identification number field, a new record can be written in a way of overwriting the original record. For example, when writing the primary key generation token, the latest primary key generation token can replace the original token value in the token field, thereby ensuring that the field value recorded in each field is the most recently obtained field value.

[0082] Step 250, obtaining the last primary key generation time and the current system time, and calculating the difference between the last primary key generation time and the current system time.

[0083] After the client obtains the master key generation token, it can perform a clock dial back detection through the clock dial back detection unit. Specifically, the clock dial back detection unit first reads the last master key generation time from the "last master key generation time" field of the local storage unit, and then obtains the real-time current system time, and detects whether the clock dial back occurs by comparing the last master key generation time with the current system time. In one implementation, the above comparison can be to calculate the difference between the last master key generation time and the current system time, that is, the difference = the last master key generation time - the current system time.

[0084] Step 260: Obtain a preset concurrency and a preset primary key count capacity of the application server to which the current client belongs, and determine a detection time threshold according to the preset concurrency and the preset primary key count capacity.

[0085] In this step, each application server may have a preset concurrency and primary key count capacity. The clock dial back detection unit may obtain the preset concurrency and the preset primary key count capacity from the application server to which the current client belongs, and then determine the detection time threshold k according to the preset concurrency and the preset primary key count capacity. The detection time threshold k refers to the maximum tolerable clock dial back detection period.

[0086] In practice, to prevent the clock from setting back and causing duplication of primary keys, the detection time threshold k can be expressed in milliseconds (ms), such as 10000 for 10 seconds.

[0087] In one implementation, the detection time threshold may be determined by the following formula: detection time threshold = preset primary key count capacity / preset concurrency. For example, assuming that the preset primary key count capacity is 1 billion and the preset concurrency is 10 million, the detection time threshold k = 1 billion / 10 million = 100,000 milliseconds, i.e., 100 seconds.

[0088] It should be noted that the execution order of step 260 is not limited to after step 250 or before step 270, and the detection time threshold may also be calculated during the distributed application startup process. In addition, the detection time threshold is not limited to being calculated in the client, and may also be calculated in the application server and transmitted to the application client, which is not limited in this embodiment.

[0089] Step 270: If the difference is greater than the detection time threshold, it is determined that clock dialback occurs; if the difference is less than or equal to the detection time threshold, it is determined that clock dialback does not occur.

[0090] If the above difference is greater than the detection time threshold, it means that the last primary key generation time is later than the current system time, that is, the current system time is earlier than the last primary key generation time and has exceeded the maximum tolerance. At this time, it can be considered that the clock is rolled back.

[0091] If the above difference is not greater than the detection time threshold, there are two cases. One case is that the difference is a negative number, which means that the last primary key generation time is earlier than the current system time. In this case, it can be considered that no clock dialback has occurred. In the other case, the difference is a positive number, which means that the last primary key generation time is later than the current system time, but does not exceed the maximum tolerance. Within the tolerance period, it can also be considered that no clock dialback has occurred, thereby avoiding too frequent triggering of the clock dialback condition, resulting in frequent requests to the token registration server to update the token, and meeting the needs of high concurrency.

[0092] Step 280: If the clock is dialed back, it is determined that the token generated by the master key is invalid, and the client identification number is read from the local storage unit, and a token update request is generated based on the client identification number.

[0093] Step 290: Send the token update request to the server, and receive a new primary key generated token returned by the server based on the token update request.

[0094] In this step, if a clock dialback is detected, the client can set the master key generation token obtained above to be invalid, and then apply to the token registration server for updating the master key generation token through the token registration unit. Specifically, the token registration unit first reads the client identification number of the current client from the local storage unit, and then generates a token update request based on the client identification number. Then, the token update request is sent to the token registration server, and the new master key generation token returned by the token registration server based on the token update request is received.

[0095] Step 2110: Set the state of the primary key generation unit to a ready state.

[0096] In this step, when a new master key generation token is obtained, or when it is determined that no clock rollback occurs, the state of the master key generation unit can be set to a ready state, wherein the ready state is a state indicating that the timing master key generation condition is met and the master key generation token is valid.

[0097] Step 2111, update the last primary key generation time to the current system time.

[0098] When generating a temporal primary key, you can also update the value in the "Last primary key generation time" field to update the last primary key generation time to the current system time.

[0099] Step 2112, based on the new primary key generation token, the current system time and the primary key count capacity, generate a temporal primary key for the current data record according to a preset primary key generation rule.

[0100] When ready, the primary key generation unit can generate the time sequence primary key of the current data record based on the new primary key generation token. The new primary key generation token may refer to the updated primary key generation token obtained in step 270. However, when the clock is not dialed back, the primary key generation token does not need to be updated, and the new primary key generation token refers to the primary key generation token read from the local in step 220.

[0101] In this embodiment, the time-series primary key includes at least the following three parts: primary key generation token, real-time current system time, and primary key count capacity. The preset primary key generation rule may specify the order of the above three parts, for example, Figure 5 The generated time series primary key includes the current system time, primary key count capacity and primary key generation token from left to right. Figure 5 The length of each part is only exemplary, and those skilled in the art can set the current system time, primary key count capacity, and the length of the primary key generation token according to actual conditions. In addition, the primary key generation rules listed above are also exemplary, and those skilled in the art can set the primary key generation rules according to actual needs, which is not limited in this embodiment.

[0102] In this embodiment, when the client detects clock dialback, it can determine the detection time threshold in combination with the preset concurrency of the application server to which it belongs and the preset primary key count capacity. After calculating the difference between the last primary key generation time and the current system time, it determines whether the clock dialback occurs based on the comparison of the detection time threshold and the difference, thereby improving the detection conditions of the clock dialback and avoiding the frequent application of tokens to the server due to frequent triggering of the clock dialback detection conditions. When the clock dialback is detected, the current primary key generation token is invalidated, and a new primary key generation token is requested from the server, and then a sequential primary key is generated based on the current latest primary key generation token, the current system time and the primary key count capacity, thereby ensuring the uniqueness of the sequential primary key.

[0103] Embodiment 3

[0104] Figure 6 This is a flowchart of a method embodiment of generating a sequential primary key provided in the third embodiment of the present application. This embodiment can be applied to a server, which is a token registration server for generating a primary key generation token. In this embodiment, the token registration server adopts a passive request mode, so the concurrency pressure is small, and the requirements can be met by deploying a dual-machine hot standby mode.

[0105] like Figure 7 As shown, in one embodiment, the token registration server may include a slot management unit, a token management unit and a token storage unit. The slot management unit is used to manage the token slots in use, the idle token slots and the unavailable token slots, as well as the expiration time of the unavailable token slots. The token management unit is mainly used to maintain the corresponding relationship management between the application client identification number, the slot and the primary key to generate the token. Globally, each application client corresponds to a slot and has an available token; when the clock of the host where the application client is located is dialed back, a new slot is replaced and a new token is generated by the token management unit. The token storage unit is the storage center of the token registration server, which is used to store the token management queue, as well as the corresponding client identification number and application client parameters.

[0106] like Figure 6 As shown, this embodiment may include the following steps:

[0107] Step 310: Receive a token request sent by the client.

[0108] In practice, the token request may include an initial token request and a non-initial token request. The initial token request is equivalent to the token application request in Example 2, and the information carried includes client parameters. The non-initial token request is equivalent to the token update request in Example 2, and the information carried includes the client identification number.

[0109] Step 320: Generate a token using the primary key corresponding to the client according to the token request.

[0110] In one embodiment, when the token request includes client parameters, it means that the request is the first token request, and step 320 may further include the following steps:

[0111] Step 320-1, generating a client identification number according to the client parameters.

[0112] Exemplarily, the client parameters may include, but are not limited to: host name, application name, physical address of the network card corresponding to the distributed application outbound call, IP address, external service port number of the application instance, etc.

[0113] In one implementation, a hash algorithm may be used to calculate a hash value of the client parameters as the client identification number. For example, the client parameters may be combined into a string, and a hash value of the string may be calculated to obtain the client identification number.

[0114] Step 320-3: allocate a first slot for the client, and bind the identifier of the first slot to the client identification number.

[0115] In this embodiment, the slot refers to a distribution API for allocating primary key generation tokens. <slot>< / slot> The element serves as an outlet for hosting distributed content.

[0116] After receiving the first token request, the server can allocate a slot for the current client. For easy identification, the slot allocated for the first time for the current client is called the first slot. Binding the identifier of the first slot with the client identification number of the current client can realize the association record between the client and the slot.

[0117] During implementation, the server can manage the status of each slot through the slot management unit, and the slot status may include: use status, idle status, and unusable status. Figure 8 As shown, the slot management unit may maintain a use status list, an idle status list, and an unusable status list. The use status list contains one or more slots (S i ), each use state slot has a one-to-one corresponding application client; the idle state list contains one or more slots in the idle state (F i ); The unusable state list contains one or more slots in the unusable state (N i ).

[0118] When the token registration server is started, all slots are in an idle state, waiting for application client registration, that is, when the slot is not assigned to any client, its state is idle; when the slot is assigned to a client, the state of the slot can be changed from idle to used, and moved from the idle state list to the used state list; when the slot is set to unavailable due to the client updating the token, its state is changed from used to unavailable, and moved from the used state list to the unavailable state list. Therefore, when the server assigns the first slot to the current client, the state of the first slot can be changed from idle to used.

[0119] During implementation, for the status maintenance of the slots in use, the application client can maintain the status of the slots in use by means of a timed heartbeat or by means of the token registration server periodically calling the application client health check interface.

[0120] Step 320-5: Use the first slot to generate a primary key to generate a token.

[0121] The slot has a token generation logic. When the slot is used, that is, when it is assigned to a client, a primary key generation token can be generated according to the token generation logic in the slot.

[0122] In another embodiment, when the token request includes the client identification number, it means that the request is not the first token request, and step 320 may further include the following steps:

[0123] Step 320-2: Determine a first slot allocated to the client according to the client identification number.

[0124] When the server receives the token request, it parses the client identification number from the token request, then searches the client identification number from the token storage unit to obtain the identifier of the slot bound to the client identification number, and determines the first slot according to the identifier of the slot.

[0125] Step 320-4: Set the first slot to an unavailable state and set an unavailable expiration time.

[0126] In this embodiment, when the original master key generation token is invalidated in the client, the slot that generates the master key generation token needs to be synchronously invalidated in the server. Specifically, after the first slot is determined, the state of the first slot can be changed from an available state to an unavailable state, and an expiration time for the unavailability of the first slot can be set, and the binding relationship between the identifier of the first slot and the client identification number is released. When the expiration time is exceeded, the state of the slot is changed back to an idle state.

[0127] In one embodiment, the unavailable expiration time is determined as follows:

[0128] Step 320-4-1, determine the information response time.

[0129] The information response time is the time from when the server receives a request to when it responds to the request. In implementation, the response time of the server for several requests in the recent period can be obtained, and the average of these response times can be calculated to determine the information response time.

[0130] Step 320-4-2, obtaining the preset concurrency and preset primary key count capacity of the application server to which the client belongs, and determining the detection time threshold according to the preset concurrency and the preset primary key count capacity.

[0131] In this step, each application server may have a preset concurrency and primary key count capacity. The server may obtain the preset concurrency and the preset primary key count capacity from the application server to which the current client belongs, and then determine the detection time threshold k according to the preset concurrency and the preset primary key count capacity. The detection time threshold k refers to the maximum tolerable clock dialback detection period.

[0132] In practice, the detection time threshold k can be expressed in milliseconds (ms), such as 10000 represents 10 seconds.

[0133] In one implementation, the detection time threshold may be determined by the following formula: detection time threshold = preset primary key count capacity / preset concurrency. For example, assuming that the preset primary key count capacity is 1 billion and the preset concurrency is 10 million, the detection time threshold k = 1 billion / 10 million = 100,000 milliseconds, i.e., 100 seconds.

[0134] Step 320-4-3: determine the unavailable expiration time according to the sum of the detection time threshold and the information response time.

[0135] In one implementation, in order to avoid allocating the same slot to the same client in a short period of time, the unavailable expiration time can be set to at least twice the sum of the detection time threshold and the information response time, for example, c=2(k+p), where c is the unavailable expiration time, k is the detection time threshold, and p is the information response time.

[0136] Step 320-6: allocate a second slot for the client, and bind the identifier of the second slot to the client identification number.

[0137] Since each application client corresponds to a slot globally, when the slot originally occupied by the current client is invalidated, a new slot can be allocated to the client in this step. The newly allocated slot is used as the second slot, and the identifier of the second slot is bound to the client identification number of the current client to realize the association record of the client and the second slot.

[0138] Step 320-8, using the second slot to generate a primary key to generate a token.

[0139] After the second slot is allocated to the client, the token generation logic in the second slot can be used to generate a new primary key generation token for the current client.

[0140] In one embodiment, the primary key generation token is a token with a set length, and the length can be determined in the following manner: obtain the number of global clients; determine the length of the primary key generation token according to the number. For example, the length of the primary key generation token is set to twice the number, that is, if the number of global clients is n, the length of the primary key generation token is 2n.

[0141] Step 330: Return the primary key generation token to the client, so that the client generates a temporal primary key of the current data record based on the primary key generation token.

[0142] After the server generates the primary key generation token, it can send the primary key generation token to the application server to which the current client belongs, and the application server sends the primary key generation token to the client. The client can then generate the time sequence primary key of the current data record based on the primary key generation token.

[0143] It should be noted that the token is part of the sequential primary key. When the client applies for a token from the server, the application is blocked until the primary key generated token is received from the server.

[0144] In this embodiment, when the client generates the sequential primary key of the current data record, one of the generation elements of the sequential primary key, namely the primary key generation token, can be obtained through the server. On the server side, different processing logics are used for token requests in different scenarios. If the token request is the first token request, a slot is allocated to the client and the primary key generation token is generated using the slot. If the token request is not the first token request, the slot originally allocated to the client is first invalidated, and then a new slot is allocated to the client, and then the new slot is used to generate the primary key generation token. Since the sequential primary key contains the primary key generation token, by updating the slot and issuing a new primary key generation token, the application client has a new primary key generation token when generating the sequential primary key. The generated sequential primary key can be within the detection time threshold k, and it is not repeated with the original sequential primary key, thereby avoiding the duplication of the sequential primary key caused by the host clock dialback, and ensuring that the sequential primary key generated by the client is globally unique.

[0145] Embodiment 4

[0146] Fig. 9 This is a structural block diagram of an embodiment of a device for generating a time sequence primary key provided in Embodiment 4 of the present application. The device can be applied to a client and may include the following modules:

[0147] The token acquisition module 410 is used to acquire a primary key to generate a token;

[0148] The clock dial back judgment module 420 is used to obtain the last master key generation time and the current system time, and judge whether the clock dial back occurs according to the last master key generation time and the current system time;

[0149] The token update module 430 is used to determine that the master key generation token is invalid if the clock is dialed back, and apply for a new master key generation token from the server;

[0150] The temporal primary key generation module 440 is used to generate a temporal primary key of the current data record based on the new primary key generation token.

[0151] In one embodiment, the token acquisition module 410 is specifically used to:

[0152] Check whether a primary key generation token is stored in the local storage unit;

[0153] If yes, reading the master key from the local storage unit to generate a token;

[0154] If not, obtain the client parameters, generate a token application request based on the client parameters, send the token application request to the server, and receive the first response information returned by the server based on the token application request, wherein the first response information includes a primary key to generate a token.

[0155] In one embodiment, the first response information further includes a client identification number generated by the server based on the client parameters, and the apparatus further includes:

[0156] The storage module is used to associate and store the primary key generation token and the client identification number in the first response information in the local storage unit when receiving the first response information returned by the server.

[0157] In one embodiment, the token update module 430 is specifically used to:

[0158] reading the client identification number from the local storage unit and generating a token update request based on the client identification number;

[0159] Sending the token update request to the server;

[0160] The server generates a token based on the new primary key received from the token update request.

[0161] In one embodiment, the clock dial-back determination module 420 is specifically used to:

[0162] Obtaining a preset concurrency and a preset primary key count capacity of the application server to which the current client belongs, and determining a detection time threshold according to the preset concurrency and the preset primary key count capacity;

[0163] Calculate the difference between the last primary key generation time and the current system time;

[0164] If the difference is greater than the detection time threshold, it is determined that a clock rollback occurs;

[0165] If the difference is less than or equal to the detection time threshold, it is determined that no clock rollback occurs.

[0166] In one embodiment, the temporal primary key generation module 440 is specifically used to:

[0167] A temporal primary key of the current data record is generated according to the new primary key generation token, the current system time and the primary key counting capacity and in accordance with a preset primary key generation rule.

[0168] In one embodiment, the device may further include the following modules:

[0169] The last primary key generation time updating module is used to update the last primary key generation time to the current system time before generating the temporal primary key of the current data record based on the new primary key generation token.

[0170] In one embodiment, the device may further include the following modules:

[0171] A state setting module, configured to set the state of the master key generation unit to a ready state when it is determined that no clock callback occurs or after obtaining the new master key generation token;

[0172] The time series primary key generation module 440 is specifically used for:

[0173] The primary key generation unit is used to generate a temporal primary key of the current data record based on the new primary key generation token.

[0174] An apparatus for generating a sequential primary key provided in an embodiment of the present application can execute a method for generating a sequential primary key in Embodiment 1 or Embodiment 2 of the present application, and has functional modules and beneficial effects corresponding to the execution method.

[0175] Embodiment 5

[0176] Fig.10 This is a structural block diagram of an embodiment of a device for generating a time sequence primary key provided in Embodiment 5 of the present application. The device can be applied to a server and may include the following modules:

[0177] The token request receiving module 510 is used to receive a token request sent by a client;

[0178] The token generation module 520 is used to generate a primary key generation token corresponding to the client according to the token request;

[0179] The token return module 530 is used to return the primary key generation token to the client, so that the client generates a temporal primary key of the current data record based on the primary key generation token.

[0180] In one embodiment, the token request includes client parameters, and the token generation module 520 is specifically used to:

[0181] generating a client identification number according to the client parameters;

[0182] Allocating a first slot for the client, and binding an identifier of the first slot to the client identification number;

[0183] The first slot is used to generate a primary key to generate a token.

[0184] In another embodiment, the token request includes a client identification number, and the token generation module 520 is specifically configured to:

[0185] Determining a first slot allocated to the client according to the client identification number;

[0186] Setting the first slot to an unavailable state and setting an unavailable expiration time;

[0187] Allocating a second slot for the client, and binding an identifier of the second slot to the client identification number;

[0188] The second slot is used to generate a primary key generation token.

[0189] In one embodiment, the unavailable expiration time is determined in the following manner:

[0190] Determine information response time;

[0191] Obtaining a preset concurrency and a preset primary key count capacity of the application server to which the client belongs, and determining a detection time threshold according to the preset concurrency and the preset primary key count capacity;

[0192] The unavailable expiration time is determined according to the sum of the detection time threshold and the information response time.

[0193] In one embodiment, the primary key generation token is a token with a set length, and the length is determined as follows:

[0194] Get the number of global clients;

[0195] The length of the primary key generation token is determined based on the said number.

[0196] An apparatus for generating a sequential primary key provided in an embodiment of the present application can execute a method for generating a sequential primary key in Embodiment 3 of the present application, and has functional modules and beneficial effects corresponding to the execution method.

[0197] Embodiment 6

[0198] Fig.11 A schematic diagram of the structure of an electronic device provided in Example 6 of the present application is shown in FIG. Fig.11 As shown, the electronic device includes a processor 610, a memory 620, an input device 630, and an output device 640; the number of the processor 610 in the electronic device can be one or more. Fig.11 A processor 610 is taken as an example; the processor 610, the memory 620, the input device 630 and the output device 640 in the electronic device can be connected via a bus or other means. Fig.11 The example of connecting through bus is taken in the following.

[0199] The memory 620, as a computer-readable storage medium, can be used to store software programs, computer executable programs and modules, such as program instructions / modules corresponding to any one of the above-mentioned embodiments 1 to 3 in the embodiments of the present application. The processor 610 executes various functional applications and data processing of the electronic device by running the software programs, instructions and modules stored in the memory 620, that is, implements the method mentioned in any one of the above-mentioned method embodiments 1 to 3.

[0200] The memory 620 may mainly include a program storage area and a data storage area, wherein the program storage area may store an operating system and at least one application required for a function; the data storage area may store data created according to the use of the terminal, etc. In addition, the memory 620 may include a high-speed random access memory, and may also include a non-volatile memory, such as at least one disk storage device, a flash memory device, or other non-volatile solid-state storage device. In some instances, the memory 620 may further include a memory remotely arranged relative to the processor 610, and these remote memories may be connected to the device / terminal / server via a network. Examples of the above-mentioned network include, but are not limited to, the Internet, an intranet, a local area network, a mobile communication network, and combinations thereof.

[0201] The input device 630 may be used to receive input digital or character information and generate key signal input related to user settings and function control of the electronic device. The output device 640 may include a display device such as a display screen.

[0202] Embodiment 7

[0203] Embodiment 7 of the present application further provides a storage medium comprising computer executable instructions, wherein the computer executable instructions, when executed by a computer processor, are used to execute the method in any one of Embodiments 1 to 3 of the above-mentioned method.

[0204] Embodiment 8

[0205] Embodiment 8 of the present application further provides a computer program product, which includes computer executable instructions. When the computer executable instructions are executed by a computer processor, they are used to execute the method in any one of the above-mentioned method embodiments 1 to 3.

[0206] Of course, the computer program product provided by the embodiment of the present application, whose computer executable instructions are not limited to the method operations described above, can also execute related operations in the method provided by any embodiment of the present application.

[0207] Through the above description of the implementation method, the technicians in the relevant field can clearly understand that the present application can be implemented with the help of software and necessary general hardware, and of course it can also be implemented by hardware, but in many cases the former is a better implementation method. Based on this understanding, the technical solution of the present application can be essentially or the part that contributes to the prior art can be embodied in the form of a software product, and the computer software product can be stored in a computer-readable storage medium, such as a computer floppy disk, read-only memory (ROM), random access memory (RAM), flash memory (FLASH), hard disk or optical disk, etc., including a number of instructions for an electronic device (which can be a personal computer, server, or network device, etc.) to execute the methods described in each embodiment of the present application.

[0208] It is worth noting that in the embodiment of the above-mentioned device, the various units and modules included are only divided according to functional logic, but are not limited to the above-mentioned division, as long as the corresponding functions can be achieved; in addition, the specific names of the functional units are only for the convenience of distinguishing each other, and are not used to limit the scope of protection of this application.

[0209] Note that the above are only preferred embodiments of the present application and the technical principles used. Those skilled in the art will understand that the present application is not limited to the specific embodiments described herein, and that various obvious changes, readjustments and substitutions can be made by those skilled in the art without departing from the scope of protection of the present application. Therefore, although the present application is described in more detail through the above embodiments, the present application is not limited to the above embodiments, and may include more other equivalent embodiments without departing from the concept of the present application, and the scope of the present application is determined by the scope of the appended claims.

[0210] In addition, it should also be noted that the acquisition, storage, use, and processing of data in the above embodiments of the present application are in compliance with the relevant provisions of national laws and regulations.

Claims

1. A method for generating a temporal primary key, characterized in that: The method is applied in a client, and the method includes: Get the primary key generation token; Obtain the last primary key generation time and the current system time, and determine whether a clock rollback occurs based on the last primary key generation time and the current system time; If the clock is dialed back, the master key generation token is determined to be invalid, and a new master key generation token is requested from the server; Generate a temporal primary key of the current data record based on the new primary key generation token; The step of obtaining a primary key to generate a token includes: Check whether a primary key generation token is stored in the local storage unit; If yes, reading the master key from the local storage unit to generate a token; If not, obtaining client parameters, generating a token application request based on the client parameters, sending the token application request to the server, and receiving first response information returned by the server based on the token application request, wherein the first response information includes a primary key to generate a token; The step of determining whether a clock rollback occurs according to the last master key generation time and the current system time includes: Obtaining a preset concurrency and a preset primary key count capacity of the application server to which the current client belongs, and determining a detection time threshold according to the preset concurrency and the preset primary key count capacity; Calculate the difference between the last primary key generation time and the current system time; If the difference is greater than the detection time threshold, it is determined that a clock rollback occurs; If the difference is less than or equal to the detection time threshold, it is determined that no clock rollback occurs.

2. The method according to claim 1, characterized in that The first response information also includes a client identification number generated by the server based on the client parameters, and the method further includes: When the first response information returned by the server is received, the primary key generation token and the client identification number in the first response information are associated and stored in the local storage unit.

3. The method according to claim 2, characterized in that The step of applying for a new primary key from the server to generate a token includes: reading the client identification number from the local storage unit and generating a token update request based on the client identification number; Sending the token update request to the server; The server generates a token based on the new primary key received from the token update request.

4. The method according to claim 1, characterized in that: The generating a time-series primary key of the current data record based on the new primary key generating token includes: A temporal primary key of the current data record is generated according to the new primary key generation token, the current system time and the primary key counting capacity and in accordance with a preset primary key generation rule.

5. The method according to any one of claims 1 to 3, characterized in that: Before generating the temporal primary key of the current data record based on the new primary key generation token, the method further includes: The last primary key generation time is updated to the current system time.

6. The method according to claim 1, characterized in that The client includes a primary key generation unit. Before generating a temporal primary key of a current data record based on the new primary key generation token, the method further includes: When it is determined that no clock callback occurs or after obtaining the new master key generation token, setting the state of the master key generation unit to a ready state; The generating a time-series primary key of the current data record based on the new primary key generating token includes: The primary key generation unit is used to generate a temporal primary key of the current data record based on the new primary key generation token.

7. A method for generating a temporal primary key, characterized in that: The method is applied in a server, and the method comprises: Receive the token request sent by the client; Generate a token with a primary key corresponding to the client according to the token request; Returning the primary key generation token to the client, so that the client generates a temporal primary key of the current data record based on the primary key generation token; The token request includes client parameters, and generating a token with a primary key corresponding to the client according to the token request includes: generating a client identification number according to the client parameters; Allocating a first slot for the client, and binding an identifier of the first slot to the client identification number; Using the first slot to generate a primary key to generate a token; The token request includes a client identification number, and generating a token with a primary key corresponding to the client according to the token request includes: Determining a first slot allocated to the client according to the client identification number; Setting the first slot to an unavailable state and setting an unavailable expiration time; Allocating a second slot for the client, and binding an identifier of the second slot to the client identification number; Using the second slot to generate a primary key generation token; The unavailable expiration time is determined in the following manner: Determine information response time; Obtaining a preset concurrency and a preset primary key count capacity of the application server to which the client belongs, and determining a detection time threshold according to the preset concurrency and the preset primary key count capacity; The unavailable expiration time is determined according to the sum of the detection time threshold and the information response time.

8. The method according to claim 7, characterized in that The primary key generation token is a token with a set length, and the length is determined in the following manner: Get the number of global clients; The length of the primary key generation token is determined based on the said number.

9. A device for generating a temporal primary key, characterized in that: The device is applied in a client, and comprises: Token acquisition module, used to obtain the primary key to generate the token; A clock dial-back judgment module is used to obtain the last master key generation time and the current system time, and judge whether the clock dial-back occurs according to the last master key generation time and the current system time; A token update module, used to determine that the master key generation token is invalid if a clock is set back, and apply for a new master key generation token from the server; A temporal primary key generation module, used to generate a temporal primary key of a current data record based on the new primary key generation token; Wherein, the token acquisition module is specifically used for: Check whether a primary key generation token is stored in the local storage unit; If yes, reading the master key from the local storage unit to generate a token; If not, obtaining client parameters, generating a token application request based on the client parameters, sending the token application request to the server, and receiving first response information returned by the server based on the token application request, wherein the first response information includes a primary key to generate a token; The clock dial-back judgment module is specifically used for: Obtaining a preset concurrency and a preset primary key count capacity of the application server to which the current client belongs, and determining a detection time threshold according to the preset concurrency and the preset primary key count capacity; Calculate the difference between the last primary key generation time and the current system time; If the difference is greater than the detection time threshold, it is determined that a clock rollback occurs; If the difference is less than or equal to the detection time threshold, it is determined that no clock rollback occurs.

10. A device for generating a temporal primary key, characterized in that: The device is applied in a server, and comprises: A token request receiving module, used for receiving a token request sent by a client; A token generation module, used to generate a primary key generation token corresponding to the client according to the token request; A token return module, used for returning the primary key generation token to the client, so that the client generates a temporal primary key of the current data record based on the primary key generation token; The token request includes client parameters, and the token generation module is specifically used to: generating a client identification number according to the client parameters; Allocating a first slot for the client, and binding an identifier of the first slot to the client identification number; Using the first slot to generate a primary key to generate a token; The token request includes a client identification number, and the token generation module is further used to: Determining a first slot allocated to the client according to the client identification number; Setting the first slot to an unavailable state and setting an unavailable expiration time; Allocating a second slot for the client, and binding an identifier of the second slot to the client identification number; Using the second slot to generate a primary key generation token; The unavailable expiration time is determined in the following manner: Determine information response time; Obtaining a preset concurrency and a preset primary key count capacity of the application server to which the client belongs, and determining a detection time threshold according to the preset concurrency and the preset primary key count capacity; The unavailable expiration time is determined according to the sum of the detection time threshold and the information response time.

11. An electronic device, characterized in that: The electronic device comprises: one or more processors; a storage device for storing one or more programs, When the one or more programs are executed by the one or more processors, the one or more processors implement the method according to any one of claims 1 to 8.

12. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the program is executed by a processor, the method according to any one of claims 1 to 8 is implemented.

13. A computer program product, comprising computer executable instructions, wherein the computer executable instructions are used to implement the method according to any one of claims 1 to 8 when executed.