Method, device and readable storage medium for adjusting the number of threads in a microservice thread pool

By setting a time range in the microservice thread pool, the first thread processing capability of the thread pool is determined at every time in the microservice thread pool, and adjusting the number of threads in the thread pool according to this capability, the problem that the microservice thread pool cannot respond in time when the number of requests increases, and the performance stability of the microservice is improved.

CN114443274BActive Publication Date: 2025-05-13新奥新智科技有限公司
View PDF 1 Cites 0 Cited by

Patent Information

Application Number
CN202111577233.4
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-12-22
Publication Date
2025-05-13
Estimated Expiration
2041-12-22

AI Technical Summary

Technical Problem

In the prior art, the preset configuration of the microservice thread pool cannot respond in time when the number of requests increases, resulting in a degradation of microservice performance.

Method used

The first thread processing capability of the thread pool is determined by setting the time range at every set time, and the preset maximum number of threads is adjusted to determine the first maximum number of threads according to the ratio of the number of pending requests and the first thread processing capability and the number of idle threads.

Benefits of technology

Ensure that the thread pool can handle all requests when facing a sudden increase in the number of pending requests, reducing the risk that microservice performance is affected by the number of requests.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN114443274B_ABST
    Figure CN114443274B_ABST
Patent Text Reader

Abstract

The present application discloses a method, device and readable storage medium for adjusting the number of threads in a microservice thread pool, which is used to solve the problem that when the number of requests increases suddenly, the preset configuration of the thread pool cannot respond, resulting in a decrease in microservice performance. The method includes: determining the first thread processing capacity at every set time range; obtaining the number of pending requests, the number of idle threads, and the preset maximum number of threads of the thread pool; wherein the preset maximum number of threads indicates the maximum number of threads provided by the thread pool; when the ratio of the number of pending requests to the first thread processing capacity is greater than the idle thread, determining to adjust the preset maximum number of threads to obtain the first maximum number of threads.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of information processing technology, and in particular to a method, device and readable storage medium for adjusting the number of threads in a microservice thread pool. Background Art

[0002] Microservices divide applications into independent programs or services according to business. They communicate with each other through HTTP protocol (message queues can also be used, such as RocketMQ, Kafaka, etc.), and can use different programming languages, different storage technologies, and automated deployment (such as Jenkins) to reduce human control and reduce the probability of errors. Microservices will allocate 1 thread for each request, that is, each request needs to complete the microservice through a thread, and all threads set for all requests form a thread pool.

[0003] Currently, the configuration of the thread pool by the microservice provider is preset by simulating specific business scenarios. However, in actual applications, the number of requested accesses will change according to the actual situation. Especially when the number of requests increases suddenly, the preset configuration of the thread pool cannot meet the demand, resulting in a serious decline in the performance of the microservice.

[0004] Therefore, there is a problem in the prior art that when the number of requests increases suddenly, the preset configuration of the thread pool cannot respond, resulting in a degradation of microservice performance. Summary of the invention

[0005] The present application provides a method, device and readable storage medium for adjusting the number of threads in a microservice thread pool, so as to solve the problem that when the number of requests increases suddenly, the preset configuration of the thread pool cannot respond, resulting in a decrease in microservice performance.

[0006] In a first aspect, the present application provides a method for adjusting the number of threads in a microservice thread pool, the method comprising:

[0007] Determine a first thread processing capability at every set time range; wherein the first thread processing capability indicates the capability of the thread pool to process multiple requests simultaneously;

[0008] Obtaining the number of pending requests, the number of idle threads, and the preset maximum number of threads of the thread pool; wherein the preset maximum number of threads indicates the maximum number of threads provided by the thread pool;

[0009] When the ratio of the number of pending requests to the processing capacity of the first thread is greater than the idle thread, it is determined to adjust the preset maximum number of threads to obtain the first maximum number of threads; wherein the first maximum number of threads is the sum of the difference between the ratio of the number of pending requests to the processing capacity of the first thread and the number of idle threads and the preset maximum number of threads; the first maximum number of threads is greater than the preset maximum number of threads.

[0010] The above step determines the first maximum number of threads based on the processing capacity of the first thread, which can ensure that when the thread pool faces a sudden increase in the number of pending requests, the thread pool can still process all pending requests, thereby reducing the impact of the sudden increase in the number of requests on the performance of the microservice.

[0011] In a possible implementation manner, the first thread processing capacity is the ratio of the total number of requests completed by the thread pool within a set time range to the maximum number of concurrent threads within the set time range.

[0012] This method enables the thread pool to provide a matching number of concurrent threads to process requests when the number of requests suddenly increases, thereby ensuring the performance of the microservice.

