autoclean: save stats on how much we cleaned.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell
2022-09-19 10:19:52 +09:30
committed by Christian Decker
parent 17858c9490
commit 7da51892e8
2 changed files with 33 additions and 11 deletions

View File

@@ -38,6 +38,7 @@ static bool json_to_subsystem(const char *buffer, const jsmntok_t *tok,
static u64 deprecated_cycle_seconds = UINT64_MAX; static u64 deprecated_cycle_seconds = UINT64_MAX;
static u64 cycle_seconds = 3600; static u64 cycle_seconds = 3600;
static u64 subsystem_age[NUM_SUBSYSTEM]; static u64 subsystem_age[NUM_SUBSYSTEM];
static u64 num_cleaned[NUM_SUBSYSTEM];
static size_t cleanup_reqs_remaining; static size_t cleanup_reqs_remaining;
static struct plugin *plugin; static struct plugin *plugin;
static struct plugin_timer *cleantimer; static struct plugin_timer *cleantimer;
@@ -70,17 +71,33 @@ static struct command_result *set_next_timer(struct plugin *plugin)
return timer_complete(plugin); return timer_complete(plugin);
} }
static struct command_result *clean_finished_one(struct command *cmd)
{
assert(cleanup_reqs_remaining != 0);
if (--cleanup_reqs_remaining > 0)
return command_still_pending(cmd);
for (enum subsystem i = 0; i < NUM_SUBSYSTEM; i++) {
if (num_cleaned[i] == 0)
continue;
jsonrpc_set_datastore_string(plugin, cmd,
datastore_path(tmpctx, i, "num"),
tal_fmt(tmpctx, "%"PRIu64, num_cleaned[i]),
"create-or-replace", NULL, NULL, NULL);
}
return set_next_timer(plugin);
}
static struct command_result *del_done(struct command *cmd, static struct command_result *del_done(struct command *cmd,
const char *buf, const char *buf,
const jsmntok_t *result, const jsmntok_t *result,
ptrint_t *unused) ptrint_t *unused)
{ {
assert(cleanup_reqs_remaining != 0); num_cleaned[EXPIREDINVOICES]++;
plugin_log(plugin, LOG_DBG, "delinvoice_done: %zu remaining", return clean_finished_one(cmd);
cleanup_reqs_remaining-1);
if (--cleanup_reqs_remaining == 0)
return set_next_timer(plugin);
return command_still_pending(cmd);
} }
static struct command_result *del_failed(struct command *cmd, static struct command_result *del_failed(struct command *cmd,
@@ -92,10 +109,7 @@ static struct command_result *del_failed(struct command *cmd,
subsystem_to_str(ptr2int(subsystemp)), subsystem_to_str(ptr2int(subsystemp)),
json_tok_full_len(result), json_tok_full_len(result),
json_tok_full(buf, result)); json_tok_full(buf, result));
assert(cleanup_reqs_remaining != 0); return clean_finished_one(cmd);
if (--cleanup_reqs_remaining == 0)
return command_still_pending(cmd);
return set_next_timer(plugin);
} }
static struct command_result *listinvoices_done(struct command *cmd, static struct command_result *listinvoices_done(struct command *cmd,
@@ -239,7 +253,7 @@ static struct command_result *json_success_subsystems(struct command *cmd,
json_add_bool(response, "enabled", subsystem_age[i] != 0); json_add_bool(response, "enabled", subsystem_age[i] != 0);
if (subsystem_age[i] != 0) if (subsystem_age[i] != 0)
json_add_u64(response, "age", subsystem_age[i]); json_add_u64(response, "age", subsystem_age[i]);
/* FIXME: Add stats! */ json_add_u64(response, "cleaned", num_cleaned[i]);
json_object_end(response); json_object_end(response);
} }
json_object_end(response); json_object_end(response);
@@ -311,6 +325,10 @@ static const char *init(struct plugin *p,
cleantimer = plugin_timer(p, time_from_sec(cycle_seconds), do_clean, p); cleantimer = plugin_timer(p, time_from_sec(cycle_seconds), do_clean, p);
for (enum subsystem i = 0; i < NUM_SUBSYSTEM; i++) {
rpc_scan_datastore_str(plugin, datastore_path(tmpctx, i, "num"),
JSON_SCAN(json_to_u64, &num_cleaned[i]));
}
return NULL; return NULL;
} }

View File

@@ -2946,6 +2946,7 @@ def test_autoclean(node_factory):
assert l1.rpc.autoclean_status()['autoclean']['expiredinvoices']['age'] == 2 assert l1.rpc.autoclean_status()['autoclean']['expiredinvoices']['age'] == 2
# Both should still be there. # Both should still be there.
assert l1.rpc.autoclean_status()['autoclean']['expiredinvoices']['cleaned'] == 0
assert len(l1.rpc.listinvoices('inv1')['invoices']) == 1 assert len(l1.rpc.listinvoices('inv1')['invoices']) == 1
assert len(l1.rpc.listinvoices('inv2')['invoices']) == 1 assert len(l1.rpc.listinvoices('inv2')['invoices']) == 1
assert l1.rpc.listinvoices('inv1')['invoices'][0]['description'] == 'description1' assert l1.rpc.listinvoices('inv1')['invoices'][0]['description'] == 'description1'
@@ -2954,11 +2955,13 @@ def test_autoclean(node_factory):
wait_for(lambda: only_one(l1.rpc.listinvoices('inv1')['invoices'])['status'] == 'expired') wait_for(lambda: only_one(l1.rpc.listinvoices('inv1')['invoices'])['status'] == 'expired')
# Now will get autocleaned # Now will get autocleaned
wait_for(lambda: l1.rpc.listinvoices('inv1')['invoices'] == []) wait_for(lambda: l1.rpc.listinvoices('inv1')['invoices'] == [])
assert l1.rpc.autoclean_status()['autoclean']['expiredinvoices']['cleaned'] == 1
# Keeps settings across restarts. # Keeps settings across restarts.
l1.restart() l1.restart()
assert l1.rpc.autoclean_status()['autoclean']['expiredinvoices']['enabled'] is True assert l1.rpc.autoclean_status()['autoclean']['expiredinvoices']['enabled'] is True
assert l1.rpc.autoclean_status()['autoclean']['expiredinvoices']['age'] == 2 assert l1.rpc.autoclean_status()['autoclean']['expiredinvoices']['age'] == 2
assert l1.rpc.autoclean_status()['autoclean']['expiredinvoices']['cleaned'] == 1
# Disabling works # Disabling works
l1.rpc.autoclean(subsystem='expiredinvoices', age='never') l1.rpc.autoclean(subsystem='expiredinvoices', age='never')
@@ -2981,6 +2984,7 @@ def test_autoclean(node_factory):
# Now enable: they will get autocleaned # Now enable: they will get autocleaned
l1.rpc.autoclean(subsystem='expiredinvoices', age=2) l1.rpc.autoclean(subsystem='expiredinvoices', age=2)
wait_for(lambda: l1.rpc.listinvoices()['invoices'] == []) wait_for(lambda: l1.rpc.listinvoices()['invoices'] == [])
assert l1.rpc.autoclean_status()['autoclean']['expiredinvoices']['cleaned'] == 3
def test_block_added_notifications(node_factory, bitcoind): def test_block_added_notifications(node_factory, bitcoind):