A tag-based method, device, and storage medium for database read-write separation control

By passing the database tags through the RPC parameters and combining the call stack and function code identification, fine-grained database read and write separation control is achieved, solving the problem of inability to flexibly specify database access in the existing technology, and improving system efficiency and flexibility.

CN116340019BActive Publication Date: 2025-08-05INTELLIGENT INTERNET (XIAMEN) HEALTH TECH CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202310143268.X
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2023-02-21
Publication Date
2025-08-05
Estimated Expiration
2043-02-21

AI Technical Summary

Technical Problem

The existing database read and write separation middleware cannot perform fine-grained read and write separation control for specific services, and R&D personnel cannot specify database access quickly and flexibly, and cannot meet business needs.

Method used

By passing the database tags through the parameters of RPC, defining the database access section, and saving the database call stack in the RPC call stack, using AOP facets to obtain the database tags, implementing fine-grained database access control, and matching them with the access identifiers in the function code to determine the access link.

Benefits of technology

It realizes fine-grained read and write separation control of the database, improves system efficiency and flexibility, and can accurately control the number of database accesses according to business needs.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN116340019B_ABST
    Figure CN116340019B_ABST
Patent Text Reader

Abstract

The present invention proposes a tag-based database read-write separation control method, device, and storage medium. The method includes: adding transparent data to RPC parameters, the transparent data including multiple database tags accessed by multiple functions called by the RPC; defining a database access aspect based on the multiple database tags, wherein each database tag corresponds to a database access aspect; saving a database call stack in a thread-local variable of the RPC call stack, the database call stack being used to store the multiple database tags; determining the database tag to be accessed based on the RPC called function, loading the corresponding database tag from the call stack, obtaining an access link for the database access aspect based on the database tag, and sending the access link to the RPC called function; and the RPC called function accessing the corresponding database based on the access link. This improves the flexibility of database access.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of database access control, and in particular to a tag-based database read-write separation control method, device and storage medium. Background Art

[0002] Database read-write separation refers to the situation in which, in systems with complex business logic across product lines, different interfaces, microservices, and gateways may have inconsistent database access requirements for accessing system business data. Some businesses requiring strong consistency require access to the master database for core data, while other businesses requiring less strong consistency require access to a backup database to alleviate pressure on the master database.

[0003] Currently, the market generally uses middleware to separate the read and write of master and slave databases. Business code directly accesses the middleware, and the middleware configuration automatically determines whether to access the master or slave database. This has the following drawbacks:

[0004] 1. It is impossible to perform read-write separation control for specific business and specific method calls;

[0005] 2. It is impossible to perform fine-grained read-write separation control on the entire call chain based on microservices;

[0006] 3. R&D personnel cannot specify the database they need to access more quickly, conveniently, and flexibly in the code.

[0007] In short, although the existing database read-write separation middleware on the market is excellent, it cannot well meet the growing demand for database master-slave read-write separation in our business. We need a database read-write separation control component that is more convenient, more fine-grained, and more in line with business needs. Summary of the Invention

[0008] The present invention aims to solve one or more technical deficiencies in the above-mentioned prior art and proposes the following technical solutions.

[0009] A tag-based database read-write separation control method, the method comprising:

[0010] A transparent transmission step of adding transparent transmission data to the RPC parameters, wherein the transparent transmission data includes multiple database tags accessed by multiple functions called by the RPC, and defining database access aspects based on the multiple database tags, wherein each database tag corresponds to a database access aspect;

[0011] A setting step of saving a database call stack in a thread local variable of the RPC call stack, wherein the database call stack is used to save the multiple database tags;

[0012] An AOP aspect acquisition step is performed to determine a database tag to be accessed based on the function called by the RPC, load the corresponding database tag from the call stack, obtain an access link of the database access aspect based on the database tag, and send the access link to the function called by the RPC;

[0013] In the access step, the function called by the RPC accesses the corresponding database based on the access link.

[0014] Furthermore, the plurality of database tags include at least read-only, master database and slave database.

[0015] Furthermore, the operation of determining the database tag that needs to be accessed based on the RPC call function includes: obtaining the function called by the RPC, parsing the code of the function to obtain a parsed code, and determining whether there is an access identifier that controls database access in the parsed code; if so, matching the access identifier in the code with the multiple database tags saved in the call stack; if the match is successful, using the successfully matched database tag as the database tag that needs to be accessed.

