Master-slave database configuration detection method and device, storage medium and computer equipment

通过监听应用代码变动并解析配置文件,自动检测主从数据库的读写配置,解决了现有技术中配置错误无法及时发现的问题,实现了高效、自动化的数据库配置监控,提升了数据库的稳定性和安全性。

CN119513070APending Publication Date: 2025-02-25GUANGZHOU PINWEI SOFTWARE CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202411736072.2
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-11-29
Publication Date
2025-02-25

AI Technical Summary

Technical Problem

In the prior art, the configuration errors of master-slave databases cannot be detected in time, especially in low traffic scenarios, resulting in potential problems being ignored.

Method used

By listening to application code changes, obtaining context configuration files, initial function methods and their class information, parsing configuration files to locate database interaction functions, match class information and Asgard SDK, building a call chain and filtering the read-only database call chain, establishing a mapping relationship between the top-level interface and the Asgard SDK, and automatically detecting the read and write configuration of the master and slave database.

Benefits of technology

Automatic detection is realized, which avoids the delay and error of manual intervention, ensures accurate capture of database interaction functions and the binding of transaction management mechanisms, reduces the risk of read and write chaos, and improves the security and stability of the database.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119513070A_ABST
    Figure CN119513070A_ABST
Patent Text Reader

Abstract

The invention provides a master-slave database configuration detection method and device, a storage medium and computer equipment. The method comprises the following steps: acquiring a context configuration file of a changed application code, each initial function method and corresponding class information; obtaining each objective function method for database interaction through the context configuration file, and obtaining each initial Asgard SDK; according to the class information of each target function method, determining a target Asgard SDK (Software Development Kit) of each target function method for database transaction operation; determining each initial call chain according to the call relationship of each target function method, and filtering to obtain each target call chain for the read-only database; and establishing a mapping relationship between a top layer interface of each target call chain and each Asgard SDK, and detecting read-write configuration of the master and slave databases according to each mapping relationship. In this way, the read-write configuration problem of the master database and the slave database is accurately detected.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of computer technology, and in particular to a master-slave database configuration detection method, device, storage medium and computer equipment. Background Art

[0002] As the data scale and access volume continue to grow, a single database can no longer meet the requirements of high concurrency and high performance. Therefore, many systems have introduced a read-write separation mechanism to separate transactional operations from query operations to improve database performance and scalability. Read-write separation reduces the pressure on the master database by using the master database to process transactional operations such as inserts, updates, and deletes, and using the slave database to process query operations.

[0003] In application scenarios based on microservice architecture, read-write separation configuration is usually manually configured by developers or operation and maintenance personnel. Specifically, the application can choose to write operations in the master database and read operations in the slave database through configuration. The database replication mechanism is used to synchronize changes on the master database to the slave database to ensure the consistency of the slave database data.

[0004] Although this method is designed to effectively share database pressure, since read-write separation configuration is done manually, there is inevitably a risk of configuration errors. If the configuration is wrong, it may cause the application to perform read and write operations on the master database at the same time or write operations on the slave database. In low-traffic scenarios, even if the configuration is wrong, the application may still run normally, resulting in the problem of master-slave database configuration errors not being discovered in time. Summary of the invention

[0005] The purpose of the present application is to solve at least one of the above-mentioned technical defects, especially the technical defect that the master-slave database configuration error problem in the prior art cannot be discovered in time.

[0006] In a first aspect, the present application provides a master-slave database configuration detection method, the method comprising:

[0007] When a change in the application code is detected, the context configuration file of the changed application code, each initial function method and its corresponding class information are obtained;

[0008] Through the context configuration file, obtain each target function method used for database interaction in each initial function method, and obtain each initial Asgard SDK. Asgard SDK is used to represent the data source corresponding to the changed application code;

[0009] According to the class information of each target function method, in each initial Asgard SDK, the target Asgard SDK for each target function method to be used for database transaction operations is determined;

[0010] According to the calling relationship of each target function method, each initial calling chain is determined, and each initial calling chain is filtered to obtain each target calling chain for the read-only database;

[0011] Establish a mapping relationship between the top-level interface of each target call chain and each Asgard SDK, and detect the read and write configuration of the master-slave database corresponding to the changed application code based on each mapping relationship.

[0012] In one embodiment, the step of obtaining the context configuration file of the changed application code, each initial function method and its corresponding class information includes:

[0013] Get the configuration tags of the changed application code and build a node tree based on each configuration tag;

[0014] Traverse the node tree interface to obtain the context configuration file;

[0015] The changed application code is constructed as a syntax tree, and the syntax tree is traversed to obtain each initial function method and its corresponding class information.

[0016] In one embodiment, the step of obtaining multiple target function methods for database interaction in each initial function method through a context configuration file includes:

[0017] Get the location of each mapping configuration file from the context configuration file;

[0018] According to each mapping configuration file location, obtain the mapping configuration file in each mapping configuration file location;

[0019] According to the namespace in each mapping configuration file, each target function method used for database interaction in each initial function method is obtained.

[0020] In one embodiment, according to the class information of each target function method, in each initial Asgard SDK, the step of determining the target Asgard SDK for each target function method for database transaction operation includes:

[0021] According to the class information of each target function method, the mapping configuration file path corresponding to each target function method is determined through regular matching;

[0022] Determine the mapping configuration file corresponding to each objective function method according to the mapping configuration file path corresponding to each objective function method;

[0023] The mapping configuration file corresponding to each target function method is parsed, and in each initial Asgard SDK, the target Asgard SDK for database transaction operations of each target function method is determined.

[0024] In one embodiment, the steps of determining each initial call chain according to the call relationship of each target function method include:

[0025] Determine the syntax tree of the changed application code and assign a method number to each original function method in the syntax tree;

