f21139eab2cab36b3f4d5449588d3106c8ae1776
[vlendec/samba-autobuild/.git] / ctdb / include / ctdb_private.h
1 /* 
2    ctdb database library
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
22 /*
23   an installed ctdb remote call
24 */
25 struct ctdb_registered_call {
26         struct ctdb_registered_call *next, *prev;
27         uint32_t id;
28         ctdb_fn_t fn;
29 };
30
31 /*
32   this address structure might need to be generalised later for some
33   transports
34 */
35 struct ctdb_address {
36         const char *address;
37         int port;
38 };
39
40 /*
41   state associated with one node
42 */
43 struct ctdb_node {
44         struct ctdb_context *ctdb;
45         struct ctdb_address address;
46         const char *name; /* for debug messages */
47         void *private; /* private to transport */
48         uint32_t vnn;
49 };
50
51 /*
52   transport specific methods
53 */
54 struct ctdb_methods {
55         int (*start)(struct ctdb_context *); /* start protocol processing */    
56         int (*add_node)(struct ctdb_node *); /* setup a new node */     
57         int (*queue_pkt)(struct ctdb_node *, uint8_t *data, uint32_t length);
58 };
59
60 /*
61   transport calls up to the ctdb layer
62 */
63 struct ctdb_upcalls {
64         /* recv_pkt is called when a packet comes in */
65         void (*recv_pkt)(struct ctdb_context *, uint8_t *data, uint32_t length);
66
67         /* node_dead is called when an attempt to send to a node fails */
68         void (*node_dead)(struct ctdb_node *);
69
70         /* node_connected is called when a connection to a node is established */
71         void (*node_connected)(struct ctdb_node *);
72 };
73
74 /* main state of the ctdb daemon */
75 struct ctdb_context {
76         struct event_context *ev;
77         struct ctdb_address address;
78         const char *name;
79         uint32_t vnn; /* our own vnn */
80         uint32_t num_nodes;
81         uint32_t num_connected;
82         unsigned flags;
83         struct idr_context *idr;
84         struct ctdb_node **nodes; /* array of nodes in the cluster - indexed by vnn */
85         struct ctdb_registered_call *calls; /* list of registered calls */
86         char *err_msg;
87         struct tdb_context *ltdb;
88         const struct ctdb_methods *methods; /* transport methods */
89         const struct ctdb_upcalls *upcalls; /* transport upcalls */
90         void *private; /* private to transport */
91 };
92
93 #define CTDB_NO_MEMORY(ctdb, p) do { if (!(p)) { \
94           ctdb_set_error(ctdb, "Out of memory at %s:%d", __FILE__, __LINE__); \
95           return -1; }} while (0)
96
97 #define CTDB_NO_MEMORY_NULL(ctdb, p) do { if (!(p)) { \
98           ctdb_set_error(ctdb, "Out of memory at %s:%d", __FILE__, __LINE__); \
99           return NULL; }} while (0)
100
101 #define CTDB_NO_MEMORY_FATAL(ctdb, p) do { if (!(p)) { \
102           ctdb_fatal(ctdb, "Out of memory in " __location__ ); \
103           }} while (0)
104
105 /* arbitrary maximum timeout for ctdb operations */
106 #define CTDB_REQ_TIMEOUT 10
107
108 /* max number of redirects before we ask the lmaster */
109 #define CTDB_MAX_REDIRECT 2
110
111 /* number of consecutive calls from the same node before we give them
112    the record */
113 #define CTDB_MAX_LACOUNT 7
114
115 /*
116   the extended header for records in the ltdb
117 */
118 struct ctdb_ltdb_header {
119         uint64_t rsn;
120         uint32_t dmaster;
121         uint32_t laccessor;
122         uint32_t lacount;
123 };
124
125
126 /*
127   operation IDs
128 */
129 enum ctdb_operation {
130         CTDB_REQ_CALL       = 0,
131         CTDB_REPLY_CALL     = 1,
132         CTDB_REPLY_REDIRECT = 2,
133         CTDB_REQ_DMASTER    = 3,
134         CTDB_REPLY_DMASTER  = 4,
135         CTDB_REPLY_ERROR    = 5
136 };
137
138 /*
139   packet structures
140 */
141 struct ctdb_req_header {
142         uint32_t length;
143         uint32_t operation;
144         uint32_t destnode;
145         uint32_t srcnode;
146         uint32_t reqid;
147 };
148
149 struct ctdb_req_call {
150         struct ctdb_req_header hdr;
151         uint32_t callid;
152         uint32_t keylen;
153         uint32_t calldatalen;
154         uint8_t data[0]; /* key[] followed by calldata[] */
155 };
156
157 struct ctdb_reply_call {
158         struct ctdb_req_header hdr;
159         uint32_t datalen;
160         uint8_t  data[0];
161 };
162
163 struct ctdb_reply_error {
164         struct ctdb_req_header hdr;
165         uint32_t status;
166         uint32_t msglen;
167         uint8_t  msg[0];
168 };
169
170 struct ctdb_reply_redirect {
171         struct ctdb_req_header hdr;
172         uint32_t dmaster;
173 };
174
175 struct ctdb_req_dmaster {
176         struct ctdb_req_header hdr;
177         uint32_t dmaster;
178         uint32_t keylen;
179         uint32_t datalen;
180         uint8_t  data[0];
181 };
182
183 struct ctdb_reply_dmaster {
184         struct ctdb_req_header hdr;
185         uint32_t datalen;
186         uint8_t  data[0];
187 };
188
189 /* internal prototypes */
190 void ctdb_set_error(struct ctdb_context *ctdb, const char *fmt, ...);
191 void ctdb_fatal(struct ctdb_context *ctdb, const char *msg);
192 bool ctdb_same_address(struct ctdb_address *a1, struct ctdb_address *a2);
193 int ctdb_parse_address(struct ctdb_context *ctdb,
194                        TALLOC_CTX *mem_ctx, const char *str,
195                        struct ctdb_address *address);
196 uint32_t ctdb_hash(const TDB_DATA *key);
197 void ctdb_request_call(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
198 void ctdb_request_dmaster(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
199 void ctdb_reply_dmaster(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
200 void ctdb_reply_call(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
201 void ctdb_reply_error(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
202 void ctdb_reply_redirect(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
203
204 uint32_t ctdb_lmaster(struct ctdb_context *ctdb, const TDB_DATA *key);
205 int ctdb_ltdb_fetch(struct ctdb_context *ctdb, 
206                     TDB_DATA key, struct ctdb_ltdb_header *header, TDB_DATA *data);
207 int ctdb_ltdb_store(struct ctdb_context *ctdb, TDB_DATA key, 
208                     struct ctdb_ltdb_header *header, TDB_DATA data);
209
210