[0016] Furthermore, the method further includes: a cleaning step, after the function called by the RPC completes accessing the corresponding database, cleaning the data of the access link of the database in the memory.

[0017] Furthermore, when cleaning the data of the access link, it is determined whether the function called by the RPC subsequently accesses the database access aspect. If so, the data of the access link is retained; otherwise, the data of the access link is deleted.

[0018] The present invention also proposes a database read-write separation control device based on tags, which includes:

[0019] A transparent transmission unit, which adds transparent transmission data to the parameters of the RPC, wherein the transparent transmission data includes multiple database tags accessed by multiple functions called by the RPC, and defines a database access aspect based on the multiple database tags, wherein each database tag corresponds to a database access aspect;

[0020] A setting unit, configured to save a database call stack in a thread local variable of the RPC call stack, wherein the database call stack is used to save the multiple database tags;

[0021] An AOP aspect acquisition unit determines a database tag that needs to be accessed based on the function called by the RPC, loads the corresponding database tag from the call stack, obtains an access link of the database access aspect based on the database tag, and sends the access link to the function called by the RPC;

[0022] The access unit accesses the corresponding database based on the access link.

[0023] Furthermore, the plurality of database tags include at least read-only, master database and slave database.

[0024] Furthermore, the operation of determining the database tag that needs to be accessed based on the RPC call function includes: obtaining the function called by the RPC, parsing the code of the function to obtain a parsed code, and determining whether there is an access identifier that controls database access in the parsed code; if so, matching the access identifier in the code with the multiple database tags saved in the call stack; if the match is successful, using the successfully matched database tag as the database tag that needs to be accessed.

[0025] Furthermore, the device further includes: a cleaning unit, which cleans up the data of the access link of the database in the memory after the function called by the RPC completes accessing the corresponding database.

[0026] Furthermore, when cleaning the data of the access link, it is determined whether the function called by the RPC subsequently accesses the database access aspect. If so, the data of the access link is retained; otherwise, the data of the access link is deleted.

[0027] The present invention further provides a computer-readable storage medium, wherein the storage medium stores computer program code, and when the computer program code is executed by a computer, any one of the above methods is executed.

[0028] The technical effect of the present invention is: a tag-based database read-write separation control method, device and storage medium of the present invention, the method comprising: a transparent transmission step, adding transparent transmission data to the parameters of RPC, the transparent transmission data including multiple database tags accessed by multiple functions called by RPC, defining a database access aspect according to the multiple database tags, wherein each database tag corresponds to a database access aspect; a setting step, saving a database call stack in a thread local variable of the RPC call stack, the database call stack being used to save the multiple database tags; an AOP aspect acquisition step, determining the database tag to be accessed based on the function called by RPC, loading the corresponding database tag from the call stack, acquiring an access link of the database access aspect based on the database tag, and sending the access link to the function called by the RPC; an access step, wherein the function called by the RPC accesses the corresponding database based on the access link. In the present invention, multiple database tags accessed by multiple functions called by RPC are set in the transparent data, the transparent data is added to the parameters of RPC, the database tags are passed in the calling process through the transparent data, and then the database access aspect is defined according to the multiple database tags, each database tag corresponds to a database access aspect, that is, the reading and writing of the database are divided into two access aspects, and then the database call stack is saved in the thread local variable of the RPC call stack, the database call stack is used to save the multiple database tags, the database tag to be accessed is determined based on the function called by RPC, the corresponding database tag is loaded from the call stack, the access link of the database access aspect is obtained based on the database tag, the access link is sent to the function called by RPC, and then the corresponding database is accessed, thereby realizing the separation of reading and writing of the database. By adding access tags, Once the labeling is done, it can be used subsequently. In any call link node, a function of a microservice node is specified by a label to access a special master-slave database, which is easy to implement and can achieve fine-grained database access control. In the present invention, an access identifier for database access is added to the function code, that is, the database that the function needs to access is identified in the function code, and the parsed access identifier is matched with the multiple database labels saved in the call stack. If the match is successful, the database label that successfully matches is used as the database label that needs to be accessed, thereby achieving accurate control of the database access aspect and realizing read-write separation of the database. In the present invention, the number of calls of each function can be determined through the call stack, and based on the number of calls, it can be determined whether the function called by the RPC will subsequently access the database access aspect. If so, it is retained, otherwise, it is deleted.Since the number of accesses is set, the number of function calls can be accurately controlled, and the number of database accesses can be determined, thereby achieving precise control of data access and improving system efficiency. BRIEF DESCRIPTION OF THE DRAWINGS

