JingsongLi commented on code in PR #1229: URL: https://github.com/apache/incubator-paimon/pull/1229#discussion_r1209052380
########## paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/sink/WrappedManifestCommittableSerializer.java: ########## @@ -0,0 +1,112 @@ +/* + * 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; + +import org.apache.paimon.catalog.Identifier; +import org.apache.paimon.data.serializer.VersionedSerializer; +import org.apache.paimon.io.DataInputDeserializer; +import org.apache.paimon.io.DataOutputViewStreamWrapper; +import org.apache.paimon.manifest.ManifestCommittable; +import org.apache.paimon.manifest.ManifestCommittableSerializer; +import org.apache.paimon.manifest.WrappedManifestCommittable; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +/** {@link VersionedSerializer} for {@link ManifestCommittable}. */ +public class WrappedManifestCommittableSerializer + implements VersionedSerializer<WrappedManifestCommittable> { + + protected static final int CURRENT_VERSION = 2; Review Comment: CURRENT_VERSION = 1 ########## paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/sink/StoreMultiCommitter.java: ########## @@ -0,0 +1,187 @@ +/* + * 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; + +import org.apache.paimon.catalog.Catalog; +import org.apache.paimon.catalog.Identifier; +import org.apache.paimon.manifest.ManifestCommittable; +import org.apache.paimon.manifest.WrappedManifestCommittable; +import org.apache.paimon.operation.Lock; +import org.apache.paimon.table.FileStoreTable; +import org.apache.paimon.table.sink.CommitMessage; + +import org.apache.flink.api.java.tuple.Tuple2; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.TreeMap; +import java.util.stream.Collectors; + +/** + * {@link StoreMultiCommitter} for multiple dynamic store. During the commit process, it will group + * the WrappedManifestCommittables by their table identifier and use different committers to commit + * to different tables. + */ +public class StoreMultiCommitter + implements Committer<MultiTableCommittable, WrappedManifestCommittable> { + + private Catalog catalog; + private final String commitUser; + // To make the commit behavior consistent with that of Committer, + // StoreMultiCommitter manages multiple committers which are + // referenced by table id. + private Map<Identifier, StoreCommitter> tableCommitters; + private Lock.Factory lockFactory = Lock.emptyFactory(); + + public StoreMultiCommitter(String commitUser, Catalog.Loader catalogLoader) { + this.catalog = catalogLoader.load(); + this.commitUser = commitUser; + this.tableCommitters = new HashMap<>(); + } + + @Override + public List<WrappedManifestCommittable> filterRecoveredCommittables( + List<WrappedManifestCommittable> globalCommittables) { + // key by table id + Map<Identifier, List<ManifestCommittable>> committableMap = + getCommittableMap(globalCommittables); + + Map<Long, WrappedManifestCommittable> result = new TreeMap<>(); + + for (Map.Entry<Identifier, List<ManifestCommittable>> entry : committableMap.entrySet()) { + Identifier tableId = entry.getKey(); + List<ManifestCommittable> committableList = entry.getValue(); + StoreCommitter committer = getStoreCommitter(tableId); + List<ManifestCommittable> filteredCommittables = + committer.filterRecoveredCommittables(committableList); + + for (ManifestCommittable filteredCommittable : filteredCommittables) { + long identifier = filteredCommittable.identifier(); + WrappedManifestCommittable wrappedFilteredCommittable = + result.computeIfAbsent(identifier, id -> new WrappedManifestCommittable()); + + wrappedFilteredCommittable.putManifestCommittable(tableId, filteredCommittable); + } + } + + return new LinkedList<>(result.values()); + } + + @Override + public WrappedManifestCommittable combine( + long checkpointId, long watermark, List<MultiTableCommittable> committables) { + WrappedManifestCommittable wrappedManifestCommittable = new WrappedManifestCommittable(); + for (MultiTableCommittable committable : committables) { + + ManifestCommittable manifestCommittable = + wrappedManifestCommittable.getManifestCommittable( + Identifier.create(committable.getDatabase(), committable.getTable()), + checkpointId, + watermark); + + switch (committable.kind()) { + case FILE: + CommitMessage file = (CommitMessage) committable.wrappedCommittable(); + manifestCommittable.addFileCommittable(file); + break; + case LOG_OFFSET: + LogOffsetCommittable offset = + (LogOffsetCommittable) committable.wrappedCommittable(); + manifestCommittable.addLogOffset(offset.bucket(), offset.offset()); + break; + } + } + return wrappedManifestCommittable; + } + + @Override + public void commit(List<WrappedManifestCommittable> committables) + throws IOException, InterruptedException { + + // key by table id + Map<Identifier, List<ManifestCommittable>> committableMap = getCommittableMap(committables); + + for (Map.Entry<Identifier, List<ManifestCommittable>> entry : committableMap.entrySet()) { + Identifier tableId = entry.getKey(); + List<ManifestCommittable> committableList = entry.getValue(); + StoreCommitter committer = getStoreCommitter(tableId); + committer.commit(committableList); + } + } + + private Map<Identifier, List<ManifestCommittable>> getCommittableMap( + List<WrappedManifestCommittable> committables) { + return committables.stream() + .flatMap( + wrapped -> { + Map<Identifier, ManifestCommittable> manifestCommittables = + wrapped.getManifestCommittables(); + return manifestCommittables.entrySet().stream() + .map(entry -> Tuple2.of(entry.getKey(), entry.getValue())); + }) + .collect( + Collectors.groupingBy( + t -> t.f0, Collectors.mapping(t -> t.f1, Collectors.toList()))); + } + + @Override + public Map<Long, List<MultiTableCommittable>> groupByCheckpoint( + Collection<MultiTableCommittable> committables) { + Map<Long, List<MultiTableCommittable>> grouped = new HashMap<>(); + for (MultiTableCommittable c : committables) { + grouped.computeIfAbsent(c.checkpointId(), k -> new ArrayList<>()).add(c); + } + return grouped; + } + + private StoreCommitter getStoreCommitter(Identifier tableId) { + StoreCommitter committer = + tableCommitters.computeIfAbsent( + tableId, + id -> { + try { + FileStoreTable table = (FileStoreTable) catalog.getTable(id); + return new StoreCommitter( + table.newCommit(commitUser) + .withLock(lockFactory.create()) + .ignoreEmptyCommit(false)); + } catch (Catalog.TableNotExistException e) { + return null; + } + }); + + if (committer == null) { + throw new RuntimeException( Review Comment: throw exception in `catch (Catalog.TableNotExistException e)`. ########## paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/sink/WrappedManifestCommittableSerializer.java: ########## @@ -0,0 +1,112 @@ +/* + * 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; + +import org.apache.paimon.catalog.Identifier; +import org.apache.paimon.data.serializer.VersionedSerializer; +import org.apache.paimon.io.DataInputDeserializer; +import org.apache.paimon.io.DataOutputViewStreamWrapper; +import org.apache.paimon.manifest.ManifestCommittable; +import org.apache.paimon.manifest.ManifestCommittableSerializer; +import org.apache.paimon.manifest.WrappedManifestCommittable; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +/** {@link VersionedSerializer} for {@link ManifestCommittable}. */ +public class WrappedManifestCommittableSerializer + implements VersionedSerializer<WrappedManifestCommittable> { + + protected static final int CURRENT_VERSION = 2; + + private final ManifestCommittableSerializer manifestCommittableSerializer; + + public WrappedManifestCommittableSerializer() { + this.manifestCommittableSerializer = new ManifestCommittableSerializer(); + } + + @Override + public int getVersion() { + return CURRENT_VERSION; + } + + @Override + public byte[] serialize(WrappedManifestCommittable wrapped) throws IOException { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + DataOutputViewStreamWrapper view = new DataOutputViewStreamWrapper(out); + + // Serialize ManifestCommittable map inside WrappedManifestCommittable + Map<Identifier, ManifestCommittable> map = wrapped.getManifestCommittables(); + view.writeInt(map.size()); + for (Map.Entry<Identifier, ManifestCommittable> entry : map.entrySet()) { + byte[] serializedKey = entry.getKey().getFullName().getBytes(); Review Comment: getBytes(UTF_8) -- 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]
