Skip to content

Commit 5d7c20a

Browse files
committed
modified based on cr
1 parent df33e0a commit 5d7c20a

14 files changed

Lines changed: 145 additions & 46 deletions

File tree

flink-cdc-common/src/main/java/org/apache/flink/cdc/common/sink/DefaultDataChangeEventHashFunctionProvider.java

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import java.util.List;
3333
import java.util.Objects;
3434
import java.util.Optional;
35-
import java.util.concurrent.ThreadLocalRandom;
3635

3736
/** The default {@link HashFunctionProvider} implementation for data change event. */
3837
public class DefaultDataChangeEventHashFunctionProvider
@@ -64,15 +63,10 @@ public int hashcode(DataChangeEvent event) {
6463
objectsToHash.add(tableId.getTableName());
6564

6665
// Primary key
67-
if (!primaryKeyGetters.isEmpty()) {
68-
RecordData data =
69-
event.op().equals(OperationType.DELETE) ? event.before() : event.after();
70-
for (FieldGetter primaryKeyGetter : primaryKeyGetters) {
71-
objectsToHash.add(primaryKeyGetter.getFieldOrNull(data));
72-
}
73-
} else {
74-
// Avoid sending all events to the same subtask when table has no primary key.
75-
objectsToHash.add(ThreadLocalRandom.current().nextInt());
66+
RecordData data =
67+
event.op().equals(OperationType.DELETE) ? event.before() : event.after();
68+
for (FieldGetter primaryKeyGetter : primaryKeyGetters) {
69+
objectsToHash.add(primaryKeyGetter.getFieldOrNull(data));
7670
}
7771

7872
// Calculate hash

flink-cdc-connect/flink-cdc-pipeline-connectors/flink-cdc-pipeline-connector-fluss/pom.xml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ limitations under the License.
7878
<version>${fluss.version}</version>
7979
<scope>test</scope>
8080
</dependency>
81-
<!-- In cdc yaml, we use our own sink. Thus, import fluss-fink just for test -->
81+
<!-- In Flink CDC project has Pipeline Sink Connector for Fluss. we import fluss-fink for Fluss Sink Connector just for test purpose -->
8282
<dependency>
8383
<groupId>com.alibaba.fluss</groupId>
8484
<artifactId>fluss-flink-common</artifactId>
@@ -104,9 +104,6 @@ limitations under the License.
104104
<version>${project.version}</version>
105105
<scope>test</scope>
106106
</dependency>
107-
108-
109-
110107
</dependencies>
111108

112109
<build>

flink-cdc-connect/flink-cdc-pipeline-connectors/flink-cdc-pipeline-connector-fluss/src/main/java/org/apache/flink/cdc/connectors/fluss/sink/FlussDataSink.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
package org.apache.flink.cdc.connectors.fluss.sink;
1919

20+
import org.apache.flink.cdc.common.event.DataChangeEvent;
21+
import org.apache.flink.cdc.common.function.HashFunctionProvider;
2022
import org.apache.flink.cdc.common.sink.DataSink;
2123
import org.apache.flink.cdc.common.sink.EventSinkProvider;
2224
import org.apache.flink.cdc.common.sink.FlinkSinkProvider;
@@ -58,4 +60,9 @@ public MetadataApplier getMetadataApplier() {
5860
return new FlussMetaDataApplier(
5961
flussClientConfig, tableProperties, bucketKeysMap, bucketNumMap);
6062
}
63+
64+
@Override
65+
public HashFunctionProvider<DataChangeEvent> getDataChangeEventHashFunctionProvider() {
66+
return new FlussHashFunctionProvider();
67+
}
6168
}

flink-cdc-connect/flink-cdc-pipeline-connectors/flink-cdc-pipeline-connector-fluss/src/main/java/org/apache/flink/cdc/connectors/fluss/sink/FlussEventSerializationSchema.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import org.apache.flink.cdc.common.event.TableId;
2626
import org.apache.flink.cdc.common.utils.Preconditions;
2727
import org.apache.flink.cdc.connectors.fluss.sink.v2.FlussEvent;
28-
import org.apache.flink.cdc.connectors.fluss.sink.v2.FlussRecordSerializer;
28+
import org.apache.flink.cdc.connectors.fluss.sink.v2.FlussEventSerializer;
2929
import org.apache.flink.cdc.connectors.fluss.sink.v2.FlussRowWithOp;
3030

3131
import com.alibaba.fluss.client.Connection;
@@ -46,7 +46,7 @@
4646
import static org.apache.flink.cdc.connectors.fluss.utils.FlussConversions.toFlussSchema;
4747

4848
/** Serialization schema that converts a CDC data record to a Fluss event. */
49-
public class FlussEventSerializationSchema implements FlussRecordSerializer<Event> {
49+
public class FlussEventSerializationSchema implements FlussEventSerializer<Event> {
5050
private static final long serialVersionUID = 1L;
5151

5252
private transient Map<TableId, TableSchemaInfo> tableInfoMap;
@@ -59,19 +59,19 @@ public void open(Connection connection) {
5959
}
6060

6161
@Override
62-
public FlussEvent serialize(Event record) throws IOException {
63-
if (record instanceof SchemaChangeEvent) {
64-
applySchemaChangeEvent((SchemaChangeEvent) record);
65-
return new FlussEvent(getTablePath(((SchemaChangeEvent) record).tableId()), null, true);
66-
} else if (record instanceof DataChangeEvent) {
67-
FlussRowWithOp rowWithOp = applyDataChangeEvent((DataChangeEvent) record);
62+
public FlussEvent serialize(Event event) throws IOException {
63+
if (event instanceof SchemaChangeEvent) {
64+
applySchemaChangeEvent((SchemaChangeEvent) event);
65+
return new FlussEvent(getTablePath(((SchemaChangeEvent) event).tableId()), null, true);
66+
} else if (event instanceof DataChangeEvent) {
67+
FlussRowWithOp rowWithOp = applyDataChangeEvent((DataChangeEvent) event);
6868
return new FlussEvent(
69-
getTablePath(((DataChangeEvent) record).tableId()),
69+
getTablePath(((DataChangeEvent) event).tableId()),
7070
Collections.singletonList(rowWithOp),
7171
false);
7272

7373
} else {
74-
throw new UnsupportedOperationException("Don't support event " + record);
74+
throw new UnsupportedOperationException("Don't support event " + event);
7575
}
7676
}
7777

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.flink.cdc.connectors.fluss.sink;
19+
20+
import org.apache.flink.cdc.common.data.RecordData;
21+
import org.apache.flink.cdc.common.event.DataChangeEvent;
22+
import org.apache.flink.cdc.common.event.OperationType;
23+
import org.apache.flink.cdc.common.event.TableId;
24+
import org.apache.flink.cdc.common.function.HashFunction;
25+
import org.apache.flink.cdc.common.function.HashFunctionProvider;
26+
import org.apache.flink.cdc.common.schema.Schema;
27+
28+
import javax.annotation.Nullable;
29+
30+
import java.util.ArrayList;
31+
import java.util.List;
32+
import java.util.Objects;
33+
import java.util.Optional;
34+
import java.util.concurrent.ThreadLocalRandom;
35+
36+
/** {@link HashFunctionProvider} implementation for {@link FlussDataSink}. */
37+
public class FlussHashFunctionProvider implements HashFunctionProvider<DataChangeEvent> {
38+
@Override
39+
public HashFunction<DataChangeEvent> getHashFunction(@Nullable TableId tableId, Schema schema) {
40+
return new FlussHashFunction(schema);
41+
}
42+
43+
static class FlussHashFunction implements HashFunction<DataChangeEvent> {
44+
private final List<RecordData.FieldGetter> primaryKeyGetters;
45+
46+
public FlussHashFunction(Schema schema) {
47+
primaryKeyGetters = createFieldGetters(schema);
48+
}
49+
50+
@Override
51+
public int hashcode(DataChangeEvent event) {
52+
List<Object> objectsToHash = new ArrayList<>();
53+
// Table ID
54+
TableId tableId = event.tableId();
55+
Optional.ofNullable(tableId.getNamespace()).ifPresent(objectsToHash::add);
56+
Optional.ofNullable(tableId.getSchemaName()).ifPresent(objectsToHash::add);
57+
objectsToHash.add(tableId.getTableName());
58+
59+
// Primary key
60+
if (!primaryKeyGetters.isEmpty()) {
61+
RecordData data =
62+
event.op().equals(OperationType.DELETE) ? event.before() : event.after();
63+
for (RecordData.FieldGetter primaryKeyGetter : primaryKeyGetters) {
64+
objectsToHash.add(primaryKeyGetter.getFieldOrNull(data));
65+
}
66+
} else {
67+
// Avoid sending all events to the same subtask when table has no primary key.
68+
objectsToHash.add(ThreadLocalRandom.current().nextInt());
69+
}
70+
71+
// Calculate hash
72+
return (Objects.hash(objectsToHash.toArray()) * 31) & 0x7FFFFFFF;
73+
}
74+
75+
private List<RecordData.FieldGetter> createFieldGetters(Schema schema) {
76+
List<RecordData.FieldGetter> fieldGetters =
77+
new ArrayList<>(schema.primaryKeys().size());
78+
schema.primaryKeys().stream()
79+
.mapToInt(
80+
pk -> {
81+
int index = schema.getColumnNames().indexOf(pk);
82+
if (index == -1) {
83+
throw new IllegalStateException(
84+
String.format(
85+
"Unable to find column \"%s\" which is defined as primary key",
86+
pk));
87+
}
88+
return index;
89+
})
90+
.forEach(
91+
primaryKeyPosition ->
92+
fieldGetters.add(
93+
RecordData.createFieldGetter(
94+
schema.getColumns()
95+
.get(primaryKeyPosition)
96+
.getType(),
97+
primaryKeyPosition)));
98+
return fieldGetters;
99+
}
100+
}
101+
}

flink-cdc-connect/flink-cdc-pipeline-connectors/flink-cdc-pipeline-connector-fluss/src/main/java/org/apache/flink/cdc/connectors/fluss/sink/FlussMetaDataApplier.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ private void sanityCheck(TableDescriptor inferredFlussTable, TableInfo currentTa
149149
.collect(Collectors.toList());
150150
if (!inferredPrimaryKeyColumnNames.equals(currentPrimaryKeyColumnNames)) {
151151
throw new ValidationException(
152-
"The CDC create table event schema is not matched to current Fluss table schema. "
152+
"The table schema inffered by Flink CDC is not matched with current Fluss table schema. "
153153
+ "\n New Fluss table's primary keys : "
154154
+ inferredPrimaryKeyColumnNames
155155
+ "\n Current Fluss's primary keys: "
@@ -160,7 +160,7 @@ private void sanityCheck(TableDescriptor inferredFlussTable, TableInfo currentTa
160160
List<String> currentBucketKeys = currentTableInfo.getBucketKeys();
161161
if (!inferredBucketKeys.equals(currentBucketKeys)) {
162162
throw new ValidationException(
163-
"The CDC create table event schema is not matched to current Fluss table schema. "
163+
"The table schema inffered by Flink CDC is not matched with current Fluss table schema. "
164164
+ "\n New Fluss table's bucket keys : "
165165
+ inferredBucketKeys
166166
+ "\n Current Fluss's bucket keys: "
@@ -171,7 +171,7 @@ private void sanityCheck(TableDescriptor inferredFlussTable, TableInfo currentTa
171171
List<String> currentPartitionKeys = currentTableInfo.getPartitionKeys();
172172
if (!inferredPartitionKeys.equals(currentPartitionKeys)) {
173173
throw new ValidationException(
174-
"The CDC create table event schema is not matched to current Fluss table schema. "
174+
"The table schema inffered by Flink CDC is not matched with current Fluss table schema. "
175175
+ "\n New Fluss table's partition keys : "
176176
+ inferredPartitionKeys
177177
+ "\n Current Fluss's partition keys: "

flink-cdc-connect/flink-cdc-pipeline-connectors/flink-cdc-pipeline-connector-fluss/src/main/java/org/apache/flink/cdc/connectors/fluss/sink/v2/FlussRecordSerializer.java renamed to flink-cdc-connect/flink-cdc-pipeline-connectors/flink-cdc-pipeline-connector-fluss/src/main/java/org/apache/flink/cdc/connectors/fluss/sink/v2/FlussEventSerializer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
* @param <InputT> The type of the input record which comes from the upstream and will be
2929
* transformed into a FlussEvent here.
3030
*/
31-
public interface FlussRecordSerializer<InputT> extends Serializable {
31+
public interface FlussEventSerializer<InputT> extends Serializable {
3232
void open(Connection connection) throws IOException;
3333

3434
FlussEvent serialize(InputT in) throws IOException;

flink-cdc-connect/flink-cdc-pipeline-connectors/flink-cdc-pipeline-connector-fluss/src/main/java/org/apache/flink/cdc/connectors/fluss/sink/v2/FlussSink.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@
3636
* @param <InputT> The type of the input elements.
3737
*/
3838
public class FlussSink<InputT> implements Sink<InputT> {
39-
private final FlussRecordSerializer<InputT> serializer;
39+
private final FlussEventSerializer<InputT> serializer;
4040
private final Configuration flussConfig;
4141

42-
public FlussSink(Configuration flussConfig, FlussRecordSerializer<InputT> serializer) {
42+
public FlussSink(Configuration flussConfig, FlussEventSerializer<InputT> serializer) {
4343
this.serializer = serializer;
4444
this.flussConfig = flussConfig;
4545
}

flink-cdc-connect/flink-cdc-pipeline-connectors/flink-cdc-pipeline-connector-fluss/src/main/java/org/apache/flink/cdc/connectors/fluss/sink/v2/FlussSinkWriter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public class FlussSinkWriter<InputT> implements SinkWriter<InputT> {
5252

5353
private final Configuration flussConfig;
5454
private final MailboxExecutor mailboxExecutor;
55-
private final FlussRecordSerializer<InputT> flussRecordSerializer;
55+
private final FlussEventSerializer<InputT> flussRecordSerializer;
5656

5757
private transient Connection connection;
5858
protected transient FlinkMetricRegistry flinkMetricRegistry;
@@ -69,7 +69,7 @@ public class FlussSinkWriter<InputT> implements SinkWriter<InputT> {
6969
public FlussSinkWriter(
7070
Configuration flussConfig,
7171
MailboxExecutor mailboxExecutor,
72-
FlussRecordSerializer<InputT> flussRecordSerializer) {
72+
FlussEventSerializer<InputT> flussRecordSerializer) {
7373
this.flussConfig = flussConfig;
7474
this.mailboxExecutor = mailboxExecutor;
7575
this.flussRecordSerializer = flussRecordSerializer;

flink-cdc-connect/flink-cdc-pipeline-connectors/flink-cdc-pipeline-connector-fluss/src/main/java/org/apache/flink/cdc/connectors/fluss/sink/v2/metrics/FlinkMetricRegistry.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ private Map<String, String> getVariables(AbstractMetricGroup group) {
120120
private void registerMetric(MetricGroup metricGroup, Metric metric, String metricName) {
121121
switch (metric.getMetricType()) {
122122
case COUNTER:
123-
metricGroup.counter(metricName, new FlinkCounter((Counter) metric));
123+
metricGroup.counter(metricName, new WarppedFlussCounter((Counter) metric));
124124
break;
125125
case METER:
126126
metricGroup.meter(metricName, new FlinkMeter((Meter) metric));

0 commit comments

Comments
 (0)