Distributed storage process plug-in based on Golang Plug-in plug-in mechanism and implementation method thereof
Through the Golang Plugin plug-in mechanism, the flexibility and performance improvement of distributed stored procedures are achieved, solving the problems of high coupling and performance loss in traditional stored process implementation methods, and providing separated stored process support with zero performance loss, suitable for distribution Multi-language stored procedure requirements for various scenarios.
Patent Information
- Application Number
- CN202410716223.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-06-04
- Publication Date
- 2024-09-03
AI Technical Summary
The traditional database stored procedure implementation has problems of poor flexibility and performance loss, especially in distributed scenarios. The traditional single-process design leads to a high coupling degree between the stored procedure and the database engine, making it difficult to support multiple languages and flexibly expand. Process communication brings The performance loss is also larger.
A distributed stored procedure plug-in based on the Golang Plugin plug-in mechanism is adopted. The creation request is received through the plug-in basic module, the process language module is called to perform language parsing, a syntax tree is generated, and the stored procedure is executed through the executor interface of the database main program to avoid inter-process communication. , a detached stored procedure that achieves zero performance loss.
It improves the flexibility and scalability of stored procedures, avoids performance losses caused by process communication, supports multiple programming languages, and improves the execution performance and scalability of stored procedures.
Smart Images

Figure CN118585265A
Abstract
Description
Technical Field
[0001] The present invention relates to a stored procedure implementation method, and more specifically, the present invention mainly relates to a distributed stored procedure plug-in based on a Golang Plugin plug-in mechanism and an implementation method thereof. Background Art
[0002] Stored Procedure is an important function in traditional relational database systems. It defines a SQL procedure programming language that provides functions including process control, exception / error handling, variable declaration and assignment, expression calculation and cursor. Traditional databases basically support stored procedures, such as Oracle's PL / SQL, Mysql's stored procedures and PostgreSQL's PL / pgSQL. Among them, Oracle's PL / SQL is the earliest and most popular stored procedure, allowing developers to create complex business logic in the database.
[0003] There are two main ways to implement stored procedures in traditional relational databases: integral and separate. The integral way means that the stored procedure is implemented inside the main program Server and exists as an integral process with the server. The call and execution of the stored procedure are implemented through the database engine, which has the advantages of simplicity and efficiency. This integral single-process design has the advantages of high concurrency, stability, and security. Although the integral implementation method is simple and efficient, it performs poorly in terms of flexibility and scalability. On this basis, separate stored procedures based on middleware have emerged, in which the stored procedure service exists as a separate process. The independent process can communicate with the database engine through the process to access the database. Under this architecture, the stored procedure engine and the database engine exist independently. They can run on different computers respectively. The stored procedure process can be dynamically started or shut down as needed, and will not affect the normal operation of the database. In terms of process communication, the stored procedure engine and the database engine usually communicate in shared memory, pipes, message queues, etc., which improves the scalability and flexibility of the system, and also reduces the dependence of the stored procedure on the database engine, thereby improving the stability of the system. With the advent of the big data era and the development of a new generation of databases, the requirements for the flexibility and performance of stored procedures are gradually increasing. The current distributed database stored procedures must meet the needs of different businesses and be flexible and customizable in terms of language and features. At the same time, they must be able to improve the performance of stored procedures based on distributed scenarios.
[0004] The two commonly used database stored procedure implementation methods have certain shortcomings: for example, the traditional single process implementation method: the stored procedure is implemented in the main program, and the main program and the stored procedure are developed and deployed as a whole. In this way, the stored procedure is highly coupled with the specific database engine. When the stored procedure adds new functions or makes changes, the main program needs to be redeveloped and compiled, which reduces the flexibility of the stored procedure. In addition, this method only supports one stored procedure language, which lacks customizability for the needs of multiple stored procedure languages. In order to improve flexibility, separated stored procedures have emerged. Among them, the stored procedure exists in the database as a separate process, which reduces the coupling between the stored procedure and the main program to a certain extent. However, the stored procedure has a strong dependence on the context and needs to transmit relevant information with the main program through process communication, including the type, value and process object of the variable, etc. Frequent process communication will bring more performance loss to the call and execution of the stored procedure, which greatly weakens the execution performance of the stored procedure. Summary of the invention
[0005] One of the purposes of the present invention is to address the above-mentioned shortcomings and provide a distributed stored procedure plug-in based on the Golang Plugin plug-in mechanism and its implementation method, in the hope of solving the technical problems that the existing database stored procedure implementation will reduce its flexibility and bring more performance losses.
[0006] In order to solve the above technical problems, the present invention adopts the following technical solutions: On the one hand, the present invention provides a stored procedure plug-in, including a plug-in basic module, which is used to receive a stored procedure creation request and call a process language module to perform language parsing on the current stored procedure; the process language module is used to send the language parsing result to the corresponding statement processing module; the statement processing module is used to generate a syntax tree from the language parsing result through the parsing module, and then start the executor, and call the executor interface of the database main program to execute the parsed stored procedure according to the generated syntax tree; the statement processing module is also used to feed back the execution result of the stored procedure to the plug-in basic module; the plug-in basic module is also used to update the metadata in the database according to the execution result.
[0007] On the other hand, the present invention also provides a distributed storage process implementation method based on the Golang Plugin plug-in mechanism, which is based on the above plug-in and includes the following steps: Step A: The plug-in basic module receives a request to create a stored procedure and calls the procedure language module to perform language analysis on the current stored procedure.
[0008] Step B: The process language module sends the language parsing result to the corresponding statement processing module.
[0009] Step C, the statement processing module generates a syntax tree from the language parsing result through the parsing module, then starts the executor, and calls the executor interface of the database main program to execute the parsed stored procedure according to the generated syntax tree; wherein the stored procedure statement is executed by the executor of the plug-in, and the SQL statement is executed by the main program.
[0010] Step D: The statement processing module feeds back the execution result of the stored procedure to the plug-in basic module, and the plug-in basic module updates the metadata in the database according to the execution result.
[0011] Preferably, a further technical solution is: the statement processing module is any one or more of a PL / SQL module, a Java module, a Python module and a Golang module.
[0012] A further technical solution is: before the statement processing module generates a syntax tree from the language parsing result through the parsing module, it first determines whether there is a syntax error in the current stored procedure. If the judgment result is yes, the error is fed back to the client. Otherwise, the language parsing result is generated into a syntax tree through the parsing module.
[0013] A further technical solution is: the parsed stored procedure is executed according to the generated syntax tree, and the logical plan and the physical plan are respectively generated and fed back to the plug-in basic module. The plug-in basic module stores the physical plan in the cache, and the plug-in basic module accesses the database system table to determine whether the same stored procedure already exists. If the judgment result is yes, the metadata of the current stored procedure is read and updated, otherwise the metadata of the current stored procedure is stored. The metadata of the stored procedure includes the name, parameters, and return value.
[0014] A further technical solution is: the plug-in basic module receives the stored procedure execution request and parses the stored procedure execution request, then queries the physical plan cache through the parsed procedure name, and sends the corresponding physical plan to the statement processing module; the statement processing module starts the executor and calls the executor interface of the database main program to execute the parsed stored procedure according to the generated syntax tree.
[0015] A further technical solution is: if the plug-in basic module fails to query the corresponding physical plan in the physical plan cache, then recreate the stored procedure corresponding to the current stored procedure execution request.
[0016] A further technical solution is that the stored procedure creation request and the stored procedure execution request are sent by the user to the main program of the database through the client protocol, and are sent by the executor of the main program to the loaded stored procedure plug-in.
[0017] A further technical solution is: a plug-in manager is provided in the main program of the database, the plug-in manager is connected to the stored procedure plug-in through a Plugin interface, and a statement processing module in the stored procedure plug-in is connected to the main program of the database through an executor interface.
[0018] Compared with the prior art, one of the beneficial effects of the present invention is that: by adopting a multi-level plug-in architecture to implement a separate stored procedure, a hierarchical structure in which the stored procedure interacts with the main program is constructed, and different plug-ins can be dynamically loaded and added according to needs, solving the problem of poor flexibility and scalability of the traditional stored procedure implementation method. Compared with the traditional separate stored procedure, the communication between processes is avoided, and the zero performance loss of the separate stored procedure can be achieved, thereby ensuring the execution performance of the stored procedure. BRIEF DESCRIPTION OF THE DRAWINGS
[0019] Figure 1 A basic schematic diagram for illustrating a plug-in mechanism in an embodiment of the present invention.
[0020] Figure 2 The present invention is used to illustrate the storage process architecture of an embodiment of the present invention.
[0021] Figure 3 The plug-in calling flow chart is used to illustrate an embodiment of the present invention.
[0022] Figure 4 A flow chart is created for a stored procedure used to illustrate one embodiment of the present invention.
[0023] Figure 5 The present invention is a flowchart of a stored procedure call for describing an embodiment of the present invention. DETAILED DESCRIPTION
[0024] Golang (also called Go) mentioned in the present invention is an efficient, reliable and easy-to-maintain programming language that provides two mechanisms, goroutine and channel, and also supports plug-in mechanism. It has good concurrency and scalability and is very suitable for the development of high-concurrency and distributed systems. Go Context: context.Context is an interface introduced by Go to the standard library. It is a structure used to synchronize signals and pass request-related values. In the present invention, it can be used to pass information across goroutines. Compared with inter-process communication, the overhead of this communication method is much smaller, which provides good support for the fast interactive communication between the stored procedure and the main program under the plug-in mechanism.
[0025] Plugin mechanism is a way to achieve system extensibility in dynamic systems. For some large projects such as databases, combining all functions together increases the coupling of modules, and modifying one module will also affect other modules. The plugin mechanism can decouple database modules and easily expand existing program functions. The application provides the corresponding interface to be implemented by a third party, and builds the corresponding dynamic link library for dynamic loading.
[0026] The main idea of the present invention is to first add a plug-in mechanism to the database engine, allowing users to write custom stored procedures and load them into the distributed database as plug-ins. Secondly, the present invention adopts the Golang built-in Plugin plug-in mechanism as the implementation method of the stored procedure plug-in, which is divided into four levels to realize the creation and calling of stored procedures supporting multiple programming languages.
[0027] The plug-in mechanism is a software design pattern that allows users to extend the functionality of a system by writing plug-ins without having to modify the system's source code. As an independent module, a plug-in can be dynamically loaded and unloaded when the system is running, thus achieving dynamic expansion. Its general composition includes the following parts: 1. Plug-in interface: defines the interface between the plug-in and the system, including input and output parameters, calling rules, and return values.
[0028] 2. Plugin Manager: responsible for loading, unloading, initializing, etc. of plugins 3. Plug-in implementation: implement specific plug-in functions, usually including plug-in input and output parameters, algorithm implementation, etc.
[0029] The plug-in provides a plug-in interface for the main program to call. The main program provides a service interface for the plug-in to call the module service of the main program. Its basic structure is as follows: Figure 1 shown.
[0030] This invention uses the native Plugin plug-in of Golang as the plug-in implementation mechanism. The overall implementation architecture includes five modules. Figure 2 shown.
[0031] 1. SQL SERVER: the database engine used to execute SQL statements and implement the plug-in mechanism, including opening dynamic link libraries and loading and calling stored procedure functions.
[0032] 2. Plugin Manager: Plugin Manager manages stored procedure plug-ins in various programming languages, including loading, unloading, and deleting operations.
[0033] 3. SP infrastructure: The plug-in basic module of the stored procedure is used to parse and respond to stored procedure requests from the client, including the creation and calling of stored procedures.
[0034] 4. Language module: The procedural language module is used to parse the corresponding language of the stored procedure and forward the procedure to the corresponding statement processing module.
[0035] 5. SP Executor: includes stored procedure statement processing modules in multiple languages, which is used to parse the procedure syntax and complete the execution logic.
[0036] Depend on Figure 2 It can be seen that in one embodiment of the present invention, a distributed storage process implementation method based on the Golang Plugin plug-in mechanism includes the following steps: S1. The database receives the user's stored procedure request through the client. The stored procedure is sent to the SP Infrastructure through the plug-in mechanism and processed by the stored procedure infrastructure module.
[0037] Among them, the stored procedure infrastructure module is the control layer that processes the stored procedure from the client. It checks whether the stored procedure exists by accessing the database metadata and stores the newly created stored procedure in the metadata; it interacts with the language module and the statement processing module to obtain the execution results of the SP statement or update the process metadata.
[0038] S2. After the stored procedure completes the SP Infrastructure basic analysis, it is sent to the corresponding procedure statement processing module through the Language module. This module is the core execution module of the stored procedure, which completes the analysis and execution of the procedure statements, interacts with the infrastructure module, implements the storage of procedure metadata, and interacts with the SQL SERVER interface to implement the analysis and execution of SQL statements in the process.
[0039] The statement processing module has its own processing logic for stored procedures in different languages, which mainly includes a parsing module and an executor. First, the SP code is parsed in a specific language to generate a syntax tree. Then, the executor is started to execute the parsed stored procedure according to the generated syntax tree. The SP procedure statement is executed by the executor of this module. For SQL statements, the executor interface provided by the main program is called to execute the SQL statement.
[0040] In this embodiment, the stored procedure (SP) is a distributed stored procedure of a plug-in mechanism. The stored procedure exists in the form of an independent plug-in. On the basis of a distributed database, it implements stored procedure functions such as variable declaration and assignment, process control, exception handling, and cursors. The plug-in includes multiple different process processing modules such as PL / SQL, Java, and Python, which implements multi-language support for stored procedures and implements a separate stored procedure with zero performance loss based on the plug-in mechanism, solving the problem of performance loss of separate stored procedures in traditional process communication. The present invention is described in detail below based on the specific modules of the stored procedure (SP) and the creation and calling process of the SP.
[0041] First, if Figure 3 As shown in the figure, the plug-in mechanism is implemented in the database main program, that is, it can load and call SP, where SP is a stored procedure plug-in, which completes the entire stored procedure function from bottom to top through four modules. The Golang Plugin plug-in mechanism mainly includes the opening of the SP dynamic link library plugin.Open and the calling of the function in the plug-in plugin.Lookup.
[0042] On this basis, the SP creation process is as follows Figure 4 As shown, the user's request is sent to the database main program through the client protocol. The executor of the main program sends the stored procedure request to the already loaded SP plug-in. The SPInfrastructure module first processes the process request: When executing the stored procedure DDL, the SPInfrastructure module provides processing functions such as CreateSP and DropSP. Call the Language module to perform language parsing on the stored procedure DDL, and determine whether the process language processing plug-in has been loaded based on the returned result. If the PL / SQL language is used here, the DDL statement is sent to the statement processing module SP SQLServer corresponding to PL / SQL. If the process uses the Java language, then the DDL is sent to the SP JavaServe to implement parsing and generation.
[0043] Taking SP SQLServer as an example, it includes two parts: Generator and Executor. Generator is responsible for lexical and grammatical analysis of the stored procedure code of the language to determine whether there is a grammatical error. If there is an error, it will be reported to the user, otherwise a syntax tree will be generated. Then the logical plan and physical plan are generated according to the syntax tree. At this stage, SP SQLSever sends the parsed source code to SP Infrastructure, and the basic module accesses the database system table to read the stored procedure metadata, determine whether the same stored procedure already exists, and whether to add or update metadata information, including stored procedure name, parameters, return value and other information. At the same time, the physical plan tree generated in this step is stored on the server through SP Infrastructure for subsequent cache search and execution.
[0044] The stored procedure call is as follows Figure 5 As shown, the SP Infrastructure module provides the CallSP processing function. When the client requests to execute a stored procedure, the SP Infrastructure performs lexical and grammatical analysis on the statement to determine whether there are any grammatical errors, and then queries the physical plan cache through the parsed procedure name. When the procedure name is consistent with the parameters, the physical plan tree is returned and handed over to the Executor for execution. If not found, the system table is retrieved to obtain the stored procedure metadata. The queried metadata is sent to the SP SQLServer processing module according to the language module to complete the generation and execution. It also includes lexical and grammatical analysis to generate an AST syntax tree, and generates a logical plan and a physical plan based on the syntax tree. The DML statements of the stored procedure are executed by the SP SQLServer module, and DML statements such as insert and update are queried. The executor is accessed through the execution interface provided by the main program engine, and the results are returned to the SP processing module. The main program is based on the applicant's self-developed distributed database QianBase, which belongs to the prior art and will not be repeated here.
[0045] It can be seen from the above embodiments that the plug-in stored procedure implemented by the present invention is implemented from the bottom up according to the function and accessibility, including a statement processing module, a language module, a stored procedure basic module and a plug-in manager, wherein the plug-in manager is located in the main program, manages the process processing modules of different languages, and ensures the compatibility of the stored procedure plug-in with the main program. When the system starts, the plug-in manager dynamically loads stored procedure plug-ins of multiple languages and different functions by reading the plug-in list in the system configuration file, which greatly improves the flexibility and scalability of the stored procedure. In addition, unlike the traditional separated stored procedure for process communication, the plug-in mechanism has the advantage of being native and can be directly called within the same process, eliminating the overhead of inter-process communication and achieving zero performance loss. In addition, the Golang context package is used as the carrier for context transfer, which facilitates the context interaction between the stored procedure SP and the main program, and has lower resource consumption.
[0046] In addition to the above, it should be noted that "one embodiment", "another embodiment", "embodiment", etc. mentioned in this specification refer to the specific features, structures or characteristics described in conjunction with the embodiment included in at least one embodiment generally described in this application. The same expression appearing in multiple places in the specification does not necessarily refer to the same embodiment. Further, when describing a specific feature, structure or characteristic in conjunction with any embodiment, it is claimed that the realization of such feature, structure or characteristic in conjunction with other embodiments also falls within the scope of the present invention.
[0047] Although the present invention is described herein with reference to a number of illustrative embodiments of the present invention, it will be appreciated that those skilled in the art may devise many other modifications and implementations that fall within the scope and spirit of the principles disclosed herein. More specifically, within the scope of the present disclosure, drawings, and claims, a variety of variations and improvements may be made to the components and / or layout of the subject combination layout. In addition to variations and improvements made to the components and / or layout, other uses will also be apparent to those skilled in the art.
Claims
1. A distributed storage process implementation method based on the Golang Plugin plug-in mechanism, characterized in that The method comprises the following steps: The plug-in basic module receives the request to create a stored procedure and calls the procedure language module to perform language parsing on the current stored procedure; The procedural language module sends the language parsing results to the corresponding statement processing module; The statement processing module generates a syntax tree from the language parsing result through the parsing module, then starts the executor, and calls the executor interface of the database main program to execute the parsed stored procedure according to the generated syntax tree; wherein the stored procedure statement is executed by the executor of the plug-in, and the SQL statement is executed by the main program; The statement processing module feeds back the execution result of the stored procedure to the plug-in basic module, and the plug-in basic module updates the metadata in the database according to the execution result.
2. The distributed storage process implementation method based on the Golang Plugin plug-in mechanism according to claim 1 is characterized in that: The statement processing module is any one or more of a PL / SQL module, a Java module, a Python module and a Golang module.
3. The distributed storage process implementation method based on the Golang Plugin plug-in mechanism according to claim 1 is characterized in that: Before the statement processing module generates a syntax tree from the language parsing result through the parsing module, it first determines whether there is a syntax error in the current stored procedure. If the judgment result is yes, the error is fed back to the client. Otherwise, the language parsing result is generated into a syntax tree through the parsing module.
4. The distributed storage process implementation method based on the Golang Plugin plug-in mechanism according to claim 1 is characterized in that: The parsed stored procedure is executed according to the generated syntax tree, and the logical plan and the physical plan are respectively generated and fed back to the plug-in basic module. The plug-in basic module stores the physical plan in the cache, and the plug-in basic module accesses the database system table to determine whether the same stored procedure already exists. If the judgment result is yes, the metadata of the current stored procedure is read and updated, otherwise the metadata of the current stored procedure is stored, and the metadata of the stored procedure includes the name, parameters, and return value.
5. The distributed storage process implementation method based on the Golang Plugin plug-in mechanism according to claim 1 or 4, characterized in that The method further comprises: The plug-in basic module receives the stored procedure execution request, parses the stored procedure execution request, queries the physical plan cache by the parsed procedure name, and sends the corresponding physical plan to the statement processing module; The statement processing module starts the executor and calls the executor interface of the database main program to execute the parsed stored procedure according to the generated syntax tree.
6. The distributed storage process implementation method based on the Golang Plugin plug-in mechanism according to claim 5 is characterized in that If the plug-in base module fails to query the corresponding physical plan in the physical plan cache, it recreates the stored procedure corresponding to the current stored procedure execution request.
7. The distributed storage process implementation method based on the Golang Plugin plug-in mechanism according to claim 1 or 6 is characterized in that: The stored procedure creation request and the stored procedure execution request are sent by the user to the main program of the database through the client protocol, and are sent by the executor of the main program to the loaded stored procedure plug-in.
8. The distributed storage process implementation method based on the Golang Plugin plug-in mechanism according to claim 7 is characterized in that: The main program of the database has a plug-in manager, the plug-in manager is connected to the stored procedure plug-in through the Plugin interface, and the statement processing module in the stored procedure plug-in is connected to the main program of the database through the executor interface.
9. A stored procedure plug-in, characterized in that: include: The plug-in basic module is used to receive the request to create a stored procedure and call the procedure language module to perform language parsing on the current stored procedure; The procedural language module is used to send the language parsing results to the corresponding statement processing module; A statement processing module is used to generate a syntax tree from the language parsing result through the parsing module, then start the executor, and call the executor interface of the database main program to execute the parsed stored procedure according to the generated syntax tree; The statement processing module is also used to feed back the execution result of the stored procedure to the plug-in basic module; The plug-in basic module is also used to update metadata in the database according to the execution result.
10. The stored procedure plug-in according to claim 9, characterized in that: The plug-in is used to execute the method according to any one of claims 1 to 8.