[0029] Other features, objects and advantages of the present application will become more apparent from the detailed description of non-limiting embodiments made with reference to the following drawings.

[0030] Figure 1 The figure is a flowchart of a tag-based database read-write separation control method according to an embodiment of the present invention.

[0031] Figure 2 4 is a structural diagram of a database read-write separation control device based on tags according to an embodiment of the present invention. DETAILED DESCRIPTION

[0032] The present application will be further described in detail below with reference to the accompanying drawings and examples. It should be understood that the specific embodiments described herein are merely for the purpose of explaining the relevant invention and are not intended to limit the invention. It should also be noted that, for ease of description, only portions relevant to the relevant invention are shown in the accompanying drawings.

[0033] It should be noted that, in the absence of conflict, the embodiments and features of the embodiments in this application can be combined with each other. The present application will be described in detail below with reference to the accompanying drawings and in combination with the embodiments.

[0034] Figure 1 A tag-based database read-write separation control method of the present invention is shown, and the method includes:

[0035] In the transparent transmission step S101, the transparent transmission data is added to the parameters of the RPC. The transparent transmission data includes multiple database tags accessed by multiple functions called by the RPC. Database access aspects are defined based on the multiple database tags, where each database tag corresponds to a database access aspect. In the present invention, a program (such as a microservice program) can call multiple functions when executing RPC, for example, calling multiple functions according to the execution process of the program. These functions may need to access corresponding databases during execution, such as a master database, a slave database, a read-only database, etc.

[0036] In step S102, a database call stack is saved in the thread local variable of the RPC call stack, where the database call stack is used to save the multiple database tags. When saving the multiple database tags, the function and the database tag of the database to be called are saved in correspondence to facilitate subsequent search.

[0037] AOP aspect acquisition step S103, determining the database tag that needs to be accessed based on the function called by the RPC, loading the corresponding database tag from the call stack, obtaining an access link of the database access aspect based on the database tag, and sending the access link to the function called by the RPC;

[0038] In step S104 , the function called by the RPC accesses the corresponding database based on the access link.

[0039] In the present invention, multiple database tags accessed by multiple functions called by RPC are set in transparent data, and the transparent data is added to the parameters of the RPC. The database tags are passed during the call process through the transparent data. Then, database access aspects are defined based on the multiple database tags. Each database tag corresponds to a database access aspect, that is, database reading and writing are divided into two access aspects. Then, the database call stack is saved in the thread local variable of the RPC call stack. The database call stack is used to save the multiple database tags. The database tag to be accessed is determined based on the function called by RPC, the corresponding database tag is loaded from the call stack, and an access link of the database access aspect is obtained based on the database tag. The access link is sent to the function called by the RPC, and then the corresponding database is accessed, thereby achieving database read and write separation. By adding access tags, labeling is achieved once and can be used subsequently. In any call link node, a function of a microservice node is specified by the tag to access a special master-slave database. This is easy to implement and can achieve fine-grained database access control. This is an important inventive feature of the present invention.

[0040] In a further embodiment, the multiple database tags include at least read-only, master database, and slave database. For example, a read-only database is tagged with readonly, a master database is tagged with master, and a slave database is tagged with slave. In a distributed database, there may be a large amount of data, and each database is assigned a corresponding tag to facilitate subsequent access control.

[0041] In a further embodiment, the operation of determining the database tag that needs to be accessed based on the RPC called function includes: obtaining the function called by the RPC, parsing the code of the function to obtain a parsed code, and determining whether there is an access identifier that controls database access in the parsed code; if so, matching the access identifier in the code with the multiple database tags saved in the call stack; if the match is successful, using the successfully matched database tag as the database tag that needs to be accessed.

[0042] In the present invention, an access identifier for database access is added to the function code, that is, the database that the function needs to access is identified in the function code, and the parsed access identifier is matched with the multiple database tags saved in the call stack. If the match is successful, the matched database tag is used as the database tag that needs to be accessed, thereby achieving accurate control of the database access aspect and realizing the read-write separation of the database. This is another important invention point of the present invention.