[0026] Identify method call nodes in the numbered syntax tree, and build a call relationship for each target function method based on each method call node;

[0027] Based on the graph algorithm, the initial function methods in each call relationship are connected with the corresponding method numbers to obtain each initial call chain.

[0028] In one embodiment, the step of filtering each initial call chain to obtain each target call chain for a read-only database includes:

[0029] Determine the method annotations of each target function method in each initial call chain;

[0030] For each initial call chain, if the method annotation corresponding to the initial call chain includes a read-only annotation, the initial call chain is used as a target call chain to obtain each target call chain for the read-only database.

[0031] In one embodiment, the step of detecting the read-write configuration of the master-slave database corresponding to the changed application code according to each mapping relationship includes:

[0032] According to each mapping relationship, query the Asgard configuration of each top-level interface for database reading and writing;

[0033] For each top-level interface, if it is determined according to the Asgard configuration of the top-level interface that the master-slave database corresponding to the top-level interface is configured as a read master database, a prompt message indicating a master-slave database configuration error is generated for the top-level interface.

[0034] In a second aspect, the present application provides a master-slave database configuration detection device, the device comprising:

[0035] The application code monitoring module is used to obtain the context configuration file of the changed application code, each initial function method and its corresponding class information when the application code is monitored to have changed;

[0036] The target function method acquisition module is used to obtain each target function method used for database interaction in each initial function method through the context configuration file, and obtain each initial Asgard SDK. The Asgard SDK is used to represent the data source corresponding to the changed application code;

[0037] A target Asgard SDK determination module is used to determine the target Asgard SDK for database transaction operations of each target function method in each initial Asgard SDK according to the class information of each target function method;

[0038] The target call chain determination module is used to determine each initial call chain according to the call relationship of each target function method, and filter each initial call chain to obtain each target call chain for the read-only database;

[0039] The read-write configuration detection module is used to establish a mapping relationship between the top-level interface of each target call chain and each Asgard SDK, and detect the read-write configuration of the master-slave database corresponding to the changed application code based on each mapping relationship.

[0040] In a third aspect, the present application provides a storage medium: the storage medium stores computer-readable instructions, and when the computer-readable instructions are executed by one or more processors, the one or more processors execute the steps of the master-slave database configuration detection method in any of the above embodiments.

[0041] In a fourth aspect, the present application provides a computer device, comprising: one or more processors, and a memory;

[0042] The memory stores computer-readable instructions, and when the computer-readable instructions are executed by one or more processors, the steps of the master-slave database configuration detection method in any one of the above embodiments are performed.

[0043] It can be seen from the above technical solutions that the embodiments of the present application have the following advantages:

[0044] In the master-slave database configuration detection method, device, storage medium and computer equipment provided by the present application, by monitoring application code changes, the latest application code updates can be captured in time, and automated detection avoids delays and errors in manual intervention. By obtaining the context configuration file and the initial function method and its class information, the application code information can be fully grasped to ensure that the application code information analysis is correct; by parsing the context configuration file, the target function method used for database operations is accurately located to ensure that all database interaction functions are captured; by matching the class information with the initial Asgard SDK, the Asgard SDK for the database transaction operation corresponding to each target function can be accurately located, which not only realizes the automatic association of the Asgard SDK with the database operation, but also ensures that each database interaction function is bound to the corresponding transaction management mechanism, thereby avoiding transaction configuration errors; by analyzing the calling relationship of the target function, the calling chain can be completely constructed to track the execution path of the application code. Through the filtering function, only the calling chain related to the read-only database is retained, ensuring the accurate monitoring of the read-only database configuration, ensuring that the links related to the read-write mixed operation will not be mistakenly detected, reducing false alarms, and improving the monitoring efficiency of the read-write separation configuration; by building a top-level interface with Asgard SDK mapping can quickly locate whether the master-slave database configuration is correct. This process realizes the close linkage of call chain, function method and database configuration, and can automatically detect whether the read-write separation in the configuration meets expectations. In this way, the risk of master-slave database read-write confusion caused by configuration errors can be greatly reduced, especially in low-traffic situations, which can prevent hidden problems from being ignored and improve the security and stability of database use. BRIEF DESCRIPTION OF THE DRAWINGS

[0045] In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative labor.

[0046] Figure 1 A schematic diagram of a master-slave database configuration detection method provided in an embodiment of the present application;

[0047] Figure 2 A schematic diagram of the structure of a master-slave database configuration detection device provided in an embodiment of the present application;

[0048] Figure 3 A schematic diagram of the internal structure of a computer device provided in an embodiment of the present application. DETAILED DESCRIPTION

[0049] The following will be combined with the drawings in the embodiments of the present application to clearly and completely describe the technical solutions in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of this application.

[0050] The present application provides a master-slave database configuration detection method. The following embodiments are described by taking the method applied to a computer device as an example. It can be understood that the computer device can be any device with data processing functions, including but not limited to a single server, a server cluster, a personal laptop, a desktop computer, etc. Figure 1 As shown, the method may include the following steps:

[0051] S101: When a change in the application code is detected, a context configuration file of the changed application code, each initial function method and its corresponding class information are obtained.

[0052] In this embodiment, the application code refers to the source code of the application, including the business logic, interface, database interaction and other implementations of the application. The context configuration file usually refers to "applicationContext.xml" or a similar Spring configuration file, which defines the configuration information of the application's beans, data sources, mapper locations, database connections, etc. The initial function method refers to all methods in the application code, including functions at different levels such as the business layer and DAO (Data Access Object) layer. Class information refers to the class to which each initial function method belongs, including the class name, class path, class attributes, etc., which helps determine the location of the method in the application structure.

