Context communication method of GO terminal and C terminal and KaiwuDB database
The Context communication is achieved by transmitting parameters and common memory between the GO -end and C -side in the KAIWUDB database. Cancel.
Patent Information
- Application Number
- CN202411289512.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-09-14
- Publication Date
- 2025-01-24
AI Technical Summary
In KaiwuDB database, the context of the Go side cannot be passed to the C side, resulting in the task exception in the C side that cannot be promptly fed back to the Go side, and the tasks of the Go side and the C side cannot be cancelled at the same time when canceling the current service.
Context communication is achieved by passing parameters and sharing memory between the Go side and the C side. The task ID or memory address is saved in the context of the Go side and passed to the C side. The C side completes it by recording the task ID or modifying the memory address. communication at both ends.
It ensures that the business communication between the Go side and the C side is fast and complete. When canceling the business, the executing tasks of the Go side and the C side can be ended at the same time, which improves the efficiency and convenience of the system.
Smart Images

Figure CN119356902A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of time series databases, and in particular to a context communication method between a GO terminal and a C terminal and a KaiwuDB database. Background Art
[0002] Context is a data that can carry timeout, cancellation signal, or other data related to the current request, and is used to pass between API methods or goroutines. When using Go to write background services, each request usually starts a goroutine, which may start multiple goroutines to cooperate with each other to complete the request. For example, if you request data from multiple modules at a time, you can start multiple goroutines to initiate db and rpc requests at the same time.
[0003] The basic data related to this request needs to be shared among multiple goroutines and different methods, such as userId and logId. In addition, the server generally sets a timeout for the request to avoid long-term occupation of goroutines, which may lead to resource exhaustion and service avalanche. When the timeout is reached or it is manually canceled, the goroutines related to the request need to exit quickly because their work results are no longer needed, which can also save resource overhead. In summary, context is used to solve the functions of exit notification and metadata transmission between goroutines.
[0004] Since the Inspur time series database KaiwuDB is developed in Go and C languages, in the business logic, the context of the Go side runs through the entire execution process of the Go side. When calling the task processing logic in the C side, the context of the Go side cannot be passed to the C side, resulting in the failure to timely feedback to the Go side for processing after an exception occurs in the task on the C side. When canceling the currently executing business, it is necessary to cancel the business tasks on both the Go side and the C side at the same time, so it is particularly important to ensure that the Go and C side contexts communicate. Summary of the invention
[0005] The technical task of the present invention is to address the above shortcomings and provide a context communication method and KaiwuDB database between the GO end and the C end, which can realize the business communication between the GO and the C end in the KaiwuDB database to ensure the rapid communication of the business at both ends.
[0006] The technical solution adopted by the present invention to solve its technical problem is:
[0007] A context communication method between a GO end and a C end, wherein the contexts of the GO end and the C end communicate by passing parameters and sharing memory: a task ID or a memory address is used to be saved in the context of the GO end and passed to the C end, and the C end completes the communication between the two ends by recording the task ID or modifying the memory address;
[0008] The context communication includes the CGO thread directly executing the task scenario and obtaining the thread in the thread pool through the CGO call, executing the task scenario through the thread, and canceling the task logic;
[0009] The context of the GO side and the C side is communicated. An interruption checkpoint is added to the task execution process to end the task being executed. When canceling the business, both the tasks being executed on the GO side and the tasks being executed on the C side can be canceled at the same time.
[0010] Go side: task processing logic implemented in Golang language;
[0011] C-side: Task processing logic implemented in C++ / C language;
[0012] CGO: CGO call refers to the processing logic of the GO side calling the C side;
[0013] Context: context in the Go-side business logic, which exists in the entire Go business processing process and can be abbreviated as ctx;
[0014] Thread pool: A mode of using thread resources by utilizing the idea of resource "pooling" to reduce resource consumption when threads are created, scheduled, and destroyed.
[0015] This method provides a context communication solution between the GO side and the C side. When the business processing logic needs to be executed on the Go side and the C side, the context information needs to be transmitted between the two sides to ensure the integrity of the business execution information. When the database executes a business for a long time and needs to cancel the currently executing business, it is necessary to cancel the tasks corresponding to the business on the Go side and the C side at the same time. This solution can ensure the integrity of the business information during the entire execution process, and when canceling the business, it can ensure that the executing tasks on the Go and C sides are terminated at the same time, which is more efficient and convenient.
[0016] Furthermore, the context of the GO end and the C end communicate by passing parameters: the task executor first specifies the task ID, saves the task ID to ctx through ctx on the GO end, and then copies the task ID through CGO call and writes it to the Context of the C end;
[0017] cancel_id is the C-side used to record the task id that needs to be canceled, and task_id is the C-side used to record the threads in the thread pool and the tasks they execute.
[0018] Furthermore, the CGO thread directly executes the task scenario, that is, the GO side directly executes the task on the C side through the CGO method; when the Go side calls and executes the task through CGO, the detailed processing flow of canceling the task operation is as follows:
[0019] The GO side calls CancelTask(taskID, cancel) to end the task being executed by the GO side by calling cancel(). At the same time, it calls CancleTaskID(taskID). This interface uses the CancleTaskID() method to pass the task ID to the C side through the CGO call. After receiving the task cancellation message, the C side adds the task number taskID to cancel_id.
[0020] A new interruption check is added to the task execution logic on the C side to determine whether the task needs to be canceled. The task ID is checked from cancel_id to see if it exists. If it does exist, the task is deleted from cancel_id, the executing task is terminated, and the result is returned.
[0021] Furthermore, after obtaining the thread in the thread pool through CGO call, the scenario of executing the task through the thread, that is, after the Go side executes the task, the task logic of the C side is executed through CGO call. The task logic execution process of the C side is as follows:
[0022] 1) Get the thread pool object on the C side, through which thread resources can be applied;
[0023] 2) After obtaining the thread in the thread pool, execute the task through the thread and return the threadID of the task;
[0024] 3) Save threadID and taskID in ctx to task_id;
[0025] 4) Subtask execution process: Add interruption check in the subtask processing logic to cancel the task after receiving the interruption signal. The specific process is: when the task performs interruption check, it is necessary to first read the task number from cancel_id and find the thread corresponding to the task number from task_id. If the thread is the current thread, terminate the task and return the result to the upper layer;
[0026] 5) After the subtask is completed or canceled, the task ID saved in task_id and cancel_id is deleted to ensure the correctness of the subsequent task cancellation operation;
[0027] When the GO side cancels a task, the GO side calls CancelTask(taskID, cancel), cancels the task being executed by the GO side by calling cancel(), and calls CancleTaskID(taskID) at the same time. This interface uses the CancleTaskID() method to pass the task ID to the C side through the CGO side; after the C side receives the task cancellation message, it first adds the taskID to the cancel_id, and searches for the thread number corresponding to the task ID from the task_id, and uses the thread number to terminate the executing thread task. After the cancellation is successful, the taskID in the task_id is deleted, and the taskID in the cancel_id is also deleted.
[0028] Furthermore, the context of the GO end and the C end communicate through shared memory. The GO end and the C end perform context communication through shared memory: first, the Go end applies for a piece of memory and saves the memory address in the context. When called through CGO, the memory address is passed to the C end through parameters; when the GO end or the C end modifies the content of the memory, the other end can read it directly, thereby realizing context communication between the two ends and ensuring that the two ends can transmit messages in the same task; wherein, whether the address space is released is determined by setting the value of the memory address.
[0029] Furthermore, the way to execute tasks directly on the C side through CGO calls is as follows:
[0030] When CGO executes a task, the GO side creates GOCtx (C.ctxCGO structure) and initializes the memory space. The address of GOCtx is passed to the C side through the cgo call. After receiving the GOCtx address, the C side records it in the context bound to the thread. When executing a task, a new interrupt checkpoint is added to determine whether the task needs to be canceled. If GOCtx.cancel == 1, the executing task is interrupted.
[0031] When a task is to be canceled, first cancel the task being executed on the Go side, and call CancelTask(ctxCGO*GOCtx) at the same time, directly set *ctx.cancel=1. After the setting is successful, the task executed on the C side reads the value and ends the task at the interrupt checkpoint.
[0032] Furthermore, after applying for a thread in the thread pool through CGO, the task is executed. The task execution process in the C end is as follows:
[0033] After the CGO thread applies for thread resources in the thread pool, it executes the task, adds an interruption checkpoint during task execution, and passes the address of GOCtx.cancel as a parameter of the task. When the task detects that the content in the address has changed, it interrupts the ongoing task and stores the results and data in the memory space for the GO end to receive.
[0034] After the context on the GO side is used up, it will automatically release the memory space it has applied for.
[0035] The present invention also claims protection for a KaiwuDB database, in which the contexts of the GO end and the C end communicate by passing parameters and sharing memory: the task ID or memory address is used to be stored in the context of the GO end and passed to the C end, and the C end completes the communication between the two ends by recording the task ID or modifying the memory address;
[0036] Among them, context communication includes the CGO thread directly executing the task scenario and obtaining the thread in the thread pool through CGO call, executing the task scenario through the thread, and canceling the task logic;
[0037] The context of the GO side and the C side is communicated. An interruption checkpoint is added to the task execution process to end the task being executed. When canceling the business, both the tasks being executed on the GO side and the tasks being executed on the C side can be canceled at the same time.
[0038] The present invention also claims protection for a context communication device between a GO terminal and a C terminal, comprising: at least one memory and at least one processor;
[0039] The at least one memory is used to store a machine-readable program;
[0040] The at least one processor is used to call the machine-readable program to implement the above method.
[0041] The present invention also claims protection for a computer-readable medium having computer instructions stored thereon, which can implement the above method when executed by a processor.
[0042] Compared with the prior art, the context communication method between the GO end and the C end and the KaiwuDB database of the present invention have the following beneficial effects:
[0043] The context communication solution between the GO side and the C side can ensure the integrity of the business execution information. When the business processing logic needs to be executed in the Go side and the C side, the context information needs to be transmitted between the two ends. The business can be ended more efficiently and conveniently. When the business execution time in the database is too long, the currently executing business needs to be canceled. In this case, the tasks corresponding to the business in the Go side and the C side need to be canceled at the same time. This communication solution can more conveniently end the tasks being executed at both ends. BRIEF DESCRIPTION OF THE DRAWINGS
[0044] Figure 1 It is a task processing flow diagram of the context of the GO end and the C end communicating by passing parameters provided by an embodiment of the present invention;
[0045] Figure 2 It is a task processing flow diagram of the context of the GO end and the C end communicating through a shared memory provided by an embodiment of the present invention. DETAILED DESCRIPTION
[0046] The present invention will be further described below in conjunction with specific embodiments.
[0047] The embodiment of the present invention provides a context communication method between a GO end and a C end. The contexts of the GO end and the C end communicate by passing parameters and sharing memory: a task ID or a memory address is stored in the context of the GO end and passed to the C end. The C end completes the communication between the two ends by recording the task ID or modifying the memory address.
[0048] The context communication includes the CGO thread directly executing the task scenario and obtaining the thread in the thread pool through the CGO call, executing the task scenario through the thread, and canceling the task logic;
[0049] The context of the GO side and the C side is communicated. An interruption checkpoint is added to the task execution process to end the task being executed. When canceling the business, both the tasks being executed on the GO side and the tasks being executed on the C side can be canceled at the same time.
[0050] Go side: task processing logic implemented in Golang language;
[0051] C-side: Task processing logic implemented in C++ / C language;
[0052] CGO: CGO call refers to the processing logic of the GO side calling the C side;
[0053] Context: context in the Go-side business logic, which exists in the entire Go business processing process and can be abbreviated as ctx;
[0054] Thread pool: A mode of using thread resources by utilizing the idea of resource "pooling" to reduce resource consumption when threads are created, scheduled, and destroyed.
[0055] This method provides a context communication solution between the GO side and the C side. When the business processing logic needs to be executed on the Go side and the C side, the context information needs to be transmitted between the two sides to ensure the integrity of the business execution information. When the database executes a business for a long time and needs to cancel the currently executing business, it is necessary to cancel the tasks corresponding to the business on the Go side and the C side at the same time. This solution can ensure the integrity of the business information during the entire execution process, and when canceling the business, it can ensure that the executing tasks on the Go and C sides are terminated at the same time, which is more efficient and convenient.
[0056] 1. The context of GO and C communicate by passing parameters:
[0057] The task executor first specifies the task ID, saves the task ID to ctx through ctx on the GO side, and then copies the task ID through CGO call and writes it to the Context on the C side;
[0058] cancel_id is the task id that needs to be canceled on the C side, and task_id is the thread in the thread pool and the task it executes on the C side. Figure 1 The following is the overall task processing flow, and two specific task cancellation scenarios are given:
[0059] 1.1. CGO thread directly executes tasks:
[0060] In this scenario, the GO side directly executes tasks on the C side through CGO. When the Go side calls and executes tasks through CGO, the detailed processing flow for canceling the task operation is as follows:
[0061] First, the GO side calls CancelTask(taskID, cancel), and also calls cancel() to end the task being executed by the Go side. At the same time, it calls CancleTaskID(taskID). This interface uses the CancleTaskID() method to pass the task ID to the C side through the CGO call. After the C side receives the task cancellation message, it adds the task number taskID to cancel_id.
[0062] A new interrupt check needs to be added to the task execution logic on the C side to determine whether the task needs to be canceled. The task ID is checked from cancel_id to see if it exists. If it does, the task is deleted from cancel_id, the executing task is terminated, and the result is returned.
[0063] 1.2. After obtaining the thread in the thread pool through CGO call, execute the task through the thread:
[0064] That is, after the Go side executes the task, it calls the C side’s task logic through CGO. The C side’s task logic execution process is as follows:
[0065] (1) Get the thread pool object on the C side, through which thread resources can be applied;
[0066] (2) After obtaining the thread in the thread pool, execute the task through the thread and return the threadID of the task;
[0067] (3) Save threadID and taskID in ctx to task_id;
[0068] (4) Subtask execution process: An interrupt check is added to the subtask processing logic to cancel the task after receiving an interrupt signal. The specific process is: when the task executes the interrupt check, it is necessary to first read the task number from cancel_id and find the thread corresponding to the task number from task_id. If the thread is the current thread, the task is terminated and the result is returned to the upper layer;
[0069] (5) After the subtask is completed or canceled, the task ID stored in task_id and cancel_id needs to be deleted to ensure the correctness of the subsequent task cancellation operation.
[0070] When the GO side cancels a task, the GO side calls CancelTask(taskID, cancel), which calls cancel() to cancel the task being executed by the GO side, and calls CancleTaskID(taskID) at the same time. This interface uses the CancleTaskID() method to pass the task ID to the C side through the CGO side.
[0071] After receiving the task cancellation message, the C end first adds the taskID to cancel_id, and searches for the thread number corresponding to the task ID from task_id, and uses the thread number to terminate the executing thread task. After the cancellation is successful, the taskID in task_id is deleted, and the taskID in cancel_id is also deleted.
[0072] 2. The contexts of GO and C communicate through shared memory:
[0073] The Go side and the C side communicate in context by sharing memory. First, the Go side applies for a piece of memory and saves the memory address in the context. When calling through CGO, the memory address is passed to the C side through parameters. After the Go side or the C side modifies the content of the memory, the other side can read it directly, thus realizing context communication between the two sides and ensuring that the two sides can pass messages in the same task. Figure 2 The figure shows a flow chart of the shared memory communication, in which the value of the memory address is set to determine whether the address space is released.
[0074] 2.1. Execute tasks directly on the C side through CGO calls:
[0075] When CGO executes a task, the GO side creates GOCtx (C.ctxCGO structure) and initializes the memory space. The address of GOCtx is passed to the C side through the cgo call. After receiving the GOCtx address, the C side records it in the context bound to the thread. When executing a task, a new interrupt checkpoint is added to determine whether the task needs to be canceled. If GOCtx.cancel == 1, the executing task is interrupted.
[0076] When a task is to be canceled, first cancel the task being executed on the Go side, and call CancelTask(ctxCGO*GOCtx) at the same time. This method directly sets *ctx.cancel=1. After the setting is successful, the task executed on the C side reads the value and ends the task at the interrupt checkpoint.
[0077] 2.2. Apply for threads in the thread pool through CGO and execute tasks:
[0078] The task execution process in the C end is as follows: First, the CGO thread applies for thread resources in the thread pool and then executes the task. A new interruption checkpoint is added during the task execution. At the same time, the address of GOCtx.cancel needs to be passed in as a parameter of the task. When the task detects that the content in the address has changed, the ongoing task is interrupted and the results and data are stored in the memory space for the GO end to receive. After the GO end context is used, the requested memory space needs to be actively released.
[0079] In Inspur's time series database KaiwuDB, the recorded data is passed to the C-end through the context of the GO end. The C-end reads the corresponding data to execute tasks. After the GO or C-end modifies the context, the other end can directly read the modified data. In this way, business communication between the GO and C-end is achieved to ensure fast communication of business on both ends.
[0080] The embodiment of the present invention further provides a KaiwuDB database, in which the contexts of the GO end and the C end communicate by passing parameters and sharing memory: the task ID or memory address is used to be stored in the context of the GO end and passed to the C end, and the C end completes the communication between the two ends by recording the task ID or modifying the memory address;
[0081] Among them, context communication includes the CGO thread directly executing the task scenario and obtaining the thread in the thread pool through CGO call, executing the task scenario through the thread, and canceling the task logic;
[0082] The context of the GO side and the C side is communicated. An interruption checkpoint is added to the task execution process to end the task being executed. When canceling the business, both the tasks being executed on the GO side and the tasks being executed on the C side can be canceled at the same time.
[0083] Among them, Go side: task processing logic implemented by Golang language;
[0084] C-side: Task processing logic implemented in C++ / C language;
[0085] CGO: CGO call refers to the processing logic of the GO side calling the C side;
[0086] Context: context in the Go-side business logic, which exists in the entire Go business processing process and can be abbreviated as ctx;
[0087] Thread pool: A mode of using thread resources by utilizing the idea of resource "pooling" to reduce resource consumption when threads are created, scheduled, and destroyed.
[0088] 1. The context of GO and C communicate by passing parameters:
[0089] The task executor first specifies the task ID, saves the task ID to ctx through ctx on the GO side, and then copies the task ID through CGO call and writes it to the Context on the C side;
[0090] cancel_id is the task id that needs to be canceled on the C side, and task_id is the thread in the thread pool and the task it executes on the C side. Figure 1 The following is the overall task processing flow, and two specific task cancellation scenarios are given:
[0091] 1.1. CGO thread directly executes tasks:
[0092] In this scenario, the GO side directly executes tasks on the C side through CGO. When the Go side calls and executes tasks through CGO, the detailed processing flow for canceling the task operation is as follows:
[0093] First, the GO side calls CancelTask(taskID, cancel), and ends the task being executed by the Go side by calling cancel(). At the same time, it calls CancleTaskID(taskID). This interface uses the CancleTaskID() method to pass the task ID to the C side through the CGO call. After the C side receives the task cancellation message, it adds the task number taskID to cancel_id.
[0094] A new interrupt check needs to be added to the task execution logic on the C side to determine whether the task needs to be canceled. The task ID is checked from cancel_id to see if it exists. If it does, the task is deleted from cancel_id, the executing task is terminated, and the result is returned.
[0095] 1.2. After obtaining the thread in the thread pool through CGO call, execute the task through the thread:
[0096] That is, after the Go side executes the task, it calls the C side’s task logic through CGO. The C side’s task logic execution process is as follows:
[0097] (1) Get the thread pool object on the C side, through which thread resources can be applied;
[0098] (2) After obtaining the thread in the thread pool, execute the task through the thread and return the threadID of the task;
[0099] (3) Save threadID and taskID in ctx to task_id;
[0100] (4) Subtask execution process: An interrupt check is added to the subtask processing logic to cancel the task after receiving an interrupt signal. The specific process is: when the task executes the interrupt check, it is necessary to first read the task number from cancel_id and find the thread corresponding to the task number from task_id. If the thread is the current thread, the task is terminated and the result is returned to the upper layer;
[0101] (5) After the subtask is completed or canceled, the task ID stored in task_id and cancel_id needs to be deleted to ensure the correctness of the subsequent task cancellation operation.
[0102] When the GO side cancels a task, the GO side calls CancelTask(taskID, cancel), cancels the task being executed by the GO side by calling cancel(), and calls CancleTaskID(taskID) at the same time. This interface uses the CancleTaskID() method to pass the task ID to the C side through the CGO side.
[0103] After receiving the task cancellation message, the C end first adds the taskID to cancel_id, and searches for the thread number corresponding to the task ID from task_id, and uses the thread number to terminate the executing thread task. After the cancellation is successful, the taskID in task_id is deleted, and the taskID in cancel_id is also deleted.
[0104] 2. The contexts of GO and C communicate through shared memory:
[0105] The Go side and the C side communicate in context by sharing memory. First, the Go side applies for a piece of memory and saves the memory address in the context. When calling through CGO, the memory address is passed to the C side through parameters. After the Go side or the C side modifies the content of the memory, the other side can read it directly, thus realizing context communication between the two sides and ensuring that the two sides can pass messages in the same task. Figure 2 The figure shows a flow chart of the shared memory communication, in which the value of the memory address is set to determine whether the address space is released.
[0106] 2.1. Execute tasks directly on the C side through CGO calls:
[0107] When CGO executes a task, the GO side creates GOCtx (C.ctxCGO structure) and initializes the memory space. The address of GOCtx is passed to the C side through the cgo call. After receiving the GOCtx address, the C side records it in the context bound to the thread. When executing a task, a new interrupt checkpoint is added to determine whether the task needs to be canceled. If GOCtx.cancel == 1, the executing task is interrupted.
[0108] When a task is to be canceled, first cancel the task being executed on the Go side, and call CancelTask(ctxCGO*GOCtx) at the same time. This method directly sets *ctx.cancel=1. After the setting is successful, the task executed on the C side reads the value and ends the task at the interrupt checkpoint.
[0109] 2.2. Apply for threads in the thread pool through CGO and execute tasks:
[0110] The task execution process in the C end is as follows: First, the CGO thread applies for thread resources in the thread pool and then executes the task. A new interruption checkpoint is added during the task execution. At the same time, the address of GOCtx.cancel needs to be passed in as a parameter of the task. When the task detects that the content in the address has changed, the ongoing task is interrupted and the results and data are stored in the memory space for the GO end to receive. After the GO end context is used, the requested memory space needs to be actively released.
[0111] In Inspur's time series database KaiwuDB, the recorded data is passed to the C-end through the context of the GO end. The C-end reads the corresponding data to execute tasks. After the GO or C-end modifies the context, the other end can directly read the modified data. In this way, business communication between the GO and C-end is achieved to ensure fast communication of business on both ends.
[0112] The embodiment of the present invention further provides a context communication device between a GO terminal and a C terminal, comprising: at least one memory and at least one processor;
[0113] The at least one memory is used to store a machine-readable program;
[0114] The at least one processor is used to call the machine-readable program to implement the context communication method between the GO end and the C end described in the above embodiment.
[0115] The embodiment of the present invention further provides a computer-readable medium, on which computer instructions are stored, and when the computer instructions are executed by the processor, the context communication method between the GO end and the C end described in the above embodiment is implemented. Specifically, a system or device equipped with a storage medium can be provided, on which a software program code for implementing the functions of any of the above embodiments is stored, and a computer (or CPU or MPU) of the system or device reads and executes the program code stored in the storage medium.
[0116] In this case, the program code itself read from the storage medium can realize the function of any one of the above-mentioned embodiments, and thus the program code and the storage medium storing the program code constitute a part of the present invention.
[0117] The storage medium embodiments for providing the program code include a floppy disk, a hard disk, a magneto-optical disk, an optical disk (such as CD-ROM, CD-R, CD-RW, DVD-ROM, DVD-RAM, DVD-RW, DVD+RW), a magnetic tape, a non-volatile memory card, and a ROM. Alternatively, the program code can be downloaded from a server computer by a communication network.
[0118] In addition, it should be clear that the functions of any of the above embodiments can be implemented not only by executing the program code read by the computer, but also by enabling an operating system operating on the computer to complete part or all of the actual operations based on instructions from the program code.
[0119] In addition, it can be understood that the program code read from the storage medium is written to a memory provided in an expansion board inserted into the computer or written to a memory provided in an expansion unit connected to the computer, and then based on the instructions of the program code, a CPU installed on the expansion board or the expansion unit is enabled to perform part or all of the actual operations, thereby realizing the functions of any of the above-mentioned embodiments.
[0120] The present invention is shown and described in detail above through the accompanying drawings and preferred embodiments. However, the present invention is not limited to these disclosed embodiments. Based on the above multiple embodiments, those skilled in the art can know that the code review methods in the above different embodiments can be combined to obtain more embodiments of the present invention, and these embodiments are also within the protection scope of the present invention.
Claims
1. A context communication method between a GO terminal and a C terminal, characterized in that: The contexts of the GO side and the C side communicate by passing parameters and sharing memory: the task ID or memory address is saved in the context of the GO side and passed to the C side. The C side completes the communication between the two sides by recording the task ID or modifying the memory address. The context communication includes the CGO thread directly executing the task scenario and obtaining the thread in the thread pool through the CGO call, executing the task scenario through the thread, and canceling the task logic; The context of the GO side and the C side is communicated. An interruption checkpoint is added to the task execution process to end the task being executed. When canceling the business, both the tasks being executed on the GO side and the tasks being executed on the C side can be canceled at the same time.
2. A context communication method between a GO terminal and a C terminal according to claim 1, characterized in that: The context of the GO end and the C end communicate by passing parameters: the task executor first specifies the task ID, saves the task ID to ctx through ctx on the GO end, and then copies the task ID through CGO call and writes it to the Context of the C end; cancel_id is the C-side used to record the task id that needs to be canceled, and task_id is the C-side used to record the threads in the thread pool and the tasks they execute.
3. A context communication method between a GO terminal and a C terminal according to claim 2, characterized in that: The CGO thread directly executes the task scenario, that is, the GO side directly executes the task on the C side through the CGO method; when the Go side calls and executes the task through CGO, the detailed processing flow for canceling the task operation is as follows: The GO side calls CancelTask(taskID, cancel) to end the task being executed by the GO side by calling cancel(). At the same time, it calls CancleTaskID(taskID). This interface uses the CancleTaskID() method to pass the task ID to the C side through the CGO call. After receiving the task cancellation message, the C side adds the task number taskID to cancel_id. A new interruption check is added to the task execution logic on the C side to determine whether the task needs to be canceled. The task ID is checked from cancel_id to see if it exists. If it does exist, the task is deleted from cancel_id, the executing task is terminated, and the result is returned.
4. A context communication method between a GO terminal and a C terminal according to claim 2, characterized in that: After obtaining the thread in the thread pool through CGO call, the scenario of executing the task through the thread, that is, after the Go side executes the task, the task logic of the C side is executed through CGO call. The task logic execution process of the C side is as follows: 1) Get the thread pool object on the C side, through which thread resources can be applied; 2) After obtaining the thread in the thread pool, execute the task through the thread and return the threadID of the task; 3) Save threadID and taskID in ctx to task_id; 4) Subtask execution process: Add interruption check in the subtask processing logic to cancel the task after receiving the interruption signal. The specific process is: when the task performs interruption check, it is necessary to first read the task number from cancel_id and find the thread corresponding to the task number from task_id. If the thread is the current thread, terminate the task and return the result to the upper layer; 5) After the subtask is completed or canceled, the task ID saved in task_id and cancel_id is deleted to ensure the correctness of the subsequent task cancellation operation; When the GO side cancels a task, the GO side calls CancelTask(taskID, cancel), cancels the task being executed by the GO side by calling cancel(), and calls CancleTaskID(taskID) at the same time. This interface uses the CancleTaskID() method to pass the task ID to the C side through the CGO side; after the C side receives the task cancellation message, it first adds the taskID to the cancel_id, and searches for the thread number corresponding to the task ID from the task_id, and uses the thread number to terminate the executing thread task. After the cancellation is successful, the taskID in the task_id is deleted, and the taskID in the cancel_id is also deleted.
5. A context communication method between a GO terminal and a C terminal according to claim 1, characterized in that: The context of the GO end and the C end communicate through shared memory. The GO end and the C end perform context communication through shared memory: first, the Go end applies for a piece of memory and saves the memory address in the context. When called through CGO, the memory address is passed to the C end through parameters; when the GO end or the C end modifies the content of the memory, the other end can read it directly, thereby realizing context communication between the two ends and ensuring that the two ends can transmit messages in the same task; wherein, whether the address space is released is determined by setting the value of the memory address.
6. A context communication method between a GO terminal and a C terminal according to claim 5, characterized in that: The way to execute tasks directly on the C side through CGO calls is as follows: When CGO executes a task, the GO side creates GOCtx and initializes the memory space. The address of GOCtx is passed to the C side through the cgo call. After receiving the GOCtx address, the C side records it in the context bound to the thread. When executing a task, a new interrupt checkpoint is added to determine whether the task needs to be canceled. If GOCtx.cancel == 1, the executing task is interrupted. When a task is to be canceled, first cancel the task being executed on the Go side, and call CancelTask(ctxCGO*GOCtx) at the same time, directly set *ctx.cancel=1. After the setting is successful, the task executed on the C side reads the value and ends the task at the interrupt checkpoint.
7. A context communication method between a GO terminal and a C terminal according to claim 5, characterized in that: After applying for a thread in the thread pool through CGO, the task is executed. The task execution process in the C end is as follows: After the CGO thread applies for thread resources in the thread pool, it executes the task, adds an interruption checkpoint during task execution, and passes the address of GOCtx.cancel as a parameter of the task. When the task detects that the content in the address has changed, it interrupts the ongoing task and stores the results and data in the memory space for the GO end to receive. After the context on the GO side is used up, it will automatically release the memory space it has applied for.
8. A KaiwuDB database, characterized in that: In the KaiwuDB database, the contexts of the GO side and the C side communicate by passing parameters and sharing memory: the task ID or memory address is saved in the context of the GO side and passed to the C side. The C side completes the communication between the two sides by recording the task ID or modifying the memory address. Among them, context communication includes the CGO thread directly executing the task scenario and obtaining the thread in the thread pool through CGO call, executing the task scenario through the thread, and canceling the task logic; The context of the GO side and the C side is communicated. An interruption checkpoint is added to the task execution process to end the task being executed. When canceling the business, both the tasks being executed on the GO side and the tasks being executed on the C side can be canceled at the same time.
9. A context communication device between a GO terminal and a C terminal, characterized in that: include: at least one memory and at least one processor; The at least one memory is used to store a machine-readable program; The at least one processor is used to call the machine-readable program to implement the method described in any one of claims 1 to 7.
10. A computer-readable medium, characterized in that The computer readable medium stores computer instructions, which, when executed by a processor, can implement the method according to any one of claims 1 to 7.