Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allows setting custom snapshot timestamp through properties #1

Open
wants to merge 2 commits into
base: 1.4.x
Choose a base branch
from
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
4 changes: 3 additions & 1 deletion core/src/main/java/org/apache/iceberg/RemoveSnapshots.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ public void accept(String file) {
PropertyUtil.propertyAsLong(
base.properties(), MAX_SNAPSHOT_AGE_MS, MAX_SNAPSHOT_AGE_MS_DEFAULT);

this.now = System.currentTimeMillis();
this.now =
ops.current()
.propertyAsLong(TableProperties.SNAPSHOT_TIMESTAMP, System.currentTimeMillis());
this.defaultExpireOlderThan = now - defaultMaxSnapshotAgeMs;
this.defaultMinNumSnapshots =
PropertyUtil.propertyAsInt(
Expand Down
6 changes: 5 additions & 1 deletion core/src/main/java/org/apache/iceberg/SnapshotProducer.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import static org.apache.iceberg.TableProperties.COMMIT_TOTAL_RETRY_TIME_MS_DEFAULT;
import static org.apache.iceberg.TableProperties.MANIFEST_TARGET_SIZE_BYTES;
import static org.apache.iceberg.TableProperties.MANIFEST_TARGET_SIZE_BYTES_DEFAULT;
import static org.apache.iceberg.TableProperties.SNAPSHOT_TIMESTAMP;

import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;
Expand Down Expand Up @@ -252,11 +253,14 @@ public Snapshot apply() {
throw new RuntimeIOException(e, "Failed to write manifest list file");
}

final long timeToSet =
ops.current().propertyAsLong(SNAPSHOT_TIMESTAMP, System.currentTimeMillis());

return new BaseSnapshot(
sequenceNumber,
snapshotId(),
parentSnapshotId,
System.currentTimeMillis(),
timeToSet,
operation(),
summary(base),
base.currentSchemaId(),
Expand Down
19 changes: 17 additions & 2 deletions core/src/main/java/org/apache/iceberg/TableMetadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -1216,7 +1216,11 @@ public Builder setRef(String name, SnapshotRef ref) {
if (SnapshotRef.MAIN_BRANCH.equals(name)) {
this.currentSnapshotId = ref.snapshotId();
if (lastUpdatedMillis == null) {
this.lastUpdatedMillis = System.currentTimeMillis();
this.lastUpdatedMillis =
Long.parseLong(
properties.getOrDefault(
TableProperties.SNAPSHOT_TIMESTAMP,
Long.toString(System.currentTimeMillis())));
}

snapshotLog.add(new SnapshotLogEntry(lastUpdatedMillis, ref.snapshotId()));
Expand Down Expand Up @@ -1343,6 +1347,14 @@ public Builder setProperties(Map<String, String> updated) {
return this;
}

if (updated.get(TableProperties.SNAPSHOT_TIMESTAMP) != null
&& base.lastUpdatedMillis != 0L
&& Long.parseLong(updated.get(TableProperties.SNAPSHOT_TIMESTAMP))
< base.lastUpdatedMillis) {
throw new IllegalArgumentException(
"Cannot set snapshot.timestamp to a time earlier than the latest snapshot");
}

properties.putAll(updated);
changes.add(new MetadataUpdate.SetProperties(updated));

Expand Down Expand Up @@ -1393,7 +1405,10 @@ public TableMetadata build() {
}

if (lastUpdatedMillis == null) {
this.lastUpdatedMillis = System.currentTimeMillis();
this.lastUpdatedMillis =
Long.parseLong(
properties.getOrDefault(
TableProperties.SNAPSHOT_TIMESTAMP, Long.toString(System.currentTimeMillis())));
}

// when associated with a metadata file, table metadata must have no changes so that the
Expand Down
2 changes: 2 additions & 0 deletions core/src/main/java/org/apache/iceberg/TableProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -360,4 +360,6 @@ private TableProperties() {}

public static final String UPSERT_ENABLED = "write.upsert.enabled";
public static final boolean UPSERT_ENABLED_DEFAULT = false;

public static final String SNAPSHOT_TIMESTAMP = "snapshot.timestamp";
}