Skip to content

Commit

Permalink
aggregate-tally: Propagate errors while iterating
Browse files Browse the repository at this point in the history
Previously emer_aggregate_tally_iter() and
emer_aggregate_tally_iter_before() had no return value and would simply
call g_warning() in the error case. This makes it hard to test the error
paths without aborting the test program.

Make each function return an error and move the g_warning() out into the
calling functions.

https://phabricator.endlessm.com/T35331
  • Loading branch information
wjt committed Apr 19, 2024
1 parent a45a4b3 commit d982bd5
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 109 deletions.
70 changes: 36 additions & 34 deletions daemon/emer-aggregate-tally.c
Original file line number Diff line number Diff line change
Expand Up @@ -659,18 +659,23 @@ emer_aggregate_tally_iter_internal (EmerAggregateTally *self,
* @flags: Whether to delete events while iterating
* @func: (scope call): Callback to call for each matching event
* @user_data: (closure func): Data to pass to @func
* @error: Location to store an error if one occurs while iterating or
* deleting events.
*
* Iterate all events in the tally for the given frequency, either on a
* specific date/month or (if @datetime is %NULL) on all dates at that
* frequency.
*
* Returns: %TRUE on success, or %FALSE with @error set otherwise.
*/
void
emer_aggregate_tally_iter (EmerAggregateTally *self,
EmerTallyType tally_type,
GDateTime *datetime,
EmerTallyIterFlags flags,
EmerTallyIterFunc func,
gpointer user_data)
gboolean
emer_aggregate_tally_iter (EmerAggregateTally *self,
EmerTallyType tally_type,
GDateTime *datetime,
EmerTallyIterFlags flags,
EmerTallyIterFunc func,
gpointer user_data,
GError **error)
{
const char *SELECT_SQL =
"SELECT id, event_id, unix_user_id, "
Expand All @@ -683,43 +688,40 @@ emer_aggregate_tally_iter (EmerAggregateTally *self,
"FROM tally "
"WHERE length(date) = ?";
const char *query = (datetime != NULL) ? SELECT_SQL : SELECT_ALL_SQL;
g_autoptr(GError) error = NULL;

if (!emer_aggregate_tally_iter_internal (self,
query,
tally_type,
datetime,
flags,
func,
user_data,
&error))
g_critical ("%s: %s", G_STRFUNC, error->message);
return emer_aggregate_tally_iter_internal (self,
query,
tally_type,
datetime,
flags,
func,
user_data,
error);
}

void
emer_aggregate_tally_iter_before (EmerAggregateTally *self,
EmerTallyType tally_type,
GDateTime *datetime,
EmerTallyIterFlags flags,
EmerTallyIterFunc func,
gpointer user_data)
gboolean
emer_aggregate_tally_iter_before (EmerAggregateTally *self,
EmerTallyType tally_type,
GDateTime *datetime,
EmerTallyIterFlags flags,
EmerTallyIterFunc func,
gpointer user_data,
GError **error)
{
const char *SELECT_SQL =
"SELECT id, event_id, unix_user_id, "
" payload, counter, date "
"FROM tally "
"WHERE length(date) = length(?1) AND date < ?1;";
g_autoptr(GError) error = NULL;

if (!emer_aggregate_tally_iter_internal (self,
SELECT_SQL,
tally_type,
datetime,
flags,
func,
user_data,
&error))
g_critical ("%s: %s", G_STRFUNC, error->message);
return emer_aggregate_tally_iter_internal (self,
SELECT_SQL,
tally_type,
datetime,
flags,
func,
user_data,
error);
}

gboolean
Expand Down
28 changes: 15 additions & 13 deletions daemon/emer-aggregate-tally.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,21 @@ gboolean emer_aggregate_tally_store_event (EmerAggregateTally *self,
GDateTime *datetime,
GError **error);

void emer_aggregate_tally_iter (EmerAggregateTally *self,
EmerTallyType tally_type,
GDateTime *datetime,
EmerTallyIterFlags flags,
EmerTallyIterFunc func,
gpointer user_data);

void emer_aggregate_tally_iter_before (EmerAggregateTally *self,
EmerTallyType tally_type,
GDateTime *datetime,
EmerTallyIterFlags flags,
EmerTallyIterFunc func,
gpointer user_data);
gboolean emer_aggregate_tally_iter (EmerAggregateTally *self,
EmerTallyType tally_type,
GDateTime *datetime,
EmerTallyIterFlags flags,
EmerTallyIterFunc func,
gpointer user_data,
GError **error);

gboolean emer_aggregate_tally_iter_before (EmerAggregateTally *self,
EmerTallyType tally_type,
GDateTime *datetime,
EmerTallyIterFlags flags,
EmerTallyIterFunc func,
gpointer user_data,
GError **error);

gboolean emer_aggregate_tally_clear (EmerAggregateTally *self,
GError **error);
Expand Down
71 changes: 46 additions & 25 deletions daemon/emer-daemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -1152,6 +1152,7 @@ clock_ticked_midnight_cb (gpointer user_data)
g_autoptr (GDateTime) now = NULL;
g_autofree char *date = NULL;
gint64 now_monotonic_us;
g_autoptr(GError) error = NULL;

now_monotonic_us = g_get_monotonic_time ();

Expand All @@ -1167,12 +1168,17 @@ clock_ticked_midnight_cb (gpointer user_data)
self->current_aggregate_tally_date,
now_monotonic_us);

emer_aggregate_tally_iter (self->aggregate_tally,
EMER_TALLY_DAILY_EVENTS,
self->current_aggregate_tally_date,
EMER_TALLY_ITER_FLAG_DELETE,
buffer_aggregate_event_to_queue,
self);
if (!emer_aggregate_tally_iter (self->aggregate_tally,
EMER_TALLY_DAILY_EVENTS,
self->current_aggregate_tally_date,
EMER_TALLY_ITER_FLAG_DELETE,
buffer_aggregate_event_to_queue,
self,
&error))
{
g_warning ("Failed to buffer daily events for %s: %s", date, error->message);
g_clear_error (&error);
}

/* Monthly events */
now = g_date_time_new_now_local ();
Expand All @@ -1191,12 +1197,17 @@ clock_ticked_midnight_cb (gpointer user_data)
self->current_aggregate_tally_date,
now_monotonic_us);

