56eadc5e20ef8e60e071135675435d2c0ef620dc
[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         { "RecoverTimeout",      20,  offsetof(struct ctdb_tunable, recover_timeout) },
34         { "RecoverInterval",      1,  offsetof(struct ctdb_tunable, recover_interval) },
35         { "ElectionTimeout",      3,  offsetof(struct ctdb_tunable, election_timeout) },
36         { "TakeoverTimeout",      9,  offsetof(struct ctdb_tunable, takeover_timeout) },
37         { "MonitorInterval",     15,  offsetof(struct ctdb_tunable, monitor_interval) },
38         { "TickleUpdateInterval",20,  offsetof(struct ctdb_tunable, tickle_update_interval) },
39         { "EventScriptTimeout",  30,  offsetof(struct ctdb_tunable, script_timeout) },
40         { "EventScriptTimeoutCount", 1,  offsetof(struct ctdb_tunable, script_timeout_count) },
41         { "EventScriptUnhealthyOnTimeout", 0, offsetof(struct ctdb_tunable, script_unhealthy_on_timeout) },/* OBSOLETE */
42         { "RecoveryGracePeriod", 120,  offsetof(struct ctdb_tunable, recovery_grace_period) },
43         { "RecoveryBanPeriod",  300,  offsetof(struct ctdb_tunable, recovery_ban_period) },
44         { "DatabaseHashSize", 100001, 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",     0,  offsetof(struct ctdb_tunable, deterministic_public_ips) },
49         { "LCP2PublicIPs",        1,  offsetof(struct ctdb_tunable, lcp2_public_ip_assignment) },
50         { "ReclockPingPeriod",   60,  offsetof(struct ctdb_tunable,  reclock_ping_period) },
51         { "NoIPFailback",         0,  offsetof(struct ctdb_tunable, no_ip_failback) },
52         { "DisableIPFailover",    0,  offsetof(struct ctdb_tunable, disable_ip_failover) },
53         { "VerboseMemoryNames",   0,  offsetof(struct ctdb_tunable, verbose_memory_names) },
54         { "RecdPingTimeout",     60,  offsetof(struct ctdb_tunable, recd_ping_timeout) },
55         { "RecdFailCount",       10,  offsetof(struct ctdb_tunable, recd_ping_failcount) },
56         { "LogLatencyMs",         0,  offsetof(struct ctdb_tunable, log_latency_ms) },
57         { "RecLockLatencyMs",  1000,  offsetof(struct ctdb_tunable, reclock_latency_ms) },
58         { "RecoveryDropAllIPs", 120,  offsetof(struct ctdb_tunable, recovery_drop_all_ips) },
59         { "VerifyRecoveryLock",   1,  offsetof(struct ctdb_tunable, verify_recovery_lock) },
60         { "VacuumDefaultInterval", 10,  offsetof(struct ctdb_tunable, vacuum_default_interval) },
61         { "VacuumMaxRunTime",     120,  offsetof(struct ctdb_tunable, vacuum_max_run_time) },
62         { "RepackLimit",      10000,  offsetof(struct ctdb_tunable, repack_limit) },
63         { "VacuumLimit",       5000,  offsetof(struct ctdb_tunable, vacuum_limit) },
64         { "VacuumMinInterval",   10,  offsetof(struct ctdb_tunable, vacuum_min_interval) },
65         { "VacuumMaxInterval",   10,  offsetof(struct ctdb_tunable, vacuum_max_interval) },
66         { "VacuumFastPathCount", 60, offsetof(struct ctdb_tunable, vacuum_fast_path_count) },
67         { "MaxQueueDropMsg",  1000000, offsetof(struct ctdb_tunable, max_queue_depth_drop_msg) },
68         { "UseStatusEvents",     0,  offsetof(struct ctdb_tunable, use_status_events_for_monitoring) },
69         { "AllowUnhealthyDBRead", 0,  offsetof(struct ctdb_tunable, allow_unhealthy_db_read) },
70         { "StatHistoryInterval",  1,  offsetof(struct ctdb_tunable, stat_history_interval) },
71         { "DeferredAttachTO",  120,  offsetof(struct ctdb_tunable, deferred_attach_timeout) },
72         { "AllowClientDBAttach", 1, offsetof(struct ctdb_tunable, allow_client_db_attach) },
73         { "FetchLockCollapse", 1, offsetof(struct ctdb_tunable, fetch_lock_collapse) }
74 };
75
76 /*
77   set all tunables to defaults
78  */
79 void ctdb_tunables_set_defaults(struct ctdb_context *ctdb)
80 {
81         int i;
82         for (i=0;i<ARRAY_SIZE(tunable_map);i++) {
83                 *(uint32_t *)(tunable_map[i].offset + (uint8_t*)&ctdb->tunable) = tunable_map[i].default_v;
84         }
85 }
86
87
88 /*
89   get a tunable
90  */
91 int32_t ctdb_control_get_tunable(struct ctdb_context *ctdb, TDB_DATA indata, 
92                                  TDB_DATA *outdata)
93 {
94         struct ctdb_control_get_tunable *t = 
95                 (struct ctdb_control_get_tunable *)indata.dptr;
96         char *name;
97         uint32_t val;
98         int i;
99
100         if (indata.dsize < sizeof(*t) ||
101             t->length > indata.dsize - offsetof(struct ctdb_control_get_tunable, name)) {
102                 DEBUG(DEBUG_ERR,("Bad indata in ctdb_control_get_tunable\n"));
103                 return -1;
104         }
105
106         name = talloc_strndup(ctdb, (char*)t->name, t->length);
107         CTDB_NO_MEMORY(ctdb, name);
108
109         for (i=0;i<ARRAY_SIZE(tunable_map);i++) {
110                 if (strcasecmp(name, tunable_map[i].name) == 0) break;
111         }
112         talloc_free(name);
113         
114         if (i == ARRAY_SIZE(tunable_map)) {
115                 return -1;
116         }
117
118         val = *(uint32_t *)(tunable_map[i].offset + (uint8_t*)&ctdb->tunable);
119
120         outdata->dptr = (uint8_t *)talloc(outdata, uint32_t);
121         CTDB_NO_MEMORY(ctdb, outdata->dptr);
122
123         *(uint32_t *)outdata->dptr = val;
124         outdata->dsize = sizeof(uint32_t);
125
126         return 0;
127 }
128
129
130 /*
131   set a tunable
132  */
133 int32_t ctdb_control_set_tunable(struct ctdb_context *ctdb, TDB_DATA indata)
134 {
135         struct ctdb_control_set_tunable *t = 
136                 (struct ctdb_control_set_tunable *)indata.dptr;
137         char *name;
138         int i;
139
140         if (indata.dsize < sizeof(*t) ||
141             t->length > indata.dsize - offsetof(struct ctdb_control_set_tunable, name)) {
142                 DEBUG(DEBUG_ERR,("Bad indata in ctdb_control_set_tunable\n"));
143                 return -1;
144         }
145
146         name = talloc_strndup(ctdb, (char *)t->name, t->length);
147         CTDB_NO_MEMORY(ctdb, name);
148
149         for (i=0;i<ARRAY_SIZE(tunable_map);i++) {
150                 if (strcasecmp(name, tunable_map[i].name) == 0) break;
151         }
152
153         if (!strcmp(name, "VerifyRecoveryLock") && t->value != 0
154         && ctdb->recovery_lock_file == NULL) {
155                 DEBUG(DEBUG_ERR,("Can not activate tunable \"VerifyRecoveryLock\" since there is no recovery lock file set.\n"));
156                 talloc_free(name);
157                 return -1;
158         }
159
160         talloc_free(name);
161         
162         if (i == ARRAY_SIZE(tunable_map)) {
163                 return -1;
164         }
165
166         *(uint32_t *)(tunable_map[i].offset + (uint8_t*)&ctdb->tunable) = t->value;
167
168         return 0;
169 }
170
171 /*
172   list tunables
173  */
174 int32_t ctdb_control_list_tunables(struct ctdb_context *ctdb, TDB_DATA *outdata)
175 {
176         char *list = NULL;
177         int i;
178         struct ctdb_control_list_tunable *t;
179
180         list = talloc_strdup(outdata, tunable_map[0].name);
181         CTDB_NO_MEMORY(ctdb, list);
182
183         for (i=1;i<ARRAY_SIZE(tunable_map);i++) {
184                 list = talloc_asprintf_append(list, ":%s", tunable_map[i].name);
185                 CTDB_NO_MEMORY(ctdb, list);             
186         }
187
188         outdata->dsize = offsetof(struct ctdb_control_list_tunable, data) + 
189                 strlen(list) + 1;
190         outdata->dptr = talloc_size(outdata, outdata->dsize);
191         CTDB_NO_MEMORY(ctdb, outdata->dptr);
192
193         t = (struct ctdb_control_list_tunable *)outdata->dptr;
194         t->length = strlen(list)+1;
195
196         memcpy(t->data, list, t->length);
197         talloc_free(list);
198
199         return 0;       
200 }