merge from tridge
[metze/ctdb/wip.git] / common / ctdb_util.c
1 /* 
2    ctdb utility 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
28 int LogLevel;
29
30 /*
31   return error string for last error
32 */
33 const char *ctdb_errstr(struct ctdb_context *ctdb)
34 {
35         return ctdb->err_msg;
36 }
37
38
39 /*
40   remember an error message
41 */
42 void ctdb_set_error(struct ctdb_context *ctdb, const char *fmt, ...)
43 {
44         va_list ap;
45         talloc_free(ctdb->err_msg);
46         va_start(ap, fmt);
47         ctdb->err_msg = talloc_vasprintf(ctdb, fmt, ap);
48         DEBUG(0,("ctdb error: %s\n", ctdb->err_msg));
49         va_end(ap);
50 }
51
52 /*
53   a fatal internal error occurred - no hope for recovery
54 */
55 void ctdb_fatal(struct ctdb_context *ctdb, const char *msg)
56 {
57         DEBUG(0,("ctdb fatal error: %s\n", msg));
58         abort();
59 }
60
61 /*
62   parse a IP:port pair
63 */
64 int ctdb_parse_address(struct ctdb_context *ctdb,
65                        TALLOC_CTX *mem_ctx, const char *str,
66                        struct ctdb_address *address)
67 {
68         char *p;
69         p = strchr(str, ':');
70         if (p == NULL) {
71                 ctdb_set_error(ctdb, "Badly formed node '%s'\n", str);
72                 return -1;
73         }
74         
75         address->address = talloc_strndup(mem_ctx, str, p-str);
76         address->port = strtoul(p+1, NULL, 0);
77         return 0;
78 }
79
80
81 /*
82   check if two addresses are the same
83 */
84 bool ctdb_same_address(struct ctdb_address *a1, struct ctdb_address *a2)
85 {
86         return strcmp(a1->address, a2->address) == 0 && a1->port == a2->port;
87 }
88
89
90 /*
91   hash function for mapping data to a VNN - taken from tdb
92 */
93 uint32_t ctdb_hash(const TDB_DATA *key)
94 {
95         uint32_t value; /* Used to compute the hash value.  */
96         uint32_t i;     /* Used to cycle through random values. */
97
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)));
101
102         return (1103515243 * value + 12345);  
103 }
104
105 /*
106   hash function for a string
107 */
108 uint32_t ctdb_hash_string(const char *str)
109 {
110         TDB_DATA data;
111         data.dptr = (uint8_t *)discard_const(str);
112         data.dsize = strlen(str)+1;
113         return ctdb_hash(&data);
114 }
115
116 /*
117   a type checking varient of idr_find
118  */
119 static void *_idr_find_type(struct idr_context *idp, int id, const char *type, const char *location)
120 {
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)));
125                 return NULL;
126         }
127         return p;
128 }
129
130
131 /*
132   update a max latency number
133  */
134 void ctdb_latency(double *latency, struct timeval t)
135 {
136         double l = timeval_elapsed(&t);
137         if (l > *latency) {
138                 *latency = l;
139         }
140 }
141
142 uint32_t ctdb_reqid_new(struct ctdb_context *ctdb, void *state)
143 {
144         uint32_t id;
145
146         id  = ctdb->idr_cnt++ & 0xFFFF;
147         id |= (idr_get_new(ctdb->idr, state, 0xFFFF)<<16);
148         return id;
149 }
150
151 void *_ctdb_reqid_find(struct ctdb_context *ctdb, uint32_t reqid, const char *type, const char *location)
152 {
153         void *p;
154
155         p = _idr_find_type(ctdb->idr, (reqid>>16)&0xFFFF, type, location);
156         if (p == NULL) {
157                 DEBUG(0, ("Could not find idr:%u\n",reqid));
158         }
159
160         return p;
161 }
162
163
164 void ctdb_reqid_remove(struct ctdb_context *ctdb, uint32_t reqid)
165 {
166         int ret;
167
168         ret = idr_remove(ctdb->idr, (reqid>>16)&0xFFFF);
169         if (ret != 0) {
170                 DEBUG(0, ("Removing idr that does not exist\n"));
171         }
172 }
173
174
175 /*
176   form a ctdb_rec_data record from a key/data pair
177  */
178 struct ctdb_rec_data *ctdb_marshall_record(TALLOC_CTX *mem_ctx, uint32_t reqid, TDB_DATA key, TDB_DATA data)
179 {
180         size_t length;
181         struct ctdb_rec_data *d;
182
183         length = offsetof(struct ctdb_rec_data, data) + key.dsize + data.dsize;
184         d = (struct ctdb_rec_data *)talloc_size(mem_ctx, length);
185         if (d == NULL) {
186                 return NULL;
187         }
188         d->length = length;
189         d->reqid = reqid;
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);
194         return d;
195 }
196
197 #if HAVE_SCHED_H
198 #include <sched.h>
199 #endif
200
201 /*
202   if possible, make this task real time
203  */
204 void ctdb_set_realtime(void)
205 {
206 #if HAVE_SCHED_SETSCHEDULER
207         struct sched_param p;
208         p.__sched_priority = 1;
209
210         if (sched_setscheduler(getpid(), SCHED_FIFO, &p) == -1) {
211                 DEBUG(0,("Unable to set scheduler to SCHED_FIFO (%s)\n", strerror(errno)));
212         } else {
213                 DEBUG(0,("Set scheduler to SCHED_FIFO\n"));
214         }
215 #endif
216 }