Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 30 additions & 5 deletions core/src/main/java/org/apache/iceberg/SetStatistics.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,31 @@
*/
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.util.List;
import java.util.Map;
import java.util.Optional;
import org.apache.iceberg.exceptions.CommitFailedException;
import org.apache.iceberg.relocated.com.google.common.collect.Maps;
import org.apache.iceberg.util.Tasks;

public class SetStatistics implements UpdateStatistics {

private final TableOperations ops;
private final Map<Long, Optional<StatisticsFile>> statisticsToSet = Maps.newHashMap();
private TableMetadata base;

public SetStatistics(TableOperations ops) {
this.ops = ops;
this.base = ops.current();
}

@Override
Expand All @@ -45,17 +59,28 @@ public UpdateStatistics removeStatistics(long snapshotId) {

@Override
public List<StatisticsFile> apply() {
return internalApply(ops.current()).statisticsFiles();
return internalApply().statisticsFiles();
}

@Override
public void commit() {
TableMetadata base = ops.current();
TableMetadata newMetadata = internalApply(base);
ops.commit(base, newMetadata);
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(
item -> {
TableMetadata updated = internalApply();
ops.commit(base, updated);
});
}

private TableMetadata internalApply(TableMetadata base) {
private TableMetadata internalApply() {
this.base = ops.refresh();
TableMetadata.Builder builder = TableMetadata.buildFrom(base);
statisticsToSet.forEach(
(snapshotId, statistics) -> {
Expand Down
58 changes: 58 additions & 0 deletions core/src/test/java/org/apache/iceberg/TestSetStatistics.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
package org.apache.iceberg;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import org.apache.iceberg.exceptions.CommitFailedException;
import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList;
import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
import org.junit.jupiter.api.TestTemplate;
Expand Down Expand Up @@ -106,4 +108,60 @@ public void testRemoveStatistics() {
assertThat(version()).isEqualTo(3);
assertThat(metadata.statisticsFiles()).isEmpty();
}

@TestTemplate
public void testSetStatisticsRetryWithConcurrentModification() {
table.newFastAppend().appendFile(FILE_A).commit();
TableMetadata base = readMetadata();
long snapshotId = base.currentSnapshot().snapshotId();

GenericStatisticsFile statisticsFile =
new GenericStatisticsFile(
snapshotId, "/some/statistics/file.puffin", 100, 42, ImmutableList.of());

UpdateStatistics updateStats = table.updateStatistics().setStatistics(statisticsFile);

table.newFastAppend().appendFile(FILE_B).commit();

updateStats.commit();

assertThat(readMetadata().statisticsFiles()).containsExactly(statisticsFile);
}

@TestTemplate
public void testSetStatisticsRetryExhaustion() {
table.updateProperties().set(TableProperties.COMMIT_NUM_RETRIES, "2").commit();

table.newFastAppend().appendFile(FILE_A).commit();
long snapshotId = readMetadata().currentSnapshot().snapshotId();

GenericStatisticsFile statisticsFile =
new GenericStatisticsFile(
snapshotId, "/some/statistics/file.puffin", 100, 42, ImmutableList.of());

TestTables.TestTableOperations ops = table.ops();
ops.failCommits(3);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit, I think $maxAttempt+1 would be helpful here like

    ops.failCommits(TableProperties.COMMIT_NUM_RETRIES_DEFAULT + 1);


UpdateStatistics updateStats = table.updateStatistics().setStatistics(statisticsFile);
assertThatThrownBy(updateStats::commit)
.isInstanceOf(CommitFailedException.class)
.hasMessage("Injected failure");
}

@TestTemplate
public void testSetStatisticsRetrySuccess() {
table.newFastAppend().appendFile(FILE_A).commit();
long snapshotId = readMetadata().currentSnapshot().snapshotId();

GenericStatisticsFile statisticsFile =
new GenericStatisticsFile(
snapshotId, "/some/statistics/file.puffin", 100, 42, ImmutableList.of());

TestTables.TestTableOperations ops = table.ops();
ops.failCommits(2);

table.updateStatistics().setStatistics(statisticsFile).commit();

assertThat(readMetadata().statisticsFiles()).containsExactly(statisticsFile);
}
}