qidian99 commented on code in PR #1212: URL: https://github.com/apache/incubator-paimon/pull/1212#discussion_r1203361518
########## paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/sink/cdc/CdcRecordStoreMultiWriteOperator.java: ########## @@ -0,0 +1,176 @@ +/* + * 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.paimon.flink.sink.cdc; + +import org.apache.paimon.catalog.Catalog; +import org.apache.paimon.catalog.Identifier; +import org.apache.paimon.data.GenericRow; +import org.apache.paimon.flink.sink.Committable; +import org.apache.paimon.flink.sink.MultiTableCommittable; +import org.apache.paimon.flink.sink.PrepareCommitOperator; +import org.apache.paimon.flink.sink.StateUtils; +import org.apache.paimon.flink.sink.StoreSinkWrite; +import org.apache.paimon.flink.sink.StoreSinkWriteState; +import org.apache.paimon.table.FileStoreTable; + +import org.apache.flink.runtime.state.StateInitializationContext; +import org.apache.flink.runtime.state.StateSnapshotContext; +import org.apache.flink.streaming.runtime.streamrecord.StreamRecord; + +import java.io.IOException; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.stream.Collectors; + +import static org.apache.paimon.flink.sink.cdc.CdcRecordStoreWriteOperator.RETRY_SLEEP_TIME; + +/** + * A {@link PrepareCommitOperator} to write {@link CdcRecord}. Record schema may change. If current + * known schema does not fit record schema, this operator will wait for schema changes. + */ +public class CdcRecordStoreMultiWriteOperator extends PrepareCommitOperator<MultiplexCdcRecord> { + + private static final long serialVersionUID = 1L; + private final Catalog catalog; + private final StoreSinkWrite.Provider storeSinkWriteProvider; + private final String initialCommitUser; + private FileStoreTable table; + private Map<Identifier, FileStoreTable> tables; + private StoreSinkWriteState state; + private StoreSinkWrite write; + private Map<Identifier, StoreSinkWrite> writes; + private String commitUser; + + public CdcRecordStoreMultiWriteOperator( + Catalog catalog, + StoreSinkWrite.Provider storeSinkWriteProvider, + String initialCommitUser) { + this.catalog = catalog; + this.storeSinkWriteProvider = storeSinkWriteProvider; + this.initialCommitUser = initialCommitUser; + } + + @Override + public void initializeState(StateInitializationContext context) throws Exception { + super.initializeState(context); + + // Each job can only have one user name and this name must be consistent across restarts. + // We cannot use job id as commit user name here because user may change job id by creating + // a savepoint, stop the job and then resume from savepoint. + commitUser = + StateUtils.getSingleValueFromState( + context, "commit_user_state", String.class, initialCommitUser); + + state = new StoreSinkWriteState(context, (tableName, partition, bucket) -> true); + this.tables = new HashMap<>(); + this.writes = new HashMap<>(); + } + + @Override + public void processElement(StreamRecord<MultiplexCdcRecord> element) throws Exception { + MultiplexCdcRecord record = element.getValue(); + + String databaseName = record.getDatabaseName(); + String tableName = record.getTableName(); + Identifier key = Identifier.create(databaseName, tableName); + table = + tables.computeIfAbsent( + key, + id -> { + try { + return (FileStoreTable) catalog.getTable(id); + } catch (Catalog.TableNotExistException e) { + return null; + } + }); + + if (table == null) { Review Comment: That makes sense... Changed the code and added async table create test. -- 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]