[0053] In this step, in the application code repository monitoring, once a new code submission or code change is detected, the `applicationContext.xml` or other similar configuration files in the project can be automatically obtained through the file listener or build script. For example, use a file scanning tool or Git tool to locate the changed file and load the configuration file from the predefined configuration directory.

[0054] The compiler or analysis tool can scan all classes and methods in the project through static code analysis, and with the help of reflection or code parsing libraries, it can extract information such as method definitions, method signatures, annotations, and class paths in each class. For example, it can traverse the changed source code files, parse the method signatures of each class and function, record the class path to which the function belongs, the modifiers, parameter types, and return values ​​of the methods, etc.

[0055] As you can understand, the context configuration file defines the global settings of the application, especially key information such as database mapping and Asgard SDK configuration. Obtaining this file can help you understand how the application manages database connections, transactions, and dependencies of various service beans. The initial function methods and class information are the basis for identifying application functional modules. By parsing all function methods, you can understand the overall logical structure of the application and the division of labor between the business layer and the data access layer. At the same time, the class information can accurately find the path to which the method belongs in the subsequent stage to facilitate database interaction and call chain analysis.

[0056] S102: Obtain each target function method for database interaction in each initial function method through the context configuration file, and obtain each initial Asgard SDK.

[0057] Database interaction refers to operations involving communication with the database, such as adding, deleting, modifying, and checking. It is usually handled by the methods of the DAO layer, which interact directly with the database through SQL statements. The target function method is those methods that are filtered out from the initial function method and are specifically used for database interaction, mainly referring to the methods of the DAO layer. Asgard SDK is used to represent the data source corresponding to the changed application code. Asgard is a unified data management platform, and the SDK corresponds to the dataSource used by the Mybatis data source.

[0058] In this step, you can use static code analysis tools to scan all function methods and identify which methods are responsible for interacting with the database. The target function methods for database interaction are usually located in the DAO layer, and database operations are implemented through annotations such as "@Select", "@Insert", "@Update", or binding with the "Mapper" interface. Therefore, you can scan all methods in the project code, parse method signatures and annotations, and use regular expressions or annotation parsing to identify methods with database operation-related annotations, such as the "@Mapper" interface in MyBatis. You can also obtain each target function method based on JavaParse. After that, you can obtain each Asgard SDK based on jdom, or parse the contents of the context configuration file, especially the configuration items related to the Asgard SDK, such as the definition of the data source. By scanning the " <bean>"or" <context:component-scan>" tab, you can determine the data source of each Asgard SDK.

[0059] It is understandable that in an application, although there are many initial function methods, only a part of them interact directly with the database. Identifying these target function methods can focus on the read and write operations of the database, avoid processing code unrelated to the database, and improve efficiency. Each Asgard SDK is a data source for the changed application code. By obtaining the initial Asgard SDK related to the application, you can understand the various data sources required for the application to run. Each data source corresponds to operations related to database transactions. Therefore, obtaining the initial Asgard SDK related to the application can prevent data consistency problems caused by incorrect read and write configurations.

[0060] S103: According to the class information of each target function method, in each initial Asgard SDK, a target Asgard SDK for database transaction operation of each target function method is determined.

[0061] The target Asgard SDK refers to the SDK that is specifically configured for each target function method and is used to process the database transaction operations of the method. Different DAO methods may be bound to different Asgard SDKs.

[0062] In this step, first, the class information of the target function method can be compared with the Asgard SDK configuration through regular expressions or direct class path matching mechanism to find the Asgard SDK corresponding to the class path. For example, the class path "com.example.dao.OrderDAO" may be mapped to the Asgard SDK responsible for order operations. After that, once the class path is matched successfully, the target function method is bound to the corresponding target Asgard SDK.

[0063] The sample code is as follows:

[0064]

[0065] It can be understood that the class to which the target function method belongs often defines its business domain and the scope of database operations. For example, a class that handles order operations needs to bind its function method to the Asgard SDK that specifically handles order transactions. Each Asgard SDK may correspond to different database logic and strategies, so it is necessary to determine which Asgard SDK each function method should use through class information to ensure the correctness and consistency of database operations. The class path provides a unique identifier for the target function method, which can accurately locate the location of each method in the code base. By matching the class path and Asgard SDK configuration, the most suitable database operation encapsulation can be automatically found, avoiding manual configuration or incorrect binding. In this way, it can ensure that each target function method is bound to the correct database transaction Asgard SDK.

[0066] S104: Determine each initial call chain according to the call relationship of each target function method, and filter each initial call chain to obtain each target call chain for the read-only database.

[0067] Among them, the call relationship is the dependency relationship between function methods, which indicates the calling order of function methods and forms the basis of the call chain. For example, function A calls function B, and function B calls function C, and the call relationship formed is A->B->C. The initial call chain is the function call sequence obtained by tracing according to the call relationship between functions. This sequence includes not only DAO methods but also intermediate business logic functions. The target call chain is a filtered call chain that only retains those call chains involving database read and write operations and that meet the requirements of read-only databases. Read-only databases refer to database instances that do not perform modification operations such as insert, update, and delete, but only perform query operations. They are usually used for read operations from the database in a master-slave database architecture.

[0068] In this step, you can use static code analysis tools or AOP (Aspect-Oriented Programming) technology to analyze the calling relationship of functions in the entire code base. Specifically, you can start from the entry function, recursively trace the call of each function, and generate a complete call sequence, which includes all functions related to business logic and functions that interact with the database. For example, start from the main business function or interface in the application code, trace each function it calls, and for each function, check whether it calls other functions. If so, continue to recursively trace these call relationships until the entire call chain is formed.

[0069] After that, you can filter out the call chains that only perform read operations by filtering by annotations or matching the methods by regular expressions. For example, traverse the generated initial call chains and check whether the function methods in each call chain belong to read-only query operations. If the call chain contains methods that are not read-only operations, exclude the chain and save the filtered call chain as the target call chain.

