File: diskq.c

package info (click to toggle)
syslog-ng 4.8.1-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,456 kB
  • sloc: ansic: 177,631; python: 13,035; cpp: 11,611; makefile: 7,012; sh: 5,147; java: 3,651; xml: 3,344; yacc: 1,377; lex: 599; perl: 193; awk: 190; objc: 162
file content (335 lines) | stat: -rw-r--r-- 11,618 bytes parent folder | download | duplicates (2)
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*
 * Copyright (c) 2002-2016 Balabit
 * Copyright (c) 2016 Viktor Juhasz <viktor.juhasz@balabit.com>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published
 * by the Free Software Foundation, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * As an additional exemption you are allowed to compile & link against the
 * OpenSSL libraries as published by the OpenSSL project. See the file
 * COPYING for details.
 *
 */

#include <math.h>

#include "diskq.h"
#include "diskq-config.h"
#include "diskq-global-metrics.h"

#include "driver.h"
#include "messages.h"
#include "logqueue-disk.h"
#include "logqueue-disk-reliable.h"
#include "logqueue-disk-non-reliable.h"
#include "persist-state.h"

#define DISKQ_PLUGIN_NAME "diskq"

struct _DiskQDestPlugin
{
  LogDriverPlugin super;
  DiskQueueOptions options;
};

static gboolean
log_queue_disk_is_file_in_directory(const gchar *file, const gchar *directory)
{
  gchar *basedir = g_path_get_dirname(file);
  const gboolean result = (0 == strcmp(basedir, directory));

  g_free(basedir);
  return result;
}

static LogQueue *
_create_disk_queue(DiskQDestPlugin *self, const gchar *filename, const gchar *persist_name, gint stats_level,
                   StatsClusterKeyBuilder *driver_sck_builder, StatsClusterKeyBuilder *queue_sck_builder)
{
  if (self->options.reliable)
    return log_queue_disk_reliable_new(&self->options, filename, persist_name, stats_level, driver_sck_builder,
                                       queue_sck_builder);

  return log_queue_disk_non_reliable_new(&self->options, filename, persist_name, stats_level, driver_sck_builder,
                                         queue_sck_builder);
}

static void
_warn_if_dir_changed(const gchar *qfile_name, const gchar *dir)
{
  if (!log_queue_disk_is_file_in_directory(qfile_name, dir))
    {
      msg_warning("The disk buffer directory has changed in the configuration, but the disk queue file cannot be moved",
                  evt_tag_str("qfile", qfile_name),
                  evt_tag_str("dir", dir));
    }
}

static LogQueue *
_create_and_start_disk_queue_with_filename_from_persist(DiskQDestPlugin *self, const gchar *persist_qfile_name,
                                                        const gchar *persist_name, gint stats_level,
                                                        StatsClusterKeyBuilder *driver_sck_builder,
                                                        StatsClusterKeyBuilder *queue_sck_builder)
{
  if (!persist_qfile_name)
    return FALSE;

  _warn_if_dir_changed(persist_qfile_name, self->options.dir);

  LogQueue *queue = _create_disk_queue(self, persist_qfile_name, persist_name, stats_level, driver_sck_builder,
                                       queue_sck_builder);
  if (log_queue_disk_start(queue))
    return queue;

  log_queue_unref(queue);

  gchar *new_qfile_name = qdisk_get_next_filename(self->options.dir, self->options.reliable);
  if (!new_qfile_name)
    return NULL;

  queue = _create_disk_queue(self, persist_qfile_name, persist_name, stats_level, driver_sck_builder, queue_sck_builder);
  if (log_queue_disk_start(queue))
    {
      msg_error("Error opening disk-queue file, a new one started",
                evt_tag_str("old_filename", persist_qfile_name),
                evt_tag_str("new_filename", log_queue_disk_get_filename(queue)));
      g_free(new_qfile_name);
      return queue;
    }

  msg_error("Error initializing log queue");

  log_queue_unref(queue);
  g_free(new_qfile_name);
  return NULL;
}

