use of org.apache.flink.connector.jdbc.JdbcTestFixture.TEST_DATA in project flink by apache.
the class JdbcTableOutputFormatTest method testDeleteExecutorUpdatedOnReconnect.
/**
* Test that the delete executor in {@link TableJdbcUpsertOutputFormat} is updated when {@link
* JdbcOutputFormat#attemptFlush()} fails.
*/
@Test
public void testDeleteExecutorUpdatedOnReconnect() throws Exception {
// first fail flush from the main executor
boolean[] exceptionThrown = { false };
// then record whether the delete executor was updated
// and check it on the next flush attempt
boolean[] deleteExecutorPrepared = { false };
boolean[] deleteExecuted = { false };
format = new TableJdbcUpsertOutputFormat(new SimpleJdbcConnectionProvider(JdbcConnectorOptions.builder().setDBUrl(getDbMetadata().getUrl()).setTableName(OUTPUT_TABLE).build()) {
@Override
public boolean isConnectionValid() throws SQLException {
// trigger reconnect and re-prepare on flush failure
return false;
}
}, JdbcExecutionOptions.builder().withMaxRetries(1).withBatchIntervalMs(// disable periodic flush
Long.MAX_VALUE).build(), ctx -> new JdbcBatchStatementExecutor<Row>() {
@Override
public void executeBatch() throws SQLException {
if (!exceptionThrown[0]) {
exceptionThrown[0] = true;
throw new SQLException();
}
}
@Override
public void prepareStatements(Connection connection) {
}
@Override
public void addToBatch(Row record) {
}
@Override
public void closeStatements() {
}
}, ctx -> new JdbcBatchStatementExecutor<Row>() {
@Override
public void prepareStatements(Connection connection) {
if (exceptionThrown[0]) {
deleteExecutorPrepared[0] = true;
}
}
@Override
public void addToBatch(Row record) {
}
@Override
public void executeBatch() {
deleteExecuted[0] = true;
}
@Override
public void closeStatements() {
}
});
RuntimeContext context = Mockito.mock(RuntimeContext.class);
ExecutionConfig config = Mockito.mock(ExecutionConfig.class);
doReturn(config).when(context).getExecutionConfig();
doReturn(true).when(config).isObjectReuseEnabled();
format.setRuntimeContext(context);
format.open(0, 1);
format.writeRecord(Tuple2.of(false, /* false = delete*/
toRow(TEST_DATA[0])));
format.flush();
assertTrue("Delete should be executed", deleteExecuted[0]);
assertTrue("Delete executor should be prepared" + exceptionThrown[0], deleteExecutorPrepared[0]);
}
Aggregations