|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +#include "iceberg/update/set_snapshot.h" |
| 21 | + |
| 22 | +#include <gmock/gmock.h> |
| 23 | +#include <gtest/gtest.h> |
| 24 | + |
| 25 | +#include "iceberg/result.h" |
| 26 | +#include "iceberg/snapshot.h" |
| 27 | +#include "iceberg/test/matchers.h" |
| 28 | +#include "iceberg/test/update_test_base.h" |
| 29 | +#include "iceberg/transaction.h" |
| 30 | + |
| 31 | +namespace iceberg { |
| 32 | + |
| 33 | +class SetSnapshotTest : public UpdateTestBase { |
| 34 | + protected: |
| 35 | + // Snapshot IDs from TableMetadataV2Valid.json |
| 36 | + static constexpr int64_t kOldestSnapshotId = 3051729675574597004; |
| 37 | + static constexpr int64_t kCurrentSnapshotId = 3055729675574597004; |
| 38 | + |
| 39 | + // Timestamps from TableMetadataV2Valid.json |
| 40 | + static constexpr int64_t kOldestSnapshotTimestamp = 1515100955770; |
| 41 | + static constexpr int64_t kCurrentSnapshotTimestamp = 1555100955770; |
| 42 | +}; |
| 43 | + |
| 44 | +TEST_F(SetSnapshotTest, SetCurrentSnapshotValid) { |
| 45 | + ICEBERG_UNWRAP_OR_FAIL(auto transaction, table_->NewTransaction()); |
| 46 | + ICEBERG_UNWRAP_OR_FAIL(auto set_snapshot, transaction->NewSetSnapshot()); |
| 47 | + EXPECT_EQ(set_snapshot->kind(), PendingUpdate::Kind::kSetSnapshot); |
| 48 | + |
| 49 | + set_snapshot->SetCurrentSnapshot(kOldestSnapshotId); |
| 50 | + |
| 51 | + ICEBERG_UNWRAP_OR_FAIL(auto snapshot_id, set_snapshot->Apply()); |
| 52 | + EXPECT_EQ(snapshot_id, kOldestSnapshotId); |
| 53 | + |
| 54 | + // Commit and verify the change was persisted |
| 55 | + EXPECT_THAT(set_snapshot->Commit(), IsOk()); |
| 56 | + EXPECT_THAT(transaction->Commit(), IsOk()); |
| 57 | + ICEBERG_UNWRAP_OR_FAIL(auto reloaded, catalog_->LoadTable(table_ident_)); |
| 58 | + ICEBERG_UNWRAP_OR_FAIL(auto current_snapshot, reloaded->current_snapshot()); |
| 59 | + EXPECT_EQ(current_snapshot->snapshot_id, kOldestSnapshotId); |
| 60 | +} |
| 61 | + |
| 62 | +TEST_F(SetSnapshotTest, SetCurrentSnapshotToCurrentSnapshot) { |
| 63 | + ICEBERG_UNWRAP_OR_FAIL(auto transaction, table_->NewTransaction()); |
| 64 | + ICEBERG_UNWRAP_OR_FAIL(auto set_snapshot, transaction->NewSetSnapshot()); |
| 65 | + set_snapshot->SetCurrentSnapshot(kCurrentSnapshotId); |
| 66 | + |
| 67 | + ICEBERG_UNWRAP_OR_FAIL(auto snapshot_id, set_snapshot->Apply()); |
| 68 | + EXPECT_EQ(snapshot_id, kCurrentSnapshotId); |
| 69 | +} |
| 70 | + |
| 71 | +TEST_F(SetSnapshotTest, SetCurrentSnapshotInvalid) { |
| 72 | + ICEBERG_UNWRAP_OR_FAIL(auto transaction, table_->NewTransaction()); |
| 73 | + ICEBERG_UNWRAP_OR_FAIL(auto set_snapshot, transaction->NewSetSnapshot()); |
| 74 | + // Try to set to a non-existent snapshot |
| 75 | + int64_t invalid_snapshot_id = 9999999999999999; |
| 76 | + set_snapshot->SetCurrentSnapshot(invalid_snapshot_id); |
| 77 | + |
| 78 | + auto result = set_snapshot->Apply(); |
| 79 | + EXPECT_THAT(result, IsError(ErrorKind::kValidationFailed)); |
| 80 | + EXPECT_THAT(result, HasErrorMessage("is not found")); |
| 81 | +} |
| 82 | + |
| 83 | +TEST_F(SetSnapshotTest, RollbackToValid) { |
| 84 | + ICEBERG_UNWRAP_OR_FAIL(auto transaction, table_->NewTransaction()); |
| 85 | + ICEBERG_UNWRAP_OR_FAIL(auto set_snapshot, transaction->NewSetSnapshot()); |
| 86 | + // Rollback to the oldest snapshot (which is an ancestor) |
| 87 | + set_snapshot->RollbackTo(kOldestSnapshotId); |
| 88 | + |
| 89 | + ICEBERG_UNWRAP_OR_FAIL(auto snapshot_id, set_snapshot->Apply()); |
| 90 | + EXPECT_EQ(snapshot_id, kOldestSnapshotId); |
| 91 | +} |
| 92 | + |
| 93 | +TEST_F(SetSnapshotTest, RollbackToInvalidSnapshot) { |
| 94 | + ICEBERG_UNWRAP_OR_FAIL(auto transaction, table_->NewTransaction()); |
| 95 | + ICEBERG_UNWRAP_OR_FAIL(auto set_snapshot, transaction->NewSetSnapshot()); |
| 96 | + // Try to rollback to a non-existent snapshot |
| 97 | + int64_t invalid_snapshot_id = 9999999999999999; |
| 98 | + set_snapshot->RollbackTo(invalid_snapshot_id); |
| 99 | + |
| 100 | + auto result = set_snapshot->Apply(); |
| 101 | + EXPECT_THAT(result, IsError(ErrorKind::kValidationFailed)); |
| 102 | + EXPECT_THAT(result, HasErrorMessage("unknown snapshot id")); |
| 103 | +} |
| 104 | + |
| 105 | +TEST_F(SetSnapshotTest, RollbackToTimeValidOldestSnapshot) { |
| 106 | + ICEBERG_UNWRAP_OR_FAIL(auto transaction, table_->NewTransaction()); |
| 107 | + ICEBERG_UNWRAP_OR_FAIL(auto set_snapshot, transaction->NewSetSnapshot()); |
| 108 | + // Rollback to a time between the two snapshots |
| 109 | + // This should select the oldest snapshot |
| 110 | + int64_t time_between = (kOldestSnapshotTimestamp + kCurrentSnapshotTimestamp) / 2; |
| 111 | + set_snapshot->RollbackToTime(time_between); |
| 112 | + |
| 113 | + ICEBERG_UNWRAP_OR_FAIL(auto snapshot_id, set_snapshot->Apply()); |
| 114 | + EXPECT_EQ(snapshot_id, kOldestSnapshotId); |
| 115 | +} |
| 116 | + |
| 117 | +TEST_F(SetSnapshotTest, RollbackToTimeBeforeAnySnapshot) { |
| 118 | + ICEBERG_UNWRAP_OR_FAIL(auto transaction, table_->NewTransaction()); |
| 119 | + ICEBERG_UNWRAP_OR_FAIL(auto set_snapshot, transaction->NewSetSnapshot()); |
| 120 | + // Try to rollback to a time before any snapshot |
| 121 | + int64_t time_before_all = kOldestSnapshotTimestamp - 1000000; |
| 122 | + set_snapshot->RollbackToTime(time_before_all); |
| 123 | + |
| 124 | + // Should fail - no snapshot older than the specified time |
| 125 | + auto result = set_snapshot->Apply(); |
| 126 | + EXPECT_THAT(result, IsError(ErrorKind::kValidationFailed)); |
| 127 | + EXPECT_THAT(result, HasErrorMessage("no valid snapshot older than")); |
| 128 | +} |
| 129 | + |
| 130 | +TEST_F(SetSnapshotTest, RollbackToTimeExactMatch) { |
| 131 | + ICEBERG_UNWRAP_OR_FAIL(auto transaction, table_->NewTransaction()); |
| 132 | + ICEBERG_UNWRAP_OR_FAIL(auto set_snapshot, transaction->NewSetSnapshot()); |
| 133 | + // Rollback to a timestamp just after the oldest snapshot |
| 134 | + int64_t time_just_after_oldest = kOldestSnapshotTimestamp + 1; |
| 135 | + set_snapshot->RollbackToTime(time_just_after_oldest); |
| 136 | + |
| 137 | + // Apply and verify - should return the oldest snapshot |
| 138 | + ICEBERG_UNWRAP_OR_FAIL(auto snapshot_id, set_snapshot->Apply()); |
| 139 | + EXPECT_EQ(snapshot_id, kOldestSnapshotId); |
| 140 | +} |
| 141 | + |
| 142 | +TEST_F(SetSnapshotTest, ApplyWithoutChanges) { |
| 143 | + ICEBERG_UNWRAP_OR_FAIL(auto transaction, table_->NewTransaction()); |
| 144 | + ICEBERG_UNWRAP_OR_FAIL(auto set_snapshot, transaction->NewSetSnapshot()); |
| 145 | + ICEBERG_UNWRAP_OR_FAIL(auto snapshot_id, set_snapshot->Apply()); |
| 146 | + EXPECT_EQ(snapshot_id, kCurrentSnapshotId); |
| 147 | + |
| 148 | + EXPECT_THAT(set_snapshot->Commit(), IsOk()); |
| 149 | + EXPECT_THAT(transaction->Commit(), IsOk()); |
| 150 | + ICEBERG_UNWRAP_OR_FAIL(auto reloaded, catalog_->LoadTable(table_ident_)); |
| 151 | + ICEBERG_UNWRAP_OR_FAIL(auto current_snapshot, reloaded->current_snapshot()); |
| 152 | + EXPECT_EQ(current_snapshot->snapshot_id, kCurrentSnapshotId); |
| 153 | +} |
| 154 | + |
| 155 | +} // namespace iceberg |
0 commit comments