Hello I'm trying to use the flink connector to loa...
# questions-and-troubleshooting
j
Hello I'm trying to use the flink connector to load data in a batch job. I see that the data is loaded but then I cannot see the data with the mysql client. I set up a minimal example:
Copy code
import com.starrocks.connector.flink.StarRocksSink;
import com.starrocks.connector.flink.table.sink.StarRocksSinkOptions;
import lombok.Data;
import lombok.val;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.configuration.RestOptions;
import org.apache.flink.runtime.testutils.MiniClusterResourceConfiguration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.DataTypes;
import org.apache.flink.table.api.TableSchema;
import org.apache.flink.test.junit5.MiniClusterExtension;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.testcontainers.containers.DockerComposeContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

import java.io.File;
import java.sql.DriverManager;

@Data
@Testcontainers
public class DebugIT {
	@RegisterExtension
	public static final MiniClusterExtension FLINK_CLUSTER = new MiniClusterExtension(
			new MiniClusterResourceConfiguration.Builder()
					.setNumberSlotsPerTaskManager(1)
					.setNumberTaskManagers(1)
					.setConfiguration(new Configuration().set(RestOptions.PORT, 8081))
					.build());

	@Container
	static DockerComposeContainer? environment =
			new DockerComposeContainer<>(new File("db/docker-compose.yml"))
					.withExposedService("starrocks", 9030)
					.withExposedService("starrocks", 8040)
					.withExposedService("starrocks", 8030)
					.withExposedService("starrocks", 9060);

	@BeforeAll
	static void beforeAll() {
		Runtime.getRuntime().addShutdownHook(new Thread(environment::stop));
	}

	@Data
	public static class Pojo {
		long id;
	}