[0070] It can be understood that by building the initial call chain, the function call process in the entire business logic can be clearly identified, especially which functions are involved in database interaction. The complete call chain can understand all steps from the entry to the DAO layer, which is the basis for ensuring the correctness of business logic and the accuracy of database operations. However, not all call chains involve database operations, and some call chains may contain write operations, which may affect the read-write separation database configuration. Therefore, filtering out call chains that are not read-only databases can help further ensure that the read-only database only carries query operations and reduce the complexity of master-slave database read-write configuration detection.

[0071] S105: Establish a mapping relationship between the top-level interface of each target call chain and each Asgard SDK, and detect the read and write configuration of the master-slave database corresponding to the changed application code according to each mapping relationship.

[0072] Among them, the top-level interface is the top-level interface or method in the target call chain. It is the starting point of the call chain and is usually used to receive requests and start subsequent call processes. The mapping relationship is the association between the top-level interface of the target call chain and its corresponding AsgardSDK. Through this mapping, it can be determined which Asgard SDK each call chain uses for database operations. The master-slave database read and write configuration is the relationship and allocation rules between the master database and the slave database in the database configuration. The purpose of checking the read and write configuration is to ensure that when operating data, read requests are correctly routed to the slave database and write requests are correctly routed to the master database.

[0073] In this step, first, all target Asgard SDKs and the selected target call chains can be traversed, and the methods in the call chain and the target Asgard SDK bound to them can be analyzed one by one. For each target call chain, identify the top-level method of the call chain, which is the entry method of the entire call chain. Associate the top-level interface of the target call chain with all the bound target Asgard SDKs in the call chain to form a mapping relationship between the top-level interface and the Asgard SDK. Afterwards, based on the mapping relationship, it can be checked whether each call chain correctly uses the master library and the slave library, that is, read the read and write configuration of the Asgard SDK, detect whether the read operation request is routed to the slave library, and whether the write operation request is routed to the master library, so as to determine whether there is a configuration inconsistency between the master and slave libraries.

[0074] In an example, interface A reads dao method M and dao method N at the same time. These two methods correspond to sdk M and sdk N respectively. Then I need to integrate these two call chains M and N into one according to the interface dimension. The data interface is roughly as follows:

[0075]

[0076] It can be understood that the top-level interface is the entry point of the call chain, and all database operations will start from this entry. By binding the top-level interface with the Asgard SDK, the database transaction management method of each call chain can be clearly determined, ensuring that in complex call chains, database operations are always performed through the correct Asgard SDK. By detecting the read and write configuration of the master and slave libraries, it can ensure that the database operation symbols are correctly separated to avoid read and write mismatches. At the same time, this detection can reduce the occurrence of human configuration errors and improve the efficiency and security of database operations. In addition, this automated check can significantly reduce the complexity of operation and maintenance and ensure efficient database operations.

[0077] In the above embodiment, by monitoring the changes in the application code, the latest application code updates can be captured in time, and the automated detection avoids delays and errors caused by manual intervention. By obtaining the context configuration file and the initial function method and its class information, the application code information can be fully grasped to ensure that the application code information analysis is correct; by parsing the context configuration file, the target function method used for database operations can be accurately located to ensure that all database interaction functions are captured; by matching the class information with the initial Asgard SDK, the Asgard SDK for the database transaction operation corresponding to each target function can be accurately located, which not only realizes the automatic association between the Asgard SDK and the database operation, but also ensures that each database interaction function is bound to the corresponding transaction management mechanism, thereby avoiding transaction configuration errors; by analyzing the calling relationship of the target function, the calling chain can be completely constructed to track the execution path of the application code. Through the filtering function, only the calling chain related to the read-only database is retained, ensuring the accurate monitoring of the read-only database configuration, ensuring that the links related to the read-write mixed operation will not be mistakenly detected, reducing false alarms, and improving the monitoring efficiency of the read-write separation configuration; by building a top-level interface with Asgard SDK mapping can quickly locate whether the master-slave database configuration is correct. This process realizes the close linkage of call chain, function method and database configuration, and can automatically detect whether the read-write separation in the configuration meets expectations. In this way, the risk of master-slave database read-write confusion caused by configuration errors can be greatly reduced, especially in low-traffic situations, which can prevent hidden problems from being ignored and improve the security and stability of database use.

[0078] In one embodiment, the step of obtaining the context configuration file of the changed application code, each initial function method and its corresponding class information includes:

[0079] Get the configuration tags of the changed application code and build a node tree based on each configuration tag;

[0080] Traverse the node tree interface to obtain the context configuration file;

[0081] The changed application code is constructed as a syntax tree, and the syntax tree is traversed to obtain each initial function method and its corresponding class information.

[0082] Configuration tags refer to special tags or configuration elements in application code, usually in XML or annotation files, which are used to define application dependencies, services, components, database connections, or other system configuration parameters. Common configuration tags include Spring's ` <bean>`Tags,` <context>`Tags, etc. The node tree refers to a tree structure arranged in a certain hierarchical relationship, which represents the mutual nesting and dependency between configuration tags. Each configuration tag corresponds to a node, and the nodes form a tree structure through the parent-child relationship. Interface refers to the call boundary or contract between software modules. Interface can also refer to the entry point for interacting with other systems or codes in some way, which may be a programming interface (API) or a logical interface in the configuration. Syntax tree is a tree-like data structure that represents the grammatical structure of the code. It is usually used to parse the structure and logic in the source code. The syntax tree decomposes the code into individual elements and nodes for subsequent analysis and processing.