[0043] In a further embodiment, the method further includes a cleaning step (S105) of cleaning the database access link data in memory after the function called by the RPC completes accessing the corresponding database. During the cleaning of the access link data, it is determined whether the function called by the RPC subsequently accesses the database access aspect. If so, the access link data is retained; otherwise, the access link data is deleted.

[0044] In the present invention, the call stack can be used to determine the number of times each function has been called. Based on this number of calls, it can be determined whether the function called by the RPC will subsequently access the database access aspect. If so, it is retained; otherwise, it is deleted. Because the access count is set, the number of function calls can be precisely controlled, and thus the number of database accesses can be determined, achieving precise control of data access and improving system efficiency, which is a key feature of the present invention.

[0045] Figure 2 The present invention shows a database read-write separation control device based on tags, which includes:

[0046] The transparent transmission unit 201 adds the transparent transmission data to the parameters of the RPC, wherein the transparent transmission data includes multiple database tags accessed by multiple functions called by the RPC, and defines a database access aspect based on the multiple database tags, wherein each database tag corresponds to a database access aspect; in the present invention, a program (such as a microservice program) can call multiple functions when executing the RPC, for example, calling multiple functions according to the execution process of the program, and these functions may need to access corresponding databases during the execution process, such as a master database, a slave database, a read-only database, etc.

[0047] The setting unit 202 saves a database call stack in the thread local variable of the RPC call stack, and the database call stack is used to save the multiple database tags; when saving the multiple database tags, the function and the database tag of the database it needs to call are saved in correspondence to facilitate subsequent search.

[0048] The AOP aspect acquisition unit 203 determines the database tag that needs to be accessed based on the function called by the RPC, loads the corresponding database tag from the call stack, obtains an access link of the database access aspect based on the database tag, and sends the access link to the function called by the RPC;

[0049] Access unit 204, the function called by the RPC accesses the corresponding database based on the access link.

[0050] In the present invention, multiple database tags accessed by multiple functions called by RPC are set in transparent data, and the transparent data is added to the parameters of the RPC. The database tags are passed during the call process through the transparent data. Then, database access aspects are defined based on the multiple database tags. Each database tag corresponds to a database access aspect, that is, database reading and writing are divided into two access aspects. Then, the database call stack is saved in the thread local variable of the RPC call stack. The database call stack is used to save the multiple database tags. The database tag to be accessed is determined based on the function called by RPC, the corresponding database tag is loaded from the call stack, and an access link of the database access aspect is obtained based on the database tag. The access link is sent to the function called by the RPC, and then the corresponding database is accessed, thereby achieving database read and write separation. By adding access tags, labeling is achieved once and can be used subsequently. In any call link node, a function of a microservice node is specified by the tag to access a special master-slave database. This is easy to implement and can achieve fine-grained database access control. This is an important inventive feature of the present invention.

[0051] In a further embodiment, the multiple database tags include at least read-only, master database, and slave database. For example, a read-only database is tagged with readonly, a master database is tagged with master, and a slave database is tagged with slave. In a distributed database, there may be a large amount of data, and each database is assigned a corresponding tag to facilitate subsequent access control.

[0052] In a further embodiment, the operation of determining the database tag that needs to be accessed based on the RPC called function includes: obtaining the function called by the RPC, parsing the code of the function to obtain a parsed code, and determining whether there is an access identifier that controls database access in the parsed code; if so, matching the access identifier in the code with the multiple database tags saved in the call stack; if the match is successful, using the successfully matched database tag as the database tag that needs to be accessed.

[0053] In the present invention, an access identifier for database access is added to the function code, that is, the database that the function needs to access is identified in the function code, and the parsed access identifier is matched with the multiple database tags saved in the call stack. If the match is successful, the matched database tag is used as the database tag that needs to be accessed, thereby achieving accurate control of the database access aspect and realizing the read-write separation of the database. This is another important invention point of the present invention.

[0054] In a further embodiment, the method further includes: a cleaning unit 205 that cleans up the database access link data in memory after the function called by the RPC completes accessing the corresponding database. When cleaning up the access link data, it is determined whether the function called by the RPC subsequently accesses the database access aspect. If so, the access link data is retained; otherwise, the access link data is deleted.

