Skip to content

Commit

Permalink
rename admin command count to census, report min/max on key & value s…
Browse files Browse the repository at this point in the history
…izes in addition to counts & total sizes
  • Loading branch information
Yao Yue committed Oct 27, 2018
1 parent fd3cd70 commit 6532ecf
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 22 deletions.
10 changes: 8 additions & 2 deletions src/protocol/admin/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@
#define METRIC_DESCRIBE_LEN 120 /* 34 (name) + 16 (type) + 68 (description) + CRLF */
#define METRIC_END "END\r\n"
#define METRIC_END_LEN (sizeof(METRIC_END) - 1)
#define KEYCOUNT_FMT "%zu %zu %zu\r\n"
#define KEYCOUNT_LEN 64 /* 20 * 3 (numbers) + 2 (spaces) + CRLF */
#define CENSUS_COUNT_FMT "item count: %zu %zu %zu\r\n"
#define CENSUS_COUNT_LEN 34 /* 12 (name string) + 20 + CRLF */
#define CENSUS_KEY_FMT "key min: %zu, max: %zu, total: %zu\r\n"
#define CENSUS_KEY_LEN 87 /* 9 + 7 + 9 (name strings) + 20 * 3 + CRLF */
#define CENSUS_VAL_FMT "val min: %zu, max: %zu, total: %zu\r\n"
#define CENSUS_VAL_LEN 87 /* 9 + 7 + 9 (name strings) + 20 * 3 + CRLF */
#define CENSUS_FMT CENSUS_COUNT_FMT CENSUS_KEY_FMT CENSUS_VAL_FMT
#define CENSUS_LEN CENSUS_COUNT_LEN + CENSUS_KEY_LEN + CENSUS_VAL_LEN

#define VERSION_PRINTED "VERSION " VERSION_STRING "\r\n"

Expand Down
4 changes: 2 additions & 2 deletions src/protocol/admin/parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ _get_req_type(struct request *req, struct bstring *type)
break;
}

if (str5cmp(type->data, 'c', 'o', 'u', 'n', 't')) {
req->type = REQ_COUNT;
if (str6cmp(type->data, 'c', 'e', 'n', 's', 'u', 's')) {
req->type = REQ_CENSUS;
break;
}

Expand Down
2 changes: 1 addition & 1 deletion src/protocol/admin/request.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
ACTION( REQ_UNKNOWN, "" )\
ACTION( REQ_STATS, "stats" )\
ACTION( REQ_VERSION, "version" )\
ACTION( REQ_COUNT, "count" )\
ACTION( REQ_CENSUS, "census" )\
ACTION( REQ_DUMP, "dump" )\
ACTION( REQ_QUIT, "quit" )

Expand Down
18 changes: 10 additions & 8 deletions src/server/twemcache/admin/process.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ admin_process_setup(void)
}

nmetric_perslab = METRIC_CARDINALITY(perslab[0]);
/* so far the largest response comes from per-slab metrics */
/* perslab metric size <(32 + 20)B, prefix/suffix 12B, total < 64 */
cap = MAX(nmetric, nmetric_perslab * SLABCLASS_MAX_ID) * METRIC_PRINT_LEN +
METRIC_END_LEN;
Expand Down Expand Up @@ -115,22 +116,23 @@ _key_dump(struct response *rsp, struct request *req)
}

