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
|
/*
* Copyright (c) 2002-2014 Balabit
* Copyright (c) 2014 Laszlo Budai
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; 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 "ack_tracker.h"
#include "bookmark.h"
#include "ringbuffer.h"
#include "syslog-ng.h"
typedef struct _LateAckRecord
{
AckRecord super;
gboolean acked;
Bookmark bookmark;
} LateAckRecord;
typedef struct LateAckTracker
{
AckTracker super;
LateAckRecord *pending_ack_record;
RingBuffer ack_record_storage;
GStaticMutex storage_mutex;
} LateAckTracker;
static inline void
late_ack_record_destroy(LateAckRecord *self)
{
if (self->bookmark.destroy)
self->bookmark.destroy(&(self->bookmark));
}
static inline void
_late_tracker_lock(LateAckTracker *self)
{
g_static_mutex_lock(&self->storage_mutex);
}
static inline void
_late_tracker_unlock(LateAckTracker *self)
{
g_static_mutex_unlock(&self->storage_mutex);
}
static inline gboolean
_ack_range_is_continuous(void *data)
{
LateAckRecord *ack_rec = (LateAckRecord *)data;
return ack_rec->acked;
}
static inline guint32
_get_continuous_range_length(LateAckTracker *self)
{
return ring_buffer_get_continual_range_length(&self->ack_record_storage, _ack_range_is_continuous);
}
static inline void
_drop_range(LateAckTracker *self, guint32 n)
{
int i;
LateAckRecord *ack_rec;
for (i = 0; i < n; i++)
{
ack_rec = ring_buffer_element_at(&self->ack_record_storage, i);
ack_rec->acked = FALSE;
late_ack_record_destroy(ack_rec);
ack_rec->bookmark.save = NULL;
ack_rec->bookmark.destroy = NULL;
}
ring_buffer_drop(&self->ack_record_storage, n);
}
static void
late_ack_tracker_track_msg(AckTracker *s, LogMessage *msg)
{
LateAckTracker *self = (LateAckTracker *)s;
LogSource *source = self->super.source;
g_assert(self->pending_ack_record != NULL);
log_pipe_ref((LogPipe *)source);
msg->ack_record = (AckRecord *)self->pending_ack_record;
_late_tracker_lock(self);
{
LateAckRecord *ack_rec;
ack_rec = (LateAckRecord *)ring_buffer_push(&self->ack_record_storage);
g_assert(ack_rec == self->pending_ack_record);
}
_late_tracker_unlock(self);
self->pending_ack_record = NULL;
}
static void
late_ack_tracker_manage_msg_ack(AckTracker *s, LogMessage *msg, AckType ack_type)
{
LateAckTracker *self = (LateAckTracker *)s;
LateAckRecord *ack_rec = (LateAckRecord *)msg->ack_record;
LateAckRecord *last_in_range = NULL;
guint32 ack_range_length = 0;
ack_rec->acked = TRUE;
_late_tracker_lock(self);
{
ack_range_length = _get_continuous_range_length(self);
if (ack_range_length > 0)
{
last_in_range = ring_buffer_element_at(&self->ack_record_storage, ack_range_length - 1);
if (ack_type != AT_ABORTED)
{
Bookmark *bookmark = &(last_in_range->bookmark);
bookmark->save(bookmark);
}
_drop_range(self, ack_range_length);
if (ack_type == AT_SUSPENDED)
log_source_flow_control_suspend(self->super.source);
else
log_source_flow_control_adjust(self->super.source, ack_range_length);
}
}
_late_tracker_unlock(self);
log_msg_unref(msg);
log_pipe_unref((LogPipe *)self->super.source);
}
static Bookmark *
late_ack_tracker_request_bookmark(AckTracker *s)
{
LateAckTracker *self = (LateAckTracker *)s;
_late_tracker_lock(self);
{
self->pending_ack_record = ring_buffer_tail(&self->ack_record_storage);
}
_late_tracker_unlock(self);
if (self->pending_ack_record)
{
self->pending_ack_record->bookmark.persist_state = s->source->super.cfg->state;
self->pending_ack_record->super.tracker = (AckTracker *)self;
return &(self->pending_ack_record->bookmark);
}
return NULL;
}
static void
late_ack_tracker_init_instance(LateAckTracker *self, LogSource *source)
{
self->super.late = TRUE;
self->super.source = source;
source->ack_tracker = (AckTracker *)self;
self->super.request_bookmark = late_ack_tracker_request_bookmark;
self->super.track_msg = late_ack_tracker_track_msg;
self->super.manage_msg_ack = late_ack_tracker_manage_msg_ack;
ring_buffer_alloc(&self->ack_record_storage, sizeof(LateAckRecord), log_source_get_init_window_size(source));
g_static_mutex_init(&self->storage_mutex);
}
AckTracker*
late_ack_tracker_new(LogSource *source)
{
LateAckTracker *self = g_new0(LateAckTracker, 1);
late_ack_tracker_init_instance(self, source);
return (AckTracker *)self;
}
void
late_ack_tracker_free(AckTracker *s)
{
LateAckTracker *self = (LateAckTracker *)s;
guint32 count = ring_buffer_count(&self->ack_record_storage);
g_static_mutex_free(&self->storage_mutex);
_drop_range(self, count);
ring_buffer_free(&self->ack_record_storage);
g_free(self);
}
|