Skip to content

Commit

Permalink
[flink] support to report statistic to hms after partition idle
Browse files Browse the repository at this point in the history
  • Loading branch information
Aitozi committed Oct 29, 2024
1 parent 3c953be commit fcaab8a
Show file tree
Hide file tree
Showing 12 changed files with 537 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@
<td>Duration</td>
<td>Set a time duration when a partition has no new data after this time duration, mark the done status to indicate that the data is ready.</td>
</tr>
<tr>
<td><h5>partition.idle-time-to-report-statistic</h5></td>
<td style="word-wrap: break-word;">1 h</td>
<td>Duration</td>
<td>Set a time duration when a partition has no new data after this time duration, start to report the partition statistics to hms.</td>
</tr>
<tr>
<td><h5>partition.time-interval</h5></td>
<td style="word-wrap: break-word;">(none)</td>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import java.io.Serializable;
import java.util.LinkedHashMap;
import java.util.Map;

/**
* A metastore client related to a table. All methods of this interface operate on the same specific
Expand All @@ -37,6 +38,12 @@ public interface MetastoreClient extends AutoCloseable {

void markDone(LinkedHashMap<String, String> partitionSpec) throws Exception;

void alterPartition(
LinkedHashMap<String, String> partitionSpec,
Map<String, String> parameters,
long modifyTime)
throws Exception;

/** Factory to create {@link MetastoreClient}. */
interface Factory extends Serializable {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,14 @@ public class FlinkConnectorOptions {
"You can specify time interval for partition, for example, "
+ "daily partition is '1 d', hourly partition is '1 h'.");

public static final ConfigOption<Duration> PARTITION_IDLE_TIME_TO_REPORT_STATISTIC =
key("partition.idle-time-to-report-statistic")
.durationType()
.defaultValue(Duration.ofHours(1))
.withDescription(
"Set a time duration when a partition has no new data after this time duration, "
+ "start to report the partition statistics to hms.");

public static final ConfigOption<String> CLUSTERING_COLUMNS =
key("sink.clustering.by-columns")
.stringType()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import org.apache.paimon.annotation.VisibleForTesting;
import org.apache.paimon.flink.metrics.FlinkMetricRegistry;
import org.apache.paimon.flink.sink.partition.PartitionListeners;
import org.apache.paimon.flink.sink.partition.PartitionMarkDone;
import org.apache.paimon.io.DataFileMeta;
import org.apache.paimon.manifest.ManifestCommittable;
import org.apache.paimon.table.FileStoreTable;
Expand All @@ -44,7 +43,6 @@ public class StoreCommitter implements Committer<Committable, ManifestCommittabl

private final TableCommitImpl commit;
@Nullable private final CommitterMetrics committerMetrics;
@Nullable private final PartitionMarkDone partitionMarkDone;
private final PartitionListeners partitionListeners;

public StoreCommitter(FileStoreTable table, TableCommit commit, Context context) {
Expand All @@ -58,18 +56,7 @@ public StoreCommitter(FileStoreTable table, TableCommit commit, Context context)
}

try {
this.partitionMarkDone =
PartitionMarkDone.create(
context.streamingCheckpointEnabled(),
context.isRestored(),
context.stateStore(),
table);
this.partitionListeners =
PartitionListeners.create(
context.streamingCheckpointEnabled(),
context.isRestored(),
context.stateStore(),
table);
this.partitionListeners = PartitionListeners.create(context, table);
} catch (Exception e) {
throw new RuntimeException(e);
}
Expand Down Expand Up @@ -119,29 +106,20 @@ public void commit(List<ManifestCommittable> committables)
throws IOException, InterruptedException {
commit.commitMultiple(committables, false);
calcNumBytesAndRecordsOut(committables);
if (partitionMarkDone != null) {
partitionMarkDone.notifyCommittable(committables);
}
partitionListeners.notifyCommittable(committables);
}

@Override
public int filterAndCommit(
List<ManifestCommittable> globalCommittables, boolean checkAppendFiles) {
int committed = commit.filterAndCommitMultiple(globalCommittables, checkAppendFiles);
if (partitionMarkDone != null) {
partitionMarkDone.notifyCommittable(globalCommittables);
}
partitionListeners.notifyCommittable(globalCommittables);
return committed;
}

@Override
public Map<Long, List<Committable>> groupByCheckpoint(Collection<Committable> committables) {
try {
if (partitionMarkDone != null) {
partitionMarkDone.snapshotState();
}
partitionListeners.snapshotState();
} catch (Exception e) {
throw new RuntimeException(e);
Expand All @@ -157,9 +135,6 @@ public Map<Long, List<Committable>> groupByCheckpoint(Collection<Committable> co
@Override
public void close() throws Exception {
commit.close();
if (partitionMarkDone != null) {
partitionMarkDone.close();
}
partitionListeners.close();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* 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.partition;

import org.apache.paimon.Snapshot;
import org.apache.paimon.fs.Path;
import org.apache.paimon.io.DataFileMeta;
import org.apache.paimon.metastore.MetastoreClient;
import org.apache.paimon.table.FileStoreTable;
import org.apache.paimon.table.source.DataSplit;
import org.apache.paimon.table.source.DeletionFile;
import org.apache.paimon.table.source.ScanMode;
import org.apache.paimon.table.source.snapshot.SnapshotReader;
import org.apache.paimon.utils.Preconditions;
import org.apache.paimon.utils.SnapshotManager;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Closeable;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import static org.apache.paimon.utils.PartitionPathUtils.extractPartitionSpecFromPath;

/** Action to report the table statistic from the latest snapshot to HMS. */
public class HmsReporter implements Closeable {

private static final Logger LOG = LoggerFactory.getLogger(HmsReporter.class);

private final MetastoreClient metastoreClient;
private final SnapshotReader snapshotReader;
private final SnapshotManager snapshotManager;

public HmsReporter(FileStoreTable table, MetastoreClient client) {
this.metastoreClient =
Preconditions.checkNotNull(client, "the metastore client factory is null");
this.snapshotReader = table.newSnapshotReader();
this.snapshotManager = table.snapshotManager();
}

public void report(String partition, long modifyTime) throws Exception {
Snapshot snapshot = snapshotManager.latestSnapshot();
if (snapshot != null) {
LinkedHashMap<String, String> partitionSpec =
extractPartitionSpecFromPath(new Path(partition));
List<DataSplit> splits =
new ArrayList<>(
snapshotReader
.withMode(ScanMode.ALL)
.withPartitionFilter(partitionSpec)
.withSnapshot(snapshot)
.read()
.dataSplits());
long rowCount = 0;
long totalSize = 0;
long fileCount = 0;
for (DataSplit split : splits) {
List<DataFileMeta> fileMetas = split.dataFiles();
rowCount += split.rowCount();
fileCount += fileMetas.size();
for (DataFileMeta fileMeta : fileMetas) {
totalSize += fileMeta.fileSize();
}

if (split.deletionFiles().isPresent()) {
fileCount += split.deletionFiles().get().size();
totalSize +=
split.deletionFiles().get().stream()
.map(DeletionFile::length)
.reduce(0L, Long::sum);
}
}
Map<String, String> statistic = new HashMap<>();
// refer to org.apache.hadoop.hive.common.StatsSetupConst
statistic.put("numFiles", String.valueOf(fileCount));
statistic.put("totalSize", String.valueOf(totalSize));
statistic.put("numRows", String.valueOf(rowCount));
// refer to org.apache.hadoop.hive.metastore.api.hive_metastoreConstants
statistic.put("transient_lastDdlTime", String.valueOf(modifyTime / 1000));

LOG.info("alter partition {} with statistic {}.", partition, statistic);
metastoreClient.alterPartition(partitionSpec, statistic, modifyTime);
}
}

@Override
public void close() throws IOException {
try {
metastoreClient.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,17 @@

package org.apache.paimon.flink.sink.partition;

import org.apache.paimon.flink.sink.Committer;
import org.apache.paimon.manifest.ManifestCommittable;
import org.apache.paimon.table.FileStoreTable;
import org.apache.paimon.utils.IOUtils;

import org.apache.flink.api.common.state.OperatorStateStore;

import java.io.Closeable;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/** Partition collector. */
/** Partition listeners. */
public class PartitionListeners implements Closeable {

private final List<PartitionListener> listeners;
Expand All @@ -55,13 +54,19 @@ public void close() throws IOException {
IOUtils.closeAllQuietly(listeners);
}

public static PartitionListeners create(
boolean isStreaming,
boolean isRestored,
OperatorStateStore stateStore,
FileStoreTable table)
public static PartitionListeners create(Committer.Context context, FileStoreTable table)
throws Exception {
List<PartitionListener> listeners = new ArrayList<>();

ReportHmsListener.create(context.isRestored(), context.stateStore(), table)
.ifPresent(listeners::add);
PartitionMarkDone.create(
context.streamingCheckpointEnabled(),
context.isRestored(),
context.stateStore(),
table)
.ifPresent(listeners::add);

return new PartitionListeners(new ArrayList<>());
return new PartitionListeners(listeners);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,28 +33,25 @@

import org.apache.flink.api.common.state.OperatorStateStore;

import javax.annotation.Nullable;

import java.io.Closeable;
import java.io.IOException;
import java.time.Duration;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;

import static org.apache.paimon.CoreOptions.PARTITION_MARK_DONE_WHEN_END_INPUT;
import static org.apache.paimon.flink.FlinkConnectorOptions.PARTITION_IDLE_TIME_TO_DONE;

/** Mark partition done. */
public class PartitionMarkDone implements Closeable {
public class PartitionMarkDone implements PartitionListener {

private final InternalRowPartitionComputer partitionComputer;
private final PartitionMarkDoneTrigger trigger;
private final List<PartitionMarkDoneAction> actions;
private final boolean waitCompaction;

@Nullable
public static PartitionMarkDone create(
public static Optional<PartitionMarkDone> create(
boolean isStreaming,
boolean isRestored,
OperatorStateStore stateStore,
Expand All @@ -64,7 +61,7 @@ public static PartitionMarkDone create(
Options options = coreOptions.toConfiguration();

if (disablePartitionMarkDone(isStreaming, table, options)) {
return null;
return Optional.empty();
}

InternalRowPartitionComputer partitionComputer =
Expand All @@ -87,7 +84,8 @@ public static PartitionMarkDone create(
&& (coreOptions.deletionVectorsEnabled()
|| coreOptions.mergeEngine() == MergeEngine.FIRST_ROW);

return new PartitionMarkDone(partitionComputer, trigger, actions, waitCompaction);
return Optional.of(
new PartitionMarkDone(partitionComputer, trigger, actions, waitCompaction));
}

private static boolean disablePartitionMarkDone(
Expand Down Expand Up @@ -116,6 +114,7 @@ public PartitionMarkDone(
this.waitCompaction = waitCompaction;
}

@Override
public void notifyCommittable(List<ManifestCommittable> committables) {
Set<BinaryRow> partitions = new HashSet<>();
boolean endInput = false;
Expand Down Expand Up @@ -153,6 +152,7 @@ public static void markDone(List<String> partitions, List<PartitionMarkDoneActio
}
}

@Override
public void snapshotState() throws Exception {
trigger.snapshotState();
}
Expand Down
Loading

0 comments on commit fcaab8a

Please sign in to comment.