[0083] Specifically, once a code update is detected, the latest version of the changed code is automatically pulled, and then the parts related to the configuration files, such as XML configuration files, Java annotations, etc., are scanned, and all tags or annotations that mark the system configuration are extracted from them through regular expression matching, file parsing libraries, etc. After that, the elements extracted from the configuration tags are parsed to establish parent-child dependencies between them. Through recursion or iteration, a tree structure is constructed to represent the hierarchy and association relationship of each tag, and each node represents a configuration tag. The entire node tree is traversed using a depth-first search or breadth-first search algorithm. Every time a node is traversed, it is checked whether it has external dependencies, such as other configuration files and interfaces, and these dependent files or interfaces are returned as results.

[0084] For all the function methods and class information of the application code, you can use the code parsing tool to parse the changed application code into a syntax tree. The code parsing tool will generate a tree structure based on the syntax rules of the code, where each node represents an element in the code, such as a class, method, and variable. The nodes in the syntax tree contain detailed structural information of the code. Traversing the generated syntax tree is similar to traversing a tree structure, and a depth-first or breadth-first algorithm can be used. During traversal, information about each function method and its class will be recorded.

[0085] In an example, for XML configuration, the parsing method of Mybatis source code is referred to, tags are parsed according to the xml file format, and the xml file is abstracted into a tree with tag Node as a single node, so that the tree interface can be traversed to obtain the corresponding xml file content; for method and class information, the open source javaParser is used to parse the java code into a syntax tree, and all method and class information is obtained by traversing the syntax tree.

[0086] In this embodiment, the tags in the configuration file usually have a hierarchical structure and dependencies. Building a node tree helps to understand these dependencies and ensure that all related configurations are correctly loaded and parsed in a complex environment. The syntax tree helps to accurately understand the code structure and automatically extract method and class information.

[0087] In one embodiment, the step of obtaining multiple target function methods for database interaction in each initial function method through a context configuration file includes:

[0088] Get the location of each mapping configuration file from the context configuration file;

[0089] According to each mapping configuration file location, obtain the mapping configuration file in each mapping configuration file location;

[0090] According to the namespace in each mapping configuration file, each target function method used for database interaction in each initial function method is obtained.

[0091] The mapping configuration file location refers to the path or location of the configuration files related to the data layer interaction. The mapping configuration file refers to a specific configuration file, such as the "mapper.xml" file of MyBatis, which is used to describe the relationship between the database table and the DAO layer method in the application. These files define the execution logic of SQL statements and the mapping of DAO layer methods. Namespace is a logical grouping used to distinguish multiple mapping files. It defines the unique identifier of the method in each mapping file, which usually matches the full path of the DAO class and is used to distinguish different SQL statements and DAO methods.

[0092] Specifically, by parsing the context configuration file, the "mapperLocations" or "resources" tags defined in the file are searched using an XML parser or based on regular expressions to identify and extract the location of each database mapping configuration file. Once the location of the mapping configuration file is obtained, these paths are traversed and each mapping configuration file is loaded through the file reader. The namespace of each mapping configuration file is parsed, which can be a string corresponding to the DAO class path. Next, the corresponding method is found from the DAO class in the code based on the namespace. Then, by matching the method names in the configuration file with the method names in these DAO classes, these methods are marked as "target function methods", that is, functions used to interact with the database.

[0093] In an example, the sample code for obtaining the location of each mapping configuration file is as follows:

[0094]

[0095] The sample code for obtaining the target function method through the namespace is as follows:

[0096]

[0097] In this embodiment, the mapping configuration file contains key information about how the application interacts with the database, such as the association between SQL statements and DAO layer methods. By reading and parsing these configuration files, it is possible to parse how the application performs database operations such as SQL queries, inserts, updates, and establish mappings with DAO methods. The namespace is a key identifier that distinguishes different DAO classes and methods. Through the namespace, the function method that is specifically responsible for database interaction can be found.

[0098] In one embodiment, according to the class information of each target function method, in each initial Asgard SDK, the step of determining the target Asgard SDK for each target function method for database transaction operation includes:

[0099] According to the class information of each target function method, the mapping configuration file path corresponding to each target function method is determined by regular matching;

[0100] Determine the mapping configuration file corresponding to each objective function method according to the mapping configuration file path corresponding to each objective function method;

[0101] The mapping configuration file corresponding to each target function method is parsed, and in each initial Asgard SDK, the target Asgard SDK for database transaction operations of each target function method is determined.

[0102] Specifically, read the class information of the target function method, then use regular expressions to match and search for the mapping configuration file path to which the function method belongs. Regular expressions can match the location of the mapping file according to the rules of the class path, which is usually related to the file directory structure. Through the determined mapping configuration file path, read the corresponding configuration file from the file system, usually a ".xml" file, such as "Mapper.xml". Each file contains SQL mapping rules related to the target function method. Parse each mapping configuration file, extract the SQL mapping rules related to the target function method, and match them with the initial Asgard SDK. The parsing process includes reading the file, understanding the SQL statements and associated information therein. By parsing the mapping file, combined with the business logic and database operations of each target function method, the target function method is bound to a specific target Asgard SDK in the initial Asgard SDK to ensure that the target Asgard SDK can perform related database transaction operations.

[0103] In this embodiment, the target function method and its mapping configuration file usually have a consistent naming or path pattern. Using regular expressions for matching can quickly and automatically determine the mapping configuration file path corresponding to each target function method. The mapping configuration file contains specific mapping rules between the target function method and the database operation. After parsing, it can automatically identify the specific database operation that the target function method needs to perform, ensuring that the correct SQL statement is called. Each target function method requires a specific SDK to perform its database transaction operation. By selecting the appropriate target Asgard SDK from the initial Asgard SDK, the database operation can be more accurately bound to the Asgard SDK.

[0104] In one embodiment, the steps of determining each initial call chain according to the call relationship of each target function method include:

