4 Copyright (C) Andrew Tridgell 2006
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.
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.
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
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"
31 return error string for last error
33 const char *ctdb_errstr(struct ctdb_context *ctdb)
40 remember an error message
42 void ctdb_set_error(struct ctdb_context *ctdb, const char *fmt, ...)
45 talloc_free(ctdb->err_msg);
47 ctdb->err_msg = talloc_vasprintf(ctdb, fmt, ap);
48 DEBUG(0,("ctdb error: %s\n", ctdb->err_msg));
53 a fatal internal error occurred - no hope for recovery
55 void ctdb_fatal(struct ctdb_context *ctdb, const char *msg)
57 DEBUG(0,("ctdb fatal error: %s\n", msg));
64 int ctdb_parse_address(struct ctdb_context *ctdb,
65 TALLOC_CTX *mem_ctx, const char *str,
66 struct ctdb_address *address)
71 ctdb_set_error(ctdb, "Badly formed node '%s'\n", str);
75 address->address = talloc_strndup(mem_ctx, str, p-str);
76 address->port = strtoul(p+1, NULL, 0);
82 check if two addresses are the same
84 bool ctdb_same_address(struct ctdb_address *a1, struct ctdb_address *a2)
86 return strcmp(a1->address, a2->address) == 0 && a1->port == a2->port;
91 hash function for mapping data to a VNN - taken from tdb
93 uint32_t ctdb_hash(const TDB_DATA *key)
95 uint32_t value; /* Used to compute the hash value. */
96 uint32_t i; /* Used to cycle through random values. */
98 /* Set the initial value from the key size. */
99 for (value = 0x238F13AF * key->dsize, i=0; i < key->dsize; i++)
100 value = (value + (key->dptr[i] << (i*5 % 24)));
102 return (1103515243 * value + 12345);
106 hash function for a string
108 uint32_t ctdb_hash_string(const char *str)
111 data.dptr = (uint8_t *)discard_const(str);
112 data.dsize = strlen(str)+1;
113 return ctdb_hash(&data);
117 a type checking varient of idr_find
119 static void *_idr_find_type(struct idr_context *idp, int id, const char *type, const char *location)
121 void *p = idr_find(idp, id);
122 if (p && talloc_check_name(p, type) == NULL) {
123 DEBUG(0,("%s idr_find_type expected type %s but got %s\n",
124 location, type, talloc_get_name(p)));
132 update a max latency number
134 void ctdb_latency(double *latency, struct timeval t)
136 double l = timeval_elapsed(&t);
142 uint32_t ctdb_reqid_new(struct ctdb_context *ctdb, void *state)
146 id = ctdb->idr_cnt++ & 0xFFFF;
147 id |= (idr_get_new(ctdb->idr, state, 0xFFFF)<<16);
151 void *_ctdb_reqid_find(struct ctdb_context *ctdb, uint32_t reqid, const char *type, const char *location)
155 p = _idr_find_type(ctdb->idr, (reqid>>16)&0xFFFF, type, location);
157 DEBUG(0, ("Could not find idr:%u\n",reqid));
164 void ctdb_reqid_remove(struct ctdb_context *ctdb, uint32_t reqid)
168 ret = idr_remove(ctdb->idr, (reqid>>16)&0xFFFF);
170 DEBUG(0, ("Removing idr that does not exist\n"));
176 form a ctdb_rec_data record from a key/data pair
178 struct ctdb_rec_data *ctdb_marshall_record(TALLOC_CTX *mem_ctx, uint32_t reqid, TDB_DATA key, TDB_DATA data)
181 struct ctdb_rec_data *d;
183 length = offsetof(struct ctdb_rec_data, data) + key.dsize + data.dsize;
184 d = (struct ctdb_rec_data *)talloc_size(mem_ctx, length);
190 d->keylen = key.dsize;
191 d->datalen = data.dsize;
192 memcpy(&d->data[0], key.dptr, key.dsize);
193 memcpy(&d->data[key.dsize], data.dptr, data.dsize);
202 if possible, make this task real time
204 void ctdb_set_realtime(void)
206 #if HAVE_SCHED_SETSCHEDULER
207 struct sched_param p;
208 p.__sched_priority = 1;
210 if (sched_setscheduler(getpid(), SCHED_FIFO, &p) == -1) {
211 DEBUG(0,("Unable to set scheduler to SCHED_FIFO (%s)\n", strerror(errno)));
213 DEBUG(0,("Set scheduler to SCHED_FIFO\n"));