Asynchronous program processing method, system and computer device
By creating independent resource coordination and program execution services in the asynchronous program execution system, using message queues and distributed locks to monitor heartbeat information and load balancing algorithms to handle downtime situations, the problem of insufficient execution node monitoring and management in traditional systems is solved. Efficient and stable execution and resource optimization of asynchronous programs are achieved.
Patent Information
- Application Number
- CN202410842628.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-06-27
- Publication Date
- 2024-09-03
- Estimated Expiration
- 2044-06-27
AI Technical Summary
Traditional asynchronous program execution systems lack effective monitoring and management of execution nodes, resulting in tight coupling of resource coordination and task execution, making it difficult to cope with complex business requirements, resulting in low efficiency or failure of task execution.
By creating independent resource coordination services and program execution services, using message queues and distributed lock mechanisms, we monitor the heartbeat information of asynchronous programs, detect outages in a timely manner, and transfer the outage programs to healthy instances through load balancing algorithms to try again. , ensure reasonable allocation of resources and continuity of tasks.
It achieves efficient, orderly and stable execution of asynchronous programs, enhances the robustness, maintainability and observability of the system, improves processing efficiency and user experience, and ensures effective utilization of resources.
Smart Images

Figure CN118394531B
Abstract
Description
Technical Field
[0001] The present application relates to the technical field of computer program processing, and in particular to an asynchronous program processing method, system and computer device. Background Art
[0002] With the rapid development of computer information technology, the demand for concurrent and asynchronous processing in modern software systems is increasing. Asynchronous program execution can not only improve the responsiveness and throughput of the system, but also effectively avoid resource blocking and improve resource utilization.
[0003] Although asynchronous execution and processing of programs are widely used in various computer systems, traditional asynchronous program execution systems often lack effective monitoring and management of execution nodes: on the one hand, resource coordination and task execution are often tightly coupled during traditional asynchronous task execution, which limits the flexibility and scalability of the system. When a task execution node fails, there is a lack of effective monitoring, failover and recovery mechanisms. Once an execution node crashes or other abnormal conditions occur, the entire service may be seriously affected. On the other hand, existing processing systems usually adopt static strategies in execution and management, which are difficult to adapt to complex and changing business needs, resulting in inefficient or failed task execution. Therefore, how to efficiently, orderly and stably manage and execute these asynchronous programs to ensure the rational allocation and effective use of resources has become an urgent problem in the current technical field. Summary of the invention
[0004] Based on this, the present application proposes an asynchronous program processing method and system to address the above-mentioned problems, aiming to manage and execute asynchronous programs efficiently, orderly and stably, and ensure the rational allocation and effective utilization of resources.
[0005] On one hand, the present application provides an asynchronous program processing method, characterized in that the method comprises:
[0006] Create independent resource coordination services and program execution services;
[0007] Encapsulate the asynchronous program to be executed and add it to the preset message queue, and set a compensation retry flag for the asynchronous program;
[0008] Creating one or more service instances based on the program execution service;
[0009] Execute the asynchronous program according to the created service instance, and send the heartbeat information of the asynchronous program to the resource coordination service;
[0010] The resource coordination service determines whether the asynchronous program execution is down according to the heartbeat of the asynchronous program;
[0011] If the system crashes, the compensation retry flag of the asynchronous program is modified, and a crash retry is triggered.
[0012] Preferably, the executing the asynchronous program based on the service instance of the asynchronous program service comprises:
[0013] Setting a timeout parameter of the distributed lock according to the execution duration of the asynchronous program;
[0014] Obtaining a compensation retry flag of the asynchronous program;
[0015] If the compensation retry flag is the first value, after requesting the distributed lock service to obtain the distributed lock, the asynchronous program business logic is executed;
[0016] If the compensation retry flag is the second value, the distributed lock and cached data are released, and the asynchronous program business logic is executed after the distributed lock is re-requested.
[0017] Preferably, the resource coordination service determines whether the asynchronous program execution is down according to the heartbeat of the asynchronous program, including:
[0018] Query the asynchronous program being executed, read and detect the heartbeat information of the corresponding asynchronous program;
[0019] If the heartbeat of the executing asynchronous program is not received within the preset time window, the container of the corresponding service instance is monitored;
[0020] If the container of the corresponding service instance is abnormal, it is determined that the current asynchronous program is down.
[0021] Preferably, the method further comprises:
[0022] Obtaining the downtime asynchronous program ID, and adding the downtime asynchronous program to the queue again;
[0023] The downtime asynchronous program is transferred to a non-downtime target service instance for retry.
[0024] Preferably, transferring the downtime asynchronous program to a non-downtime target service instance for retrying includes:
[0025] Assign a weight to each non-downtime service instance and calculate the cumulative weight of each non-downtime service instance:
[0026] a[i] = a[i-1] + w i , a[0] = 0;
[0027] Generate a random number r and take the modulus of the random number: I=r mod a[n];
[0028] Match the target service instance k: k=min{i|I≤a[i], 1≤i≤n};
[0029] Where k is the index of the first service instance that meets the condition, and w i is the weight of the i-th service instance, and n is the number of service instances that have not crashed;
[0030] Updating the target service instance weight;
[0031] The downtime asynchronous program is transferred to the target service instance for retrying execution.
[0032] Furthermore, the weight assigned to each non-downtime service instance includes:
[0033]
[0034] Among them, w i is the weight of the i-th service instance, R ij The quantitative score of the i-th service instance on the j-th resource dimension, is the weight of the j-th resource dimension, T i , L i 、E i are the quantitative scores of the average response time, load, and historical failures of the i-th instance, and α, β, and γ correspond to their weights respectively.
[0035] Preferably, the downtime retry includes:
[0036] Record the number of retries of asynchronous programs that crash;
[0037] If the number of retries is less than the preset threshold, the retry is performed at a time interval set according to the exponential backoff algorithm:
[0038] , T i is the time interval of the i-th retry, b is the preset exponential base, and c is the adjustment coefficient;
[0039] If the number of retries is greater than or equal to a preset threshold, the asynchronous program execution is terminated, log information is generated, and an asynchronous program execution exception notification is sent.
[0040] A second aspect of the present application provides a computer asynchronous program processing system, the system comprising:
[0041] A service creation module, used to create independent resource coordination service modules and program execution service modules;
[0042] A message queue module, used to encapsulate the asynchronous program to be executed and add it to a preset message queue, and set a compensation retry flag for the asynchronous program;
[0043] The program execution service module is used to create one or more service instances, execute the asynchronous program, and send the heartbeat information of the asynchronous program to the resource coordination service module;
[0044] The resource coordination service module is used to determine whether the asynchronous program execution is down based on the heartbeat of the asynchronous program. If it is down, the compensation retry flag of the corresponding asynchronous program is modified and the downtime retry is triggered.
[0045] The third aspect of the present application provides a computer-readable storage medium storing a computer program, which, when executed by a processor, enables the processor to perform the steps of any one of the above methods.
[0046] A fourth aspect of the present application provides a computer terminal device, comprising a memory and a processor, wherein the memory stores a computer program, and when the computer program is executed by the processor, the processor executes the steps of any one of the above methods.
[0047] The asynchronous program processing solution provided in the present application is based on the resource coordination-program execution design pattern, and establishes independent resource coordination services and program services to monitor and execute programs respectively, so that the resource coordination service and program execution are logically and physically decoupled. The resource coordination service is independently executed to monitor and detect the heartbeat of program execution, which can timely discover potential problems and downtime, and quickly distribute the downtime program to other non-downtime service instances for retry based on service high availability, ensuring the continuity and integrity of the task, helping to enhance the robustness, maintainability and observability of the system, and improving the stability of the system, processing efficiency, and user experience. Furthermore, when the asynchronous program is down and retried, the solution of the present application matches the target service instance through a load balancing optimization algorithm to ensure that the task or program is distributed according to the processing capacity of the instance, effectively balancing the load of each service instance and optimizing the efficiency of resource utilization. BRIEF DESCRIPTION OF THE DRAWINGS
[0048] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art 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.
[0049] in:
[0050] Figure 1 is a flow chart of an asynchronous program processing method in one embodiment;
[0051] Figure 2A flowchart of an asynchronous program processing process in one embodiment;
[0052] Figure 3 It is a structural block diagram of a computer asynchronous program processing system in one embodiment;
[0053] Figure 4 FIG. 4 is a structural block diagram of a computer device in one embodiment. DETAILED DESCRIPTION
[0054] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of 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.
[0055] The terms "include", "comprising" and "having" and any variations thereof in the specification and claims of the present application and the above-mentioned drawings are intended to cover non-exclusive inclusions. For example, a process, method, system, product or device comprising a series of steps or units is not limited to the listed steps or units, but may optionally include steps or units that are not listed, or may optionally include other steps or units that are inherent to these processes, methods, products or devices. In the claims, specification and drawings of the present application, relational terms such as "first" and "second" are merely used to distinguish one entity / operation / object from another entity / operation / object, and do not necessarily require or imply any such actual relationship or order between these entities / operations / objects.
[0056] Reference to "embodiments" herein means that a particular feature, structure, or characteristic described in conjunction with the embodiments may be included in at least one embodiment of the present application. The occurrence of the phrase at various locations in the specification does not necessarily refer to the same embodiment, nor is it an independent or alternative embodiment that is mutually exclusive with other embodiments. It is explicitly and implicitly understood by those skilled in the art that the embodiments described herein may be combined with other embodiments.
[0057] It should be noted that the steps shown in the flowcharts of the accompanying drawings can be executed in a computer system such as a set of computer executable instructions, and that, although a logical order is shown in the flowcharts, in some cases, the steps shown or described can be executed in an order different from that shown here.
[0058] In one embodiment, if Figure 1 The figure shows a flow chart of an asynchronous program processing method of the present application, the method comprising:
[0059] S10. Create independent resource coordination services and program execution services.
[0060] Specifically, in an asynchronous program processing implementation of the present application, monitoring scheduling and program execution adopt a design pattern that is independent of each other, wherein the resource coordination service is mainly responsible for the scheduling and monitoring of asynchronous programs, and the program execution service is mainly responsible for executing various tasks or applications, and providing the necessary operating environment and service support. In the embodiments of the present application, the resource coordination service and the program service can be distributed on different computer nodes for execution, or they can be executed on the same computer node, and independent microservices, containerization (docker) or virtual machine technologies can be used.
[0061] In some embodiments, the present application containerizes the resource coordination service and the program execution service separately, uses Docker container technology, and adopts container orchestration tools such as Kubernetes to manage the deployment, expansion, and automatic recovery of services, and ensures that the monitoring service and the program execution service are independent of each other in different containers or clusters through independent deployment.
[0062] The above solution of this application creates independent resource coordination services and program services to ensure that they run in independent and isolated environments. The services and program execution services are logically and physically decoupled, thereby enhancing the robustness, maintainability and observability of the system.
[0063] S11. Encapsulate the asynchronous program to be executed and add it to a preset message queue, and set a compensation retry flag for the asynchronous program.
[0064] Specifically, in one embodiment of the present application, the asynchronous program to be processed is processed by means of a message queue. The package of the asynchronous program to be executed is added to the preset message queue, which can be executed by the corresponding message service of the system, or by calling the corresponding message service by the resource coordination service, which specifically includes:
[0065] S111. Choose a suitable message queue system to store and manage asynchronous tasks, such as RabbitMQ, Kafka, Amazon SQS, Google Cloud Pub / Sub, etc.
[0066] S112, define the format of the message, create one or more queues in the message queue system according to the business logic and performance requirements, and store the messages to be processed to achieve more fine-grained control and optimization. Preferably, the message defined in this application includes relevant information such as message identifier, message type, program identifier, program address, program status, input parameters, priority, and calling service instance parameters;
[0067] S113: Encapsulate the asynchronous program to be executed as a message and send it to a preset message queue. When sending the message, you can specify options such as the target queue, message priority, and delayed sending time.
[0068] Furthermore, in some embodiments of the present application, a compensation retry flag forceFlag is set for each asynchronous program. The supplementary retry flag is used to determine whether the current execution of the program is a retry execution, so that the program execution service adopts different execution strategies according to the flag. The compensation retry flag forceFlag is initially assigned a value of 0.
[0069] S12. Create one or more service instances based on the program execution service.
[0070] In a distributed system or microservice architecture, each service instance is a separate copy of the service that runs independently in a physical or virtual environment and has its own resource allocation (such as CPU, memory, disk space, etc.) and network address (such as IP address and port number).
[0071] Preferably, the present application establishes multiple service instances in the program execution service, specifically including: multiple replica instances of the service deployed on containers or virtual machines, as well as the computing, storage and network resources required for each instance, and configures the corresponding service interface, access point and environment for each service instance, including operating system, database, middleware, etc., to ensure that they can receive and respond to requests. Then, a service registry is used to register each service instance and allow the client or load balancer to discover available service instances. Further, requests are distributed to different service instances through a load balancer to balance the load and improve response speed. And regular health checks are performed on service instances to ensure that they are operating normally and trigger failover when problems are detected.
[0072] The above solution of the present application improves the throughput and availability of the system by establishing one or more service instances in the program execution service to execute asynchronous programs, and ensures that the service can be independently deployed, expanded and highly available.
[0073] S13. Execute the asynchronous program according to the created service instance, and send the heartbeat information of the asynchronous program to the resource coordination service.
[0074] Specifically, the service instance pulls or receives push messages from the message queue, parses the message content, and extracts task data. It executes asynchronous programs or tasks by calling preset APIs and performing corresponding calculations or data processing, and then stores the program execution results in a database, cache, or other storage system for subsequent use or query.
[0075] Furthermore, in the implementation scheme of the present application, when the service instance executes the asynchronous program, it will simultaneously start a listening thread in the thread to regularly report the heartbeat, and send the heartbeat information of the current asynchronous program to the resource coordination service at preset time intervals, such as 5s, 10s, 30s, 60s, 100s, etc. Preferably, the heartbeat information can be sent to the corresponding message queue or database or API endpoint through a preset API or by creating a corresponding function, and then read by the resource coordination service from the corresponding queue, database, API data end.
[0076] More specifically, in some embodiments of the present application, the execution logic and steps when executing an asynchronous program based on the service instance of the asynchronous program service include:
[0077] S131. Setting a timeout parameter of the distributed lock according to the execution duration of the asynchronous program.
[0078] Specifically, a preset distributed lock implementation method is selected, such as a distributed lock based on Redis, Zookeeper, etc. For example, the setnx command of Redis can be used to implement a distributed lock. At the same time, in order to avoid deadlock, a timeout parameter needs to be set for the lock. In one embodiment of the present application, the timeout parameter t of the lock is set according to the execution time tc parameter of the current asynchronous program: tc
[0079] S132: Obtain a compensation retry flag of the asynchronous program.
[0080] Specifically, before executing the asynchronous program business logic, the service instance obtains the compensation retry flag of the asynchronous program to be executed, and determines whether the current asynchronous program execution is a retry execution according to the compensation retry flag value of the asynchronous program. Preferably, the value of the compensation retry flag forceFlag includes two values: 0 and 1.
[0081] S133: If the compensation retry flag is the first value, request a distributed lock service to obtain a distributed lock and execute the asynchronous program business logic.
[0082] Specifically, the compensation retry flag forceFlag=0 of the asynchronous program indicates that the current asynchronous program is executed as a non-retry execution. The service instance sends a request to obtain a distributed lock to the distributed lock service to obtain the distributed lock. If the lock is successfully obtained, the task processing begins; otherwise, it waits or retries to obtain the lock. After the task processing is completed, the distributed lock is released.
[0083] S134: If the compensation retry flag is the second value, the distributed lock and cached data are released, and after re-requesting the distributed lock, the asynchronous program business logic is executed.
[0084] Specifically, the compensation retry flag forceFlag=1 of the asynchronous program indicates that the current asynchronous program is executed as a retry. After the service instance releases the distributed lock and the cached data, it resends a request to obtain the distributed lock to the distributed lock service to obtain the distributed lock. If the lock is successfully acquired, the task is processed. After the task is processed, the distributed lock is released.
[0085] S14. The resource coordination service determines whether the asynchronous program execution is down based on the heartbeat of the asynchronous program.
[0086] Specifically, the resource coordination service uses heartbeats to determine whether the asynchronous program being executed has crashed, including:
[0087] The resource coordination service queries the asynchronous program being executed, and reads and detects the heartbeat information of the asynchronous program being executed from the corresponding queue, database or API data terminal: the resource coordination service sets a time threshold, which is usually set to be several times the heartbeat interval, and checks the timestamp of the most recently received heartbeat message. If the heartbeat of the asynchronous program being executed is not received within the preset time threshold window, then it can be considered that the asynchronous program may have been abnormal or become unresponsive. The resource coordination service further calls and monitors whether the container of the corresponding service instance is abnormal, including: detecting the CPU, memory, disk and network status in the container of the corresponding service instance. If the container of the corresponding service instance is abnormal, it is determined that the current asynchronous program is down.
[0088] S15. If the system crashes, modify the compensation retry flag of the crash asynchronous program and trigger a crash retry.
[0089] Specifically, if the resource coordination service determines that the asynchronous program execution is down based on the heartbeat information, the value of the compensation retry flag forceFlag of the corresponding down asynchronous program is modified to 1, so as to inform the asynchronous program execution service that the asynchronous program is down and retry, so that when the asynchronous program execution service executes the asynchronous program again, it adopts the corresponding execution strategy to execute the asynchronous program. When a downtime is detected, the resource coordination service records the downtime event in the log system, including the program ID, downtime time, possible error information, etc.
[0090] Preferably, the method further comprises:
[0091] S151, obtaining the downtime asynchronous program ID, and adding the downtime asynchronous program to the queue again.
[0092] Specifically, the ID of the downtime asynchronous program is obtained, and a new task message is constructed for the asynchronous program, and the message is sent to the task queue. At the same time, in order to avoid adding the same task to the queue multiple times, this application also adopts a deduplication mechanism to deduplicate: use data structures such as Redis to track which tasks have been re-added, and before adding a task to the queue, check whether the ID of the task exists in Redis.
[0093] Furthermore, during the requeuing process, add corresponding error handling logic to handle possible exceptions or failures. For example, if you cannot connect to the task queue system or send a message, the program should catch these exceptions and record error logs.
[0094] S152: Transfer the downtime asynchronous program to a non-downtime target service instance for retry.
[0095] Specifically, when an asynchronous program crashes, the program execution service ends abnormally due to the crash. The service instance can be automatically removed from the service list, and available service instances that have not crashed can be dynamically obtained based on the service discovery mechanism (such as Consul, Eureka, ZooKeeper, etc.), and a target service instance is selected for retry through a preset load balancing algorithm.
[0096] Preferably, in one embodiment of the present application, the target service instance that is not down is matched based on a weighted round-robin load balancing algorithm. This includes:
[0097] S1521. Assign weight to each service instance that is not down:
[0098]
[0099] Among them, w i is the weight of the i-th service instance, R ij The quantitative score of the i-th service instance in the j-th resource dimension, including: CPU score, memory score, storage score and scores in other resource dimensions, is the weight of the j-th resource dimension, T i , L i 、E i are the quantitative scores of the average response time, load, and historical failures of the ith instance, and α, β, and γ correspond to their weights respectively.
[0100] S1522. Calculate the cumulative weight of each service instance that is not down: a[i] = a[i-1] + w i , where a[0] = 0;
[0101] S1523, generate a random number r and perform modulus on the random number: I=r mod a[n];
[0102] S1524. Match and obtain the target service instance k: k=min{i|I≤a[i],1≤i≤n}, where k is the index of the first service instance that meets the conditions, and n is the number of service instances that are not down.
[0103] S1525. Update the target service instance weight;
[0104] The downtime asynchronous program is transferred to the target service instance for retrying execution.
[0105] The above-mentioned embodiment of the present application assigns different weight values to each service instance, and then uses weighted polling to determine whether the downed program should be transferred to the corresponding healthy target instance for execution. The weight can be dynamically adjusted according to the performance and load of the service to ensure that the tasks are evenly distributed among the healthy instances, thereby improving the availability and stability of the system.
[0106] Furthermore, in some embodiments of the present application, after matching the target service instance, when the program task is taken out of the queue, a lock associated with the asynchronous program ID or unique identifier is acquired, or tags or parameters are set to associate with the target service instance, such as by adding a field to the program data or setting the service instance parameters through a message, where the field or parameter contains the identifier of the instance that processes the task, and the service instance can then check this field to ensure that it only processes tasks that match its own identifier.
[0107] Preferably, in some embodiments, the downtime asynchronous program retry further includes:
[0108] Record the number of retries of asynchronous programs that crash;
[0109] If the number of retries is less than the preset threshold, the retry is performed at a time interval set according to the exponential backoff algorithm:
[0110] , T i is the time interval of the i-th retry, b is the preset exponential base, and c is the adjustment coefficient;
[0111] If the number of retries is greater than or equal to a preset threshold, the asynchronous program execution is terminated, log information is generated, and an asynchronous program execution exception notification is sent.
[0112] In one embodiment, Figure 2 Shown is a flowchart of the asynchronous program processing process based on the resource coordination service-program execution service design pattern of this application.
[0113] The above-mentioned embodiment of the present application determines the number of retries during retry. If it is within the preset number, the exponential backoff algorithm is used to determine the time for retrying, which effectively reduces the frequency of resending requests immediately after continuous failures and reduces the possibility of generating a large number of retry requests in a short period of time, thereby reducing the risk of network congestion. At the same time, as the backoff time increases, the possibility of network congestion decreases, which increases the possibility of successful operation in subsequent retries or retries. Especially when dealing with current limiting or API call frequency limiting, the exponential backoff strategy can ensure that the client waits enough time before retrying to avoid being rejected due to excessive frequency.
[0114] The above implementation scheme of the present application establishes independent resource coordination services and program services to monitor and execute programs respectively based on the resource coordination-program execution design pattern, so that the resource coordination service and program execution are logically and physically decoupled. The resource coordination service is independently executed to monitor and detect the heartbeat of program execution, which can timely discover potential problems and downtime, and based on the high availability of the service, quickly allocate the downtime program to other non-downtime service instances for retry, ensuring the continuity and integrity of the task, helping to enhance the robustness, maintainability and observability of the system, and improving the stability, efficiency and user experience of the system.
[0115] In one embodiment, Figure 3 This is a structural block diagram of a computer asynchronous program processing system provided by the present application (the arrows in the figure are only simple relationship diagrams), the system includes:
[0116] A service creation module, used to create independent resource coordination service modules and program execution service modules;
[0117] A message queue module, used to encapsulate the asynchronous program to be executed and add it to a preset message queue, and set a compensation retry flag for the asynchronous program;
[0118] The program execution service module is used to create one or more service instances, execute the asynchronous program, and send the heartbeat information of the asynchronous program to the resource coordination service module;
[0119] The resource coordination service module is used to determine whether the asynchronous program execution is down based on the heartbeat of the asynchronous program. If it is down, the compensation retry flag of the corresponding asynchronous program is modified and the downtime retry is triggered.
[0120] In one embodiment, the present application further provides a computer-readable storage medium storing a computer program, wherein when the computer program is executed by a processor, the processor performs the following steps:
[0121] Create independent resource coordination services and program execution services;
[0122] Encapsulate the asynchronous program to be executed and add it to the preset message queue, and set a compensation retry flag for the asynchronous program;
[0123] Creating one or more service instances based on the program execution service;
[0124] Execute the asynchronous program according to the created service instance, and send the heartbeat information of the asynchronous program to the resource coordination service;
[0125] The resource coordination service determines whether the asynchronous program execution is down according to the heartbeat of the asynchronous program;
[0126] If the system crashes, the compensation retry flag of the asynchronous program is modified, and a crash retry is triggered.
[0127] In one embodiment, Figure 4 The present application also provides a computer device, including a memory and a processor, wherein the memory stores a computer program, and when the computer program is executed by the processor, the processor performs the following steps:
[0128] Create independent resource coordination services and program execution services;
[0129] Encapsulate the asynchronous program to be executed and add it to the preset message queue, and set a compensation retry flag for the asynchronous program;
[0130] Creating one or more service instances based on the program execution service;
[0131] Execute the asynchronous program according to the created service instance, and send the heartbeat information of the asynchronous program to the resource coordination service;
[0132] The resource coordination service determines whether the asynchronous program execution is down according to the heartbeat of the asynchronous program;
[0133] If the system crashes, the compensation retry flag of the asynchronous program is modified, and a crash retry is triggered.
[0134] Those skilled in the art can understand that all or part of the processes in the above-mentioned embodiment methods can be completed by instructing the relevant hardware through a computer program, and the program can be stored in a non-volatile computer-readable storage medium. When the program is executed, it can include the processes of the embodiments of the above-mentioned methods. Among them, any reference to memory, storage, database or other media used in the embodiments provided in this application can include non-volatile and / or volatile memory. Non-volatile memory may include read-only memory (ROM), programmable ROM (PROM), electrically programmable ROM (EPROM), electrically erasable programmable ROM (EEPROM) or flash memory. Volatile memory may include random access memory (RAM) or external cache memory. As an illustration and not limitation, RAM is available in many forms, such as static RAM (SRAM), dynamic RAM (DRAM), synchronous DRAM (SDRAM), double data rate SDRAM (DDRSDRAM), enhanced SDRAM (ESDRAM), synchronous link (Synchlink) DRAM (SLDRAM), memory bus (Rambus) direct RAM (RDRAM), direct memory bus dynamic RAM (DRDRAM), and memory bus dynamic RAM (RDRAM).
[0135] The technical features of the above embodiments may be combined arbitrarily. To make the description concise, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this specification.
[0136] The above-mentioned embodiments only express several implementation methods of the present application, and the descriptions thereof are relatively specific and detailed, but they cannot be understood as limiting the scope of the present application. It should be pointed out that, for a person of ordinary skill in the art, several variations and improvements can be made without departing from the concept of the present application, and these all belong to the protection scope of the present application. Therefore, the protection scope of the present application shall be subject to the attached claims.
Claims
1. An asynchronous program processing method, It is characterized in that The method comprises: Create independent resource coordination services and program execution services; Encapsulate the asynchronous program to be executed and add it to the preset message queue, and set a compensation retry flag for the asynchronous program; Creating one or more service instances based on the program execution service; Execute the asynchronous program according to the created service instance, and send the heartbeat information of the asynchronous program to the resource coordination service; The resource coordination service determines whether the asynchronous program execution is down according to the heartbeat of the asynchronous program; If the system crashes, modify the compensation retry flag of the asynchronous program and trigger a crash retry; The step of executing the asynchronous program according to the created service instance includes: Obtaining a compensation retry flag of the asynchronous program; If the compensation retry flag is the first value, after requesting the distributed lock service to obtain the distributed lock, the asynchronous program business logic is executed; If the compensation retry flag is the second value, the distributed lock and cached data are released, and the asynchronous program business logic is executed after the distributed lock is re-requested.
2. The method according to claim 1, It is characterized in that The method further comprises: The timeout parameter of the distributed lock is set according to the execution duration of the asynchronous program.
3. The method according to claim 1, It is characterized in that The resource coordination service determines whether the asynchronous program execution is down according to the heartbeat of the asynchronous program, including: Query the asynchronous program being executed, read and detect the heartbeat information of the corresponding asynchronous program; If the heartbeat of the executing asynchronous program is not received within the preset time window, the container of the corresponding service instance is monitored; If the container of the corresponding service instance is abnormal, it is determined that the asynchronous program execution is down.
4. The method according to claim 1, It is characterized in that The triggering of downtime retry includes: Obtaining the downtime asynchronous program ID, and adding the downtime asynchronous program to the queue again; The downtime asynchronous program is transferred to a non-downtime target service instance for retry.
5. The method according to claim 4, It is characterized in that The transferring the downtime asynchronous program to a non-downtime target service instance for retrying also includes: Assign a weight to each non-downtime service instance and calculate the cumulative weight of each non-downtime service instance: a[i] = a[i-1] + w i ,a[0] = 0; Generate a random number r and take the modulus of the random number: I=r mod a[n]; Match the target service instance k: k=min{i|I≤a[i], 1≤i≤n}; Where k is the index of the first service instance that meets the condition, and w i is the weight of the i-th service instance, and n is the number of service instances that have not crashed; Updating the target service instance weight; The downtime asynchronous program is transferred to the target service instance for retrying execution.
6. The method according to claim 5, It is characterized in that The weight is assigned to each non-downtime service instance, including: Among them, w i is the weight of the i-th service instance, R ij The quantitative score of the i-th service instance on the j-th resource dimension, is the weight of the j-th resource dimension, T i , L i 、E i are the quantitative scores of the average response time, load, and historical failures of the ith instance, and α, β, and γ correspond to their weights respectively.
7. The method according to any one of claims 1 to 6, It is characterized in that The downtime retry includes: Record the number of retries of asynchronous programs that crash; If the number of retries is less than the preset threshold, the retry is performed at a time interval set according to the exponential backoff algorithm: Among them, T i is the time interval of the i-th retry, b is the preset exponential base, and c is the adjustment coefficient; If the number of retries is greater than or equal to a preset threshold, the asynchronous program execution is terminated, log information is generated, and an asynchronous program execution exception notification is sent.
8. A computer asynchronous program processing system, It is characterized in that The system comprises: A service creation module, used to create independent resource coordination service modules and program execution service modules; A message queue module, used to encapsulate the asynchronous program to be executed and add it to a preset message queue, and set a compensation retry flag for the asynchronous program; The program execution service module is used to create one or more service instances, execute the asynchronous program according to the created service instances, and send the heartbeat information of the asynchronous program to the resource coordination service module; The resource coordination service module is used to determine whether the asynchronous program execution is down according to the heartbeat of the asynchronous program. If it is down, modify the compensation retry flag of the corresponding asynchronous program and trigger a downtime retry; The step of executing the asynchronous program according to the created service instance includes: Obtaining a compensation retry flag of the asynchronous program; If the compensation retry flag is the first value, after requesting the distributed lock service to obtain the distributed lock, the asynchronous program business logic is executed; If the compensation retry flag is the second value, the distributed lock and cached data are released, and the asynchronous program business logic is executed after the distributed lock is re-requested.
9. A computer-readable storage medium storing a computer program, wherein when the computer program is executed by a processor, the processor is caused to perform the steps of the method according to any one of claims 1 to 7.
10. A computer device comprising a memory and a processor, wherein the memory stores a computer program, and when the computer program is executed by the processor, the processor executes the steps of the method according to any one of claims 1 to 7.
