/
bc_file.c
283 lines (229 loc) · 7 KB
/
bc_file.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
* Copyright 2013 Couchbase, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "internal.h"
#include "clconfig.h"
#include "simplestring.h"
#define CONFIG_CACHE_MAGIC "{{{fb85b563d0a8f65fa8d3d58f1b3a0708}}}"
#define LOG(fprovider, sevbase, msg) \
LCB_LOG_EX(fprovider->base.parent->settings, "file", LCB_LOG_##sevbase, msg)
typedef struct {
clconfig_provider base;
char *filename;
clconfig_info *config;
time_t last_mtime;
int last_errno;
lcb_async_t async;
clconfig_listener listener;
} file_provider;
static int load_cache(file_provider *provider)
{
lcb_string str;
char line[1024];
lcb_ssize_t nr;
int fail;
FILE *fp = NULL;
VBUCKET_CONFIG_HANDLE config = NULL;
char *end;
struct stat st;
int status = -1;
lcb_string_init(&str);
if (provider->filename == NULL) {
return -1;
}
fp = fopen(provider->filename, "r");
if (fp == NULL) {
LOG(provider, ERROR, "Couldn't open filename");
return -1;
}
if (fstat(fileno(fp), &st)) {
provider->last_errno = errno;
goto GT_DONE;
}
if (provider->last_mtime == st.st_mtime) {
LOG(provider, INFO, "Rejecting file. Modification time too old");
goto GT_DONE;
}
config = vbucket_config_create();
if (config == NULL) {
goto GT_DONE;
}
lcb_string_init(&str);
while ((nr = fread(line, 1, sizeof(line), fp)) > 0) {
if (lcb_string_append(&str, line, nr)) {
goto GT_DONE;
}
}
if (ferror(fp)) {
goto GT_DONE;
}
fclose(fp);
fp = NULL;
if (!str.nused) {
status = -1;
goto GT_DONE;
}
end = strstr(str.base, CONFIG_CACHE_MAGIC);
if (end == NULL) {
LOG(provider, ERROR, "Couldn't find magic in file");
remove(provider->filename);
status = -1;
goto GT_DONE;
}
fail = vbucket_config_parse(config, LIBVBUCKET_SOURCE_MEMORY, str.base);
if (fail) {
status = -1;
LOG(provider, ERROR, "Couldn't parse configuration");
remove(provider->filename);
goto GT_DONE;
}
if (vbucket_config_get_distribution_type(config) != VBUCKET_DISTRIBUTION_VBUCKET) {
status = -1;
LOG(provider, ERROR, "Not applying cached memcached config");
goto GT_DONE;
}
if (provider->config) {
lcb_clconfig_decref(provider->config);
}
provider->config = lcb_clconfig_create(config,
&str,
LCB_CLCONFIG_FILE);
provider->config->cmpclock = gethrtime();
provider->config->origin = provider->base.type;
provider->last_mtime = st.st_mtime;
status = 0;
config = NULL;
GT_DONE:
if (fp != NULL) {
fclose(fp);
}
if (config != NULL) {
vbucket_config_destroy(config);
}
lcb_string_release(&str);
return status;
}
void lcb_clconfig_write_file(clconfig_provider *provider_base, lcb_string *data)
{
FILE *fp;
file_provider *provider = (file_provider *)provider_base;
/** Get the provider */
if (provider->filename == NULL) {
return;
}
fp = fopen(provider->filename, "w");
if (fp) {
fprintf(fp, "%s%s", data->base, CONFIG_CACHE_MAGIC);
fclose(fp);
}
}
static clconfig_info * get_cached(clconfig_provider *pb)
{
file_provider *provider = (file_provider *)pb;
if (!provider->filename) {
return NULL;
}
return provider->config;
}
static void async_callback(lcb_timer_t timer,
lcb_t notused,
const void *cookie)
{
file_provider *provider = (file_provider *)cookie;
lcb_async_destroy(NULL, timer);
provider->async = NULL;
LOG(provider, TRACE, "Got async callback. Will load");
if (load_cache(provider) == 0) {
lcb_confmon_provider_success(&provider->base, provider->config);
} else {
lcb_confmon_provider_failed(&provider->base, LCB_ERROR);
}
(void)notused;
}
static lcb_error_t refresh_file(clconfig_provider *pb)
{
file_provider *provider = (file_provider *)pb;
lcb_error_t err;
if (provider->async) {
return LCB_SUCCESS;
}
LOG(provider, TRACE, "Starting async file load");
provider->async = lcb_async_create(pb->parent->settings->io,
pb,
async_callback,
&err);
lcb_assert(err == LCB_SUCCESS);
lcb_assert(provider->async != NULL);
return err;
}
static lcb_error_t pause_file(clconfig_provider *pb)
{
(void)pb;
return LCB_SUCCESS;
}
static void shutdown_file(clconfig_provider *pb)
{
file_provider *provider = (file_provider *)pb;
free(provider->filename);
if (provider->config) {
lcb_clconfig_decref(provider->config);
}
free(provider);
}
static void config_listener(clconfig_listener *lsn, clconfig_event_t event,
clconfig_info *info)
{
file_provider *provider;
if (event != CLCONFIG_EVENT_GOT_NEW_CONFIG) {
return;
}
provider = (file_provider *) (((char *)lsn) - offsetof(file_provider, listener));
LOG(provider, DEBUG, "Got updated configuration. Flushing to file");
if (info->origin == LCB_CLCONFIG_PHONY || info->origin == LCB_CLCONFIG_FILE) {
LOG(provider, DEBUG, "Rejecting configuration. Not valid");
return;
}
if (!info->raw.nused) {
return;
}
lcb_clconfig_write_file(&provider->base, &info->raw);
}
clconfig_provider * lcb_clconfig_create_file(lcb_confmon *parent)
{
file_provider *provider = calloc(1, sizeof(*provider));
if (!provider) {
return NULL;
}
provider->base.get_cached = get_cached;
provider->base.refresh = refresh_file;
provider->base.pause = pause_file;
provider->base.shutdown = shutdown_file;
provider->base.type = LCB_CLCONFIG_FILE;
provider->listener.callback = config_listener;
lcb_confmon_add_listener(parent, &provider->listener);
return &provider->base;
}
int lcb_clconfig_file_set_filename(clconfig_provider *p, const char *f)
{
file_provider *provider = (file_provider *)p;
lcb_assert(provider->base.type == LCB_CLCONFIG_FILE);
provider->base.enabled = 1;
if (provider->filename) {
free(provider->filename);
}
provider->filename = strdup(f);
return 0;
}