[0013] In a possible implementation manner, the first thread processing capacity is an arithmetic average of the first thread processing capacities corresponding to two adjacent set time ranges.

[0014] In a possible implementation manner, after obtaining the first maximum number of threads, the method further comprises:

[0015] Obtain the maximum number of concurrent threads in the thread pool; wherein the maximum number of concurrent threads indicates the number of threads in the thread pool that simultaneously process requests per unit time;

[0016] Determine a minimum value of the first maximum number of threads as the maximum number of concurrent threads.

[0017] The above method can ensure the stable performance of the thread pool.

[0018] In a possible implementation manner, the method further includes:

[0019] When the system load is lower than a first threshold, or the system load change rate is lower than a second threshold, or every set time range, the first maximum number of threads is reduced by a set value to obtain a second maximum number of threads; wherein the second maximum number of threads is not less than the minimum value.

[0020] The above method can avoid the first maximum number of threads being too large, which leads to waste of resources.

[0021] In a second aspect, the present application further provides a device for adjusting the number of threads in a microservice thread pool, the device comprising:

[0022] A determining unit: used to determine the first thread processing capacity at every set time range; wherein the first thread processing capacity indicates the ability of the thread pool to process multiple requests simultaneously;

[0023] An acquisition unit: used to acquire the number of pending requests, the number of idle threads, and the preset maximum number of threads of the thread pool; wherein the preset maximum number of threads indicates the maximum number of threads provided by the thread pool;

[0024] An adjustment unit: used for determining to adjust the preset maximum number of threads to obtain a first maximum number of threads when the ratio of the number of pending requests to the processing capacity of the first thread is greater than the idle thread; wherein the first maximum number of threads is the sum of the difference between the ratio of the number of pending requests to the processing capacity of the first thread and the number of idle threads and the preset maximum number of threads; the first maximum number of threads is greater than the preset maximum number of threads.

[0025] In a possible implementation manner, the determining unit is specifically used to determine the first thread processing capacity as a ratio of the total number of requests completed by the thread pool within a set time range to the maximum number of concurrent threads within the set time range.

[0026] In a possible implementation manner, the device also includes a minimum unit, which is specifically used to obtain the maximum number of concurrent threads of the thread pool; wherein the maximum number of concurrent threads indicates the number of threads in the thread pool that simultaneously process requests per unit time; and the minimum value of the first maximum number of threads is determined to be the maximum number of concurrent threads.

[0027] In a possible implementation manner, the device also includes a reduction unit, which is specifically used to reduce the first maximum number of threads by a set value to obtain a second maximum number of threads when the system load is lower than a first threshold, or the system load change rate is lower than a second threshold, or every set time range; wherein the second maximum number of threads is not less than the minimum value.

[0028] In a third aspect, the present application further provides a readable storage medium, which includes a memory,

[0029] The memory is used to store instructions. When the instructions are executed by the processor, the device including the readable storage medium performs the method described in the first aspect and any one of the embodiments. BRIEF DESCRIPTION OF THE DRAWINGS

[0030] Figure 1 A flowchart of a method for adjusting the number of threads in a microservice thread pool provided in this application;

[0031] Figure 2A schematic diagram of the structure of a device for adjusting the number of threads in a microservice thread pool provided in the present application. DETAILED DESCRIPTION

[0032] In view of the problem in the prior art that when the number of requests suddenly increases, the preset configuration of the microservice thread pool cannot respond in time, resulting in a decrease in microservice performance. The present application provides a method for adjusting the number of threads in a microservice thread pool: according to the ability of the thread pool to process multiple requests simultaneously within a set time range, it is pre-determined whether it is necessary to adjust the preset maximum number of threads (preset configuration) of the thread pool according to the number of pending requests to access the microservice, and then determine the first maximum number of threads of the thread pool.

[0033] In order to better understand the above technical scheme, the technical scheme of the present application is described in detail below through the accompanying drawings and specific embodiments. It should be understood that the embodiments of the present application and the specific features in the embodiments are detailed descriptions of the technical scheme of the present application, rather than limitations on the technical scheme of the present application. In the absence of conflict, the embodiments of the present application and the technical features in the embodiments can be combined with each other.

[0034] Please refer to Figure 1 The embodiment of the present application provides a method for adjusting the number of threads in a microservice thread pool, so as to solve the problem that the number of requests suddenly increases and the preset maximum number of threads in the thread pool cannot meet the demand, resulting in a decrease in microservice performance.

[0035] Step 101: Determine a first thread processing capability at every set time range; wherein the first thread processing capability indicates the capability of a thread pool to process multiple requests simultaneously.

