A split-account billing method based on user-level resource tracking
By introducing a user-level resource tracking method into the database system, DolphinDB solves the challenge of split billing, achieves accurate statistics and fair billing of resource usage, and improves operational efficiency and transparency.
Patent Information
- Application Number
- CN202410977314.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-22
- Publication Date
- 2024-10-11
- Estimated Expiration
- 2044-07-22
AI Technical Summary
The existing database system DolphinDB is difficult to accurately track and manage the resource usage of different business groups in terms of split accounting, resulting in unreasonable resource allocation and unfair billing, affecting business operation efficiency and transparency.
Using a user-level resource tracking method, by building a resource tracking and recording unit, including a resource tracker and a thread resource recorder, it samples and records the resource information and data access requested by the user, counts the user's CPU and memory usage, and Realize resource tracking and billing through scheduled sampling and logging.
It achieves accurate tracking of user-level resource consumption, ensures reasonable allocation and fair billing of resources, improves system observability and operational efficiency, and meets the needs of financial institutions.
Smart Images

Figure CN118503073B_ABST
Abstract
Description
Technical Field
[0001] The invention belongs to the technical field of database systems, and in particular relates to a split-account billing method based on user-level resource tracking. Background Art
[0002] In today's increasingly complex financial markets, real-time data processing and efficient computing are key to staying competitive. Financial institutions not only need to be able to respond quickly to market fluctuations, but also need to ensure data accuracy and security. In the face of this challenge, opening the database system DolphinDB as a middle-office technology to other business groups can provide financial institutions with an efficient, flexible and scalable data processing solution to better meet growing business needs.
[0003] However, although the database system DolphinDB performs well in large-scale data processing and real-time analysis, there are certain challenges in accounting and billing. The current system makes it difficult to accurately track and bill resource usage of different business groups, which leads to unreasonable resource allocation and unfair billing, which in turn affects the efficiency and transparency of business operations. For example, CPU usage cannot be counted; memory usage requires users to write scripts for regular collection, which is not convenient; and the data access volume of SQL queries can be viewed through the task log, but the data access volume cannot be counted. To solve this problem, a new method is needed to track and manage user-level resource usage. Summary of the invention
[0004] In order to solve the shortcomings of the prior art, to achieve the purpose of tracking user-level resource usage in the database system and performing corresponding account splitting and billing, the present invention adopts the following technical solutions:
[0005] A method based on user-level resource tracking comprises the following steps:
[0006] Step S1: construct a resource tracking and recording unit, including a resource tracker ResourceTracker and a thread resource recorder. The thread resource recorder is used to collect resource information and data access information in each thread, and the resource tracker ResourceTracker counts the information collected by the thread resource recorder;
[0007] Step S2: Based on the user request, the resource tracking and recording unit performs corresponding resource information sampling and data access information recording; before the user request is distributed to the database thread Worker for execution, the thread resource recorder records the session information Session of the user request, and after the database thread Worker is executed, the thread resource recorder clears the corresponding session information Session of this thread; the resource tracker ResourceTracker obtains the session information Session from all thread resource recorders to record the corresponding resource information; the thread resource recorder obtains the data access information according to the user request, and the resource tracker ResourceTracker obtains and records the corresponding data access information from all thread resource recorders;
[0008] Step S3: According to the recorded resource information and data access volume information, statistics are collected on the resource usage and data access volume of the user.
[0009] Furthermore, the session information Session includes information of a user ID and a user heap Heap. The resource tracker ResourceTracker counts the number of session information Session as the CPU usage of the user according to the user ID, and counts all variables in each user heap Heap as the memory usage.
[0010] Furthermore, the resource information includes CPU usage, which is obtained by sampling the number of database thread workers currently occupied by each user; the user ID is recorded first, and when the database thread worker is executed, the record of the user ID is canceled. In this way, the user IDs recorded by all database thread workers can be scanned during scheduled sampling, thereby recording the CPU usage at the user level.
[0011] Furthermore, the resource information includes memory usage. When the database thread Worker starts to process a request, the user heap is recorded. The user heap is used to record variables created by the user. When the variables need to be obtained during the execution of the database DolphinDB script, the executor takes out the corresponding C++ object from the user heap and schedules the sampling process of resource information. By scanning all variables in the user heap and calculating the memory allocation size of each object, the user's memory usage at that moment is recorded.
[0012] Furthermore, the user request includes a SQL query initiated by the user, and the thread resource recorder records the current query related information from the SQL query, including time, user ID, database name, data table name, root query rootQueryID and SQL query script. For the distributed execution process of the SQL query, the SQL query request initiated by the user will be sent to a node in the cluster. On the current node, the SQL query will be split into multiple single-partition queries according to the partition situation, and the data access volume information of the split single-partition queries will be recorded. These single-partition queries will be distributed to each node for execution. Finally, all queries split from the same SQL query are associated through the root query rootQueryID. If there is no rootQueryID, it is impossible to know which SQL query a split query is split from. The current node merges the query results into the final user query result.
[0013] Furthermore, the data access volume information is recorded in the thread resource recorder. When the query of this partition is completed, the data access volume information will be displayed to the resource tracker ResourceTracker. This is because if the ResourceTracker triggers logging during the query process, some data will be written to the log, resulting in inconsistent information.
[0014] Furthermore, when a data node executes a split single partition query, different recording methods are used depending on the current storage engine:
[0015] For the OLAP engine, there are three logics for obtaining data:
[0016] 1. Obtain data by scanning documents;
[0017] 2. Read the partition cache through CHUNK_CACHE_ENGINE to obtain data;
[0018] 3. Get data through column cache;
[0019] Resource tracking will record the number of rows actually read in the above three cases, and will not record other cases, such as reading rows from metadata;
[0020] For the TSDB engine, there are two logics for obtaining data:
[0021] 1. Obtain data by scanning documents;
[0022] 2. Get data through the cache engine Cache Engine;
[0023] Resource tracking will record the number of rows actually read here. Other situations, such as reading pre-aggregated results (such as sum) from the Zone Map, will not be recorded;
[0024] For the PKEY engine, the logic for obtaining data is:
[0025] 1. Obtain data by scanning documents;
[0026] 2. Get data through the cache engine Cache Engine;
[0027] Resource tracking will record here how many lines are actually read.
[0028] Furthermore, the resource tracker ResourceTracker is used to periodically trigger resource information (CPU and memory) sampling, periodically trigger data volume log storage, and start and stop resource tracking; when the sampling time arrives, the resource tracker ResourceTracker periodically triggers resource information sampling, and after the recording is completed, the resource information is stored in the disk and recorded in the resource collection log file hardware.log in CSV format; the resource tracker ResourceTracker periodically triggers data volume log storage, at which time, the information recorded by all thread resource recorders is collected, and the data access volume information (user ID, rootQueryId, database name, data table name) is aggregated once, in order to reduce the number of logs written, and finally recorded in the access log file access.log in CSV format;
[0029] To facilitate operation and maintenance, resource tracking needs to automatically split log files and regularly recycle obsolete log files. When the resource tracker ResourceTracker triggers sampling and storage at a scheduled time, it first checks whether the resource sampling log file hardware.log and access log file access.log exist. If they do not exist, it creates the file and writes the CSV file header. After sampling and storage are completed, the resource tracker ResourceTracker checks whether the log file exceeds the threshold (1GB by default) and performs log splitting. When the log needs to be split, the resource tracker ResourceTracker renames the current log file to a log file with a timestamp as the prefix.
[0030] A method for split-account billing based on user-level resource tracking, which performs user-level split-account billing based on the statistical results of user resource usage and data access volume counted by the method for split-account billing based on user-level resource tracking.
[0031] Furthermore, a user resource acquisition unit and an access record acquisition unit are constructed. Through the user resource acquisition unit, the log records of CPU usage and memory usage of the node corresponding to the user are queried according to the time range, and the sampling results split into multiple files are collected; through the user access record acquisition unit, the SQL table access log records of the node corresponding to the user are queried according to the time range.
[0032] The advantages and beneficial effects of the present invention are:
[0033] The present invention proposes a split-account billing method based on user-level resource tracking, aiming to solve the challenges of the current database system DolphinDB in split-account billing. The method records the resource consumption of each user, including CPU, memory usage and query data volume, to achieve user-level resource consumption tracking. This method can not only ensure the reasonable allocation and utilization of resources, but also provide users with fair billing information, thereby improving the observability and operational efficiency of the system. By introducing the method of the present invention, the database system DolphinDB can better meet the needs of financial institutions. BRIEF DESCRIPTION OF THE DRAWINGS
[0034] Figure 1 is a flow chart of a method in an embodiment of the present invention. DETAILED DESCRIPTION
[0035] 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.
[0036] In the embodiment of the present invention, according to different resources, tracking is divided into three types: one is the CPU usage, which is tracked by periodically sampling the Worker; the other is the memory usage, which is tracked by periodically sampling the size of the user-created variables in the Heap; the last is the data access volume of the SQL query, which is divided into the number of SQL queries initiated, the number of data rows actually accessed by the SQL query, and the size of the data actually accessed by the SQL query. Their tracking is achieved by recording the information in the execution engine and the storage engine.
[0037] The CPU usage is tracked by sampling. The relevant designs inside the database system DolphinDB are as follows:
[0038] First, the script initiated by the user will be received and executed by the Worker. There are multiple threads inside DolphinDB, among which Worker is a general term for different types of threads that execute requests. Workers can be divided into nearly 10 types of Workers according to the number of distributed request layers, network protocols, and urgency. The CPU usage is essentially the number of threads occupied by the user, so the CPU sampling method is to sample the number of Workers occupied by each user at the current moment. Specifically, before the Worker executes the user request, the present invention first records the user ID, and when the Worker execution is completed, the record of the user ID is canceled. In this way, the user IDs recorded by all Workers can be scanned during timed sampling, thereby recording the CPU usage at the user level.
[0039] Memory usage is also tracked through sampling. Similar to CPU resource tracking, memory tracking also requires recording the user's Heap when the Worker starts processing requests. Heap is a concept implemented internally by DolphinDB. Part of its function is to record variables created by users. If a variable needs to be obtained during the execution of a DolphinDB script, the executor can retrieve the corresponding C++ object from the Heap. After the Worker records the user's heap, the scheduled sampling process can scan all variables in the Heap and calculate the memory allocation size of each object to count the user's memory usage at that moment.
[0040] The data access volume of SQL query is divided into the number of SQL queries initiated, the number of data rows actually accessed by the SQL query, and the size of the data actually accessed by the SQL query. Considering that the execution of SQL is a distributed process, the present invention divides the record of data access volume into two parts, one part is the number of times the SQL query is initiated, and the other part is the number of rows and data actually accessed by the query. Among them, the execution process of a distributed SQL query is when a user initiates an SQL query on the client, this SQL query request will be sent to a data node or computing node in the cluster. On this node (hereinafter referred to as the client node), an SQL query will be split into multiple single-partition queries according to the partition situation. These single-partition queries will be distributed to each data node for execution. Finally, the client node will process the query results of these single partitions and merge them into the final user query results. For the record of initiating SQL query, the process of initiating SQL query occurs on the client node, so the present invention should record the information of initiating SQL here, including time, user ID, database name, data table name, rootQueryID and SQL query script, where rootQueryID is a UUID (Universally UniqueIdentifier), which is used to associate all queries split from the same SQL query. If there is no rootQueryID, it is impossible to know which SQL query a split query is obtained by splitting. For the record of SQL query data volume, the query for a single partition will be distributed to each data node. The present invention will record the access data volume in the storage engine corresponding to each partition on each data node. Since the database system DolphinDB supports multiple storage engines, the resource isolation solution must also consider how to obtain access volume information from each storage engine.
[0041] The above recorded information will be recorded on the disk in the form of logs. In order to reduce the performance overhead of writing to the disk, the log is written in the background and needs to be written in batches regularly.
[0042] The design requirements met by the resource tracking method of the present invention are as follows:
[0043] 1. Ability to maintain user ID and user Heap before and after Worker starts executing user requests;
[0044] 2. A script that can record the time, user ID, database name, table name, rootQueryID, and SQL query on the node that initiated the SQL query;
[0045] 3. Ability to record time, user ID, database name, data table name, rootQueryID, number of accessed rows and accessed data size on the data node.
[0046] In addition, considering performance, usability, operation and maintenance, the solution should also meet the following requirements:
[0047] 4. The performance overhead of resource tracking needs to be strictly controlled so as not to affect the performance of normal query, write and other operations.
[0048] 5. It is necessary to support online start and stop functions to avoid performance degradation and impact on other modules.
[0049] 6. It is necessary to provide a convenient method for querying user-level resource access volume.
[0050] 7. Space needs to be reclaimed regularly to avoid the problem of unlimited growth of records generated by resource tracking.
[0051] like Figure 1 As shown, a method for split-account billing based on user-level resource tracking includes the following steps:
[0052] Step S1: construct a resource tracking and recording unit;
[0053] There are two core structures of resource tracking, resource tracker ResourceTracker and thread local resource recorder ThreadLocalResourceRecorder. The design focuses on low code intrusion and low performance overhead. The two core structures of the present invention are introduced below around these two points; ResourceTracker is used to regularly trigger CPU and memory sampling, regularly trigger data volume log storage, and start and stop resource tracking. When the sampling time arrives, ResourceTracker will obtain the CPU, memory and access volume information of each thread from ThreadLocalResourceRecorder; ThreadLocalResourceRecorder is a thread local object used to collect CPU, memory and data access volume information in each thread. When a thread needs to record the above information, ThreadLocalResourceRecorder will be generated;
[0054] Step S2: Based on the user request, resource sampling and data access volume recording are performed through the resource tracking and recording unit, which specifically includes the following steps:
[0055] Step S2.1: CPU and memory sampling, the process is as follows:
[0056] Whenever a user request is distributed to a Worker for execution, ThreadLocalResourceRecorder will record the Session of this user request. Session is a user session that records information including user ID and user Heap. After the Worker is executed, ThreadLocalResourceRecorder will clear the Session record of this thread.
[0057] When the sampling time arrives, ResourceTracker will trigger CPU and memory sampling at a fixed time. At this time, it will obtain sessions from all ThreadLocalResourceRecorders. Next, it will count the number of sessions as CPU usage according to the user ID, and count all variables in each Heap as memory usage. Finally, it will write the statistical information to the disk and record it in the hardware.log log in CSV format.
[0058] Memory sampling can also be achieved by customizing malloc to record the user ID when malloc allocates memory, but this makes the solution very complicated and requires a lot of manpower.
[0059] The log sample is as follows:
[0060] timestamp,userId,cpu,memory
[0061] 2023.12.15T08:38:17.219438396,guest,0,32
[0062] 2023.12.15T08:38:17.219438396,admin,0,16
[0063] 2023.12.15T08:38:18.219753330,guest,0,32
[0064] 2023.12.15T08:38:18.219753330,admin,0,16
[0065] 2023.12.15T08:38:19.219972866,guest,0,32
[0066] 2023.12.15T08:38:19.219972866,admin,0,16
[0067] 2023.12.15T08:38:20.220196488,guest,0,32
[0068] 2023.12.15T08:38:20.220196488,admin,0,16
[0069] 2023.12.15T08:38:21.220409418,guest,0,32
[0070] The meaning of each column is:
[0071] The first column: NANOTIMESTAMP type timestamp, converted to local time using localtime;
[0072] Second column: User name admin or guest;
[0073] The third column: CPU usage, which refers to the number of workers currently occupied by the user. In the above example, both are 0.
[0074] The fourth column: memory usage, specifically refers to the size of all variables in the current user's Heap, in bytes.
[0075] Step S2.2: Recording of SQL data access volume. The specific process is as follows:
[0076] When a user initiates a SQL query, ThreadLocalResourceRecorder records the current time, user ID, rootQueryID, and SQL query script at the SQL query function entry.
[0077] When a data node executes a split single partition query, it records the query in different ways depending on the current storage engine:
[0078] For OLAP engine: There are three logics for OLAP engine to obtain data:
[0079] 1. Obtain data by scanning documents;
[0080] 2. Read the partition cache through CHUNK_CACHE_ENGINE to obtain data;
[0081] 3. Get data through column cache;
[0082] Resource tracking will record the number of rows actually read in the above three cases, and will not record other cases, such as reading rows from metadata.
[0083] For TSDB engine: There are two logics for TSDB engine to obtain data:
[0084] 1. Obtain data by scanning documents;
[0085] 2. Get data through Cache Engine;
[0086] Resource tracking will record the number of rows actually read here. Other situations, such as reading pre-aggregate results (such as sum) from Zone Map, will not be recorded.
[0087] For PKEY engine: The logic of PKEY engine to obtain data is:
[0088] 1. Obtain data by scanning documents;
[0089] 2. Get data through Cache Engine;
[0090] Resource tracking will record here how many lines are actually read.
[0091] The above data access information will be recorded in the internal storage of ThreadLocalResourceRecorder. The access information will be exposed to ResourceTracker only after the query of this partition is completed. This is because if ResourceTracker triggers logging during the query process, some data will be written to the log, causing inconsistent information. For example, if the query accesses a total of 100 rows in columns A and B, the number of rows and data volume of column A are recorded in ThreadLocalResourceRecorder. If ResourceTracker writes the log at this time, it will record that the table has accessed 100 rows, and then ThreadLocalResourceRecorder will record that column B has accessed 100 rows. In this way, when the information of column B is written to the disk, the resource tracking result will be incorrect because it records that a certain table has accessed 200 rows.
[0092] ResourceTracker periodically triggers the writing of data volume logs to disk. At this time, it collects all the record information of ThreadLocalResourceRecorder and aggregates it according to user ID, rootQueryId, database name, and data table name to reduce the number of logs written. Finally, it will be recorded in the access.log file in CSV format.
[0093] The log sample is as follows:
[0094] timestamp,rootQueryId,userId,database,table,type,value,script
[0095] 2023.12.15T09:53:52.806155145,314d9a90-8111-bf90-3443-671fd71a7d82,admin,dfs: / / test,pt,sql,1,"select timestamp,price * count as price_mul fromej(pt,dt,(pt.sym),(dt.sym),,) where timestamp<2001.01.01"
[0096] 2023.12.15T09:53:52.806155145,314d9a90-8111-bf90-3443-671fd71a7d82,admin,dfs: / / test,dt,sql,1,"select timestamp,price * count as price_mul fromej(pt,dt,(pt.sym),(dt.sym),,) where timestamp<2001.01.01"
[0097] 2023.12.15T09:53:52.950018450,314d9a90-8111-bf90-3443-671fd71a7d82,admin,dfs: / / test,pt,rowCount,46080,""
[0098] 2023.12.15T09:53:52.950018450,314d9a90-8111-bf90-3443-671fd71a7d82,admin,dfs: / / test,pt,memUsage,1476352,""
[0099] 2023.12.15T09:53:52.952316907,314d9a90-8111-bf90-3443-671fd71a7d82,admin,dfs: / / test,dt,rowCount,3,""
[0100] 2023.12.15T09:53:52.952316907,314d9a90-8111-bf90-3443-671fd71a7d82,admin,dfs: / / test,dt,memUsage,332,""
[0101] 2023.12.15T09:53:52.950068788,314d9a90-8111-bf90-3443-671fd71a7d82,admin,dfs: / / test,pt,rowCount,480960,""
[0102] 2023.12.15T09:53:52.950068788,314d9a90-8111-bf90-3443-671fd71a7d82,admin,dfs: / / test,pt,memUsage,15409424,""
[0103] The meaning of each column is:
[0104] The first column (timestamp): a timestamp of the NANOTIMESTAMP type, which is the end time of data reading;
[0105] The second column (rootQueryId): the root ID of the SQL query. A distributed SQL query is split into multiple SQL queries by partition, and these SQL queries have the same root ID.
[0106] The third column (userId): user name;
[0107] The fourth column (database): database name;
[0108] Fifth column (table): table name;
[0109] The sixth column (type): record type; divided into sql, rowCount, memUsage;
[0110] The seventh column (value): the recorded value;
[0111] When the type is sql, the execution times of the root SQL are recorded (always 1);
[0112] When the type is rowCount, the number of rows read is recorded;
[0113] When the type is memUsage, the amount of data in the table read is recorded;
[0114] The eighth column (script): script string; when the type is SQL, it records the SQL script; otherwise, it is an empty string.
[0115] The amount of data can also be reduced by rewriting the file scanning method, recording user ID, library name, table name and other information during file scanning, but this will make the code difficult to maintain and not flexible enough.
[0116] Step S2.3: Log segmentation and recycling;
[0117] The sampled and recorded information will be written into two types of logs. The estimated data volume of the two logs is as follows:
[0118] 1. CPU and memory logs: With a sampling interval of 30 seconds, 100 users will generate an estimated 4 GB of logs per year.
[0119] 2. SQL query logs: Assuming that 300 single-table SQL queries occur in 1 second, an estimated 3 TB of logs are generated in a year;
[0120] To facilitate operation and maintenance, resource tracking needs to automatically split log files and regularly recycle obsolete log files. When ResourceTracker triggers sampling and storage at a scheduled time, it will first check whether hardware.log and access.log exist. If they do not exist, it will create files and write CSV file headers. After sampling and storage are completed, it will check whether the log file exceeds the threshold (default 1GB) and perform log segmentation. When the log needs to be split, the current log will be renamed to a file with a timestamp as the prefix.
[0121] Step S3: According to the sampling and recording results, the user's resource usage and data access records are counted, and user-level account splitting and billing are performed based on the statistical results.
[0122] In order to facilitate the query of resource usage, the present invention designs two DolphinDB functions: getUserResourceRecords and getUserSQLRecords. The user statistical resources first obtain the user's CPU memory usage and SQL query data volume records through the getUserResourceRecords and getUserSQLRecords functions, and then perform SQL query on the results. Finally, user-level account splitting and billing are performed based on the statistical results.
[0123] 1. getUserHardwareUsage function:
[0124] Query the CPU and memory usage logs of this node by time range [from, to). This function collects sampling results that are split into multiple files and can only be called by administrator users.
[0125] Function definition:
[0126] getUserHardwareUsage([from=0], [to])
[0127] from: the starting time point of the query, similar to the nanotimestamp parameter, must be an integer scalar or a time scalar. The default value is 0, which means querying records starting from 00:00 on January 1, 1970.
[0128] to: The end time point of the query. It must be an integer scalar or a time scalar. The default value is empty, indicating that the records so far are queried. from must be less than or equal to to.
[0129] Function output:
[0130] Returns the table after loadText of the above log within the time range, including timestamp, user name, CPU usage and memory usage. The table structure is NANOTIMESTAMP, STRING, LONG, LONG.
[0131] 2. getUserTableAccessRecords function
[0132] Query the table access records of this node by time range [from, to). Only administrator users can call this function.
[0133] Function definition:
[0134] getUserTableAccessRecords([from=0], [to])
[0135] from: the starting time point of the query, which must be an integer scalar or a time scalar. The default value is 0, which means querying records starting from 00:00 on January 1, 1970;
[0136] to: The end time point of the query, which must be an integer scalar or a time scalar. The default value is empty, indicating that the records so far are queried. from must be less than or equal to to.
[0137] Function output:
[0138] Returns the table after loadText of the above log within the time range, including timestamp, SQL query root ID, user name, library name, table name, record type, record value and script. The table structure is NANOTIMESTAMP, UUID, STRING, STRING, STRING, STRING, LONG, STRING.
[0139] 3. Statistical Examples
[0140] Example 1: Count the user's CPU and memory usage:
[0141] select sum(cpu), sum(memory) from pnodeRun(getUserHardwareUsage{2023.12.13}) group by userId
[0142] The above SQL will return the following results:
[0143] userId sum_cpu sum_memory admin 11 9,729,019,616 guest 0 7,968
[0144] Example 2: Count the query data volume of the distributed table:
[0145] dataUsage = pnodeRun(getUserTableAccessRecords{2023.12.13})
[0146] query = select distinct script, rootQueryId, timestamp from dataUsagewhere type="sql"
[0147] stats = select sum(value) from dataUsage d inner join query q ond.rootQueryId = q.rootQueryId where type!="sql" pivot by q.timestamp, userId,database, table, q.script, type
[0148] stats.reorderColumns!(`timestamp`userId`database`table`rowCount`memUsage`script)
[0149] The above script will return the following results:
[0150] timestamp userId database table memUsage rowCount script 2023.12.15T17:53:52.806155145 admin dfs: / / test dt 332 3 selecttimestamp,price *count asprice_mulfrom ej(pt,dt,(pt.sym),(dt.sym),,) wheretimestamp<2001.01.01 2023.12.15T17:53:52.806155145 admin dfs: / / test pt 16,885,776 527,040 selecttimestamp,price *count asprice_mulfrom ej(pt,dt,(pt.sym),(dt.sym),,) wheretimestamp<2001.01.01
[0151] Example 3: Number of database table queries:
[0152] select count(*) from pnodeRun(getUserTableAccessRecords{2023.12.13})where type="sql" group by userId, database, table
[0153] The above SQL will return the following results:
[0154] userId database table count admin dfs: / / test dt 5 admin dfs: / / test pt 3
[0155] Performance analysis based on the method of the present invention:
[0156] When resource tracking is not enabled, when SQL is initiated and data is read from a partition, a static variable is read once to determine whether resource tracking is enabled. When resource tracking is enabled, for CPU and memory sampling, each sampling will compete with the creation and deletion of variables in the Heap; for recording access to distributed tables, all data is stored in TLS (Thread Local Storage), and each record will compete with the background thread's regular disk writes, so a SQL will request as little memory as possible.
[0157] 1. The performance impact of CPU and memory sampling; the specific implementation of sampling is:
[0158] 1) When a Worker starts to execute a user's request, it records the current session in TLS. When the Worker finishes executing, it sets the TLS session to empty.
[0159] 2) When the sampling time arrives, traverse the session of each thread: count how many threads a user uses, and count how much memory the user uses through getHeap()->getAllocatedMemory(); getAllocatedMemory will obtain the lock of Heap::HeapMeta::mutex_ to count the memory usage, and release it immediately after calculation; it will compete with the Heap to add and delete variables, and is not frequent;
[0160] 3) Write to the log.
[0161] 2. Record the performance impact of SQL access to distributed tables. The specific implementation is as follows: each thread independently collects data of the same user and aggregates it into TLS memory. When the sampling time arrives, the background thread will traverse all threads and write them into logs in batches.
[0162] The following discussion of the performance impact is divided into two parts: collecting data into memory and writing logs to disk.
[0163] Data collection process: When each thread executes SQL, it will be divided into two different processing logics depending on whether the query is a root query:
[0164] 1) When a user initiates an SQL query, a table access of type==sql is recorded;
[0165] The number of log records in a single SQL query is equal to the number of tables involved in the SQL; each record will acquire a lock and copy some basic types and strings; among them, there will be a lock competition when the sampling time arrives, and only the string involves memory application during the copying process;
[0166] 2) When the storage engine reads data, it records the amounts of rowCount and memUsage once;
[0167] Each record will acquire a lock and copy some basic types and strings; in order to reduce the amount of log output, an aggregation of <rootId, username, database name, table name> will be done here; the lock competition in this process is the same as above and is not frequent;
[0168] 3) The process of writing logs: when the sampling time arrives, the background thread acquires the lock, retrieves the records from each thread, releases the lock and writes the logs.
[0169] The above embodiments are only used to illustrate the technical solutions of the present invention and are not intended to limit them; although the present invention has been described in detail with reference to the foregoing embodiments, those of ordinary skill in the art should understand that: they can still modify the technical solutions recorded in the foregoing embodiments, or perform equivalent replacements on some or all of the technical features; and these modifications or replacements do not make the essence of the corresponding technical solutions deviate from the scope of the technical solutions of the embodiments of the present invention.
Claims
1. A method based on user-level resource tracking, characterized in that The steps include: Step S1: construct a resource tracking and recording unit, including a resource tracker and a thread resource recorder. The thread resource recorder is used to collect resource information and data access information in each thread, and the resource tracker collects statistics on the information collected by the thread resource recorder. Step S2: Based on the user request, the resource tracking and recording unit performs corresponding resource information sampling and data access information recording; before the user request is distributed to the database thread for execution, the thread resource recorder records the session information of the user request; the resource tracker obtains the session information from all thread resource recorders to record the corresponding resource information; The thread resource recorder obtains data access information according to user requests, and the resource tracker obtains and records the corresponding data access information from all thread resource recorders; The user request includes an SQL query initiated by the user. The thread resource recorder records the current query related information from the SQL query, including the root query ID. For the distributed execution process of the SQL query, the SQL query request initiated by the user will be sent to a node in the cluster. On the current node, the SQL query will be split into multiple single-partition queries according to the partition situation, and the data access volume information of the split single-partition queries will be recorded. These single-partition queries will be distributed to various nodes for execution. Finally, all queries split from the same SQL query are associated through the root query ID, and the current node merges the query results into the final user query results. When a data node executes a split single partition query, different recording methods are used depending on the current storage engine: For the OLAP engine, there are three logics for obtaining data: 1) Obtain data by scanning documents; 2) Get data by reading the partition cache; 3) Get data through column cache; The resource tracker will record the number of lines actually read in the above three cases; For the TSDB engine, there are two logics for obtaining data: 1) Obtain data by scanning documents; 2) Get data through the cache engine; The resource tracker will record the number of lines actually read in the above two cases; For the PKEY engine, the logic for obtaining data is: 1) Obtain data by scanning documents; 2) Get data through the cache engine; The resource tracker will record the number of lines actually read in the above two cases; Step S3: According to the recorded resource information and data access volume information, statistics are collected on the resource usage and data access volume of the user.
2. A method for user-level resource tracking according to claim 1, characterized in that: The session information includes information of the user ID and the user heap. The resource tracker counts the number of session information as the CPU usage of the user according to the user ID, and counts all variables in each user heap as the memory usage.
3. A method for tracking user-level resources according to claim 2, characterized in that: The resource information includes CPU usage, which is obtained by sampling the number of database threads currently occupied by each user; the user ID is recorded first, and when the database thread is executed, the record of the user ID is cancelled.
4. The method according to claim 2, characterized in that: The resource information includes memory usage. When the database thread starts processing a request, the user heap is recorded. The user heap is used to record variables created by the user. When the database needs to obtain variables during execution, the executor takes out the corresponding object from the user heap. The sampling process of resource information scans all variables in the user heap and calculates the memory allocation size of each object to record the user's memory usage at that moment.
5. The method according to claim 1, characterized in that: The data access volume information is recorded in the thread resource recorder. When the query of this partition is completed, the data access volume information will be displayed to the resource tracker.
6. The method according to claim 1, characterized in that: The resource tracker is used to regularly trigger resource information sampling, regularly trigger the storage of data volume logs, and start and stop resource tracking; when the sampling time is reached, the resource tracker regularly triggers resource information sampling, and after the recording is completed, the resource information is stored on the disk and recorded in the resource collection log file; The resource tracker periodically triggers the flushing of data volume logs. At this time, it collects the information recorded by all thread resource recorders, aggregates the data access volume information, and records it in the access log file; splits the log files and regularly recycles obsolete log files. When the resource tracker periodically triggers sampling and flushing, it first checks whether the resource sampling log file and access log file exist. If they do not exist, it creates the file; after sampling and flushing are completed, the resource tracker checks whether the log file exceeds the threshold and splits the log; when the log needs to be split, the resource tracker renames the current log file to a log file with a timestamp as the prefix.
7. A method for split-account billing based on user-level resource tracking, characterized in that: According to the user resource usage and data access volume counted based on the user-level resource tracking method as described in any one of claims 1 to 6, user-level account splitting and billing are performed based on the statistical results.
8. A method for split-account billing based on user-level resource tracking according to claim 7, characterized in that: Construct a user resource acquisition unit and an access record acquisition unit. Through the user resource acquisition unit, query the log records of the CPU usage and memory usage of the node corresponding to the user by time range, and collect the sampling results split into multiple files; Through the user access record acquisition unit, the SQL table access log record of the node corresponding to the user is queried according to the time range.
