1e8a99fed4703eb7cee11cd0d9e6eb3c60343524
[sahlberg/ctdb.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 3 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, see <http://www.gnu.org/licenses/>.
18 */
19 #include "includes.h"
20 #include "../include/ctdb_private.h"
21
22 static const struct {
23         const char *name;
24         uint32_t default_v;
25         size_t offset;  
26 } tunable_map[] = {
27         { "MaxRedirectCount",     3,  offsetof(struct ctdb_tunable, max_redirect_count) },
28         { "SeqnumInterval",      1000,  offsetof(struct ctdb_tunable, seqnum_interval) },
29         { "ControlTimeout",      60, offsetof(struct ctdb_tunable, control_timeout) },
30         { "TraverseTimeout",     20, offsetof(struct ctdb_tunable, traverse_timeout) },
31         { "KeepaliveInterval",    5,  offsetof(struct ctdb_tunable, keepalive_interval) },
32         { "KeepaliveLimit",       5,  offsetof(struct ctdb_tunable, keepalive_limit) },
33         { "MaxLACount",           7,  offsetof(struct ctdb_tunable, max_lacount) },
34         { "RecoverTimeout",      20,  offsetof(struct ctdb_tunable, recover_timeout) },
35         { "RecoverInterval",      1,  offsetof(struct ctdb_tunable, recover_interval) },
36         { "ElectionTimeout",      3,  offsetof(struct ctdb_tunable, election_timeout) },
37         { "TakeoverTimeout",      5,  offsetof(struct ctdb_tunable, takeover_timeout) },
38         { "MonitorInterval",     15,  offsetof(struct ctdb_tunable, monitor_interval) },
39         { "TickleUpdateInterval",20,  offsetof(struct ctdb_tunable, tickle_update_interval) },
40         { "EventScriptTimeout",  20,  offsetof(struct ctdb_tunable, script_timeout) },
41         { "EventScriptBanCount",  5,  offsetof(struct ctdb_tunable, script_ban_count) },
42         { "RecoveryGracePeriod", 120,  offsetof(struct ctdb_tunable, recovery_grace_period) },
43         { "RecoveryBanPeriod",  300,  offsetof(struct ctdb_tunable, recovery_ban_period) },
44         { "DatabaseHashSize", 10000,  offsetof(struct ctdb_tunable, database_hash_size) },
45         { "DatabaseMaxDead",      5,  offsetof(struct ctdb_tunable, database_max_dead) },
46         { "RerecoveryTimeout",   10,  offsetof(struct ctdb_tunable, rerecovery_timeout) },
47         { "EnableBans",           1,  offsetof(struct ctdb_tunable, enable_bans) },
48         { "DeterministicIPs",     1,  offsetof(struct ctdb_tunable, deterministic_public_ips) },
49         { "DisableWhenUnhealthy", 0,  offsetof(struct ctdb_tunable, disable_when_unhealthy) },
50         { "ReclockPingPeriod",   60,  offsetof(struct ctdb_tunable,  reclock_ping_period) },
51         { "NoIPFailback",         0,  offsetof(struct ctdb_tunable, no_ip_failback) },
52         { "VerboseMemoryNames",   0,  offsetof(struct ctdb_tunable, verbose_memory_names) },
53         { "RecdPingTimeout",     20,  offsetof(struct ctdb_tunable, recd_ping_timeout) },
54         { "RecdFailCount",        3,  offsetof(struct ctdb_tunable, recd_ping_failcount) },
55         { "LogLatencyMs",         0,  offsetof(struct ctdb_tunable, log_latency_ms) },
56         { "RecLockLatencyMs",  1000,  offsetof(struct ctdb_tunable, reclock_latency_ms) },
57         { "RecoveryDropAllIPs",  60,  offsetof(struct ctdb_tunable, recovery_drop_all_ips) },
58         { "VerifyRecoveryLock",   1,  offsetof(struct ctdb_tunable, verify_recovery_lock) },
59         { "VacuumDefaultInterval", 120,  offsetof(struct ctdb_tunable, vacuum_default_interval) },
60         { "VacuumMaxRunTime",     30,  offsetof(struct ctdb_tunable, vacuum_max_run_time) },
61         { "RepackLimit",      10000,  offsetof(struct ctdb_tunable, repack_limit) },
62 };
63
64 /*
65   set all tunables to defaults
66  */
67 void ctdb_tunables_set_defaults(struct ctdb_context *ctdb)
68 {
69         int i;
70         for (i=0;i<ARRAY_SIZE(tunable_map);i++) {
71                 *(uint32_t *)(tunable_map[i].offset + (uint8_t*)&ctdb->tunable) = tunable_map[i].default_v;
72         }
73 }
74
75
76 /*
77   get a tunable
78  */
79 int32_t ctdb_control_get_tunable(struct ctdb_context *ctdb, TDB_DATA indata, 
80                                  TDB_DATA *outdata)
81 {
82         struct ctdb_control_get_tunable *t = 
83                 (struct ctdb_control_get_tunable *)indata.dptr;
84         char *name;
85         uint32_t val;
86         int i;
87
88         if (indata.dsize < sizeof(*t) ||
89             t->length > indata.dsize - offsetof(struct ctdb_control_get_tunable, name)) {
90                 DEBUG(DEBUG_ERR,("Bad indata in ctdb_control_get_tunable\n"));
91                 return -1;
92         }
93
94         name = talloc_strndup(ctdb, (char*)t->name, t->length);
95         CTDB_NO_MEMORY(ctdb, name);
96
97         for (i=0;i<ARRAY_SIZE(tunable_map);i++) {
98                 if (strcasecmp(name, tunable_map[i].name) == 0) break;
99         }
100         talloc_free(name);
101         
102         if (i == ARRAY_SIZE(tunable_map)) {
103                 return -1;
104         }
105
106         val = *(uint32_t *)(tunable_map[i].offset + (uint8_t*)&ctdb->tunable);
107
108         outdata->dptr = (uint8_t *)talloc(outdata, uint32_t);
109         CTDB_NO_MEMORY(ctdb, outdata->dptr);
110
111         *(uint32_t *)outdata->dptr = val;
112         outdata->dsize = sizeof(uint32_t);
113
114         return 0;
115 }
116
117
118 /*
119   set a tunable
120  */
121 int32_t ctdb_control_set_tunable(struct ctdb_context *ctdb, TDB_DATA indata)
122 {
123         struct ctdb_control_set_tunable *t = 
124                 (struct ctdb_control_set_tunable *)indata.dptr;
125         char *name;
126         int i;
127
128         if (indata.dsize < sizeof(*t) ||
129             t->length > indata.dsize - offsetof(struct ctdb_control_set_tunable, name)) {
130                 DEBUG(DEBUG_ERR,("Bad indata in ctdb_control_set_tunable\n"));
131                 return -1;
132         }
133
134         name = talloc_strndup(ctdb, (char *)t->name, t->length);
135         CTDB_NO_MEMORY(ctdb, name);
136
137         for (i=0;i<ARRAY_SIZE(tunable_map);i++) {
138                 if (strcasecmp(name, tunable_map[i].name) == 0) break;
139         }
140
141         talloc_free(name);
142         
143         if (i == ARRAY_SIZE(tunable_map)) {
144                 return -1;
145         }
146         
147         *(uint32_t *)(tunable_map[i].offset + (uint8_t*)&ctdb->tunable) = t->value;
148
149         return 0;
150 }
151
152 /*
153   list tunables
154  */
155 int32_t ctdb_control_list_tunables(struct ctdb_context *ctdb, TDB_DATA *outdata)
156 {
157         char *list = NULL;
158         int i;
159         struct ctdb_control_list_tunable *t;
160
161         list = talloc_strdup(outdata, tunable_map[0].name);
162         CTDB_NO_MEMORY(ctdb, list);
163
164         for (i=1;i<ARRAY_SIZE(tunable_map);i++) {
165                 list = talloc_asprintf_append(list, ":%s", tunable_map[i].name);
166                 CTDB_NO_MEMORY(ctdb, list);             
167         }
168
169         outdata->dsize = offsetof(struct ctdb_control_list_tunable, data) + 
170                 strlen(list) + 1;
171         outdata->dptr = talloc_size(outdata, outdata->dsize);
172         CTDB_NO_MEMORY(ctdb, outdata->dptr);
173
174         t = (struct ctdb_control_list_tunable *)outdata->dptr;
175         t->length = strlen(list)+1;
176
177         memcpy(t->data, list, t->length);
178         talloc_free(list);
179
180         return 0;       
181 }