Identity number generation method and device, equipment and storage medium
By implementing a double-layer anti-retest verification mechanism in the single number generation system, the global uniqueness of local cached number and key-value pairs is used to solve the repetition of the single number caused by Redis failure, ensuring the uniqueness of the single number and the service availability rate.
Patent Information
- Application Number
- CN202311270386.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2023-09-27
- Publication Date
- 2025-03-28
AI Technical Summary
Redis instance failure results in inconsistent data of master and slave nodes or cache loss, which poses a risk of generating duplicate order numbers.
By obtaining the order number category identification and order number to be checked for the current service, and obtaining the local cache order number at the local device, performing double-layer anti-re-checking. The first layer of anti-re-checking passes the local cache number to judge, and the second layer of anti-re-checking passes the global uniqueness of the key-value pair to ensure the uniqueness of the order number.
It effectively overcomes the repetition of order numbers caused by Redis failure, ensures the uniqueness of order numbers, and improves the availability rate of order number generation services.
Smart Images

Figure CN119719614A_ABST
Abstract
Description
Technical Field
[0001] The embodiments of the present invention relate to the field of data processing technology, and in particular to a method, device, equipment and storage medium for generating a single number. Background Art
[0002] The logistics order layer order number generation system provides a unique order number generation service. The system calls the Redis (remote dictionary service) cluster instance in sequence by passing in different uniqueKeys (unique identifiers) as input parameters by the caller. After using the atomic increment operation to generate the order number, it shifts the binary bits of the number of Redis machines to the left (for example, four machines are numbered with two binary digits, and eight machines are numbered with three binary digits), and then performs an "OR" operation with the Redis instance number to obtain the logistics order layer order number.
[0003] However, in the process of implementing the present invention, it is found that there are at least the following problems in the prior art:
[0004] When a Redis instance fails and data inconsistency between the master and slave nodes or cache loss occurs, there is a risk of generating duplicate order numbers. Summary of the invention
[0005] The embodiments of the present invention provide a method, device, equipment and storage medium for generating a tracking number to ensure the uniqueness of the tracking number and improve the availability of the tracking number generation service.
[0006] In a first aspect, an embodiment of the present invention provides a method for generating a tracking number, the method comprising:
[0007] Obtain a current order number category identifier and a current order number to be verified corresponding to the current business, and obtain a local cache order number corresponding to the current order number category identifier at the current device processing the current business;
[0008] In the case where it is determined according to the result of obtaining the locally cached order number that the current order number to be verified meets the first-layer anti-duplicate verification rule, a key-value pair is generated based on the current order number category identifier and the current order number to be verified;
[0009] When it is determined that the current order number to be verified satisfies the second-layer anti-duplicate verification rule according to the global uniqueness of the key-value pair, a target order number is generated based on the current order number to be verified.
[0010] In a second aspect, an embodiment of the present invention further provides a device for generating a single number, the device comprising:
[0011] The order number acquisition module is used to obtain the current order number category identifier and the current order number to be verified corresponding to the current business, and obtain the local cache order number corresponding to the current order number category identifier at the current device processing the current business;
[0012] A key-value pair generation module, configured to generate a key-value pair based on the current order number category identifier and the current order number to be verified, when it is determined that the current order number to be verified meets the first-layer anti-duplicate verification rule according to the result of obtaining the local cache order number;
[0013] The order number generation module is used to generate a target order number based on the current order number to be verified when it is determined that the current order number to be verified meets the second-level anti-duplicate verification rule according to the global uniqueness of the key-value pair.
[0014] In a third aspect, an embodiment of the present invention provides an electronic device, the electronic device comprising:
[0015] one or more processors;
[0016] A memory for storing one or more programs;
[0017] When the one or more programs are executed by the one or more processors, the one or more processors implement the method for generating a single number as provided in any embodiment of the present invention.
[0018] In a fourth aspect, an embodiment of the present invention further provides a computer-readable storage medium on which a computer program is stored, and when the program is executed by a processor, the method for generating a single number as provided in any embodiment of the present invention is implemented.
[0019] The embodiments of the above invention have the following advantages or beneficial effects:
[0020] The technical solution of the embodiment of the present invention is to obtain the current order number category identifier and the current order number to be verified corresponding to the current business, and obtain the local cached order number corresponding to the current order number category identifier at the current device processing the current business; when it is determined that the current order number to be verified meets the first layer anti-duplicate verification rules based on the result of obtaining the local cached order number, a key-value pair is generated based on the current order number category identifier and the current order number to be verified; when it is determined that the current order number to be verified meets the second layer anti-duplicate verification rules based on the global uniqueness of the key-value pair, a target order number is generated based on the current order number to be verified. Because the technical means of realizing double-layer anti-duplicate verification is adopted by determining that the current order number to be verified meets the first layer anti-duplicate verification based on the local cached order number, and then judging the global uniqueness of the order number category identifier and the order number to be verified, the technical problem of duplicate order numbers caused by Redis failure in the prior art is overcome, the uniqueness of the order number is guaranteed, and the availability of the order number generation service is improved. BRIEF DESCRIPTION OF THE DRAWINGS
[0021] Figure 1 A flowchart of a method for generating a tracking number provided by an embodiment of the present invention;
[0022] Figure 2a A flowchart of another method for generating a tracking number provided by an embodiment of the present invention;
[0023] Figure 2b A flowchart of a double-layer anti-duplicate verification based on Redis order number generation and automatic recovery of a faulty Redis instance provided in this embodiment;
[0024] Figure 3 A schematic diagram of the structure of a device for generating a single number according to an embodiment of the present invention;
[0025] Figure 4 It is a structural schematic diagram of an electronic device provided by an embodiment of the present invention. DETAILED DESCRIPTION
[0026] The present invention 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 invention, rather than to limit the present invention. It should also be noted that, for ease of description, only parts related to the present invention, rather than all structures, are shown in the accompanying drawings.
[0027] Figure 1 The flowchart of a method for generating a tracking number provided in an embodiment of the present invention is applicable to the case of generating a logistics tracking number. The method can be executed by a tracking number generating device, which can be implemented by software and / or hardware, and the tracking number generating device can be configured in a server integrated with a tracking number generating system.
[0028] Among them, the order number generation system may include multiple machines and devices that provide order number generation services to the outside world, and multiple Redis instances in the Redis cluster registration center. The order number generation principle of the order number generation system can be that the user initiates a request to a Redis instance in the Redis cluster registration center through a certain machine device, and the Redis instance generates the order number based on its own number, which is provided to the user by the machine device. For example, there are 4 Redis instances, and the binary numbers are 00, 01, 10 and 11, respectively. Among them, the Redis instance "01" generates 100 through an atomic increment operation based on the user request, and the final number to be verified can be obtained by combining "100" and "01" as "10001".
[0029] like Figure 1 As shown, the method specifically comprises the following steps:
[0030] S110, obtaining a current order number category identifier and a current order number to be verified corresponding to the current business, and obtaining a local cache order number corresponding to the current order number category identifier at a current device processing the current business.
[0031] The current business may refer to logistics-level business such as order business and waybill business issued by the user through the order number generation system, or other types of order number business.
[0032] The current order number category identifier may be an identifier used to distinguish the type of order number. For example, if the current business is an order business, the current order number category identifier, i.e., uniqueKey, may be "Order number"; if the current business is a waybill business, the current order number category identifier, i.e., uniqueKey, may be "waybill number". The current order number to be verified may refer to the order number generated by the Redis instance that processes the current business and has not been verified for anti-duplicate. The locally cached order number may refer to the order number generated by the Redis instance that corresponds to the current order number category identifier and is cached locally on the machine device (i.e., the current device) that processes the current business. For example, if the current order number category identifier is "Order number", the locally cached order number corresponding to "Order number" is searched from the local cache of the current device.
[0033] In this embodiment, the current order number category identifier corresponding to the current business and the current order number to be verified generated based on the current business can be obtained, and the local cached order number matching the current order number category identifier can be obtained from the machine device processing the current business.
[0034] S120. When it is determined that the current order number to be verified satisfies the first-layer anti-duplicate verification rule according to the result of obtaining the locally cached order number, a key-value pair is generated based on the current order number category identifier and the current order number to be verified.
[0035] In this embodiment, if the current order number category identifier is the category processed for the first time by the current device, then there is no locally cached order number matching the current order number category identifier in the local cache of the current device before, so it is necessary to determine the result of obtaining the locally cached order number.
[0036] Specifically, local cache information can be obtained from the current device; check whether the current order number category identifier exists in the local cache information; if the current order number category identifier exists in the local cache information, obtain the local cache order number corresponding to the current order number category identifier from the local cache information; if the current order number category identifier does not exist in the local cache information, determine that the local cache order number has not been obtained from the current device.
[0037] Optionally, if the current order number category identifier does not exist in the local cache information, a corresponding relationship between the current order number category identifier and the current order number to be verified is created in the local cache information. This can be understood as timely recording the order number to be verified generated by the Redis instance in the local cache information to facilitate subsequent anti-duplicate verification operations for other order numbers to be verified.
[0038] Furthermore, determining whether the current order number to be verified satisfies the first-layer anti-duplication verification rules based on the result of obtaining the locally cached order number may include: if the locally cached order number is not obtained from the current device, determining that the current order number to be verified satisfies the first-layer anti-duplication verification rules; if the locally cached order number is obtained from the current device, determining that the current order number to be verified satisfies the first-layer anti-duplication verification rules when the size relationship between the current order number to be verified and the locally cached order number meets the set conditions.
[0039] The set condition may refer to that the current order number to be verified is greater than the local cached order number. That is, when the current order number to be verified is greater than the local cached order number, it is determined that the current order number to be verified meets the first-level anti-duplicate verification rule; when the current order number to be verified is less than or equal to the local cached order number, it can be determined that the current order number to be verified does not meet the first-level anti-duplicate verification rule.
[0040] Exemplarily, based on the current order number category identifier "Order number", the current order number to be verified generated by the "01" Redis instance is "10001". If there is no "Order number" identifier in the local cache information, it is determined that "10001" meets the first-level anti-duplicate verification rule, and "Order number-10001" can be cached locally on the current device; if there is an "Order number" identifier in the local cache information, and the local cache order number is "Order number-1001", then 10001>1001, then it is determined that "10001" meets the first-level anti-duplicate verification rule; if the local cache order number is "100001", then 10001<100001, then it is determined that "10001" does not meet the first-level anti-duplicate verification rule.
[0041] S130. When it is determined that the current order number to be verified satisfies the second-layer anti-duplicate verification rule according to the global uniqueness of the key-value pair, a target order number is generated based on the current order number to be verified.
[0042] The key-value pair is composed of the current order number category identifier as the key and the current order number to be verified as the value.
[0043] Optionally, determining whether the current order number to be verified satisfies the second-layer anti-duplicate verification rules based on the global uniqueness of the key-value pair may include: determining that the current order number to be verified satisfies the second-layer anti-duplicate verification rules when the key-value pair is globally unique, and determining that the current order number to be verified does not satisfy the second-layer anti-duplicate verification rules when the key-value pair is not globally unique. The global uniqueness of the key-value pair may refer to the absence of a key-value pair identical to the key-value pair in each Redis instance and each machine device of the Redis cluster registration center of the order number generation system. Global uniqueness may be determined by the setNX operation.
[0044] Specifically, the second-layer anti-duplicate verification rule can be used to ensure the atomic operation of different requests calling the original order number generation system concurrently. The setNX operation can be used to ensure that only one request returns a true result under concurrency. For requests that return false results, the second-layer anti-duplicate verification is considered to have failed. After the second-layer anti-duplicate verification, the current order number to be verified is used as the target order number, and the current order number category identifier and the target order number can be asynchronously stored (for example, asynchronously stored in MySQL), and after being stored in the local cache, the target order number is returned as the result and the anti-duplicate call is considered to be successful.
[0045] The technical solution of the embodiment of the present invention is to obtain the current order number category identifier and the current order number to be verified corresponding to the current business, and obtain the local cached order number corresponding to the current order number category identifier at the current device processing the current business; when it is determined that the current order number to be verified meets the first layer anti-duplicate verification rules based on the result of obtaining the local cached order number, a key-value pair is generated based on the current order number category identifier and the current order number to be verified; when it is determined that the current order number to be verified meets the second layer anti-duplicate verification rules based on the global uniqueness of the key-value pair, a target order number is generated based on the current order number to be verified. Because the technical means of realizing double-layer anti-duplicate verification is adopted by determining that the current order number to be verified meets the first layer anti-duplicate verification based on the local cached order number, and then judging the global uniqueness of the order number category identifier and the order number to be verified, the technical problem of duplicate order numbers caused by Redis failure in the prior art is overcome, the uniqueness of the order number is guaranteed, and the availability of the order number generation service is improved.
[0046] Figure 2a This is a flow chart of another method for generating a tracking number provided by an embodiment of the present invention. This embodiment further adds operations based on the above embodiment. Figure 2a As shown, the method includes:
[0047] S210, obtaining a current order number category identifier and a current order number to be verified corresponding to the current business, and obtaining a local cache order number corresponding to the current order number category identifier at a current device processing the current business.
[0048] S220. When it is determined that the current order number to be verified satisfies the first-layer anti-duplicate verification rule based on the result of obtaining the locally cached order number, a key-value pair is generated based on the current order number category identifier and the current order number to be verified.
[0049] S230. When it is determined that the current order number to be verified satisfies the second-layer anti-duplicate verification rule according to the global uniqueness of the key-value pair, a target order number is generated based on the current order number to be verified.
[0050] S240. When it is determined that the current order number to be verified does not satisfy the first-layer anti-duplicate verification rule or the second-layer anti-duplicate verification rule, a faulty remote dictionary service instance is determined according to the remote dictionary service instance number in the current order number to be verified, and the faulty remote dictionary service instance is taken offline.
[0051] For example, there are 4 Redis instances online in the Redis cluster registration center, which are numbered 00, 01, 10 and 11 respectively. The current number to be verified that does not meet the first-level anti-duplicate verification rules or the second-level anti-duplicate verification rules is "10001". According to the last two digits of the current number to be verified, it can be determined that the faulty remote dictionary service instance (i.e., the faulty Redis instance) is the Redis instance numbered "01", and the instance is taken offline from the Redis cluster registration center, that is, the faulty Redis instance is not used to provide number generation service.
[0052] S250, using the new remote dictionary service instance to generate a new to-be-verified order number for the current business, using the new to-be-verified order number as the current to-be-verified order number and returning to execute the operation of determining whether the current to-be-verified order number satisfies the first-layer anti-duplicate verification rules based on the result of obtaining the locally cached order number, until it is determined that the current to-be-verified order number satisfies the second-layer anti-duplicate verification rules.
[0053] The new remote dictionary service instance may be another online Redis instance in the Redis cluster registration center except the failed Redis instance.
[0054] In this embodiment, in the process of processing the current business, if the current to-be-verified order number does not meet the first-layer anti-duplicate verification rules or the second-layer anti-duplicate verification rules, the current business will not be stopped. The new remote dictionary service instance can be used to process the current business, generate a new to-be-verified order number as the current to-be-verified order number, and return to execute the operation of determining whether the current to-be-verified order number meets the first-layer anti-duplicate verification rules based on the result of obtaining the local cache order number, until it is determined that the current to-be-verified order number meets the second-layer anti-duplicate verification rules. The advantage of this setting is that the order number can be automatically and synchronously re-requested and the current request can be ensured not to fail after verification.
[0055] S260. In the first asynchronous thread, obtain the distributed lock of the faulty remote dictionary service instance; check the global recovery identifier of the faulty remote dictionary service instance; when the global recovery identifier is the first identifier, process the faulty remote dictionary service instance using a preset recovery logic in the second asynchronous thread, and reset the global recovery identifier to the second identifier to release the distributed lock; when the global recovery identifier is the second identifier, directly release the distributed lock and end the first asynchronous thread.
[0056] The operation S260 may be performed after S240, and specifically may be performed in parallel with S250.
[0057] Distributed locks can be a solution to achieve thread-safe mutual exclusion access in a distributed environment. It is based on Redis and can be used to control the synchronous execution of multiple processes or multiple servers. It can be understood that a faulty Redis instance has only one lock and one key. If there are multiple requests to recover the faulty Redis instance, the purpose of obtaining the distributed lock of the faulty Redis instance is to obtain the unique key of the unique lock of the faulty Redis instance at a time so as to process the faulty Redis instance.
[0058] For example, if at this time there are two recovery requests for the current to-be-verified order number "10001" for the faulty remote dictionary service instance, and the request time of one "10001" is earlier than the other "10001", then it can be understood that the "10001" with the earlier request time obtains the distributed lock of the faulty remote dictionary service instance.
[0059] According to the global recovery flag, it can be determined what operation can be performed after the global recovery flag is recognized in the first asynchronous thread. The first flag and the second flag can be flags with opposite meanings, and the first flag is the default value of the global recovery flag, for example, the first flag can be "false" and the second flag can be "true".
[0060] It can be understood that after obtaining the distributed lock of the faulty remote dictionary service instance, if the global recovery flag is identified as the default value (such as "false"), it means that the faulty Redis instance is in a state where the recovery logic can be continued to be executed, that is, the operation of processing the faulty remote dictionary service instance using the preset recovery logic in the second asynchronous thread can be continued, as well as the operation of resetting the global recovery flag to the second flag to release the distributed lock; if the global recovery flag is identified as a non-default value (such as "true"), it means that the faulty Redis instance is not in a state where the recovery logic can be continued to be executed, and the recovery logic cannot be continued, that is, the distributed lock can be directly released and the first asynchronous thread can be ended.
[0061] Continuing with the previous example, after the earlier "10001" obtains the distributed lock of the faulty remote dictionary service instance, it recognizes that the global recovery identifier is the default first identifier. At this time, the second asynchronous thread can be submitted immediately, and the global recovery identifier is reset from the default first identifier to the second identifier synchronously. In this way, even if the later "10001" obtains the distributed lock, it will release the distributed lock at the next moment because it recognizes that the global recovery identifier is the second identifier.
[0062] In one case, "processing the faulty remote dictionary service instance using the preset recovery logic in the second asynchronous thread" in S260 can be refined as follows: after waiting for a preset time period, clear the cached data of the faulty remote dictionary service instance; add the preset initial value and the current order number to be verified to obtain the processed order number; use the current order number category identifier and the processed order number to reset the cached data of the faulty remote dictionary service instance, and put the faulty remote dictionary service instance back online; reset the global recovery identifier to the first identifier.
[0063] Since the Redis instance will replace the previous order number with the cached one after each order number to be verified is generated, after a failure occurs, its own cached data can be cleared first, and a very large number (i.e., the preset initial value) can be used to process the current order number to be verified into a very large number (i.e., the processed order number) to avoid generating duplicate order numbers again after going online due to delays. After resetting the cached data of the faulty remote dictionary service instance using the processed order number and the current order number category identifier, the faulty remote dictionary service instance is put back online in the Redis cluster registration center, and the global identifier of the faulty remote dictionary service instance is reset to "false".
[0064] It should be noted that the preset time period can be, for example, 5 seconds. The purpose of waiting for this preset time period is to make the execution time of the second asynchronous thread later than "when the global recovery flag is the first flag, reset the global recovery flag to the second flag and release the distributed lock" in S260, so as to avoid resetting the global flag to "false" in the second asynchronous thread and then resetting it to "true".
[0065] The technical solution of the embodiment of the present invention is to obtain the current order number category identifier and the current order number to be verified corresponding to the current business, and obtain the local cached order number corresponding to the current order number category identifier at the current device processing the current business; when it is determined that the current order number to be verified meets the first-level anti-duplication verification rules according to the acquisition result of the local cached order number, a key-value pair is generated based on the current order number category identifier and the current order number to be verified; when it is determined that the current order number to be verified meets the second-level anti-duplication verification rules according to the global uniqueness of the key-value pair, a target order number is generated based on the current order number to be verified; when it is determined that the current order number to be verified does not meet the first-level anti-duplication verification rules or the second-level anti-duplication verification rules, a faulty remote dictionary service instance is determined according to the remote dictionary service instance number in the current order number to be verified, and the faulty remote dictionary service instance is offline. a faulty remote dictionary service instance; using the new remote dictionary service instance to generate a new to-be-verified order number for the current business, using the new to-be-verified order number as the current to-be-verified order number and returning to perform an operation of determining whether the current to-be-verified order number satisfies the first-layer anti-duplicate verification rule based on the result of obtaining the locally cached order number, until it is determined that the current to-be-verified order number satisfies the second-layer anti-duplicate verification rule; in the first asynchronous thread, obtaining the distributed lock of the faulty remote dictionary service instance; checking the global recovery identifier of the faulty remote dictionary service instance; when the global recovery identifier is the first identifier, processing the faulty remote dictionary service instance using the preset recovery logic in the second asynchronous thread, resetting the global recovery identifier to the second identifier, and releasing the distributed lock; when the global recovery identifier is the second identifier, directly releasing the distributed lock and terminating the first asynchronous thread. Because the current order number to be verified is double-layered and double-layered anti-duplicate verification is implemented, a new order number to be verified is regenerated and re-verified for the business that has not implemented double-layered anti-duplicate verification, and the faulty Redis instance is asynchronously and automatically recovered, the technical problem of duplicate order numbers caused by Redis failure in the existing technology is overcome, the uniqueness of the order number is guaranteed, the availability of the order number generation service is improved, and the business request is ensured not to fail. The faulty Redis instance can be put back online after a short suspension of service, reducing operation and maintenance costs and improving service robustness.
[0066] In order to enable those skilled in the art to better understand the method for generating a single number of the present invention, Figure 2b A flowchart of double-layer anti-duplicate verification based on Redis order number generation and automatic recovery of faulty Redis instances is provided for this embodiment.
[0067] 1. Verification Module
[0068] 1. First-level anti-duplicate verification rules
[0069] On the machine that provides the order number generation service (equivalent to the current device that processes the current business), add a local cache to verify the current uniqueKey (equivalent to the current order number category identifier) and the current number (equivalent to the current order number to be verified). The specific verification logic is as follows:
[0070] a. Check whether the current uniqueKey exists in the local cache;
[0071] b. If the current uniqueKey does not exist in the local cache, return true and consider that the anti-duplicate verification rule verification is successful;
[0072] c. If the current uniqueKey exists in the local cache, but the local cache order number corresponding to the current uniqueKey in the local cache is ≥ the current incoming order number (that is, the current order number to be verified), false is returned and the anti-duplicate verification rule verification is considered to have failed;
[0073] d. If the current uniqueKey exists in the local cache, and the local cache order number corresponding to the current uniqueKey in the local cache is smaller than the order number passed in this time, true is returned and the anti-duplicate verification rule verification is considered successful.
[0074] 2. Second-layer anti-duplicate verification rules
[0075] The second level of verification ensures the atomic operation of the original order number generation system when different order number requests are concurrently called. By using setNX, it can be ensured that only the unique request returns a true result under concurrency. For requests that return false results, the anti-duplicate calculation rule verification is considered to have failed. After the second level of anti-duplicate verification, the uniqueKey and order number are asynchronously stored (eg asynchronously stored in MySQL), and after being stored in the local cache, the order number is returned as the result and the anti-duplicate call is considered successful. The specific verification logic detailed steps are as follows:
[0076] a. Use the current uniqueKey as the key and the current number as the value to perform the setNX operation. If true is returned, the current number is returned and the anti-duplicate verification rule verification is considered successful.
[0077] b. If the result returns false, it is considered that the anti-duplicate verification rule verification has failed.
[0078] 2. Automatic recovery module
[0079] 1. Synchronous recovery logic
[0080] The synchronization recovery logic is triggered after any of the two layers of anti-duplicate verification rules of the verification module (the first layer of anti-duplicate verification failed (the current number to be verified ≤ the local cache number) and the second layer of anti-duplicate verification failed (setNX returns false result)) fails. The detailed steps are as follows:
[0081] a. Delete the faulty Redis instance from the Redis cluster registration center;
[0082] b. Reselect a Redis instance from the Redis cluster registration center to generate a new verification number;
[0083] c. Re-verify the two-layer anti-duplicate verification rules of the verification module based on the newly generated order number to be verified.
[0084] d. Generate the target order number and return it.
[0085] 2. Asynchronous recovery logic
[0086] After completing the above synchronous recovery logic and only deregistering successful requests, the Redis instance with abnormal conditions (i.e., the faulty Redis instance) is processed through an asynchronous thread. The detailed steps are as follows:
[0087] a. Obtain the distributed lock of the failed Redis instance;
[0088] b. Check the global recovery flag of the failed Redis instance. If it is false, continue with operations c, d, and e. If it is true, directly release the distributed lock and return.
[0089] c. Submit the second asynchronous thread to process the recovery logic of the failed Redis instance. The specific logic is as follows:
[0090] a) Delay waiting for 5 seconds; the purpose of waiting for 5 seconds is to make the execution time of operation c later than that of operation d, so as to avoid the global recovery flag being reset to "false" and then reset to "true" again after operation e) is executed in operation c;
[0091] b) Clear the cache data of the failed Redis instance;
[0092] c) Reset the cached data of the failed Redis instance based on the current order number category identifier and the order number to be verified recorded asynchronously;
[0093] d) Re-register the Redis instance to the Redis cluster registration center;
[0094] e) Reset the global recovery flag of the failed Redis instance to false;
[0095] d. Set the global recovery flag of the failed Redis instance to true;
[0096] e. Release the distributed lock.
[0097] According to this asynchronous recovery logic, since the end time of the five operations of the recovery logic a), b), c), d) and e) of the second asynchronous thread cannot be determined, the d operation synchronously sets the global recovery flag to true after the c operation submits the second asynchronous thread. This can prevent other recovery requests for the current faulty Redis instance (such as the later "10001" in the example of S260) from being able to execute the c operation after obtaining the distributed lock of the current faulty Redis instance. Therefore, the purpose of setting the global recovery flag is to further lock the asynchronous recovery logic based on the distributed lock.
[0098] In this embodiment, the verification module uses two layers of anti-duplicate verification rules to verify the order number to be verified and ensure the uniqueness of the returned target order number, thereby improving the availability of the order number generation service. When an exception occurs in the Redis instance, the automatic recovery module synchronously re-requests the order number and ensures that the request does not fail after verification, and performs asynchronous automatic recovery on the recovery logic of Redis, and resumes the service after a short stop, thereby reducing operation and maintenance costs and improving service robustness.
[0099] The following is an embodiment of the order number generating device provided by an embodiment of the present invention. The device and the order number generating method of the above-mentioned embodiment 1 belong to the same inventive concept. For details not described in detail in the embodiment of the order number generating device, please refer to the contents of the above-mentioned embodiments.
[0100] Figure 3 The structure diagram of the order number generating device provided by the embodiment of the present invention is configured in the order number generating system, such as Figure 3 As shown, the device may include: a tracking number acquisition module 310, a key-value pair generation module 320 and a tracking number generation module 330. Among them:
[0101] The order number acquisition module 310 is used to obtain the current order number category identifier and the current order number to be verified corresponding to the current business, and obtain the local cache order number corresponding to the current order number category identifier at the current device processing the current business;
[0102] A key-value pair generation module 320 is used to generate a key-value pair based on the current order number category identifier and the current order number to be verified, when it is determined that the current order number to be verified meets the first-level anti-duplicate verification rule according to the result of obtaining the local cache order number;
[0103] The order number generation module 330 is used to generate a target order number based on the current order number to be verified when it is determined that the current order number to be verified meets the second-level anti-duplicate verification rule according to the global uniqueness of the key-value pair.
[0104] The technical solution of the embodiment of the present invention is to obtain the current order number category identifier and the current order number to be verified corresponding to the current business, and obtain the local cached order number corresponding to the current order number category identifier at the current device processing the current business; when it is determined that the current order number to be verified meets the first layer anti-duplicate verification rules based on the result of obtaining the local cached order number, a key-value pair is generated based on the current order number category identifier and the current order number to be verified; when it is determined that the current order number to be verified meets the second layer anti-duplicate verification rules based on the global uniqueness of the key-value pair, a target order number is generated based on the current order number to be verified. Because the technical means of realizing double-layer anti-duplicate verification is adopted by determining that the current order number to be verified meets the first layer anti-duplicate verification based on the local cached order number, and then judging the global uniqueness of the order number category identifier and the order number to be verified, the technical problem of duplicate order numbers caused by Redis failure in the prior art is overcome, the uniqueness of the order number is guaranteed, and the availability of the order number generation service is improved.
[0105] In the above device, optionally, the key-value pair generating module 320 can be specifically used for:
[0106] If the local cached order number is not obtained from the current device, determining that the current order number to be verified meets the first-level anti-duplicate verification rule;
[0107] If the local cached order number is obtained from the current device, then when the size relationship between the current order number to be verified and the local cached order number meets the set conditions, it is determined that the current order number to be verified meets the first-level anti-duplicate verification rule.
[0108] In the above device, optionally, the order number acquisition module 310 can be specifically used for:
[0109] Obtaining local cache information from the current device;
[0110] Check whether the current order number category identifier exists in the local cache information;
[0111] In the case where the current order number category identifier exists in the local cache information, obtaining the local cache order number corresponding to the current order number category identifier from the local cache information;
[0112] When the current order number category identifier does not exist in the local cache information, it is determined that the local cache order number is not obtained from the current device.
[0113] The above device may optionally further include a corresponding relationship establishing module, which is used to:
[0114] In the case that the current order number category identifier does not exist in the local cache information, a corresponding relationship between the current order number category identifier and the current order number to be verified is created in the local cache information.
[0115] In the above device, optionally, the setting condition includes:
[0116] The current order number to be verified is greater than the local cache order number.
[0117] In the above device, optionally, the order number generating module 330 can be specifically used for:
[0118] In the case where the key-value pair is globally unique, it is determined that the current order number to be verified satisfies the second-layer anti-duplicate verification rule, and the global uniqueness means that there is no key-value pair identical to the key-value pair in the order number generation system.
[0119] The above device may optionally further include a module for regenerating and verifying the order number to be verified, which is used to, when it is determined that the current order number to be verified does not satisfy the first-layer anti-duplicate verification rule or the second-layer anti-duplicate verification rule:
[0120] Determine the faulty remote dictionary service instance according to the remote dictionary service instance number in the current to-be-verified order number, and take the faulty remote dictionary service instance offline;
[0121] A new order number to be verified is generated for the current business using a new remote dictionary service instance, and the new order number to be verified is used as the current order number to be verified and returned to execute an operation of determining whether the current order number to be verified satisfies the first layer anti-duplicate verification rules based on the result of obtaining the locally cached order number, until it is determined that the current order number to be verified satisfies the second layer anti-duplicate verification rules.
[0122] The above device may optionally further include a faulty remote dictionary service instance recovery module, which is used to, after the faulty remote dictionary service instance is offline, include:
[0123] A distributed lock acquisition unit, configured to acquire, in a first asynchronous thread, a distributed lock of the faulty remote dictionary service instance;
[0124] A global recovery identifier checking unit, used for checking the global recovery identifier of the faulty remote dictionary service instance;
[0125] A distributed lock releasing unit, configured to process the faulty remote dictionary service instance by using a preset recovery logic in a second asynchronous thread when the global recovery identifier is the first identifier, and reset the global recovery identifier to the second identifier to release the distributed lock;
[0126] The first asynchronous thread termination unit is used to directly release the distributed lock and terminate the first asynchronous thread when the global recovery identifier is the second identifier.
[0127] In the above device, optionally, the distributed lock release unit can be specifically used for:
[0128] After waiting for a preset time period, clearing the cache data of the faulty remote dictionary service instance;
[0129] Add the preset initial value and the current order number to be verified to obtain the processed order number;
[0130] Reset the cache data of the faulty remote dictionary service instance by using the current order number category identifier and the processed order number, and bring the faulty remote dictionary service instance back online;
[0131] The global recovery flag is reset to the first flag.
[0132] The order number generating device provided in the embodiment of the present invention can execute the order number generating method provided in the first embodiment of the present invention, and has the corresponding functional modules and beneficial effects for executing the order number generating method.
[0133] Figure 4 A schematic diagram of the structure of an electronic device provided by an embodiment of the present invention. Figure 4 A block diagram of an exemplary server 12 suitable for use in implementing embodiments of the present invention is shown. Figure 4 The server 12 shown is only an example and should not bring any limitation to the functions and scope of use of the embodiments of the present invention.
[0134] like Figure 4 As shown, the server 12 is in the form of a general purpose computing device. The components of the server 12 may include, but are not limited to: one or more processors or processing units 16, a system memory 28, and a bus 18 that connects various system components (including the system memory 28 and the processing unit 16).
[0135] Bus 18 represents one or more of several types of bus structures, including a memory bus or memory controller, a peripheral bus, an accelerated graphics port, a processor or a local bus using any of a variety of bus architectures. By way of example, these architectures include, but are not limited to, an Industry Standard Architecture (ISA) bus, a Micro Channel Architecture (MAC) bus, an Enhanced ISA bus, a Video Electronics Standards Association (VESA) local bus, and a Peripheral Component Interconnect (PCI) bus.
[0136] The server 12 typically includes a variety of computer system readable media. These media can be any available media that can be accessed by the server 12, including volatile and non-volatile media, removable and non-removable media.
[0137] The system memory 28 may include computer system readable media in the form of volatile memory, such as random access memory (RAM) 30 and / or cache memory 32. The server 12 may further include other removable / non-removable, volatile / non-volatile computer system storage media. By way of example only, the storage system 34 may be used to read and write non-removable, non-volatile magnetic media ( Figure 4 not shown, usually called a "hard drive"). Although Figure 4 Not shown in the figure, a disk drive for reading and writing to a removable non-volatile disk (e.g., a "floppy disk"), and an optical disk drive for reading and writing to a removable non-volatile optical disk (e.g., a CD-ROM, a DVD-ROM, or other optical media) may be provided. In these cases, each drive may be connected to the bus 18 via one or more data medium interfaces. The system memory 28 may include at least one program product having a set (e.g., at least one) of program modules that are configured to perform the functions of the various embodiments of the present invention.
[0138] A program / utility 40 having a set (at least one) of program modules 42 may be stored, for example, in system memory 28, such program modules 42 including, but not limited to, an operating system, one or more application programs, other program modules, and program data, each of which or some combination may include an implementation of a network environment. Program modules 42 generally perform the functions and / or methods of the embodiments described herein.
[0139] The server 12 may also communicate with one or more external devices 14 (e.g., keyboards, pointing devices, displays 24, etc.), may communicate with one or more devices that enable a user to interact with the server 12, and / or may communicate with any device that enables the server 12 to communicate with one or more other computing devices (e.g., network cards, modems, etc.). Such communication may be performed via an input / output (I / O) interface 22. In addition, the server 12 may also communicate with one or more networks (e.g., local area networks (LANs), wide area networks (WANs), and / or public networks, such as the Internet) via a network adapter 20. As shown, the network adapter 20 communicates with other modules of the server 12 via a bus 18. It should be understood that, although not shown in the figure, other hardware and / or software modules may be used in conjunction with the server 12, including, but not limited to, microcode, device drivers, redundant processing units, external disk drive arrays, RAID systems, tape drives, and data backup storage systems.
[0140] The processing unit 16 executes various functional applications and data processing by running the program stored in the system memory 28, for example, implementing a method for generating a single number provided in the first embodiment of the present invention, the method comprising:
[0141] Obtain a current order number category identifier and a current order number to be verified corresponding to the current business, and obtain a local cache order number corresponding to the current order number category identifier at the current device processing the current business;
[0142] In the case where it is determined according to the result of obtaining the locally cached order number that the current order number to be verified meets the first-layer anti-duplicate verification rule, a key-value pair is generated based on the current order number category identifier and the current order number to be verified;
[0143] When it is determined that the current order number to be verified satisfies the second-layer anti-duplicate verification rule according to the global uniqueness of the key-value pair, a target order number is generated based on the current order number to be verified.
[0144] Of course, those skilled in the art can understand that the processor can also implement the technical solution of the method for generating a tracking number provided in any embodiment of the present invention.
[0145] This embodiment provides a computer-readable storage medium, on which a computer program is stored. When the program is executed by a processor, the steps of a method for generating a single number provided in the above embodiment of the present invention are implemented. The method includes:
[0146] Obtain a current order number category identifier and a current order number to be verified corresponding to the current business, and obtain a local cache order number corresponding to the current order number category identifier at the current device processing the current business;
[0147] In the case where it is determined according to the result of obtaining the locally cached order number that the current order number to be verified meets the first-layer anti-duplicate verification rule, a key-value pair is generated based on the current order number category identifier and the current order number to be verified;
[0148] When it is determined that the current order number to be verified satisfies the second-layer anti-duplicate verification rule according to the global uniqueness of the key-value pair, a target order number is generated based on the current order number to be verified.
[0149] The computer storage medium of the embodiment of the present invention can adopt any combination of one or more computer-readable media. The computer-readable medium can be a computer-readable signal medium or a computer-readable storage medium. The computer-readable storage medium can be, for example, but not limited to: an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, device or device, or any combination of the above. More specific examples (non-exhaustive list) of computer-readable storage media include: an electrical connection with one or more wires, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above. In this document, a computer-readable storage medium can be any tangible medium containing or storing a program, which can be used by an instruction execution system, device or device or used in combination with it.
[0150] Computer-readable signal media may include data signals propagated in baseband or as part of a carrier wave, which carry computer-readable program code. Such propagated data signals may take a variety of forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination of the above. Computer-readable signal media may also be any computer-readable medium other than a computer-readable storage medium, which may send, propagate, or transmit a program for use by or in conjunction with an instruction execution system, apparatus, or device.
[0151] The program code embodied on the computer readable medium may be transmitted using any appropriate medium, including but not limited to wireless, wireline, optical fiber cable, RF, etc., or any suitable combination of the foregoing.
[0152] Computer program code for performing the operations of the present invention may be written in one or more programming languages or a combination thereof, including object-oriented programming languages such as Java, Smalltalk, C++, and conventional procedural programming languages such as "C" or similar programming languages. The program code may be executed entirely on the user's computer, partially on the user's computer, as a separate software package, partially on the user's computer and partially on a remote computer, or entirely on a remote computer or server. In the case of a remote computer, the remote computer may be connected to the user's computer through any type of network, including a local area network (LAN) or a wide area network (WAN), or may be connected to an external computer (e.g., via the Internet using an Internet service provider).
[0153] It should be understood by those skilled in the art that the modules or steps of the present invention described above can be implemented by a general-purpose computing device, they can be concentrated on a single computing device, or distributed on a network composed of multiple computing devices, optionally, they can be implemented by a program code executable by a computer device, so that they can be stored in a storage device and executed by the computing device, or they can be made into individual integrated circuit modules, or multiple modules or steps therein can be made into a single integrated circuit module for implementation. Thus, the present invention is not limited to any specific combination of hardware and software.
[0154] Note that the above are only preferred embodiments of the present invention and the technical principles used. Those skilled in the art will understand that the present invention 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 invention. Therefore, although the present invention has been described in more detail through the above embodiments, the present invention is not limited to the above embodiments, and may include more other equivalent embodiments without departing from the concept of the present invention, and the scope of the present invention is determined by the scope of the appended claims.
Claims
1. A method for generating a single number, characterized in that: The method comprises: Obtain a current order number category identifier and a current order number to be verified corresponding to the current business, and obtain a local cache order number corresponding to the current order number category identifier at the current device processing the current business; In the case where it is determined according to the result of obtaining the locally cached order number that the current order number to be verified meets the first-layer anti-duplicate verification rule, a key-value pair is generated based on the current order number category identifier and the current order number to be verified; When it is determined that the current order number to be verified satisfies the second-layer anti-duplicate verification rule according to the global uniqueness of the key-value pair, a target order number is generated based on the current order number to be verified.
2. The method according to claim 1, characterized in that Determining, based on the result of obtaining the locally cached order number, that the current order number to be verified satisfies the first-layer anti-duplicate verification rule includes: If the local cached order number is not obtained from the current device, determining that the current order number to be verified meets the first-level anti-duplicate verification rule; If the local cached order number is obtained from the current device, then when the size relationship between the current order number to be verified and the local cached order number meets the set conditions, it is determined that the current order number to be verified meets the first-level anti-duplicate verification rule.
3. The method according to claim 2, characterized in that Acquiring, at a current device processing the current service, a locally cached order number corresponding to the current order number category identifier, comprising: Obtaining local cache information from the current device; Check whether the current order number category identifier exists in the local cache information; In the case where the current order number category identifier exists in the local cache information, obtaining the local cache order number corresponding to the current order number category identifier from the local cache information; When the current order number category identifier does not exist in the local cache information, it is determined that the local cache order number is not obtained from the current device.
4. The method according to claim 3, characterized in that Also includes: In the case that the current order number category identifier does not exist in the local cache information, a corresponding relationship between the current order number category identifier and the current order number to be verified is created in the local cache information.
5. The method according to claim 2, characterized in that: The setting conditions include: The current order number to be verified is greater than the local cache order number.
6. The method according to claim 1, characterized in that Determining that the current to-be-verified order number satisfies the second-layer anti-duplicate verification rule according to the global uniqueness of the key-value pair includes: In the case where the key-value pair is globally unique, it is determined that the current order number to be verified satisfies the second-layer anti-duplicate verification rule, and the global uniqueness means that there is no key-value pair identical to the key-value pair in the order number generation system.
7. The method according to any one of claims 1 to 6, characterized in that: In the case where it is determined that the current to-be-verified order number does not satisfy the first-layer anti-duplicate verification rule or the second-layer anti-duplicate verification rule, the method further includes: Determine the faulty remote dictionary service instance according to the remote dictionary service instance number in the current to-be-verified order number, and take the faulty remote dictionary service instance offline; A new order number to be verified is generated for the current business using a new remote dictionary service instance, and the new order number to be verified is used as the current order number to be verified and returned to execute an operation of determining whether the current order number to be verified satisfies the first layer anti-duplicate verification rules based on the result of obtaining the locally cached order number, until it is determined that the current order number to be verified satisfies the second layer anti-duplicate verification rules.
8. The method according to claim 7, characterized in that After the faulty remote dictionary service instance is offline, the following steps are also included: In the first asynchronous thread, acquiring a distributed lock of the faulty remote dictionary service instance; Checking the global recovery flag of the faulty remote dictionary service instance; When the global recovery identifier is the first identifier, the faulty remote dictionary service instance is processed by using a preset recovery logic in the second asynchronous thread, and the global recovery identifier is reset to the second identifier, and the distributed lock is released; When the global recovery identifier is the second identifier, the distributed lock is directly released and the first asynchronous thread is terminated.
9. The method according to claim 8, characterized in that Processing the faulty remote dictionary service instance by using preset recovery logic in the second asynchronous thread includes: After waiting for a preset time period, clearing the cache data of the faulty remote dictionary service instance; The preset initial value and the current number to be verified are added to obtain the processed number; Reset the cache data of the faulty remote dictionary service instance by using the current order number category identifier and the processed order number, and bring the faulty remote dictionary service instance back online; The global recovery flag is reset to the first flag.
10. A device for generating a single number, characterized in that: include: The order number acquisition module is used to obtain the current order number category identifier and the current order number to be verified corresponding to the current business, and obtain the local cache order number corresponding to the current order number category identifier at the current device processing the current business; A key-value pair generation module, configured to generate a key-value pair based on the current order number category identifier and the current order number to be verified, when it is determined that the current order number to be verified meets the first-layer anti-duplicate verification rule according to the result of obtaining the local cache order number; The order number generation module is used to generate a target order number based on the current order number to be verified when it is determined that the current order number to be verified meets the second-level anti-duplicate verification rule according to the global uniqueness of the key-value pair.
11. An electronic device, characterized in that: The electronic device comprises: one or more processors; A memory 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 for generating a single number as described in any one of claims 1 to 9.
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 for generating a single number as described in any one of claims 1 to 9 is implemented.