-
Notifications
You must be signed in to change notification settings - Fork 902
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add datastoreusage
Command
#6442
Add datastoreusage
Command
#6442
Conversation
OK, I would omit the level option for now: simply give a root value (or []) and return the bytes used. Note that bytes here is not an accurate reflection of actual db usage, since there is overhead. However, make sure you correctly account for keys, as well as values! (People can store information in keys!) Also, result for command X should usually be an object called similar to X. Perhaps something like:
|
lightningd/datastore.c
Outdated
const jsmntok_t *params) | ||
{ | ||
struct json_stream *response; | ||
const char **key, **k = NULL; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This initialization is wrong BTW, it's **key which needs initializing, not **k!
lightningd/datastore.c
Outdated
@@ -343,6 +349,11 @@ static struct command_result *json_datastoreusage(struct command *cmd, | |||
stmt; | |||
stmt = wallet_datastore_next(cmd, cmd->ld->wallet, stmt, | |||
&k, &data, &gen)) { | |||
|
|||
// Break if we got a key that we are not interested in. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just merged API change means you don't need to do this any more: wallet_datastore_next will do it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rebased to include the API change.
ea1eb80
to
42cf691
Compare
Rebased onto master to include new wallet API and implemented the proposed changes. Since the datastore-API currently only allows "datastoreusage": {
"key": [],
"total_bytes": 2222 /* len(key_blob + data) + all descendents' len(key_blob + data) */
} |
Oh, and I saved the key as a string in the response, but I'm happy to change it back to an array type if that's more convenient. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Look like the CI is complaining about some whitespace
ightningd/datastore.c:319: stmt = wallet_datastore_next(cmd, key, stmt,
lightningd/datastore.c:323:
lightningd/datastore.c:334:
Extraneous whitespace found
make: *** [Makefile:521: check-whitespace/lightningd/datastore.c] Error 1
make: *** Waiting for unfinished jobs....
Cloning into '/home/runner/work/lightning/lightning/external/libwally-core'...
Cloning into '/home/runner/work/lightning/lightning/external/lnprototest'...
Cloning into '/home/runner/work/lightning/lightning/external/lowdown'...
8c79c96
to
5984290
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is great, but it needs:
- A Changelog-Added entry saying there's a new RPC command.
- A test :)
5984290
to
ac4c919
Compare
Changed the version, updated pyln-client, added some tests and a Changelog entry. |
8e4d618
to
d1c6c59
Compare
CHANGELOG.md
Outdated
## [Unreleased] | ||
|
||
### Added | ||
|
||
- JSON-RPC: `datastoreusage`: returns the total bytes that are stored under a given key. | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, don't edit CHANGELOG.md!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use a Changelog-Added line in your commit message instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uh, sorry about that, I added a Changelog-Added
line to the initial commit message.
"added": "v23.11", | ||
"properties": { | ||
"key": { | ||
"oneOf": [ | ||
{ | ||
"type": "array", | ||
"description": "key is an array of values (though a single value is treated as a one-element array). Used as the staring point to traverse the datastore.", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo nit: "starting point"
lightningd/datastore.c
Outdated
const char **k, **key = NULL; | ||
struct db_stmt *stmt; | ||
const u8 *data; | ||
u64 gen; | ||
u64 total = 0; | ||
u64 gen, total_bytes = 0; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Combine first two commits, since the first doesn't compile anywayk, and you radically change behavior.
Also **key doesn't need initialization.
lightningd/datastore.c
Outdated
|
||
// We use the formatted datastore key to calculate the size of | ||
// the key to account for the delimiter byte in the database. | ||
self_bytes += strlen(datastore_key_fmt(tmpctx, k)) - 2; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doing the fmt to get the right answer seems strange here?
How about:
u64 self_bytes = tal_bytelen(data);
/* One byte per separator */
self_bytes += tal_count(key) - 1;
for (size_t i = 0; i < tal_count(key); i++) {
self_bytes += strlen(key[i]);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed, It now is calculated form the key chars itself not the formatted string. This is straight forward and makes it even easier as it saves us a weird -2
or -1
to remove separators from the calculation.
ce3631d
to
d0a310b
Compare
|
||
u64 self_bytes = tal_bytelen(data); | ||
for (size_t i = 0; i < tal_count(k); i++) { | ||
self_bytes += strlen(k[i]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't quite right, since the actual space in the db includes the tal_count(k)-1 separators. Note that tal_count(k) is always >=1 though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your review!
Uh yeah I guess I was still asleep when I did this yesterday. I forgot to double check the db schema. The PK of the table is the key as a BLOB
with key entries separated by a string terminator \0
. I forgot to count these and added a fix self_bytes += tal_count(k) - 1;
datastoreusage returns the total_bytes that are stored under a given {Key} or from root. {Key} is the entry point from which we begin to traverse the datastore. Changelog-Added: JSON-RPC: `datastoreusage`: returns the total bytes that are stored under a given key. Signed-off-by: Peter Neuroth <[email protected]>
Signed-off-by: Peter Neuroth <[email protected]>
Signed-off-by: Peter Neuroth <[email protected]>
Signed-off-by: Peter Neuroth <[email protected]>
Signed-off-by: Peter Neuroth <[email protected]>
Signed-off-by: Peter Neuroth <[email protected]>
d0a310b
to
2acb8b3
Compare
This PR aims to add a RPC command to the datastore-api called
datastoreusage
that is somewhat inspired by the unix commanddu
.datastoreusage
returns an object that contains ausage
report andtotal
that is the size in byte occupied by the data stored in thedatastore
.datastoreusage
accepts the following (optional) parameter:key
is used as a starting point to traverse thedatastore
to collect the size of the data that is stored under this key and all children.depth
gives the max depth of children that are returned in theusage
report from the (optional) key.depth
does NOT affect the total size, only the depth of the details inusage
(much likedu
).Examples (all from the same datastore):
datastoreusage
datastoreusage "" 1
datastoreusage "level0"
datastoreusage "level0" 0
Thoughts and comments are appreciated!