Skip to content

Commit b0fb155

Browse files
Balázs BarkóBalázs Barkó
authored andcommitted
stats: add StatsAggregator abstract class
it can be a stats cluster if we made some modification some of the stats cluster function change to virtual function the track_counter function responsible for, regist the aggregator stats counter the untrack_counter function responsible for,unregist the aggregator stats counter there is 2 reson why we aggregate before unregist 1. before unregisting the stats, we want to update (aggregate) the stats 2. it is possible, after the aggregation the stats isn't orphenaed (CPS!!!) Signed-off-by: Balázs Barkó <Balazs.Barko@oneidentity.com>
1 parent 68c0e57 commit b0fb155

File tree

6 files changed

+208
-1
lines changed

6 files changed

+208
-1
lines changed

lib/stats/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
add_subdirectory(aggregator)
2+
13
set(STATS_HEADERS
24
stats/stats.h
35
stats/stats-control.h
@@ -10,6 +12,7 @@ set(STATS_HEADERS
1012
stats/stats-query-commands.h
1113
stats/stats-cluster-logpipe.h
1214
stats/stats-cluster-single.h
15+
${STATS_AGGREGATOR_HEADERS}
1316
PARENT_SCOPE)
1417

1518
set(STATS_SOURCES
@@ -23,6 +26,7 @@ set(STATS_SOURCES
2326
stats/stats-query-commands.c
2427
stats/stats-cluster-logpipe.c
2528
stats/stats-cluster-single.c
29+
${STATS_AGGREGATOR_SOURCES}
2630
PARENT_SCOPE)
2731

2832
add_test_subdirectory(tests)

lib/stats/Makefile.am

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ statsincludedir = ${pkgincludedir}/stats
22

33
EXTRA_DIST += lib/stats/CMakeLists.txt
44

5+
include lib/stats/aggregator/Makefile.am
6+
57
statsinclude_HEADERS = \
68
lib/stats/stats.h \
79
lib/stats/stats-control.h \
@@ -25,6 +27,7 @@ stats_sources = \
2527
lib/stats/stats-query.c \
2628
lib/stats/stats-query-commands.c \
2729
lib/stats/stats-cluster-logpipe.c \
28-
lib/stats/stats-cluster-single.c
30+
lib/stats/stats-cluster-single.c \
31+
$(statsaggregator_sources)
2932

3033
include lib/stats/tests/Makefile.am
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
set(STATS_AGGREGATOR_HEADERS
2+
stats/aggregator/stats-aggregator.h
3+
PARENT_SCOPE)
4+
5+
set(STATS_AGGREGATOR_SOURCES
6+
stats/aggregator/stats-aggregator.c
7+
PARENT_SCOPE)

lib/stats/aggregator/Makefile.am

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
statsaggregatorincludedir = ${pkgincludedir}/stats/aggregator
2+
3+
EXTRA_DIST += lib/stats/aggregator/CMakeLists.txt
4+
5+
6+
statsaggregatorinclude_HEADERS = \
7+
lib/stats/aggregator/stats-aggregator.h
8+
9+
statsaggregator_sources = \
10+
lib/stats/aggregator/stats-aggregator.c
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*
2+
* Copyright (c) 2021 One Identity
3+
*
4+
* This library is free software; you can redistribute it and/or
5+
* modify it under the terms of the GNU Lesser General Public
6+
* License as published by the Free Software Foundation; either
7+
* version 2.1 of the License, or (at your option) any later version.
8+
*
9+
* This library is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+
* Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public
15+
* License along with this library; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17+
*
18+
* As an additional exemption you are allowed to compile & link against the
19+
* OpenSSL libraries as published by the OpenSSL project. See the file
20+
* COPYING for details.
21+
*
22+
*/
23+
24+
#include "stats/aggregator/stats-aggregator.h"
25+
26+
gboolean
27+
stats_aggregator_is_orphaned(StatsAggregator *self)
28+
{
29+
if (self && self->is_orphaned)
30+
return self->is_orphaned(self);
31+
32+
return TRUE;
33+
}
34+
35+
void
36+
stats_aggregator_track_counter(StatsAggregator *self)
37+
{
38+
if (!self)
39+
return;
40+
41+
if (stats_aggregator_is_orphaned(self) && self->register_aggr)
42+
self->register_aggr(self);
43+
44+
++self->use_count;
45+
}
46+
47+
void
48+
stats_aggregator_untrack_counter(StatsAggregator *self)
49+
{
50+
if (!self)
51+
return;
52+
53+
if (self->use_count > 0)
54+
--self->use_count;
55+
56+
if (self->use_count == 0)
57+
stats_aggregator_aggregate(self);
58+
59+
if (stats_aggregator_is_orphaned(self))
60+
stats_aggregator_unregister(self);
61+
}
62+
63+
64+
void
65+
stats_aggregator_insert_data(StatsAggregator *self, gsize value)
66+
{
67+
if (self && self->insert_data)
68+
self->insert_data(self, value);
69+
}
70+
71+
void
72+
stats_aggregator_aggregate(StatsAggregator *self)
73+
{
74+
if (self && self->aggregate)
75+
self->aggregate(self);
76+
}
77+
78+
void
79+
stats_aggregator_reset(StatsAggregator *self)
80+
{
81+
if (self && self->reset)
82+
self->reset(self);
83+
}
84+
85+
void
86+
stats_aggregator_free(StatsAggregator *self)
87+
{
88+
if (self && self->free)
89+
self->free(self);
90+
stats_cluster_key_cloned_free(&self->key);
91+
g_free(self);
92+
}
93+
94+
static gboolean
95+
_is_orphaned(StatsAggregator *self)
96+
{
97+
return self->use_count == 0;
98+
}
99+
100+
static void
101+
_set_virtual_functions(StatsAggregator *self)
102+
{
103+
self->is_orphaned = _is_orphaned;
104+
}
105+
106+
void
107+
stats_aggregator_init_instance(StatsAggregator *self, StatsClusterKey *sc_key, gint stats_level)
108+
{
109+
self->use_count = 0;
110+
self->stats_level = stats_level;
111+
stats_cluster_key_clone(&self->key, sc_key);
112+
_set_virtual_functions(self);
113+
}
114+
115+
void
116+
stats_aggregator_unregister(StatsAggregator *self)
117+
{
118+
if (self && self->unregister_aggr)
119+
self->unregister_aggr(self);
120+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright (c) 2021 One Identity
3+
*
4+
* This library is free software; you can redistribute it and/or
5+
* modify it under the terms of the GNU Lesser General Public
6+
* License as published by the Free Software Foundation; either
7+
* version 2.1 of the License, or (at your option) any later version.
8+
*
9+
* This library is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+
* Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public
15+
* License along with this library; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17+
*
18+
* As an additional exemption you are allowed to compile & link against the
19+
* OpenSSL libraries as published by the OpenSSL project. See the file
20+
* COPYING for details.
21+
*
22+
*/
23+
24+
#ifndef STATS_AGGREGATOR_H
25+
#define STATS_AGGREGATOR_H
26+
27+
#include "stats/stats-counter.h"
28+
#include "syslog-ng.h"
29+
#include "stats/stats-cluster.h"
30+
31+
typedef struct _StatsAggregator StatsAggregator;
32+
33+
struct _StatsAggregator
34+
{
35+
void (*insert_data)(StatsAggregator *self, gsize value);
36+
void (*aggregate)(StatsAggregator *self);
37+
void (*reset)(StatsAggregator *self);
38+
void (*free)(StatsAggregator *self);
39+
gboolean (*is_orphaned)(StatsAggregator *self);
40+
41+
void (*register_aggr)(StatsAggregator *self);
42+
void (*unregister_aggr)(StatsAggregator *self);
43+
44+
gssize use_count;
45+
StatsClusterKey key;
46+
gint stats_level;
47+
};
48+
49+
/* public */
50+
void stats_aggregator_insert_data(StatsAggregator *self, gsize value);
51+
void stats_aggregator_aggregate(StatsAggregator *self);
52+
void stats_aggregator_reset(StatsAggregator *self);
53+
54+
/* stats-internals */
55+
gboolean stats_aggregator_is_orphaned(StatsAggregator *self);
56+
void stats_aggregator_track_counter(StatsAggregator *self);
57+
void stats_aggregator_untrack_counter(StatsAggregator *self);
58+
void stats_aggregator_free(StatsAggregator *self);
59+
void stats_aggregator_init_instance(StatsAggregator *self, StatsClusterKey *sc_key, gint stats_level);
60+
void stats_aggregator_unregister(StatsAggregator *self);
61+
62+
63+
#endif /* STATS_AGGREGATOR_H */

0 commit comments

Comments
 (0)