|
| 1 | +/* |
| 2 | + * Copyright 2023 Greptime Team |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.greptime; |
| 18 | + |
| 19 | +import static org.junit.Assert.assertEquals; |
| 20 | +import io.greptime.models.DataType; |
| 21 | +import io.greptime.models.Table; |
| 22 | +import io.greptime.models.TableSchema; |
| 23 | +import java.sql.Connection; |
| 24 | +import org.junit.After; |
| 25 | +import org.junit.AfterClass; |
| 26 | +import org.junit.Before; |
| 27 | +import org.junit.BeforeClass; |
| 28 | +import org.junit.Test; |
| 29 | +import org.slf4j.Logger; |
| 30 | +import org.slf4j.LoggerFactory; |
| 31 | + |
| 32 | +/** |
| 33 | + * Integration tests for Bulk Write API. |
| 34 | + */ |
| 35 | +public class BulkWriteIT { |
| 36 | + |
| 37 | + private static final Logger LOG = LoggerFactory.getLogger(BulkWriteIT.class); |
| 38 | + private static final int ROW_COUNT = 100; |
| 39 | + |
| 40 | + private static GreptimeDB client; |
| 41 | + private static Connection jdbcConn; |
| 42 | + |
| 43 | + private String tableName; |
| 44 | + private TableSchema schema; |
| 45 | + |
| 46 | + @BeforeClass |
| 47 | + public static void setupClass() throws Exception { |
| 48 | + client = ITHelper.createClient(); |
| 49 | + jdbcConn = ITHelper.getJdbcConnection(); |
| 50 | + LOG.info("Integration test client initialized"); |
| 51 | + } |
| 52 | + |
| 53 | + @AfterClass |
| 54 | + public static void teardownClass() { |
| 55 | + if (client != null) { |
| 56 | + client.shutdownGracefully(); |
| 57 | + } |
| 58 | + if (jdbcConn != null) { |
| 59 | + try { |
| 60 | + jdbcConn.close(); |
| 61 | + } catch (Exception e) { |
| 62 | + LOG.warn("Failed to close JDBC connection", e); |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + @Before |
| 68 | + public void setup() throws Exception { |
| 69 | + tableName = ITHelper.uniqueTableName("test_bulk_write"); |
| 70 | + schema = TableSchema.newBuilder(tableName) |
| 71 | + .addTag("host", DataType.String) |
| 72 | + .addTimestamp("ts", DataType.TimestampMillisecond) |
| 73 | + .addField("cpu_user", DataType.Float64) |
| 74 | + .addField("cpu_sys", DataType.Float64) |
| 75 | + .build(); |
| 76 | + |
| 77 | + // Bulk Write requires pre-existing table, create via DDL |
| 78 | + String createTableSql = String.format( |
| 79 | + "CREATE TABLE %s (" |
| 80 | + + "host STRING," |
| 81 | + + "ts TIMESTAMP(3) TIME INDEX," |
| 82 | + + "cpu_user DOUBLE," |
| 83 | + + "cpu_sys DOUBLE," |
| 84 | + + "PRIMARY KEY (host)" |
| 85 | + + ")", |
| 86 | + tableName); |
| 87 | + ITHelper.executeUpdate(jdbcConn, createTableSql); |
| 88 | + LOG.info("Created table: {}", tableName); |
| 89 | + } |
| 90 | + |
| 91 | + @After |
| 92 | + public void teardown() { |
| 93 | + ITHelper.dropTableIfExists(jdbcConn, tableName); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + public void testBulkWrite() throws Exception { |
| 98 | + BulkWrite.Config cfg = BulkWrite.Config.newBuilder() |
| 99 | + .allocatorInitReservation(0) |
| 100 | + .allocatorMaxAllocation(64 * 1024 * 1024L) |
| 101 | + .timeoutMsPerMessage(30000) |
| 102 | + .maxRequestsInFlight(4) |
| 103 | + .build(); |
| 104 | + |
| 105 | + try (BulkStreamWriter writer = client.bulkStreamWriter(schema, cfg)) { |
| 106 | + // Prepare test data with deterministic values |
| 107 | + Table.TableBufferRoot table = writer.tableBufferRoot(128); |
| 108 | + long baseTs = 1700000000000L; |
| 109 | + |
| 110 | + for (int i = 0; i < ROW_COUNT; i++) { |
| 111 | + String host = "host_" + i; |
| 112 | + long ts = baseTs + i * 1000; |
| 113 | + double cpuUser = i * 0.1; |
| 114 | + double cpuSys = i * 0.05; |
| 115 | + table.addRow(host, ts, cpuUser, cpuSys); |
| 116 | + } |
| 117 | + table.complete(); |
| 118 | + |
| 119 | + // Write data |
| 120 | + Integer affectedRows = writer.writeNext().get(); |
| 121 | + LOG.info("Bulk write affected rows: {}", affectedRows); |
| 122 | + |
| 123 | + // Verify exact row count, not just > 0 |
| 124 | + assertEquals("Should write exact row count", ROW_COUNT, affectedRows.intValue()); |
| 125 | + |
| 126 | + writer.completed(); |
| 127 | + } |
| 128 | + |
| 129 | + // Verify row count via JDBC |
| 130 | + int count = ITHelper.queryCount(jdbcConn, tableName); |
| 131 | + assertEquals("Row count should match", ROW_COUNT, count); |
| 132 | + |
| 133 | + // Verify actual data content - spot check first, middle, and last rows |
| 134 | + ITHelper.verifyRow(jdbcConn, tableName, "host_0", 0.0, 0.0); |
| 135 | + ITHelper.verifyRow(jdbcConn, tableName, "host_50", 5.0, 2.5); |
| 136 | + ITHelper.verifyRow(jdbcConn, tableName, "host_99", 9.9, 4.95); |
| 137 | + |
| 138 | + LOG.info("Verified {} rows with correct data in table {}", count, tableName); |
| 139 | + } |
| 140 | + |
| 141 | + @Test |
| 142 | + public void testBulkWriteMultipleBatches() throws Exception { |
| 143 | + BulkWrite.Config cfg = BulkWrite.Config.newBuilder() |
| 144 | + .allocatorInitReservation(0) |
| 145 | + .allocatorMaxAllocation(64 * 1024 * 1024L) |
| 146 | + .timeoutMsPerMessage(30000) |
| 147 | + .maxRequestsInFlight(4) |
| 148 | + .build(); |
| 149 | + |
| 150 | + int batchCount = 5; |
| 151 | + int rowsPerBatch = 20; |
| 152 | + int totalWritten = 0; |
| 153 | + |
| 154 | + try (BulkStreamWriter writer = client.bulkStreamWriter(schema, cfg)) { |
| 155 | + long baseTs = 1700000000000L; |
| 156 | + |
| 157 | + for (int batch = 0; batch < batchCount; batch++) { |
| 158 | + Table.TableBufferRoot table = writer.tableBufferRoot(64); |
| 159 | + |
| 160 | + for (int i = 0; i < rowsPerBatch; i++) { |
| 161 | + int rowNum = batch * rowsPerBatch + i; |
| 162 | + String host = "host_" + rowNum; |
| 163 | + long ts = baseTs + rowNum * 1000; |
| 164 | + double cpuUser = rowNum * 0.1; |
| 165 | + double cpuSys = rowNum * 0.05; |
| 166 | + table.addRow(host, ts, cpuUser, cpuSys); |
| 167 | + } |
| 168 | + table.complete(); |
| 169 | + |
| 170 | + Integer affectedRows = writer.writeNext().get(); |
| 171 | + // Verify each batch writes exact count |
| 172 | + assertEquals("Batch " + batch + " should write exact count", rowsPerBatch, affectedRows.intValue()); |
| 173 | + totalWritten += affectedRows; |
| 174 | + LOG.info("Batch {} wrote {} rows", batch, affectedRows); |
| 175 | + } |
| 176 | + |
| 177 | + writer.completed(); |
| 178 | + } |
| 179 | + |
| 180 | + // Verify total written count |
| 181 | + int expectedCount = batchCount * rowsPerBatch; |
| 182 | + assertEquals("Total written should match", expectedCount, totalWritten); |
| 183 | + |
| 184 | + // Verify row count via JDBC |
| 185 | + int count = ITHelper.queryCount(jdbcConn, tableName); |
| 186 | + assertEquals("Row count should match", expectedCount, count); |
| 187 | + |
| 188 | + // Verify actual data content across batches |
| 189 | + ITHelper.verifyRow(jdbcConn, tableName, "host_0", 0.0, 0.0); // first row of batch 0 |
| 190 | + ITHelper.verifyRow(jdbcConn, tableName, "host_40", 4.0, 2.0); // first row of batch 2 |
| 191 | + ITHelper.verifyRow(jdbcConn, tableName, "host_99", 9.9, 4.95); // last row |
| 192 | + |
| 193 | + LOG.info("Verified {} rows with correct data in table {}", count, tableName); |
| 194 | + } |
| 195 | +} |
0 commit comments