1 /*
2    Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved.
3 
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License, version 2.0,
6    as published by the Free Software Foundation.
7 
8    This program is also distributed with certain software (including
9    but not limited to OpenSSL) that is licensed under separate terms,
10    as designated in a particular file or component or in included license
11    documentation.  The authors of MySQL hereby grant you an additional
12    permission to link the program and your derivative works with the
13    separately licensed software that they have included with MySQL.
14 
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License, version 2.0, for more details.
19 
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
23 */
24 
25 #ifndef NDB_COMPONENT_H
26 #define NDB_COMPONENT_H
27 
28 #include "mysql/psi/mysql_cond.h"
29 #include "mysql/psi/mysql_mutex.h"
30 #include "mysql/psi/mysql_thread.h"
31 
32 extern "C" void *Ndb_component_run_C(void *);
33 
34 /**
35  * Baseclass encapsulating the different components
36  * in ndbcluster.
37  *
38  * NOTE! The intention should be to not correlate to number of
39  * threads since that is an implementation detail in each
40  * component.
41  */
42 
43 class Ndb_component {
44  public:
45   virtual int init();
46   virtual int start();
47   virtual int stop();
48   virtual int deinit();
49 
50   /*
51     Set the server as started, this means that the Ndb_component
52     can continue processing and use parts of the MySQL Server which are
53     not available unilt it's been fully started
54   */
55   void set_server_started();
56 
57  protected:
58   /*
59     Check if the server has started. This checks if the Ndb_component
60     has been informed that the server has started.
61    */
62   bool is_server_started();
63 
64   /**
65    * Con/de-structor is protected...so that sub-class needs to provide own
66    */
67   Ndb_component(const char *name);
68   virtual ~Ndb_component();
69 
70   /**
71    * Component init function
72    */
73   virtual int do_init() = 0;
74 
75   /**
76    * Component run function
77    */
78   virtual void do_run() = 0;
79 
80   /**
81    * Component deinit function
82    */
83   virtual int do_deinit() = 0;
84 
85   /**
86    * Component wakeup function
87    * - called when component is set to stop, should
88    *   wakeup component from waiting
89    */
90   virtual void do_wakeup() = 0;
91 
92   /**
93    * For usage in threads main loop
94    */
95   bool is_stop_requested();
96 
97  protected:
98   void log_verbose(unsigned verbose_level, const char *fmt, ...) const
99       MY_ATTRIBUTE((format(printf, 3, 4)));
100   void log_error(const char *fmt, ...) const
101       MY_ATTRIBUTE((format(printf, 2, 3)));
102   void log_warning(const char *fmt, ...) const
103       MY_ATTRIBUTE((format(printf, 2, 3)));
104   void log_info(const char *fmt, ...) const
105       MY_ATTRIBUTE((format(printf, 2, 3)));
106 
107   /*
108     Wait for the server started. The Ndb_component(and its thread(s))
109     are normally started before the MySQL Server is fully operational
110     and some functionality which the Ndb_component depend on isn't
111     yet initialized fully. This function will wait until the server
112     has reported started or shutdown has been requested.
113    */
114   bool wait_for_server_started(void);
115 
116  private:
117   enum ThreadState {
118     TS_UNINIT = 0,
119     TS_INIT = 1,
120     TS_STARTING = 2,
121     TS_RUNNING = 3,
122     TS_STOPPING = 4,
123     TS_STOPPED = 5
124   };
125 
126   ThreadState m_thread_state;
127   my_thread_handle m_thread;
128   mysql_mutex_t m_start_stop_mutex;
129   mysql_cond_t m_start_stop_cond;
130   bool m_server_started;  // Protected by m_start_stop_mutex
131 
132   const char *m_name;
133 
134   void run_impl();
135   friend void *Ndb_component_run_C(void *);
136 };
137 
138 #endif
139