[0036] Specifically, the first thread processing capacity is the ratio of the total number of requests completed by the thread pool within a set time range to the maximum number of concurrent threads within the set time range.

[0037] For example, if the time range is set to 3 seconds, then in the first second of (0-3) seconds, the number of processed requests is 15, and the number of concurrent threads is 10; the number of processed requests in the first to second seconds is 25, and the number of concurrent threads is 14; the number of processed requests in the second to third seconds is 16, and the number of concurrent threads is 11. Then, in the set time range, the total number of processed requests is 56, the maximum number of concurrent threads is 14, and the first thread processing capacity is 4. The first thread processing capacity can represent the instantaneous capacity of the thread pool to process a sudden increase in the number of requests.

[0038] The first thread processing capability may also be an arithmetic average of first thread capabilities corresponding to at least two consecutive set time ranges.

[0039] In the embodiment of the present application, the arithmetic average of the first thread processing capabilities corresponding to two adjacent set time ranges is preferably used as the first thread processing capability.

[0040] Step 102: Obtain the number of pending requests, the number of idle threads, and the preset maximum number of threads of the thread pool.

[0041] The preset maximum number of threads indicates the maximum number of threads provided by the thread pool.

[0042] The above-mentioned number of idle threads and the preset maximum number of threads belong to the thread pool configuration information. The thread pool configuration information can be saved as cache data. The cache data can be divided into remote cache data and local cache data. Compared with local cache data, remote cache data can respond efficiently and is convenient for real-time acquisition. However, it cannot be guaranteed that the remote cache data can be successfully connected every time. Therefore, it is necessary to synchronize the thread pool configuration information in the remote cache data to the local cache data every set time range. This ensures that when the remote cache data fails to be obtained, it can be obtained through the local cache data. In addition, the local cache number needs to be saved in a shared persistent storage device, so as to ensure that when the system is restarted or reloaded, that is, when the remote cache data and the local cache data are cleared, the thread pool configuration information can be obtained from the shared persistent storage device.

[0043] Step 103: When the ratio of the number of pending requests to the processing capability of the first thread is greater than the idle thread, it is determined to adjust the preset maximum number of threads to obtain a first maximum number of threads.

[0044] Among them, the first maximum number of threads is the sum of the ratio of the number of pending requests to the processing capacity of the first thread and the difference between the number of idle threads and the preset maximum number of threads; the first maximum number of threads is greater than the preset maximum number of threads.

[0045] It should be noted that in the embodiment of the present application, determining the first maximum number of threads based on the maximum number of concurrent threads can ensure that the thread pool maintains an excellent performance state each time it receives a request.

[0046] For example, if the time range is set to 2 seconds, then in the past 2 seconds, the number of requests processed in the first second is 44, and the number of concurrent threads is 8; the number of requests processed in the first 2 seconds (from the last second to the last 2 seconds) is 36, and the number of concurrent threads is 5; then the maximum number of concurrent threads in the set time range is 8, and the first thread processing capacity is (36+44) / 8=10. At this time, the number of requests increases suddenly, the number of pending requests is 100, the preset maximum number of threads is 12, and the number of idle threads is 4, then the first maximum number of threads = 100 / 10-4+12=18. That is, when the first maximum number of threads is set to 18, it can at least meet the number of burst requests in the next 2 seconds.

[0047] According to the above steps, when the thread pool can still process requests normally, it can be determined in advance that the preset maximum number of threads needs to be adjusted, thereby ensuring that the thread pool can still process all pending requests when facing a sudden increase in the number of requests.

[0048] After determining the first maximum number of threads, the maximum number of concurrent threads of the thread pool can also be obtained. The number of concurrent threads indicates the number of threads in the thread pool that simultaneously process requests per unit time. At this time, it can be determined that the minimum value of the first maximum number of threads is the maximum number of concurrent threads. It is worth noting that when determining that the minimum value of the first maximum number of threads is the maximum number of concurrent threads, the maximum number of concurrent threads is at least 1.

[0049] Furthermore, when the number of requests decreases after a sudden increase, in order to avoid wasting resources, it is necessary to scale down the thread pool, that is, gradually reduce the first maximum number of threads.

[0050] When the system load is lower than a first threshold, or the system load change rate is lower than a second threshold, or every set time range, the first maximum number of threads is reduced by a set value to obtain a second maximum number of threads; wherein the second maximum number of threads is not less than the minimum value.