emer_aggregate_tally_iter (self->aggregate_tally,
EMER_TALLY_MONTHLY_EVENTS,
self->current_aggregate_tally_date,
EMER_TALLY_ITER_FLAG_DELETE,
buffer_aggregate_event_to_queue,
self);
if (!emer_aggregate_tally_iter (self->aggregate_tally,
EMER_TALLY_MONTHLY_EVENTS,
self->current_aggregate_tally_date,
EMER_TALLY_ITER_FLAG_DELETE,
buffer_aggregate_event_to_queue,
self,
&error))
{
g_warning ("Failed to buffer monthly events for %s: %s", month_date, error->message);
g_clear_error (&error);
}
}

split_aggregate_timers (self, now_monotonic_us);
Expand Down Expand Up @@ -1318,21 +1329,31 @@ static void
buffer_past_aggregate_events (EmerDaemon *self)
{
g_autoptr(GDateTime) now = g_date_time_new_now_local ();
g_autoptr(GError) error = NULL;

emer_aggregate_tally_iter_before (self->aggregate_tally,
EMER_TALLY_DAILY_EVENTS,
now,
EMER_TALLY_ITER_FLAG_DELETE,
buffer_aggregate_event_to_queue,
self);

emer_aggregate_tally_iter_before (self->aggregate_tally,
EMER_TALLY_MONTHLY_EVENTS,
now,
EMER_TALLY_ITER_FLAG_DELETE,
buffer_aggregate_event_to_queue,
self);
if (!emer_aggregate_tally_iter_before (self->aggregate_tally,
EMER_TALLY_DAILY_EVENTS,
now,
EMER_TALLY_ITER_FLAG_DELETE,
buffer_aggregate_event_to_queue,
self,
&error))
{
g_warning ("Failed to buffer past daily events: %s", error->message);
g_clear_error (&error);
}

if (!emer_aggregate_tally_iter_before (self->aggregate_tally,
EMER_TALLY_MONTHLY_EVENTS,
now,
EMER_TALLY_ITER_FLAG_DELETE,
buffer_aggregate_event_to_queue,
self,
&error))
{
g_warning ("Failed to buffer past monthly events: %s", error->message);
g_clear_error (&error);
}
}

static void
Expand Down
Loading

0 comments on commit d982bd5

Please sign in to comment.