|
| 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 | + ICEBERG_UNWRAP_OR_FAIL(auto transaction, table_->NewTransaction()); |
| 49 | + ICEBERG_UNWRAP_OR_FAIL(auto set_snapshot, transaction->NewSetSnapshot()); |
| 50 | + EXPECT_EQ(set_snapshot->kind(), PendingUpdate::Kind::kSetSnapshot); |
| 51 | + |
| 52 | + set_snapshot->SetCurrentSnapshot(kOldestSnapshotId); |
| 53 | + |
| 54 | + ICEBERG_UNWRAP_OR_FAIL(auto snapshot_id, set_snapshot->Apply()); |
| 55 | + EXPECT_EQ(snapshot_id, kOldestSnapshotId); |
| 56 | + |
| 57 | + // Commit and verify the change was persisted |
| 58 | + EXPECT_THAT(set_snapshot->Commit(), IsOk()); |
| 59 | + EXPECT_THAT(transaction->Commit(), IsOk()); |
| 60 | + ICEBERG_UNWRAP_OR_FAIL(auto reloaded, catalog_->LoadTable(table_ident_)); |
| 61 | + ICEBERG_UNWRAP_OR_FAIL(auto current_snapshot, reloaded->current_snapshot()); |
| 62 | + EXPECT_EQ(current_snapshot->snapshot_id, kOldestSnapshotId); |
| 63 | +} |
| 64 | + |
| 65 | +TEST_F(SetSnapshotTest, SetCurrentSnapshotToCurrentSnapshot) { |
| 66 | + ICEBERG_UNWRAP_OR_FAIL(auto transaction, table_->NewTransaction()); |
| 67 | + ICEBERG_UNWRAP_OR_FAIL(auto set_snapshot, transaction->NewSetSnapshot()); |
| 68 | + set_snapshot->SetCurrentSnapshot(kCurrentSnapshotId); |
| 69 | + |
| 70 | + ICEBERG_UNWRAP_OR_FAIL(auto snapshot_id, set_snapshot->Apply()); |
| 71 | + EXPECT_EQ(snapshot_id, kCurrentSnapshotId); |
| 72 | +} |
| 73 | + |
| 74 | +TEST_F(SetSnapshotTest, SetCurrentSnapshotInvalid) { |
| 75 | + ICEBERG_UNWRAP_OR_FAIL(auto transaction, table_->NewTransaction()); |
| 76 | + ICEBERG_UNWRAP_OR_FAIL(auto set_snapshot, transaction->NewSetSnapshot()); |
| 77 | + // Try to set to a non-existent snapshot |
| 78 | + int64_t invalid_snapshot_id = 9999999999999999; |
| 79 | + set_snapshot->SetCurrentSnapshot(invalid_snapshot_id); |
| 80 | + |
| 81 | + auto result = set_snapshot->Apply(); |
| 82 | + EXPECT_THAT(result, IsError(ErrorKind::kValidationFailed)); |
| 83 | + EXPECT_THAT(result, HasErrorMessage("is not found")); |
| 84 | +} |
| 85 | + |
| 86 | +TEST_F(SetSnapshotTest, RollbackToValid) { |
| 87 | + ICEBERG_UNWRAP_OR_FAIL(auto transaction, table_->NewTransaction()); |
| 88 | + ICEBERG_UNWRAP_OR_FAIL(auto set_snapshot, transaction->NewSetSnapshot()); |
| 89 | + // Rollback to the oldest snapshot (which is an ancestor) |
| 90 | + set_snapshot->RollbackTo(kOldestSnapshotId); |
| 91 | + |
| 92 | + ICEBERG_UNWRAP_OR_FAIL(auto snapshot_id, set_snapshot->Apply()); |
| 93 | + EXPECT_EQ(snapshot_id, kOldestSnapshotId); |
| 94 | +} |
| 95 | + |
| 96 | +TEST_F(SetSnapshotTest, RollbackToInvalidSnapshot) { |
| 97 | + ICEBERG_UNWRAP_OR_FAIL(auto transaction, table_->NewTransaction()); |
| 98 | + ICEBERG_UNWRAP_OR_FAIL(auto set_snapshot, transaction->NewSetSnapshot()); |
| 99 | + // Try to rollback to a non-existent snapshot |
| 100 | + int64_t invalid_snapshot_id = 9999999999999999; |
| 101 | + set_snapshot->RollbackTo(invalid_snapshot_id); |
| 102 | + |
| 103 | + auto result = set_snapshot->Apply(); |
| 104 | + EXPECT_THAT(result, IsError(ErrorKind::kValidationFailed)); |
| 105 | + EXPECT_THAT(result, HasErrorMessage("unknown snapshot id")); |
| 106 | +} |
| 107 | + |
| 108 | +TEST_F(SetSnapshotTest, RollbackToTimeValidOldestSnapshot) { |
| 109 | + ICEBERG_UNWRAP_OR_FAIL(auto transaction, table_->NewTransaction()); |
| 110 | + ICEBERG_UNWRAP_OR_FAIL(auto set_snapshot, transaction->NewSetSnapshot()); |
| 111 | + // Rollback to a time between the two snapshots |
| 112 | + // This should select the oldest snapshot |
| 113 | + int64_t time_between = (kOldestSnapshotTimestamp + kCurrentSnapshotTimestamp) / 2; |
| 114 | + set_snapshot->RollbackToTime(time_between); |
| 115 | + |
| 116 | + ICEBERG_UNWRAP_OR_FAIL(auto snapshot_id, set_snapshot->Apply()); |
| 117 | + EXPECT_EQ(snapshot_id, kOldestSnapshotId); |
| 118 | +} |
| 119 | + |
| 120 | +TEST_F(SetSnapshotTest, RollbackToTimeBeforeAnySnapshot) { |
| 121 | + ICEBERG_UNWRAP_OR_FAIL(auto transaction, table_->NewTransaction()); |
| 122 | + ICEBERG_UNWRAP_OR_FAIL(auto set_snapshot, transaction->NewSetSnapshot()); |
| 123 | + // Try to rollback to a time before any snapshot |
| 124 | + int64_t time_before_all = kOldestSnapshotTimestamp - 1000000; |
| 125 | + set_snapshot->RollbackToTime(time_before_all); |
| 126 | + |
| 127 | + // Should fail - no snapshot older than the specified time |
| 128 | + auto result = set_snapshot->Apply(); |
| 129 | + EXPECT_THAT(result, IsError(ErrorKind::kValidationFailed)); |
| 130 | + EXPECT_THAT(result, HasErrorMessage("no valid snapshot older than")); |
| 131 | +} |
| 132 | + |
| 133 | +TEST_F(SetSnapshotTest, RollbackToTimeExactMatch) { |
| 134 | + ICEBERG_UNWRAP_OR_FAIL(auto transaction, table_->NewTransaction()); |
| 135 | + ICEBERG_UNWRAP_OR_FAIL(auto set_snapshot, transaction->NewSetSnapshot()); |
| 136 | + // Rollback to a timestamp just after the oldest snapshot |
| 137 | + int64_t time_just_after_oldest = kOldestSnapshotTimestamp + 1; |
| 138 | + set_snapshot->RollbackToTime(time_just_after_oldest); |
| 139 | + |
| 140 | + // Apply and verify - should return the oldest snapshot |
| 141 | + ICEBERG_UNWRAP_OR_FAIL(auto snapshot_id, set_snapshot->Apply()); |
| 142 | + EXPECT_EQ(snapshot_id, kOldestSnapshotId); |
| 143 | +} |
| 144 | + |
| 145 | +TEST_F(SetSnapshotTest, ApplyWithoutChanges) { |
| 146 | + ICEBERG_UNWRAP_OR_FAIL(auto transaction, table_->NewTransaction()); |
| 147 | + ICEBERG_UNWRAP_OR_FAIL(auto set_snapshot, transaction->NewSetSnapshot()); |
| 148 | + ICEBERG_UNWRAP_OR_FAIL(auto snapshot_id, set_snapshot->Apply()); |
| 149 | + EXPECT_EQ(snapshot_id, kCurrentSnapshotId); |
| 150 | + |
| 151 | + EXPECT_THAT(set_snapshot->Commit(), IsOk()); |
| 152 | + EXPECT_THAT(transaction->Commit(), IsOk()); |
| 153 | + ICEBERG_UNWRAP_OR_FAIL(auto reloaded, catalog_->LoadTable(table_ident_)); |
| 154 | + ICEBERG_UNWRAP_OR_FAIL(auto current_snapshot, reloaded->current_snapshot()); |
| 155 | + EXPECT_EQ(current_snapshot->snapshot_id, kCurrentSnapshotId); |
| 156 | +} |
| 157 | + |
| 158 | +} // namespace iceberg |
0 commit comments