[0105] Determine the syntax tree of the changed application code and assign a method number to each original function method in the syntax tree;

[0106] Identify method call nodes in the numbered syntax tree, and build a call relationship for each target function method based on each method call node;

[0107] Based on the graph algorithm, the initial function methods in each call relationship are connected with the corresponding method numbers to obtain each initial call chain.

[0108] The method number is a number that uniquely identifies each function method in the syntax tree, and is used to distinguish and identify different methods. A method call node is a node in the syntax tree that represents a method call. Each node contains the called method information and is a link in the function call chain.

[0109] Specifically, the syntax parser can be used to parse the changed application code, analyze the code line by line, and decompose the structures such as classes, methods, and variables to generate a syntax tree for the code. Then, a unique number is assigned to each initial function method by traversing the syntax tree to ensure that each method can be uniquely identified by the number. In the numbered syntax tree, the call nodes of each method are scanned, and the target function of each method call is identified by parsing the syntax information of these nodes. Based on these call nodes, a call relationship can be constructed to form a graph that represents the call order and dependency relationship between methods. Using a graph algorithm, the method call nodes in the syntax tree are connected in their call order. Through these connections, a complete call chain is constructed with the method number as the index. Each call chain shows how the initial function forms an execution path by calling other functions.

[0110] It can be understood that the syntax tree can help understand the structural hierarchy of the application code, and the method number provides a concise and effective way to uniquely identify each method. By establishing a syntax tree and numbering, the functions in the code can be accurately tracked. At the same time, the numbering simplifies the reference of function methods, thereby reducing memory usage. By identifying the method call nodes, the calling order and dependencies between functions can be understood, thereby building the calling relationship of the functions. Graph algorithms provide an efficient way to handle complex dependencies. When there are multiple calls between methods, graph algorithms can quickly find all possible calling paths.

[0111] In one embodiment, the steps of filtering each initial call chain to obtain each target call chain for a read-only database include:

[0112] Determine the method annotations of each target function method in each initial call chain;

[0113] For each initial call chain, if the method annotation corresponding to the initial call chain includes a read-only annotation, the initial call chain is used as a target call chain to obtain each target call chain for the read-only database.

[0114] In the code, annotations are used to mark the metadata of function methods, and annotations can indicate special behaviors or rules of methods. Read-only annotations are a specific type of annotation, such as "@ReadWrite(type=READ)", which is used to mark that the method only performs operations to read the database, but does not write or modify it.

[0115] Specifically, you can use static code analysis tools or reflection mechanisms to scan the function methods in each initial call chain one by one and check the annotations of these methods. Parse the annotations above the method to extract whether it contains tags such as "@ReadWrite(type=READ)", indicating whether the method performs read-only database operations. Filter the call chains with confirmed annotations. For each initial call chain, determine whether it contains one or more target function methods with read-only annotations. If the database interaction function in the call chain contains a read-only annotation, this call chain is classified as a target call chain, that is, a call chain for read-only database operations.

[0116] In an example, the sample code for using annotation filtering call chain is as follows:

[0117]

[0118] In this embodiment, by analyzing the method annotations, the specific behavior of each method can be understood, especially the read and write operations can be distinguished. Determining the method annotations can quickly distinguish which call chains involve read-only database operations. By screening out the call chains of the read-only database, the read and write configuration of the database can be detected for these call chains, thereby reducing the amount of data for detecting the read and write configuration.

[0119] In one embodiment, the step of detecting the read-write configuration of the master-slave database corresponding to the changed application code according to each mapping relationship includes:

[0120] According to each mapping relationship, query the Asgard configuration of each top-level interface for database reading and writing;

[0121] For each top-level interface, if it is determined according to the Asgard configuration of the top-level interface that the master-slave database corresponding to the top-level interface is configured as a read master database, a prompt message indicating a master-slave database configuration error is generated for the top-level interface.

[0122] Among them, Asgard configuration is a configuration file generated by Asgard SDK for specifying the database read-write separation strategy. The configuration contains the read and write operation strategy of the master-slave database.

[0123] Specifically, the database read and write configuration corresponding to each top-level interface can be retrieved from the Asgard configuration file through the API or system interface. That is, first determine the target top-level interface, then locate the Asgard SDK configuration file associated with it, and finally extract the read and write configuration rules related to database operations from these files. After that, analyze the extracted Asgard configuration. If it is found that the database read and write strategy of a top-level interface is to read the master database instead of the slave database, mark the master-slave database configuration abnormality. When a master-slave database configuration error is detected, a prompt message is automatically generated, which can include detailed information about the error, such as which top-level interface has a problem, specific read and write configuration abnormalities, etc.

[0124] In this embodiment, the purpose of querying the Asgard configuration and determining the read and write status of the master and slave databases is to automatically detect the correctness of the database read and write separation strategy. Through the Asgard configuration of each top-level interface, it is ensured that all database operations are distributed between the master and slave databases as expected, reducing the occurrence of human configuration errors. Through the automated detection and prompt mechanism, performance bottlenecks or data consistency problems caused by database configuration errors can be effectively prevented, ensuring the healthy operation of the database architecture.

[0125] The following describes the master-slave database configuration detection device provided by the embodiment of the present application. The master-slave database configuration detection device described below and the master-slave database configuration detection method described above can be referred to each other. Figure 2 As shown, the present application provides a master-slave database configuration detection device, the device comprising:

[0126] The application code monitoring module 201 is used to obtain the context configuration file of the changed application code, each initial function method and its corresponding class information when the application code is monitored to have changed;

[0127] The target function method acquisition module 202 is used to acquire each target function method used for database interaction in each initial function method through the context configuration file, and acquire each initial Asgard SDK, where the Asgard SDK is used to represent the data source corresponding to the changed application code;

