1 /*
2  Copyright (c) 2011, 2021, Oracle and/or its affiliates. All rights
3  reserved.
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License, version 2.0,
7  as published by the Free Software Foundation.
8 
9  This program is also distributed with certain software (including
10  but not limited to OpenSSL) that is licensed under separate terms,
11  as designated in a particular file or component or in included license
12  documentation.  The authors of MySQL hereby grant you an additional
13  permission to link the program and your derivative works with the
14  separately licensed software that they have included with MySQL.
15 
16  This program is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  GNU General Public License, version 2.0, for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with this program; if not, write to the Free Software
23  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
24  02110-1301  USA
25  */
26 #ifndef NDBMEMCACHE_CLUSTERCONNECTIONPOOL_H
27 #define NDBMEMCACHE_CLUSTERCONNECTIONPOOL_H
28 
29 #define MAX_CONNECT_POOL 4
30 
31 #include <memcached/engine.h>
32 
33 #include <NdbApi.hpp>
34 
35 #include "NdbInstance.h"
36 
37 class ClusterConnectionPool;
38 
39 /*  ClusterConnectionPool.cc keeps a global hash table of
40  *  ClusterConnectionPool instances, keyed by connect string
41  */
42 ClusterConnectionPool * get_connection_pool_for_cluster(const char * connect_string);
43 
44 
45 class ClusterConnectionPool {
46 
47 /* public instance variables */
48 public:
49   const char * const connect_string;   /*< cluster connect string */
50   unsigned int usec_rtt;         /*< estimated newtork latency within cluster */
51 
52 /* private instance variables */
53 private:
54   Ndb_cluster_connection *main_conn;
55   unsigned int pool_size;
56   Ndb_cluster_connection * pool_connections[MAX_CONNECT_POOL];
57   void * custom_data_ptr;
58 
59 /* public class methods */
60 public:
61   static Ndb_cluster_connection * connect(const char *connectstring);
62 
63 /* public instance methods */
64 public:
65   ClusterConnectionPool(const char *s = 0);
66   ~ClusterConnectionPool();
67   void setMainConnection(Ndb_cluster_connection *);                  // inlined
getMainConnection()68   Ndb_cluster_connection *getMainConnection() const      { return main_conn; };
getPoolSize()69   unsigned int getPoolSize() const                       { return pool_size; };
70 
71   /** After startup time, create an additional connection in the pool.
72       Returns the new connection
73   */
74   Ndb_cluster_connection * addPooledConnection();
75 
76   /** Get the connection numbered "my_id modulo pool_size" */
77   Ndb_cluster_connection *getPooledConnection(int my_id) const;      // inlined
78 
79   /** Get aggregated NDB API client statistics */
80   void add_stats(const char *, ADD_STAT, const void *);
81 
82   /** Set/get a user-supplied pointer */
83   void setCustomData(void *p);
84   void * getCustomData();
85 };
86 
87 
88 /* Inline functions */
89 
setMainConnection(Ndb_cluster_connection * c)90 inline void ClusterConnectionPool::setMainConnection(Ndb_cluster_connection *c) {
91   main_conn = c;
92   pool_size = 1;
93   pool_connections[0] = c;
94 }
95 
getPooledConnection(int i)96 inline Ndb_cluster_connection * ClusterConnectionPool::getPooledConnection(int i) const {
97   return pool_connections[i % pool_size];
98 }
99 
setCustomData(void * p)100 inline void ClusterConnectionPool::setCustomData(void *p) {
101   custom_data_ptr = p;
102 }
103 
getCustomData(void)104 inline void * ClusterConnectionPool::getCustomData(void) {
105   return custom_data_ptr;
106 }
107 
108 #endif
109