Skip to content

Commit

Permalink
kvs: use zhashx for roothash
Browse files Browse the repository at this point in the history
Problem: The primary roothash is a zhash_t, however it is more
common to use a zhashx_t in flux-core since the initial memory
footprint is much smaller.

Convert roothash to use a zhashx_t over a zhash_t.
  • Loading branch information
chu11 committed Feb 12, 2025
1 parent 41df35c commit 605b4c4
Showing 1 changed file with 20 additions and 23 deletions.
43 changes: 20 additions & 23 deletions src/modules/kvs/kvsroot.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,26 @@
#include "kvsroot.h"

struct kvsroot_mgr {
zhash_t *roothash;
zhashx_t *roothash;
zlist_t *removelist;
bool iterating_roots;
flux_t *h;
void *arg;
};

static void kvsroot_destroy (void **data);

kvsroot_mgr_t *kvsroot_mgr_create (flux_t *h, void *arg)
{
kvsroot_mgr_t *krm = NULL;

if (!(krm = calloc (1, sizeof (*krm))))
goto error;
if (!(krm->roothash = zhash_new ())) {
if (!(krm->roothash = zhashx_new ())) {
errno = ENOMEM;
goto error;
}
zhashx_set_destructor (krm->roothash, kvsroot_destroy);
if (!(krm->removelist = zlist_new ())) {
errno = ENOMEM;
goto error;
Expand All @@ -65,7 +68,7 @@ void kvsroot_mgr_destroy (kvsroot_mgr_t *krm)
if (krm) {
int save_errno = errno;
if (krm->roothash)
zhash_destroy (&krm->roothash);
zhashx_destroy (&krm->roothash);
if (krm->removelist)
zlist_destroy (&krm->removelist);
free (krm);
Expand All @@ -75,13 +78,14 @@ void kvsroot_mgr_destroy (kvsroot_mgr_t *krm)

int kvsroot_mgr_root_count (kvsroot_mgr_t *krm)
{
return zhash_size (krm->roothash);
return zhashx_size (krm->roothash);
}

static void kvsroot_destroy (void *data)
/* zhashx_destructor_fn */
static void kvsroot_destroy (void **data)
{
if (data) {
struct kvsroot *root = data;
struct kvsroot *root = *data;
int save_errno = errno;
if (root->ns_name)
free (root->ns_name);
Expand All @@ -93,7 +97,7 @@ static void kvsroot_destroy (void *data)
zlist_destroy (&root->wait_version_list);
if (root->setroot_queue)
flux_msglist_destroy (root->setroot_queue);
free (data);
free (root);
errno = save_errno;
}
}
Expand All @@ -105,6 +109,8 @@ static void flux_msg_decref_wrapper (void **item)
flux_msg_decref (msg);
}

/* zhashx_destructor_fn */

struct kvsroot *kvsroot_mgr_create_root (kvsroot_mgr_t *krm,
struct cache *cache,
const char *hash_name,
Expand All @@ -113,7 +119,6 @@ struct kvsroot *kvsroot_mgr_create_root (kvsroot_mgr_t *krm,
int flags)
{
struct kvsroot *root;
int save_errnum;

/* Don't modify hash while iterating */
if (krm->iterating_roots) {
Expand Down Expand Up @@ -163,25 +168,17 @@ struct kvsroot *kvsroot_mgr_create_root (kvsroot_mgr_t *krm,
root->flags = flags;
root->remove = false;

if (zhash_insert (krm->roothash, ns, root) < 0) {
if (zhashx_insert (krm->roothash, ns, root) < 0) {
errno = EEXIST;
flux_log_error (krm->h, "zhash_insert");
goto error;
}

if (!zhash_freefn (krm->roothash, ns, kvsroot_destroy)) {
flux_log_error (krm->h, "zhash_freefn");
save_errnum = errno;
zhash_delete (krm->roothash, ns);
errno = save_errnum;
flux_log_error (krm->h, "zhashx_insert");

Check warning on line 173 in src/modules/kvs/kvsroot.c

View check run for this annotation

Codecov / codecov/patch

src/modules/kvs/kvsroot.c#L173

Added line #L173 was not covered by tests
goto error;
}

list_node_init (&root->work_queue_node);
return root;

error:
kvsroot_destroy (root);
kvsroot_destroy ((void **)&root);

Check warning on line 181 in src/modules/kvs/kvsroot.c

View check run for this annotation

Codecov / codecov/patch

src/modules/kvs/kvsroot.c#L181

Added line #L181 was not covered by tests
return NULL;
}

Expand All @@ -204,14 +201,14 @@ int kvsroot_mgr_remove_root (kvsroot_mgr_t *krm, const char *ns)
}
}
else
zhash_delete (krm->roothash, ns);
zhashx_delete (krm->roothash, ns);
return 0;
}

struct kvsroot *kvsroot_mgr_lookup_root (kvsroot_mgr_t *krm,
const char *ns)
{
return zhash_lookup (krm->roothash, ns);
return zhashx_lookup (krm->roothash, ns);
}

struct kvsroot *kvsroot_mgr_lookup_root_safe (kvsroot_mgr_t *krm,
Expand All @@ -233,7 +230,7 @@ int kvsroot_mgr_iter_roots (kvsroot_mgr_t *krm, kvsroot_root_f cb, void *arg)

krm->iterating_roots = true;

root = zhash_first (krm->roothash);
root = zhashx_first (krm->roothash);
while (root) {
int ret;

Expand All @@ -243,7 +240,7 @@ int kvsroot_mgr_iter_roots (kvsroot_mgr_t *krm, kvsroot_root_f cb, void *arg)
if (ret == 1)
break;

root = zhash_next (krm->roothash);
root = zhashx_next (krm->roothash);
}

krm->iterating_roots = false;
Expand Down

0 comments on commit 605b4c4

Please sign in to comment.