[0128] A target Asgard SDK determination module 203 is used to determine, in each initial Asgard SDK, a target Asgard SDK for database transaction operations of each target function method according to class information of each target function method;

[0129] The target call chain determination module 204 is used to determine each initial call chain according to the call relationship of each target function method, and filter each initial call chain to obtain each target call chain for the read-only database;

[0130] The read-write configuration detection module 205 is used to establish a mapping relationship between the top-level interface of each target call chain and each Asgard SDK, and detect the read-write configuration of the master-slave database corresponding to the changed application code according to each mapping relationship.

[0131] In one embodiment, the application code monitoring module 201 includes:

[0132] A configuration tag acquisition unit, used to acquire configuration tags of the changed application code and construct a node tree according to each configuration tag;

[0133] A context configuration file acquisition unit is used to traverse the node tree interface to obtain the context configuration file;

[0134] The initial function method acquisition unit is used to construct the changed application code into a syntax tree, and traverse the syntax tree to obtain each initial function method and its corresponding class information.

[0135] In one embodiment, the objective function method acquisition module 202 includes:

[0136] A first mapping configuration file location acquisition unit, configured to acquire locations of respective mapping configuration files from a context configuration file;

[0137] A first mapping configuration file acquisition unit, configured to acquire a mapping configuration file in each mapping configuration file position according to each mapping configuration file position;

[0138] The target function method determination unit is used to obtain each target function method used for database interaction in each initial function method according to the namespace in each mapping configuration file.

[0139] In one embodiment, the target Asgard SDK determination module 203 includes:

[0140] A second mapping configuration file location acquisition unit, configured to determine a mapping configuration file path corresponding to each target function method through regular matching according to class information of each target function method;

[0141] A second mapping configuration file obtaining unit, configured to determine a mapping configuration file corresponding to each objective function method according to a mapping configuration file path corresponding to each objective function method;

[0142] The target Asgard SDK determination unit is used to parse the mapping configuration file corresponding to each target function method, and determine the target Asgard SDK for database transaction operation of each target function method in each initial Asgard SDK.

[0143] In one embodiment, the target call chain determination module 204 includes:

[0144] A method number marking unit, used for determining a syntax tree of the changed application code and assigning a method number to each initial function method in the syntax tree;

[0145] A call relationship building unit, used for identifying method call nodes in the numbered syntax tree, and building a call relationship of each target function method according to each method call node;

[0146] The initial call chain determination unit is used to connect the initial function methods in each call relationship with the corresponding method numbers based on the graph algorithm to obtain each initial call chain.

[0147] In one embodiment, the target call chain determination module 204 includes:

[0148] A method annotation determination unit, used for determining a method annotation of each target function method in each initial call chain;

[0149] The target call chain determination unit is used to, for each initial call chain, if the method annotation corresponding to the initial call chain includes a read-only annotation, use the initial call chain as the target call chain to obtain each target call chain for the read-only database.

[0150] In one embodiment, the read-write configuration detection module 205 includes:

[0151] Asgard configuration query unit, used to query the Asgard configuration of each top-level interface for database reading and writing according to each mapping relationship;

[0152] The read-write configuration detection unit is used to generate a prompt message indicating a master-slave database configuration error for each top-level interface if the master-slave database corresponding to the top-level interface is determined to be a read-master database according to the Asgard configuration of the top-level interface.

[0153] In one embodiment, the present application also provides a storage medium, which stores computer-readable instructions. When the computer-readable instructions are executed by one or more processors, the one or more processors execute the steps of the master-slave database configuration detection method as described in any of the above embodiments.

[0154] In one embodiment, the present application also provides a computer device, in which computer-readable instructions are stored. When the computer-readable instructions are executed by one or more processors, the one or more processors execute the steps of the master-slave database configuration detection method as described in any of the above embodiments.

[0155] Indicatively, Figure 3 As shown, Figure 3 This is a schematic diagram of the internal structure of a computer device provided in an embodiment of the present application. The computer device 300 may be provided as a server. Figure 3 The computer device 300 includes a processing component 302, which further includes one or more processors, and a memory resource represented by a memory 301, for storing instructions executable by the processing component 302, such as an application. The application stored in the memory 301 may include one or more modules, each corresponding to a set of instructions. In addition, the processing component 302 is configured to execute instructions to perform the master-slave database configuration detection method of any of the above embodiments.

[0156] The computer device 300 may further include a power supply component 303 configured to perform power management of the computer device 300, a wired or wireless network interface 304 configured to connect the computer device 300 to a network, and an input / output (I / O) interface 305. The computer device 300 may operate based on an operating system stored in the memory 301, such as Windows Server TM, Mac OS X TM, Unix TM, Linux TM, Free BSD TM, or the like.

[0157] Those skilled in the art will understand that Figure 3 The structure shown in the figure is only a block diagram of a part of the structure related to the solution of the present application, and does not constitute a limitation on the computer device to which the solution of the present application is applied. The specific computer device may include more or fewer components than those shown in the figure, or combine certain components, or have a different arrangement of components.

[0158] Finally, it should be noted that, in this article, relational terms such as first and second, etc. are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply that there is any such actual relationship or order between these entities or operations. Moreover, the term "include", "comprise" or any other variant thereof is intended to cover non-exclusive inclusion, so that the process, method, article or equipment including a series of elements not only includes those elements, but also includes other elements not clearly listed, or also includes elements inherent to such process, method, article or equipment. In the absence of more restrictions, the elements limited by the sentence "include one..." do not exclude the existence of other identical elements in the process, method, article or equipment including the elements. Herein, "one", "one", "said", "the" and "it" may also include plural forms, unless the context clearly indicates another way. A plurality refers to at least two cases, such as 2, 3, 5 or 8, etc. "And / or" includes any and all combinations of the relevant listed items.

