yuxiqian commented on code in PR #3801: URL: https://github.com/apache/flink-cdc/pull/3801#discussion_r1891253357
########## flink-cdc-runtime/src/main/java/org/apache/flink/cdc/runtime/partitioning/DistributedPrePartitionOperator.java: ########## @@ -0,0 +1,138 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.flink.cdc.runtime.partitioning; + +import org.apache.flink.cdc.common.annotation.Internal; +import org.apache.flink.cdc.common.event.DataChangeEvent; +import org.apache.flink.cdc.common.event.Event; +import org.apache.flink.cdc.common.event.SchemaChangeEvent; +import org.apache.flink.cdc.common.event.TableId; +import org.apache.flink.cdc.common.function.HashFunction; +import org.apache.flink.cdc.common.function.HashFunctionProvider; +import org.apache.flink.cdc.common.schema.Schema; +import org.apache.flink.cdc.common.utils.SchemaUtils; +import org.apache.flink.cdc.runtime.operators.schema.regular.SchemaOperator; +import org.apache.flink.cdc.runtime.serializer.event.EventSerializer; +import org.apache.flink.runtime.state.StateSnapshotContext; +import org.apache.flink.streaming.api.operators.AbstractStreamOperator; +import org.apache.flink.streaming.api.operators.ChainingStrategy; +import org.apache.flink.streaming.api.operators.OneInputStreamOperator; +import org.apache.flink.streaming.runtime.streamrecord.StreamRecord; + +import java.io.Serializable; +import java.util.HashMap; +import java.util.Map; + +/** + * Operator for processing events from {@link SchemaOperator} before {@link EventPartitioner} with + * distributed topology. + */ +@Internal +public class DistributedPrePartitionOperator extends AbstractStreamOperator<PartitioningEvent> + implements OneInputStreamOperator<Event, PartitioningEvent>, Serializable { + private static final long serialVersionUID = 1L; + + private final int downstreamParallelism; + private final HashFunctionProvider<DataChangeEvent> hashFunctionProvider; + + // Schema and HashFunctionMap used in schema inferencing mode. + private transient Map<TableId, Schema> schemaMap; + private transient Map<TableId, HashFunction<DataChangeEvent>> hashFunctionMap; + + private transient int subTaskId; + + public DistributedPrePartitionOperator( + int downstreamParallelism, HashFunctionProvider<DataChangeEvent> hashFunctionProvider) { + this.chainingStrategy = ChainingStrategy.ALWAYS; + this.downstreamParallelism = downstreamParallelism; + this.hashFunctionProvider = hashFunctionProvider; + } + + @Override + public void open() throws Exception { + super.open(); + subTaskId = getRuntimeContext().getTaskInfo().getIndexOfThisSubtask(); + schemaMap = new HashMap<>(); + hashFunctionMap = new HashMap<>(); + } + + @Override + public void processElement(StreamRecord<Event> element) throws Exception { + Event event = element.getValue(); + if (event instanceof SchemaChangeEvent) { + SchemaChangeEvent schemaChangeEvent = (SchemaChangeEvent) event; + TableId tableId = schemaChangeEvent.tableId(); + + // Update schema map + schemaMap.compute( + tableId, + (tId, oldSchema) -> + SchemaUtils.applySchemaChangeEvent(oldSchema, schemaChangeEvent)); + + // For malformed dangling dropTableEvents, we simply ignore this event to avoid breaking + // the pipeline. Review Comment: Sorry, they're some residual codes from previous attempt to create a unified topology for both regular and distributed sources. Since this operator will only be used in the new topology, the dangling "DropTableEvent" case is impossible. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
