libmsc/db.c: get rid of hard-coded SMS expiry threshold

The initial idea of the SMS expiry threshold was to avoid storing
SMS messages with too long validity time (e.g. 63 weeks).
Unfortunately, neither this feature was properly documented, nor
the expiry threshold is configurable. Moreover, it has been
implemented in a wrong way, so instead of deleting the oldest
expired message, it would delete the youngest one or nothing:

  SELECT ... FROM SMS ORDER BY created LIMIT 1;

while it should be sorted by 'valid_until' in ascending order:

  SELECT .. FROM SMS ORDER BY valid_until LIMIT 1;

Thus, if the oldest message is expired, it gets deleted. If the
oldest message is not expired yet, there is nothing to delete.

Change-Id: I0ce6b1ab50986dc69a2be4ea62b6a24c7f3f8f0a
This commit is contained in:
Vadim Yanitskiy 2019-04-14 16:39:12 +07:00
parent 0d13e8358e
commit 570b4c8be2
2 changed files with 12 additions and 19 deletions

View File

@ -1021,20 +1021,15 @@ int db_sms_delete_sent_message_by_id(unsigned long long sms_id)
}
static int delete_expired_sms(unsigned long long sms_id, time_t created, time_t validity_timestamp)
static int delete_expired_sms(unsigned long long sms_id, time_t validity_timestamp)
{
dbi_result result;
time_t now, min_created;
time_t now;
now = time(NULL);
if (validity_timestamp > now)
return -1;
/* Our SMS expiry threshold is hard-coded to roughly 2 weeks at the moment. */
min_created = now - (time_t)(60 * 60 * 24 * 7 * 2);
if (min_created < 0) /* bogus system clock? */
return -1;
if (created >= min_created) /* not yet expired */
/* Net yet expired */
if (validity_timestamp > now)
return -1;
result = dbi_conn_queryf(conn, "DELETE FROM SMS WHERE id = %llu", sms_id);
@ -1049,9 +1044,9 @@ static int delete_expired_sms(unsigned long long sms_id, time_t created, time_t
int db_sms_delete_expired_message_by_id(unsigned long long sms_id)
{
dbi_result result;
time_t created, validity_timestamp;
time_t validity_timestamp;
result = dbi_conn_queryf(conn, "SELECT created,valid_until FROM SMS WHERE id = %llu", sms_id);
result = dbi_conn_queryf(conn, "SELECT valid_until FROM SMS WHERE id = %llu", sms_id);
if (!result)
return -1;
if (!next_row(result)) {
@ -1059,29 +1054,28 @@ int db_sms_delete_expired_message_by_id(unsigned long long sms_id)
return -1;
}
created = dbi_result_get_datetime(result, "created");
validity_timestamp = dbi_result_get_datetime(result, "valid_until");
dbi_result_free(result);
return delete_expired_sms(sms_id, created, validity_timestamp);
return delete_expired_sms(sms_id, validity_timestamp);
}
void db_sms_delete_oldest_expired_message(void)
{
dbi_result result;
result = dbi_conn_queryf(conn, "SELECT id,created,valid_until FROM SMS ORDER BY created LIMIT 1");
result = dbi_conn_queryf(conn, "SELECT id,valid_until FROM SMS "
"ORDER BY valid_until LIMIT 1");
if (!result)
return;
if (next_row(result)) {
unsigned long long sms_id;
time_t created, validity_timestamp;
time_t validity_timestamp;
sms_id = dbi_result_get_ulonglong(result, "id");
created = dbi_result_get_datetime(result, "created");
validity_timestamp = dbi_result_get_datetime(result, "valid_until");
delete_expired_sms(sms_id, created, validity_timestamp);
delete_expired_sms(sms_id, validity_timestamp);
}
dbi_result_free(result);

View File

@ -68,8 +68,7 @@ DDB NOTICE test_db_sms_get('Truncated TP-UD (255 octets, 8-bit encoding)'): succ
DDB NOTICE verify_sms('Truncated TP-UD (255 octets, 8-bit encoding)'): TP-User-Data mismatch
DDB NOTICE test_db_sms_get('Same MSISDN #1'): failure, as expected
DDB NOTICE test_db_sms_get('Same MSISDN #2'): failure, as expected
DDB NOTICE test_db_sms_get('Expired SMS'): unexpected result
DDB NOTICE verify_sms('Expired SMS'): match
DDB NOTICE test_db_sms_get('Expired SMS'): failure, as expected
DDB NOTICE test_db_sms_get('Empty TP-UD'): success, as expected
DDB NOTICE verify_sms('Empty TP-UD'): match
full talloc report on 'null_context' (total 0 bytes in 1 blocks)