static void
_key_count(struct response *rsp, struct request *req)
_key_census(struct response *rsp, struct request *req)
{
size_t nkey, ksize, vsize;
size_t nkey, ktotal, vtotal, kmin, kmax, vmin, vmax;
int ret = 0;

if (req->arg.len > 0) { /* skip initial space */
req->arg.len--;
req->arg.data++;
}
log_info("count keys with prefix %.*s", req->arg.len, req->arg.data);
log_info("census on keys with prefix %.*s", req->arg.len, req->arg.data);

item_count(&nkey, &ksize, &vsize, &req->arg);
item_census(&nkey, &ktotal, &kmin, &kmax, &vtotal, &vmin, &vmax, &req->arg);
rsp->type = RSP_GENERIC;
ret = cc_scnprintf(buf, cap, KEYCOUNT_FMT, nkey, ksize, vsize);
ret = cc_scnprintf(buf, cap, CENSUS_FMT, nkey, ktotal, kmin, kmax, vtotal,
vmin, vmax);
if (ret < 0) {
rsp->data = str2bstr("ERROR: cannot format key count");
rsp->data = str2bstr("ERROR: cannot format key census result");
} else {
rsp->data.len = ret;
rsp->data.data = buf;
Expand All @@ -155,8 +157,8 @@ admin_process_request(struct response *rsp, struct request *req)
_key_dump(rsp, req);
break;

case REQ_COUNT:
_key_count(rsp, req);
case REQ_CENSUS:
_key_census(rsp, req);
break;

default:
Expand Down
28 changes: 22 additions & 6 deletions src/storage/slab/item.c
Original file line number Diff line number Diff line change
Expand Up @@ -384,25 +384,37 @@ item_flush(void)

/* this dumps all keys (matching a prefix if given) regardless of expiry status */
void
item_count(size_t *nkey, size_t *ksize, size_t *vsize, struct bstring *prefix)
item_census(size_t *nkey, size_t *ktotal, size_t *kmin, size_t *kmax,
size_t *vtotal, size_t *vmin, size_t *vmax, struct bstring *prefix)
{
uint32_t nbucket = HASHSIZE(hash_table->hash_power);
size_t klen, vlen;

log_info("start scanning all %"PRIu32" keys", hash_table->nhash_item);

*nkey = 0;
*ksize = 0;
*vsize = 0;
*ktotal = 0;
*vtotal = 0;
*kmin = SIZE_MAX;
*kmax = 0;
*vmin = SIZE_MAX;
*vmax = 0;
for (uint32_t i = 0; i < nbucket; i++) {
struct item_slh *entry = &hash_table->table[i];
struct item *it;

SLIST_FOREACH(it, entry, i_sle) {
if (it->klen >= prefix->len &&
klen = it->klen;
vlen = it->vlen;
if (klen >= prefix->len &&
cc_bcmp(prefix->data, item_key(it), prefix->len) == 0) {
*nkey += 1;
*ksize += it->klen;
*vsize += it->vlen;
*ktotal += klen;
*vtotal += vlen;
*kmin = MIN(*kmin, klen);
*kmax = MAX(*kmax, klen);
*vmin = MIN(*vmin, vlen);
*vmax = MAX(*vmax, vlen);
}
}

Expand All @@ -412,6 +424,10 @@ item_count(size_t *nkey, size_t *ksize, size_t *vsize, struct bstring *prefix)
}
}

if (*nkey == 0) { /* report 0 on all fields if no match has been found */
*kmin = *vmin = 0;
}

log_info("finish scanning all keys");
}

Expand Down
6 changes: 3 additions & 3 deletions src/storage/slab/item.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@
* | | \
* \ | item_key()
* item \
* item->end, (if enabled) item_get_cas(), metadata
* item->end, (if used) optional metadata
*
* item->end is followed by:
* - 8-byte cas, if ITEM_CAS flag is set
* - optional metadata
* - key
* - data
*/
Expand Down Expand Up @@ -214,6 +214,6 @@ bool item_delete(const struct bstring *key);
void item_flush(void);

/* this surveys all keys (matching a prefix if given) regardless of expiry status */
void item_count(size_t *nkey, size_t *ksize, size_t *vsize, struct bstring *prefix);
void item_census(size_t *nkey, size_t *ktotal, size_t *kmin, size_t *kmax, size_t *vtotal, size_t *vmin, size_t *vmax, struct bstring *prefix);
/* this dumps all keys (matching a prefix if given) regardless of expiry status */
bool item_dump(struct bstring *prefix);

0 comments on commit 6532ecf

Please sign in to comment.