recoverd: Handle errors carefully when fetching tunables
[obnox/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         bool obsolete;
27 } tunable_map[] = {
28         { "MaxRedirectCount",     3,  offsetof(struct ctdb_tunable, max_redirect_count), false },
29         { "SeqnumInterval",      1000,  offsetof(struct ctdb_tunable, seqnum_interval), false },
30         { "ControlTimeout",      60, offsetof(struct ctdb_tunable, control_timeout), false },
31         { "TraverseTimeout",     20, offsetof(struct ctdb_tunable, traverse_timeout), false },
32         { "KeepaliveInterval",    5,  offsetof(struct ctdb_tunable, keepalive_interval), false },
33         { "KeepaliveLimit",       5,  offsetof(struct ctdb_tunable, keepalive_limit), false },
34         { "RecoverTimeout",     120,  offsetof(struct ctdb_tunable, recover_timeout), false },
35         { "RecoverInterval",      1,  offsetof(struct ctdb_tunable, recover_interval), false },
36         { "ElectionTimeout",      3,  offsetof(struct ctdb_tunable, election_timeout), false },
37         { "TakeoverTimeout",      9,  offsetof(struct ctdb_tunable, takeover_timeout), false },
38         { "MonitorInterval",     15,  offsetof(struct ctdb_tunable, monitor_interval), false },
39         { "TickleUpdateInterval",20,  offsetof(struct ctdb_tunable, tickle_update_interval), false },
40         { "EventScriptTimeout",  30,  offsetof(struct ctdb_tunable, script_timeout), false },
41         { "EventScriptTimeoutCount", 20,  offsetof(struct ctdb_tunable, script_timeout_count), false },
42         { "EventScriptUnhealthyOnTimeout", 0, offsetof(struct ctdb_tunable, script_unhealthy_on_timeout), true },
43         { "RecoveryGracePeriod", 120,  offsetof(struct ctdb_tunable, recovery_grace_period), false },
44         { "RecoveryBanPeriod",  300,  offsetof(struct ctdb_tunable, recovery_ban_period), false },
45         { "DatabaseHashSize", 100001, offsetof(struct ctdb_tunable, database_hash_size), false },
46         { "DatabaseMaxDead",      5,  offsetof(struct ctdb_tunable, database_max_dead), false },
47         { "RerecoveryTimeout",   10,  offsetof(struct ctdb_tunable, rerecovery_timeout), false },
48         { "EnableBans",           1,  offsetof(struct ctdb_tunable, enable_bans), false },
49         { "DeterministicIPs",     0,  offsetof(struct ctdb_tunable, deterministic_public_ips), false },
50         { "LCP2PublicIPs",        1,  offsetof(struct ctdb_tunable, lcp2_public_ip_assignment), false },
51         { "ReclockPingPeriod",   60,  offsetof(struct ctdb_tunable,  reclock_ping_period), false },
52         { "NoIPFailback",         0,  offsetof(struct ctdb_tunable, no_ip_failback), false },
53         { "DisableIPFailover",    0,  offsetof(struct ctdb_tunable, disable_ip_failover), false },
54         { "VerboseMemoryNames",   0,  offsetof(struct ctdb_tunable, verbose_memory_names), false },
55         { "RecdPingTimeout",     60,  offsetof(struct ctdb_tunable, recd_ping_timeout), false },
56         { "RecdFailCount",       10,  offsetof(struct ctdb_tunable, recd_ping_failcount), false },
57         { "LogLatencyMs",         0,  offsetof(struct ctdb_tunable, log_latency_ms), false },
58         { "RecLockLatencyMs",  1000,  offsetof(struct ctdb_tunable, reclock_latency_ms), false },
59         { "RecoveryDropAllIPs", 120,  offsetof(struct ctdb_tunable, recovery_drop_all_ips), false },
60         { "VerifyRecoveryLock",   1,  offsetof(struct ctdb_tunable, verify_recovery_lock), false },
61         { "VacuumInterval",   10,  offsetof(struct ctdb_tunable, vacuum_interval), false },
62         { "VacuumDefaultInterval", 10,  offsetof(struct ctdb_tunable, vacuum_default_interval), true },
63         { "VacuumMaxRunTime",     120,  offsetof(struct ctdb_tunable, vacuum_max_run_time), false },
64         { "RepackLimit",      10000,  offsetof(struct ctdb_tunable, repack_limit), false },
65         { "VacuumLimit",       5000,  offsetof(struct ctdb_tunable, vacuum_limit), false },
66         { "VacuumMinInterval",   10,  offsetof(struct ctdb_tunable, vacuum_min_interval), true },
67         { "VacuumMaxInterval",   10,  offsetof(struct ctdb_tunable, vacuum_max_interval), true },
68         { "VacuumFastPathCount", 60, offsetof(struct ctdb_tunable, vacuum_fast_path_count), false },
69         { "MaxQueueDropMsg",  1000000, offsetof(struct ctdb_tunable, max_queue_depth_drop_msg), false },
70         { "UseStatusEvents",     0,  offsetof(struct ctdb_tunable, use_status_events_for_monitoring), false },
71         { "AllowUnhealthyDBRead", 0,  offsetof(struct ctdb_tunable, allow_unhealthy_db_read), false },
72         { "StatHistoryInterval",  1,  offsetof(struct ctdb_tunable, stat_history_interval), false },
73         { "DeferredAttachTO",  120,  offsetof(struct ctdb_tunable, deferred_attach_timeout), false },
74         { "AllowClientDBAttach", 1, offsetof(struct ctdb_tunable, allow_client_db_attach), false },
75         { "RecoverPDBBySeqNum",  0, offsetof(struct ctdb_tunable, recover_pdb_by_seqnum), false },
76         { "DeferredRebalanceOnNodeAdd", 300, offsetof(struct ctdb_tunable, deferred_rebalance_on_node_add) },
77         { "FetchCollapse",       1, offsetof(struct ctdb_tunable, fetch_collapse) },
78         { "MaxLACount",         20,  offsetof(struct ctdb_tunable, max_lacount) },
79         { "HopcountMakeSticky",   50,  offsetof(struct ctdb_tunable, hopcount_make_sticky) },
80         { "StickyDuration",      600,  offsetof(struct ctdb_tunable, sticky_duration) },
81         { "StickyPindown",       200,  offsetof(struct ctdb_tunable, sticky_pindown) },
82         { "NoIPTakeover",         0,  offsetof(struct ctdb_tunable, no_ip_takeover), false },
83         { "DBRecordCountWarn",    100000,  offsetof(struct ctdb_tunable, db_record_count_warn), false },
84         { "DBRecordSizeWarn",   10000000,  offsetof(struct ctdb_tunable, db_record_size_warn), false },
85         { "DBSizeWarn",        100000000,  offsetof(struct ctdb_tunable, db_size_warn), false },
86         { "PullDBPreallocation", 10*1024*1024,  offsetof(struct ctdb_tunable, pulldb_preallocation_size), false },
87         { "NoIPHostOnAllDisabled",    0,  offsetof(struct ctdb_tunable, no_ip_host_on_all_disabled), false },
88         { "DeadlockTimeout",    300, offsetof(struct ctdb_tunable, deadlock_timeout), false },
89         { "Samba3AvoidDeadlocks", 0, offsetof(struct ctdb_tunable, samba3_hack), false },
90 };
91
92 /*
93   set all tunables to defaults
94  */
95 void ctdb_tunables_set_defaults(struct ctdb_context *ctdb)
96 {
97         int i;
98         for (i=0;i<ARRAY_SIZE(tunable_map);i++) {
99                 *(uint32_t *)(tunable_map[i].offset + (uint8_t*)&ctdb->tunable) = tunable_map[i].default_v;
100         }
101 }
102
103
104 /*
105   get a tunable
106  */
107 int32_t ctdb_control_get_tunable(struct ctdb_context *ctdb, TDB_DATA indata, 
108                                  TDB_DATA *outdata)
109 {
110         struct ctdb_control_get_tunable *t = 
111                 (struct ctdb_control_get_tunable *)indata.dptr;
112         char *name;
113         uint32_t val;
114         int i;
115
116         if (indata.dsize < sizeof(*t) ||
117             t->length > indata.dsize - offsetof(struct ctdb_control_get_tunable, name)) {
118                 DEBUG(DEBUG_ERR,("Bad indata in ctdb_control_get_tunable\n"));
119                 return -1;
120         }
121
122         name = talloc_strndup(ctdb, (char*)t->name, t->length);
123         CTDB_NO_MEMORY(ctdb, name);
124
125         for (i=0;i<ARRAY_SIZE(tunable_map);i++) {
126                 if (strcasecmp(name, tunable_map[i].name) == 0) break;
127         }
128         talloc_free(name);
129         
130         if (i == ARRAY_SIZE(tunable_map)) {
131                 return -EINVAL;
132         }
133
134         val = *(uint32_t *)(tunable_map[i].offset + (uint8_t*)&ctdb->tunable);
135
136         outdata->dptr = (uint8_t *)talloc(outdata, uint32_t);
137         CTDB_NO_MEMORY(ctdb, outdata->dptr);
138
139         *(uint32_t *)outdata->dptr = val;
140         outdata->dsize = sizeof(uint32_t);
141
142         return 0;
143 }
144
145
146 /*
147   set a tunable
148  */
149 int32_t ctdb_control_set_tunable(struct ctdb_context *ctdb, TDB_DATA indata)
150 {
151         struct ctdb_control_set_tunable *t = 
152                 (struct ctdb_control_set_tunable *)indata.dptr;
153         char *name;
154         int i;
155
156         if (indata.dsize < sizeof(*t) ||
157             t->length > indata.dsize - offsetof(struct ctdb_control_set_tunable, name)) {
158                 DEBUG(DEBUG_ERR,("Bad indata in ctdb_control_set_tunable\n"));
159                 return -1;
160         }
161
162         name = talloc_strndup(ctdb, (char *)t->name, t->length);
163         CTDB_NO_MEMORY(ctdb, name);
164
165         for (i=0;i<ARRAY_SIZE(tunable_map);i++) {
166                 if (strcasecmp(name, tunable_map[i].name) == 0) break;
167         }
168
169         if (!strcmp(name, "VerifyRecoveryLock") && t->value != 0
170         && ctdb->recovery_lock_file == NULL) {
171                 DEBUG(DEBUG_ERR,("Can not activate tunable \"VerifyRecoveryLock\" since there is no recovery lock file set.\n"));
172                 talloc_free(name);
173                 return -1;
174         }
175
176         talloc_free(name);
177         
178         if (i == ARRAY_SIZE(tunable_map)) {
179                 return -1;
180         }
181
182         *(uint32_t *)(tunable_map[i].offset + (uint8_t*)&ctdb->tunable) = t->value;
183
184         return 0;
185 }
186
187 /*
188   list tunables
189  */
190 int32_t ctdb_control_list_tunables(struct ctdb_context *ctdb, TDB_DATA *outdata)
191 {
192         char *list = NULL;
193         int i;
194         struct ctdb_control_list_tunable *t;
195
196         list = talloc_strdup(outdata, tunable_map[0].name);
197         CTDB_NO_MEMORY(ctdb, list);
198
199         for (i=1;i<ARRAY_SIZE(tunable_map);i++) {
200                 if (tunable_map[i].obsolete) {
201                         continue;
202                 }
203                 list = talloc_asprintf_append(list, ":%s", tunable_map[i].name);
204                 CTDB_NO_MEMORY(ctdb, list);             
205         }
206
207         outdata->dsize = offsetof(struct ctdb_control_list_tunable, data) + 
208                 strlen(list) + 1;
209         outdata->dptr = talloc_size(outdata, outdata->dsize);
210         CTDB_NO_MEMORY(ctdb, outdata->dptr);
211
212         t = (struct ctdb_control_list_tunable *)outdata->dptr;
213         t->length = strlen(list)+1;
214
215         memcpy(t->data, list, t->length);
216         talloc_free(list);
217
218         return 0;       
219 }