|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 OceanBase. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#define USING_LOG_PREFIX SHARE |
| 18 | + |
| 19 | +#include <gtest/gtest.h> |
| 20 | +#include <gmock/gmock.h> |
| 21 | +#define private public |
| 22 | +#include "share/ob_max_id_cache.h" |
| 23 | +#include "rootserver/ob_root_service.h" |
| 24 | +#include "mittest/simple_server/env/ob_simple_cluster_test_base.h" |
| 25 | +#include "deps/oblib/src/common/ob_tablet_id.h" |
| 26 | +#include "share/ob_max_id_fetcher.h" |
| 27 | + |
| 28 | +#define ASSERT_SUCCESS(x) ASSERT_EQ((x), OB_SUCCESS) |
| 29 | + |
| 30 | +namespace oceanbase |
| 31 | +{ |
| 32 | +namespace pl |
| 33 | +{ |
| 34 | +int ObPLPackageManager::load_all_sys_package(common::ObMySQLProxy &sql_proxy) |
| 35 | +{ |
| 36 | + return OB_SUCCESS; |
| 37 | +} |
| 38 | +} |
| 39 | +using namespace common; |
| 40 | +class TestMaxIdCache : public unittest::ObSimpleClusterTestBase |
| 41 | +{ |
| 42 | +public: |
| 43 | + TestMaxIdCache() : unittest::ObSimpleClusterTestBase("test_max_id_cache") {} |
| 44 | +}; |
| 45 | +// Test various max_id_cache interfaces. |
| 46 | +TEST_F(TestMaxIdCache, basic) |
| 47 | +{ |
| 48 | + ASSERT_NE(GCTX.root_service_, nullptr); |
| 49 | + ASSERT_NE(GCTX.sql_proxy_, nullptr); |
| 50 | + while (!GCTX.root_service_->is_full_service()) { |
| 51 | + ob_usleep(1_s); |
| 52 | + } |
| 53 | + rootserver::ObSysStat sys_stat; |
| 54 | + ASSERT_SUCCESS(sys_stat.set_initial_values(OB_SYS_TENANT_ID)); |
| 55 | + ObMaxIdFetcher id_fetcher(*GCTX.sql_proxy_); |
| 56 | + uint64_t id = OB_INVALID_ID; |
| 57 | + int64_t extended_tablet_init_id = sys_stat.ob_max_used_extended_rowid_table_tablet_id_.value_.get_int(); |
| 58 | + uint64_t normal_tablet_init_id = sys_stat.ob_max_used_normal_rowid_table_tablet_id_.value_.get_int(); |
| 59 | + uint64_t table_init_id = sys_stat.ob_max_used_object_id_.value_.get_int(); |
| 60 | + |
| 61 | + // This interface returns [id, id + size). |
| 62 | + id = OB_INVALID_ID; |
| 63 | + ASSERT_SUCCESS(id_fetcher.fetch_new_max_ids(OB_SYS_TENANT_ID, OB_MAX_USED_EXTENDED_ROWID_TABLE_TABLET_ID_TYPE, id, 1)); |
| 64 | + ASSERT_EQ(extended_tablet_init_id + 1, id); |
| 65 | + id = OB_INVALID_ID; |
| 66 | + ASSERT_SUCCESS(id_fetcher.fetch_new_max_ids(OB_SYS_TENANT_ID, OB_MAX_USED_EXTENDED_ROWID_TABLE_TABLET_ID_TYPE, id, 2)); |
| 67 | + ASSERT_EQ(extended_tablet_init_id + 2, id); |
| 68 | + id = OB_INVALID_ID; |
| 69 | + ASSERT_SUCCESS(id_fetcher.fetch_new_max_ids(OB_SYS_TENANT_ID, OB_MAX_USED_EXTENDED_ROWID_TABLE_TABLET_ID_TYPE, id, 3)); |
| 70 | + ASSERT_EQ(extended_tablet_init_id + 4, id); |
| 71 | + |
| 72 | + // This interface returns [id, id + size). |
| 73 | + id = OB_INVALID_ID; |
| 74 | + ASSERT_SUCCESS(id_fetcher.fetch_new_max_ids(OB_SYS_TENANT_ID, OB_MAX_USED_NORMAL_ROWID_TABLE_TABLET_ID_TYPE, id, 1)); |
| 75 | + ASSERT_EQ(normal_tablet_init_id + 1, id); |
| 76 | + id = OB_INVALID_ID; |
| 77 | + ASSERT_SUCCESS(id_fetcher.fetch_new_max_ids(OB_SYS_TENANT_ID, OB_MAX_USED_NORMAL_ROWID_TABLE_TABLET_ID_TYPE, id, 2)); |
| 78 | + ASSERT_EQ(normal_tablet_init_id + 2, id); |
| 79 | + id = OB_INVALID_ID; |
| 80 | + ASSERT_SUCCESS(id_fetcher.fetch_new_max_ids(OB_SYS_TENANT_ID, OB_MAX_USED_NORMAL_ROWID_TABLE_TABLET_ID_TYPE, id, 3)); |
| 81 | + ASSERT_EQ(normal_tablet_init_id + 4, id); |
| 82 | + |
| 83 | + // This interface returns (id - size, id]. |
| 84 | + id = OB_INVALID_ID; |
| 85 | + ASSERT_SUCCESS(id_fetcher.fetch_new_max_id(OB_SYS_TENANT_ID, OB_MAX_USED_TABLE_ID_TYPE, id, UINT64_MAX, 1)); |
| 86 | + ASSERT_EQ(table_init_id + 1, id); // 1 |
| 87 | + id = OB_INVALID_ID; |
| 88 | + ASSERT_SUCCESS(id_fetcher.fetch_new_max_id(OB_SYS_TENANT_ID, OB_MAX_USED_TABLE_ID_TYPE, id, UINT64_MAX, 2)); |
| 89 | + ASSERT_EQ(table_init_id + 3, id); // 2 3 |
| 90 | + id = OB_INVALID_ID; |
| 91 | + ASSERT_SUCCESS(id_fetcher.fetch_new_max_id(OB_SYS_TENANT_ID, OB_MAX_USED_TABLE_ID_TYPE, id, UINT64_MAX, 3)); |
| 92 | + ASSERT_EQ(table_init_id + 6, id); // 4 5 6 |
| 93 | +} |
| 94 | + |
| 95 | +// Cached IDs are wasted after RS leader switch. |
| 96 | +TEST_F(TestMaxIdCache, rs_change) |
| 97 | +{ |
| 98 | + ASSERT_NE(GCTX.root_service_, nullptr); |
| 99 | + ASSERT_SUCCESS(GCTX.root_service_->revoke_rs()); |
| 100 | + while (!GCTX.root_service_->is_full_service()) { |
| 101 | + ob_usleep(1_s); |
| 102 | + } |
| 103 | + rootserver::ObSysStat sys_stat; |
| 104 | + ASSERT_SUCCESS(sys_stat.set_initial_values(OB_SYS_TENANT_ID)); |
| 105 | + ObMaxIdFetcher id_fetcher(*GCTX.sql_proxy_); |
| 106 | + uint64_t id = OB_INVALID_ID; |
| 107 | + int64_t extended_tablet_init_id = sys_stat.ob_max_used_extended_rowid_table_tablet_id_.value_.get_int() + ObMaxIdCacheItem::CACHE_SIZE; |
| 108 | + uint64_t normal_tablet_init_id = sys_stat.ob_max_used_normal_rowid_table_tablet_id_.value_.get_int() + ObMaxIdCacheItem::CACHE_SIZE; |
| 109 | + uint64_t table_init_id = sys_stat.ob_max_used_object_id_.value_.get_int() + ObMaxIdCacheItem::CACHE_SIZE; |
| 110 | + |
| 111 | + // This interface returns [id, id + size). |
| 112 | + id = OB_INVALID_ID; |
| 113 | + ASSERT_SUCCESS(id_fetcher.fetch_new_max_ids(OB_SYS_TENANT_ID, OB_MAX_USED_EXTENDED_ROWID_TABLE_TABLET_ID_TYPE, id, 1)); |
| 114 | + ASSERT_EQ(extended_tablet_init_id + 1, id); |
| 115 | + id = OB_INVALID_ID; |
| 116 | + ASSERT_SUCCESS(id_fetcher.fetch_new_max_ids(OB_SYS_TENANT_ID, OB_MAX_USED_EXTENDED_ROWID_TABLE_TABLET_ID_TYPE, id, 2)); |
| 117 | + ASSERT_EQ(extended_tablet_init_id + 2, id); |
| 118 | + id = OB_INVALID_ID; |
| 119 | + ASSERT_SUCCESS(id_fetcher.fetch_new_max_ids(OB_SYS_TENANT_ID, OB_MAX_USED_EXTENDED_ROWID_TABLE_TABLET_ID_TYPE, id, 3)); |
| 120 | + ASSERT_EQ(extended_tablet_init_id + 4, id); |
| 121 | + |
| 122 | + // This interface returns [id, id + size). |
| 123 | + id = OB_INVALID_ID; |
| 124 | + ASSERT_SUCCESS(id_fetcher.fetch_new_max_ids(OB_SYS_TENANT_ID, OB_MAX_USED_NORMAL_ROWID_TABLE_TABLET_ID_TYPE, id, 1)); |
| 125 | + ASSERT_EQ(normal_tablet_init_id + 1, id); |
| 126 | + id = OB_INVALID_ID; |
| 127 | + ASSERT_SUCCESS(id_fetcher.fetch_new_max_ids(OB_SYS_TENANT_ID, OB_MAX_USED_NORMAL_ROWID_TABLE_TABLET_ID_TYPE, id, 2)); |
| 128 | + ASSERT_EQ(normal_tablet_init_id + 2, id); |
| 129 | + id = OB_INVALID_ID; |
| 130 | + ASSERT_SUCCESS(id_fetcher.fetch_new_max_ids(OB_SYS_TENANT_ID, OB_MAX_USED_NORMAL_ROWID_TABLE_TABLET_ID_TYPE, id, 3)); |
| 131 | + ASSERT_EQ(normal_tablet_init_id + 4, id); |
| 132 | + |
| 133 | + // This interface returns (id - size, id]. |
| 134 | + id = OB_INVALID_ID; |
| 135 | + ASSERT_SUCCESS(id_fetcher.fetch_new_max_id(OB_SYS_TENANT_ID, OB_MAX_USED_TABLE_ID_TYPE, id, UINT64_MAX, 1)); |
| 136 | + ASSERT_EQ(table_init_id + 1, id); // 1 |
| 137 | + id = OB_INVALID_ID; |
| 138 | + ASSERT_SUCCESS(id_fetcher.fetch_new_max_id(OB_SYS_TENANT_ID, OB_MAX_USED_TABLE_ID_TYPE, id, UINT64_MAX, 2)); |
| 139 | + ASSERT_EQ(table_init_id + 3, id); // 2 3 |
| 140 | + id = OB_INVALID_ID; |
| 141 | + ASSERT_SUCCESS(id_fetcher.fetch_new_max_id(OB_SYS_TENANT_ID, OB_MAX_USED_TABLE_ID_TYPE, id, UINT64_MAX, 3)); |
| 142 | + ASSERT_EQ(table_init_id + 6, id); // 4 5 6 |
| 143 | +} |
| 144 | + |
| 145 | +TEST_F(TestMaxIdCache, max_id) |
| 146 | +{ |
| 147 | + ObMaxIdFetcher id_fetcher(*GCTX.sql_proxy_); |
| 148 | + uint64_t id = OB_INVALID_ID; |
| 149 | + ASSERT_NE(GCTX.root_service_, nullptr); |
| 150 | + ASSERT_NE(GCTX.sql_proxy_, nullptr); |
| 151 | + uint64_t current_id = OB_INVALID_ID; |
| 152 | + ASSERT_SUCCESS(id_fetcher.fetch_new_max_ids(OB_SYS_TENANT_ID, OB_MAX_USED_EXTENDED_ROWID_TABLE_TABLET_ID_TYPE, current_id, 1)); |
| 153 | + id = OB_INVALID_ID; |
| 154 | + ASSERT_SUCCESS(id_fetcher.fetch_new_max_ids(OB_SYS_TENANT_ID, OB_MAX_USED_EXTENDED_ROWID_TABLE_TABLET_ID_TYPE, |
| 155 | + id, ObTabletID::MAX_USER_EXTENDED_ROWID_TABLE_TABLET_ID - 1 - current_id)); |
| 156 | + id = OB_INVALID_ID; |
| 157 | + ASSERT_NE(id_fetcher.fetch_new_max_ids(OB_SYS_TENANT_ID, OB_MAX_USED_EXTENDED_ROWID_TABLE_TABLET_ID_TYPE, current_id, 1), |
| 158 | + OB_SUCCESS) << current_id; |
| 159 | + |
| 160 | + current_id = OB_INVALID_ID; |
| 161 | + ASSERT_SUCCESS(id_fetcher.fetch_new_max_ids(OB_SYS_TENANT_ID, OB_MAX_USED_NORMAL_ROWID_TABLE_TABLET_ID_TYPE, current_id, 1)); |
| 162 | + id = OB_INVALID_ID; |
| 163 | + ASSERT_SUCCESS(id_fetcher.fetch_new_max_ids(OB_SYS_TENANT_ID, OB_MAX_USED_NORMAL_ROWID_TABLE_TABLET_ID_TYPE, |
| 164 | + id, ObTabletID::MAX_USER_NORMAL_ROWID_TABLE_TABLET_ID - 1 - current_id)); |
| 165 | + id = OB_INVALID_ID; |
| 166 | + ASSERT_NE(id_fetcher.fetch_new_max_ids(OB_SYS_TENANT_ID, OB_MAX_USED_NORMAL_ROWID_TABLE_TABLET_ID_TYPE, current_id, 1), |
| 167 | + OB_SUCCESS) << current_id; |
| 168 | + |
| 169 | + current_id = OB_INVALID_ID; |
| 170 | + ASSERT_SUCCESS(id_fetcher.fetch_new_max_id(OB_SYS_TENANT_ID, OB_MAX_USED_TABLE_ID_TYPE, current_id, UINT64_MAX, 1)); |
| 171 | + id = OB_INVALID_ID; |
| 172 | + ASSERT_SUCCESS(id_fetcher.fetch_new_max_id(OB_SYS_TENANT_ID, OB_MAX_USED_TABLE_ID_TYPE, id, UINT64_MAX, INT64_MAX - current_id)); |
| 173 | + id = OB_INVALID_ID; |
| 174 | + ASSERT_NE(id_fetcher.fetch_new_max_id(OB_SYS_TENANT_ID, OB_MAX_USED_TABLE_ID_TYPE, current_id, UINT64_MAX, 1), |
| 175 | + OB_SUCCESS) << current_id; |
| 176 | +} |
| 177 | +} |
| 178 | + |
| 179 | +int main(int argc, char **argv) |
| 180 | +{ |
| 181 | + oceanbase::unittest::init_log_and_gtest(argc, argv); |
| 182 | + OB_LOGGER.set_log_level("INFO"); |
| 183 | + ::testing::InitGoogleTest(&argc, argv); |
| 184 | + return RUN_ALL_TESTS(); |
| 185 | +} |
0 commit comments