aokolnychyi commented on a change in pull request #2500: URL: https://github.com/apache/iceberg/pull/2500#discussion_r617034436
########## File path: spark3/src/main/java/org/apache/iceberg/spark/FileRewriteCoordinator.java ########## @@ -0,0 +1,169 @@ +/* + * 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.iceberg.spark; + +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; +import org.apache.iceberg.DataFile; +import org.apache.iceberg.FileScanTask; +import org.apache.iceberg.HasTableOperations; +import org.apache.iceberg.Table; +import org.apache.iceberg.TableOperations; +import org.apache.iceberg.exceptions.ValidationException; +import org.apache.iceberg.io.FileIO; +import org.apache.iceberg.relocated.com.google.common.base.Preconditions; +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableSet; +import org.apache.iceberg.relocated.com.google.common.collect.Lists; +import org.apache.iceberg.relocated.com.google.common.collect.Maps; +import org.apache.iceberg.relocated.com.google.common.collect.Sets; +import org.apache.iceberg.util.Pair; +import org.apache.iceberg.util.Tasks; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class FileRewriteCoordinator { + + private static final Logger LOG = LoggerFactory.getLogger(FileRewriteCoordinator.class); + private static final FileRewriteCoordinator INSTANCE = new FileRewriteCoordinator(); + + private final Map<String, Lock> lockMap = Maps.newConcurrentMap(); + private final Map<Pair<String, String>, Set<DataFile>> resultMap = Maps.newConcurrentMap(); + + private FileRewriteCoordinator() { + } + + public static FileRewriteCoordinator get() { + return INSTANCE; + } + + public void stageRewrite(Table table, String fileSetID, Set<DataFile> newDataFiles) { + Preconditions.checkArgument(newDataFiles != null && newDataFiles.size() > 0, "Cannot stage null or empty files"); + LOG.info("Staging results of rewriting file set {} for table {}", fileSetID, table); + Pair<String, String> id = toID(table, fileSetID); + resultMap.put(id, newDataFiles); + } + + public void commitRewrite(Table table, String fileSetID) { + commitRewrite(table, ImmutableSet.of(fileSetID)); + } + + public void commitRewrite(Table table, Set<String> fileSetIDs) { + LOG.info("Committing rewrite of file sets {} for table {}", fileSetIDs, table); + + Set<DataFile> rewrittenDataFiles = fetchRewrittenDataFiles(table, fileSetIDs); + Set<DataFile> newDataFiles = fetchNewDataFiles(table, fileSetIDs); + + String tableUUID = tableUUID(table); + Lock tableLock = lockMap.computeIfAbsent(tableUUID, uuid -> new ReentrantLock()); + + try { + LOG.info("Locking table {} to commit rewrite of file sets {}", table, fileSetIDs); Review comment: I'll addd a test for this now. -- 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. For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