	@Test
	void integrationTest() throws Exception {
		environment.start();

		val jdbcUrl = "jdbc:mysql://localhost:9030";
		val user = "root";
		val password = "";
		try (val con = DriverManager.getConnection(jdbcUrl, user, password);
		     val stmt = con.createStatement()) {
			stmt.executeUpdate("CREATE DATABASE IF NOT EXISTS DB;");
			stmt.executeUpdate("""
					                   CREATE TABLE IF NOT EXISTS DB.TEST
					                   (
					                       ID         BIGINT      NOT NULL
					                   );""");
		}

		val env = StreamExecutionEnvironment.getExecutionEnvironment();

		val stream = env.fromData(new Pojo());

		val schema = TableSchema.builder().field("ID", DataTypes.BIGINT()).build();

		val options = StarRocksSinkOptions
				.builder()
				.withProperty("jdbc-url", jdbcUrl)
				.withProperty("load-url", "localhost:8030")
				.withProperty("table-name", "TEST")
				.withProperty("database-name", "DB")
				.withProperty("username", "root")
				.withProperty("password", "")
				.withProperty("sink.label-prefix", "my-prefix")
				.withProperty("sink.semantic", "exactly-once").build();

		val sink = StarRocksSink.Pojosink(schema, options, (objects, o) -> objects[0] = o.id);

		stream.addSink(sink);
		env.execute();

		try (val con = DriverManager.getConnection(jdbcUrl, user, password);
		     val stmt = con.createStatement()) {
			val result = stmt.executeQuery("select * from DB.TEST");
			Assertions.assertTrue(result.next()); // this assert fails
		}
	}
}
Here is what I see in the output from starrocks:
Copy code
2025-09-05 15:00:45 INFO  StreamLoadManagerV2:351 - Stream load manager flush
2025-09-05 15:00:45 INFO  TransactionTableRegion:269 - Flush db: DB, table: TEST, label: null, cacheBytes: 1, cacheRows: 1, reason: FORCE
2025-09-05 15:00:45 INFO  TransactionTableRegion:409 - Stream load chunk, db: DB, table: TEST, numRows: 1, rowBytes: 1, chunkBytes: 2
2025-09-05 15:00:45 INFO  TransactionStreamLoader:117 - Transaction start, label : my-prefix-TEST-0-1
2025-09-05 15:00:45 INFO  TransactionStreamLoader:133 - Transaction start, db: DB, table: TEST, label: my-prefix-TEST-0-1, request : POST http://localhost:8030/api/transaction/begin HTTP/1.1
2025-09-05 15:00:45 INFO  TransactionStreamLoader:140 - Transaction started, db: DB, table: TEST, label: my-prefix-TEST-0-1, body : {
    "Status": "OK",
    "Message": "",
    "Label": "my-prefix-TEST-0-1",
    "TxnId": 15016,
    "BeginTxnTimeMs": 13
}
2025-09-05 15:00:45 INFO  DefaultStreamLoader:306 - Stream loading, label : my-prefix-TEST-0-1, region : DB-TEST, request : PUT http://localhost:8030/api/transaction/load HTTP/1.1
2025-09-05 15:00:45 INFO  ChunkHttpEntity:104 - Write entity for table DB-TEST, size:2, time:1446us
2025-09-05 15:00:45 INFO  DefaultStreamLoader:314 - Stream load completed, label : my-prefix-TEST-0-1, database : DB, table : TEST, body : {
    "Status": "OK",
    "Message": "",
    "Label": "my-prefix-TEST-0-1",
    "TxnId": 15016,
    "LoadBytes": 2,
    "StreamLoadPlanTimeMs": 50,
    "ReceivedDataTimeMs": 0
}
2025-09-05 15:00:45 INFO  StreamLoadManagerV2:300 - Receive load response, cacheByteBeforeFlush: 1, currentCacheBytes: 0, totalFlushRows : 1
2025-09-05 15:00:45 INFO  TransactionTableRegion:396 - Stream load completed, db: DB, table: TEST, label: my-prefix-TEST-0-1, cacheBytes: 0, cacheRows: 0
2025-09-05 15:00:45 INFO  StarRocksDynamicSinkFunctionV2:248 - Close sink function
2025-09-05 15:00:45 INFO  StreamLoadManagerV2:351 - Stream load manager flush
2025-09-05 15:00:45 INFO  TransactionStreamLoader:318 - Transaction rollback, label : my-prefix-TEST-0-1
2025-09-05 15:00:45 INFO  TransactionStreamLoader:332 - Transaction rollback, label: my-prefix-TEST-0-1, body : {
    "Status": "OK",
    "Message": "",
    "Label": "my-prefix-TEST-0-1"
}
2025-09-05 15:00:45 INFO  StreamLoadManagerV2:412 - StreamLoadManagerV2 close, loadMetrics: LoadMetrics{startTimeNano=889074820011000, totalRunningTimeNano=272783800, numberOfSuccessLoad=1, totalSuccessLoadBytes=1, totalSuccessLoadRows=1, totalSuccessLoadTimeNano=65970100, numberOfFailedLoad=0, numberWriteTriggerFlush=0, numberWriteBlock=0, totalWriteBlockTimeNano=0}, flushAndCommit: FlushAndCommitStrategy{expectDelayTime=300000, scanFrequency=50, ageThreshold=6000, maxCacheBytes=94371840, enableAutoCommit=false, numAgeTriggerFlush=0, numCacheTriggerFlush=0, numTableTriggerFlush=0}
2025-09-05 15:00:45 INFO  DefaultStreamLoader:140 - Default Stream loader closed
Why is there a transaction rollback? Does the sink not work in batch mode because there is no checkpoint in batch mode?
е
reading https://docs.starrocks.io/docs/loading/Flink-connector-starrocks/#usage-notes seems like you are on to somethink it says with exactly-once it commits on checkpoint
j
Okay it does work if I just don't set these options
Copy code
.withProperty("sink.label-prefix", "my-prefix")
.withProperty("sink.semantic", "exactly-once")
very unfortunate if I cannot have exactly once semantics in batch jobs.
е
what semantic do you think it conforms to in batch?
but i agree it should not rollback on successful exit
i think you should file a bug and provide expected behavior, that way there is hope for a patch
j
Well the default from looking at the documentation is at least once. I would assume this means if a batch job experienced a failure and restarted, there might be duplicate data.
I think the intended way is to use streaming mode with checkpointing instead of batch mode, even if I have a batch use case. If I turn on checkpointing it also works with exactly once, so long as I use streaming mode. Looking at the flink documentation "It is important to remember that because there are no checkpoints, certain features such as CheckpointListener and, as a result, Kafka’s EXACTLY_ONCE mode or `File Sink`’s OnCheckpointRollingPolicy won’t work. If you need a transactional sink that works in
BATCH
mode make sure it uses the Unified Sink API as proposed in FLIP-143." So to have a sink which supports exactly once in both streaming and batch mode, it needs to be implemented with the new
Sink
interface. Looking at the starrocks sink, it implements the deprecated
SinkFunction
interface. So I think this is not a bug, just a consequence of the starrocks sink being implemented with the older API.
❤️ 1
е
thanks for digging into this i think it still makes sense to file a feature request with this exert from flink docs