-
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.
Flink: Maintenance - TableManager + ExpireSnapshots
- Loading branch information
Peter Vary
committed
Sep 16, 2024
1 parent
8b4b2c1
commit 70e0783
Showing
27 changed files
with
2,338 additions
and
205 deletions.
There are no files selected for viewing
106 changes: 106 additions & 0 deletions
106
...0/flink/src/main/java/org/apache/iceberg/flink/maintenance/operator/AsyncDeleteFiles.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,106 @@ | ||
/* | ||
* 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.flink.maintenance.operator; | ||
|
||
import java.io.Serializable; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.function.Predicate; | ||
import org.apache.flink.annotation.Internal; | ||
import org.apache.flink.configuration.Configuration; | ||
import org.apache.flink.metrics.Counter; | ||
import org.apache.flink.streaming.api.functions.async.ResultFuture; | ||
import org.apache.flink.streaming.api.functions.async.RichAsyncFunction; | ||
import org.apache.iceberg.Table; | ||
import org.apache.iceberg.flink.TableLoader; | ||
import org.apache.iceberg.io.FileIO; | ||
import org.apache.iceberg.relocated.com.google.common.base.Preconditions; | ||
import org.apache.iceberg.util.ThreadPools; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** Delete the files using the {@link FileIO}. */ | ||
@Internal | ||
public class AsyncDeleteFiles extends RichAsyncFunction<String, Boolean> { | ||
private static final Logger LOG = LoggerFactory.getLogger(AsyncDeleteFiles.class); | ||
public static final Predicate<Collection<Boolean>> FAILED_PREDICATE = new FailedPredicate(); | ||
|
||
private final String name; | ||
private final FileIO io; | ||
private final int workerPoolSize; | ||
private final String tableName; | ||
|
||
private transient ExecutorService workerPool; | ||
private transient Counter failedCounter; | ||
private transient Counter succeededCounter; | ||
|
||
public AsyncDeleteFiles(String name, TableLoader tableLoader, int workerPoolSize) { | ||
Preconditions.checkNotNull(name, "Name should no be null"); | ||
Preconditions.checkNotNull(tableLoader, "Table loader should no be null"); | ||
|
||
this.name = name; | ||
tableLoader.open(); | ||
Table table = tableLoader.loadTable(); | ||
this.io = table.io(); | ||
this.workerPoolSize = workerPoolSize; | ||
this.tableName = table.name(); | ||
} | ||
|
||
@Override | ||
public void open(Configuration parameters) throws Exception { | ||
this.failedCounter = | ||
getRuntimeContext() | ||
.getMetricGroup() | ||
.addGroup(TableMaintenanceMetrics.GROUP_KEY, name) | ||
.counter(TableMaintenanceMetrics.DELETE_FILE_FAILED_COUNTER); | ||
this.succeededCounter = | ||
getRuntimeContext() | ||
.getMetricGroup() | ||
.addGroup(TableMaintenanceMetrics.GROUP_KEY, name) | ||
.counter(TableMaintenanceMetrics.DELETE_FILE_SUCCEEDED_COUNTER); | ||
|
||
this.workerPool = | ||
ThreadPools.newWorkerPool(tableName + "-" + name + "-async-delete-files", workerPoolSize); | ||
} | ||
|
||
@Override | ||
public void asyncInvoke(String fileName, ResultFuture<Boolean> resultFuture) { | ||
workerPool.execute( | ||
() -> { | ||
try { | ||
LOG.info("Deleting file: {} with {}", fileName, name); | ||
io.deleteFile(fileName); | ||
resultFuture.complete(Collections.singletonList(true)); | ||
succeededCounter.inc(); | ||
} catch (Throwable e) { | ||
LOG.info("Failed to delete file {} with {}", fileName, name, e); | ||
resultFuture.complete(Collections.singletonList(false)); | ||
failedCounter.inc(); | ||
} | ||
}); | ||
} | ||
|
||
private static class FailedPredicate implements Predicate<Collection<Boolean>>, Serializable { | ||
@Override | ||
public boolean test(Collection<Boolean> collection) { | ||
return collection.size() != 1 || !collection.iterator().next(); | ||
} | ||
} | ||
} |
99 changes: 99 additions & 0 deletions
99
...src/main/java/org/apache/iceberg/flink/maintenance/operator/ExpireSnapshotsProcessor.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,99 @@ | ||
/* | ||
* 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.flink.maintenance.operator; | ||
|
||
import java.util.Collections; | ||
import java.util.concurrent.ExecutorService; | ||
import org.apache.flink.api.common.typeinfo.Types; | ||
import org.apache.flink.configuration.Configuration; | ||
import org.apache.flink.streaming.api.functions.ProcessFunction; | ||
import org.apache.flink.util.Collector; | ||
import org.apache.flink.util.OutputTag; | ||
import org.apache.iceberg.ExpireSnapshots; | ||
import org.apache.iceberg.Table; | ||
import org.apache.iceberg.flink.TableLoader; | ||
import org.apache.iceberg.relocated.com.google.common.base.Preconditions; | ||
import org.apache.iceberg.relocated.com.google.common.collect.Lists; | ||
import org.apache.iceberg.util.ThreadPools; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* Calls the {@link ExpireSnapshots} to remove the old snapshots and emits the filenames which could | ||
* be removed in the {@link #DELETE_STREAM} side output. | ||
*/ | ||
public class ExpireSnapshotsProcessor extends ProcessFunction<Trigger, TaskResult> { | ||
private static final Logger LOG = LoggerFactory.getLogger(ExpireSnapshotsProcessor.class); | ||
public static final OutputTag<String> DELETE_STREAM = | ||
new OutputTag<>("delete-stream", Types.STRING); | ||
|
||
private final TableLoader tableLoader; | ||
private final Long minAgeMs; | ||
private final Integer retainLast; | ||
private final int plannerPoolSize; | ||
private transient ExecutorService plannerPool; | ||
private transient Table table; | ||
|
||
public ExpireSnapshotsProcessor( | ||
TableLoader tableLoader, Long minAgeMs, Integer retainLast, int plannerPoolSize) { | ||
Preconditions.checkNotNull(tableLoader, "Table loader should no be null"); | ||
|
||
this.tableLoader = tableLoader; | ||
this.minAgeMs = minAgeMs; | ||
this.retainLast = retainLast; | ||
this.plannerPoolSize = plannerPoolSize; | ||
} | ||
|
||
@Override | ||
public void open(Configuration parameters) throws Exception { | ||
tableLoader.open(); | ||
this.table = tableLoader.loadTable(); | ||
this.plannerPool = ThreadPools.newWorkerPool(table.name() + "-table--planner", plannerPoolSize); | ||
} | ||
|
||
@Override | ||
public void processElement(Trigger trigger, Context ctx, Collector<TaskResult> out) | ||
throws Exception { | ||
try { | ||
table.refresh(); | ||
ExpireSnapshots expireSnapshots = table.expireSnapshots(); | ||
if (minAgeMs != null) { | ||
expireSnapshots = expireSnapshots.expireOlderThan(ctx.timestamp() - minAgeMs); | ||
} | ||
|
||
if (retainLast != null) { | ||
expireSnapshots = expireSnapshots.retainLast(retainLast); | ||
} | ||
|
||
expireSnapshots | ||
.planWith(plannerPool) | ||
.deleteWith(file -> ctx.output(DELETE_STREAM, file)) | ||
.cleanExpiredFiles(true) | ||
.commit(); | ||
|
||
LOG.info("Successfully finished expiring snapshots for {} at {}", table, ctx.timestamp()); | ||
out.collect( | ||
new TaskResult(trigger.taskId(), trigger.timestamp(), true, Collections.emptyList())); | ||
} catch (Exception e) { | ||
LOG.info("Exception expiring snapshots for {} at {}", table, ctx.timestamp(), e); | ||
out.collect( | ||
new TaskResult(trigger.taskId(), trigger.timestamp(), false, Lists.newArrayList(e))); | ||
} | ||
} | ||
} |
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
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.