|
| 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 | +} |
0 commit comments