rdblue commented on a change in pull request #200: Add SnapshotProducer that can rewrite manifests URL: https://github.com/apache/incubator-iceberg/pull/200#discussion_r291375315
########## File path: core/src/main/java/org/apache/iceberg/ReplaceManifests.java ########## @@ -0,0 +1,217 @@ +/* + * 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; + +import com.google.common.base.Preconditions; +import com.google.common.collect.Lists; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.function.Function; +import java.util.stream.Collectors; +import org.apache.iceberg.exceptions.RuntimeIOException; +import org.apache.iceberg.util.Tasks; +import org.apache.iceberg.util.ThreadPools; + +import static org.apache.iceberg.TableProperties.MANIFEST_TARGET_SIZE_BYTES; +import static org.apache.iceberg.TableProperties.MANIFEST_TARGET_SIZE_BYTES_DEFAULT; + + +public class ReplaceManifests extends SnapshotProducer<RewriteManifests> implements RewriteManifests { + private final TableOperations ops; + private final PartitionSpec spec; + private final long manifestTargetSizeBytes; + private final SnapshotSummary.Builder summaryBuilder = SnapshotSummary.builder(); + private final List<ManifestFile> newManifests = Collections.synchronizedList(Lists.newArrayList()); + private final Map<Object, WriterWrapper> writers = Collections.synchronizedMap(new HashMap<>()); + private final AtomicInteger manifestCount = new AtomicInteger(0); + + private Function<DataFile, Object> clusterByFunc; + private Function<ManifestFile, Boolean> filterFunc; + + ReplaceManifests(TableOperations ops) { + super(ops); + this.ops = ops; + this.spec = ops.current().spec(); + this.manifestTargetSizeBytes = + ops.current().propertyAsLong(MANIFEST_TARGET_SIZE_BYTES, MANIFEST_TARGET_SIZE_BYTES_DEFAULT); + } + + @Override + protected String operation() { + return DataOperations.REPLACE; + } + + @Override + public RewriteManifests set(String property, String value) { + summaryBuilder.set(property, value); + return this; + } + + @Override + protected Map<String, String> summary() { + return summaryBuilder.build(); + } + + @Override + public ReplaceManifests clusterBy(Function<DataFile, Object> func) { + this.clusterByFunc = func; + return this; + } + + @Override + public ReplaceManifests filter(Function<ManifestFile, Boolean> func) { + this.filterFunc = func; + return this; + } + + @Override + public List<ManifestFile> apply(TableMetadata base) { Review comment: This can handle concurrent appends. Appends that create new manifests and add them to the manifest list shouldn't trigger rerunning the metadata rewrite. Instead, this should keep track of the manifests that it rewrites the first time. If apply is called a second time because another commit succeeded concurrently, this should: * Rerun the rewrite if any manifest that was originally rewritten is no longer in the current manifest list * Reuse the rewritten manifests otherwise, and add the new manifests from the concurrent write without modification ---------------------------------------------------------------- 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] With regards, Apache Git Services --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
