SequoiaDB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
sdbDataSource.hpp
Go to the documentation of this file.
1 /*******************************************************************************
2 
3 
4  Copyright (C) 2011-2018 SequoiaDB Ltd.
5 
6  Licensed under the Apache License, Version 2.0 (the "License");
7  you may not use this file except in compliance with the License.
8  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, software
13  distributed under the License is distributed on an "AS IS" BASIS,
14  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  See the License for the specific language governing permissions and
16  limitations under the License.
17 
18  Source File Name = sdbDataSource.hpp
19 
20  Descriptive Name = SDB Data Source Include Header
21 
22  When/how to use: this program may be used on sequoiadb data source function.
23 
24  Dependencies: N/A
25 
26  Restrictions: N/A
27 
28  Change Activity:
29  defect Date Who Description
30  ====== =========== === ==============================================
31  06/30/2016 LXJ Initial Draft
32 
33  Last Changed =
34 
35 *******************************************************************************/
36 
42 #ifndef SDB_DATA_SOURCE_HPP_
43 #define SDB_DATA_SOURCE_HPP_
44 
45 #include "sdbDataSourceComm.hpp"
46 #include <list>
47 
48 #ifndef SDB_CLIENT
49 #define SDB_CLIENT
50 #endif
51 
52 #include "ossLatch.hpp"
53 #include "ossMem.hpp"
54 #include "ossAtomic.hpp"
55 
56 
61 namespace sdbclient
62 {
63  class sdbDataSourceStrategy ;
64  class sdbDSWorker ;
68  class DLLEXPORT sdbDataSource : public SDBObject
69  {
70  friend void createConnFunc( void *args ) ;
71  friend void destroyConnFunc( void *args ) ;
72  friend void bgTaskFunc( void *args ) ;
73 
74  public:
79  : _idleList(),
80  _idleSize(0),
81  _busyList(),
82  _busySize(0),
83  _destroyList(),
84  _conf(),
85  _strategy(NULL),
86  _connMutex(),
87  _globalMutex(),
88  _isInited(FALSE),
89  _isEnabled(FALSE),
90  _toCreateConn(FALSE),
91  _toDestroyConn(FALSE),
92  _toStopWorkers(FALSE),
93  _createConnWorker(NULL),
94  _destroyConnWorker(NULL),
95  _bgTaskWorker(NULL) {}
96 
100  ~sdbDataSource() ;
101 
102  private:
103  sdbDataSource( const sdbDataSource &datasource ) ;
104  sdbDataSource& operator=( const sdbDataSource &datasource ) ;
105 
106  public:
115  INT32 init( const std::string &url, const sdbDataSourceConf &conf ) ;
116 
125  INT32 init(
126  const std::vector<std::string> &vUrls,
127  const sdbDataSourceConf &conf ) ;
128 
134  INT32 getIdleConnNum()const ;
135 
141  INT32 getUsedConnNum()const ;
142 
148  INT32 getNormalCoordNum()const ;
149 
155  INT32 getAbnormalCoordNum() const ;
156 
162  INT32 getLocalCoordNum()const ;
163 
168  void addCoord( const string &url ) ;
169 
174  void removeCoord( const string &url ) ;
175 
181  INT32 enable() ;
182 
191  INT32 disable() ;
192 
203  INT32 getConnection( sdb*& conn, INT64 timeoutms = 5000 ) ;
204 
211  void releaseConnection(sdb *conn) ;
212 
216  void close() ;
217 
218 
219 //#if defined (_DEBUG)
220 // private:
221 // void printCreateInfo(const std::string& coord) ;
222 //#endif
223 
224 
225  private:
226  // check address arguments, if valid, add it
227  BOOLEAN _checkAddrArg( const string &url ) ;
228 
229  // new a strategy with config
230  INT32 _buildStrategy() ;
231 
232  // clear data source
233  void _clearDataSource() ;
234 
235  // try to get a connection
236  BOOLEAN _tryGetConn( sdb*& conn ) ;
237 
238  // create connection by a number
239  INT32 _createConnByNum( INT32 num ) ;
240 
241  // sync coord node info
242  void _syncCoordNodes() ;
243 
244  // get back the coord address from the abnormal address list
245  INT32 _retrieveAddrFromAbnormalList() ;
246 
247  // check keep alive time out or not
248  BOOLEAN _keepAliveTimeOut( sdb *conn ) ;
249 
250  // check max connection number intervally
251  void _checkMaxIdleConn() ;
252 
253  // add new connection and make sure not reach max connection count
254  BOOLEAN _addNewConnSafely( sdb *conn, const std::string &coord );
255 
256  private:
257  // create connections function
258  void _createConn() ;
259 
260  //destroy connections function
261  void _destroyConn() ;
262 
263  // background task function
264  void _bgTask() ;
265 
266  private:
267  // idle connection list
268  std::list<sdb*> _idleList ;
269  ossAtomic32 _idleSize ;
270  // busy connection list
271  std::list<sdb*> _busyList ;
272  ossAtomic32 _busySize ;
273  // to be destroyed connection list
274  std::list<sdb*> _destroyList ;
275  // data source confiture
276  sdbDataSourceConf _conf ;
277  // data source strategy
278  sdbDataSourceStrategy* _strategy ;
279  // lock for connection lists
280  ossSpinXLatch _connMutex ;
281  // lock for global commuincate
282  ossSpinXLatch _globalMutex ;
283  // if has been inited
284  BOOLEAN _isInited ;
285  // if is enabled
286  BOOLEAN _isEnabled ;
287 
288  private:
289  BOOLEAN _toCreateConn ;
290  BOOLEAN _toDestroyConn ;
291  BOOLEAN _toStopWorkers ;
292  sdbDSWorker* _createConnWorker ;
293  sdbDSWorker* _destroyConnWorker ;
294  sdbDSWorker* _bgTaskWorker ;
295  } ;
296 }
297 
298 #endif