[0159] The various embodiments in this specification are described in a progressive manner, and each embodiment focuses on the differences from other embodiments. The various embodiments can be combined as needed, and the same or similar parts can refer to each other.

[0160] The above description of the disclosed embodiments enables those skilled in the art to implement or use the present application. Various modifications to these embodiments will be apparent to those skilled in the art, and the general principles defined herein may be implemented in other embodiments without departing from the spirit or scope of the present application. Therefore, the present application will not be limited to the embodiments shown herein, but will conform to the widest scope consistent with the principles and novel features disclosed herein.< / context> < / bean> < / bean>

Claims

1. A master-slave database configuration detection method, characterized in that: The method comprises: When a change in the application code is detected, the context configuration file of the changed application code, each initial function method and its corresponding class information are obtained; Obtaining, through the context configuration file, each target function method for database interaction in each of the initial function methods, and obtaining each initial Asgard SDK, wherein the Asgard SDK is used to represent the data source corresponding to the changed application code; According to the class information of each of the target function methods, determining, in each of the initial Asgard SDKs, a target Asgard SDK for database transaction operations of each of the target function methods; Determine each initial call chain according to the calling relationship of each target function method, and filter each initial call chain to obtain each target call chain for the read-only database; A mapping relationship between the top-level interface of each target call chain and each target Asgard SDK is established, and based on each mapping relationship, the read and write configuration of the master-slave database corresponding to the changed application code is detected.

2. The master-slave database configuration detection method according to claim 1, characterized in that: The step of obtaining the context configuration file of the changed application code, each initial function method and its corresponding class information includes: Obtaining configuration tags of the changed application code, and constructing a node tree according to each of the configuration tags; Traversing the interface of the node tree to obtain the context configuration file; The changed application code is constructed into a syntax tree, and the syntax tree is traversed to obtain each of the initial function methods and their corresponding class information.

3. The master-slave database configuration detection method according to claim 1, characterized in that: The step of obtaining, through the context configuration file, a plurality of target function methods for database interaction in each of the initial function methods comprises: Obtaining locations of various mapping configuration files from the context configuration file; According to each of the mapping configuration file positions, obtaining the mapping configuration file in each of the mapping configuration file positions; According to the namespace in each of the mapping configuration files, each target function method for database interaction in each of the initial function methods is obtained.

4. The master-slave database configuration detection method according to claim 1, characterized in that: The step of determining, in each of the initial Asgard SDKs, a target Asgard SDK for database transaction operations of each of the target function methods according to the class information of each of the target function methods comprises: According to the class information of each of the target function methods, determine the mapping configuration file path corresponding to each of the target function methods through regular matching; Determine the mapping configuration file corresponding to each of the objective function methods according to the mapping configuration file path corresponding to each of the objective function methods; The mapping configuration file corresponding to each of the target function methods is parsed, and in each of the initial AsgardSDKs, the target Asgard SDK for database transaction operations of each of the target function methods is determined.

5. The master-slave database configuration detection method according to claim 1, characterized in that: The step of determining each initial call chain according to the calling relationship of each target function method comprises: Determine a syntax tree of the changed application code, and assign a method number to each of the initial function methods in the syntax tree; Identify method call nodes in the numbered syntax tree, and construct a call relationship of each target function method according to each method call node; Based on the graph algorithm, the initial function methods in each of the calling relationships are connected with corresponding method numbers to obtain each of the initial calling chains.

6. The master-slave database configuration detection method according to claim 1, characterized in that: The step of filtering each of the initial call chains to obtain each target call chain for the read-only database includes: Determine the method annotation of each target function method in each of the initial call chains; For each of the initial call chains, if the method annotation corresponding to the initial call chain includes a read-only annotation, the initial call chain is used as the target call chain to obtain each of the target call chains for the read-only database.

7. The master-slave database configuration detection method according to claim 1, characterized in that: The step of detecting the read-write configuration of the master-slave database corresponding to the changed application code according to each of the mapping relationships includes: According to each of the mapping relationships, query the Asgard configuration of each of the top-level interfaces for database reading and writing; For each of the top-level interfaces, if it is determined according to the Asgard configuration of the top-level interface that the master-slave database corresponding to the top-level interface is configured as a read master database, a prompt message indicating a master-slave database configuration error is generated for the top-level interface.

8. A master-slave database configuration detection device, characterized in that: The device comprises: The application code monitoring module is used to obtain the context configuration file of the changed application code, each initial function method and its corresponding class information when the application code is monitored to have changed; An objective function method acquisition module, used to acquire each objective function method used for database interaction in each of the initial function methods through the context configuration file, and to acquire each initial Asgard SDK, wherein the Asgard SDK is used to represent the data source corresponding to the changed application code; A target Asgard SDK determination module, used to determine, in each of the initial Asgard SDKs, a target Asgard SDK for database transaction operations of each of the target function methods according to the class information of each of the target function methods; A target call chain determination module, used to determine each initial call chain according to the call relationship of each target function method, and filter each initial call chain to obtain each target call chain for the read-only database; The read-write configuration detection module is used to establish a mapping relationship between the top-level interface of each target call chain and each Asgard SDK, and detect the read-write configuration of the master-slave database corresponding to the changed application code based on each mapping relationship.

9. A storage medium, characterized in that: The storage medium stores computer-readable instructions, and when the computer-readable instructions are executed by one or more processors, the one or more processors execute the steps of the master-slave database configuration detection method according to any one of claims 1 to 7.

10. A computer device, characterized in that: include: one or more processors, and memory; The memory stores computer-readable instructions, and when the computer-readable instructions are executed by the one or more processors, the steps of the master-slave database configuration detection method according to any one of claims 1 to 7 are performed.