A method and device for resource allocation
By pre -loaded the resources to the database in the application server, and handle user requests and resource loading in the first and second threads, the problem of low efficiency of resource acquisition in existing technology has been solved. Resource distribution.
Patent Information
- Application Number
- CN202011187131.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2020-10-30
- Publication Date
- 2025-01-24
- Estimated Expiration
- 2040-10-30
AI Technical Summary
When users collect coupons, the existing technology will seize the exclusive lock of the database, making other threads unable to execute in parallel, resulting in inefficient users to collect coupons.
The application server loads the resources to be allocated from the database in advance, and executes the user client's resource acquisition request in parallel in the first thread. At the same time, it determines in the second thread whether it is necessary to continue loading the resources to be allocated to avoid resource shortages.
It realizes parallel processing of users receiving coupons, improves the efficiency, timeliness and efficiency of resource distribution, and avoids the repeated allocation of the same resource.
Smart Images

Figure CN112286685B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of financial technology (Fintech), and in particular to a method and device for resource allocation. Background Art
[0002] With the development of computer technology, more and more technologies (such as blockchain, cloud computing or big data) are being applied in the financial field. The traditional financial industry is gradually transforming into financial technology, and big data technology is no exception. However, due to the security and real-time requirements of the financial and payment industries, higher requirements are also placed on big data technology.
[0003] In the prior art, users generally initiate a request to receive a coupon through a user client, and the user client then obtains the coupon to be received from a database (such as MySQL) through an application server. After the user receives the coupon, the database registers the recipient and the time of receipt, and changes the status of the coupon to received. However, this method will seize the exclusive lock of the database when the user receives the coupon, making it impossible for other threads to execute in parallel, that is, the database can only issue coupons to one user at a time, resulting in low efficiency for users to receive coupons.
[0004] Therefore, a parallel coupon collection method is now needed to improve the efficiency of users collecting coupons. Summary of the invention
[0005] The embodiment of the present invention provides a method and device for allocating resources, which are used to improve the efficiency, timeliness and high efficiency of distributing resources.
[0006] In a first aspect, an embodiment of the present invention provides a method for resource allocation, including:
[0007] The application server obtains the resource acquisition request initiated by the user client;
[0008] The application server acquires the first resource sent to the user client based on the first thread and changes the state of the first resource in the database to allocated; the first resource is a resource to be allocated in the database pre-loaded by the application server to the application server;
[0009] When the application server determines based on the second thread that the number of resources in the resource queue of the application server does not meet the preset condition, the application server continues to load the resources to be allocated from the database to the resource queue; the preset condition is determined according to the throughput of the application server.
[0010] In the above technical solution, since the application server has pre-loaded the resources to be allocated from the database, the resource acquisition requests of each user client can be executed in parallel in the application server through the first thread, and after determining the resources to be provided to the user client, the application server changes the state of the resources in the database, thereby avoiding repeated allocation of the same resource. At the same time, the application server determines whether it is necessary to continue to load the resources to be allocated to the application server through the second thread, thereby avoiding the situation where the application server lacks resources when providing resources to the user client, further improving the efficiency of distributing resources. It should be noted that the first thread and the second thread are asynchronously performed simultaneously without affecting each other, thereby ensuring the timeliness and efficiency of distributing resources.
[0011] Optionally, the application server acquires, based on the first thread, a first resource sent to the user client and changes a state of the first resource in the database to allocated, including:
[0012] The application server acquires a first resource from a resource queue of the application server based on the first thread;
[0013] The application server sends an update status request for the first resource to the database; the update status request is used to update the allocation status of the first resource;
[0014] After receiving the update confirmation result, the application server sends the first resource to the user client.
[0015] In the above technical solution, the application server determines the first resource in the resource queue of the application server based on the first thread. The resource queue of the application server is the resources to be allocated that are pre-loaded from the database. Then, an update status request of the first resource is sent to the database, and after receiving the update confirmation result of the first resource, the first resource is sent to the user client. Because no locking mechanism is required when sending resources to the user client, and no need to obtain the first resource from the database, parallel distribution of resources can be achieved, and repeated allocation of the same resource is avoided, thereby improving the efficiency of distributing resources.
[0016] Optionally, the throughput of the application server includes the quantity of resources to be allocated loaded by the application server, the loading time, and a preset rate of distributing the resources to be allocated;
[0017] The preset condition is determined according to the throughput of the application server, including:
[0018] The application server determines the number of resources to be allocated loaded by the application server according to the product of the loading time and a preset rate of distributing the resources to be allocated; the preset condition is that the number of resources in the resource queue of the application server is greater than the number of resources to be allocated loaded by the application server.
[0019] In the above technical solution, the application server loads the resources to be allocated from the database to the resource queue based on the second thread, which does not affect the first thread. While distributing resources based on the first thread, the application server loads the resources to be allocated to ensure that when the user client initiates a resource acquisition request, the number of resources to be allocated loaded by the application server is greater than the product of the loading time and the preset rate of distributing the resources to be allocated, ensuring that the number of resources to be allocated loaded each time is greater than the number distributed within the loading time, the number of resources in the resource queue of the application server is greater than the number of resources to be allocated loaded by the application server, ensuring that during the loading process, the resources to be allocated in the resource queue of the application server can be distributed to the user client, over and over again, ensuring that the resource queue always stores the resources to be allocated, does not affect the distribution of the first resource, and ensures the timeliness of the distribution of resources.
[0020] Optionally, when the preset condition is not met, the application server continues to load the resources to be allocated from the database to the resource queue, including:
[0021] The application server acquires a reentrant lock after the preset condition is not met;
[0022] After acquiring the reentrant lock, the application server determines again whether the resource queue meets the preset condition. If it still does not meet the preset condition, the application server loads the resources to be allocated from the database to the resource queue.
[0023] In the above technical solution, the application server determines again whether the resource queue meets the preset condition after acquiring the reentrant lock, thereby avoiding the application server repeatedly requesting the database to load the resource to be allocated after loading the resource to be allocated.
[0024] Optionally, continuing to load the resources to be allocated from the database to the application server includes:
[0025] The application server acquires a distributed lock of a resource allocation table in the database based on the second thread; the resource allocation table is used to record a starting identification number of a resource to be allocated; the distributed lock is used to exclusively read and write the resource allocation table;
[0026] The application server queries a first set number of resources to be allocated from a resource table of the database based on the distributed lock and loads the first set number of resources to be allocated to the application server;
[0027] The application server releases the distributed lock.
[0028] In the above technical solution, the application server can load the resources to be allocated only after obtaining the distributed lock, ensuring that each application server loads the resources to be allocated in an orderly manner, and the resources to be allocated will not be repeatedly allocated to different application servers, ensuring that the updated resource status is not conflicting or abnormal, and improving the efficiency of querying the resources to be allocated through the starting identification number. Because the locked resources of the distributed lock are the resource allocation table, not the resources to be allocated in the database, the running memory of the distributed lock is reduced.
[0029] Optionally, if the application server determines that the number of resources to be allocated in the database is less than a second preset number, the first preset number is reduced; the second preset number is determined based on the first preset number and the number of application servers.
[0030] In the above technical solution, when the application server determines that the number of resources to be allocated in the database is less than the second preset number, it reduces the first preset number, that is, reduces the unallocated resources loaded into the resource queue, to prevent the situation where there are no resources to be allocated in the database when the resource queues of other application servers do not meet the preset conditions.
[0031] Optionally, when the application server receives a resource empty identifier sent by the database and the resource queue is empty, a resource acquisition stop request is sent to the user client.
[0032] In the above technical solution, by stopping resource acquisition requests, the resource acquisition requests initiated by the user client when there are no resources to be allocated are reduced.
[0033] Optionally, if the application server receives a restart instruction for the database, the resource queue is cleared; the restart instruction is sent after the database is restarted and the starting identification number in the resource allocation table is set to empty. After the database is restarted, the update status request of the first resource is not received and / or the update confirmation result of the first resource is not sent before sending the restart instruction.
[0034] In the above technical solution, the resource queue is cleared to prevent the update confirmation result of the first resource from not being sent when the database is restarted. As a result, before the database is restarted, the application server sends the update status request of the first resource to the database based on the first thread. When the database is restarted, the update confirmation result of the first resource is not sent, so the first resource cannot be sent to the user client and cannot be used, thereby avoiding waste of resources.
[0035] In a second aspect, an embodiment of the present invention provides a resource allocation device, including:
[0036] The acquisition module is used to obtain the resource acquisition request initiated by the user client;
[0037] A processing module, configured to acquire, based on a first thread, a first resource sent to the user client and change the state of the first resource in the database to be allocated; the first resource is a resource to be allocated in the database pre-loaded by the application server to the application server;
[0038] When it is determined based on the second thread that the number of resources in the resource queue of the application server does not meet the preset condition, continue to load the resources to be allocated from the database to the resource queue; the preset condition is determined according to the throughput of the application server.
[0039] Optionally, the processing module is specifically used for:
[0040] Based on the first thread, obtaining a first resource from a resource queue of the application server;
[0041] Sending an update status request for the first resource to the database; the update status request is used to update the allocation status of the first resource;
[0042] After receiving the update confirmation result, the first resource is sent to the user client.
[0043] Optionally, the throughput of the application server includes the quantity of resources to be allocated loaded by the application server, the loading time, and a preset rate of distributing the resources to be allocated;
[0044] The processing module is specifically used for:
[0045] The number of resources to be allocated loaded by the application server is determined according to the product of the loading time and a preset rate of distributing the resources to be allocated; the preset condition is that the number of resources in the resource queue of the application server is greater than the number of resources to be allocated loaded by the application server.
[0046] Optionally, the processing module is specifically used for:
[0047] After the preset condition is not met, acquiring a reentrant lock;
[0048] After acquiring the reentrant lock, it is determined again whether the resource queue meets the preset condition. If it still does not meet the preset condition, the resources to be allocated are loaded from the database to the resource queue.
[0049] Optionally, the processing module is specifically used for:
[0050] Based on the second thread, a distributed lock of a resource allocation table in the database is obtained; the resource allocation table is used to record the starting identification number of the resource to be allocated; the distributed lock is used to exclusively read and write the resource allocation table;
[0051] Based on the distributed lock, querying a first set number of resources to be allocated from a resource table of the database and loading the first set number of resources to be allocated to the application server;
[0052] Release the distributed lock.
[0053] Optionally, the processing module is further used for:
[0054] If it is determined that the number of resources to be allocated in the database is less than a second preset number, the first preset number is reduced; the second preset number is determined based on the first preset number and the number of application servers.
[0055] Optionally, the processing module is further used for:
[0056] When a resource empty identifier sent by the database is received and the resource queue is empty, a resource acquisition stop request is sent to the user client.
[0057] Optionally, the processing module is further used for:
[0058] If a restart instruction for the database is received, the resource queue is cleared; the restart instruction is sent after the database is restarted and the starting identification number in the resource allocation table is set to empty. After the database is restarted, the update status request of the first resource is not received and / or the update confirmation result of the first resource is not sent before sending the restart instruction.
[0059] In a third aspect, an embodiment of the present invention further provides a computing device, including:
[0060] A memory for storing program instructions;
[0061] The processor is used to call the program instructions stored in the memory and execute the above resource allocation method according to the obtained program.
[0062] In a fourth aspect, an embodiment of the present invention further provides a computer-readable storage medium, wherein the computer-readable storage medium stores computer-executable instructions, and the computer-executable instructions are used to enable a computer to execute the above-mentioned resource allocation method. BRIEF DESCRIPTION OF THE DRAWINGS
[0063] In order to more clearly illustrate the technical solutions in the embodiments of the present invention, the drawings required for use in the description of the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0064] Figure 1 A schematic diagram of a method for allocating coupons provided by an embodiment of the present invention;
[0065] Figure 2 A schematic diagram of a system architecture provided by an embodiment of the present invention;
[0066] Figure 3 A schematic diagram of a flow chart of a resource allocation method provided by an embodiment of the present invention;
[0067] Figure 4 A schematic diagram of a first resource provided by an embodiment of the present invention;
[0068] Figure 5 A schematic diagram of a resource allocation table provided in an embodiment of the present invention;
[0069] Figure 6 A schematic diagram of a resource allocation method provided by an embodiment of the present invention;
[0070] Figure 7 A schematic diagram of the structure of a resource allocation device provided by an embodiment of the present invention. DETAILED DESCRIPTION
[0071] In order to make the purpose, technical scheme and advantages of the present invention clearer, the present invention will be further described in detail below with reference to the accompanying drawings. Obviously, the described embodiments are only part of the embodiments of the present invention, rather than all the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0072] In the prior art, resources are generally allocated through the following two methods, taking allocation coupons as an example:
[0073] 1. After receiving the coupon request initiated by the user client, the application server obtains the exclusive lock of the database and locks all unallocated coupons in the database. Then, it selects the unallocated coupons in the database, updates the coupon recipient and the coupon collection time after obtaining the coupon, and then sends the coupon to the user client.
[0074] 2. Figure 1 A schematic diagram showing a method of allocating coupons is shown as an example. Figure 1As shown, the application server pre-queries the coupon in the database, and then stores the coupon in the remote dictionary service (such as Redis, Remote DictionaryServer). After the application server obtains the coupon request initiated by the user client, it obtains the coupon in the remote dictionary service, and then updates the status of the coupon in the database, and finally sends it to the user client.
[0075] However, the disadvantage of method 1 is that every time the application server goes to the database to obtain coupons, it must grab the exclusive lock of the database so that other threads cannot execute in parallel, that is, all coupon requests are processed serially and cannot be processed in parallel.
[0076] The disadvantage of method 2 is that when the application server synchronizes data between the database and the remote dictionary service, it increases the complexity and possibility of error. For example, if there are 1 million unallocated coupons in the database, the application server synchronizes the data to the remote dictionary service in 1,000 times. If the database is restarted during the synchronization process, data loss may occur. After the application server obtains the coupon from the remote dictionary service, the remote dictionary service will no longer store the coupon. If the database fails to update the status of the coupon, the coupon cannot be obtained again from the remote dictionary service, resulting in the loss of the coupon.
[0077] Therefore, a resource allocation method is needed to reduce resource waste and improve the efficiency of resource allocation.
[0078] Figure 2 A system architecture applicable to an embodiment of the present invention is exemplarily shown, and the system architecture includes a user client 210 , an application server 220 and a database 230 .
[0079] Among them, the user client 210 can be logged in on the user terminal, such as a certain treasure logged in by a user on a mobile phone, a certain cat logged in by a user on a tablet computer, and a certain JD logged in by a user on a personal computer. The user client 210 is used to initiate a resource acquisition request, such as a coupon acquisition request.
[0080] The application server 220 is used to obtain a resource acquisition request, and after obtaining the resource acquisition request, obtains the first resource sent to the user client 210 in the resource queue of the application server 220 based on the first thread, and sends an update status request of the first resource to the database 230. After obtaining the update confirmation result of the first resource sent by the database 230, the first resource is sent to the user client 210. It should be noted that the application server 220 can establish data connection with multiple user clients 210, and there is no limit on the number here.
[0081] The database 230 is used to provide the application server 220 with resources to be allocated, and after receiving the update status request of the first resource sent by the application server 220, change the status of the first resource in the database 230 to allocated, and after the change, send the update confirmation result of the first resource to the application server 220.
[0082] It should be noted that the above Figure 2 The structure shown is only an example and is not limited to this embodiment of the present invention.
[0083] Based on the above description, Figure 3 The process of a resource allocation method provided by an embodiment of the present invention is exemplarily shown, and the process can be executed by a resource allocation device.
[0084] like Figure 3 As shown in the figure, the process specifically includes:
[0085] Step 310: The application server obtains a resource acquisition request initiated by the user client.
[0086] In the embodiment of the present invention, the user clicks on the request transaction in the user client, and then the user client initiates the corresponding resource acquisition request, and then the application server obtains the resource acquisition request initiated by the user client, wherein the resource acquisition request may be a request for receiving a coupon, etc.
[0087] Step 320: The application server obtains a first resource sent to the user client based on the first thread and changes the state of the first resource in the database to allocated.
[0088] In the embodiment of the present invention, the first resource is the resource to be allocated in the database that is pre-loaded to the application server by the application server. The first resource is obtained from the application server. After obtaining the first resource, the application server sends an update status request of the first resource to the database and receives the update confirmation result sent by the database. It should be noted that the status of the first resource also includes the allocation person and the allocation time. Figure 4 A schematic diagram of a first resource is shown as an example. Figure 4 As shown, after receiving the update status request of the first resource sent by the application server, the database will update the assignor, assignment time and assignment status.
[0089] Furthermore, the application server obtains the first resource from the resource queue of the application server based on the first thread, and then sends an update status request for the first resource to the database, wherein the update status request is used to update the allocation status of the first resource, and then the application server sends the first resource to the user client after receiving the update confirmation result.
[0090] In an embodiment of the present invention, the resource queue of the application server can be obtained by sorting the resources to be allocated according to the identification numbers of the resources to be allocated. For example, the resources to be allocated are sorted according to the identification numbers from small to large. When the application server obtains the first resource, it can determine the first (i.e., the resource with the smallest identification number) to be allocated in the sorted resource queue as the first resource. After determining the first resource, the first resource is popped out from the resource queue. Exemplarily, the resource queue can be obtained by sorting the resources to be allocated according to other rules, or it can be irregular.
[0091] Step 330: When the application server determines based on the second thread that the number of resources in the resource queue of the application server does not meet the preset condition, the application server continues to load the resources to be allocated from the database to the resource queue.
[0092] In an embodiment of the present invention, the preset condition is determined according to the throughput of the application server. After the application server obtains the resource acquisition request initiated by the user client, if it is determined based on the second thread that the number of resources in the resource queue of the application server is greater than the number of resources to be allocated loaded by the application server, the resources to be allocated are loaded from the database to the application server.
[0093] Further, the throughput of the application server includes the number of resources to be allocated loaded by the application server, the loading time, and the preset rate of distributing the resources to be allocated. The application server determines the number of resources to be allocated loaded by the application server according to the product of the loading time and the preset rate of distributing the resources to be allocated. The preset condition is that the number of resources in the resource queue of the application server is greater than the number of resources to be allocated loaded by the application server. For example, the maximum number of resources to be allocated that can exist in the application server is 1000, and the number of resources to be allocated loaded by the application server each time is 500. If the number of resources in the resource queue of the application server is greater than 500, the application server meets the preset condition. If the number of resources in the resource queue of the application server is not greater than 500, the application server does not meet the preset condition. Among them, the number of resources to be allocated loaded by the application server is determined according to the product of the loading time and the preset rate of distributing the resources to be allocated. For example, the loading time is 0.1 second, and the preset rate of distributing the resources to be allocated is 1000 / second (the unit can be determined according to the specific resource, for example, if the resource to be allocated is a coupon, then the rate of distributing the resources to be allocated can be 1000 coupons / second), then the number of resources to be allocated distributed in the resource queue every 0.1 second is 100, so the number of resources to be allocated loaded by the application server should be greater than 100 (such as 200) to ensure that the number of resources to be allocated loaded each time is greater than the number distributed within the loading time, otherwise the application server will not be loaded during the process of distributing the resources to be allocated, and there will be no resources to be allocated in the resource queue. In order to prevent the situation where there is no resource to be allocated in the resource queue, which affects the resource allocation efficiency, a buffer is generally set to allow the application server to have enough time to load the resources to be allocated. The maximum number of resources to be allocated that can be loaded in the application server can be increased, and the multiples of the preset conditions can be increased. For example, the number of resources to be allocated loaded by the application server is n times the product of the loading time and the preset rate of distributing the resources to be allocated, where n is greater than 1.
[0094] When the application server determines that the preset conditions are not met, it continues to load the resources to be allocated from the database to the resource queue. Specifically, after the preset conditions are not met, the application server obtains a reentrant lock, and after obtaining the reentrant lock, it determines again whether the resource queue meets the preset conditions. If it is still not met, it loads the resources to be allocated from the database to the resource queue.
[0095] In the embodiment of the present invention, the reentrant lock includes a counting function. If the application server does not meet the preset conditions for the current resource acquisition request, the second thread of the application server acquires the reentrant lock and then loads the resources to be allocated from the database. During the process of loading the resources to be allocated, the application server acquires the next resource acquisition request for the current resource, which triggers the second thread to acquire the reentrant lock again. At this time, the counting function in the reentrant lock is increased by 1, that is, the second thread is run twice (the first thread that runs for the first time locks the second thread again after the reentrant lock is released, and runs the second thread for the second time) to prevent the second thread that runs for the first time from failing to load the resources to be allocated and no longer loading the resources to be allocated. At the same time, it is determined again whether the resource queue meets the preset conditions to prevent the second thread that runs for the second time from repeatedly loading the resources to be allocated. For example, if the second thread that runs for the first time successfully loads the resources to be allocated, when the second thread that runs for the second time determines the resource queue again, it meets the preset conditions (because the resources to be allocated are loaded successfully), and then the reentrant lock is released.
[0096] Furthermore, the application server obtains a distributed lock of a resource allocation table in the database based on the second thread, wherein the resource allocation table is used to record the starting identification number of the resources to be allocated, and the distributed lock is used to exclusively read and write the resource allocation table. Then, based on the distributed lock, the application server queries the first set number of resources to be allocated from the resource table in the database and loads the first set number of resources to be allocated to the application server. After loading is complete, the application server releases the distributed lock.
[0097] In an embodiment of the present invention, Figure 5 A schematic diagram of a resource allocation table is shown as an example. Figure 5 As shown, the resource allocation table is used to record the application servers that access the resource allocation table, and the row lock in the resource allocation table is used to lock the second thread of the application server so that the application server can query the resources to be allocated in the database, wherein the row lock is a specific implementation method of the distributed lock, and the distributed lock of the resource allocation table in the database is used to implement multiple application servers to access the resource allocation table. For example, when application server a accesses the resource allocation table, the resource allocation table records application service a, and when no other application server accesses, the row lock of the resource allocation table locks the second thread of application server a so that application server a can query the resources to be allocated in the database and load them. In the process of application server a loading the resources to be allocated, according to the distributed lock, application server b can also access the resource allocation table, and the resource allocation table will record application server b, and after application service a releases the row lock, the second thread of application server b is locked through the row lock in the resource allocation table so that application server b can query the resources to be allocated in the database.
[0098] Alternatively, the application coordination service (such as ZooKeeper) of the distributed lock records multiple accessed application services, and sequentially locks the application servers that access the resource allocation table, so that the locked application servers can read and write the resource allocation table and the resources to be allocated in the database, and other threads are placed in a waiting queue. After the previous thread releases the distributed lock, the threads are locked in the order of access by other threads in the waiting queue. For example, during the distributed locking process (i.e., the process of loading resources to be allocated), the second thread of application service 1 accesses the resource allocation table, and the distributed application coordination service will record the second thread of application service 2 as the next locked thread, and so on, so that other threads can queue up for access in an orderly manner according to the access time, thereby reducing thread collisions.
[0099] It should be noted that the resource allocation table includes a starting identification number and a batch number. The batch number includes the type of resources to be allocated, for example, the batch number of a 10-yuan daily necessities coupon, the batch number of a 100-yuan electronic product coupon, etc.
[0100] Specifically, after the application server obtains the distributed lock of the resource allocation table based on the second thread, it is also necessary to determine whether the starting identification number of the resource allocation table is empty. If the starting identification number is empty, the first preset number of resources to be allocated is read from the first resource to be allocated in the order of the resources to be allocated in the database, wherein the resources to be allocated in the database are obtained after being sorted according to the identification numbers of the resources to be allocated. For example, the identification numbers of the resources to be allocated are 1-1000, and the first preset number is 200, then the application server loads the resources to be allocated with identification numbers 1-200. If it is determined that the starting identification number is not empty, the first preset number of resources to be allocated is read according to the next resource to be allocated with the starting identification number. For example, if the starting identification number is 200, the application server loads the resources to be allocated with identification numbers 201-400. It should be noted that after loading the resources to be allocated, the application server modifies the starting identification number to the last identification number of the loaded resources to be allocated, such as modifying the starting identification number to 400.
[0101] Exemplarily, the application server determines that the number of resources to be allocated in the database is less than a second preset number, and then reduces the first preset number, wherein the second preset number is determined according to the first preset number and the number of application servers.
[0102] In an embodiment of the present invention, in order to prevent the application server from being unable to load the resources to be allocated when the number of resources to be allocated in the database is less than the first preset number and greater than 0, the first preset number is reduced so that all the resources to be allocated in the database are sent to the application server. For example, if the first preset number of application servers is 200, and the database provides resources to be allocated to 5 application servers, the second preset number may be 1000. When the application server determines that the number of resources to be allocated in the database is less than 1000, the first preset number is set to half of the previous first preset number, which is 100. Similarly, when the application server determines that the number of resources to be allocated in the database is less than 500, the first preset number is set to 50. When the application server determines that the number of resources to be allocated in the database is less than 250, the first preset number is set to 25, until there are no resources to be allocated in the database.
[0103] Exemplarily, if the application server receives a resource empty identifier sent by the database and the resource queue is empty, it sends a stop resource acquisition request to the user client.
[0104] In the embodiment of the present invention, the stop resource acquisition request can also set a cache time, and within the cache time, the user client is controlled not to initiate a resource acquisition request. For example, the cache time is 5 minutes. After the application server receives the resource empty identifier sent by the database and the resource queue is empty, it sends a stop resource acquisition request to the user client, and the user client cannot initiate a resource acquisition request within 5 minutes.
[0105] Exemplarily, if the application server receives a restart instruction for the database, it clears the resource queue, wherein the restart instruction is sent after the database is restarted and the starting identification number in the resource allocation table is set to empty. After the database is restarted, the update status request of the first resource is not received and / or the update confirmation result of the first resource is not sent before the restart instruction is sent.
[0106] Specifically, after receiving the resource acquisition request, the application server obtains the first resource from the resource queue of the application server based on the first thread, and then sends an update status request for the first resource to the database. When the database receives the update status request for the first resource and restarts, it does not send an update confirmation result of the first resource, and / or cannot receive subsequent update status requests for the first resource. Therefore, the resources to be allocated in the application server cannot be sent to the user client. After receiving the restart instruction, the application server clears the resource queue to avoid resource waste.
[0107] In the embodiment of the present invention, since the application server has pre-loaded the resources to be allocated from the database, the resource acquisition requests of each user client can be executed in parallel in the application server through the first thread, and after the application server determines the resources to be provided to the user client, it changes the state of the resource in the database, thereby avoiding repeated allocation of the same resource. At the same time, the application server determines whether it is necessary to continue to load the resources to be allocated to the application server through the second thread, thereby avoiding the situation where the application server lacks resources when providing resources to the user client, and also locks the second thread through the reentrant lock mechanism of the application server, thereby avoiding the application server from repeatedly requesting to load the resources to be allocated from the database after loading the resources to be allocated, and then loading the resources to be allocated only after obtaining the distributed lock, thereby ensuring that each application server loads the resources to be allocated in an orderly manner, and the resources to be allocated will not be repeatedly allocated to different application servers, thereby ensuring that the updated resource state does not conflict or is abnormal, thereby further improving the efficiency of distributing resources. It should be noted that the first thread and the second thread are asynchronously performed simultaneously without affecting each other, thereby ensuring the timeliness and efficiency of distributing resources.
[0108] In order to better explain the above technical solution, Figure 6 A schematic diagram exemplarily shows a method for allocating resources applicable to an embodiment of the present invention, such as Figure 6 As shown, the process includes:
[0109] Step 610: Obtain a resource acquisition request.
[0110] The resource acquisition request may be to obtain a coupon, and the coupon includes an identification number. After obtaining the resource allocation request initiated by the user, the application server simultaneously executes step 620 and step 630.
[0111] Step 620, obtaining a first resource.
[0112] The application server obtains the first coupon in the resource queue as the first resource, deletes the coupon, and then executes step 621. The resource queue is obtained by sorting the coupons in ascending order according to the identification number.
[0113] Step 621, send an update status request.
[0114] The application server sends the update status request of the first resource to the database, so that the database updates the status of the first resource in the database according to the update status request of the first resource, such as the assignee, assignment time and assignment status, and then executes step 622.
[0115] Step 622, receiving the update confirmation result.
[0116] The application server receives the update confirmation result of the first resource returned by the database, and then executes step 623 .
[0117] Step 623: Send the first resource.
[0118] The application server sends the first resource to the user client.
[0119] Step 630: If the preset condition is met, a reentrant lock is acquired.
[0120] The maximum number of resources to be allocated that can be loaded in the application server is 500. After obtaining the resource acquisition request, it is determined whether the number of resources to be allocated in the resource queue is greater than 250 (preset condition). If so, the application server obtains the reentrant lock based on the second thread, and after obtaining the reentrant lock, execute step 631.
[0121] Step 631: If it is determined again that the preset condition is met, the distributed lock is acquired.
[0122] The application server again determines whether the number of resources to be allocated in the resource queue is greater than 250 (preset condition). If so, the application server acquires the distributed lock of the resource allocation table based on the second thread.
[0123] Step 632, receiving a starting identification number.
[0124] After the application server acquires the distributed lock of the resource allocation table based on the second thread, it receives the starting identification number 400 of the resource allocation table returned by the database.
[0125] Step 633, query the resources to be allocated.
[0126] The application server queries the identification numbers of the resources to be allocated, which are 401-600, according to the starting identification number 400 and the first set number (200).
[0127] Step 634, receiving the query result.
[0128] Step 635: Load the resources to be allocated.
[0129] In the embodiment of the present invention, since the application server pre-loads the resources to be allocated from the database, it is possible to execute the resource acquisition requests of each user client in parallel. At the same time, the second thread is locked by the reentrant lock mechanism of the application server to prevent the application server from repeatedly requesting to load the resources to be allocated from the database after loading the resources to be allocated, and then loading the resources to be allocated only after obtaining the distributed lock, ensuring that each application server loads the resources to be allocated in an orderly manner, and the resources to be allocated will not be repeatedly allocated to different application servers, ensuring that the updated resource status does not conflict or is abnormal, further improving the efficiency of distributing resources, and importantly, the first thread and the second thread are asynchronously performed at the same time without affecting each other, ensuring the timeliness and efficiency of distributing resources.
[0130] Based on the same technical concept, Figure 7 The structure of a resource allocation device provided in an embodiment of the present invention is exemplarily shown, and the device can execute the process of the resource allocation method.
[0131] like Figure 7 As shown, the device specifically includes:
[0132] The acquisition module 710 is used to acquire the resource acquisition request initiated by the user client;
[0133] The processing module 720 is configured to obtain, based on the first thread, a first resource sent to the user client and change the state of the first resource in the database to be allocated; the first resource is a resource to be allocated in the database pre-loaded by the application server to the application server;
[0134] When it is determined based on the second thread that the number of resources in the resource queue of the application server does not meet the preset condition, continue to load the resources to be allocated from the database to the resource queue; the preset condition is determined according to the throughput of the application server.
[0135] Optionally, the processing module 720 is specifically used for:
[0136] Based on the first thread, obtaining a first resource from a resource queue of the application server;
[0137] Sending an update status request for the first resource to the database; the update status request is used to update the allocation status of the first resource;
[0138] After receiving the update confirmation result, the first resource is sent to the user client.
[0139] Optionally, the throughput of the application server includes the quantity of resources to be allocated loaded by the application server, the loading time, and a preset rate of distributing the resources to be allocated;
[0140] The processing module 720 is specifically used for:
[0141] The number of resources to be allocated loaded by the application server is determined according to the product of the loading time and a preset rate of distributing the resources to be allocated; the preset condition is that the number of resources in the resource queue of the application server is greater than the number of resources to be allocated loaded by the application server.
[0142] Optionally, the processing module 720 is specifically used for:
[0143] After the preset condition is not met, acquiring a reentrant lock;
[0144] After acquiring the reentrant lock, it is determined again whether the resource queue meets the preset condition. If it still does not meet the preset condition, the resources to be allocated are loaded from the database to the resource queue.
[0145] Optionally, the processing module 720 is specifically used for:
[0146] Based on the second thread, a distributed lock of a resource allocation table in the database is obtained; the resource allocation table is used to record the starting identification number of the resource to be allocated; the distributed lock is used to exclusively read and write the resource allocation table;
[0147] Based on the distributed lock, querying a first set number of resources to be allocated from a resource table of the database and loading the first set number of resources to be allocated to the application server;
[0148] Release the distributed lock.
[0149] Optionally, the processing module 720 is further used for:
[0150] If it is determined that the number of resources to be allocated in the database is less than a second preset number, the first preset number is reduced; the second preset number is determined based on the first preset number and the number of application servers.
[0151] Optionally, the processing module 720 is further used for:
[0152] When a resource empty identifier sent by the database is received and the resource queue is empty, a resource acquisition stop request is sent to the user client.
[0153] Optionally, the processing module 720 is further used for:
[0154] If a restart instruction for the database is received, the resource queue is cleared; the restart instruction is sent after the database is restarted and the starting identification number in the resource allocation table is set to empty. After the database is restarted, the update status request of the first resource is not received and / or the update confirmation result of the first resource is not sent before sending the restart instruction.
[0155] Based on the same technical concept, an embodiment of the present invention further provides a computing device, including:
[0156] A memory for storing program instructions;
[0157] The processor is used to call the program instructions stored in the memory and execute the above resource allocation method according to the obtained program.
[0158] Based on the same technical concept, an embodiment of the present invention further provides a computer-readable storage medium, wherein the computer-readable storage medium stores computer-executable instructions, and the computer-executable instructions are used to enable a computer to execute the above-mentioned resource allocation method.
[0159] Those skilled in the art will appreciate that the embodiments of the present application may be provided as methods, systems, or computer program products. Therefore, the present application may adopt the form of a complete hardware embodiment, a complete software embodiment, or an embodiment in combination with software and hardware. Moreover, the present application may adopt the form of a computer program product implemented in one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) that include computer-usable program code.
[0160] The present application is described with reference to the flowcharts and / or block diagrams of the methods, devices (systems), and computer program products according to the present application. It should be understood that each process and / or box in the flowchart and / or block diagram, as well as the combination of the processes and / or boxes in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing device to produce a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.
[0161] These computer program instructions may also be stored in a computer-readable memory capable of directing a computer or other programmable data processing device to operate in a specific manner, so that the instructions stored in the computer-readable memory produce an article of manufacture comprising an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1 A function specified in one or more boxes.
[0162] These computer program instructions can also be loaded onto a computer or other programmable data processing device so that a series of operating steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing instructions for implementing the process. Figure 1 A process or multiple processes and / or boxes Figure 1 The steps of the functions specified in one or more boxes. Obviously, those skilled in the art can make various changes and modifications to the present application without departing from the spirit and scope of the present application. Thus, if these modifications and variations of the present application fall within the scope of the claims of the present application and their equivalents, the present application is also intended to include these modifications and variations.
Claims
1. A method for resource allocation, characterized in that: include: The application server obtains the resource acquisition request initiated by the user client; The application server acquires the first resource sent to the user client based on the first thread and changes the state of the first resource in the database to allocated; the first resource is a resource to be allocated in the database pre-loaded by the application server to the application server; When the application server determines based on the second thread that the number of resources in the resource queue of the application server does not meet a preset condition, the application server continues to load the resources to be allocated from the database to the resource queue; the preset condition is determined according to the throughput of the application server; When the preset condition is not met, the application server continues to load the resources to be allocated from the database to the resource queue, including: The application server acquires a reentrant lock after the preset condition is not met; After acquiring the reentrant lock, the application server determines again whether the resource queue meets the preset condition. If it still does not meet the preset condition, the resource to be allocated is loaded from the database to the resource queue; the reentrant lock includes a counting function. When the application server obtains the next resource acquisition request for the current one, the second thread is triggered again to acquire the reentrant lock. The counting function in the reentrant lock is increased by 1, and the second thread is run twice.
2. The method according to claim 1, characterized in that The application server acquires the first resource sent to the user client based on the first thread and changes the state of the first resource in the database to allocated, including: The application server acquires a first resource from a resource queue of the application server based on the first thread; The application server sends an update status request for the first resource to the database; the update status request is used to update the allocation status of the first resource; After receiving the update confirmation result, the application server sends the first resource to the user client.
3. The method according to claim 1, characterized in that The throughput of the application server includes the amount of resources to be allocated loaded by the application server, the loading time and the preset rate of distributing the resources to be allocated; The preset condition is determined according to the throughput of the application server, including: The application server determines the amount of resources to be allocated loaded by the application server according to the product of the loading time and a preset rate of distributing the resources to be allocated; The preset condition is that the number of resources in the resource queue of the application server is greater than the number of resources to be allocated loaded by the application server.
4. The method according to any one of claims 1 to 3, characterized in that Continuing to load the resources to be allocated from the database to the application server includes: The application server acquires a distributed lock of a resource allocation table in the database based on the second thread; the resource allocation table is used to record a starting identification number of a resource to be allocated; the distributed lock is used to exclusively read and write the resource allocation table; The application server queries a first preset number of resources to be allocated from a resource table of the database based on the distributed lock and loads the first preset number of resources to be allocated to the application server; The application server releases the distributed lock.
5. The method according to claim 4, characterized in that The method further comprises: If the application server determines that the number of resources to be allocated in the database is less than a second preset number, the first preset number is reduced; the second preset number is determined according to the first preset number and the number of application servers.
6. The method according to claim 4, characterized in that The method further comprises: When the application server receives the resource empty identifier sent by the database and the resource queue is empty, it sends a stop resource acquisition request to the user client.
7. The method according to claim 4, characterized in that The method further comprises: If the application server receives a restart instruction for the database, the resource queue is cleared; the restart instruction is sent after the database is restarted and the starting identification number in the resource allocation table is set to empty. After the database is restarted, the update status request of the first resource is not received and / or the update confirmation result of the first resource is not sent before sending the restart instruction.
8. A computing device, characterized in that include: A memory for storing program instructions; A processor, configured to call the program instructions stored in the memory, and execute the method according to any one of claims 1 to 7 according to the obtained program.
9. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores computer-executable instructions, and the computer-executable instructions are used to enable a computer to execute the method according to any one of claims 1 to 7.
Citation Information
Patent Citations
Method for resource acquisition and server
CN106293946A
Resource allocation method and device, electronic equipment and storage medium
CN111782391A