merge from ronnie
[metze/ctdb/wip.git] / server / ctdb_tunables.c
1 /* 
2    ctdb tunables code
3
4    Copyright (C) Andrew Tridgell  2007
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 2 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, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20 #include "includes.h"
21 #include "../include/ctdb_private.h"
22
23 static const struct {
24         const char *name;
25         uint32_t default_v;
26         size_t offset;  
27 } tunable_map[] = {
28         { "MaxRedirectCount",     3,  offsetof(struct ctdb_tunable, max_redirect_count) },
29         { "SeqnumFrequency",      1,  offsetof(struct ctdb_tunable, seqnum_frequency) },
30         { "ControlTimeout",      60, offsetof(struct ctdb_tunable, control_timeout) },
31         { "TraverseTimeout",     20, offsetof(struct ctdb_tunable, traverse_timeout) },
32         { "KeepaliveInterval",    2,  offsetof(struct ctdb_tunable, keepalive_interval) },
33         { "KeepaliveLimit",       5,  offsetof(struct ctdb_tunable, keepalive_limit) },
34         { "MaxLACount",           7,  offsetof(struct ctdb_tunable, max_lacount) },
35         { "RecoverTimeout",       5,  offsetof(struct ctdb_tunable, recover_timeout) },
36         { "RecoverInterval",      1,  offsetof(struct ctdb_tunable, recover_interval) },
37         { "ElectionTimeout",      3,  offsetof(struct ctdb_tunable, election_timeout) },
38         { "TakeoverTimeout",      5,  offsetof(struct ctdb_tunable, takeover_timeout) },
39         { "MonitorInterval",     15,  offsetof(struct ctdb_tunable, monitor_interval) },
40         { "EventScriptTimeout",  20,  offsetof(struct ctdb_tunable, script_timeout) },
41         { "RecoveryGracePeriod", 60,  offsetof(struct ctdb_tunable, recovery_grace_period) },
42         { "RecoveryBanPeriod",  300,  offsetof(struct ctdb_tunable, recovery_ban_period) },
43         { "DatabaseHashSize", 10000,  offsetof(struct ctdb_tunable, database_hash_size) },
44         { "RerecoveryTimeout",   10,  offsetof(struct ctdb_tunable, rerecovery_timeout) },
45 };
46
47 /*
48   set all tunables to defaults
49  */
50 void ctdb_tunables_set_defaults(struct ctdb_context *ctdb)
51 {
52         int i;
53         for (i=0;i<ARRAY_SIZE(tunable_map);i++) {
54                 *(uint32_t *)(tunable_map[i].offset + (uint8_t*)&ctdb->tunable) = tunable_map[i].default_v;
55         }
56 }
57
58
59 /*
60   get a tunable
61  */
62 int32_t ctdb_control_get_tunable(struct ctdb_context *ctdb, TDB_DATA indata, 
63                                  TDB_DATA *outdata)
64 {
65         struct ctdb_control_get_tunable *t = 
66                 (struct ctdb_control_get_tunable *)indata.dptr;
67         char *name;
68         uint32_t val;
69         int i;
70
71         if (indata.dsize < sizeof(*t) ||
72             t->length > indata.dsize - offsetof(struct ctdb_control_get_tunable, name)) {
73                 DEBUG(0,("Bad indata in ctdb_control_get_tunable\n"));
74                 return -1;
75         }
76
77         name = talloc_strndup(ctdb, (char*)t->name, t->length);
78         CTDB_NO_MEMORY(ctdb, name);
79
80         for (i=0;i<ARRAY_SIZE(tunable_map);i++) {
81                 if (strcasecmp(name, tunable_map[i].name) == 0) break;
82         }
83         talloc_free(name);
84         
85         if (i == ARRAY_SIZE(tunable_map)) {
86                 return -1;
87         }
88
89         val = *(uint32_t *)(tunable_map[i].offset + (uint8_t*)&ctdb->tunable);
90
91         outdata->dptr = (uint8_t *)talloc(outdata, uint32_t);
92         CTDB_NO_MEMORY(ctdb, outdata->dptr);
93
94         *(uint32_t *)outdata->dptr = val;
95         outdata->dsize = sizeof(uint32_t);
96
97         return 0;
98 }
99
100
101 /*
102   set a tunable
103  */
104 int32_t ctdb_control_set_tunable(struct ctdb_context *ctdb, TDB_DATA indata)
105 {
106         struct ctdb_control_set_tunable *t = 
107                 (struct ctdb_control_set_tunable *)indata.dptr;
108         char *name;
109         int i;
110
111         if (indata.dsize < sizeof(*t) ||
112             t->length > indata.dsize - offsetof(struct ctdb_control_set_tunable, name)) {
113                 DEBUG(0,("Bad indata in ctdb_control_set_tunable\n"));
114                 return -1;
115         }
116
117         name = talloc_strndup(ctdb, (char *)t->name, t->length);
118         CTDB_NO_MEMORY(ctdb, name);
119
120         for (i=0;i<ARRAY_SIZE(tunable_map);i++) {
121                 if (strcasecmp(name, tunable_map[i].name) == 0) break;
122         }
123
124         talloc_free(name);
125         
126         if (i == ARRAY_SIZE(tunable_map)) {
127                 return -1;
128         }
129         
130         *(uint32_t *)(tunable_map[i].offset + (uint8_t*)&ctdb->tunable) = t->value;
131
132         return 0;
133 }
134
135 /*
136   list tunables
137  */
138 int32_t ctdb_control_list_tunables(struct ctdb_context *ctdb, TDB_DATA *outdata)
139 {
140         char *list = NULL;
141         int i;
142         struct ctdb_control_list_tunable *t;
143
144         list = talloc_strdup(outdata, tunable_map[0].name);
145         CTDB_NO_MEMORY(ctdb, list);
146
147         for (i=1;i<ARRAY_SIZE(tunable_map);i++) {
148                 list = talloc_asprintf_append(list, ":%s", tunable_map[i].name);
149                 CTDB_NO_MEMORY(ctdb, list);             
150         }
151
152         outdata->dsize = offsetof(struct ctdb_control_list_tunable, data) + 
153                 strlen(list) + 1;
154         outdata->dptr = talloc_size(outdata, outdata->dsize);
155         CTDB_NO_MEMORY(ctdb, outdata->dptr);
156
157         t = (struct ctdb_control_list_tunable *)outdata->dptr;
158         t->length = strlen(list)+1;
159
160         memcpy(t->data, list, t->length);
161         talloc_free(list);
162
163         return 0;       
164 }