Unverified Commit 71131ffa authored by Zzm0809's avatar Zzm0809 Committed by GitHub

[Optimization] optimizing the construction of cdcsource out of order results in…

[Optimization] optimizing the construction of cdcsource out of order results in incorrect recovery from the savepoint (#3349) Signed-off-by: 's avatarZzm0809 <934230207@qq.com> Co-authored-by: 's avatarZzm0809 <Zzm0809@users.noreply.github.com>
parent fffe53ef
......@@ -65,6 +65,7 @@ import java.time.Instant;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
......@@ -103,6 +104,7 @@ public abstract class AbstractSinkBuilder implements SinkBuilder {
typeConverterList = Lists.newArrayList(
this::convertVarCharType,
this::convertDateType,
this::convertTimeType,
this::convertVarBinaryType,
this::convertBigIntType,
this::convertFloatType,
......@@ -272,15 +274,30 @@ public abstract class AbstractSinkBuilder implements SinkBuilder {
config.getSink().remove("timezone");
if (Asserts.isNotNullString(timeZone)) {
sinkTimeZone = ZoneId.of(timeZone);
logger.info("Sink timezone is {}", sinkTimeZone);
}
final List<Schema> schemaList = config.getSchemaList();
if (Asserts.isNullCollection(schemaList)) {
logger.warn("Schema list is empty, please check your configuration and try again.");
return dataStreamSource;
}
final String schemaFieldName = config.getSchemaFieldName();
if (Asserts.isNotNullCollection(schemaList)) {
SingleOutputStreamOperator<Map> mapOperator = deserialize(dataStreamSource);
for (Schema schema : schemaList) {
for (Table table : schema.getTables()) {
if (Asserts.isNullCollection(schema.getTables())) {
// if schema tables is empty, throw exception
throw new IllegalArgumentException(
"Schema tables is empty, please check your configuration or check your database permission and try again.");
}
// if schema tables is not empty, sort by table name
List<Table> tableList = schema.getTables().stream()
.sorted(Comparator.comparing(Table::getName))
.collect(Collectors.toList());
for (Table table : tableList) {
SingleOutputStreamOperator<Map> filterOperator = shunt(mapOperator, table, schemaFieldName);
List<String> columnNameList = new ArrayList<>();
......@@ -307,8 +324,6 @@ public abstract class AbstractSinkBuilder implements SinkBuilder {
public LogicalType getLogicalType(Column column) {
switch (column.getJavaType()) {
case STRING:
return new VarCharType();
case BOOLEAN:
case JAVA_LANG_BOOLEAN:
return new BooleanType();
......@@ -351,6 +366,7 @@ public abstract class AbstractSinkBuilder implements SinkBuilder {
}
case BYTES:
return new VarBinaryType(Integer.MAX_VALUE);
case STRING:
default:
return new VarCharType();
}
......@@ -456,6 +472,16 @@ public abstract class AbstractSinkBuilder implements SinkBuilder {
return Optional.empty();
}
protected Optional<Object> convertTimeType(Object target, LogicalType logicalType) {
if (logicalType instanceof TimeType) {
return Optional.of(StringData.fromString(Instant.ofEpochMilli((long) target)
.atZone(ZoneId.systemDefault())
.toLocalTime()
.toString()));
}
return Optional.empty();
}
protected Optional<Object> convertVarCharType(Object target, LogicalType logicalType) {
if (logicalType instanceof VarCharType) {
return Optional.of(StringData.fromString((String) target));
......
......@@ -63,11 +63,12 @@ import org.apache.flink.util.Collector;
import org.apache.flink.util.OutputTag;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.stream.Collectors;
/**
* MysqlCDCBuilder
......@@ -128,17 +129,27 @@ public class KafkaSinkBuilder extends AbstractSinkBuilder implements Serializabl
KafkaSink<String> kafkaSink = kafkaSinkBuilder.build();
dataStreamSource.sinkTo(kafkaSink);
} else {
Map<Table, OutputTag<String>> tagMap = new HashMap<>();
Map<String, Table> tableMap = new HashMap<>();
Map<Table, OutputTag<String>> tagMap = new LinkedHashMap<>();
Map<String, Table> tableMap = new LinkedHashMap<>();
ObjectMapper objectMapper = new ObjectMapper();
SingleOutputStreamOperator<Map> mapOperator = dataStreamSource
.map(x -> objectMapper.readValue(x, Map.class))
.returns(Map.class);
final List<Schema> schemaList = config.getSchemaList();
final String schemaFieldName = config.getSchemaFieldName();
if (Asserts.isNotNullCollection(schemaList)) {
for (Schema schema : schemaList) {
for (Table table : schema.getTables()) {
if (Asserts.isNullCollection(schema.getTables())) {
// if schema tables is empty, throw exception
throw new IllegalArgumentException(
"Schema tables is empty, please check your configuration or check your database permission and try again.");
}
// if schema tables is not empty, sort by table name
List<Table> tableList = schema.getTables().stream()
.sorted(Comparator.comparing(Table::getName))
.collect(Collectors.toList());
for (Table table : tableList) {
String sinkTableName = getSinkTableName(table);
OutputTag<String> outputTag = new OutputTag<String>(sinkTableName) {};
tagMap.put(table, outputTag);
......
......@@ -51,7 +51,7 @@ import java.io.Serializable;
import java.math.BigDecimal;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
......@@ -207,6 +207,7 @@ public abstract class AbstractSqlSinkBuilder extends AbstractSinkBuilder impleme
config.getSink().remove("timezone");
if (Asserts.isNotNullString(timeZone)) {
sinkTimeZone = ZoneId.of(timeZone);
logger.info("Sink timezone is {}", sinkTimeZone);
}
final List<Schema> schemaList = config.getSchemaList();
......@@ -218,10 +219,19 @@ public abstract class AbstractSqlSinkBuilder extends AbstractSinkBuilder impleme
logger.info("Build deserialize successful...");
Map<Table, OutputTag<Map>> tagMap = new HashMap<>();
Map<String, Table> tableMap = new HashMap<>();
Map<Table, OutputTag<Map>> tagMap = new LinkedHashMap<>();
Map<String, Table> tableMap = new LinkedHashMap<>();
for (Schema schema : schemaList) {
for (Table table : schema.getTables()) {
if (Asserts.isNullCollection(schema.getTables())) {
// if schema tables is empty, throw exception
throw new IllegalArgumentException(
"Schema tables is empty, please check your configuration or check your database permission and try again.");
}
// if schema tables is not empty, sort by table name
List<Table> tableList = schema.getTables().stream()
.sorted(Comparator.comparing(Table::getName))
.collect(Collectors.toList());
for (Table table : tableList) {
String sinkTableName = getSinkTableName(table);
OutputTag<Map> outputTag = new OutputTag<Map>(sinkTableName) {};
tagMap.put(table, outputTag);
......
......@@ -41,12 +41,12 @@ import org.apache.flink.util.Collector;
import org.apache.flink.util.OutputTag;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.UUID;
import java.util.stream.Collectors;
public class DorisSchemaEvolutionSinkBuilder extends AbstractSinkBuilder implements Serializable {
......@@ -92,9 +92,19 @@ public class DorisSchemaEvolutionSinkBuilder extends AbstractSinkBuilder impleme
dataStreamSource.map(x -> objectMapper.readValue(x, Map.class)).returns(Map.class);
final String schemaFieldName = config.getSchemaFieldName();
Map<Table, OutputTag<String>> tagMap = new HashMap<>();
Map<String, Table> tableMap = new HashMap<>();
Map<Table, OutputTag<String>> tagMap = new LinkedHashMap<>();
Map<String, Table> tableMap = new LinkedHashMap<>();
for (Schema schema : schemaList) {
if (Asserts.isNullCollection(schema.getTables())) {
// if schema tables is empty, throw exception
throw new IllegalArgumentException(
"Schema tables is empty, please check your configuration or check your database permission and try again.");
}
// if schema tables is not empty, sort by table name
List<Table> tableList = schema.getTables().stream()
.sorted(Comparator.comparing(Table::getName))
.collect(Collectors.toList());
for (Table table : schema.getTables()) {
OutputTag<String> outputTag = new OutputTag<String>(getSinkTableName(table)) {};
tagMap.put(table, outputTag);
......@@ -150,7 +160,8 @@ public class DorisSchemaEvolutionSinkBuilder extends AbstractSinkBuilder impleme
getSinkSchemaName(table),
getSinkTableName(table)));
} else {
// flink-cdc-pipeline-connector-doris 3.0.0 以上版本内部已经拼接了 SchemaName + SinkTableName,并且约定 TableLabel 正则表达式如下 --> regex: ^[-_A-Za-z0-9]{1,128}$
// flink-cdc-pipeline-connector-doris 3.0.0 以上版本内部已经拼接了 SchemaName + SinkTableName,并且约定 TableLabel
// 正则表达式如下 --> regex: ^[-_A-Za-z0-9]{1,128}$
executionBuilder.setLabelPrefix("dinky");
}
......@@ -160,11 +171,13 @@ public class DorisSchemaEvolutionSinkBuilder extends AbstractSinkBuilder impleme
executionBuilder.setStreamLoadProp(properties).setDeletable(true);
JsonDebeziumSchemaSerializer.Builder jsonDebeziumSchemaSerializerBuilder = JsonDebeziumSchemaSerializer.builder();
JsonDebeziumSchemaSerializer.Builder jsonDebeziumSchemaSerializerBuilder =
JsonDebeziumSchemaSerializer.builder();
// use new schema change
if (sink.containsKey(DorisSinkOptions.SINK_USE_NEW_SCHEMA_CHANGE.key())) {
jsonDebeziumSchemaSerializerBuilder.setNewSchemaChange(Boolean.valueOf(sink.get(DorisSinkOptions.SINK_USE_NEW_SCHEMA_CHANGE.key())));
jsonDebeziumSchemaSerializerBuilder.setNewSchemaChange(
Boolean.valueOf(sink.get(DorisSinkOptions.SINK_USE_NEW_SCHEMA_CHANGE.key())));
}
DorisSink.Builder<String> builder = DorisSink.builder();
......
......@@ -41,11 +41,12 @@ import org.apache.flink.util.Collector;
import org.apache.flink.util.OutputTag;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.stream.Collectors;
public class KafkaSinkBuilder extends AbstractSinkBuilder implements Serializable {
......@@ -97,17 +98,27 @@ public class KafkaSinkBuilder extends AbstractSinkBuilder implements Serializabl
KafkaSink<String> kafkaSink = kafkaSinkBuilder.build();
dataStreamSource.sinkTo(kafkaSink);
} else {
Map<Table, OutputTag<String>> tagMap = new HashMap<>();
Map<String, Table> tableMap = new HashMap<>();
Map<Table, OutputTag<String>> tagMap = new LinkedHashMap<>();
Map<String, Table> tableMap = new LinkedHashMap<>();
ObjectMapper objectMapper = new ObjectMapper();
SingleOutputStreamOperator<Map> mapOperator = dataStreamSource
.map(x -> objectMapper.readValue(x, Map.class))
.returns(Map.class);
final List<Schema> schemaList = config.getSchemaList();
final String schemaFieldName = config.getSchemaFieldName();
if (Asserts.isNotNullCollection(schemaList)) {
for (Schema schema : schemaList) {
for (Table table : schema.getTables()) {
if (Asserts.isNullCollection(schema.getTables())) {
// if schema tables is empty, throw exception
throw new IllegalArgumentException(
"Schema tables is empty, please check your configuration or check your database permission and try again.");
}
// if schema tables is not empty, sort by table name
List<Table> tableList = schema.getTables().stream()
.sorted(Comparator.comparing(Table::getName))
.collect(Collectors.toList());
for (Table table : tableList) {
String sinkTableName = getSinkTableName(table);
OutputTag<String> outputTag = new OutputTag<String>(sinkTableName) {};
tagMap.put(table, outputTag);
......
......@@ -43,10 +43,12 @@ import org.apache.flink.util.Collector;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
......@@ -91,7 +93,16 @@ public class KafkaSinkJsonBuilder extends AbstractSinkBuilder implements Seriali
}
for (Schema schema : schemaList) {
for (Table table : schema.getTables()) {
if (Asserts.isNullCollection(schema.getTables())) {
// if schema tables is empty, throw exception
throw new IllegalArgumentException(
"Schema tables is empty, please check your configuration or check your database permission and try again.");
}
// if schema tables is not empty, sort by table name
List<Table> tableList = schema.getTables().stream()
.sorted(Comparator.comparing(Table::getName))
.collect(Collectors.toList());
for (Table table : tableList) {
final String tableName = table.getName();
final String schemaName = table.getSchema();
SingleOutputStreamOperator<Map> filterOperator = mapOperator.filter((FilterFunction<Map>) value -> {
......
......@@ -20,7 +20,7 @@
package org.dinky.data.model;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import lombok.Getter;
......@@ -38,11 +38,11 @@ public class Schema implements Serializable, Comparable<Schema> {
private static final long serialVersionUID = 4278304357661271040L;
private String name;
private List<Table> tables = new ArrayList<>();
private List<String> views = new ArrayList<>();
private List<String> functions = new ArrayList<>();
private List<String> userFunctions = new ArrayList<>();
private List<String> modules = new ArrayList<>();
private List<Table> tables = new LinkedList<>();
private List<String> views = new LinkedList<>();
private List<String> functions = new LinkedList<>();
private List<String> userFunctions = new LinkedList<>();
private List<String> modules = new LinkedList<>();
/** 需要保留一个空构造方法,否则序列化有问题 */
public Schema() {}
......
......@@ -39,8 +39,9 @@ import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.TableResult;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
......@@ -81,16 +82,19 @@ public class CreateCDCSourceOperation extends AbstractOperation implements Opera
Map<String, Map<String, String>> allConfigMap = cdcBuilder.parseMetaDataConfigs();
config.setSchemaFieldName(cdcBuilder.getSchemaFieldName());
SinkBuilder sinkBuilder = SinkBuilderFactory.buildSinkBuilder(config);
List<Schema> schemaList = new ArrayList<>();
final List<String> schemaNameList = cdcBuilder.getSchemaList();
final List<String> tableRegList = cdcBuilder.getTableList();
final List<String> schemaTableNameList = new ArrayList<>();
final List<Schema> schemaList = new LinkedList<>();
final List<String> schemaTableNameList = new LinkedList<>();
// Scenario of dividing databases and tables
if (SplitUtil.isEnabled(cdcSource.getSplit())) {
logger.info("Split table or database mode is enabled...");
Map<String, String> confMap = cdcBuilder.parseMetaDataConfig();
Driver driver =
Driver.buildWithOutPool(confMap.get("name"), confMap.get("type"), JsonUtils.toMap(confMap));
// 这直接传正则过去
// This is passed directly to the regularization process
schemaTableNameList.addAll(tableRegList.stream()
.map(x -> x.replaceFirst("\\\\.", "."))
.collect(Collectors.toList()));
......@@ -100,12 +104,17 @@ public class CreateCDCSourceOperation extends AbstractOperation implements Opera
Set<Table> tables = driver.getSplitTables(tableRegList, cdcSource.getSplit());
for (Table table : tables) {
// Filter out views
if (Asserts.isEquals(table.getType(), "VIEW")) {
continue;
}
String schemaName = table.getSchema();
Schema schema = Schema.build(schemaName);
schema.setTables(Collections.singletonList(table));
// 分库分表所有表结构都是一样的,取出列表中第一个表名即可
// The structure of all tables in a database or table is the same, just take out the first table
// name from the list
String schemaTableName = table.getSchemaTableNameList().get(0);
// 真实的表名
// Real Table Name
String realSchemaName = schemaTableName.split("\\.")[0];
String tableName = schemaTableName.split("\\.")[1];
table.setColumns(driver.listColumnsSortByPK(realSchemaName, tableName));
......@@ -167,7 +176,9 @@ public class CreateCDCSourceOperation extends AbstractOperation implements Opera
logger.info("{}: {}", i + 1, schemaTableNameList.get(i));
}
config.setSchemaTableNameList(schemaTableNameList);
config.setSchemaList(schemaList);
config.setSchemaList(schemaList.stream()
.sorted(Comparator.comparing(Schema::getName))
.collect(Collectors.toList()));
StreamExecutionEnvironment streamExecutionEnvironment = executor.getStreamExecutionEnvironment();
if (Asserts.isNotNull(config.getParallelism())) {
streamExecutionEnvironment.setParallelism(config.getParallelism());
......@@ -201,6 +212,8 @@ public class CreateCDCSourceOperation extends AbstractOperation implements Opera
driver.createSchema(schema);
}
sink.put(FlinkCDCConfig.SINK_DB, schema);
// todo: There is a bug that can cause the problem of URL duplicate concatenation of schema, for example: jdbc:
// mysql://localhost:3306/test?useSSL=false/test -1
if (!url.contains(schema)) {
sink.put("url", url + "/" + schema);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment