reftable: return proper error code from block_writer_add()
PATCH 1/1 reftable: return proper error code from block_writer_add()
Previously, block_writer_add() used to return generic -1, which forced an assumption about the error type.
Replace generic -1 returns in block_writer_add() and related functions with defined error codes.
---
This patch attempts to avoid making an assumption regarding error codes returned by block_writer_add().
reftable/block.c | 9 +++++---- reftable/record.c | 16 +++++++++++----- reftable/writer.c | 8 +------- 3 files changed, 17 insertions(+), 16 deletions(-)
reftable/block.c +5 -4
@@ -49,7 +49,7 @@ static int block_writer_register_restart(struct block_writer *w, int n,if (is_restart) rlen++;if (2 +3 * rlen + n > w->block_size - w->next) -return -1 ; +return REFTABLE_ENTRY_TOO_BIG_ERROR ;if (is_restart) {REFTABLE_ALLOC_GROW_OR_NULL (w->restarts, w->restart_len +1 , w->restart_cap);
@@ -115,8 +115,9 @@ int block_writer_add(struct block_writer *w, struct reftable_record *rec)int err; err =reftable_record_key (rec, &w->scratch); -if (err <0 ) +if (err <0 ) { goto done; + }if (!w->scratch.len) { err =REFTABLE_API_ERROR ;
@@ -126,14 +127,14 @@ int block_writer_add(struct block_writer *w, struct reftable_record *rec) n =reftable_encode_key (&is_restart, out, last, w->scratch,reftable_record_val_type (rec));if (n <0 ) { - err =-1 ; + err = n; goto done; }string_view_consume (&out, n); n =reftable_record_encode (rec, out, w->hash_size);if (n <0 ) { - err =-1 ; + err = n; goto done; }string_view_consume (&out, n);
reftable/record.c +11 -5
@@ -148,18 +148,18 @@ int reftable_encode_key(int *restart, struct string_view dest,uint64_t suffix_len = key.len - prefix_len;int n =put_var_int (&dest, prefix_len);if (n <0 ) -return -1 ; +return REFTABLE_ENTRY_TOO_BIG_ERROR ;string_view_consume (&dest, n); *restart = (prefix_len ==0 ); n =put_var_int (&dest, suffix_len <<3 | (uint64_t )extra);if (n <0 ) -return -1 ; +return REFTABLE_ENTRY_TOO_BIG_ERROR ;string_view_consume (&dest, n);if (dest.len < suffix_len) -return -1 ; +return REFTABLE_ENTRY_TOO_BIG_ERROR ;memcpy (dest.buf, key.buf + prefix_len, suffix_len);string_view_consume (&dest, suffix_len);
@@ -1144,14 +1144,20 @@ static struct reftable_record_vtable reftable_index_record_vtable = {int reftable_record_key (struct reftable_record *rec,struct reftable_buf *dest) { -return reftable_record_vtable (rec)->key (reftable_record_data (rec), dest); +int key_len =reftable_record_vtable (rec)->key (reftable_record_data (rec), dest); +if (key_len <0 ) +return REFTABLE_ENTRY_TOO_BIG_ERROR ; +return key_len; }int reftable_record_encode (struct reftable_record *rec,struct string_view dest,uint32_t hash_size) { -return reftable_record_vtable (rec)->encode (reftable_record_data (rec), +int encode_len =reftable_record_vtable (rec)->encode (reftable_record_data (rec), dest, hash_size); +if (encode_len <0 ) +return REFTABLE_ENTRY_TOO_BIG_ERROR ; +return encode_len; } int reftable_record_copy_from(struct reftable_record *rec,
reftable/writer.c +1 -7
@@ -327,16 +327,10 @@ static int writer_add_record(struct reftable_writer *w, goto done;/* -* Try to add the record to the writer again. If this still fails then -* the record does not fit into the block size. -* -* TODO: it would be great to have `block_writer_add()` return proper -* error codes so that we don't have to second-guess the failure -* mode here. +* Try to add the record to the writer again. */ err =block_writer_add (w->block_writer, rec);if (err) { - err =REFTABLE_ENTRY_TOO_BIG_ERROR ; goto done; }
base-commit: e969bc875963a10890d61ba84eab3a460bd9e535
Signature · 1 line
2.34.1
reftable/block.c int block_writer_add(struct block_writer *w, struct reftable_record *rec) · line 123
Quoted 11 lines · Meet Soni
reftable/block.c +2
-1@@ -115,8 +115,9 @@ int block_writer_add(struct block_writer *w, struct reftable_record *rec)int err; err =reftable_record_key (rec, &w->scratch); -if (err <0 ) +if (err <0 ) { goto done; + }if (!w->scratch.len) { err =REFTABLE_API_ERROR ;
This change probably shouldn't be here. Our style guide mentions that we prefer to not have curly braces around single-line statements.
Quoted 7 lines
reftable/block.c +1
-1@@ -126,14 +127,14 @@ int block_writer_add(struct block_writer *w, struct reftable_record *rec) n =reftable_encode_key (&is_restart, out, last, w->scratch,reftable_record_val_type (rec));if (n <0 ) { - err =-1 ; + err = n; goto done;
reftable/block.c int block_writer_add(struct block_writer *w, struct reftable_record *rec) · line 140
Quoted 10 lines
reftable/block.c +1
-1}string_view_consume (&out, n); n =reftable_record_encode (rec, out, w->hash_size);if (n <0 ) { - err =-1 ; + err = n; goto done; }string_view_consume (&out, n);
Okay. reftable_encode_key() right now only knows to return generic errors, but you fix that further down, and you also adapt reftable_record_encode().
Quoted 16 lines
reftable/record.c +1
-1@@ -148,18 +148,18 @@ int reftable_encode_key(int *restart, struct string_view dest,uint64_t suffix_len = key.len - prefix_len;int n =put_var_int (&dest, prefix_len);if (n <0 ) -return -1 ; +return REFTABLE_ENTRY_TOO_BIG_ERROR ;string_view_consume (&dest, n); *restart = (prefix_len ==0 ); n =put_var_int (&dest, suffix_len <<3 | (uint64_t )extra);if (n <0 )
reftable/record.c int reftable_encode_key(int *restart, struct string_view dest, · line 165
Quoted 10 lines
reftable/record.c +2
-2-return -1 ; +return REFTABLE_ENTRY_TOO_BIG_ERROR ;string_view_consume (&dest, n);if (dest.len < suffix_len) -return -1 ; +return REFTABLE_ENTRY_TOO_BIG_ERROR ;memcpy (dest.buf, key.buf + prefix_len, suffix_len);string_view_consume (&dest, suffix_len);
Makes sense.
reftable/record.c static struct reftable_record_vtable reftable_index_record_vtable = { · line 1151
Quoted 10 lines
reftable/record.c +4
-1@@ -1144,14 +1144,20 @@ static struct reftable_record_vtable reftable_index_record_vtable = {int reftable_record_key (struct reftable_record *rec,struct reftable_buf *dest) { -return reftable_record_vtable (rec)->key (reftable_record_data (rec), dest); +int key_len =reftable_record_vtable (rec)->key (reftable_record_data (rec), dest); +if (key_len <0 ) +return REFTABLE_ENTRY_TOO_BIG_ERROR ; +return key_len; }
This here is incorrect. We don't know why the ->key() function has failed, so we shouldn't assume TOO_BIG_ERROR. We'd have to vet all the implementations of that function and then should bubble up their respective error codes.
reftable/record.c static struct reftable_record_vtable reftable_index_record_vtable = { · line 1163
Quoted 12 lines
reftable/record.c +4
-1int reftable_record_encode (struct reftable_record *rec,struct string_view dest,uint32_t hash_size) { -return reftable_record_vtable (rec)->encode (reftable_record_data (rec), +int encode_len =reftable_record_vtable (rec)->encode (reftable_record_data (rec), dest, hash_size); +if (encode_len <0 ) +return REFTABLE_ENTRY_TOO_BIG_ERROR ; +return encode_len; } int reftable_record_copy_from(struct reftable_record *rec,
Same remark here.
Thanks!
Patrick
Quoted 5 lines · Meet Soni
Previously,
block_writer_add()used to return generic -1, which forced an assumption about the error type.Replace generic -1 returns in
block_writer_add()and related functions with defined error codes.
What's missing from this proposed log message is an audit of the callers to tell readers that this change is safe and expected by the callers. IOW, are there callers that start to behave differently when they see ENTRY_TOO_BIG instead of -1, for example?
Quoted 14 lines
Signed-off-by Meet Soni <[email protected]>---
This patch attempts to avoid making an assumption regarding error codes returned byblock_writer_add().reftable/block.c | 9 +++++---- reftable/record.c | 16 +++++++++++----- reftable/writer.c | 8 +------- 3 files changed, 17 insertions(+), 16 deletions(-)reftable/block.c +0
-0
reftable/block.c static int block_writer_register_restart(struct block_writer *w, int n, · line 52
Quoted 5 lines
reftable/block.c +1
-1@@ -49,7 +49,7 @@ static int block_writer_register_restart(struct block_writer *w, int n,if (is_restart) rlen++;if (2 +3 * rlen + n > w->block_size - w->next) -return -1 ; +return REFTABLE_ENTRY_TOO_BIG_ERROR ;
So this makes block_writer_register_restart() to return -11 instead of -1; the sole caller of the function is block_writer_add() that begins like so:
/* Adds the reftable_record to the block. Returns -1 if it does not fit, 0 on
success. Returns REFTABLE_API_ERROR if attempting to write a record with empty key. */
int block_writer_add(struct block_writer *w, struct reftable_record *rec) {
Needless to say, the comment before the function needs to be adjusted together with the above hunk (and others). But more importantly, are existing callers of this function now expected to adjust to the change in behaviour when they receive the return value of this function? It used to be sufficient for them to deal with -1, 0 or API_ERROR, but now they are required to handle other errors (like the one that comes back from reftable_encode_key(). Do they already handle these new error codes just fine? Have you traced the code paths to see how they react to them?
reftable/block.c int block_writer_add(struct block_writer *w, struct reftable_record *rec) · line 120
Quoted 8 lines
reftable/block.c +2
-1@@ -115,8 +115,9 @@ int block_writer_add(struct block_writer *w, struct reftable_record *rec)int err; err =reftable_record_key (rec, &w->scratch); -if (err <0 ) +if (err <0 ) { goto done; + }
This is unwarranted, isn't it?
reftable/block.c int block_writer_add(struct block_writer *w, struct reftable_record *rec) · line 132
Quoted 8 lines
reftable/block.c +1
-1@@ -126,14 +127,14 @@ int block_writer_add(struct block_writer *w, struct reftable_record *rec) n =reftable_encode_key (&is_restart, out, last, w->scratch,reftable_record_val_type (rec));if (n <0 ) { - err =-1 ; + err = n; goto done; }
Here, block_writer_add() starts returning new error codes to its callers that it never returned.
reftable/block.c int block_writer_add(struct block_writer *w, struct reftable_record *rec) · line 139
Quoted 8 lines
reftable/block.c +1
-1string_view_consume (&out, n); n =reftable_record_encode (rec, out, w->hash_size);if (n <0 ) { - err =-1 ; + err = n; goto done; }
Ditto.
Note that I am not saying that it is a bad idea to make the error codes more specific so that the callers can tell them apart. I am only saying that the patch that makes such a change must also make sure that the callers are prepared to handle error coes that they have never seen from the current callee.
The same applies to the remainder of the patch.
Thanks.
PATCH v2 1/1 reftable: return proper error code from block_writer_add()
In reply to Meet SoniPreviously, block_writer_add() used to return generic -1, which forced an assumption about the error type. Replace these generic -1 returns in block_writer_add() and related functions with defined error codes.
Reviewed all call sites to ensure they check for nonzero error returns rather than strictly -1, confirming that this change is safe.
---
This patch attempts to avoid making an assumption regarding error codes returned by block_writer_add().
Changes since v1:
- Update commit message to specify safe usage of the patch.
- Update function doc comment.
- Propagate errors to ->key() and ->encode() functions instead of making any assumptions.
Dropped Han-Wen Nienhuys <[email protected]> from CC, as the email doesn't exist. Range-diff against v1:
1: 10d8bbeebc < -: ---------- reftable: return proper error code from block_writer_add() -: ---------- > 1: 7cdc7ce0ce reftable: return proper error code from block_writer_add()
reftable/block.c | 12 +++++------ reftable/block.h | 2 +- reftable/record.c | 53 +++++++++++++++++++++-------------------------- reftable/writer.c | 11 ++-------- 4 files changed, 33 insertions(+), 45 deletions(-)
reftable/block.c +6 -6
@@ -49,7 +49,7 @@ static int block_writer_register_restart(struct block_writer *w, int n,if (is_restart) rlen++;if (2 +3 * rlen + n > w->block_size - w->next) -return -1 ; +return REFTABLE_ENTRY_TOO_BIG_ERROR ;if (is_restart) {REFTABLE_ALLOC_GROW_OR_NULL (w->restarts, w->restart_len +1 , w->restart_cap);
@@ -97,9 +97,9 @@ uint8_t block_writer_type(struct block_writer *bw)return bw->block[bw->header_off]; } -/* Adds the reftable_record to the block. Returns -1 if it does not fit, 0 on -success. Returns REFTABLE_API_ERROR if attempting to write a record with -empty key. */ +/* Adds the reftable_record to the block. Returns 0 on success and +* appropriate error codes on failure. +*/ int block_writer_add (struct block_writer *w,struct reftable_record *rec) {struct reftable_buf empty =REFTABLE_BUF_INIT ;
@@ -126,14 +126,14 @@ int block_writer_add(struct block_writer *w, struct reftable_record *rec) n =reftable_encode_key (&is_restart, out, last, w->scratch,reftable_record_val_type (rec));if (n <0 ) { - err =-1 ; + err = n; goto done; }string_view_consume (&out, n); n =reftable_record_encode (rec, out, w->hash_size);if (n <0 ) { - err =-1 ; + err = n; goto done; }string_view_consume (&out, n);
reftable/block.h +1 -1
@@ -53,7 +53,7 @@ int block_writer_init(struct block_writer *bw, uint8_t typ, uint8_t *block,/* returns the block type (eg. 'r' for ref records. */ uint8_t block_writer_type (struct block_writer *bw); -/* appends the record, or -1 if it doesn't fit. */ +/* attempts to append the record. returns 0 on success or error code on failure. */ int block_writer_add (struct block_writer *w,struct reftable_record *rec);/* appends the key restarts, and compress the block if necessary. */
reftable/record.c +24 -29
149 lines not on this page · raw message
reftable/writer.c +2 -9
@@ -327,18 +327,11 @@ static int writer_add_record(struct reftable_writer *w, goto done;/* -* Try to add the record to the writer again. If this still fails then -* the record does not fit into the block size. -* -* TODO: it would be great to have `block_writer_add()` return proper -* error codes so that we don't have to second-guess the failure -* mode here. +* Try to add the record to the writer again. */ err =block_writer_add (w->block_writer, rec); -if (err) { - err =REFTABLE_ENTRY_TOO_BIG_ERROR ; +if (err) goto done; - } done:return err;
Signature · 1 line
2.34.1
Quoted 16 lines · Meet Soni
reftable/block.c +1
-1@@ -49,7 +49,7 @@ static int block_writer_register_restart(struct block_writer *w, int n,if (is_restart) rlen++;if (2 +3 * rlen + n > w->block_size - w->next) -return -1 ; +return REFTABLE_ENTRY_TOO_BIG_ERROR ;if (is_restart) {REFTABLE_ALLOC_GROW_OR_NULL (w->restarts, w->restart_len +1 , w->restart_cap);@@ -97,9 +97,9 @@ uint8_t block_writer_type(struct block_writer *bw)return bw->block[bw->header_off]; }
reftable/block.c uint8_t block_writer_type(struct block_writer *bw) · line 105
Quoted 10 lines
reftable/block.c +3
-3-/* Adds the reftable_record to the block. Returns -1 if it does not fit, 0 on -success. Returns REFTABLE_API_ERROR if attempting to write a record with -empty key. */ +/* Adds the reftable_record to the block. Returns 0 on success and +* appropriate error codes on failure. +*/ int block_writer_add (struct block_writer *w,struct reftable_record *rec) {struct reftable_buf empty =REFTABLE_BUF_INIT ;
I'm in favor of touching up the comment's formatting while at it, but if we do so we should use the correct style, which has the opening and closing parts on their own line:
/*
* Yadda yadda.
*/reftable/block.h int block_writer_init(struct block_writer *bw, uint8_t typ, uint8_t *block, · line 59
Quoted 13 lines
reftable/block.h +1
-1@@ -53,7 +53,7 @@ int block_writer_init(struct block_writer *bw, uint8_t typ, uint8_t *block,/* returns the block type (eg. 'r' for ref records. */ uint8_t block_writer_type (struct block_writer *bw); -/* appends the record, or -1 if it doesn't fit. */ +/* attempts to append the record. returns 0 on success or error code on failure. */ int block_writer_add (struct block_writer *w,struct reftable_record *rec);/* appends the key restarts, and compress the block if necessary. */
We might also touch up this comment to start with an upper-case "A" while at it.
Quoted 15 lines
reftable/record.c +0
-0reftable/writer.c +0
-3@@ -327,18 +327,11 @@ static int writer_add_record(struct reftable_writer *w, goto done;/* -* Try to add the record to the writer again. If this still fails then -* the record does not fit into the block size. -* TODO: it would be great to have `block_writer_add()` return proper
reftable/writer.c static int writer_add_record(struct reftable_writer *w, · line 340
Quoted 10 lines
reftable/writer.c +2
-5-* error codes so that we don't have to second-guess the failure -* mode here. +* Try to add the record to the writer again. */ err =block_writer_add (w->block_writer, rec); -if (err) { - err =REFTABLE_ENTRY_TOO_BIG_ERROR ; +if (err) goto done; - }
Let's not drop the second sentence of the comment, as it is important to give context. Also, let's take a step back here and figure out what this function is doing:
1. We compute the record data and try to append it to the current block. If this succeeds, we can return immediately and are done.
2. If appending to the current block fails we assume that we have failed because the block is full. This is because reftable blocks have a specific maximum length that we cannot exceed. We thus flush the block and start writing a new one.
3. We now try to add the same record to the new block again and hope that we can now write the record successfully.
The important part that the TODO comment refers to is in (2), indicated by "assume": we don't actually check what the error is that we've got from block_writer_add(), but simply pretend as if it was REFTABLE_ENTRY_TOO_BIG_ERROR. This means that we'd even re-try writing the record in case we had for example a memory allocation failure, or an I/O error, and that is plain wrong.
With your changes we have now started to plumb proper errors through from block_writer_add(). But that doesn't mean we can just drop the comment and bubble up the error. Instead, we should also be adapting the code in
(2) to do the right thing: we shouldn't _assume_ that the current block is full, but instead check the error code returned by the first call to block_writer_add():
- If it is REFTABLE_ENTRY_TOO_BIG_ERROR we indeed should flush the current block and try to write a new one.
- Otherwise we bail out and bubble up the error.
And once we do that, it is fine to remove the comment indeed. It's somewhat funny because from my point of view the comment is in the wrong
spot: it does correctly point out that _this_ particular callsite is doing the wrong thing, but it didn't mention that the other callsite also has the same problem. And that other callsite is more important from my perspective.
So I'd recommend to split up this commit into two commits:
- The first commit prepares all transitively called functions as you already do.
- The second commit adapts "reftable/writer.c" and fixes both callsites of block_writer_add() to do proper error handling.
Patrick
v3 cover reftable: return proper error codes from block_writer_add
In reply to Meet SoniThis patch attempts to avoid making an assumption regarding error codes returned by block_writer_add().
Changes since v2:
- Split the commit into two to separate transitively called function updates from writer call-site adaptations
- Made formatting improvements in comments and code for better readability.
- Modified the writer logic to flush and retry only when a specific error occurs, while other errors are propagated as-is.
Meet Soni (2):
reftable: propagate specific error codes in block_writer_add()
reftable: adapt writer code to propagate block_writer_add() errors
reftable/block.c | 13 ++++++------ reftable/block.h | 2 +- reftable/record.c | 53 +++++++++++++++++++++-------------------------- reftable/writer.c | 30 ++++++++++++++++----------- 4 files changed, 50 insertions(+), 48 deletions(-)
Range-diff against v2:
1: 7cdc7ce0ce ! 1: 6ab35d569c reftable: return proper error code from block_writer_add()
@@ Metadata
Author: Meet Soni <[email protected]>
## Commit message ##
- reftable: return proper error code from block_writer_add()
+ reftable: propagate specific error codes in block_writer_add()
- Previously, block_writer_add() used to return generic -1, which forced
- an assumption about the error type. Replace these generic -1 returns in
- block_writer_add() and related functions with defined error codes.
+ Previously, functions block_writer_add() and related functions returned
+ -1 when the record did not fit, forcing the caller to assume that any
+ failure meant the entry was too big. Replace these generic -1 returns
+ with defined error codes.
- Reviewed all call sites to ensure they check for nonzero error returns
- rather than strictly -1, confirming that this change is safe.
+ This prepares the codebase for finer-grained error handling so that
+ callers can distinguish between a block-full condition and other errors.
@@ reftable/block.c: uint8_t block_writer_type(struct block_writer *bw)
-/* Adds the reftable_record to the block. Returns -1 if it does not fit, 0 on
- success. Returns REFTABLE_API_ERROR if attempting to write a record with
- empty key. */
-+/* Adds the reftable_record to the block. Returns 0 on success and ++/*
++ * Adds the reftable_record to the block. Returns 0 on success and
+ * appropriate error codes on failure.
+ */
int block_writer_add(struct block_writer *w, struct reftable_record *rec)
@@ reftable/block.h: int block_writer_init(struct block_writer *bw, uint8_t typ, ui
uint8_t block_writer_type(struct block_writer *bw);
-/* appends the record, or -1 if it doesn't fit. */
-+/* attempts to append the record. returns 0 on success or error code on failure. */ ++/* Attempts to append the record. Returns 0 on success or error code on failure. */
int block_writer_add(struct block_writer *w, struct reftable_record *rec);
/* appends the key restarts, and compress the block if necessary. */
@@ reftable/record.c: static int reftable_log_record_encode(const void *rec, struct
string_view_consume(&s, n);
return start.len - s.len;
-
- ## reftable/writer.c ##
-@@ reftable/writer.c: static int writer_add_record(struct reftable_writer *w,
- goto done;
-
- /*
-- * Try to add the record to the writer again. If this still fails then
-- * the record does not fit into the block size.
-- *
-- * TODO: it would be great to have block_writer_add() return proper
-- * error codes so that we don't have to second-guess the failure
-- * mode here.
-+ * Try to add the record to the writer again.
- */
- err = block_writer_add(w->block_writer, rec);
-- if (err) {
-- err = REFTABLE_ENTRY_TOO_BIG_ERROR;
-+ if (err)
- goto done;
-- }
-
- done:
- return err;
-: ---------- > 2: a54d440dd3 reftable: adapt writer code to propagate block_writer_add() errors
Signature · 1 line
2.34.1
PATCH v3 1/2 reftable: propagate specific error codes in block_writer_add()
Previously, functions block_writer_add() and related functions returned -1 when the record did not fit, forcing the caller to assume that any failure meant the entry was too big. Replace these generic -1 returns with defined error codes.
This prepares the codebase for finer-grained error handling so that callers can distinguish between a block-full condition and other errors.
--- reftable/block.c | 13 ++++++------ reftable/block.h | 2 +- reftable/record.c | 53 +++++++++++++++++++++-------------------------- 3 files changed, 32 insertions(+), 36 deletions(-)
reftable/block.c +7 -6
@@ -49,7 +49,7 @@ static int block_writer_register_restart(struct block_writer *w, int n,if (is_restart) rlen++;if (2 +3 * rlen + n > w->block_size - w->next) -return -1 ; +return REFTABLE_ENTRY_TOO_BIG_ERROR ;if (is_restart) {REFTABLE_ALLOC_GROW_OR_NULL (w->restarts, w->restart_len +1 , w->restart_cap);
@@ -97,9 +97,10 @@ uint8_t block_writer_type(struct block_writer *bw)return bw->block[bw->header_off]; } -/* Adds the reftable_record to the block. Returns -1 if it does not fit, 0 on -success. Returns REFTABLE_API_ERROR if attempting to write a record with -empty key. */ +/* +* Adds the reftable_record to the block. Returns 0 on success and +* appropriate error codes on failure. +*/ int block_writer_add (struct block_writer *w,struct reftable_record *rec) {struct reftable_buf empty =REFTABLE_BUF_INIT ;
@@ -126,14 +127,14 @@ int block_writer_add(struct block_writer *w, struct reftable_record *rec) n =reftable_encode_key (&is_restart, out, last, w->scratch,reftable_record_val_type (rec));if (n <0 ) { - err =-1 ; + err = n; goto done; }string_view_consume (&out, n); n =reftable_record_encode (rec, out, w->hash_size);if (n <0 ) { - err =-1 ; + err = n; goto done; }string_view_consume (&out, n);
reftable/block.h +1 -1
@@ -53,7 +53,7 @@ int block_writer_init(struct block_writer *bw, uint8_t typ, uint8_t *block,/* returns the block type (eg. 'r' for ref records. */ uint8_t block_writer_type (struct block_writer *bw); -/* appends the record, or -1 if it doesn't fit. */ +/* Attempts to append the record. Returns 0 on success or error code on failure. */ int block_writer_add (struct block_writer *w,struct reftable_record *rec);/* appends the key restarts, and compress the block if necessary. */
reftable/record.c +24 -29
149 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH v3 2/2 reftable: adapt writer code to propagate block_writer_add() errors
In reply to Meet SoniPreviously, writer_add_record() and write_object_record() would flush the current block and retry appending the record whenever block_writer_add() returned any nonzero error. This forced an assumption that every failure meant the block was full, even when errors such as memory allocation or I/O failures occurred.
Update the writer code to inspect the error code returned by block_writer_add() and only flush and reinitialize the writer when the error is REFTABLE_ENTRY_TOO_BIG_ERROR. For any other error, immediately propagate it.
All call sites now handle various error codes returned by block_writer_add().
--- reftable/writer.c | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-)
reftable/writer.c +18 -12
@@ -310,11 +310,12 @@ static int writer_add_record(struct reftable_writer *w,* done. Otherwise the block writer may have hit the block size limit * and needs to be flushed. */ -if (!block_writer_add (w->block_writer, rec)) { - err =0 ; + err =block_writer_add (w->block_writer, rec); +if (err ==0 ) goto done; - } +if (err !=REFTABLE_ENTRY_TOO_BIG_ERROR ) + goto done;/* * The current block is full, so we need to flush and reinitialize the * writer to start writing the next block.
@@ -327,18 +328,11 @@ static int writer_add_record(struct reftable_writer *w, goto done;/* -* Try to add the record to the writer again. If this still fails then -* the record does not fit into the block size. -* -* TODO: it would be great to have `block_writer_add()` return proper -* error codes so that we don't have to second-guess the failure -* mode here. +* Try to add the record to the writer again. */ err =block_writer_add (w->block_writer, rec); -if (err) { - err =REFTABLE_ENTRY_TOO_BIG_ERROR ; +if (err) goto done; - } done:return err;
@@ -625,10 +619,22 @@ static void write_object_record(void *void_arg, void *key)if (arg->err <0 ) goto done; +/* +* Try to add the record to the writer. If this succeeds then we're +* done. Otherwise the block writer may have hit the block size limit +* and needs to be flushed. +*/ arg->err =block_writer_add (arg->w->block_writer, &rec);if (arg->err ==0 ) goto done; +if (arg->err !=REFTABLE_ENTRY_TOO_BIG_ERROR ) + goto done; + +/* +* The current block is full, so we need to flush and reinitialize the +* writer to start writing the next block. +*/ arg->err =writer_flush_block (arg->w);if (arg->err <0 ) goto done;
Signature · 1 line
2.34.1
Re: [PATCH v3 2/2] reftable: adapt writer code to propagate block_writer_add() errors
Quoted 5 lines · Meet Soni
reftable/writer.c +0
-0
reftable/writer.c static int writer_add_record(struct reftable_writer *w, · line 316
Quoted 9 lines
reftable/writer.c +2
-3@@ -310,11 +310,12 @@ static int writer_add_record(struct reftable_writer *w,* done. Otherwise the block writer may have hit the block size limit * and needs to be flushed. */ -if (!block_writer_add (w->block_writer, rec)) { - err =0 ; + err =block_writer_add (w->block_writer, rec); +if (err ==0 ) goto done; - }
Style: we'd typically say if (!err) here, even though I see that we have explicit comparisons with 0 elsewhere in this file, too. So I guess ultimately this is okay.
reftable/writer.c static int writer_add_record(struct reftable_writer *w, · line 332
Quoted 12 lines
reftable/writer.c +1
-6@@ -327,18 +328,11 @@ static int writer_add_record(struct reftable_writer *w, goto done;/* -* Try to add the record to the writer again. If this still fails then -* the record does not fit into the block size. -* -* TODO: it would be great to have `block_writer_add()` return proper -* error codes so that we don't have to second-guess the failure -* mode here. +* Try to add the record to the writer again. */
My comment on the preceding version still applies here: the second sentence (the one starting with "If this still fails...") should be retained.
Quoted 14 lines
reftable/writer.c +2
-3err =block_writer_add (w->block_writer, rec); -if (err) { - err =REFTABLE_ENTRY_TOO_BIG_ERROR ; +if (err) goto done; - } done:return err;@@ -625,10 +619,22 @@ static void write_object_record(void *void_arg, void *key)if (arg->err <0 ) goto done; +/*
reftable/writer.c static void write_object_record(void *void_arg, void *key) · line 632
Quoted 10 lines
reftable/writer.c +6
-0+* Try to add the record to the writer. If this succeeds then we're +* done. Otherwise the block writer may have hit the block size limit +* and needs to be flushed. +*/ arg->err =block_writer_add (arg->w->block_writer, &rec);if (arg->err ==0 ) goto done; +if (arg->err !=REFTABLE_ENTRY_TOO_BIG_ERROR ) + goto done;
Good catch that there is another such pattern!
reftable/writer.c static void write_object_record(void *void_arg, void *key) · line 640
Quoted 7 lines
reftable/writer.c +4
-0+/* +* The current block is full, so we need to flush and reinitialize the +* writer to start writing the next block. +*/ arg->err =writer_flush_block (arg->w);if (arg->err <0 ) goto done;
But there is another case further down where we do block_writer_add() and then re-try in case the write fails. This one is a bit more curious:
if the write fails, we don't create a new block -- after all we have just created one. Instead, we reset the record's offset length to zero before retrying.
I _think_ that this is done because we know that when resetting the offset we would write less data to the block, as can be seen in reftable_obj_record_encode(). But I'm honestly not quite sure here as I haven't yet done a deep dive into object records -- after all, we don't even really use them in Git.
In any case, I think that this callsite also needs adjustment and warrants a comment. And if so, all changes to write_object_record() should probably go into a separate commit, as well.
Thanks!
Patrick
Re: [PATCH v3 2/2] reftable: adapt writer code to propagate block_writer_add() errors
Quoted 21 lines · Patrick Steinhardt
reftable/writer.c static void write_object_record(void *void_arg, void *key) · line 640
Quoted 8 lines
reftable/writer.c +4
-0+/* +* The current block is full, so we need to flush and reinitialize the +* writer to start writing the next block. +*/ arg->err =writer_flush_block (arg->w);if (arg->err <0 ) goto done;But there is another case further down where we do
block_writer_add()and then re-try in case the write fails. This one is a bit more curious:
if the write fails, we don't create a new block -- after all we have just created one. Instead, we reset the record's offset length to zero before retrying.I _think_ that this is done because we know that when resetting the offset we would write less data to the block, as can be seen in
reftable_obj_record_encode(). But I'm honestly not quite sure here as I haven't yet done a deep dive into object records -- after all, we don't even really use them in Git.
Quoted 4 lines
In any case, I think that this callsite also needs adjustment and warrants a comment. And if so, all changes to
write_object_record()should probably go into a separate commit, as well.
Regarding the callsite in write_object_record() where we reset the record's offset length to zero before retrying: my changes currently follow the same principle.
- If block_writer_add() returns an error other than REFTABLE_ENTRY_TOO_BIG_ERROR, we simply return.
- For REFTABLE_ENTRY_TOO_BIG_ERROR, we flush the block and retry.
- If that fails, we reset the record's offset length to zero and then retry.
I'm not sure what adjustments or additional comments you are referring to. Could you please clarify what changes you expect at this callsite?
Thanks!
Meet
Re: [PATCH v3 2/2] reftable: adapt writer code to propagate block_writer_add() errors
Quoted 22 lines · Meet Soni
Quoted 21 lines · Patrick Steinhardt
Review
Quoted 8 lines
(quoted diff) +4
-0+ /* + * The current block is full, so we need to flush and reinitialize the + * writer to start writing the next block. + */ arg->err = writer_flush_block(arg->w); if (arg->err < 0) goto done;But there is another case further down where we do
block_writer_add()and then re-try in case the write fails. This one is a bit more curious:
if the write fails, we don't create a new block -- after all we have just created one. Instead, we reset the record's offset length to zero before retrying.I _think_ that this is done because we know that when resetting the offset we would write less data to the block, as can be seen in
reftable_obj_record_encode(). But I'm honestly not quite sure here as I haven't yet done a deep dive into object records -- after all, we don't even really use them in Git.
Quoted 4 lines
Quoted 4 lines
In any case, I think that this callsite also needs adjustment and warrants a comment. And if so, all changes to
write_object_record()should probably go into a separate commit, as well.
Sorry for taking so long to respond.
Quoted 11 lines
Regarding the callsite in
write_object_record()where we reset the record's offset length to zero before retrying: my changes currently follow the same principle.- If
block_writer_add()returns an error other thanREFTABLE_ENTRY_TOO_BIG_ERROR, we simply return.- For
REFTABLE_ENTRY_TOO_BIG_ERROR, we flush the block and retry.- If that fails, we reset the record's offset length to zero and then retry.
It's this last step that also needs to be adapted to check for REFTABLE_ENTRY_TOO_BIG_ERROR, because the intent here is the exact same:
if writing the object record failed even in a completely new block then we reset the object's offset and try a third time. But same as for the first time, we don't check whether we get REFTABLE_ENTRY_TOO_BIG_ERROR here and thus we might be failing and retrying even in unintended cases.
Quoted 2 lines
I'm not sure what adjustments or additional comments you are referring to. Could you please clarify what changes you expect at this callsite?
So overall we'd add the check to both callsites, like in the below patch.
Patrick
reftable/writer.c +4 -0
@@ -628,6 +628,8 @@ static void write_object_record(void *void_arg, void *key) arg->err =block_writer_add (arg->w->block_writer, &rec);if (arg->err ==0 ) goto done; +if (arg->err !=REFTABLE_ENTRY_TOO_BIG_ERROR ) + goto done; arg->err =writer_flush_block (arg->w);if (arg->err <0 )
@@ -640,6 +642,8 @@ static void write_object_record(void *void_arg, void *key) arg->err =block_writer_add (arg->w->block_writer, &rec);if (arg->err ==0 ) goto done; +if (arg->err !=REFTABLE_ENTRY_TOO_BIG_ERROR ) + goto done; rec.u.obj.offset_len =0 ; arg->err =block_writer_add (arg->w->block_writer, &rec);
v4 cover reftable: return proper error codes from block_writer_add
In reply to Meet SoniThis patch attempts to avoid making an assumption regarding error codes returned by block_writer_add().
Changes since v3:
- split commit based on the functions it alters
- add comment back that was earlier removed.
Meet Soni (3):
reftable: propagate specific error codes in block_writer_add()
reftable: adapt writer_add_record() to propagate block_writer_add()
errors
reftable: adapt write_object_record() to propagate block_writer_add()
errors
reftable/block.c | 13 ++++++------ reftable/block.h | 2 +- reftable/record.c | 53 +++++++++++++++++++++-------------------------- reftable/writer.c | 27 +++++++++++++++--------- 4 files changed, 49 insertions(+), 46 deletions(-)
Range-diff against v3:
1: 6ab35d569c = 1: 6ab35d569c reftable: propagate specific error codes in block_writer_add() 2: a54d440dd3 ! 2: 7f0bdc27e1 reftable: adapt writer code to propagate block_writer_add() errors
@@ Metadata
Author: Meet Soni <[email protected]>
## Commit message ##
- reftable: adapt writer code to propagate block_writer_add() errors
+ reftable: adapt writer_add_record() to propagate block_writer_add() errors
- Previously, writer_add_record() and write_object_record() would flush the
- current block and retry appending the record whenever block_writer_add()
- returned any nonzero error. This forced an assumption that every failure
- meant the block was full, even when errors such as memory allocation or
- I/O failures occurred.
+ Previously, writer_add_record() would flush the current block and
+ retry appending the record whenever block_writer_add() returned any
+ nonzero error. This forced an assumption that every failure meant
+ the block was full, even when errors such as memory allocation or I/O
+ failures occurred.
- Update the writer code to inspect the error code returned by
- block_writer_add() and only flush and reinitialize the writer when the
- error is REFTABLE_ENTRY_TOO_BIG_ERROR. For any other error, immediately
- propagate it.
-
- All call sites now handle various error codes returned by
- block_writer_add().
+ Update the writer_add_record() to inspect the error code returned by
+ block_writer_add() and only flush and reinitialize the writer when the
+ error is REFTABLE_ENTRY_TOO_BIG_ERROR. For any other error, immediately
+ propagate it.
@@ reftable/writer.c: static int writer_add_record(struct reftable_writer *w,
* The current block is full, so we need to flush and reinitialize the
* writer to start writing the next block.
@@ reftable/writer.c: static int writer_add_record(struct reftable_writer *w,
- goto done;
-
/*
-- * Try to add the record to the writer again. If this still fails then
-- * the record does not fit into the block size.
+ * Try to add the record to the writer again. If this still fails then
+ * the record does not fit into the block size.
- *
- * TODO: it would be great to have block_writer_add() return proper
- * error codes so that we don't have to second-guess the failure
- * mode here.
-+ * Try to add the record to the writer again.
*/
err = block_writer_add(w->block_writer, rec);
- if (err) {
@@ reftable/writer.c: static int writer_add_record(struct reftable_writer *w,
done:
return err;
-@@ reftable/writer.c: static void write_object_record(void *void_arg, void *key)
- if (arg->err < 0)
- goto done;
-
-+ /*
-+ * Try to add the record to the writer. If this succeeds then we're
-+ * done. Otherwise the block writer may have hit the block size limit
-+ * and needs to be flushed.
-+ */
- arg->err = block_writer_add(arg->w->block_writer, &rec);
- if (arg->err == 0)
- goto done;
-
-+ if (arg->err != REFTABLE_ENTRY_TOO_BIG_ERROR)
-+ goto done;
-+
-+ /*
-+ * The current block is full, so we need to flush and reinitialize the
-+ * writer to start writing the next block.
-+ */
- arg->err = writer_flush_block(arg->w);
- if (arg->err < 0)
- goto done;
-: ---------- > 3: 480ac27797 reftable: adapt write_object_record() to propagate block_writer_add() errors
Signature · 1 line
2.34.1
PATCH v4 1/3 reftable: propagate specific error codes in block_writer_add()
Previously, functions block_writer_add() and related functions returned -1 when the record did not fit, forcing the caller to assume that any failure meant the entry was too big. Replace these generic -1 returns with defined error codes.
This prepares the codebase for finer-grained error handling so that callers can distinguish between a block-full condition and other errors.
--- reftable/block.c | 13 ++++++------ reftable/block.h | 2 +- reftable/record.c | 53 +++++++++++++++++++++-------------------------- 3 files changed, 32 insertions(+), 36 deletions(-)
reftable/block.c +7 -6
@@ -49,7 +49,7 @@ static int block_writer_register_restart(struct block_writer *w, int n,if (is_restart) rlen++;if (2 +3 * rlen + n > w->block_size - w->next) -return -1 ; +return REFTABLE_ENTRY_TOO_BIG_ERROR ;if (is_restart) {REFTABLE_ALLOC_GROW_OR_NULL (w->restarts, w->restart_len +1 , w->restart_cap);
@@ -97,9 +97,10 @@ uint8_t block_writer_type(struct block_writer *bw)return bw->block[bw->header_off]; } -/* Adds the reftable_record to the block. Returns -1 if it does not fit, 0 on -success. Returns REFTABLE_API_ERROR if attempting to write a record with -empty key. */ +/* +* Adds the reftable_record to the block. Returns 0 on success and +* appropriate error codes on failure. +*/ int block_writer_add (struct block_writer *w,struct reftable_record *rec) {struct reftable_buf empty =REFTABLE_BUF_INIT ;
@@ -126,14 +127,14 @@ int block_writer_add(struct block_writer *w, struct reftable_record *rec) n =reftable_encode_key (&is_restart, out, last, w->scratch,reftable_record_val_type (rec));if (n <0 ) { - err =-1 ; + err = n; goto done; }string_view_consume (&out, n); n =reftable_record_encode (rec, out, w->hash_size);if (n <0 ) { - err =-1 ; + err = n; goto done; }string_view_consume (&out, n);
reftable/block.h +1 -1
@@ -53,7 +53,7 @@ int block_writer_init(struct block_writer *bw, uint8_t typ, uint8_t *block,/* returns the block type (eg. 'r' for ref records. */ uint8_t block_writer_type (struct block_writer *bw); -/* appends the record, or -1 if it doesn't fit. */ +/* Attempts to append the record. Returns 0 on success or error code on failure. */ int block_writer_add (struct block_writer *w,struct reftable_record *rec);/* appends the key restarts, and compress the block if necessary. */
reftable/record.c +24 -29
149 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH v4 2/3 reftable: adapt writer_add_record() to propagate block_writer_add() errors
In reply to Meet Soni Previously, writer_add_record() would flush the current block and retry appending the record whenever block_writer_add() returned any nonzero error. This forced an assumption that every failure meant the block was full, even when errors such as memory allocation or I/O failures occurred.
Update the writer_add_record() to inspect the error code returned by block_writer_add() and only flush and reinitialize the writer when the error is REFTABLE_ENTRY_TOO_BIG_ERROR. For any other error, immediately propagate it.
--- reftable/writer.c | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-)
reftable/writer.c +5 -10
@@ -310,11 +310,12 @@ static int writer_add_record(struct reftable_writer *w,* done. Otherwise the block writer may have hit the block size limit * and needs to be flushed. */ -if (!block_writer_add (w->block_writer, rec)) { - err =0 ; + err =block_writer_add (w->block_writer, rec); +if (err ==0 ) goto done; - } +if (err !=REFTABLE_ENTRY_TOO_BIG_ERROR ) + goto done;/* * The current block is full, so we need to flush and reinitialize the * writer to start writing the next block.
@@ -329,16 +330,10 @@ static int writer_add_record(struct reftable_writer *w,/* * Try to add the record to the writer again. If this still fails then * the record does not fit into the block size. -* -* TODO: it would be great to have `block_writer_add()` return proper -* error codes so that we don't have to second-guess the failure -* mode here. */ err =block_writer_add (w->block_writer, rec); -if (err) { - err =REFTABLE_ENTRY_TOO_BIG_ERROR ; +if (err) goto done; - } done:return err;
Signature · 1 line
2.34.1
PATCH v4 3/3 reftable: adapt write_object_record() to propagate block_writer_add() errors
In reply to Meet Soni Previously, write_object_record() would flush the current block and retry appending the record whenever block_writer_add() returned any nonzero error. This forced an assumption that every failure meant the block was full, even when errors such as memory allocation or I/O failures occurred.
Update the write_object_record() to inspect the error code returned by block_writer_add() and only flush and reinitialize the writer when the error is REFTABLE_ENTRY_TOO_BIG_ERROR. For any other error, immediately propagate it.
All call sites now handle various error codes returned by block_writer_add().
--- reftable/writer.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
reftable/writer.c +12 -0
@@ -620,10 +620,22 @@ static void write_object_record(void *void_arg, void *key)if (arg->err <0 ) goto done; +/* +* Try to add the record to the writer. If this succeeds then we're +* done. Otherwise the block writer may have hit the block size limit +* and needs to be flushed. +*/ arg->err =block_writer_add (arg->w->block_writer, &rec);if (arg->err ==0 ) goto done; +if (arg->err !=REFTABLE_ENTRY_TOO_BIG_ERROR ) + goto done; + +/* +* The current block is full, so we need to flush and reinitialize the +* writer to start writing the next block. +*/ arg->err =writer_flush_block (arg->w);if (arg->err <0 ) goto done;
Signature · 1 line
2.34.1
v5 cover reftable: return proper error codes from block_writer_add
In reply to Meet SoniThis patch series attempts to avoid making an assumption regarding error codes returned by block_writer_add().
Changes since v4:
- update commit message.
- add documentation comment.
Meet Soni (3):
reftable: propagate specific error codes in block_writer_add()
reftable: adapt writer_add_record() to propagate block_writer_add()
errors
reftable: adapt write_object_record() to propagate block_writer_add()
errors
reftable/block.c | 13 ++++++------ reftable/block.h | 2 +- reftable/record.c | 53 +++++++++++++++++++++-------------------------- reftable/writer.c | 34 +++++++++++++++++++++--------- 4 files changed, 56 insertions(+), 46 deletions(-)
Range-diff against v4:
1: 6ab35d569c = 1: 6ab35d569c reftable: propagate specific error codes in block_writer_add() 2: 7f0bdc27e1 ! 2: 873a991a2c reftable: adapt writer_add_record() to propagate block_writer_add() errors
@@ Metadata
## Commit message ##
reftable: adapt writer_add_record() to propagate block_writer_add() errors
- Previously, writer_add_record() would flush the current block and
- retry appending the record whenever block_writer_add() returned any
- nonzero error. This forced an assumption that every failure meant
- the block was full, even when errors such as memory allocation or I/O
- failures occurred.
+ Previously, writer_add_record() would flush the current block and retry
+ appending the record whenever block_writer_add() returned any nonzero
+ error. This forced an assumption that every failure meant the block was
+ full, even when errors such as memory allocation or I/O failures occurred.
- Update the writer_add_record() to inspect the error code returned by
- block_writer_add() and only flush and reinitialize the writer when the
- error is REFTABLE_ENTRY_TOO_BIG_ERROR. For any other error, immediately
- propagate it.
+ Update the writer_add_record() to inspect the error code returned by
+ block_writer_add() and only flush and reinitialize the writer when the
+ error is REFTABLE_ENTRY_TOO_BIG_ERROR. For any other error, immediately
+ propagate it.
3: 480ac27797 ! 3: 1e2f7ff83f reftable: adapt write_object_record() to propagate block_writer_add() errors
@@ Metadata
## Commit message ##
reftable: adapt write_object_record() to propagate block_writer_add() errors
- Previously, write_object_record() would flush the current block and
- retry appending the record whenever block_writer_add() returned any
- nonzero error. This forced an assumption that every failure meant the
- block was full, even when errors such as memory allocation or I/O
- failures occurred.
+ Previously, write_object_record() would flush the current block and retry
+ appending the record whenever block_writer_add() returned any nonzero
+ error. This forced an assumption that every failure meant the block was
+ full, even when errors such as memory allocation or I/O failures occurred.
- Update the write_object_record() to inspect the error code returned by
- block_writer_add() and only flush and reinitialize the writer when the
- error is REFTABLE_ENTRY_TOO_BIG_ERROR. For any other error, immediately
- propagate it.
+ Update the write_object_record() to inspect the error code returned by
+ block_writer_add() and flush and reinitialize the writer iff the
+ error is REFTABLE_ENTRY_TOO_BIG_ERROR. For any other error, immediately
+ propagate it.
- All call sites now handle various error codes returned by
- block_writer_add().
+ If the flush and reinitialization still fail with
+ REFTABLE_ENTRY_TOO_BIG_ERROR, reset the record's offset length to zero
+ before a final attempt.
+
+ All call sites now handle various error codes returned by
+ block_writer_add().
@@ reftable/writer.c: static void write_object_record(void *void_arg, void *key)
arg->err = writer_flush_block(arg->w);
if (arg->err < 0)
goto done;
+@@ reftable/writer.c: static void write_object_record(void *void_arg, void *key)
+ if (arg->err < 0)
+ goto done;
+
++ /*
++ * If this still fails then we may need to reset record's offset
++ * length to reduce the data size to be written.
++ */
+ arg->err = block_writer_add(arg->w->block_writer, &rec);
+ if (arg->err == 0)
+ goto done;
+
++ if (arg->err != REFTABLE_ENTRY_TOO_BIG_ERROR)
++ goto done;
++
+ rec.u.obj.offset_len = 0;
+ arg->err = block_writer_add(arg->w->block_writer, &rec);
+
Signature · 1 line
2.34.1
PATCH v5 1/3 reftable: propagate specific error codes in block_writer_add()
Previously, functions block_writer_add() and related functions returned -1 when the record did not fit, forcing the caller to assume that any failure meant the entry was too big. Replace these generic -1 returns with defined error codes.
This prepares the codebase for finer-grained error handling so that callers can distinguish between a block-full condition and other errors.
--- reftable/block.c | 13 ++++++------ reftable/block.h | 2 +- reftable/record.c | 53 +++++++++++++++++++++-------------------------- 3 files changed, 32 insertions(+), 36 deletions(-)
reftable/block.c +7 -6
@@ -49,7 +49,7 @@ static int block_writer_register_restart(struct block_writer *w, int n,if (is_restart) rlen++;if (2 +3 * rlen + n > w->block_size - w->next) -return -1 ; +return REFTABLE_ENTRY_TOO_BIG_ERROR ;if (is_restart) {REFTABLE_ALLOC_GROW_OR_NULL (w->restarts, w->restart_len +1 , w->restart_cap);
@@ -97,9 +97,10 @@ uint8_t block_writer_type(struct block_writer *bw)return bw->block[bw->header_off]; } -/* Adds the reftable_record to the block. Returns -1 if it does not fit, 0 on -success. Returns REFTABLE_API_ERROR if attempting to write a record with -empty key. */ +/* +* Adds the reftable_record to the block. Returns 0 on success and +* appropriate error codes on failure. +*/ int block_writer_add (struct block_writer *w,struct reftable_record *rec) {struct reftable_buf empty =REFTABLE_BUF_INIT ;
@@ -126,14 +127,14 @@ int block_writer_add(struct block_writer *w, struct reftable_record *rec) n =reftable_encode_key (&is_restart, out, last, w->scratch,reftable_record_val_type (rec));if (n <0 ) { - err =-1 ; + err = n; goto done; }string_view_consume (&out, n); n =reftable_record_encode (rec, out, w->hash_size);if (n <0 ) { - err =-1 ; + err = n; goto done; }string_view_consume (&out, n);
reftable/block.h +1 -1
@@ -53,7 +53,7 @@ int block_writer_init(struct block_writer *bw, uint8_t typ, uint8_t *block,/* returns the block type (eg. 'r' for ref records. */ uint8_t block_writer_type (struct block_writer *bw); -/* appends the record, or -1 if it doesn't fit. */ +/* Attempts to append the record. Returns 0 on success or error code on failure. */ int block_writer_add (struct block_writer *w,struct reftable_record *rec);/* appends the key restarts, and compress the block if necessary. */
reftable/record.c +24 -29
149 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH v5 2/3 reftable: adapt writer_add_record() to propagate block_writer_add() errors
In reply to Meet SoniPreviously, writer_add_record() would flush the current block and retry appending the record whenever block_writer_add() returned any nonzero error. This forced an assumption that every failure meant the block was full, even when errors such as memory allocation or I/O failures occurred.
Update the writer_add_record() to inspect the error code returned by block_writer_add() and only flush and reinitialize the writer when the error is REFTABLE_ENTRY_TOO_BIG_ERROR. For any other error, immediately propagate it.
--- reftable/writer.c | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-)
reftable/writer.c +5 -10
@@ -310,11 +310,12 @@ static int writer_add_record(struct reftable_writer *w,* done. Otherwise the block writer may have hit the block size limit * and needs to be flushed. */ -if (!block_writer_add (w->block_writer, rec)) { - err =0 ; + err =block_writer_add (w->block_writer, rec); +if (err ==0 ) goto done; - } +if (err !=REFTABLE_ENTRY_TOO_BIG_ERROR ) + goto done;/* * The current block is full, so we need to flush and reinitialize the * writer to start writing the next block.
@@ -329,16 +330,10 @@ static int writer_add_record(struct reftable_writer *w,/* * Try to add the record to the writer again. If this still fails then * the record does not fit into the block size. -* -* TODO: it would be great to have `block_writer_add()` return proper -* error codes so that we don't have to second-guess the failure -* mode here. */ err =block_writer_add (w->block_writer, rec); -if (err) { - err =REFTABLE_ENTRY_TOO_BIG_ERROR ; +if (err) goto done; - } done:return err;
Signature · 1 line
2.34.1
PATCH v5 3/3 reftable: adapt write_object_record() to propagate block_writer_add() errors
In reply to Meet SoniPreviously, write_object_record() would flush the current block and retry appending the record whenever block_writer_add() returned any nonzero error. This forced an assumption that every failure meant the block was full, even when errors such as memory allocation or I/O failures occurred.
Update the write_object_record() to inspect the error code returned by block_writer_add() and flush and reinitialize the writer iff the error is REFTABLE_ENTRY_TOO_BIG_ERROR. For any other error, immediately propagate it.
If the flush and reinitialization still fail with REFTABLE_ENTRY_TOO_BIG_ERROR, reset the record's offset length to zero before a final attempt.
All call sites now handle various error codes returned by block_writer_add().
--- reftable/writer.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+)
reftable/writer.c +19 -0
@@ -620,10 +620,22 @@ static void write_object_record(void *void_arg, void *key)if (arg->err <0 ) goto done; +/* +* Try to add the record to the writer. If this succeeds then we're +* done. Otherwise the block writer may have hit the block size limit +* and needs to be flushed. +*/ arg->err =block_writer_add (arg->w->block_writer, &rec);if (arg->err ==0 ) goto done; +if (arg->err !=REFTABLE_ENTRY_TOO_BIG_ERROR ) + goto done; + +/* +* The current block is full, so we need to flush and reinitialize the +* writer to start writing the next block. +*/ arg->err =writer_flush_block (arg->w);if (arg->err <0 ) goto done;
@@ -632,10 +644,17 @@ static void write_object_record(void *void_arg, void *key)if (arg->err <0 ) goto done; +/* +* If this still fails then we may need to reset record's offset +* length to reduce the data size to be written. +*/ arg->err =block_writer_add (arg->w->block_writer, &rec);if (arg->err ==0 ) goto done; +if (arg->err !=REFTABLE_ENTRY_TOO_BIG_ERROR ) + goto done; + rec.u.obj.offset_len =0 ; arg->err =block_writer_add (arg->w->block_writer, &rec);
Signature · 1 line
2.34.1
Re: [GSoC PATCH v5 0/3] reftable: return proper error codes from block_writer_add
In reply to Meet SoniQuoted 6 lines · Meet Soni
This patch series attempts to avoid making an assumption regarding error codes returned by
block_writer_add().Changes since v4:
- update commit message.
- add documentation comment.
One additional change that isn't mentioned here is that we now check for REFTABLE_ENTRY_TOO_BIG_ERROR the second time we call block_writer_add() when writing object records, which is what my only concern was. So with that now addressed I'm happy with this patch series, thanks for working on it!
Patrick