Skip to content

Commit

Permalink
[flink] support computing the changelog generated by compact during r…
Browse files Browse the repository at this point in the history
…ead time.

This is used when changelog producer is none, but CoreOptions#needLookup is true and the table is used as a dim table.
  • Loading branch information
liming30 committed Sep 2, 2024
1 parent 16734b8 commit e65e3ea
Show file tree
Hide file tree
Showing 16 changed files with 375 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2362,7 +2362,8 @@ public static StreamingReadMode fromValue(String value) {
public enum StreamScanMode implements DescribedEnum {
NONE("none", "No requirement."),
COMPACT_BUCKET_TABLE("compact-bucket-table", "Compaction for traditional bucket table."),
FILE_MONITOR("file-monitor", "Monitor data file changes.");
FILE_MONITOR("file-monitor", "Monitor data file changes."),
COMPACT_DELTA_MONITOR("compact-delta-monitor", "Monitor delta changes for compaction.");

private final String value;
private final String description;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,11 @@ public ScanMode scanMode() {
public List<ManifestEntry> files() {
return files;
}

@Override
public Snapshot.CommitKind commitKind() {
return readSnapshot == null ? null : readSnapshot.commitKind();
}
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ default List<ManifestEntry> files(FileKind kind) {
return files().stream().filter(e -> e.kind() == kind).collect(Collectors.toList());
}

/** {@link org.apache.paimon.Snapshot.CommitKind} of the snapshot. */
Snapshot.CommitKind commitKind();

/** Return a map group by partition and bucket. */
static Map<BinaryRow, Map<Integer, List<DataFileMeta>>> groupByPartFiles(
List<ManifestEntry> files) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ protected StartingScanner createStartingScanner(boolean isStreaming) {
isStreaming, "Set 'streaming-compact' in batch mode. This is unexpected.");
return new ContinuousCompactorStartingScanner(snapshotManager);
case FILE_MONITOR:
case COMPACT_DELTA_MONITOR:
return new FullStartingScanner(snapshotManager);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package org.apache.paimon.table.source;

import org.apache.paimon.Snapshot;
import org.apache.paimon.data.BinaryRow;
import org.apache.paimon.io.DataFileMeta;
import org.apache.paimon.io.DataFileMeta08Serializer;
Expand Down Expand Up @@ -65,6 +66,9 @@ public class DataSplit implements Split {
private boolean isStreaming = false;
private boolean rawConvertible;

// Internal use only, not involved in serde.
@Nullable private transient Snapshot.CommitKind commitKind;

public DataSplit() {}

public long snapshotId() {
Expand Down Expand Up @@ -108,6 +112,11 @@ public boolean rawConvertible() {
return rawConvertible;
}

@Nullable
public Snapshot.CommitKind commitKind() {
return commitKind;
}

public OptionalLong latestFileCreationEpochMillis() {
return this.dataFiles.stream().mapToLong(DataFileMeta::creationTimeEpochMillis).max();
}
Expand Down Expand Up @@ -230,6 +239,7 @@ private void assign(DataSplit other) {
this.dataDeletionFiles = other.dataDeletionFiles;
this.isStreaming = other.isStreaming;
this.rawConvertible = other.rawConvertible;
this.commitKind = other.commitKind;
}

public void serialize(DataOutputView out) throws IOException {
Expand Down Expand Up @@ -387,6 +397,11 @@ public Builder rawConvertible(boolean rawConvertible) {
return this;
}

public Builder withCommitKind(Snapshot.CommitKind commitKind) {
this.split.commitKind = commitKind;
return this;
}

public DataSplit build() {
checkArgument(split.partition != null);
checkArgument(split.bucket != -1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.apache.paimon.table.source.snapshot.AllDeltaFollowUpScanner;
import org.apache.paimon.table.source.snapshot.BoundedChecker;
import org.apache.paimon.table.source.snapshot.CompactionChangelogFollowUpScanner;
import org.apache.paimon.table.source.snapshot.CompactionFollowUpScanner;
import org.apache.paimon.table.source.snapshot.DeltaFollowUpScanner;
import org.apache.paimon.table.source.snapshot.FollowUpScanner;
import org.apache.paimon.table.source.snapshot.InputChangelogFollowUpScanner;
Expand Down Expand Up @@ -227,6 +228,8 @@ private FollowUpScanner createFollowUpScanner() {
return new DeltaFollowUpScanner();
case FILE_MONITOR:
return new AllDeltaFollowUpScanner();
case COMPACT_DELTA_MONITOR:
return new CompactionFollowUpScanner();
}

CoreOptions.ChangelogProducer changelogProducer = options.changelogProducer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.apache.paimon.reader.RecordReader;
import org.apache.paimon.schema.TableSchema;
import org.apache.paimon.table.source.splitread.IncrementalChangelogReadProvider;
import org.apache.paimon.table.source.splitread.IncrementalCompactDiffReadProvider;
import org.apache.paimon.table.source.splitread.IncrementalDiffReadProvider;
import org.apache.paimon.table.source.splitread.MergeFileSplitReadProvider;
import org.apache.paimon.table.source.splitread.RawFileSplitReadProvider;
Expand Down Expand Up @@ -62,6 +63,8 @@ public KeyValueTableRead(
Arrays.asList(
new RawFileSplitReadProvider(batchRawReadSupplier, this::assignValues),
new MergeFileSplitReadProvider(mergeReadSupplier, this::assignValues),
new IncrementalCompactDiffReadProvider(
mergeReadSupplier, this::assignValues),
new IncrementalChangelogReadProvider(mergeReadSupplier, this::assignValues),
new IncrementalDiffReadProvider(mergeReadSupplier, this::assignValues));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* 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.table.source.snapshot;

import org.apache.paimon.Snapshot;
import org.apache.paimon.table.source.ScanMode;

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

/** {@link FollowUpScanner} for read all changed files after compact. */
public class CompactionFollowUpScanner implements FollowUpScanner {

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

@Override
public boolean shouldScanSnapshot(Snapshot snapshot) {
if (snapshot.commitKind() == Snapshot.CommitKind.COMPACT) {
return true;
}

LOG.debug(
"Next snapshot id {} is not COMPACT, but is {}, check next one.",
snapshot.id(),
snapshot.commitKind());
return false;
}

@Override
public SnapshotReader.Plan scan(Snapshot snapshot, SnapshotReader snapshotReader) {
return snapshotReader.withMode(ScanMode.DELTA).withSnapshot(snapshot).readChanges();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,8 @@ private Plan toChangesPlan(
.withBeforeFiles(before)
.withDataFiles(data)
.isStreaming(isStreaming)
.withBucketPath(pathFactory.bucketPath(part, bucket).toString());
.withBucketPath(pathFactory.bucketPath(part, bucket).toString())
.withCommitKind(plan.commitKind());
if (deletionVectors) {
builder.withBeforeDeletionFiles(
getDeletionFiles(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* 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.table.source.splitread;

import org.apache.paimon.Snapshot;
import org.apache.paimon.data.InternalRow;
import org.apache.paimon.operation.MergeFileSplitRead;
import org.apache.paimon.operation.SplitRead;
import org.apache.paimon.table.source.DataSplit;
import org.apache.paimon.utils.LazyField;

import java.util.function.Consumer;
import java.util.function.Supplier;

/** A {@link SplitReadProvider} to streaming incremental diff read after compaction. */
public class IncrementalCompactDiffReadProvider implements SplitReadProvider {

private final LazyField<SplitRead<InternalRow>> splitRead;

public IncrementalCompactDiffReadProvider(
Supplier<MergeFileSplitRead> supplier,
Consumer<SplitRead<InternalRow>> valuesAssigner) {
this.splitRead =
new LazyField<>(
() -> {
SplitRead<InternalRow> read = create(supplier);
valuesAssigner.accept(read);
return read;
});
}

private SplitRead<InternalRow> create(Supplier<MergeFileSplitRead> supplier) {
return new IncrementalCompactDiffSplitRead(supplier.get());
}

@Override
public boolean match(DataSplit split, boolean forceKeepDelete) {
return split.commitKind() == Snapshot.CommitKind.COMPACT
&& !split.beforeFiles().isEmpty()
&& split.isStreaming();
}

@Override
public boolean initialized() {
return splitRead.initialized();
}

@Override
public SplitRead<InternalRow> getOrCreate() {
return splitRead.get();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* 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.table.source.splitread;

import org.apache.paimon.data.InternalRow;
import org.apache.paimon.io.DataFileMeta;
import org.apache.paimon.operation.MergeFileSplitRead;
import org.apache.paimon.operation.SplitRead;
import org.apache.paimon.reader.EmptyRecordReader;
import org.apache.paimon.reader.RecordReader;
import org.apache.paimon.table.source.DataSplit;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

/** A {@link SplitRead} for streaming incremental diff after compaction. */
public class IncrementalCompactDiffSplitRead extends IncrementalDiffSplitRead {

public IncrementalCompactDiffSplitRead(MergeFileSplitRead mergeRead) {
super(mergeRead);
}

@Override
public RecordReader<InternalRow> createReader(DataSplit split) throws IOException {
if (split.beforeFiles().stream().noneMatch(file -> file.level() == 0)) {
return new EmptyRecordReader<>();
}
return super.createReader(filterLevel0Files(split));
}

private DataSplit filterLevel0Files(DataSplit split) {
List<DataFileMeta> beforeFiles =
split.beforeFiles().stream()
.filter(file -> file.level() > 0)
.collect(Collectors.toList());
List<DataFileMeta> afterFiles =
split.dataFiles().stream()
.filter(file -> file.level() > 0)
.collect(Collectors.toList());
DataSplit.Builder builder =
new DataSplit.Builder()
.withSnapshot(split.snapshotId())
.withPartition(split.partition())
.withBucket(split.bucket())
.withBucketPath(split.bucketPath())
.withBeforeFiles(beforeFiles)
.withDataFiles(afterFiles)
.isStreaming(split.isStreaming())
.rawConvertible(split.rawConvertible())
.withCommitKind(split.commitKind());

if (split.beforeDeletionFiles().isPresent()) {
builder.withBeforeDeletionFiles(split.beforeDeletionFiles().get());
}
if (split.deletionFiles().isPresent()) {
builder.withDataDeletionFiles(split.deletionFiles().get());
}
return builder.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,21 @@ protected void write(Table table, IOManager ioManager, InternalRow... rows) thro
}

protected void compact(Table table, BinaryRow partition, int bucket) throws Exception {
compact(table, partition, bucket, null, true);
}

protected void compact(
Table table,
BinaryRow partition,
int bucket,
IOManager ioManager,
boolean fullCompaction)
throws Exception {
BatchWriteBuilder writeBuilder = table.newBatchWriteBuilder();
try (BatchTableWrite write = writeBuilder.newWrite();
BatchTableCommit commit = writeBuilder.newCommit()) {
write.compact(partition, bucket, true);
write.withIOManager(ioManager);
write.compact(partition, bucket, fullCompaction);
commit.commit(write.prepareCommit());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ public class FileStoreLookupFunction implements Serializable, Closeable {

public FileStoreLookupFunction(
Table table, int[] projection, int[] joinKeyIndex, @Nullable Predicate predicate) {
TableScanUtils.streamingReadingValidate(table);
if (!TableScanUtils.supportCompactDiffStreamingReading(table)) {
TableScanUtils.streamingReadingValidate(table);
}

this.table = table;
this.partitionLoader = DynamicPartitionLoader.of(table);
Expand Down
Loading

0 comments on commit e65e3ea

Please sign in to comment.