[0055] In the present invention, the call stack can be used to determine the number of times each function has been called. Based on this number of calls, it can be determined whether the function called by the RPC will subsequently access the database access aspect. If so, it is retained; otherwise, it is deleted. Because the access count is set, the number of function calls can be precisely controlled, and thus the number of database accesses can be determined, achieving precise control of data access and improving system efficiency, which is a key feature of the present invention.

[0056] For the convenience of description, the above device is described as being divided into various units according to their functions. Of course, when implementing the present application, the functions of each unit can be implemented in the same or multiple software and / or hardware.

[0057] Through the description of the above embodiments, it can be seen that those skilled in the art can clearly understand that the present application can be implemented by means of software plus the necessary general hardware platform. Based on this understanding, the technical solution of the present application, or the part that contributes to the prior art, can be embodied in the form of a software product. The computer software product can be stored in a storage medium, such as ROM / RAM, a magnetic disk, an optical disk, etc., and includes a number of instructions for enabling a computer device (which can be a personal computer, a server, or a network device, etc.) to execute the devices described in various embodiments of the present application or certain parts of the embodiments.

[0058] Finally, it should be noted that the above embodiments are only intended to illustrate rather than limit the technical solutions of the present invention. Although the present invention has been described in detail with reference to the above embodiments, those skilled in the art should understand that the present invention can still be modified or replaced by equivalents. Any modification or partial replacement that does not depart from the spirit and scope of the present invention should be included in the scope of the claims of the present invention.

Claims

1. A tag-based database read-write separation control method, characterized in that: The method includes: A transparent transmission step, wherein transparent transmission data is added to the parameters of the RPC, wherein the transparent transmission data includes multiple database tags accessed by multiple functions called by the RPC, and database access aspects are defined based on the multiple database tags, wherein each database tag corresponds to a database access aspect; the multiple database tags include at least read-only, master database, and slave database; A setting step of saving a database call stack in a thread local variable of the RPC call stack, wherein the database call stack is used to save the multiple database tags; An AOP aspect acquisition step is to acquire a function called by an RPC, parse the code of the function to obtain a parsed code, determine whether there is an access identifier for controlling database access in the parsed code, and if so, match the access identifier in the code with the multiple database tags stored in the database call stack. If a match is successful, use the successfully matched database tag as the database tag to be accessed, acquire an access link of the database access aspect based on the database tag, and send the access link to the function called by the RPC; In the access step, the function called by the RPC accesses the corresponding database based on the access link.

2. The method according to claim 1, characterized in that The method further includes a cleaning step of cleaning the data of the access link of the database in the memory after the function called by the RPC completes accessing the corresponding database.

3. The method according to claim 2, characterized in that When cleaning the data of the access link, it is determined whether the function called by the RPC subsequently accesses the database access aspect. If so, the data of the access link is retained; otherwise, the data of the access link is deleted.

4. A database read-write separation control device based on tags, characterized in that: The device includes: A transparent transmission unit, which adds transparent transmission data to the parameters of the RPC, wherein the transparent transmission data includes multiple database tags accessed by multiple functions called by the RPC, and defines a database access aspect based on the multiple database tags, wherein each database tag corresponds to a database access aspect; A setting unit is configured to save a database call stack in a thread local variable of the RPC call stack, wherein the database call stack is configured to save the plurality of database tags, wherein the plurality of database tags include at least read-only, master database, and slave database; An AOP aspect acquisition unit acquires a function called by an RPC, parses the code of the function to obtain a parsed code, determines whether the parsed code contains an access identifier for controlling database access, and if so, matches the access identifier in the code with the multiple database tags stored in the database call stack. If a match is successful, the matched database tag is used as the database tag to be accessed, acquires an access link of a database access aspect based on the database tag, and sends the access link to the function called by the RPC; The access unit accesses the corresponding database based on the access link.

5. The device according to claim 4, characterized in that The device further includes: a cleaning unit, which cleans up the data of the access link of the database in the memory after the function called by the RPC completes accessing the corresponding database.

6. A computer-readable storage medium having computer program code stored thereon, wherein when the computer program code is executed by a computer, the method according to any one of claims 1 to 3 is executed.

Citation Information

Patent Citations

  • Database access method and device, computer equipment and storage medium

    CN110727698A

  • Distributed graph databases that facilitate streaming data insertion and queries by reducing number of messages required to add a new edge by employing asynchronous communication

    US20180039709A1