static LogQueue *
_create_and_start_disk_queue_with_new_filename(DiskQDestPlugin *self, const gchar *new_qfile_name,
                                               const gchar *persist_name, gint stats_level,
                                               StatsClusterKeyBuilder *driver_sck_builder,
                                               StatsClusterKeyBuilder *queue_sck_builder)
{
  if (!new_qfile_name)
    return NULL;

  LogQueue *queue = _create_disk_queue(self, new_qfile_name, persist_name, stats_level, driver_sck_builder,
                                       queue_sck_builder);
  if (log_queue_disk_start(queue))
    return queue;

  msg_error("Error initializing log queue");

  log_queue_unref(queue);
  return NULL;
}

static LogQueue *
_acquire_queue(LogDestDriver *dd, const gchar *persist_name, gint stats_level,
               StatsClusterKeyBuilder *driver_sck_builder, StatsClusterKeyBuilder *queue_sck_builder)
{
  DiskQDestPlugin *self = log_driver_get_plugin(&dd->super, DiskQDestPlugin, DISKQ_PLUGIN_NAME);
  GlobalConfig *cfg = log_pipe_get_config(&dd->super.super);
  LogQueue *queue;
  gchar *persist_qfile_name, *new_qfile_name = NULL;

  if (persist_name)
    log_queue_unref(cfg_persist_config_fetch(cfg, persist_name));

  persist_qfile_name = persist_state_lookup_string(cfg->state, persist_name, NULL, NULL);
  queue = _create_and_start_disk_queue_with_filename_from_persist(
            self, persist_qfile_name, persist_name, stats_level, driver_sck_builder, queue_sck_builder);
  if (queue)
    goto exit;

  new_qfile_name = qdisk_get_next_filename(self->options.dir, self->options.reliable);
  queue = _create_and_start_disk_queue_with_new_filename(self, new_qfile_name, persist_name, stats_level,
                                                         driver_sck_builder, queue_sck_builder);

exit:
  if (queue)
    {
      log_queue_set_throttle(queue, dd->throttle);

      const gchar *qfile_name = log_queue_disk_get_filename(queue);
      diskq_global_metrics_file_acquired(qfile_name);
      if (persist_name && qfile_name)
        persist_state_alloc_string(cfg->state, persist_name, qfile_name, -1);
    }

  g_free(persist_qfile_name);
  g_free(new_qfile_name);

  return queue;
}

void
_release_queue(LogDestDriver *dd, LogQueue *queue)
{
  GlobalConfig *cfg = log_pipe_get_config(&dd->super.super);
  gboolean persistent;

  log_queue_disk_stop(queue, &persistent);
  diskq_global_metrics_file_released(log_queue_disk_get_filename(queue));

  if (queue->persist_name)
    {
      cfg_persist_config_add(cfg, queue->persist_name, queue, (GDestroyNotify) log_queue_unref);
    }
  else
    {
      log_queue_unref(queue);
    }
}

static gboolean
_is_truncate_size_ratio_set_explicitly(DiskQDestPlugin *self, LogDestDriver *dd)
{
  GlobalConfig *cfg = log_pipe_get_config(&dd->super.super);
  return fabs(self->options.truncate_size_ratio - (-1)) >= DBL_EPSILON
         || disk_queue_config_is_truncate_size_ratio_set_explicitly(cfg);
}

static gboolean
_is_prealloc_set_explicitly(DiskQDestPlugin *self, LogDestDriver *dd)
{
  GlobalConfig *cfg = log_pipe_get_config(&dd->super.super);
  return self->options.prealloc != -1 || disk_queue_config_is_prealloc_set_explicitly(cfg);
}

