Group Commit
Group commit load does not introduce a new data import method, but an extension of INSERT INTO tbl VALUS(...), Stream Load and Http Stream. It is a way to improve the write performance of Doris with high-concurrency and small-data writes. Your application can directly use JDBC to do high-concurrency insert operation into Doris, at the same time, combining PreparedStatement can get even higher performance. In logging scenarios, you can also do high-concurrency Stream Load or Http Stream into Doris.
Group Commit Mode
Group Commit provides 3 modes:
off_mode
Disable group commit, keep the original behavior for INSERT INTO VALUES, Stream Load and Http Stream.
sync_mode
Doris groups multiple loads into one transaction commit based on the group_commit_interval table property. The load is returned after the transaction commit. This mode is suitable for high-concurrency writing scenarios and requires immediate data visibility after the load is finished.
async_mode
Doris writes data to the Write Ahead Log (WAL) firstly, then the load is returned. Doris groups multiple loads into one transaction commit based on the group_commit_interval table property, and the data is visible after the commit. To prevent excessive disk space usage by the WAL, it automatically switches to sync_mode. This is suitable for latency-sensitive and high-frequency writing.
Basic operations
If the table schema is:
1. CREATE TABLE `dt` (
2. `id` int(11) NOT NULL,
3. `name` varchar(50) NULL,
4. `score` int(11) NULL
5. ) ENGINE=OLAP
6. DUPLICATE KEY(`id`)
7. DISTRIBUTED BY HASH(`id`) BUCKETS 1
8. PROPERTIES (
9. "replication_num" = "1"
10. );
Use JDBC
To reduce the CPU cost of SQL parsing and query planning, we provide the PreparedStatement in the FE. When using PreparedStatement, the SQL and its plan will be cached in the session level memory cache and will be reused later on, which reduces the CPU cost of FE. The following is an example of using PreparedStatement in JDBC:
- Setup JDBC url and enable server side prepared statement
1. url = jdbc:mysql://127.0.0.1:9030/db?useServerPrepStmts=true
-
Set
group_commitsession variable, there are two ways to do it: -
Add
sessionVariables=group_commit=async_modein JDBC url
1. url = jdbc:mysql://127.0.0.1:9030/db?useServerPrepStmts=true&sessionVariables=group_commit=async_mode
- Use
SET group_commit = async_mode;command
1. try (Statement statement = conn.createStatement()) {
2. statement.execute("SET group_commit = async_mode;");
3. }
- Using
PreparedStatement
1. private static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
2. private static final String URL_PATTERN = "jdbc:mysql://%s:%d/%s?useServerPrepStmts=true";
3. private static final String HOST = "127.0.0.1";
4. private static final int PORT = 9087;
5. private static final String DB = "db";
6. private static final String TBL = "dt";
7. private static final String USER = "root";
8. private static final String PASSWD = "";
9. private static final int INSERT_BATCH_SIZE = 10;
11. private static void groupCommitInsert() throws Exception {
12. Class.forName(JDBC_DRIVER);
13. try (Connection conn = DriverManager.getConnection(String.format(URL_PATTERN, HOST, PORT, DB), USER, PASSWD)) {
14. // set session variable 'group_commit'
15. try (Statement statement = conn.createStatement()) {
16. statement.execute("SET group_commit = async_mode;");
17. }
19. String query = "insert into " + TBL + " values(?, ?, ?)";
20. try (PreparedStatement stmt = conn.prepareStatement(query)) {
21. for (int i = 0; i < INSERT_BATCH_SIZE; i++) {
22. stmt.setInt(1, i);
23. stmt.setString(2, "name" + i);
24. stmt.setInt(3, i + 10);
25. int result = stmt.executeUpdate();
26. System.out.println("rows: " + result);
27. }
28. }
29. } catch (Exception e) {
30. e.printStackTrace();
31. }
32. }
34. private static void groupCommitInsertBatch() throws Exception {
35. Class.forName(JDBC_DRIVER);
36. // add rewriteBatchedStatements=true and cachePrepStmts=true in JDBC url
37. // set session variables by sessionVariables=group_commit=async_mode in JDBC url
38. try (Connection conn = DriverManager.getConnection(
39. String.format(URL_PATTERN + "&rewriteBatchedStatements=true&cachePrepStmts=true&sessionVariables=group_commit=async_mode", HOST, PORT, DB), USER, PASSWD)) {
41. String query = "insert into " + TBL + " values(?, ?, ?)";
42. try (PreparedStatement stmt = conn.prepareStatement(query)) {
43. for (int j = 0; j < 5; j++) {
44. // 10 rows per insert
45. for (int i = 0; i < INSERT_BATCH_SIZE; i++) {
46. stmt.setInt(1, i);
47. stmt.setString(2, "name" + i);
48. stmt.setInt(3, i + 10);
49. stmt.addBatch();
50. }
51. int[] result = stmt.executeBatch();
52. }
53. }
54. } catch (Exception e) {
55. e.printStackTrace();
56. }
57. }
See Synchronize Data Using Insert Method for more details about JDBC.
INSERT INTO VALUES
- async_mode
1. # Config session variable to enable the async group commit, the default value is off_mode
2. mysql> set group_commit = async_mode;
4. # The retured label is start with 'group_commit', which is the label of the real load job
5. mysql> insert into dt values(1, 'Bob', 90), (2, 'Alice', 99);
6. Query OK, 2 rows affected (0.05 sec)
7. {'label':'group_commit_a145ce07f1c972fc-bd2c54597052a9ad', 'status':'PREPARE', 'txnId':'181508'}
9. # The returned label and txn_id are the same as the above, which means they are handled in on load job
10. mysql> insert into dt(id, name) values(3, 'John');
11. Query OK, 1 row affected (0.01 sec)
12. {'label':'group_commit_a145ce07f1c972fc-bd2c54597052a9ad', 'status':'PREPARE', 'txnId':'181508'}
14. # The data is not visible
15. mysql> select * from dt;
16. Empty set (0.01 sec)
18. # After about 10 seconds, the data is visible
19. mysql> select * from dt;
20. +------+-------+-------+
21. | id | name | score |
22. +------+-------+-------+
23. | 1 | Bob | 90 |
24. | 2 | Alice | 99 |
25. | 3 | John | NULL |
26. +------+-------+-------+
27. 3 rows in set (0.02 sec)
- sync_mode
1. # Config session variable to enable the sync group commit
2. mysql> set group_commit = sync_mode;
4. # The retured label is start with 'group_commit', which is the label of the real load job.
5. # The insert costs at least the group_commit_interval_ms of table property.
6. mysql> insert into dt values(4, 'Bob', 90), (5, 'Alice', 99);
7. Query OK, 2 rows affected (10.06 sec)
8. {'label':'group_commit_d84ab96c09b60587_ec455a33cb0e9e87', 'status':'PREPARE', 'txnId':'3007', 'query_id':'fc6b94085d704a94-a69bfc9a202e66e2'}
10. # The data is visible after the insert is returned
11. mysql> select * from dt;
12. +------+-------+-------+
13. | id | name | score |
14. +------+-------+-------+
15. | 1 | Bob | 90 |
16. | 2 | Alice | 99 |
17. | 3 | John | NULL |
18. | 4 | Bob | 90 |
19. | 5 | Alice | 99 |
20. +------+-------+-------+
21. 5 rows in set (0.03 sec)
- off_mode
1. mysql> set group_commit = off_mode;
Stream Load
If the content of data.csv is:
1. 6,Amy,60
2. 7,Ross,98
- async_mode
1. # Add 'group_commit:async_mode' configuration in the http header
3. curl --location-trusted -u {user}:{passwd} -T data.csv -H "group_commit:async_mode" -H "column_separator:," http://{fe_host}:{http_port}/api/db/dt/_stream_load
4. {
5. "TxnId": 7009,
6. "Label": "group_commit_c84d2099208436ab_96e33fda01eddba8",
7. "Comment": "",
8. "GroupCommit": true,
9. "Status": "Success",
10. "Message": "OK",
11. "NumberTotalRows": 2,
12. "NumberLoadedRows": 2,
13. "NumberFilteredRows": 0,
14. "NumberUnselectedRows": 0,
15. "LoadBytes": 19,
16. "LoadTimeMs": 35,
17. "StreamLoadPutTimeMs": 5,
18. "ReadDataTimeMs": 0,
19. "WriteDataTimeMs": 26
20. }
22. # The returned 'GroupCommit' is 'true', which means this is a group commit load
23. # The retured label is start with 'group_commit', which is the label of the real load job
- sync_mode
1. # Add 'group_commit:sync_mode' configuration in the http header
3. curl --location-trusted -u {user}:{passwd} -T data.csv -H "group_commit:sync_mode" -H "column_separator:," http://{fe_host}:{http_port}/api/db/dt/_stream_load
4. {
5. "TxnId": 3009,
6. "Label": "group_commit_d941bf17f6efcc80_ccf4afdde9881293",
7. "Comment": "",
8. "GroupCommit": true,
9. "Status": "Success",
10. "Message": "OK",
11. "NumberTotalRows": 2,
12. "NumberLoadedRows": 2,
13. "NumberFilteredRows": 0,
14. "NumberUnselectedRows": 0,
15. "LoadBytes": 19,
16. "LoadTimeMs": 10044,
17. "StreamLoadPutTimeMs": 4,
18. "ReadDataTimeMs": 0,
19. "WriteDataTimeMs": 10038
20. }
22. # The returned 'GroupCommit' is 'true', which means this is a group commit load
23. # The retured label is start with 'group_commit', which is the label of the real load job
See Stream Load for more detailed syntax used by Stream Load.
Http Stream
- async_mode
1. # Add 'group_commit:async_mode' configuration in the http header
3. curl --location-trusted -u {user}:{passwd} -T data.csv -H "group_commit:async_mode" -H "sql:insert into db.dt select * from http_stream('column_separator'=',', 'format' = 'CSV')" http://{fe_host}:{http_port}/api/_http_stream
4. {
5. "TxnId": 7011,
6. "Label": "group_commit_3b45c5750d5f15e5_703428e462e1ebb0",
7. "Comment": "",
8. "GroupCommit": true,
9. "Status": "Success",
10. "Message": "OK",
11. "NumberTotalRows": 2,
12. "NumberLoadedRows": 2,
13. "NumberFilteredRows": 0,
14. "NumberUnselectedRows": 0,
15. "LoadBytes": 19,
16. "LoadTimeMs": 65,
17. "StreamLoadPutTimeMs": 41,
18. "ReadDataTimeMs": 47,
19. "WriteDataTimeMs": 23
20. }
22. # The returned 'GroupCommit' is 'true', which means this is a group commit load
23. # The retured label is start with 'group_commit', which is the label of the real load job
- sync_mode
1. # Add 'group_commit:sync_mode' configuration in the http header
3. curl --location-trusted -u {user}:{passwd} -T data.csv -H "group_commit:sync_mode" -H "sql:insert into db.dt select * from http_stream('column_separator'=',', 'format' = 'CSV')" http://{fe_host}:{http_port}/api/_http_stream
4. {
5. "TxnId": 3011,
6. "Label": "group_commit_fe470e6752aadbe6_a8f3ac328b02ea91",
7. "Comment": "",
8. "GroupCommit": true,
9. "Status": "Success",
10. "Message": "OK",
11. "NumberTotalRows": 2,
12. "NumberLoadedRows": 2,
13. "NumberFilteredRows": 0,
14. "NumberUnselectedRows": 0,
15. "LoadBytes": 19,
16. "LoadTimeMs": 10066,
17. "StreamLoadPutTimeMs": 31,
18. "ReadDataTimeMs": 32,
19. "WriteDataTimeMs": 10034
20. }
22. # The returned 'GroupCommit' is 'true', which means this is a group commit load
23. # The retured label is start with 'group_commit', which is the label of the real load job
See Stream Load for more detailed syntax used by Http Stream.
Group commit condition
The data will be automatically committed either when the time interval (default is 10 seconds) or the data size (default is 64 MB) conditions meet.
Modify the time interval condition
The default group commit interval is 10 seconds. Users can modify the configuration of the table:
1. # Modify the group commit interval to 2 seconds
2. ALTER TABLE dt SET ("group_commit_interval_ms" = "2000");
Modify the data size condition
The default group commit data size is 64 MB. Users can modify the configuration of the table:
1. # Modify the group commit data size to 128MB
2. ALTER TABLE dt SET ("group_commit_data_bytes" = "134217728");
Limitations
-
When the group commit is enabled, some
INSERT INTO VALUESsqls are not executed in the group commit way if they meet the following conditions: -
Transaction insert, such as
BEGIN,INSERT INTO VALUES,COMMIT - Specify the label, such as
INSERT INTO dt WITH LABEL {label} VALUES - Expressions within VALUES, such as
INSERT INTO dt VALUES (1 + 100) - Column update
- Tables that do not support light schema changes
-
When the group commit is enabled, some
Stream LoadandHttp Streamare not executed in the group commit way if they meet the following conditions: -
Two phase commit
- Specify the label by set header
-H "label:my_label" - Column update
- Tables that do not support light schema changes
- For unique table, because the group commit can not guarantee the commit order, users can use sequence column to ensure the data consistency.
-
The limit of
max_filter_ratio -
For non group commit load, filter_ratio is calculated by the failed rows and total rows when load is finished. If the filter_ratio does not match, the transaction will not commit
- In the group commit mode, multiple user loads are executed by one internal load. The internal load will commit all user loads.
- Currently, group commit supports a certain degree of max_filter_ratio semantics. When the total number of rows does not exceed group_commit_memory_rows_for_max_filter_ratio (configured in
be.conf, defaulting to10000rows), max_filter_ratio will work. -
The limit of WAL
-
For async_mode group commit, data is written to the Write Ahead Log (WAL). If the internal load succeeds, the WAL is immediately deleted. If the internal load fails, data is recovery by importing the WAL.
- Currently, WAL files are stored only on one disk of one BE. If the BE’s disk is damaged or the file is mistakenly deleted, it may result in data loss.
- When decommissioning a BE node, please use the DECOMMISSION command to safely decommission the node. This prevents potential data loss if the WAL files are not processed before the node is taken offline.
-
For async_mode group commit writes, to protect disk space, it switches to sync_mode under the following conditions:
- For an import with large amount of data: exceeding 80% of the disk space of a WAL directory.
- Chunked stream loads with an unknown data amount.
- Insufficient disk space, even with it is an import with small amount of data.
- During hard weight schema changes (adding or dropping columns, modifying varchar length, and renaming columns are lightweight schema changes, others are hard weight), to ensure WAL file is compatibility with the table’s schema, the final stage of metadata modification in FE will reject group commit writes. Clients get
insert table ${table_name} is blocked on schema changeexception and can retry the import.
Relevant system configuration
BE configuration
group_commit_wal_path
- The
WALdirectory of group commit. - Default: A directory named
walis created under each directory of thestorage_root_path. Configuration examples:
1. group_commit_wal_path=/data1/storage/wal;/data2/storage/wal;/data3/storage/wal
group_commit_memory_rows_for_max_filter_ratio
- Description: The
max_filter_ratiolimit can only work if the total rows ofgroup commitis less than this value. - Default: 10000
Performance
We have separately tested the write performance of group commit in high-concurrency scenarios with small data volumes using Stream Load and JDBC (in async mode).
Stream Load
Environment
- 1 FE: 8-core CPU, 16 GB RAM, 1 200 GB SSD disk
- 3 BE: 16-core CPU, 64 GB RAM, 1 2 TB SSD disk
- 1 Client: 8-core CPU, 64 GB RAM, 1 100 GB SSD disk
DataSet
httplogs, 31 GB, 247249096 (247 million) rows
Test Tool
Test Method
- Setting different single-concurrency data size and concurrency num between
non group_commitandgroup_commitmodes.
Test Result
| Load Way | Single-concurrency Data Size | Concurrency | Cost Seconds | Rows / Seconds | MB / Seconds |
|---|---|---|---|---|---|
group_commit |
10 KB | 10 | 3707 | 66,697 | 8.56 |
group_commit |
10 KB | 30 | 3385 | 73,042 | 9.38 |
group_commit |
100 KB | 10 | 473 | 522,725 | 67.11 |
group_commit |
100 KB | 30 | 390 | 633,972 | 81.39 |
group_commit |
500 KB | 10 | 323 | 765,477 | 98.28 |
group_commit |
500 KB | 30 | 309 | 800,158 | 102.56 |
group_commit |
1 MB | 10 | 304 | 813,319 | 104.24 |
group_commit |
1 MB | 30 | 286 | 864,507 | 110.88 |
group_commit |
10 MB | 10 | 290 | 852,583 | 109.28 |
non group_commit |
1 MB | 10 | -235 error |
||
non group_commit |
10 MB | 10 | 519 | 476,395 | 61.12 |
non group_commit |
10 MB | 30 | -235 error |
In the above test, the CPU usage of BE fluctuates between 10-40%.
The group_commit effectively enhances import performance while reducing the number of versions, thereby alleviating the pressure on compaction.
JDBC
Environment
- 1 FE: 8-core CPU, 16 GB RAM, 1 200 GB SSD disk
- 1 BE: 16-core CPU, 64 GB RAM, 1 2 TB SSD disk
- 1 Client: 16-core CPU, 64 GB RAM, 1 100 GB SSD disk
DataSet
- The data of tpch sf10
lineitemtable, 20 files, 14 GB, 120 million rows
Test Method
Test Method
- Use
txtfilereaderwtite data tomysqlwriter, config different concurrenncy and rows for perINSERTsql.
Test Result
| Rows per insert | Concurrency | Rows / Second | MB / Second |
|---|---|---|---|
| 100 | 20 | 106931 | 11.46 |
In the above test, the CPU usage of BE fluctuates between 10-20%, FE fluctuates between 60-70%.
