A method for automatically converting database single instance use cases into high availability use cases
Through an automated test framework and converter, a single-instance use case is automatically converted into a highly available use case, solving the problem of repeated writing of database test cases in multiple scenarios, and achieving efficient use case conversion and maintenance.
Patent Information
- Application Number
- CN202510554431.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-04-29
- Publication Date
- 2025-08-12
- Estimated Expiration
- 2045-04-29
AI Technical Summary
In the prior art, database test cases need to be repeatedly written in many scenarios, resulting in high maintenance costs and long time to analyze failed use cases.
Through automated testing frameworks and converters, using parser for syntax analysis, combined with HaTransformer and LogModeTransformer, automatically convert single-instance use cases into high-availability use cases, including pre-converters and core conversion logic, building clusters and handling necessary preconditions.
Reduces the time to write and maintain use cases, reduces the time to analyze failed use cases, and enables efficient use case conversion and automated testing.
Smart Images

Figure CN120066979B_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the field of database testing, and in particular relates to a method for automatically converting a database single instance use case into a high-availability use case. Background Art
[0002] Database test cases are typically written using a domain-specific language (DSL). Specifically, it is a custom scripting language designed specifically for testing database products. It provides some built-in global objects that provide various highly encapsulated interfaces, making test cases short and easy to use.
[0003] Tools such as Javacc or ANTLR can be used to design DSLs. Javacc describes a DSL by writing tokens and grammar rules in a .jj grammar file. For example, "instance = system.getInstance()" corresponds to an assignment statement, and the rule identifying it as an assignment statement is "identifier = expression." In this example, the identifier is a string starting with a letter, and the expression is a function call (a function call has another grammar rule: identifier.identifier left parenthesis parameter list right parenthesis. The parameter list has another grammar rule, which we won't discuss here). By using Javacc's .jj files to describe various grammar rules, you define your own DSL. Furthermore, key grammar rules are embedded with Java code, defining the Java statements to be parsed (translated) into when the rule is recognized. Javacc commands can generate a parser (syntax analyzer) for this DSL. This means that by passing a DSL-written use case file as input to the parser, it will be interpreted (translated) into Java for execution. For example, the line "print hello" in the DSL becomes System.out.println("hello").
[0004] However, database testing often requires testing the same functionality in multiple scenarios (modes). For example, to insert a piece of data into a table and verify its existence, the test case is typically written for a single instance. This scenario also needs to be tested for high-availability scenarios. Writing multiple high-availability test cases (at least one for each cluster) would be redundant, multiplying the number of test cases and increasing maintenance costs. Summary of the Invention
[0005] In view of this, the present invention aims to propose a method for automatically converting a single instance use case of a database into a high-availability use case, so as to solve the problems in the above-mentioned prior art such as the need for many repeated operation instructions and high maintenance costs.
[0006] To achieve the above object, the technical solution of the present invention is achieved as follows:
[0007] A method for automatically converting a database single instance use case into a high-availability use case includes the following steps:
[0008] Step 1: After the automated testing framework is started, use the parser to perform syntax analysis on the use case.
[0009] Step 2: Add a pre-converter to the high-availability converter;
[0010] Step 3: Call the apply method of HaTransformer to start transforming the use case;
[0011] In step 1, the parser performs grammatical analysis on the use case, including:
[0012] After the use case script is passed to the parser as an input stream, the input character stream is lexically analyzed and converted into a token.
[0013] Identify the grammatical rule to which the token belongs. After entering the grammatical rule corresponding to the DSL statement, use each element in the statement as the parameters of the constructor of a DslStatement implementation class to construct an object of the DslStatement implementation class.
[0014] When a semicolon is encountered, the current DSL statement is considered to be ended, and the next DSL statement is matched;
[0015] After all DSL statements of the use case are analyzed by the parser, they become List <dslstatement>;
[0016] In step 2, add a pre-transformer to the high-availability transformer, including:
[0017] To convert a single-instance use case to a high-availability use case, you need to replace the DSL statements and set up the necessary prerequisites for a high-availability cluster. If the GBase 8s database uses logical logs for master-slave synchronization, then if the database creation statement for the single-instance use case is in non-logging mode, conversion is required.
[0018] The conversion process includes:
[0019] Call the apply function, the parameter of the apply function is the use case object, the use case object includes List <dslstatement>The return value of the apply function is also a use case object, which represents the transformed use case. The two implementation classes of the apply function include LogModeTransformer and HaTransformer. LogModeTransformer and HaTransformer are log mode transformer and high availability transformer respectively.
[0020] In step 3, call the apply method of HaTransformer to start transforming the use case, including:
[0021] Call the apply method of all pre-converters;
[0022] Calls the core conversion logic of the high-availability converter.
[0023] Furthermore, the LogModeTransformer constructor has two parameters, which are used to convert the log mode represented by the first parameter to the log mode represented by the second parameter.
[0024] Furthermore, in the construction method of HaTransformer, a LogModeTransformer is first added as a front-end transformer. The two parameters are NONE and LOG, which represent the conversion from non-log mode to log mode.
[0025] If the target of the high-availability transformer is sec, that is, the previous single-instance variable name is used as the variable name of the cluster slave node, a LogModeTransformer needs to be added as a pre-transformer for the log mode of the slave node. The two parameters are BUFFERED and LOG, which represent changing the buffered log mode to the unbuffered log mode.
[0026] Furthermore, call the apply method of all pre-converters, including:
[0027] Set both pre-converters to LogModeTransformer, and the apply method directly uses the visitor corresponding to this converter to convert the use case;
[0028] Among them, the parent class of visitor is DeepCopyVisitor, which is used to deep copy each node of the abstract syntax tree; when using visitor to do depth-first traversal of testcase, it directly visits the copy node of the parent class;
[0029] The DSL of the log mode converter contains two statements for database creation. The database creation methods include common database creation and advanced database creation. Common database creation and advanced database creation correspond to two method calls. The DSLStatement implementation class corresponding to the method calls is DslMethodCallStatement. DslMethodCallStatement has an element representing the method name. As long as the method name is getDatabase or getAdvancedDatabase, it can be determined that it is a database creation statement.
[0030] When traversing to a method call statement, the specific process of entering the visitor includes:
[0031] Enter the visit method of visitor, get the method name from the stmt parameter of visit, if it is getDatabase, call the getDatabase method, if it is getAdvanceDatabase, call the getAdvancedDatabase method, if neither is true, directly call the visit method of the parent class to copy the statement.
[0032] Furthermore, the getDatabase call method includes:
[0033] The first parameter is set to set the log mode, including:
[0034] Create a new parameter list, first put the target log mode as the first list element, then remove the first element of the original parameter list elements and add them to the new parameter list in the original order, and finally recreate a method call statement object to replace the original object.
[0035] Furthermore, the getAdvancedDatabase call method includes:
[0036] The db object returned by the getAdvancedDatabase method call is an empty shell. The set method is used to set properties later. If you need to change the log mode, you don't need to worry about the previously set log mode. You only need to set it again to overwrite the original log mode.
[0037] Furthermore, the core conversion logic of the high-availability converter is called, including:
[0038] First, replace the acquisition of a single instance with the acquisition of a cluster, and make a judgment when visiting the assignment statement:
[0039] The visit method parameter is the DslStatement implementation class object corresponding to the assignment statement. The element on the right side of the equal sign is taken through the DslStatement implementation class object. If the element is a method call statement and the calling object is system and the method name is getinstance, it means that it is a DSL statement for obtaining a single instance, and a cluster is built; if the element is a method call statement and the method name is getConnection, getConnection is processed; if neither is the case, the parent class visit method is directly called to assign the statement.
[0040] Furthermore, building a cluster includes:
[0041] Define cluster = system.getCluser(); after parsing by parser, DslStatement will be obtained, and then manually constructed:
[0042] Construct a system object;
[0043] Construct a method call statement object;
[0044] Build a cluster object to receive the return value of the method call statement object;
[0045] Construct an assignment statement;
[0046] Based on clusterObj in the assignment statement, get each node in the cluster:
[0047] To obtain the DSL statement of the master node, the manual construction steps are as follows:
[0048] If the target object of the high-availability converter is the master node, the master node object of the cluster directly uses the original single instance node object, that is, uses the variable name of the original single instance node;
[0049] If the target object is a slave node, then a master node object needs to be manually constructed to obtain primaryObj as the master node object;
[0050] Construct a method call statement object with three parameters: clusterObj, "getNode", parameter list, and the obtained statement object variable name is getPrimaryExpression;
[0051] Construct an assignment statement object and obtain the DSL statement from the node;
[0052] Then call the statement object on the two cluster nodes, with three parameters: node object, "setConfig", and parameter list;
[0053] Then build a method call statement object to instantiate the cluster. The three parameters are: clusterObj and "instantiate".
[0054] Further, process getConnection, including:
[0055] The use case script steps are to first get the instance, then get the connection getConnection, and then use getConnection to perform the following operations:
[0056] If it can only be performed on the master node, when the cluster converter targets the slave node and executes this operation, it is necessary to temporarily switch to the master node for execution, and then resume execution on the slave node after execution. Therefore, another connection to the master node is required, and a method call statement object needs to be constructed. The three parameters are: primaryObj, "getConnection" and the original getConnection parameter list. The constructed connection object is named: __tempPrimConn;
[0057] In visit, when it is detected that the method name is one of the method names that must be executed on the main node, the current connection is switched to __tempPrimConn. The specific steps include:
[0058] I. Save the current connection;
[0059] II. Switch the current connection to __tempPrimConn;
[0060] III. Construct a method call statement object to obtain the name of the database to which the current connection is connected;
[0061] IV. Construct a method call statement object, and switch the library to the library name obtained in step III;
[0062] V. After the current visit statement, restore the connection saved in step I.
[0063] Compared with the prior art, the method of automatically converting a database single instance use case into a high-availability use case described in the present invention has the following advantages:
[0064] The present invention describes a method for automatically converting a database single-instance use case into a high-availability use case. The converter introduced by this method can not only reduce the time for writing and maintaining use cases, but also reduce the time for analyzing failed use cases. If the single-instance use case has failed, the high-availability use case obtained by the converter is directly skipped, so no analysis is required. BRIEF DESCRIPTION OF THE DRAWINGS
[0065] The accompanying drawings, which constitute part of the present invention, are provided to provide a further understanding of the present invention. The exemplary embodiments of the present invention and their descriptions are provided to explain the present invention and do not constitute an undue limitation of the present invention. In the accompanying drawings:
[0066] Figure 1 Schematic diagram of the overall steps of the embodiment of the present invention. DETAILED DESCRIPTION
[0067] It should be noted that, in the absence of conflict, the embodiments of the present invention and the features in the embodiments may be combined with each other.
[0068] In the description of the present invention, it should be understood that the terms "center," "longitudinal," "lateral," "upper," "lower," "front," "back," "left," "right," "vertical," "horizontal," "top," "bottom," "inside," "outside," etc., indicating orientations or positional relationships, are based on the orientations or positional relationships shown in the accompanying drawings and are intended solely for the purpose of facilitating the description of the present invention and simplifying the description. They do not indicate or imply that the devices or components referred to must have a specific orientation, be constructed, or operate in a specific orientation, and therefore should not be construed as limiting the present invention. Furthermore, the terms "first," "second," etc., etc., are used solely for descriptive purposes and should not be construed as indicating or implying relative importance or implicitly specifying the number of the technical features indicated. Thus, a feature defined as "first," "second," etc., may explicitly or implicitly include one or more of such features. In the description of the present invention, unless otherwise specified, "plurality" means two or more.
[0069] In the description of the present invention, it should be noted that, unless otherwise expressly specified or limited, the terms "mounted," "connected," and "connected" should be understood broadly. For example, they may refer to fixed, removable, or integral connections; mechanical or electrical connections; direct or indirect connections through an intermediary; and internal communication between two components. Those skilled in the art will understand the specific meanings of these terms in the present invention based on specific circumstances.
[0070] The present invention will be described in detail below with reference to the accompanying drawings and in conjunction with embodiments.
[0071] like Figure 1 As shown, a method for automatically converting a database single instance use case into a high-availability use case includes the following steps:
[0072] Step 1: After the automated testing framework is started, use the parser to perform syntax analysis on the use case.
[0073] In this embodiment, each DSL statement must match a grammar rule. The Java code embedded in the grammar rule turns it into an object of a subclass of DslStatement (interface). Each DSL statement has a corresponding grammar rule, which also has a DslStatement implementation class. After the use case is parsed by the parser, there is a List<String, String> . <dslstatement>, which contains all the statements actually executed by the use case, such as print hello; corresponding to newDslPrintStatement("hello");
[0074] Among them, the automated testing framework is any existing testing framework, wherein the parser analysis specifically includes: after the use case script is passed to the parser as an input stream, the input character stream is lexically analyzed and becomes a TOKEN (a common term in compiler theory), and further it can be identified which grammatical rule this string of TOKENs belongs to. After entering the grammatical rule corresponding to the DSL statement, there is embedded java code inside, which takes each element in the statement as each parameter of the constructor of a DslStatement implementation class, constructs an object of the DslStatement implementation class, and considers the current DSL statement to be over when a semicolon is encountered, and then matches the next DSL statement. After all DSL statements of the use case are parsed, they become List <dslstatement>.
[0075] Step 2: Add a pre-converter to the high-availability converter;
[0076] In this embodiment, converting a single-instance use case to a high-availability use case requires not only replacing DSL statements but also considering the necessary prerequisites for a high-availability cluster. For example, if the GBase 8s database uses logical logging for master-slave synchronization, then if the database creation statement in the single-instance use case is in non-logging mode, conversion is required. (The log mode converter is actually a standalone converter that can be used to test different log modes.)
[0077] Transformer is an interface that has a method (function) apply, the parameter of which is a use case object (which contains List <dslstatement>), the return value is also a use case object, representing the transformed use case. LogModeTransformer and HaTransformer are two implementation classes, one for the log mode transformer and the other for the high availability transformer.
[0078] The LogModeTransformer constructor has two parameters, which are used to convert the log mode represented by the first parameter to the log mode represented by the second parameter.
[0079] In the HaTransformer constructor, a LogModeTransformer is first added as a pre-transformer. Its two parameters are NONE and LOG, indicating that the non-logging mode is switched to logging mode. If the high-availability transformer's target is sec (slave node), meaning that the previous single-instance variable name is used as the variable name for the cluster slave node, then the subsequent operations of the original use case will also be performed on the cluster slave node, thus also requiring the slave node's logging mode. Therefore, a LogModeTransformer is added as a pre-transformer. Its two parameters are BUFFERED and LOG, indicating that the buffered logging mode is switched to unbuffered logging mode. This is a requirement of the GBase 8s database for HAC clusters and will not be explained in detail.
[0080] Step 3: Call the apply method of HaTransformer to start transforming the use case;
[0081] In this embodiment, it specifically includes:
[0082] (1) First, call the apply method of all pre-converters;
[0083] Both pre-transformers are LogModeTransformer (there is no pre-transformer). The apply method directly uses the visitor corresponding to this transformer to transform the use case. The following is the implementation of apply, which is also the typical writing method of general visitors:
[0084] LogModeVistor visitor = new LogModeVistor(original log mode, target log mode);
[0085] testcase.accept(visitor);
[0086] return visitor.getTestcase();
[0087] Each DSL statement in the testcase accepts(visitor) again, going down layer by layer (depth-first) until it encounters a leaf node in the syntax tree corresponding to the DSL statement. After visitor.visit(this);, it returns to the previous layer. After visitor.visit(this); for all elements in this layer, it visits this layer as a whole, then returns to the previous layer, and so on until it reaches the top. In other words, all leaf nodes and non-leaf nodes have been visited once.
[0088] The parent class of all visitors in this method is DeepCopyVisitor. As the name suggests, it deep copies each node in the abstract syntax tree. When using a visitor to perform a depth-first traversal of a testcase, the type of most nodes (DSL statements) is irrelevant, so the parent class visit is used to copy the node.
[0089] Among them, the log mode converter actually needs to modify the parameters of the database creation statement. The designed DSL has two statements for database creation, one is ordinary database creation and the other is advanced database creation, corresponding to two method calls. The DSLStatement implementation class corresponding to all method calls is DslMethodCallStatement. This class has an element representing the method name. As long as the method name is determined to be getDatabase or getAdvancedDatabase, it can be determined that it is a database creation statement.
[0090] When traversing to the method call statement, it will enter the following method of visitor, including:
[0091] Enter the visit(DslMethodCallStatement stmt) method of the visitor and get the method name from the stmt parameter. If it is getDatabase, replace it with 1) (see below). If it is getAdvanceDatabase, replace it with 2) (see below). If neither is true, directly call the visit method of the parent class to copy the statement.
[0092] The specific instructions are as follows:
[0093] @Override
[0094] public void visit(DslMethodCallStatement stmt)
[0095] {
[0096] / / If the function name of stmt is getDatabase, replace 1)
[0097] / / If the function name of stmt is getAdvancedDatabase, do replacement 2)
[0098] / / If the parent class's visit method is not called directly, copy this statement
[0099] }
[0100] 1) getDatabase;
[0101] This method has multiple overloads. The first parameter is always the logging mode, so you only need to replace the first parameter in the parameter list with the target logging mode. Specifically, create a new parameter list with the target logging mode as the first element. Then, remove the first element from the original parameter list and add the elements to the new parameter list in the original order. Finally, create a new method call statement object to replace the original object: new TdlMethodCallExpression(the original object that called getDatabase, "getDatabase", new parameter list).
[0102] Specifically, "all pre-converters" here actually refer to the two "log mode converters". The apply method of the log mode converter will call the corresponding visitor (LogModeVistor) of the log mode converter to traverse each DslStatement in the use case. When traversing to visit(DslMethodCallStatement stmt), the method name of the current DSL statement (that is, the method call statement) is obtained through stmt. If the method name is getDatabase, this step is processed.
[0103] 2) getAdvancedDatabase
[0104] The db object returned by this method call is actually an empty shell. A series of set methods will be used to set various properties. To change the log mode, you don't need to worry about the previously set log mode; you can just set it again to overwrite the original one. Specifically, the main method is new TdlMethodCallExpression(db object, "setLogMode", a new parameter list with one and only one element, which is the target log mode); this is used as a subsequent DSL statement (note that the db object is obtained from the visit assignment statement).
[0105] Specifically, the same as the description of 1) getDatabase, this step is performed when the method name is detected to be getAdvancedDatabase.
[0106] (2) Enter the core conversion logic of the high-availability converter
[0107] First, replace the acquisition of a single instance with the acquisition of a cluster, and make a judgment when visiting the assignment statement:
[0108] The method parameter of visit is the DslStatement implementation class object corresponding to the assignment statement. It is used to obtain the element on the right side of the equal sign. If the element is a method call statement and the calling object is system and the method name is getinstance, it means that it is a DSL statement for obtaining a single instance, then replacement 1) (see below) is performed; if the element is a method call statement and the method name is getConnection (establishes a connection and returns a connection object), then replacement 2) (see below) is performed; if neither is the case, the parent class visit method is directly called to assign the statement.
[0109] The specific instructions are as follows:
[0110] @Override;
[0111] public void visit(DslAssignmentStatement stmt);
[0112] {
[0113] / / If the right side of the equal sign of stmt is a method call statement, and the calling object is system, and the method name is getInstance, do replacement 1)
[0114] / / If the right side of the equal sign of stmt is a method call statement, and the method name is getConnection (establish a connection and return), do replacement 2)
[0115] / / If the parent class's visit method is not called directly, copy this statement
[0116] }.
[0117] 1) Build a cluster
[0118] cluster = system.getCluser(); After parsing (translation) by parser, a series of DslStatement will be obtained, which must now be constructed manually:
[0119] I. Construct a system object, systemObj = new DslObjectReference("system");
[0120] II. Construct a method call statement object, getClusterExpression = newDslMethodCallExpression(systemObj, "getCluster", empty parameter list);
[0121] III. Build a cluster object to receive the return value of II, clusterObj = newDslObjectReference("__cluster");
[0122] IV. Construct an assignment statement:
[0123] new TdlAssignmentStatement(clusterObj,getClusterExpression), when actually executed, it assigns the return value of the second parameter to the first parameter;
[0124] Now clusterObj is just a shell, let's get the nodes in the cluster:
[0125] The DSL format for obtaining the primary node is: primary = cluster.getNode(PRIMARY); the parameter is an enumeration representing the primary node type. The manual construction steps are as follows:
[0126] I. If the target object of the high-availability converter is the master node, then the cluster's master node object can directly use the original single instance node object, that is, use the variable name of the original single instance node. If the target object is a slave node, then the master node object needs to be manually constructed, the same as the previous construction method. In either case, this step obtains primaryObj as the master node object
[0127] II. Construct a method call statement object with three parameters: clusterObj, "getNode", parameter list (with one element PRIMARY), and the resulting statement object variable name is getPrimaryExpression;
[0128] III. Constructing assignment statement objects
[0129] new TdlAssignmentStatement(primaryObj,getPrimaryExpression);
[0130] The DSL statement for getting a slave node is generally written like this: secondary = cluster.getNode(SEC);
[0131] The steps are the same as building the statement to obtain the main node, except that step I is reversed, so I will not repeat them here.
[0132] Then make some settings for the two cluster nodes, also by building a method call statement object with three parameters: node object, "setConfig", and parameter list (configuration key and value).
[0133] Then construct a method call statement object to instantiate the cluster. The three parameters are: clusterObj, "instantiate", and parameter list (empty).
[0134] This step is the first step of the core conversion logic of entering the high-availability converter in (2) above, which changes the acquisition of a single instance into the construction of a cluster, corresponding to 1) above.
[0135] 2) Handle getConnection
[0136] The steps of the use case script are generally to get the instance first (build a cluster), then get the connection (getConnection), and then use this connection to perform a series of operations.
[0137] Some operations can only be performed on the master node, such as creating a database space. Therefore, when the cluster converter targets a slave node and executes such an operation, it needs to be temporarily switched to the master node for execution. After the execution is completed, it is restored to the slave node. Therefore, another connection to the master node is required. It is also necessary to construct a method call statement object with three parameters: primaryObj, "getConnection" and the original getConnection parameter list. The constructed connection object is named: __tempPrimConn;
[0138] In visit(DslMethodCallExprssion), when it is detected that the method name is one of the method names that must be executed on the main node, the current connection is switched to __tempPrimConn. Steps:
[0139] I. Save the current connection;
[0140] II. Switch the current connection to __tempPrimConn;
[0141] III. Construct a method call statement object to obtain the name of the database to which the current connection is connected;
[0142] IV. Construct a method call statement object and switch the library to the library name obtained in III;
[0143] V. After the current visit statement, restore the connection saved by I;
[0144] There are also some special cases. For example, some SQL statements need to be executed on the primary node or on both nodes. In visit(DslSqlStatement s), as long as the SQL statement matches this situation, it will be temporarily switched to the primary node for execution, or executed on both nodes. The construction steps also involve constructing various method call statements, which will not be explained in detail.
[0145] This step is the second step of the core conversion logic of entering the high-availability converter in (2) above. After the first step, the acquisition of a single instance has been converted to building a cluster. The subsequent processing is to obtain a connection, because now it may be to obtain a connection and operate on the slave node. However, in fact, some operations must be performed on the master node or on two nodes at the same time.
[0146] The automation framework starts with the parameter -xform. Each string that follows corresponds to the same high-availability converter, but with different parameters. Depending on the parameters, the high-availability converter has different processing logic in the corresponding visitor. The basic method is similar to the previous steps, manually constructing various DslStatements to replace the previous DSL statements.
[0147] The converter introduced in this method can not only reduce the time for writing and maintaining use cases, but also reduce the time for analyzing failed use cases. If a single-instance use case has failed, the high-availability use case obtained by the converter is directly skipped and no analysis is required.
[0148] The ultimate effect of this method is that by adding the -xform ? (? refers to a high-availability transformation, and xform is short for transform) parameter to the automated testing framework startup parameters, all test cases to be run will be converted to high-availability test cases. For example, -xform si_to_hacsec converts a single instance (si = single instance) to a HAC cluster (a HAC cluster mode of the Git repository). The variable names of the original single instance in the test case are used on the sec (secondary) node. The variable names, as the primary or slave of the HAC cluster, determine whether subsequent operations (such as SQL statements) in the test case are executed on the primary or slave node. This also requires some conversions tailored to the specific characteristics of the database.
[0149] This method can manually construct the actual execution statements that should be analyzed (translated) by the syntax analyzer to convert the single-instance use case into a high-availability use case.
[0150] The above description is only a preferred embodiment of the present invention and is not intended to limit the present invention. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and principles of the present invention should be included in the scope of protection of the present invention.< / dslstatement> < / dslstatement> < / dslstatement> < / dslstatement> < / dslstatement>
Claims
1. A method for automatically converting a database single instance use case into a high-availability use case, characterized by: The following steps are involved: Step 1: After the automated testing framework is started, use the parser to perform syntax analysis on the use case. Step 2: Add a pre-converter to the high-availability converter; Step 3: Call the apply method of HaTransformer to start transforming the use case; In step 1, the parser performs grammatical analysis on the use case, including: After the use case script is passed to the parser as an input stream, the input character stream is lexically analyzed and converted into a token. Identify the grammatical rule to which the token belongs. After entering the grammatical rule corresponding to the DSL statement, use each element in the statement as the parameters of the constructor of a DslStatement implementation class to construct an object of the DslStatement implementation class. When a semicolon is encountered, the current DSL statement is considered to be ended, and the next DSL statement is matched; After all DSL statements of the use case are analyzed by the parser, they become List <dslstatement> ;< / dslstatement> In step 2, add a pre-transformer to the high-availability transformer, including: To convert a single-instance use case to a high-availability use case, you need to replace the DSL statements and set up the necessary prerequisites for a high-availability cluster. If the GBase 8s database uses logical logs for master-slave synchronization, then if the database creation statement for the single-instance use case is in non-logging mode, conversion is required. The conversion process includes: Call the apply function, the parameter of the apply function is the use case object, the use case object includes List <dslstatement> The return value of the apply function is also a use case object, which represents the transformed use case. The two implementation classes of the apply function include LogModeTransformer and HaTransformer. LogModeTransformer and HaTransformer are log mode transformer and high availability transformer respectively.< / dslstatement> In step 3, call the apply method of HaTransformer to start transforming the use case, including: Call the apply method of all pre-converters; Call the core conversion logic of the high-availability converter; In the construction method of HaTransformer, first add a LogModeTransformer as a front transformer, the two parameters are NONE and LOG, which represent the conversion from non-log mode to log mode; If the target of the high-availability transformer is a slave node, that is, the previous single-instance variable name is used as the variable name of the cluster slave node, a LogModeTransformer needs to be added as a pre-transformer for the log mode of the slave node. The two parameters are BUFFERED and LOG, which represent changing the buffered log mode to the unbuffered log mode. Call the apply method of all pre-transformers, including: Set both pre-converters to LogModeTransformer, and the apply method directly uses the visitor corresponding to this converter to convert the use case; Among them, the parent class of visitor is DeepCopyVisitor, which is used to deep copy each node of the abstract syntax tree; when using visitor to do depth-first traversal of test cases, it directly visits the copy node of the parent class; The DSL of the log mode converter contains two statements for database creation. The database creation methods include common database creation and advanced database creation. Common database creation and advanced database creation correspond to two method calls. The DSLStatement implementation class corresponding to the method calls is DslMethodCallStatement. DslMethodCallStatement has an element representing the method name. As long as the method name is getDatabase or getAdvancedDatabase, it can be determined that it is a database creation statement. When traversing to a method call statement, the specific process of entering the visitor includes: Enter the visit method of visitor, get the method name from the stmt parameter of visit, if it is getDatabase, call the getDatabase method, if it is getAdvanceDatabase, call the getAdvancedDatabase method, if neither is true, directly call the visit method of the parent class to copy the statement; The getDatabase call method includes: The first parameter is set to set the log mode, including: Create a new parameter list, first put the target log mode as the first list element, then remove the first element of the original parameter list elements and add them to the new parameter list in the original order, and finally recreate a method call statement object to replace the original object; The getAdvancedDatabase call method includes: The db object returned by the getAdvancedDatabase method call is an empty shell. The set method is used to set properties later. If you need to change the log mode, you don't need to worry about the previously set log mode. You only need to set it again to overwrite the original log mode. Call the core conversion logic of the high-availability converter, including: First, replace the acquisition of a single instance with the acquisition of a cluster, and make a judgment when visiting the assignment statement: The visit method parameter is the DslStatement implementation class object corresponding to the assignment statement. The element on the right side of the equal sign is obtained through the DslStatement implementation class object. If the element is a method call statement, the calling object is system, and the method name is getinstance, it means it is a DSL statement for obtaining a single instance, and then a cluster is built; if the element is a method call statement and the method name is getConnection, then getConnection is processed; if neither is true, the parent class visit method is directly called to assign the statement; Build a cluster, including: Define cluster = system.getCluser(); after parsing by parser, DslStatement will be obtained, and then manually constructed: Construct a system object; Construct a method call statement object; Build a cluster object to receive the return value of the method call statement object; Construct an assignment statement; Based on clusterObj in the assignment statement, get each node in the cluster: To obtain the DSL statement of the master node, the manual construction steps are as follows: If the target object of the high-availability converter is the master node, the master node object of the cluster directly uses the original single instance node object, that is, uses the variable name of the original single instance node; If the target object is a slave node, then a master node object needs to be manually constructed to obtain primaryObj as the master node object; Construct a method call statement object with three parameters: clusterObj, "getNode", parameter list, and the obtained statement object variable name is getPrimaryExpression; Construct an assignment statement object and obtain the DSL statement from the node; Then call the statement object on the two cluster nodes, with three parameters: node object, "setConfig", and parameter list; Then build a method call statement object to instantiate the cluster. The three parameters are: clusterObj, "instantiate", and parameter list; Handle getConnection, including: The use case script steps are to first get the instance, then get the connection getConnection, and then use getConnection to perform a series of operations: If the operation can only be performed on the master node, when the cluster converter targets a slave node and executes this operation, it is necessary to temporarily switch to the master node for execution. After the execution is completed, the execution is resumed on the slave node. Therefore, another connection to the master node is required. It is also necessary to construct a method call statement object with three parameters: primaryObj, "getConnection" and the original getConnection parameter list. The constructed connection object is named: _tempPrimConn; In visit, when it is detected that the method name is one of the method names that must be executed on the main node, the current connection is switched to __tempPrimConn. The specific steps include: I. Save the current connection; II. Switch the current connection to __tempPrimConn; III. Construct a method call statement object to obtain the name of the database to which the current connection is connected; IV. Construct a method call statement object, and switch the library to the library name obtained in step III; V. After the current visit statement, restore the connection saved in step I.
2. The method for automatically converting a database single instance use case into a high-availability use case according to claim 1, characterized in that: The LogModeTransformer constructor has two parameters, which are used to convert the log mode represented by the first parameter to the log mode represented by the second parameter.
Citation Information
Patent Citations
Data center task resource prediction method based on massive logs
CN116089215A
Method and system for realizing general database adapter
CN117171192A