static gboolean
_set_truncate_size_ratio_and_prealloc(DiskQDestPlugin *self, LogDestDriver *dd)
{
  GlobalConfig *cfg = log_pipe_get_config(&dd->super.super);

  gdouble truncate_size_ratio = self->options.truncate_size_ratio;
  if (truncate_size_ratio < 0)
    {
      if (cfg_is_config_version_older(cfg, VERSION_VALUE_3_33))
        {
          msg_warning_once("WARNING: the truncation of the disk-buffer files is changed starting with "
                           VERSION_3_33 ". You are using an older config version and your config does not "
                           "set the truncate-size-ratio() option. We will not use the new truncating logic "
                           "with this config for compatibility.");
          /* Handle it as truncate-size-ratio(0) was set explicitly by the user. */
          disk_queue_options_set_truncate_size_ratio(&self->options, 0);
          truncate_size_ratio = 0;
        }
      else
        {
          truncate_size_ratio = disk_queue_config_get_truncate_size_ratio(cfg);
        }
    }

  gboolean prealloc = self->options.prealloc;
  if (prealloc < 0)
    {
      prealloc = disk_queue_config_get_prealloc(cfg);
    }

  if (!(prealloc && truncate_size_ratio < 1))
    {
      self->options.truncate_size_ratio = truncate_size_ratio;
      self->options.prealloc = prealloc;
      return TRUE;
    }

  if (_is_truncate_size_ratio_set_explicitly(self, dd) && _is_prealloc_set_explicitly(self, dd))
    {
      msg_error("Cannot enable preallocation and truncation at the same time. "
                "Please disable either the truncation (truncate-size-ratio(1)) or the preallocation (prealloc(no))",
                log_pipe_location_tag(&dd->super.super));
      return FALSE;
    }

  if (_is_truncate_size_ratio_set_explicitly(self, dd))
    {
      msg_warning("Cannot enable preallocation and truncation at the same time. "
                  "Preallocation disabled",
                  log_pipe_location_tag(&dd->super.super));
      self->options.truncate_size_ratio = truncate_size_ratio;
      self->options.prealloc = FALSE;
      return TRUE;
    }

  if (_is_prealloc_set_explicitly(self, dd))
    {
      msg_warning("Cannot enable preallocation and truncation at the same time. "
                  "Truncation disabled",
                  log_pipe_location_tag(&dd->super.super));
      self->options.truncate_size_ratio = 1;
      self->options.prealloc = prealloc;
      return TRUE;
    }

  /*
   * This means that our hard coded default values enabled preallocation and truncation at the same time,
   * which is a coding error.
   */
  g_assert_not_reached();
}

static gboolean
_attach(LogDriverPlugin *s, LogDriver *d)
{
  DiskQDestPlugin *self = (DiskQDestPlugin *) s;
  LogDestDriver *dd = (LogDestDriver *) d;
  GlobalConfig *cfg = log_pipe_get_config(&d->super);

  if (self->options.capacity_bytes < MIN_CAPACITY_BYTES && self->options.capacity_bytes > 0)
    {
      msg_warning("The value of 'capacity_bytes()' is too low, setting to the smallest acceptable value",
                  evt_tag_long("min_space", MIN_CAPACITY_BYTES),
                  log_pipe_location_tag(&dd->super.super));
      self->options.capacity_bytes = MIN_CAPACITY_BYTES;
    }

  if (self->options.flow_control_window_size < 0)
    self->options.flow_control_window_size = dd->log_fifo_size;
  if (self->options.flow_control_window_size < 0)
    self->options.flow_control_window_size = cfg->log_fifo_size;
  if (self->options.front_cache_size < 0)
    self->options.front_cache_size = 1000;

  if (!_set_truncate_size_ratio_and_prealloc(self, dd))
    return FALSE;

  dd->acquire_queue = _acquire_queue;
  dd->release_queue = _release_queue;
  return TRUE;
}

static void
_free(LogDriverPlugin *s)
{
  DiskQDestPlugin *self = (DiskQDestPlugin *)s;
  disk_queue_options_destroy(&self->options);
  log_driver_plugin_free_method(s);
}

DiskQueueOptions *diskq_get_options(DiskQDestPlugin *self)
{
  return &self->options;
}

DiskQDestPlugin *
diskq_dest_plugin_new(void)
{
  DiskQDestPlugin *self = g_new0(DiskQDestPlugin, 1);

  log_driver_plugin_init_instance(&self->super, DISKQ_PLUGIN_NAME);
  disk_queue_options_set_default_options(&self->options);
  self->super.attach = _attach;
  self->super.free_fn = _free;
  return self;
}