From 4feb72409a7a80f8f687022a3da45922bab456b1 Mon Sep 17 00:00:00 2001 From: Brian Kelly Date: Wed, 15 Jul 2015 13:53:19 -0400 Subject: [PATCH] write_redis plugin: Add option to limit sorted set size --- src/collectd.conf.pod | 10 ++++++++-- src/write_redis.c | 16 +++++++++++++++- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/collectd.conf.pod b/src/collectd.conf.pod index 3e43ca26..a06abe92 100644 --- a/src/collectd.conf.pod +++ b/src/collectd.conf.pod @@ -7632,7 +7632,7 @@ the timestamp as the score. Retrieving a date range can then be done using the C I command. Additionally, all the identifiers of these I are kept in a I called C (or C<${prefix}/values> if the B option was specified) and can be retrieved -using the C I command. You can specify the database to use +using the C I command. You can specify the database to use with the B parameter (default is C<0>). See L and L for details. @@ -7677,7 +7677,13 @@ is recommended but not required to include a trailing slash in I. =item B I -This index selects the redis database to use for writing operations. Defaults to C<0>. +This index selects the redis database to use for writing operations. Defaults +to C<0>. + +=item B I + +The B option limits the number of items that the I can +hold. Negative values for I sets no limit, which is the default behavior. =back diff --git a/src/write_redis.c b/src/write_redis.c index 5560b4d1..8dea10c8 100644 --- a/src/write_redis.c +++ b/src/write_redis.c @@ -46,6 +46,7 @@ struct wr_node_s struct timeval timeout; char *prefix; int database; + int max_set_size; redisContext *conn; pthread_mutex_t lock; @@ -105,7 +106,7 @@ static int wr_write (const data_set_t *ds, /* {{{ */ pthread_mutex_unlock (&node->lock); return (-1); } - + rr = redisCommand(node->conn, "SELECT %d", node->database); if (rr == NULL) WARNING("SELECT command error. database:%d message:%s", node->database, node->conn->errstr); @@ -119,6 +120,15 @@ static int wr_write (const data_set_t *ds, /* {{{ */ else freeReplyObject (rr); + if (node->max_set_size >= 0) + { + rr = redisCommand (node->conn, "ZREMRANGEBYRANK %s %d %d", key, 0, (-1 * node->max_set_size) - 1); + if (rr == NULL) + WARNING("SELECT command error. database:%d message:%s", node->database, node->conn->errstr); + else + freeReplyObject (rr); + } + /* TODO(octo): This is more overhead than necessary. Use the cache and * metadata to determine if it is a new metric and call SADD only once for * each metric. */ @@ -170,6 +180,7 @@ static int wr_config_node (oconfig_item_t *ci) /* {{{ */ node->conn = NULL; node->prefix = NULL; node->database = 0; + node->max_set_size = -1; pthread_mutex_init (&node->lock, /* attr = */ NULL); status = cf_util_get_string_buffer (ci, node->name, sizeof (node->name)); @@ -204,6 +215,9 @@ static int wr_config_node (oconfig_item_t *ci) /* {{{ */ else if (strcasecmp ("Database", child->key) == 0) { status = cf_util_get_int (child, &node->database); } + else if (strcasecmp ("MaxSetSize", child->key) == 0) { + status = cf_util_get_int (child, &node->max_set_size); + } else WARNING ("write_redis plugin: Ignoring unknown config option \"%s\".", child->key); -- 2.11.0