Add rolling statistics that are collected across 10 second intervals.
[metze/ctdb/wip.git] / libctdb / sync.c
1 /*
2    synchronous wrappers for libctdb
3
4    Copyright (C) Rusty Russell 2010
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19 #include <ctdb.h>
20 #include <stdbool.h>
21 #include <poll.h>
22 #include <errno.h>
23 #include <stdlib.h>
24 #include "libctdb_private.h"
25
26 /* Remove type-safety macros. */
27 #undef ctdb_set_message_handler
28
29 /* On failure, frees req and returns NULL. */
30 static struct ctdb_request *synchronous(struct ctdb_connection *ctdb,
31                                         struct ctdb_request *req,
32                                         bool *done)
33 {
34         struct pollfd fds;
35
36         /* Pass through allocation failures. */
37         if (!req)
38                 return NULL;
39
40         fds.fd = ctdb_get_fd(ctdb);
41         while (!*done) {
42                 fds.events = ctdb_which_events(ctdb);
43                 if (poll(&fds, 1, -1) < 0) {
44                         /* Signalled is OK, other error is bad. */
45                         if (errno == EINTR)
46                                 continue;
47                         ctdb_cancel(ctdb, req);
48                         DEBUG(ctdb, LOG_ERR, "ctdb_synchronous: poll failed");
49                         return NULL;
50                 }
51                 if (!ctdb_service(ctdb, fds.revents)) {
52                         /* It can have failed after it completed request. */
53                         if (!*done)
54                                 ctdb_cancel(ctdb, req);
55                         else
56                                 ctdb_request_free(ctdb, req);
57                         return NULL;
58                 }
59         }
60         return req;
61 }
62
63 static void set(struct ctdb_connection *ctdb,
64                 struct ctdb_request *req, bool *done)
65 {
66         *done = true;
67 }
68
69 bool ctdb_getrecmaster(struct ctdb_connection *ctdb,
70                        uint32_t destnode, uint32_t *recmaster)
71 {
72         struct ctdb_request *req;
73         bool done = false;
74         bool ret = false;
75
76         req = synchronous(ctdb,
77                           ctdb_getrecmaster_send(ctdb, destnode, set, &done),
78                           &done);
79         if (req != NULL) {
80                 ret = ctdb_getrecmaster_recv(ctdb, req, recmaster);
81                 ctdb_request_free(ctdb, req);
82         }
83         return ret;
84 }
85
86 struct ctdb_db *ctdb_attachdb(struct ctdb_connection *ctdb,
87                               const char *name, bool persistent,
88                               uint32_t tdb_flags)
89 {
90         struct ctdb_request *req;
91         bool done = false;
92         struct ctdb_db *ret = NULL;
93
94         req = synchronous(ctdb,
95                           ctdb_attachdb_send(ctdb, name, persistent, tdb_flags,
96                                              set, &done),
97                           &done);
98         if (req != NULL) {
99                 ret = ctdb_attachdb_recv(ctdb, req);
100                 ctdb_request_free(ctdb, req);
101         }
102         return ret;
103 }
104
105 bool ctdb_getpnn(struct ctdb_connection *ctdb,
106                  uint32_t destnode, uint32_t *pnn)
107 {
108         struct ctdb_request *req;
109         bool done = false;
110         bool ret = false;
111
112         req = synchronous(ctdb,
113                           ctdb_getpnn_send(ctdb, destnode, set, &done),
114                           &done);
115         if (req != NULL) {
116                 ret = ctdb_getpnn_recv(ctdb, req, pnn);
117                 ctdb_request_free(ctdb, req);
118         }
119         return ret;
120 }
121
122 bool ctdb_getnodemap(struct ctdb_connection *ctdb,
123                  uint32_t destnode, struct ctdb_node_map **nodemap)
124 {
125         struct ctdb_request *req;
126         bool done = false;
127         bool ret = false;
128
129         *nodemap = NULL;
130
131         req = synchronous(ctdb,
132                           ctdb_getnodemap_send(ctdb, destnode, set, &done),
133                           &done);
134         if (req != NULL) {
135                 ret = ctdb_getnodemap_recv(ctdb, req, nodemap);
136                 ctdb_request_free(ctdb, req);
137         }
138         return ret;
139 }
140
141 bool ctdb_getpublicips(struct ctdb_connection *ctdb,
142                        uint32_t destnode, struct ctdb_all_public_ips **ips)
143 {
144         struct ctdb_request *req;
145         bool done = false;
146         bool ret = false;
147
148         *ips = NULL;
149
150         req = synchronous(ctdb,
151                           ctdb_getpublicips_send(ctdb, destnode, set, &done),
152                           &done);
153         if (req != NULL) {
154                 ret = ctdb_getpublicips_recv(ctdb, req, ips);
155                 ctdb_request_free(ctdb, req);
156         }
157         return ret;
158 }
159
160 bool ctdb_set_message_handler(struct ctdb_connection *ctdb, uint64_t srvid,
161                               ctdb_message_fn_t handler, void *cbdata)
162 {
163         struct ctdb_request *req;
164         bool done = false;
165         bool ret = false;
166
167         req = synchronous(ctdb,
168                           ctdb_set_message_handler_send(ctdb, srvid, handler,
169                                                         cbdata, set, &done),
170                           &done);
171         if (req != NULL) {
172                 ret = ctdb_set_message_handler_recv(ctdb, req);
173                 ctdb_request_free(ctdb, req);
174         }
175         return ret;
176 }
177
178 struct rrl_info {
179         bool done;
180         struct ctdb_lock *lock;
181         TDB_DATA *data;
182 };
183
184 static void rrl_callback(struct ctdb_db *ctdb_db,
185                          struct ctdb_lock *lock,
186                          TDB_DATA data,
187                          struct rrl_info *rrl)
188 {
189         rrl->done = true;
190         rrl->lock = lock;
191         *rrl->data = data;
192 }
193
194 struct ctdb_lock *ctdb_readrecordlock(struct ctdb_connection *ctdb,
195                                       struct ctdb_db *ctdb_db, TDB_DATA key,
196                                       TDB_DATA *data)
197 {
198         struct pollfd fds;
199         struct rrl_info rrl;
200
201         rrl.done = false;
202         rrl.lock = NULL;
203         rrl.data = data;
204
205         /* Immediate failure is easy. */
206         if (!ctdb_readrecordlock_async(ctdb_db, key, rrl_callback, &rrl))
207                 return NULL;
208
209         /* Immediate success is easy. */
210         if (!rrl.done) {
211                 /* Otherwise wait until callback called. */
212                 fds.fd = ctdb_get_fd(ctdb);
213                 while (!rrl.done) {
214                         fds.events = ctdb_which_events(ctdb);
215                         if (poll(&fds, 1, -1) < 0) {
216                                 /* Signalled is OK, other error is bad. */
217                                 if (errno == EINTR)
218                                         continue;
219                                 DEBUG(ctdb, LOG_ERR,
220                                       "ctdb_readrecordlock: poll failed");
221                                 return NULL;
222                         }
223                         if (!ctdb_service(ctdb, fds.revents)) {
224                                 break;
225                         }
226                 }
227         }
228         return rrl.lock;
229 }