• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 NATIVE_RDB_SQLITE_CONNECTION_POOL_H
17 #define NATIVE_RDB_SQLITE_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 "base_transaction.h"
31 #include "connection.h"
32 #include "rdb_common.h"
33 #include "rdb_store_config.h"
34 namespace OHOS {
35 class ExecutorPool;
36 namespace NativeRdb {
37 class ConnectionPool : public std::enable_shared_from_this<ConnectionPool> {
38 public:
39     using SharedConn = std::shared_ptr<Connection>;
40     using SharedConns = std::vector<SharedConn>;
41     static constexpr std::chrono::milliseconds INVALID_TIME = std::chrono::milliseconds(0);
42     static std::shared_ptr<ConnectionPool> Create(const RdbStoreConfig &config, int &errCode);
43     ~ConnectionPool();
44     static std::pair<RebuiltType, std::shared_ptr<ConnectionPool>> HandleDataCorruption(
45         const RdbStoreConfig &storeConfig, int &errCode);
46     std::pair<int32_t, std::shared_ptr<Connection>> CreateTransConn(bool limited = true);
47     SharedConn AcquireConnection(bool isReadOnly);
48     SharedConn Acquire(bool isReadOnly, std::chrono::milliseconds ms = INVALID_TIME);
49     // this interface is only provided for resultSet
50     SharedConn AcquireRef(bool isReadOnly, std::chrono::milliseconds ms = INVALID_TIME);
51     std::pair<SharedConn, SharedConns> AcquireAll(int32_t time);
52     std::pair<int32_t, SharedConn> DisableWal();
53     int32_t EnableWal();
54     int32_t Dump(bool isWriter, const char *header);
55 
56     int RestartConns();
57     int ConfigLocale(const std::string &localeStr);
58     int ChangeDbFileForRestore(const std::string &newPath, const std::string &backupPath,
59         const std::vector<uint8_t> &newKey, SlaveStatus &slaveStatus);
60     std::stack<BaseTransaction> &GetTransactionStack();
61     std::mutex &GetTransactionStackMutex();
62     int AcquireTransaction();
63     void ReleaseTransaction();
64     void CloseAllConnections();
65     bool IsInTransaction();
66     void SetInTransaction(bool isInTransaction);
67 
68 private:
69     struct ConnNode {
70         bool using_ = false;
71         int32_t tid_ = 0;
72         int32_t id_ = 0;
73         std::chrono::steady_clock::time_point time_ = std::chrono::steady_clock::now();
74         std::shared_ptr<Connection> connect_;
75 
76         explicit ConnNode(std::shared_ptr<Connection> conn);
77         std::shared_ptr<Connection> GetConnect();
78         int64_t GetUsingTime() const;
79         bool IsWriter() const;
80         int32_t Unused(int32_t count, bool timeout);
81         bool IsRecyclable();
82     };
83 
84     struct Container {
85         using Creator = std::function<std::pair<int32_t, std::shared_ptr<Connection>>()>;
86         static constexpr int32_t MAX_RIGHT = 0x4FFFFFFF;
87         static constexpr int32_t MIN_TRANS_ID = 10000;
88         bool disable_ = true;
89         int max_ = 0;
90         int total_ = 0;
91         int count_ = 0;
92         int32_t left_ = 0;
93         int32_t right_ = 0;
94         std::chrono::seconds timeout_;
95         std::list<std::shared_ptr<ConnNode>> nodes_;
96         std::list<std::weak_ptr<ConnNode>> details_;
97         std::mutex mutex_;
98         std::condition_variable cond_;
99         Creator creator_ = nullptr;
100         std::pair<int32_t, std::shared_ptr<ConnNode>> Initialize(
101             Creator creator, int32_t max, int32_t timeout, bool disable, bool acquire = false);
102         int32_t ConfigLocale(const std::string &locale);
103         std::pair<int, std::shared_ptr<ConnNode>> Acquire(std::chrono::milliseconds milliS);
104         std::pair<bool, std::list<std::shared_ptr<ConnNode>>> AcquireAll(std::chrono::milliseconds milliS);
105         std::pair<int32_t, std::shared_ptr<ConnNode>> Create();
106         void InitMembers(Creator creator, int32_t max, int32_t timeout, bool disable);
107 
108         void Disable();
109         void Enable();
110         int32_t Release(std::shared_ptr<ConnNode> node);
111         int32_t ReleaseTrans(std::shared_ptr<ConnNode> node);
112         int32_t Clear();
113         bool IsFull();
114         bool Empty();
115         int32_t Dump(const char *header, int32_t count);
116         int32_t ClearUnusedTrans(std::shared_ptr<ConnectionPool> pool);
117 
118     private:
119         int32_t ExtendNode();
120         int32_t RelDetails(std::shared_ptr<ConnNode> node);
121     };
122 
123     explicit ConnectionPool(const RdbStoreConfig &storeConfig);
124     std::pair<int32_t, std::shared_ptr<Connection>> Init(bool isAttach = false, bool needWriter = false);
125     int32_t GetMaxReaders(const RdbStoreConfig &config);
126     std::shared_ptr<Connection> Convert2AutoConn(std::shared_ptr<ConnNode> node, bool isTrans = false);
127     void ReleaseNode(std::shared_ptr<ConnNode> node, bool reuse = true);
128     int RestoreMasterDb(const std::string &newPath, const std::string &backupPath, SlaveStatus &slaveStatus);
129     bool CheckIntegrity(const std::string &dbPath);
130     void DelayClearTrans();
131 
132     static constexpr uint32_t CHECK_POINT_INTERVAL = 5; // 5 min
133     static constexpr int LIMITATION = 1024;
134     static constexpr uint32_t ITER_V1 = 5000;
135     static constexpr uint32_t ITERS_COUNT = 2;
136     static constexpr uint32_t MAX_TRANS = 4;
137     static constexpr std::chrono::steady_clock::duration TRANS_CLEAR_INTERVAL = std::chrono::seconds(150);
138     const RdbStoreConfig &config_;
139     RdbStoreConfig attachConfig_;
140     Container writers_;
141     Container readers_;
142     Container trans_;
143     int32_t maxReader_ = 0;
144 
145     std::stack<BaseTransaction> transactionStack_;
146     std::mutex transactionStackMutex_;
147     std::condition_variable transCondition_;
148     std::mutex transMutex_;
149     bool transactionUsed_;
150     bool isAttach_ = false;
151     std::atomic<bool> isInTransaction_ = false;
152     std::atomic<uint32_t> transCount_ = 0;
153     std::atomic<std::chrono::steady_clock::time_point> failedTime_;
154 };
155 
156 } // namespace NativeRdb
157 } // namespace OHOS
158 #endif
159