forked from flux-framework/flux-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
libkvs: add kvs checkpoint convenience functions
Problem: Some kvs checkpointing operations are duplicated. It would be convenient if there were common functions for it. Solution: Add common checkpointing operations into a new kvs_checkpoint api in libkvs. Keep the API private for now. Add unit tests. Fixes flux-framework#4145
- Loading branch information
Showing
4 changed files
with
258 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
/************************************************************\ | ||
* Copyright 2022 Lawrence Livermore National Security, LLC | ||
* (c.f. AUTHORS, NOTICE.LLNS, COPYING) | ||
* | ||
* This file is part of the Flux resource manager framework. | ||
* For details, see https://github.com/flux-framework. | ||
* | ||
* SPDX-License-Identifier: LGPL-3.0 | ||
\************************************************************/ | ||
|
||
#if HAVE_CONFIG_H | ||
#include "config.h" | ||
#endif | ||
#include <assert.h> | ||
#include <errno.h> | ||
#include <stdlib.h> | ||
#include <jansson.h> | ||
#include <flux/core.h> | ||
#include <time.h> | ||
|
||
#include "kvs_checkpoint.h" | ||
|
||
flux_future_t *kvs_checkpoint_commit (flux_t *h, | ||
const char *key, | ||
const char *rootref) | ||
{ | ||
flux_future_t *f = NULL; | ||
double timestamp; | ||
|
||
if (!h || !key || !rootref) { | ||
errno = EINVAL; | ||
return NULL; | ||
} | ||
|
||
timestamp = flux_reactor_now (flux_get_reactor (h)); | ||
|
||
if (!(f = flux_rpc_pack (h, | ||
"kvs-checkpoint.put", | ||
0, | ||
0, | ||
"{s:s s:{s:i s:s s:f}}", | ||
"key", | ||
key, | ||
"value", | ||
"version", 1, | ||
"rootref", rootref, | ||
"timestamp", timestamp))) | ||
return NULL; | ||
|
||
return f; | ||
} | ||
|
||
flux_future_t *kvs_checkpoint_lookup (flux_t *h, const char *key) | ||
{ | ||
if (!h || !key) { | ||
errno = EINVAL; | ||
return NULL; | ||
} | ||
|
||
return flux_rpc_pack (h, | ||
"kvs-checkpoint.get", | ||
0, | ||
0, | ||
"{s:s}", | ||
"key", | ||
key); | ||
} | ||
|
||
int kvs_checkpoint_lookup_get_rootref (flux_future_t *f, const char **rootref) | ||
{ | ||
const char *tmp_rootref; | ||
int version; | ||
|
||
if (!f || !rootref) { | ||
errno = EINVAL; | ||
return -1; | ||
} | ||
|
||
if (flux_rpc_get_unpack (f, "{s:{s:i s:s}}", | ||
"value", | ||
"version", &version, | ||
"rootref", &tmp_rootref) < 0) | ||
return -1; | ||
|
||
if (version != 0 && version != 1) { | ||
errno = EINVAL; | ||
return -1; | ||
} | ||
|
||
(*rootref) = tmp_rootref; | ||
return 0; | ||
} | ||
|
||
/* returns "N/A" if not available */ | ||
int kvs_checkpoint_lookup_get_formatted_timestamp (flux_future_t *f, | ||
char *buf, | ||
size_t len) | ||
{ | ||
int version; | ||
double timestamp = 0.; | ||
|
||
if (!f || !buf) { | ||
errno = EINVAL; | ||
return -1; | ||
} | ||
|
||
if (flux_rpc_get_unpack (f, "{s:{s:i s?f}}", | ||
"value", | ||
"version", &version, | ||
"timestamp", ×tamp) < 0) | ||
return -1; | ||
|
||
if (version != 0 && version != 1) { | ||
errno = EINVAL; | ||
return -1; | ||
} | ||
|
||
if (version == 1) { | ||
time_t sec = timestamp; | ||
struct tm tm; | ||
gmtime_r (&sec, &tm); | ||
if (strftime (buf, len, "%FT%T", &tm) == 0) { | ||
errno = EINVAL; | ||
return -1; | ||
} | ||
} | ||
else { /* version == 0 */ | ||
if (snprintf (buf, len, "N/A") >= len) { | ||
errno = EINVAL; | ||
return -1; | ||
} | ||
} | ||
return 0; | ||
} | ||
|
||
/* | ||
* vi:tabstop=4 shiftwidth=4 expandtab | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/************************************************************\ | ||
* Copyright 2022 Lawrence Livermore National Security, LLC | ||
* (c.f. AUTHORS, NOTICE.LLNS, COPYING) | ||
* | ||
* This file is part of the Flux resource manager framework. | ||
* For details, see https://github.com/flux-framework. | ||
* | ||
* SPDX-License-Identifier: LGPL-3.0 | ||
\************************************************************/ | ||
|
||
#ifndef _KVS_CHECKPOINT_H | ||
#define _KVS_CHECKPOINT_H | ||
|
||
#include <flux/core.h> | ||
|
||
flux_future_t *kvs_checkpoint_commit (flux_t *h, | ||
const char *key, | ||
const char *rootref); | ||
|
||
flux_future_t *kvs_checkpoint_lookup (flux_t *h, const char *key); | ||
|
||
int kvs_checkpoint_lookup_get_rootref (flux_future_t *f, const char **rootref); | ||
|
||
/* returns "N/A" if not available */ | ||
int kvs_checkpoint_lookup_get_formatted_timestamp (flux_future_t *f, | ||
char *buf, | ||
size_t len); | ||
|
||
#endif /* !_KVS_CHECKPOINT_H */ | ||
|
||
/* | ||
* vi:tabstop=4 shiftwidth=4 expandtab | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/************************************************************\ | ||
* Copyright 2022 Lawrence Livermore National Security, LLC | ||
* (c.f. AUTHORS, NOTICE.LLNS, COPYING) | ||
* | ||
* This file is part of the Flux resource manager framework. | ||
* For details, see https://github.com/flux-framework. | ||
* | ||
* SPDX-License-Identifier: LGPL-3.0 | ||
\************************************************************/ | ||
|
||
#if HAVE_CONFIG_H | ||
#include "config.h" | ||
#endif | ||
|
||
#include <string.h> | ||
#include <errno.h> | ||
#include <flux/core.h> | ||
|
||
#include "src/common/libflux/flux.h" | ||
#include "kvs_checkpoint.h" | ||
#include "src/common/libtap/tap.h" | ||
|
||
void errors (void) | ||
{ | ||
flux_future_t *f; | ||
const char *rootref; | ||
char buf[128]; | ||
|
||
errno = 0; | ||
ok (kvs_checkpoint_commit (NULL, NULL, NULL) == NULL | ||
&& errno == EINVAL, | ||
"kvs_checkpoint_commit fails on bad input"); | ||
|
||
errno = 0; | ||
ok (kvs_checkpoint_lookup (NULL, NULL) == NULL | ||
&& errno == EINVAL, | ||
"kvs_checkpoint_lookup fails on bad input"); | ||
|
||
errno = 0; | ||
ok (kvs_checkpoint_lookup_get_rootref (NULL, NULL) < 0 | ||
&& errno == EINVAL, | ||
"kvs_checkpoint_lookup_get_rootref fails on bad input"); | ||
|
||
errno = 0; | ||
ok (kvs_checkpoint_lookup_get_formatted_timestamp (NULL, NULL, 0) < 0 | ||
&& errno == EINVAL, | ||
"kvs_checkpoint_lookup_get_formatted_timestamp fails on bad input"); | ||
|
||
if (!(f = flux_future_create (NULL, NULL))) | ||
BAIL_OUT ("flux_future_create failed"); | ||
|
||
errno = 0; | ||
ok (kvs_checkpoint_lookup_get_rootref (f, &rootref) < 0 | ||
&& errno == EINVAL, | ||
"kvs_checkpoint_lookup_get_rootref fails on unfulfilled future"); | ||
|
||
errno = 0; | ||
ok (kvs_checkpoint_lookup_get_formatted_timestamp (f, buf, 128) < 0 | ||
&& errno == EINVAL, | ||
"kvs_checkpoint_lookup_get_formatted_timestamp " | ||
"fails on unfulfilled future"); | ||
|
||
flux_future_destroy (f); | ||
} | ||
|
||
int main (int argc, char *argv[]) | ||
{ | ||
|
||
plan (NO_PLAN); | ||
|
||
errors (); | ||
|
||
done_testing(); | ||
return (0); | ||
} | ||
|
||
/* | ||
* vi:tabstop=4 shiftwidth=4 expandtab | ||
*/ | ||
|