[0051] Because for the thread pool, the efficiency of scaling down (reducing the first maximum number of threads) is much higher than the efficiency of scaling up (increasing the first maximum number of threads), in order to avoid an excessive increase in the number of the first maximum number of threads in the later stage, in the embodiment of the present application, the set value is set to 1.

[0052] It should be noted that the above method is also applicable to adjusting the number of coroutines in the microservice coroutine pool and the number of fibers in the fiber pool, which will not be repeated here.

[0053] Based on the same inventive concept, an embodiment of the present application provides a device for adjusting the number of threads in a microservice thread pool, which is similar to the aforementioned Figure 1 The method for adjusting the number of threads in the microservice thread pool shown in the figure corresponds to the method for adjusting the number of threads in the microservice thread pool. The specific implementation of the device can refer to the description of the aforementioned method embodiment part, and the repeated parts will not be repeated. Figure 2 , the device comprises:

[0054] Determining unit 201 is used to determine a first thread processing capability at every set time range, wherein the first thread processing capability indicates the capability of a thread pool to process multiple requests simultaneously.

[0055] Specifically used to determine the first thread processing capacity is the ratio of the total number of requests completed by the thread pool within a set time range to the maximum number of concurrent threads within the set time range.

[0056] The first thread processing capability may also be an arithmetic average of first thread capabilities that are continuously greater than one set time range.

[0057] Preferably, the arithmetic mean of the first thread processing capabilities corresponding to two adjacent set time ranges is used as the first thread processing capability.

[0058] The acquisition unit 202 is used to acquire the number of pending requests, the number of idle threads, and the preset maximum number of threads of the thread pool, wherein the preset maximum number of threads indicates the maximum number of threads provided by the thread pool.

[0059] The above-mentioned number of idle threads and the preset maximum number of threads belong to the thread pool configuration information. The thread pool configuration information can be saved as cache data. The cache data can be divided into remote cache data and local cache data. Compared with local cache data, remote cache data can respond efficiently and is convenient for real-time acquisition. However, it cannot be guaranteed that the remote cache data can be successfully connected every time. Therefore, it is necessary to synchronize the thread pool configuration information in the remote cache data to the local cache data every set time range. This ensures that when the remote cache data fails to be obtained, it can be obtained through the local cache data. In addition, the local cache number needs to be saved in a shared persistent storage device, so as to ensure that when the system is restarted or reloaded, that is, when the remote cache data and the local cache data are cleared, the thread pool configuration information can be obtained from the shared persistent storage device.

[0060] Adjustment unit 203: When the ratio of the number of pending requests to the processing capability of the first thread is greater than the idle thread, determine to adjust the preset maximum number of threads to obtain the first maximum number of threads. The first maximum number of threads is the sum of the difference between the ratio of the number of pending requests to the processing capability of the first thread and the number of idle threads and the preset maximum number of threads; the first maximum number of threads is greater than the preset maximum number of threads.

[0061] The above-mentioned device for adjusting the number of threads in the microservice thread pool also includes a minimum unit, which is specifically used to obtain the maximum number of concurrent threads in the thread pool; wherein the maximum number of concurrent threads indicates the number of threads in the thread pool that simultaneously process requests per unit time; and the minimum value of the first maximum number of threads is determined to be the maximum number of concurrent threads.

[0062] The above-mentioned device for adjusting the number of threads in the microservice thread pool also includes a reduction unit, which is specifically used to reduce the first maximum number of threads by a set value to obtain a second maximum number of threads when the system load is lower than a first threshold, or the system load change rate is lower than a second threshold, or every set time range; wherein the second maximum number of threads is not less than the minimum value.

[0063] Based on the same inventive concept, the embodiment of the present application further provides a readable storage medium, including:

[0064] Memory,

[0065] The memory is used to store instructions. When the instructions are executed by the processor, the device including the readable storage medium completes the method for adjusting the number of threads in the microservice thread pool as described above.

[0066] Those skilled in the art can clearly understand that for the convenience and simplicity of description, only the division of the above-mentioned functional modules is used as an example. In actual applications, the above-mentioned functions can be assigned to different functional modules as needed, that is, the internal structure of the device is divided into different functional modules to complete all or part of the functions described above. The specific working process of the system, device and unit described above can refer to the corresponding process in the aforementioned method embodiment, and will not be repeated here.

[0067] In the several embodiments provided by the present invention, it should be understood that the disclosed devices and methods can be implemented in other ways. For example, the device embodiments described above are only schematic. For example, the division of the modules or units is only a logical function division. There may be other division methods in actual implementation, such as multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of devices or units, which can be electrical, mechanical or other forms.

[0068] The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.

[0069] In addition, each functional unit in each embodiment of the present application may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit. The above-mentioned integrated unit may be implemented in the form of hardware or in the form of software functional units.

