|
| 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 "paimon/factories/singleton.h" |
| 21 | + |
| 22 | +#include <array> |
| 23 | +#include <atomic> |
| 24 | +#include <cstdint> |
| 25 | +#include <thread> |
| 26 | +#include <vector> |
| 27 | + |
| 28 | +#include "gtest/gtest.h" |
| 29 | +#include "paimon/common/factories/io_hook.h" |
| 30 | +#include "paimon/factories/factory.h" |
| 31 | +#include "paimon/factories/factory_creator.h" |
| 32 | + |
| 33 | +namespace paimon::test { |
| 34 | + |
| 35 | +namespace { |
| 36 | + |
| 37 | +constexpr int32_t kNumThreads = 32; |
| 38 | + |
| 39 | +class StormFactory : public Factory { |
| 40 | + public: |
| 41 | + const char* Identifier() const override { |
| 42 | + return "storm_factory"; |
| 43 | + } |
| 44 | +}; |
| 45 | + |
| 46 | +// Runs `worker(i)` on kNumThreads threads that are all blocked on a shared start |
| 47 | +// flag and released at (nearly) the same time, so that they race on the first |
| 48 | +// Singleton::GetInstance() publication. Joins all threads before returning. |
| 49 | +template <typename Worker> |
| 50 | +void RunStorm(const Worker& worker) { |
| 51 | + std::atomic<bool> start{false}; |
| 52 | + std::vector<std::thread> threads; |
| 53 | + threads.reserve(kNumThreads); |
| 54 | + for (int32_t i = 0; i < kNumThreads; ++i) { |
| 55 | + threads.emplace_back([&start, &worker, i]() { |
| 56 | + while (!start.load(std::memory_order_acquire)) { |
| 57 | + std::this_thread::yield(); |
| 58 | + } |
| 59 | + worker(i); |
| 60 | + }); |
| 61 | + } |
| 62 | + start.store(true, std::memory_order_release); |
| 63 | + for (auto& thread : threads) { |
| 64 | + thread.join(); |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +} // namespace |
| 69 | + |
| 70 | +TEST(SingletonTest, TestConcurrentIOHookGetInstance) { |
| 71 | + std::array<IOHook*, kNumThreads> hooks{}; |
| 72 | + std::array<bool, kNumThreads> try_oks{}; |
| 73 | + RunStorm([&hooks, &try_oks](int32_t i) { |
| 74 | + hooks[i] = Singleton<IOHook>::GetInstance(); |
| 75 | + // The default (and cleared) IOHook state is SILENT, so Try() must succeed. |
| 76 | + try_oks[i] = hooks[i]->Try("singleton_storm_path").ok(); |
| 77 | + }); |
| 78 | + |
| 79 | + IOHook* expected = hooks[0]; |
| 80 | + ASSERT_NE(expected, nullptr); |
| 81 | + for (int32_t i = 0; i < kNumThreads; ++i) { |
| 82 | + ASSERT_EQ(expected, hooks[i]); |
| 83 | + ASSERT_TRUE(try_oks[i]); |
| 84 | + } |
| 85 | + ASSERT_GE(expected->IOCount(), kNumThreads); |
| 86 | + // Leave the process-wide singleton in its default SILENT state for later tests. |
| 87 | + expected->Clear(); |
| 88 | +} |
| 89 | + |
| 90 | +TEST(SingletonTest, TestConcurrentFactoryCreatorGetInstance) { |
| 91 | + std::array<FactoryCreator*, kNumThreads> creators{}; |
| 92 | + RunStorm([&creators](int32_t i) { creators[i] = Singleton<FactoryCreator>::GetInstance(); }); |
| 93 | + |
| 94 | + FactoryCreator* expected = creators[0]; |
| 95 | + ASSERT_NE(expected, nullptr); |
| 96 | + for (int32_t i = 0; i < kNumThreads; ++i) { |
| 97 | + ASSERT_EQ(expected, creators[i]); |
| 98 | + } |
| 99 | + |
| 100 | + // Every thread must observe a fully constructed FactoryCreator: a lookup of a |
| 101 | + // registered factory must succeed from any thread. |
| 102 | + auto* factory = new StormFactory(); |
| 103 | + expected->Register(factory->Identifier(), factory); |
| 104 | + std::array<Factory*, kNumThreads> looked_up{}; |
| 105 | + RunStorm([&looked_up](int32_t i) { |
| 106 | + looked_up[i] = Singleton<FactoryCreator>::GetInstance()->Create("storm_factory"); |
| 107 | + }); |
| 108 | + for (int32_t i = 0; i < kNumThreads; ++i) { |
| 109 | + ASSERT_EQ(factory, looked_up[i]); |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +} // namespace paimon::test |
0 commit comments