|
| 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 <memory> |
| 23 | + |
| 24 | +#include <gmock/gmock.h> |
| 25 | +#include <gtest/gtest.h> |
| 26 | + |
| 27 | +#include "iceberg/result.h" |
| 28 | +#include "iceberg/snapshot.h" |
| 29 | +#include "iceberg/test/matchers.h" |
| 30 | +#include "iceberg/test/update_test_base.h" |
| 31 | +#include "iceberg/transaction.h" |
| 32 | + |
| 33 | +namespace iceberg { |
| 34 | + |
| 35 | +// Test fixture for SetSnapshot tests |
| 36 | +class SetSnapshotTest : public UpdateTestBase { |
| 37 | + protected: |
| 38 | + // Snapshot IDs from TableMetadataV2Valid.json |
| 39 | + static constexpr int64_t kOldestSnapshotId = 3051729675574597004; |
| 40 | + static constexpr int64_t kCurrentSnapshotId = 3055729675574597004; |
| 41 | + |
| 42 | + // Timestamps from TableMetadataV2Valid.json |
| 43 | + static constexpr int64_t kOldestSnapshotTimestamp = 1515100955770; |
| 44 | + static constexpr int64_t kCurrentSnapshotTimestamp = 1555100955770; |
| 45 | +}; |
| 46 | + |
| 47 | +TEST_F(SetSnapshotTest, SetCurrentSnapshotValid) { |
| 48 | + // Create transaction and SetSnapshot |
| 49 | + ICEBERG_UNWRAP_OR_FAIL(auto txn, table_->NewTransaction()); |
| 50 | + ICEBERG_UNWRAP_OR_FAIL(auto set_snapshot, SetSnapshot::Make(txn)); |
| 51 | + |
| 52 | + // Set current snapshot to the older snapshot |
| 53 | + set_snapshot->SetCurrentSnapshot(kOldestSnapshotId); |
| 54 | + |
| 55 | + // Apply and verify |
| 56 | + ICEBERG_UNWRAP_OR_FAIL(auto result, set_snapshot->Apply()); |
| 57 | + EXPECT_NE(result, nullptr); |
| 58 | + EXPECT_EQ(result->snapshot_id, kOldestSnapshotId); |
| 59 | +} |
| 60 | + |
| 61 | +TEST_F(SetSnapshotTest, SetCurrentSnapshotToCurrentSnapshot) { |
| 62 | + // Create transaction and SetSnapshot |
| 63 | + ICEBERG_UNWRAP_OR_FAIL(auto txn, table_->NewTransaction()); |
| 64 | + ICEBERG_UNWRAP_OR_FAIL(auto set_snapshot, SetSnapshot::Make(txn)); |
| 65 | + |
| 66 | + // Set current snapshot to the current snapshot (no-op) |
| 67 | + set_snapshot->SetCurrentSnapshot(kCurrentSnapshotId); |
| 68 | + |
| 69 | + // Apply and verify |
| 70 | + ICEBERG_UNWRAP_OR_FAIL(auto result, set_snapshot->Apply()); |
| 71 | + EXPECT_NE(result, nullptr); |
| 72 | + EXPECT_EQ(result->snapshot_id, kCurrentSnapshotId); |
| 73 | +} |
| 74 | + |
| 75 | +TEST_F(SetSnapshotTest, SetCurrentSnapshotInvalid) { |
| 76 | + // Create transaction and SetSnapshot |
| 77 | + ICEBERG_UNWRAP_OR_FAIL(auto txn, table_->NewTransaction()); |
| 78 | + ICEBERG_UNWRAP_OR_FAIL(auto set_snapshot, SetSnapshot::Make(txn)); |
| 79 | + |
| 80 | + // Try to set to a non-existent snapshot |
| 81 | + int64_t invalid_snapshot_id = 9999999999999999; |
| 82 | + set_snapshot->SetCurrentSnapshot(invalid_snapshot_id); |
| 83 | + |
| 84 | + // Should fail during Apply |
| 85 | + auto result = set_snapshot->Apply(); |
| 86 | + EXPECT_THAT(result, IsError(ErrorKind::kValidationFailed)); |
| 87 | + EXPECT_THAT(result, HasErrorMessage("unknown snapshot id")); |
| 88 | +} |
| 89 | + |
| 90 | +TEST_F(SetSnapshotTest, RollbackToValid) { |
| 91 | + // Create transaction and SetSnapshot |
| 92 | + ICEBERG_UNWRAP_OR_FAIL(auto txn, table_->NewTransaction()); |
| 93 | + ICEBERG_UNWRAP_OR_FAIL(auto set_snapshot, SetSnapshot::Make(txn)); |
| 94 | + |
| 95 | + // Rollback to the oldest snapshot (which is an ancestor) |
| 96 | + set_snapshot->RollbackTo(kOldestSnapshotId); |
| 97 | + |
| 98 | + // Apply and verify |
| 99 | + ICEBERG_UNWRAP_OR_FAIL(auto result, set_snapshot->Apply()); |
| 100 | + EXPECT_NE(result, nullptr); |
| 101 | + EXPECT_EQ(result->snapshot_id, kOldestSnapshotId); |
| 102 | +} |
| 103 | + |
| 104 | +TEST_F(SetSnapshotTest, RollbackToInvalidSnapshot) { |
| 105 | + // Create transaction and SetSnapshot |
| 106 | + ICEBERG_UNWRAP_OR_FAIL(auto txn, table_->NewTransaction()); |
| 107 | + ICEBERG_UNWRAP_OR_FAIL(auto set_snapshot, SetSnapshot::Make(txn)); |
| 108 | + |
| 109 | + // Try to rollback to a non-existent snapshot |
| 110 | + int64_t invalid_snapshot_id = 9999999999999999; |
| 111 | + set_snapshot->RollbackTo(invalid_snapshot_id); |
| 112 | + |
| 113 | + // Should fail during Apply |
| 114 | + auto result = set_snapshot->Apply(); |
| 115 | + EXPECT_THAT(result, IsError(ErrorKind::kValidationFailed)); |
| 116 | + EXPECT_THAT(result, HasErrorMessage("unknown snapshot id")); |
| 117 | +} |
| 118 | + |
| 119 | +TEST_F(SetSnapshotTest, RollbackToTimeValidOldestSnapshot) { |
| 120 | + // Create transaction and SetSnapshot |
| 121 | + ICEBERG_UNWRAP_OR_FAIL(auto txn, table_->NewTransaction()); |
| 122 | + ICEBERG_UNWRAP_OR_FAIL(auto set_snapshot, SetSnapshot::Make(txn)); |
| 123 | + |
| 124 | + // Rollback to a time between the two snapshots |
| 125 | + // This should select the oldest snapshot |
| 126 | + int64_t time_between = (kOldestSnapshotTimestamp + kCurrentSnapshotTimestamp) / 2; |
| 127 | + set_snapshot->RollbackToTime(time_between); |
| 128 | + |
| 129 | + // Apply and verify |
| 130 | + ICEBERG_UNWRAP_OR_FAIL(auto result, set_snapshot->Apply()); |
| 131 | + EXPECT_NE(result, nullptr); |
| 132 | + EXPECT_EQ(result->snapshot_id, kOldestSnapshotId); |
| 133 | +} |
| 134 | + |
| 135 | +TEST_F(SetSnapshotTest, RollbackToTimeBeforeAnySnapshot) { |
| 136 | + // Create transaction and SetSnapshot |
| 137 | + ICEBERG_UNWRAP_OR_FAIL(auto txn, table_->NewTransaction()); |
| 138 | + ICEBERG_UNWRAP_OR_FAIL(auto set_snapshot, SetSnapshot::Make(txn)); |
| 139 | + |
| 140 | + // Try to rollback to a time before any snapshot |
| 141 | + int64_t time_before_all = kOldestSnapshotTimestamp - 1000000; |
| 142 | + set_snapshot->RollbackToTime(time_before_all); |
| 143 | + |
| 144 | + // Should fail - no snapshot older than the specified time |
| 145 | + auto result = set_snapshot->Apply(); |
| 146 | + EXPECT_THAT(result, IsError(ErrorKind::kValidationFailed)); |
| 147 | + EXPECT_THAT(result, HasErrorMessage("no valid snapshot older than")); |
| 148 | +} |
| 149 | + |
| 150 | +TEST_F(SetSnapshotTest, RollbackToTimeExactMatch) { |
| 151 | + // Create transaction and SetSnapshot |
| 152 | + ICEBERG_UNWRAP_OR_FAIL(auto txn, table_->NewTransaction()); |
| 153 | + ICEBERG_UNWRAP_OR_FAIL(auto set_snapshot, SetSnapshot::Make(txn)); |
| 154 | + |
| 155 | + // Rollback to a timestamp just after the oldest snapshot |
| 156 | + // This should return the oldest snapshot (the latest one before this time) |
| 157 | + int64_t time_just_after_oldest = kOldestSnapshotTimestamp + 1; |
| 158 | + set_snapshot->RollbackToTime(time_just_after_oldest); |
| 159 | + |
| 160 | + // Apply and verify - should return the oldest snapshot |
| 161 | + ICEBERG_UNWRAP_OR_FAIL(auto result, set_snapshot->Apply()); |
| 162 | + EXPECT_NE(result, nullptr); |
| 163 | + EXPECT_EQ(result->snapshot_id, kOldestSnapshotId); |
| 164 | +} |
| 165 | + |
| 166 | +TEST_F(SetSnapshotTest, ApplyWithoutChanges) { |
| 167 | + // Create transaction and SetSnapshot |
| 168 | + ICEBERG_UNWRAP_OR_FAIL(auto txn, table_->NewTransaction()); |
| 169 | + ICEBERG_UNWRAP_OR_FAIL(auto set_snapshot, SetSnapshot::Make(txn)); |
| 170 | + |
| 171 | + // Apply without making any changes (NOOP) |
| 172 | + ICEBERG_UNWRAP_OR_FAIL(auto result, set_snapshot->Apply()); |
| 173 | + |
| 174 | + // Should return current snapshot |
| 175 | + EXPECT_NE(result, nullptr); |
| 176 | + EXPECT_EQ(result->snapshot_id, kCurrentSnapshotId); |
| 177 | +} |
| 178 | + |
| 179 | +TEST_F(SetSnapshotTest, MethodChaining) { |
| 180 | + // Create transaction and SetSnapshot |
| 181 | + ICEBERG_UNWRAP_OR_FAIL(auto txn, table_->NewTransaction()); |
| 182 | + ICEBERG_UNWRAP_OR_FAIL(auto set_snapshot, SetSnapshot::Make(txn)); |
| 183 | + |
| 184 | + // Test that methods return reference for chaining |
| 185 | + // Note: Only the last operation should take effect |
| 186 | + auto& result1 = set_snapshot->SetCurrentSnapshot(kOldestSnapshotId); |
| 187 | + EXPECT_EQ(&result1, set_snapshot.get()); |
| 188 | +} |
| 189 | + |
| 190 | +TEST_F(SetSnapshotTest, CommitSuccess) { |
| 191 | + // Create transaction and SetSnapshot |
| 192 | + ICEBERG_UNWRAP_OR_FAIL(auto txn, table_->NewTransaction()); |
| 193 | + ICEBERG_UNWRAP_OR_FAIL(auto set_snapshot, SetSnapshot::Make(txn)); |
| 194 | + |
| 195 | + // Set to oldest snapshot |
| 196 | + set_snapshot->SetCurrentSnapshot(kOldestSnapshotId); |
| 197 | + |
| 198 | + // Commit the change |
| 199 | + EXPECT_THAT(set_snapshot->Commit(), IsOk()); |
| 200 | + |
| 201 | + // Commit the transaction |
| 202 | + ICEBERG_UNWRAP_OR_FAIL(auto updated_table, txn->Commit()); |
| 203 | + |
| 204 | + // Verify the current snapshot was changed |
| 205 | + ICEBERG_UNWRAP_OR_FAIL(auto reloaded, catalog_->LoadTable(table_ident_)); |
| 206 | + ICEBERG_UNWRAP_OR_FAIL(auto current_snapshot, reloaded->current_snapshot()); |
| 207 | + EXPECT_EQ(current_snapshot->snapshot_id, kOldestSnapshotId); |
| 208 | +} |
| 209 | + |
| 210 | +TEST_F(SetSnapshotTest, CommitEmptyUpdate) { |
| 211 | + // Create transaction and SetSnapshot |
| 212 | + ICEBERG_UNWRAP_OR_FAIL(auto txn, table_->NewTransaction()); |
| 213 | + ICEBERG_UNWRAP_OR_FAIL(auto set_snapshot, SetSnapshot::Make(txn)); |
| 214 | + |
| 215 | + // Commit without making any changes (NOOP) |
| 216 | + EXPECT_THAT(set_snapshot->Commit(), IsOk()); |
| 217 | + |
| 218 | + // Commit the transaction |
| 219 | + ICEBERG_UNWRAP_OR_FAIL(auto updated_table, txn->Commit()); |
| 220 | + |
| 221 | + // Verify the current snapshot remained the same |
| 222 | + ICEBERG_UNWRAP_OR_FAIL(auto reloaded, catalog_->LoadTable(table_ident_)); |
| 223 | + ICEBERG_UNWRAP_OR_FAIL(auto current_snapshot, reloaded->current_snapshot()); |
| 224 | + EXPECT_EQ(current_snapshot->snapshot_id, kCurrentSnapshotId); |
| 225 | +} |
| 226 | + |
| 227 | +TEST_F(SetSnapshotTest, KindReturnsSetSnapshot) { |
| 228 | + // Create transaction and SetSnapshot |
| 229 | + ICEBERG_UNWRAP_OR_FAIL(auto txn, table_->NewTransaction()); |
| 230 | + ICEBERG_UNWRAP_OR_FAIL(auto set_snapshot, SetSnapshot::Make(txn)); |
| 231 | + |
| 232 | + // Verify the kind is correct |
| 233 | + EXPECT_EQ(set_snapshot->kind(), PendingUpdate::Kind::kSetSnapshot); |
| 234 | +} |
| 235 | + |
| 236 | +} // namespace iceberg |
0 commit comments