[0070] If the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present application is essentially or the part that contributes to the prior art or all or part of the technical solution can be embodied in the form of a software product, and the computer software product is stored in a storage medium, including a number of instructions to enable a computer device (which can be a personal computer, a server, or a network device, etc.) or a processor (processor) to perform all or part of the steps of the method described in each embodiment of the present application. The aforementioned storage medium includes: a universal serial bus flash disk (Universal Serial Bus flash disk), a mobile hard disk, a read-only memory (Read-Only Memory, ROM), a random access memory (Random Access Memory, RAM), a disk or an optical disk, and other media that can store program codes.

[0071] Obviously, those skilled in the art can make various changes and modifications to the present invention without departing from the spirit and scope of the present invention. Thus, if these modifications and variations of the present invention fall within the scope of the claims of the present invention and their equivalents, the present invention is also intended to include these modifications and variations.

Claims

1. A method for adjusting the number of threads in a microservice thread pool, characterized in that: The method comprises: Determine the first thread processing capacity at every set time range; wherein the first thread processing capacity indicates the ability of the thread pool to process multiple requests simultaneously, and the first thread processing capacity is the ratio of the total number of requests completed by the thread pool within the set time range to the maximum number of concurrent threads within the set time range; Obtaining the number of pending requests, the number of idle threads, and the preset maximum number of threads of the thread pool; wherein the preset maximum number of threads indicates the maximum number of threads provided by the thread pool; When the ratio of the number of pending requests to the processing capacity of the first thread is greater than the idle thread, it is determined to adjust the preset maximum number of threads to obtain the first maximum number of threads; wherein the first maximum number of threads is the sum of the difference between the ratio of the number of pending requests to the processing capacity of the first thread and the number of idle threads and the preset maximum number of threads; the first maximum number of threads is greater than the preset maximum number of threads.

2. The method according to claim 1, characterized in that The first thread processing capacity is an arithmetic average of the first thread processing capacities corresponding to two adjacent set time ranges.

3. The method according to claim 2, characterized in that After obtaining the first maximum number of threads, the method further comprises: Obtain the maximum number of concurrent threads in the thread pool; wherein the maximum number of concurrent threads indicates the number of threads in the thread pool that simultaneously process requests per unit time; Determine a minimum value of the first maximum number of threads as the maximum number of concurrent threads.

4. The method according to claim 3, characterized in that The method also includes: When the system load is lower than a first threshold, or the system load change rate is lower than a second threshold, or every set time range, the first maximum number of threads is reduced by a set value to obtain a second maximum number of threads; wherein the second maximum number of threads is not less than the minimum value.

5. A device for adjusting the number of threads in a microservice thread pool, characterized in that: The device comprises: A determination unit: used to determine the first thread processing capacity at every set time range; wherein the first thread processing capacity indicates the ability of the thread pool to process multiple requests simultaneously, and the first thread processing capacity is the ratio of the total number of requests completed by the thread pool within the set time range to the maximum number of concurrent threads within the set time range; An acquisition unit: used to acquire the number of pending requests, the number of idle threads, and the preset maximum number of threads of the thread pool; wherein the preset maximum number of threads indicates the maximum number of threads provided by the thread pool; An adjustment unit: used for determining to adjust the preset maximum number of threads to obtain a first maximum number of threads when the ratio of the number of pending requests to the processing capacity of the first thread is greater than the idle thread; wherein the first maximum number of threads is the sum of the difference between the ratio of the number of pending requests to the processing capacity of the first thread and the number of idle threads and the preset maximum number of threads; the first maximum number of threads is greater than the preset maximum number of threads.

6. The device according to claim 5, characterized in that The device also includes a minimum unit, which is specifically used to obtain the maximum number of concurrent threads in the thread pool; wherein the maximum number of concurrent threads indicates the number of threads in the thread pool that simultaneously process requests per unit time; and the minimum value of the first maximum number of threads is determined to be the maximum number of concurrent threads.

7. The device according to claim 6, characterized in that The device also includes a reduction unit, which is specifically used to reduce the first maximum number of threads by a set value to obtain a second maximum number of threads when the system load is lower than a first threshold, or the system load change rate is lower than a second threshold, or every set time range; wherein the second maximum number of threads is not less than the minimum value.

8. A readable storage medium, characterized in that: in, Including memory, The memory is used to store instructions. When the instructions are executed by the processor, the device including the readable storage medium implements the method according to any one of claims 1 to 4.

Citation Information

Patent Citations

  • Method and device for controlling thread quantity in thread pool

    CN107220033A