1 /* 2 * Copyright (c) 2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef OHOS_DISTRIBUTED_DATA_NATIVE_GDB_CONNECTION_POOL_H 17 #define OHOS_DISTRIBUTED_DATA_NATIVE_GDB_CONNECTION_POOL_H 18 19 #include <atomic> 20 #include <condition_variable> 21 #include <iostream> 22 #include <iterator> 23 #include <list> 24 #include <memory> 25 #include <mutex> 26 #include <sstream> 27 #include <stack> 28 #include <vector> 29 30 #include "connection.h" 31 #include "gdb_store_config.h" 32 33 namespace OHOS::DistributedDataAip { 34 class ConnectionPool : public std::enable_shared_from_this<ConnectionPool> { 35 public: 36 using SharedConn = std::shared_ptr<Connection>; 37 static constexpr std::chrono::milliseconds INVALID_TIME = std::chrono::milliseconds(0); 38 static std::shared_ptr<ConnectionPool> Create(const StoreConfig &config, int &errCode); 39 ~ConnectionPool(); 40 std::pair<int32_t, std::shared_ptr<Connection>> CreateTransConn(); 41 SharedConn Acquire(bool isReadOnly, std::chrono::milliseconds ms = INVALID_TIME); 42 // this interface is only provided for resultSet 43 SharedConn AcquireRef(bool isReadOnly, std::chrono::milliseconds ms = INVALID_TIME); 44 int32_t Dump(bool isWriter, const char *header); 45 int RestartReaders(); 46 void CloseAllConnections(); 47 48 explicit ConnectionPool(const StoreConfig &storeConfig); 49 50 private: 51 struct ConnNode { 52 bool using_ = false; 53 int32_t tid_ = 0; 54 int32_t id_ = 0; 55 std::chrono::steady_clock::time_point time_ = std::chrono::steady_clock::now(); 56 std::shared_ptr<Connection> connect_; 57 58 explicit ConnNode(std::shared_ptr<Connection> conn); 59 std::shared_ptr<Connection> GetConnect(); 60 int64_t GetUsingTime() const; 61 bool IsWriter() const; 62 int32_t Unused(int32_t count, bool timeout); 63 }; 64 65 struct Container { 66 using Creator = std::function<std::pair<int32_t, std::shared_ptr<Connection>>()>; 67 static constexpr int32_t MAX_RIGHT = 0x4FFFFFFF; 68 static constexpr int32_t MIN_TRANS_ID = 10000; 69 bool disable_ = true; 70 int max_ = 0; 71 int total_ = 0; 72 int count_ = 0; 73 int trans_ = 0; 74 int32_t left_ = 0; 75 int32_t right_ = 0; 76 std::chrono::seconds timeout_; 77 std::list<std::shared_ptr<ConnNode>> nodes_; 78 std::list<std::weak_ptr<ConnNode>> details_; 79 std::mutex mutex_; 80 std::condition_variable cond_; 81 Creator creator_ = nullptr; 82 std::pair<int32_t, std::shared_ptr<ConnNode>> Initialize( 83 Creator creator, int32_t max, int32_t timeout, bool disable, bool acquire = false); 84 std::shared_ptr<ConnNode> Acquire(std::chrono::milliseconds milliS); 85 std::pair<int32_t, std::shared_ptr<ConnNode>> Create(); 86 87 void Disable(); 88 void Enable(); 89 int32_t Release(std::shared_ptr<ConnNode> node); 90 int32_t Drop(std::shared_ptr<ConnNode> node); 91 int32_t Clear(); 92 bool IsFull(); 93 int32_t Dump(const char *header, int32_t count); 94 95 private: 96 int32_t ExtendNode(); 97 int32_t RelDetails(std::shared_ptr<ConnNode> node); 98 }; 99 100 std::pair<int32_t, std::shared_ptr<Connection>> Init(bool isAttach = false, bool needWriter = false); 101 int32_t GetMaxReaders(const StoreConfig &config); 102 std::shared_ptr<Connection> Convert2AutoConn(std::shared_ptr<ConnNode> node, bool isTrans = false); 103 void ReleaseNode(std::shared_ptr<ConnNode> node, bool reuse = true); 104 105 static constexpr uint32_t CHECK_POINT_INTERVAL = 5; // 5 min 106 static constexpr uint32_t ITER_V1 = 5000; 107 static constexpr uint32_t ITERS_COUNT = 2; 108 static constexpr uint32_t MAX_TRANS = 4; 109 const StoreConfig &config_; 110 Container writers_; 111 Container readers_; 112 int32_t maxReader_ = 0; 113 114 std::condition_variable transCondition_; 115 std::atomic<bool> isInTransaction_ = false; 116 std::atomic<uint32_t> transCount_ = 0; 117 std::atomic<std::chrono::steady_clock::time_point> failedTime_; 118 }; 119 120 } // namespace OHOS::DistributedDataAip 121 #endif 122