Merged tridge's ctdb branch.
[sahlberg/ctdb.git] / common / ctdb_ltdb.c
1 /* 
2    ctdb ltdb code
3
4    Copyright (C) Andrew Tridgell  2006
5
6    This library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 2 of the License, or (at your option) any later version.
10
11    This library 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 GNU
14    Lesser General Public License for more details.
15
16    You should have received a copy of the GNU Lesser General Public
17    License along with this library; if not, write to the Free Software
18    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 #include "includes.h"
22 #include "lib/events/events.h"
23 #include "lib/tdb/include/tdb.h"
24 #include "system/network.h"
25 #include "system/filesys.h"
26 #include "../include/ctdb_private.h"
27 #include "db_wrap.h"
28 #include "lib/util/dlinklist.h"
29
30 /*
31   find an attached ctdb_db handle given a name
32  */
33 struct ctdb_db_context *ctdb_db_handle(struct ctdb_context *ctdb, const char *name)
34 {
35         struct ctdb_db_context *tmp_db;
36         for (tmp_db=ctdb->db_list;tmp_db;tmp_db=tmp_db->next) {
37                 if (strcmp(name, tmp_db->db_name) == 0) {
38                         return tmp_db;
39                 }
40         }
41         return NULL;
42 }
43
44
45 /*
46   this is the dummy null procedure that all databases support
47 */
48 static int ctdb_null_func(struct ctdb_call_info *call)
49 {
50         return 0;
51 }
52
53
54 /*
55   attach to a specific database
56 */
57 struct ctdb_db_context *ctdb_attach(struct ctdb_context *ctdb, const char *name, int tdb_flags, 
58                                     int open_flags, mode_t mode)
59 {
60         struct ctdb_db_context *ctdb_db, *tmp_db;
61         TDB_DATA data;
62         int ret;
63
64         ctdb_db = talloc_zero(ctdb, struct ctdb_db_context);
65         CTDB_NO_MEMORY_NULL(ctdb, ctdb_db);
66
67         ctdb_db->ctdb = ctdb;
68         ctdb_db->db_name = talloc_strdup(ctdb_db, name);
69         CTDB_NO_MEMORY_NULL(ctdb, ctdb_db->db_name);
70
71         data.dptr = discard_const(name);
72         data.dsize = strlen(name);
73         ctdb_db->db_id = ctdb_hash(&data);
74
75         for (tmp_db=ctdb->db_list;tmp_db;tmp_db=tmp_db->next) {
76                 if (tmp_db->db_id == ctdb_db->db_id) {
77                         ctdb_set_error(ctdb, "CTDB database hash collission '%s' : '%s'",
78                                         name, tmp_db->db_name);
79                         talloc_free(ctdb_db);
80                         return NULL;
81                 }
82         }
83
84         if (mkdir(ctdb->db_directory, 0700) == -1 && errno != EEXIST) {
85                 DEBUG(0,(__location__ " Unable to create ctdb directory '%s'\n", 
86                          ctdb->db_directory));
87                 talloc_free(ctdb_db);
88                 return NULL;
89         }
90
91         /* add the node id to the database name, so when we run on loopback
92            we don't conflict in the local filesystem */
93         name = talloc_asprintf(ctdb_db, "%s/%s", ctdb->db_directory, name);
94
95         /* when we have a separate daemon this will need to be a real
96            file, not a TDB_INTERNAL, so the parent can access it to
97            for ltdb bypass */
98         ctdb_db->ltdb = tdb_wrap_open(ctdb, name, 0, TDB_CLEAR_IF_FIRST, open_flags, mode);
99         if (ctdb_db->ltdb == NULL) {
100                 ctdb_set_error(ctdb, "Failed to open tdb %s\n", name);
101                 talloc_free(ctdb_db);
102                 return NULL;
103         }
104
105
106         /* 
107            all databases support the "null" function. we need this in
108            order to do forced migration of records
109          */
110         ret = ctdb_set_call(ctdb_db, ctdb_null_func, CTDB_NULL_FUNC);
111         if (ret != 0) {
112                 talloc_free(ctdb_db);
113                 return NULL;
114         }
115
116         DLIST_ADD(ctdb->db_list, ctdb_db);
117
118         return ctdb_db;
119 }
120
121 /*
122   return the lmaster given a key
123 */
124 uint32_t ctdb_lmaster(struct ctdb_context *ctdb, const TDB_DATA *key)
125 {
126         return ctdb_hash(key) % ctdb->num_nodes;
127 }
128
129
130 /*
131   construct an initial header for a record with no ltdb header yet
132 */
133 static void ltdb_initial_header(struct ctdb_db_context *ctdb_db, 
134                                 TDB_DATA key,
135                                 struct ctdb_ltdb_header *header)
136 {
137         header->rsn = 0;
138         /* initial dmaster is the lmaster */
139         header->dmaster = ctdb_lmaster(ctdb_db->ctdb, &key);
140         header->laccessor = header->dmaster;
141         header->lacount = 0;
142 }
143
144
145 /*
146   fetch a record from the ltdb, separating out the header information
147   and returning the body of the record. A valid (initial) header is
148   returned if the record is not present
149 */
150 int ctdb_ltdb_fetch(struct ctdb_db_context *ctdb_db, 
151                     TDB_DATA key, struct ctdb_ltdb_header *header, 
152                     TALLOC_CTX *mem_ctx, TDB_DATA *data)
153 {
154         TDB_DATA rec;
155         struct ctdb_context *ctdb = ctdb_db->ctdb;
156
157         rec = tdb_fetch(ctdb_db->ltdb->tdb, key);
158         if (rec.dsize < sizeof(*header)) {
159                 TDB_DATA d2;
160                 /* return an initial header */
161                 if (rec.dptr) free(rec.dptr);
162                 ltdb_initial_header(ctdb_db, key, header);
163                 ZERO_STRUCT(d2);
164                 if (data) {
165                         *data = d2;
166                 }
167                 ctdb_ltdb_store(ctdb_db, key, header, d2);
168                 return 0;
169         }
170
171         *header = *(struct ctdb_ltdb_header *)rec.dptr;
172
173         if (data) {
174                 data->dsize = rec.dsize - sizeof(struct ctdb_ltdb_header);
175                 data->dptr = talloc_memdup(mem_ctx, 
176                                            sizeof(struct ctdb_ltdb_header)+rec.dptr,
177                                            data->dsize);
178         }
179
180         free(rec.dptr);
181         if (data) {
182                 CTDB_NO_MEMORY(ctdb, data->dptr);
183         }
184
185         return 0;
186 }
187
188
189 /*
190   fetch a record from the ltdb, separating out the header information
191   and returning the body of the record. A valid (initial) header is
192   returned if the record is not present
193 */
194 int ctdb_ltdb_store(struct ctdb_db_context *ctdb_db, TDB_DATA key, 
195                     struct ctdb_ltdb_header *header, TDB_DATA data)
196 {
197         struct ctdb_context *ctdb = ctdb_db->ctdb;
198         TDB_DATA rec;
199         int ret;
200
201         rec.dsize = sizeof(*header) + data.dsize;
202         rec.dptr = talloc_size(ctdb, rec.dsize);
203         CTDB_NO_MEMORY(ctdb, rec.dptr);
204
205         memcpy(rec.dptr, header, sizeof(*header));
206         memcpy(rec.dptr + sizeof(*header), data.dptr, data.dsize);
207
208         ret = tdb_store(ctdb_db->ltdb->tdb, key, rec, TDB_REPLACE);
209         talloc_free(rec.dptr);
210
211         return ret;
212 }
213
214
215 /*
216   lock a record in the ltdb, given a key
217  */
218 int ctdb_ltdb_lock(struct ctdb_db_context *ctdb_db, TDB_DATA key)
219 {
220         return tdb_chainlock(ctdb_db->ltdb->tdb, key);
221 }
222
223 /*
224   unlock a record in the ltdb, given a key
225  */
226 int ctdb_ltdb_unlock(struct ctdb_db_context *ctdb_db, TDB_DATA key)
227 {
228         return tdb_chainunlock(ctdb_db->ltdb->tdb, key);
229 }
230
231 struct lock_fetch_state {
232         struct ctdb_context *ctdb;
233         void (*recv_pkt)(void *, uint8_t *, uint32_t);
234         void *recv_context;
235         struct ctdb_req_header *hdr;
236 };
237
238 /*
239   called when we should retry the operation
240  */
241 static void lock_fetch_callback(void *p)
242 {
243         struct lock_fetch_state *state = talloc_get_type(p, struct lock_fetch_state);
244         state->recv_pkt(state->recv_context, (uint8_t *)state->hdr, state->hdr->length);
245         talloc_free(state);
246         DEBUG(2,(__location__ " PACKET REQUEUED\n"));
247 }
248
249
250 /*
251   do a non-blocking ltdb_lock, deferring this ctdb request until we
252   have the chainlock
253
254   It does the following:
255
256    1) tries to get the chainlock. If it succeeds, then it returns 0
257
258    2) if it fails to get a chainlock immediately then it sets up a
259    non-blocking chainlock via ctdb_lockwait, and when it gets the
260    chainlock it re-submits this ctdb request to the main packet
261    receive function
262
263    This effectively queues all ctdb requests that cannot be
264    immediately satisfied until it can get the lock. This means that
265    the main ctdb daemon will not block waiting for a chainlock held by
266    a client
267
268    There are 3 possible return values:
269
270        0:    means that it got the lock immediately.
271       -1:    means that it failed to get the lock, and won't retry
272       -2:    means that it failed to get the lock immediately, but will retry
273  */
274 int ctdb_ltdb_lock_requeue(struct ctdb_db_context *ctdb_db, 
275                            TDB_DATA key, struct ctdb_req_header *hdr,
276                            void (*recv_pkt)(void *, uint8_t *, uint32_t ),
277                            void *recv_context)
278 {
279         int ret;
280         struct tdb_context *tdb = ctdb_db->ltdb->tdb;
281         struct lockwait_handle *h;
282         struct lock_fetch_state *state;
283         
284         ret = tdb_chainlock_nonblock(tdb, key);
285
286         if (ret != 0 &&
287             !(errno == EACCES || errno == EAGAIN || errno == EDEADLK)) {
288                 /* a hard failure - don't try again */
289                 return -1;
290         }
291
292         /* when torturing, ensure we test the contended path */
293         if ((ctdb_db->ctdb->flags & CTDB_FLAG_TORTURE) &&
294             random() % 5 == 0) {
295                 ret = -1;
296                 tdb_chainunlock(tdb, key);
297         }
298
299         /* first the non-contended path */
300         if (ret == 0) {
301                 return 0;
302         }
303
304         state = talloc(ctdb_db, struct lock_fetch_state);
305         state->ctdb = ctdb_db->ctdb;
306         state->hdr = hdr;
307         state->recv_pkt = recv_pkt;
308         state->recv_context = recv_context;
309
310         /* now the contended path */
311         h = ctdb_lockwait(ctdb_db, key, lock_fetch_callback, state);
312         if (h == NULL) {
313                 tdb_chainunlock(tdb, key);
314                 return -1;
315         }
316
317         /* we need to move the packet off the temporary context in ctdb_recv_pkt(),
318            so it won't be freed yet */
319         talloc_steal(state, hdr);
320         talloc_steal(state, h);
321
322         /* now tell the caller than we will retry asynchronously */
323         return -2;
324 }
325
326 /*
327   a varient of ctdb_ltdb_lock_requeue that also fetches the record
328  */
329 int ctdb_ltdb_lock_fetch_requeue(struct ctdb_db_context *ctdb_db, 
330                                  TDB_DATA key, struct ctdb_ltdb_header *header, 
331                                  struct ctdb_req_header *hdr, TDB_DATA *data,
332                                  void (*recv_pkt)(void *, uint8_t *, uint32_t ),
333                                  void *recv_context)
334 {
335         int ret;
336
337         ret = ctdb_ltdb_lock_requeue(ctdb_db, key, hdr, recv_pkt, recv_context);
338         if (ret == 0) {
339                 ret = ctdb_ltdb_fetch(ctdb_db, key, header, hdr, data);
340                 if (ret != 0) {
341                         ctdb_ltdb_unlock(ctdb_db, key);
342                 }
343         }
344         return ret;
345 }