-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
API: Add function for removing unused specs from metadata.json
Previously there was no way to remove partition specs from a table once they were added. To fix this we add an api which searches through all reachable manifest files and records their specsIds. Any specIds which do not find are marked for removal which is done through a serializable commit.
- Loading branch information
1 parent
4482565
commit 5a3ec7c
Showing
12 changed files
with
441 additions
and
0 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
api/src/main/java/org/apache/iceberg/MetadataMaintenance.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* 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; | ||
|
||
/** APIs for table metadata maintenance, such as removing unused partition specs. */ | ||
public interface MetadataMaintenance { | ||
/** | ||
* Remove any partition specs from the Metadata that are no longer used in any data files. | ||
* | ||
* <p>Always preserves the current default spec even if it has not yet been used. | ||
* | ||
* @return a new {@link RemoveUnusedSpecs} | ||
*/ | ||
RemoveUnusedSpecs removeUnusedSpecs(); | ||
} |
29 changes: 29 additions & 0 deletions
29
api/src/main/java/org/apache/iceberg/RemoveUnusedSpecs.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* 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 java.util.List; | ||
|
||
/** | ||
* API for removing partition specs from the metadata which are not the default spec and no longer | ||
* refer to any datafiles in the table. | ||
* | ||
* <p>{@link #apply()} returns the specs that will remain if committed on the current metadata | ||
*/ | ||
public interface RemoveUnusedSpecs extends PendingUpdate<List<PartitionSpec>> {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
core/src/main/java/org/apache/iceberg/BaseMetadataMaintenance.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* 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; | ||
|
||
class BaseMetadataMaintenance implements MetadataMaintenance { | ||
private final TableOperations ops; | ||
|
||
BaseMetadataMaintenance(TableOperations ops) { | ||
this.ops = ops; | ||
} | ||
|
||
@Override | ||
public RemoveUnusedSpecs removeUnusedSpecs() { | ||
return new BaseRemoveUnusedSpecs(ops); | ||
} | ||
} |
113 changes: 113 additions & 0 deletions
113
core/src/main/java/org/apache/iceberg/BaseRemoveUnusedSpecs.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/* | ||
* 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 static org.apache.iceberg.TableProperties.COMMIT_MAX_RETRY_WAIT_MS; | ||
import static org.apache.iceberg.TableProperties.COMMIT_MAX_RETRY_WAIT_MS_DEFAULT; | ||
import static org.apache.iceberg.TableProperties.COMMIT_MIN_RETRY_WAIT_MS; | ||
import static org.apache.iceberg.TableProperties.COMMIT_MIN_RETRY_WAIT_MS_DEFAULT; | ||
import static org.apache.iceberg.TableProperties.COMMIT_NUM_RETRIES; | ||
import static org.apache.iceberg.TableProperties.COMMIT_NUM_RETRIES_DEFAULT; | ||
import static org.apache.iceberg.TableProperties.COMMIT_TOTAL_RETRY_TIME_MS; | ||
import static org.apache.iceberg.TableProperties.COMMIT_TOTAL_RETRY_TIME_MS_DEFAULT; | ||
|
||
import java.io.IOException; | ||
import java.io.UncheckedIOException; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
import org.apache.iceberg.exceptions.CommitFailedException; | ||
import org.apache.iceberg.io.CloseableIterable; | ||
import org.apache.iceberg.relocated.com.google.common.collect.Iterables; | ||
import org.apache.iceberg.relocated.com.google.common.collect.Sets; | ||
import org.apache.iceberg.util.ParallelIterable; | ||
import org.apache.iceberg.util.Tasks; | ||
import org.apache.iceberg.util.ThreadPools; | ||
|
||
/** | ||
* Implementation of RemoveUnusedSpecs API to remove unused partition specs. | ||
* | ||
* <p>When committing, these changes will be applied to the latest table metadata. Commit conflicts | ||
* will be resolved by recalculating which specs are no longer in use again in the latest metadata | ||
* and retrying. | ||
*/ | ||
class BaseRemoveUnusedSpecs implements RemoveUnusedSpecs { | ||
private final TableOperations ops; | ||
|
||
private TableMetadata base; | ||
|
||
BaseRemoveUnusedSpecs(TableOperations ops) { | ||
this.ops = ops; | ||
this.base = ops.current(); | ||
} | ||
|
||
@Override | ||
public List<PartitionSpec> apply() { | ||
TableMetadata newMetadata = internalApply(); | ||
return newMetadata.specs(); | ||
} | ||
|
||
@Override | ||
public void commit() { | ||
this.base = ops.refresh(); | ||
Tasks.foreach(ops) | ||
.retry(base.propertyAsInt(COMMIT_NUM_RETRIES, COMMIT_NUM_RETRIES_DEFAULT)) | ||
.exponentialBackoff( | ||
base.propertyAsInt(COMMIT_MIN_RETRY_WAIT_MS, COMMIT_MIN_RETRY_WAIT_MS_DEFAULT), | ||
base.propertyAsInt(COMMIT_MAX_RETRY_WAIT_MS, COMMIT_MAX_RETRY_WAIT_MS_DEFAULT), | ||
base.propertyAsInt(COMMIT_TOTAL_RETRY_TIME_MS, COMMIT_TOTAL_RETRY_TIME_MS_DEFAULT), | ||
2.0 /* exponential */) | ||
.onlyRetryOn(CommitFailedException.class) | ||
.run( | ||
taskOps -> { | ||
TableMetadata newMetadata = internalApply(); | ||
taskOps.commit(base, newMetadata); | ||
}); | ||
} | ||
|
||
private Iterable<ManifestFile> reachableManifests() { | ||
Iterable<Snapshot> snapshots = base.snapshots(); | ||
Iterable<Iterable<ManifestFile>> manifestIterables = | ||
Iterables.transform(snapshots, snapshot -> snapshot.allManifests(ops.io())); | ||
try (CloseableIterable<ManifestFile> iterable = | ||
new ParallelIterable<>(manifestIterables, ThreadPools.getWorkerPool())) { | ||
return Sets.newHashSet(iterable); | ||
} catch (IOException e) { | ||
throw new UncheckedIOException("Failed to close parallel iterable", e); | ||
} | ||
} | ||
|
||
private TableMetadata internalApply() { | ||
this.base = ops.refresh(); | ||
List<PartitionSpec> specs = base.specs(); | ||
int currentSpecId = base.defaultSpecId(); | ||
|
||
Set<Integer> specsInUse = | ||
Sets.newHashSet(Iterables.transform(reachableManifests(), ManifestFile::partitionSpecId)); | ||
|
||
specsInUse.add(currentSpecId); | ||
|
||
List<PartitionSpec> specsToRemove = | ||
specs.stream() | ||
.filter(spec -> !specsInUse.contains(spec.specId())) | ||
.collect(Collectors.toList()); | ||
|
||
return TableMetadata.buildFrom(base).removeUnusedSpecs(specsToRemove).build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.