A method and device for tracking distributed queries of database systems at the code level
By tracking distributed queries of the DolphinDB database system at the code level, building Span trees and supporting the OpenTracing specification, it solves the problem of lack of tracking methods during the execution of distributed SQL scripts, quickly locates performance bottlenecks, and improves system stability.
Patent Information
- Application Number
- CN202410764929.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-06-14
- Publication Date
- 2024-08-23
- Estimated Expiration
- 2044-06-14
AI Technical Summary
In the DolphinDB database system, the execution process of distributed SQL scripts lacks specific tracking methods, making it difficult for development and operation and maintenance personnel to quickly locate and solve performance problems, affecting the stability and reliability of the system.
It adopts the method of tracking distributed queries of database systems at the code level. By recording and building the basic tracking unit Span tree, it shows the steps and time-consuming bottlenecks in the execution process, and supports the OpenTracing specification to integrate the execution processes of different components to achieve tracking results. Persistence and visualization.
It improves the efficiency of developers in locating performance problems, allows them to quickly understand complex calling steps and time-consuming bottlenecks without affecting query performance, and reduces the workload of development and operation and maintenance personnel.
Smart Images

Figure CN118349591B
Abstract
Description
Technical Field
[0001] The present invention belongs to the technical field of database development and maintenance, and in particular relates to a method and device for tracking distributed queries of a database system at a code level. Background Art
[0002] Data analysis fields such as finance and the Internet of Things require efficient, accurate and easy-to-use data processing. The database system DolphinDB introduces the SQL computing framework, which brings a series of significant benefits to data analysis. First, as a standard query language, SQL provides analysts with familiar tools that enable them to operate and analyze data more easily. Secondly, the combination of SQL and DolphinDB scripts makes data processing more flexible and can quickly adapt to different analysis needs. DolphinDB scripts are an execution script proposed by the database system DolphinDB. Unlike common database systems that use SQL or DSL, DolphinDB provides scripting languages similar to Python and Ruby. In addition, SQL's optimized execution engine can improve the efficiency of data processing, allowing data analysis to draw accurate conclusions more quickly.
[0003] However, although DolphinDB has introduced the SQL computing framework, there are still some challenges in terms of observability. In particular, during the execution of distributed SQL and other scripts, the lack of a very specific tracking method will cause developers and operation and maintenance personnel to spend too much energy when analyzing problems. Due to the inability to track the execution steps, relationships, and possible time-consuming bottlenecks of the script in real time, developers often find it difficult to quickly locate and solve potential performance problems, thus affecting the stability and reliability of the system.
[0004] In the existing technology, DolphinDB provides the SQL Explain function, which can perform performance analysis (profiling) on distributed SQL queries. However, this function can only display some overhead and bottlenecks in the logical SQL execution process, such as the map, reduce, merge and other stages. SQL Explain cannot clearly locate the specific location of the performance bottleneck in the code. When developers analyze slow queries, they can only check based on scattered log information, and there is a high probability that they will not be able to locate the problem due to insufficient information. Summary of the invention
[0005] In order to solve the deficiencies of the prior art and achieve the purpose of improving the efficiency of R&D personnel in locating performance problems, the present invention adopts the following technical solutions:
[0006] A method for tracking distributed queries of a database system at a code level comprises the following steps:
[0007] Step S1: Tracking the distributed query of the database system at the code level, taking the context information of each calling process in a complete call chain Trace as the basic tracking unit Span, recording and constructing the basic tracking unit Span tree; by recording the Span and constructing the Span tree, the steps, relationships and possible time-consuming bottlenecks in the specific execution process can be displayed later, and at the same time, the tracking result persistence is also realized. Compared with putting the tracking result in the memory without persistence, the present invention can still use the tracking log to obtain the tracking result for analysis after the program crashes;
[0008] Specifically, the context information of the call process occurring on each node under the same complete call chain Trace is recorded to form a log, and a complete basic tracing unit Span is constructed with the start and end logs of a call in the log. The basic tracing unit Span includes the call operation name, start and end time, and call relationship. The basic tracing unit Span tree is constructed based on the call relationship.
[0009] Step S2: Display the distributed query process by visualizing the basic tracing unit Span tree. By expanding the operation name, start and end time, call relationship and other information based on the Span tree, you can quickly understand the key information such as the complex call steps, time consumption of each partition, and overall bottleneck in an execution process. On the other hand, by supporting the distributed tracing standard OpenTracing and utilizing the OpenTracing ecosystem, the tracing results of the same request can integrate the execution processes of different components, and can also be visualized on any platform that supports the specification, reducing the workload of developers and operation and maintenance personnel.
[0010] Furthermore, in the step S1, each time a function is called and the basic tracing unit Span is recorded, it is necessary to obtain the parent SpanID of the function that is called. By maintaining a basic tracing unit Span stack, when entering a function, a new basic tracing unit Span object is created and put into the stack. The basic tracing unit Span object can obtain the parent SpanID of the function that is called from the stack. When exiting the function, the basic tracing unit Span object is automatically destructed and popped from the stack, thereby avoiding the SpanID being placed in the function parameter list, and obtaining the parent SpanID of the current function that is called by parameter passing, so as to reduce the code intrusiveness as much as possible.
[0011] Furthermore, in step S1, the distributed query database system splits a query into multiple query fragments and sends them to each node for execution respectively, and finally the query results of each query fragment are merged on the root node that initiates the query; each query fragment is a distributed call, holding a common root node job number rootJobID, a tracking switch flag, and a parent SpanID, so that it can record the context information of the call process occurring on each node under the same complete call chain Trace to form a log, and by splicing the CSV logs of multiple nodes, a complete basic tracking unit Span tree corresponding to a query is constructed.
[0012] Furthermore, a macro is constructed for creating a new basic tracing unit Span. When the tracing switch flag is off, the macro will return a pre-initialized empty basic tracing unit Span object, so that the created basic tracing unit Span is a static variable and does not apply for memory, so as to reduce memory application overhead.
[0013] Furthermore, the trace switch flag includes manual triggering and query triggering. The manual triggering is set to on on the trace flag isTracing of the session Session by executing the trace mode setTraceMode(true) statement by the user; the query triggering is triggered by a partition query inside the database system, and the trace flag isTracing of the distributed task is set to on through the internal serialization protocol. The trace flag isTracing of the distributed task will be parsed on the remote node and set as the flag on the terminal console.
[0014] Furthermore, when the log type is Start, in addition to the header, it will also be accompanied by tags; the basic tracking unit Span also includes tags, and the tags include node tags, script tags and thread tags; each query fragment is sent to each node, and then executed by multiple threads of the node respectively. When displaying the query process, the calling status and time consumption of each node and its threads and scripts can be clearly displayed.
[0015] Furthermore, in step S1, the header of each log includes SpanID, timestamp, and log type. The log type includes Start and End. A pair of Start and End logs constitute a complete basic tracking unit Span. When the log type is Start, in addition to the header, it will also be accompanied by a reference relationship, parent SpanID, and function name. When the log type is End, it only includes the log header.
[0016] Furthermore, in step S2, for multiple logs of type End corresponding to the same SpanID, during the visualization process, the End log with the largest timestamp is taken to form a basic tracking unit Span with the corresponding Start log, thereby displaying the maximum task time consumption in a batch distributed call.
[0017] Furthermore, the basic tracking unit Span includes an operation name, a start and end time, and a calling relationship Reference. The calling relationship Reference is a parent SpanID and a calling relationship between a parent and a child Span. The calling relationship includes ChildOf and FollowsFrom. ChildOf means that the parent Span needs to wait for the child Span during execution, which can be understood as synchronization. FollowsFrom means that the parent Span initiates a task for the child Span but does not wait, which can be understood as asynchronous.
[0018] A device for tracking distributed queries of a database system at the code level, including a query tracking module and a display module;
[0019] The query tracking module tracks the distributed query of the database system at the code level, takes the context information of each calling process in a complete call chain Trace as the basic tracking unit Span, records and constructs the basic tracking unit Span tree; by recording the Span and constructing the Span tree, the steps, relationships and possible time-consuming bottlenecks in the specific execution process can be displayed later, and at the same time, the tracking result persistence is also realized. Compared with putting the tracking result in the memory without persistence, the present invention can still use the tracking log to obtain the tracking result for analysis after the program crashes;
[0020] Specifically, the context information of the call process occurring on each node under the same complete call chain Trace is recorded to form a log, and a complete basic tracing unit Span is constructed with the start and end logs of a call in the log. The basic tracing unit Span includes the call operation name, start and end time, and call relationship. The basic tracing unit Span tree is constructed based on the call relationship.
[0021] The display module displays the distributed query process by visualizing the basic tracing unit Span tree. Through the operation name, start and end time, call relationship and other information expanded based on the Span tree, key information such as complex call steps, time consumption of each partition, and overall bottleneck in an execution process can be quickly understood. On the other hand, by supporting the distributed tracing standard OpenTracing and utilizing the OpenTracing ecosystem, the tracing results of the same request can integrate the execution processes of different components, and can also be visualized on any platform that supports the specification, reducing the workload of development and operation and maintenance personnel.
[0022] The advantages and beneficial effects of the present invention are:
[0023] The present invention uses SQL Trace technology to analyze the time consumption of complex SQL queries at the code level. At the same time, based on the OpenTracing specification, users can access and use the open source distributed tracing platform, and use the OpenTracing ecosystem to analyze the tracing results more conveniently and quickly through some visual displays and other means, thereby making it easier to find time-consuming bottlenecks. Although SQL Trace adds invasive code, the present invention can still have almost no impact on query performance without turning on SQL Trace. BRIEF DESCRIPTION OF THE DRAWINGS
[0024] Figure 1 It is a flow chart of a method for tracking distributed queries of a database system at the code level of the present invention.
[0025] Figure 2 It is a distributed query process diagram displayed by using SQL Trace without optimizing query time in an embodiment of the present invention.
[0026] Figure 3 It is a distributed query process diagram displayed using SQL Trace after optimizing query time in an embodiment of the present invention.
[0027] Figure 4 It is a schematic diagram of the structure of a device for tracking distributed queries of a database system at the code level of the present invention. DETAILED DESCRIPTION
[0028] The specific implementation of the present invention is described in detail below in conjunction with the accompanying drawings. It should be understood that the specific implementation described here is only used to illustrate and explain the present invention, and is not used to limit the present invention.
[0029] Due to the lack of effective tools to troubleshoot slow queries (SQL statement queries that exceed the specified time), this paper introduces SQL Trace technology for DolphinDB, a database system that combines an SQL computing framework. It aims to track the execution process of distributed SQL and other scripts, so that developers and operation and maintenance personnel can quickly understand the complex call steps, time consumption of each partition, overall bottlenecks and other key information in the execution process, and provide more reliable support for data analysis. SQL Trace is implemented in accordance with the OpenTracing specification, and can use its ecosystem to access open source systems to provide visualization methods.
[0030] SQL Trace adds intrusive code to record the start and end timestamps of certain functions (or sections of code) in the OpenTracing standard (a standard for distributed tracing), and stores them on disk in a CSV-like log format. When a user queries a trace result, SQL Trace splices the logs on each node and builds a span tree for display. At the same time, the trace results are also persisted, that is, when the program crashes (for example, due to OOM being killed by the kernel in business, or due to memory out-of-bounds bugs in logic), the trace log can still be used for analysis. In addition, SQL Trace also minimizes code intrusion (how much code changes will be introduced when using a new technology in the current code), reduces the performance impact on business logic, and minimizes the cost and risk of connecting to SQL Trace.
[0031] The following introduces a method of tracking distributed SQL queries of a database system DolphinDB at the code level from three aspects: recording method, display method and performance evaluation. Figure 1 shown.
[0032] 1. Recording method: Track the distributed query of the database system at the code level, use the context information of each call process in a complete call chain as the basic tracking unit Span, record and build the basic tracking unit Span tree.
[0033] OpenTracing is a set of consistent, unified, and vendor-independent distributed tracing standards and specifications proposed by CNCF. By supporting the OpenTracing specification, the tracing results of the same request can integrate the execution process of different components, and can also be visualized on any platform that supports the specification, reducing the workload of developers and operators.
[0034] In OpenTracing, Trace refers to a complete call chain, including all spans of this request. Span refers to the context information of a call process, including the operation name, start and end time, label, and call relationship Reference. Reference refers to the parent span ID and the call relationship between the parent and child spans. There are only two values: ChildOf and FollowsFrom. ChildOf means that the parent span needs to wait for the child span during execution, which can be understood as synchronization; FollowsFrom means that the parent span initiates the task of the child span but does not wait, which can be understood as asynchronous.
[0035] Through the above-mentioned standardized tracing concept, the present invention can express a tracing process as a Span tree. Through the visualization method, key information such as complex calling steps, time consumption of each partition, overall bottleneck, etc. in an execution process can be quickly understood.
[0036] 1.1. Tracking principle:
[0037] Taking distributed SQL query as an example, since the data in DolphinDB is distributed on multiple nodes, a query will be split into multiple query fragments. Each query fragment will be sent to each node and then executed by multiple threads of the node. Finally, the results of each fragment will be merged on the node that initiated the query and sent to the user. In this process, each node communicates over the network through epoll, and each query fragment is sent and received as a distributed call.
[0038] By allowing each distributed call to hold a common root node job number rootJobID, trace switch flag, and parent SpanID, it is possible to record the spans occurring on each node under the same trace and associate these spans to form a tree structure.
[0039] These recorded spans will be written to the disk in batches to form CSV logs. When a span start or end event occurs, the log corresponding to the TraceID will be written. A pair of Start and End logs constitute a complete span. The header of each log includes the SpanID, timestamp, and log type. When the type is Start, in addition to the header, it will also include the reference relationship, parent SpanID, function name, and Tags (currently only the node name). When the type is End, only the log header is included. An example is as follows:
[0040] An example is as follows:
[0041] …
[0042] 42532801-909a-01b2-9748-72ac49ff0bf3,1715656862194056561,Start,Root,00000000-0000-0000-0000-000000000000,receiving request,local8900,,1347159
[0043] 42532801-909a-01b2-9748-72ac49ff0bf3,1715656862194067431,End
[0044] 568b8781-fba3-b2b4-6f46-dd0b281f3b2a,1715656862194091683,Start,FollowsFrom,42532801-909a-01b2-9748-72ac49ff0bf3,Worker<0>::run,local8900,,1347116
[0045] a632b4af-df6b-ecb1-804b-de6b01958428,1715656862194123252,Start,ChildOf,568b8781-fba3-b2b4-6f46-dd0b281f3b2a,Tokenizer::tokenize,local8900,,1347116
[0046] a632b4af-df6b-ecb1-804b-de6b01958428,1715656862194137111,End
[0047] 69df6fff-ce40-f5a1-cb40-18ad3e886146,1715656862194142399,Start,ChildOf,568b8781-fba3-b2b4-6f46-dd0b281f3b2a,Parser::parse,local8900,,1347116
[0048] 69df6fff-ce40-f5a1-cb40-18ad3e886146,1715656862194194429,End
[0049] eeceb5f0-e4a5-5eb4-e543-8c7f01ed5dc5,1715656862194205727,Start,ChildOf,568b8781-fba3-b2b4-6f46-dd0b281f3b2a,Statement::execute,local8900,select * from pt where SEC_ID in sec,1347116
[0050] f9632f19-cdf0-898a-8e48-f80ce7029519,1715656862194216043,Start,ChildOf,eeceb5f0-e4a5-5eb4-e543-8c7f01ed5dc5,SQLQueryImp::getReference,local8900,select * from pt where SEC_ID in sec,1347116
[0051] c279627c-3971-ada5-e446-2fc7176a6710,1715656862194631640,Start,ChildOf,9918614d-e0c6-479f-a546-52a05aaa0f01,SQLQueryImp::partitionedCall,local8900,,1347116
[0052] 58fe0c69-b56e-af9e-ff4d-e0a6e96c97bd,1715656862194759522,Start,ChildOf,d466388a-97f7-cdad-7f42-c94326c7515a,StaticStageExecutor::execute,local8900,,1347116
[0053] 83c8f1cc-0969-e7b1-0246-cc4eaa7b0e95,1715656862194764581,Start,ChildOf,58fe0c69-b56e-af9e-ff4d-e0a6e96c97bd,StaticStageExecutor::execute[probing],local8900,,1347116
[0054] 83c8f1cc-0969-e7b1-0246-cc4eaa7b0e95,1715656862194766088,End
[0055] 7458a62a-224d-31ae-2a4b-bf1c5687e59b,1715656862194770377,Start,ChildOf,58fe0c69-b56e-af9e-ff4d-e0a6e96c97bd,StaticStageExecutor::execute[localWorker],local8900,,1347116
[0056] ……
[0057] 7458a62a-224d-31ae-2a4b-bf1c5687e59b,1715656862194888714,End
[0058] ……
[0059] 7458a62a-224d-31ae-2a4b-bf1c5687e59b,1715656862404950611,End
[0060] ……
[0061] 7458a62a-224d-31ae-2a4b-bf1c5687e59b,1715656862413925966,End
[0062] ……
[0063] 7458a62a-224d-31ae-2a4b-bf1c5687e59b,1715656862415422957,End
[0064] 58fe0c69-b56e-af9e-ff4d-e0a6e96c97bd,1715656862459131962,End
[0065] c279627c-3971-ada5-e446-2fc7176a6710,1715656862460790223,End
[0066] f9632f19-cdf0-898a-8e48-f80ce7029519,1715656862460846586,End
[0067] eeceb5f0-e4a5-5eb4-e543-8c7f01ed5dc5,1715656862460862771,End
[0068] 568b8781-fba3-b2b4-6f46-dd0b281f3b2a,1715656862462253557,End
[0069] …
[0070] The following takes one paragraph as an example to explain the meaning and content of each field:
[0071] …
[0072] 7458a62a-224d-31ae-2a4b-bf1c5687e59b,1715656862194770377,Start,ChildOf,58fe0c69-b56e -af9e-ff4d-e0a6e96c97bd,StaticStageExecutor::execute[localWorker],local8900,,1347116
[0073] …
[0074] 7458a62a-224d-31ae-2a4b-bf1c5687e59b,1715656862194888714,End
[0075] …
[0076] 7458a62a-224d-31ae-2a4b-bf1c5687e59b,1715656862404950611,End
[0077] …
[0078] 7458a62a-224d-31ae-2a4b-bf1c5687e59b,1715656862413925966,End
[0079] …
[0080] 7458a62a-224d-31ae-2a4b-bf1c5687e59b,1715656862415422957,End
[0081] …
[0082] First, the first line describes the attributes of this span, including span ID, timestamp, log type, reference relationship, parent span ID, function name, and tags. The corresponding relationship of each attribute is as follows:
[0083] Group SpanID: 7458a62a-224d-31ae-2a4b-bf1c5687e59b
[0084] Group Timestamp: 1715656862194770377
[0085] Group Type: Start
[0086] Group Relation: ChildOf
[0087] Group ParentSpanID: 58fe0c69-b56e-af9e-ff4d-e0a6e96c97bd
[0088] Group FunctionName (function name): taticStageExecutor::execute[localWorker]
[0089] Group NodeTag: local8900
[0090] Group ScriptTag: empty string
[0091] Group ThreadTag: 1347116
[0092] Next are multiple End log lines. These lines all have 7458a62a-224d-31ae-2a4b-bf1c5687e59b as the SpanID, and each has a different timestamp, indicating the multiple end times of the Span. There are several sub-spans before each End log line. The specific operations represented by these spans are asynchronous operations triggered by StaticStageExecutor::execute[localWorker]. In order to facilitate the statistics of the time consumption of the distributed call process, we take the End log with the largest timestamp as the End log of this Span.
[0093] By simply concatenating the CSV logs of multiple nodes, a complete Span tree of a trace can be constructed. Multiple End logs can correspond to the same SpanID, and the End log with the largest timestamp will be used to form the Span in the final display. This is to show the maximum task time in a batch distributed call.
[0094] 1.2 Implementation details
[0095] SQL Trace needs to minimize the impact on the performance of the computing process and reduce the code intrusion, providing developers with a simple and fast recording method. To this end, SQL Trace provides some C++ macros to reduce memory allocation overhead and provides a tracing method through LocalTracer to minimize code intrusion.
[0096] If you record Span at the beginning of each function, you need to know the parent SpanID of the function that called it. A naive approach is to put this SpanID in the function parameter list and pass it through the parameter. However, this will significantly increase the invasiveness of the code. If you want to record Span for each function in the call chain, it will be a maintenance disaster. In order to reduce invasiveness, LocalTracer maintains a Span stack in thread local storage TLS (Thread Local Storage). When a function is entered, a new Span is created and put into the stack. At this time, the Span can obtain the ID of the previous Span from the stack. When the function exits, the Span object is automatically destructed and automatically popped from the stack.
[0097] In order to reduce the overhead of applying for memory, SQL Trace provides some macros that encapsulate branch prediction and ternary operators. The pseudo code is as follows:
[0098] …
[0099] #define START_SPAN(name, setter) \
[0100] UNLIKELY(Tracer::isTracing()) \
[0101] ? SpanSP{Tracer::startSpan(name)->setters} \
[0102] : Tracer::NoopSpan()
[0103] …
[0104] When the trace flag is off, this macro will return a pre-initialized NoopSpan object, which can avoid unnecessary memory allocation. Its usage is like `auto span = START_SPAN("functionName", setScript("xxx"))`, which creates a new Span with the operation name functionName and sets the script to xxx. When the trace flag is off, span will be a static variable and no memory will be allocated.
[0105] There are two types of trace flag switches. One is manually triggered by the user, by executing the setTraceMode(true) statement, the isTracing flag of the session can be set to on. The other is triggered by the partition query within the system, the system sets the isTracing bit of the distributed task to on through the internal serialization protocol. The isTracing bit of the distributed task will be parsed on the remote node and set as the flag bit on the terminal (console).
[0106] 2. Display method: Display the distributed query process by visualizing the basic tracking unit Span tree.
[0107] SQL Trace provides native display methods of DolphinDB GUI and display methods through the open source distributed tracing platform Jeager. They both visualize spans to a certain extent, making it easier for developers and operation and maintenance personnel to track requests.
[0108] 2.1. Display through DolphinDB GUI
[0109] DolphinDB GUI is an interactive client of DolphinDB, which provides interactive script execution and can display variables of various data types, such as tables, matrices, dictionaries, etc. SQL Trace formats and outputs a Span tree in the form of tables. A distributed query process includes script parsing, parsing, execution, SQL splitting, and distributed tasks.
[0110] 2.2. Display through Jeager distributed tracking platform
[0111] SQL Trace implements the OpenTracing specification, so it can be connected to a distributed tracing platform that complies with the OpenTracing specification. By parsing the CSV file, the trace information can be sent to Jeager for better visualization. Through the same query process as above, users can clearly and quickly understand the key information such as complex call steps, relationships, time-consuming bottlenecks, etc. in an execution process.
[0112] Analysis Example
[0113] This paper takes a storage engine under development as an example to introduce how to locate performance bottlenecks by using SQL Trace and reduce the time consumption of a SQL query from 180ms to less than 30ms.
[0114] The SQL that needs performance tuning can be simplified to SELECT * FROM table WHERE id IN id_set. The table is a distributed table with 5 partitions and a total of 800,000 rows of data. The unoptimized query takes about 1 second. By enabling SQL Trace for tracking, we can get the following: Figure 2 The trace results shown (only the results of one partition are captured).
[0115] From the SQL Trace results, we can find that the process of executing SQL on a single partition (basicCall) takes 183ms. The main time consumption is concentrated in PKTabletScanner::scan, with a total overhead of nearly 125ms. In addition, the second most time-consuming process is PKTabletReader::load[deduplicate], which takes 29ms.
[0116] Combined with the actual code, the present invention can quickly discover the performance problems inside the engine:
[0117] The scanning process does not allocate memory in advance, resulting in frequent memory requests and increased time consumption.
[0118] The ZoneMap matching code is not optimal: the time complexity of the ZoneMap matching algorithm is too high, and there are some unnecessary copies in the matching process. It is expected to reduce 200ms.
[0119] The deduplication process is not optimal: In order to provide correct query results, the storage engine needs to filter invalid data. However, we can execute the SQL where condition in advance to reduce the number of rows that need to be deduplicated, thereby reducing the time overhead of the deduplication part. It is expected to reduce 15ms.
[0120] Too many columns were read at one time: This SQL query reads three columns in total, so the storage engine reads three columns at one time. Similar to the previous optimization point, we let the SQL where condition execute in advance to reduce the number of rows that need to be read, which can reduce the time spent on building the table (materialization). It is expected that the time spent can be reduced by 60% after the above optimization.
[0121] After achieving the above four optimization points, track again. The tracking results are as follows Figure 3 As shown, it can be found that the original bottleneck has disappeared and the overall time consumption has been reduced to 23ms.
[0122] 4. Performance test:
[0123] The performance goal of SQL Trace is to not introduce performance loss when tracing is not enabled. In almost all places where spans are created, the START_SPAN macro is used to eliminate memory allocations and use branch prediction.
[0124] Through Python API testing, the performance is ±0.5% of the version without SQL Trace. After tracing is enabled, the time consumption varies with the number of partitions. When the number of partitions is 100, the trace consumption accounts for 22% of the total consumption; 10,000 partitions consume about 25%; 100,000 partitions consume 17%. The performance loss of enabling tracing may come from the introduction of additional log writing overhead, which affects query efficiency.
[0125] In summary, the present invention uses SQLTrace technology to track distributed tasks according to the OpenTracing specification, providing developers with a convenient and fast recording method, as well as a persistence and recycling mechanism for tracking logs, so as to more conveniently track the execution process of DolphinDB scripts and reduce the invasive TLS Span stack.
[0126] like Figure 4 As shown, a device for tracking distributed queries of a database system at the code level includes a query tracking module and a display module;
[0127] The query tracking module tracks the distributed queries of the database system at the code level, and uses the context information of each call process in a complete call chain Trace as the basic tracking unit Span, and records and constructs the basic tracking unit Span tree; by recording the Span and constructing the Span tree, the steps, relationships and possible time-consuming bottlenecks in the specific execution process can be displayed later. At the same time, the tracking results are persisted. Compared with putting the tracking results in the memory without persistence, the present invention can still use the tracking log to obtain the tracking results for analysis after the program crashes;
[0128] Specifically, the context information of the call process occurring on each node under the same complete call chain Trace is recorded to form a log, and a complete basic tracing unit Span is constructed with the start and end logs of a call in the log. The basic tracing unit Span includes the call operation name, start and end time, and call relationship. The basic tracing unit Span tree is constructed based on the call relationship.
[0129] The display module displays the distributed query process by visualizing the basic tracing unit Span tree. Through the operation name, start and end time, call relationship and other information expanded based on the Span tree, it can quickly understand the key information such as the complex call steps, time consumption of each partition, and overall bottleneck in an execution process. On the other hand, by supporting the distributed tracing standard OpenTracing and utilizing the OpenTracing ecosystem, the tracing results of the same request can integrate the execution processes of different components, and can also be visualized on any platform that supports the specification, reducing the workload of developers and operation and maintenance personnel.
[0130] The implementation method of this part is similar to that of the above method embodiment and will not be repeated here.
[0131] The above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit the same. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that the technical solutions described in the aforementioned embodiments may still be modified, or some or all of the technical features thereof may be replaced by equivalents. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the scope of the technical solutions of the embodiments of the present invention.
Claims
1. A method for tracking distributed queries in a database system at the code level, characterized in that The steps include: Step S1: Track the distributed query of the database system at the code level, use the context information of each call process in a complete call chain Trace as the basic tracking unit Span, record and build the basic tracking unit Span tree; Specifically, the context information of each call process under the same complete call chain Trace is recorded to form a log, and a complete basic tracking unit Span is constructed with the start and end logs of a call in the log. The basic tracking unit Span includes the call operation name, start and end time, and call relationship. The basic tracking unit Span tree is constructed based on the call relationship. The distributed query database system splits a query into multiple query fragments and sends them to each node for execution. Finally, the query results of each query fragment are merged on the root node that initiates the query. Each query fragment is a distributed call, holding a common root node job number rootJobID, a trace switch flag, and a parent SpanID. It records the context information of the call process occurring on each node under the same complete call chain Trace to form a log. By splicing multiple node logs, a complete basic trace unit Span tree corresponding to a query is constructed. The tracking switch flag includes manual trigger and query trigger. The manual trigger is set to on on the tracking flag of the session by the user executing the tracking mode; the query trigger is triggered by the partition query inside the database system. Through the internal serialization protocol, the tracking flag of the distributed task is set to on. The tracking flag of the distributed task will be parsed on the remote node and set as the flag on the terminal; Step S2: Display the distributed query process by visualizing the basic tracking unit Span tree.
2. A method for tracking distributed queries of a database system at the code level according to claim 1, characterized in that: In the step S1, each time a function is called and the basic tracking unit Span is recorded, it is necessary to obtain the parent SpanID of the calling function. By maintaining a basic tracking unit Span stack, when entering a function, a new basic tracking unit Span object is created and put into the stack. The basic tracking unit Span object can obtain the parent SpanID of the calling function from the stack. When exiting the function, the basic tracking unit Span object is automatically destructed and popped from the stack.
3. The method for tracking distributed queries of a database system at the code level according to claim 1, characterized in that: Construct a macro for creating a new basic tracing unit Span. When the tracing switch flag is off, the macro will return a pre-initialized empty basic tracing unit Span object, so that the created basic tracing unit Span is a static variable without applying for memory.
4. The method for tracking distributed queries of a database system at the code level according to claim 1, characterized in that: When the log type is Start, in addition to the header, it will also be accompanied by tags; the basic tracking unit Span also includes tags, including node tags, script tags and thread tags; each query fragment is sent to each node and then executed by multiple threads of the node respectively.
5. The method for tracking distributed queries of a database system at the code level according to claim 1, characterized in that: In step S1, the header of each log includes SpanID, timestamp, and log type. The log type includes Start and End. A pair of Start and End logs constitute a complete basic tracking unit Span. When the log type is Start, in addition to the header, it will also be accompanied by a reference relationship, parent SpanID, and function name. When the log type is End, it only includes the log header.
6. The method for tracking distributed queries of a database system at the code level according to claim 4, characterized in that: In step S2, for multiple logs of type End corresponding to the same SpanID, in the process of visual display, the End log with the largest timestamp is taken together with the corresponding Start log to form a basic tracking unit Span.
7. The method for tracking distributed queries of a database system at the code level according to claim 1, characterized in that: The basic tracking unit Span includes an operation name, a start and end time, and a call relationship Reference. The call relationship Reference is a parent SpanID and a call relationship between a parent and a child Span. The call relationship includes that the parent Span needs to wait for the child Span during execution, and the parent Span initiates a task for the child Span but does not wait.
8. A device for tracking distributed queries of a database system at the code level, comprising a query tracking module and a display module, characterized in that: The query tracking module tracks the distributed queries of the database system at the code level, takes the context information of each call process in a complete call chain Trace as the basic tracking unit Span, records and constructs the basic tracking unit Span tree; Specifically, the context information of each call process under the same complete call chain Trace is recorded to form a log, and a complete basic tracking unit Span is constructed with the start and end logs of a call in the log. The basic tracking unit Span includes the call operation name, start and end time, and call relationship. The basic tracking unit Span tree is constructed based on the call relationship. The distributed query database system splits a query into multiple query fragments and sends them to each node for execution. Finally, the query results of each query fragment are merged on the root node that initiates the query. Each query fragment is a distributed call, holding a common root node job number rootJobID, a trace switch flag, and a parent SpanID. It records the context information of the call process occurring on each node under the same complete call chain Trace to form a log. By splicing multiple node logs, a complete basic trace unit Span tree corresponding to a query is constructed. The tracking switch flag includes manual trigger and query trigger. The manual trigger is set to on on the tracking flag of the session by the user executing the tracking mode; the query trigger is triggered by the partition query inside the database system. Through the internal serialization protocol, the tracking flag of the distributed task is set to on. The tracking flag of the distributed task will be parsed on the remote node and set as the flag on the terminal; The display module displays the distributed query process by visualizing the basic tracking unit Span tree.
