Add a VTY command which deletes all expired SMS.

We already delete SMS which have been sent successfully. However, there
are plans to accept SMS for any subscriber in order to fix the problem
described in https://osmocom.org/issues/2354 ("SMSC: Store&Forward not
working for subscribed but unregistered MS").

This means we may end up storing SMS which never get sent, e.g. because
the B subscriber doesn't actually exist. This could lead to a higher
degree of SMS database growth over time, and therefore we need a way
to keep database size under control.

As a first step, introduce a DB function which removes an expired SMS,
and add a VTY command which removes all expired SMS from the DB.

Later commits will build upon this to remove expired SMS automatically.

The SMS expiry time period is currently hard-coded to 2 weeks.
We could make this configurable in the future if desired.

Change-Id: Icd6093b7b5d8db84b19a0aa47c68182566113ee2
Related: OS#2354
This commit is contained in:
Stefan Sperling 2018-01-22 17:05:37 +01:00 committed by Harald Welte
parent 703f2ec6f4
commit 87cba1f105
5 changed files with 85 additions and 7 deletions

View File

@ -50,6 +50,7 @@ int db_sms_mark_delivered(struct gsm_sms *sms);
int db_sms_inc_deliver_attempts(struct gsm_sms *sms);
int db_sms_delete_by_msisdn(const char *msisdn);
int db_sms_delete_sent_message_by_id(unsigned long long sms_id);
int db_sms_delete_expired_message_by_id(unsigned long long sms_id);
/* Statistics counter storage */
struct osmo_counter;

View File

@ -13,5 +13,6 @@ int sms_queue_stats(struct gsm_sms_queue *, struct vty* vty);
int sms_queue_set_max_pending(struct gsm_sms_queue *, int max);
int sms_queue_set_max_failure(struct gsm_sms_queue *, int fail);
int sms_queue_clear(struct gsm_sms_queue *);
int sms_queue_sms_is_pending(struct gsm_sms_queue *smsq, unsigned long long sms_id);
#endif

View File

@ -989,6 +989,43 @@ int db_sms_delete_sent_message_by_id(unsigned long long sms_id)
return 0;
}
int db_sms_delete_expired_message_by_id(unsigned long long sms_id)
{
dbi_result result;
time_t created, validity_timestamp, now, min_created;
result = dbi_conn_queryf(conn, "SELECT created,valid_until FROM SMS WHERE id = %llu", sms_id);
if (!result)
return -1;
if (!next_row(result)) {
dbi_result_free(result);
return -1;
}
created = dbi_result_get_datetime(result, "created");
validity_timestamp = dbi_result_get_datetime(result, "valid_until");
dbi_result_free(result);
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 */
return -1;
result = dbi_conn_queryf(conn, "DELETE FROM SMS WHERE id = %llu", sms_id);
if (!result) {
LOGP(DDB, LOGL_ERROR, "Failed to delete SMS %llu.\n", sms_id);
return -1;
}
dbi_result_free(result);
return 0;
}
int db_store_counter(struct osmo_counter *ctr)
{
dbi_result result;

View File

@ -72,21 +72,21 @@ static int sms_subscr_cb(unsigned int, unsigned int, void *, void *);
static int sms_sms_cb(unsigned int, unsigned int, void *, void *);
static struct gsm_sms_pending *sms_find_pending(struct gsm_sms_queue *smsq,
struct gsm_sms *sms)
unsigned long long sms_id)
{
struct gsm_sms_pending *pending;
llist_for_each_entry(pending, &smsq->pending_sms, entry) {
if (pending->sms_id == sms->id)
if (pending->sms_id == sms_id)
return pending;
}
return NULL;
}
static int sms_is_in_pending(struct gsm_sms_queue *smsq, struct gsm_sms *sms)
int sms_queue_sms_is_pending(struct gsm_sms_queue *smsq, unsigned long long sms_id)
{
return sms_find_pending(smsq, sms) != NULL;
return sms_find_pending(smsq, sms_id) != NULL;
}
static struct gsm_sms_pending *sms_subscriber_find_pending(
@ -286,7 +286,7 @@ static void sms_submit_pending(void *_data)
}
/* no need to send a pending sms */
if (sms_is_in_pending(smsq, sms)) {
if (sms_queue_sms_is_pending(smsq, sms->id)) {
LOGP(DLSMS, LOGL_DEBUG,
"SMSqueue with pending sms: %llu. Skipping\n", sms->id);
sms_free(sms);
@ -337,7 +337,7 @@ static void sms_send_next(struct vlr_subscr *vsub)
goto no_pending_sms;
/* The sms should not be scheduled right now */
OSMO_ASSERT(!sms_is_in_pending(smsq, sms));
OSMO_ASSERT(!sms_queue_sms_is_pending(smsq, sms->id));
/* Remember that we deliver this SMS and send it */
pending = sms_pending_from(smsq, sms);
@ -472,7 +472,7 @@ static int sms_sms_cb(unsigned int subsys, unsigned int signal,
* sms that are not in our control as we just have a channel
* open anyway.
*/
pending = sms_find_pending(network->sms_queue, sig_sms->sms);
pending = sms_find_pending(network->sms_queue, sig_sms->sms->id);
if (!pending)
return 0;

View File

@ -161,6 +161,44 @@ DEFUN(sms_send_pend,
return CMD_SUCCESS;
}
DEFUN(sms_delete_expired,
sms_delete_expired_cmd,
"sms delete expired",
"SMS related commands\n" "SMS Database related commands\n"
"Delete all expired SMS")
{
struct gsm_network *gsmnet = gsmnet_from_vty(vty);
struct gsm_sms *sms;
unsigned long long sms_id = 0;
long long num_deleted = 0;
while (1) {
sms = db_sms_get_next_unsent(gsmnet, sms_id, UINT_MAX);
if (!sms)
break;
/* Skip SMS which are currently queued for sending. */
if (sms_queue_sms_is_pending(gsmnet->sms_queue, sms->id))
continue;
/* Expiration check is performed by the DB layer. */
if (db_sms_delete_expired_message_by_id(sms->id) == 0)
num_deleted++;
sms_id = sms->id + 1;
}
if (num_deleted == 0) {
vty_out(vty, "No expired SMS in database%s", VTY_NEWLINE);
return CMD_WARNING;
}
vty_out(vty, "Deleted %llu expired SMS from database%s", num_deleted, VTY_NEWLINE);
return CMD_SUCCESS;
}
static int _send_sms_str(struct vlr_subscr *receiver,
struct vlr_subscr *sender,
char *str, uint8_t tp_pid)
@ -861,6 +899,7 @@ int bsc_vty_init_extra(void)
install_element_ve(&show_subscr_cache_cmd);
install_element_ve(&sms_send_pend_cmd);
install_element_ve(&sms_delete_expired_cmd);
install_element_ve(&subscriber_create_cmd);
install_element_ve(&subscriber_send_sms_cmd);