odb: add transaction interfaces to ODB subsystem
Subsystems (2)
cover odb: add transaction interfaces to ODB subsystem
Greetings,
This series is a followup to [1] and continues iterating on the ODB transaction interfaces.
The bulk-checkin subsystem provides an interface to manage ODB transactions. Apart from {begin,end}_odb_transaction(), these functions are only used by the object-file subsystem to manage aspects of a transaction implementation specific to the files object source.
In a pluggable object database future where we could have different types of object database sources, transaction handling will have to be implemented separately per source. Thus, the primary focus of this series is to simplify the existing ODB transaction interface and provide a means to manage transactions via the ODB subsystem in an object source agnostic manner eventually.
This series is built on top of 4975ec3473b (The seventh batch, 2025-09-08) with jt/de-global-bulk-checkin merged into it at ddc0b56ad77 (bulk-checkin: use repository variable from transaction, 2025-08-22).
Thanks,
-Justin
[1]: <[email protected]>
Justin Tobler (6):
bulk-checkin: remove ODB transaction nesting builtin/update-index: end ODB transaction when --verbose is specified
bulk-checkin: drop flush_odb_transaction()
object-file: relocate ODB transaction code
object-file: update naming from bulk-checkin
odb: add transaction interface
Makefile | 1 - builtin/add.c | 7 +- builtin/unpack-objects.c | 5 +- builtin/update-index.c | 30 +-- bulk-checkin.c | 403 --------------------------------------- bulk-checkin.h | 61 ------ cache-tree.c | 13 +- meson.build | 1 - object-file.c | 400 +++++++++++++++++++++++++++++++++++++- object-file.h | 17 ++ odb.c | 10 + odb.h | 3 + read-cache.c | 11 +- 13 files changed, 461 insertions(+), 501 deletions(-) delete mode 100644 bulk-checkin.c delete mode 100644 bulk-checkin.h
Signature · 1 line
2.51.0.193.g4975ec3473b
PATCH 1/6 bulk-checkin: remove ODB transaction nesting
ODB transactions support being nested. Only the outermost {begin,end}_odb_transaction() start and finish a transaction. This is done so that certain object write codepaths that occur internally can be optimized via ODB transactions without having to worry if a transaction has already been started or not. This can make the interface a bit awkward to use, as calling {begin,end}_odb_transaction() does not guarantee that a transaction is actually started or ended.
Instead, be more explicit and require callers who use ODB transactions internally to ensure there is not already a pending transaction before beginning or ending a transaction.
--- bulk-checkin.c | 18 ++++-------------- bulk-checkin.h | 9 ++++----- cache-tree.c | 12 +++++++++--- object-file.c | 12 ++++++++---- read-cache.c | 10 +++++++--- 5 files changed, 32 insertions(+), 29 deletions(-)
bulk-checkin.c +4 -14
@@ -33,7 +33,6 @@ struct bulk_checkin_packfile {struct odb_transaction {struct object_database *odb; -int nesting;struct tmp_objdir *objdir;struct bulk_checkin_packfile packfile; };
@@ -368,12 +367,11 @@ void fsync_loose_object_bulk_checkin(struct odb_transaction *transaction,struct odb_transaction *begin_odb_transaction (struct object_database *odb) { -if (!odb->transaction) { -CALLOC_ARRAY (odb->transaction,1 ); - odb->transaction->odb = odb; - } +if (odb->transaction) +BUG ("ODB transaction already started" ); - odb->transaction->nesting +=1 ; +CALLOC_ARRAY (odb->transaction,1 ); + odb->transaction->odb = odb;return odb->transaction; }
@@ -389,14 +387,6 @@ void flush_odb_transaction(struct odb_transaction *transaction)void end_odb_transaction (struct odb_transaction *transaction) { -if (!transaction || transaction->nesting ==0 ) -BUG ("Unbalanced ODB transaction nesting" ); - - transaction->nesting -=1 ; - -if (transaction->nesting) -return ; -flush_odb_transaction (transaction); transaction->odb->transaction =NULL ;free (transaction);
bulk-checkin.h +4 -5
@@ -38,9 +38,9 @@ int index_blob_bulk_checkin(struct odb_transaction *transaction,/* * Tell the object database to optimize for adding * multiple objects. end_odb_transaction must be called -* to make new objects visible. Transactions can be nested, -* and objects are only visible after the outermost transaction -* is complete or the transaction is flushed. +* to make new objects visible. Only a single transaction +* can be pending at a time and must be ended before +* beginning another. */ struct odb_transaction *begin_odb_transaction (struct object_database *odb);
@@ -53,8 +53,7 @@ void flush_odb_transaction(struct odb_transaction *transaction);/* * Tell the object database to make any objects from the -* current transaction visible if this is the final nested -* transaction. +* current transaction visible. */ void end_odb_transaction (struct odb_transaction *transaction);
cache-tree.c +9 -3
@@ -474,7 +474,7 @@ static int update_one(struct cache_tree *it,int cache_tree_update (struct index_state *istate,int flags) { -struct odb_transaction *transaction; +struct odb_transaction *transaction =NULL ;int skip, i; i =verify_cache (istate, flags);
@@ -490,10 +490,16 @@ int cache_tree_update(struct index_state *istate, int flags)trace_performance_enter ();trace2_region_enter ("cache_tree" ,"update" , the_repository); - transaction =begin_odb_transaction (the_repository->objects); + +if (!the_repository->objects->transaction) + transaction =begin_odb_transaction (the_repository->objects); + i =update_one (istate->cache_tree, istate->cache, istate->cache_nr,"" ,0 , &skip, flags); -end_odb_transaction (transaction); + +if (transaction) +end_odb_transaction (transaction); +trace2_region_leave ("cache_tree" ,"update" , the_repository);trace_performance_leave ("cache_tree_update" );if (i <0 )
object-file.c +8 -4
@@ -1264,13 +1264,17 @@ int index_fd(struct index_state *istate, struct object_id *oid, ret =index_core (istate, oid, fd,xsize_t (st->st_size), type, path, flags); }else { -struct odb_transaction *transaction; +struct odb_transaction *transaction =NULL ; - transaction =begin_odb_transaction (the_repository->objects); - ret =index_blob_bulk_checkin (transaction, +if (!the_repository->objects->transaction) + transaction =begin_odb_transaction (the_repository->objects); + + ret =index_blob_bulk_checkin (the_repository->objects->transaction, oid, fd,xsize_t (st->st_size), path, flags); -end_odb_transaction (transaction); + +if (transaction) +end_odb_transaction (transaction); }close (fd);
read-cache.c +7 -3
@@ -3947,7 +3947,7 @@ int add_files_to_cache(struct repository *repo, const char *prefix, const struct pathspec *pathspec, char *ps_matched, int include_sparse, int flags) { - struct odb_transaction *transaction; + struct odb_transaction *transaction = NULL;struct update_callback_data data;struct rev_info rev;
@@ -3973,9 +3973,13 @@ int add_files_to_cache(struct repository *repo, const char *prefix,* This function is invoked from commands other than 'add', which * may not have their own transaction active. */ - transaction =begin_odb_transaction (repo->objects); +if (!repo->objects->transaction) + transaction =begin_odb_transaction (repo->objects); +run_diff_files (&rev,DIFF_RACY_IS_MODIFIED ); -end_odb_transaction (transaction); + +if (transaction) +end_odb_transaction (transaction);release_revisions (&rev);return !!data.add_errors;
Signature · 1 line
2.51.0.193.g4975ec3473b
Re: [PATCH 1/6] bulk-checkin: remove ODB transaction nesting
Quoted 11 lines · Justin Tobler
ODB transactions support being nested. Only the outermost {begin,end}_odb_transaction() start and finish a transaction. This is done so that certain object write codepaths that occur internally can be optimized via ODB transactions without having to worry if a transaction has already been started or not. This can make the interface a bit awkward to use, as calling {begin,end}_odb_transaction() does not guarantee that a transaction is actually started or ended.
Instead, be more explicit and require callers who use ODB transactions internally to ensure there is not already a pending transaction before beginning or ending a transaction.
I think one bit missing in the commit message is to explain what this buys us. Does it for example enable subsequent changes? Or is this really only done to have clean ownership semantics for the transaction?
Quoted 5 lines
bulk-checkin.c +0
-0
bulk-checkin.c void fsync_loose_object_bulk_checkin(struct odb_transaction *transaction, · line 371
Quoted 9 lines
bulk-checkin.c +2
-4@@ -368,12 +367,11 @@ void fsync_loose_object_bulk_checkin(struct odb_transaction *transaction,struct odb_transaction *begin_odb_transaction (struct object_database *odb) { -if (!odb->transaction) { -CALLOC_ARRAY (odb->transaction,1 ); - odb->transaction->odb = odb; - } +if (odb->transaction) +BUG ("ODB transaction already started" );
Okay, good to see we have a BUG() here now.
Quoted 5 lines
bulk-checkin.c +0
-1@@ -389,14 +387,6 @@ void flush_odb_transaction(struct odb_transaction *transaction)void end_odb_transaction (struct odb_transaction *transaction) { -if (!transaction || transaction->nesting ==0 )
bulk-checkin.c void flush_odb_transaction(struct odb_transaction *transaction) · line 392
Quoted 10 lines
bulk-checkin.c +0
-7-BUG ("Unbalanced ODB transaction nesting" ); - - transaction->nesting -=1 ; - -if (transaction->nesting) -return ; -flush_odb_transaction (transaction); transaction->odb->transaction =NULL ;free (transaction);
But I wonder whether we also want to make this function here a bit more robust:
- I think we should handle NULL pointers here and convert them into a no-op. This allows us to skip the if (transaction) checks in many places.
- We probably should have an assert that if a pointer is given, then
`transaction->odb->transaction == transaction`.
The conversions all look correct to me.
Patrick
Re: [PATCH 1/6] bulk-checkin: remove ODB transaction nesting
Quoted 9 lines · Patrick Steinhardt
Quoted 8 lines · Justin Tobler
ODB transactions support being nested. Only the outermost {begin,end}_odb_transaction() start and finish a transaction. This is done so that certain object write codepaths that occur internally can be optimized via ODB transactions without having to worry if a transaction has already been started or not. This can make the interface a bit awkward to use, as calling {begin,end}_odb_transaction() does not guarantee that a transaction is actually started or ended.
Quoted 7 lines
Quoted 3 lines
Instead, be more explicit and require callers who use ODB transactions internally to ensure there is not already a pending transaction before beginning or ending a transaction.
I think one bit missing in the commit message is to explain what this buys us. Does it for example enable subsequent changes? Or is this really only done to have clean ownership semantics for the transaction?
This change does help with the subsequent patches which drop flush_odb_transaction() by removing reasons for this explicit operation to exist. Now it is guaranteed that calling end_odb_transaction() also writes the changes. The change is largely just to clarify ownership sematics for the transaction though. I'll try to clarify this in the commit message in the next version.
Quoted 19 lines
Quoted 5 lines
bulk-checkin.c +0
-1@@ -389,14 +387,6 @@ void flush_odb_transaction(struct odb_transaction *transaction)void end_odb_transaction (struct odb_transaction *transaction) { -if (!transaction || transaction->nesting ==0 )bulk-checkin.c void flush_odb_transaction(struct odb_transaction *transaction) · line 392
Quoted 10 lines
bulk-checkin.c +0
-7-BUG ("Unbalanced ODB transaction nesting" ); - - transaction->nesting -=1 ; - -if (transaction->nesting) -return ; -flush_odb_transaction (transaction); transaction->odb->transaction =NULL ;free (transaction);But I wonder whether we also want to make this function here a bit more robust:
Quoted 3 lines
- I think we should handle
NULLpointers here and convert them into a no-op. This allows us to skip theif (transaction)checks in many places.
This sounds reasonable. Will do.
Quoted 2 lines
- We probably should have an assert that if a pointer is given, then
`transaction->odb->transaction == transaction`.
Good idea. I'll also add this in the next version.
-Justin
Re: [PATCH 1/6] bulk-checkin: remove ODB transaction nesting
In reply to Patrick SteinhardtQuoted 9 lines · Patrick Steinhardt
Quoted 8 lines · Justin Tobler
ODB transactions support being nested. Only the outermost {begin,end}_odb_transaction() start and finish a transaction. This is done so that certain object write codepaths that occur internally can be optimized via ODB transactions without having to worry if a transaction has already been started or not. This can make the interface a bit awkward to use, as calling {begin,end}_odb_transaction() does not guarantee that a transaction is actually started or ended.
Quoted 7 lines
Quoted 3 lines
Instead, be more explicit and require callers who use ODB transactions internally to ensure there is not already a pending transaction before beginning or ending a transaction.
I think one bit missing in the commit message is to explain what this buys us. Does it for example enable subsequent changes? Or is this really only done to have clean ownership semantics for the transaction?
In addition, it would be useful to hear from the commit message *why* this is safe to do. Justin's message suggests that nested transactions are noops, so doing something like:
begin_odb_transaction();
begin_odb_transaction();
write_object();
end_odb_transaction(); <- object not yet added to the main ODB
end_odb_transaction(); <- now it isonly results in the object being added to the main ODB when the final end_odb_transaction() is called.
Instead it looks like this patch pushes us towards having callers check whether or not there is a transaction in progress before starting a new one. So it seems like this is safe to do only for callers that check whether or not there is an ongoing transaction before beginning a new one.
(I think this is what the second paragraph of the quoted part is trying to say, but I think it may be clearer to say "To preserve the same semantics, callers MUST ensure there is not [...]").
That's more work for callers, and at first blush feels a little more error-prone.
Specifically, if some new piece of code is written that does not first check whether there is an ongoing transaction, it could result in a BUG() either at the time it is written, or worse, later on when that function is called in the context of an outer transaction.
So I am not sure whether this patch is making things simpler or safer. Certainly the bulk-checkin API is a little simpler, since we no longer have to keep track of the nesting level within an odb_transaction. But I think it pushes more burden onto the callers in a way that I worry could create the potential for BUG()s later on.
I think that takes us back to Patrick's question: what do we gain by simplifying the internals of the bulk-checkin API, and how does (or doesn't) that justify the added burden on callers? Looking at the newer version of this patch in [1], I see that you addressed what we gain, but I am still curious about how we justify the added cost.
Thanks,
Taylor
Re: [PATCH 1/6] bulk-checkin: remove ODB transaction nesting
Quoted 22 lines · Taylor Blau
Quoted 9 lines · Patrick Steinhardt
Quoted 8 lines · Justin Tobler
ODB transactions support being nested. Only the outermost {begin,end}_odb_transaction() start and finish a transaction. This is done so that certain object write codepaths that occur internally can be optimized via ODB transactions without having to worry if a transaction has already been started or not. This can make the interface a bit awkward to use, as calling {begin,end}_odb_transaction() does not guarantee that a transaction is actually started or ended.
Quoted 7 lines
Quoted 3 lines
Instead, be more explicit and require callers who use ODB transactions internally to ensure there is not already a pending transaction before beginning or ending a transaction.
I think one bit missing in the commit message is to explain what this buys us. Does it for example enable subsequent changes? Or is this really only done to have clean ownership semantics for the transaction?
In addition, it would be useful to hear from the commit message *why* this is safe to do. Justin's message suggests that nested transactions are noops, so doing something like:
Quoted 8 lines
begin_odb_transaction(); begin_odb_transaction(); write_object(); end_odb_transaction(); <- object not yet added to the main ODB end_odb_transaction(); <- now it isonly results in the object being added to the main ODB when the final
end_odb_transaction()is called.
Yes, well said. {begin,end}_odbtransaction() operations on inner transactions are effectively a noop. They simple manage an internal counter to know when a new transaction should be started/finished.
Quoted 5 lines
Instead it looks like this patch pushes us towards having callers check whether or not there is a transaction in progress before starting a new one. So it seems like this is safe to do only for callers that check whether or not there is an ongoing transaction before beginning a new one.
Yes, this patch removes the logic that manages the internal nested transaction counter in favor of requiring callers to check if a transaction has already been started or not.
Quoted 3 lines
(I think this is what the second paragraph of the quoted part is trying to say, but I think it may be clearer to say "To preserve the same semantics, callers MUST ensure there is not [...]").
Yes, you are correct. Apologies for the poor wording.
Quoted 8 lines
That's more work for callers, and at first blush feels a little more error-prone.
Specifically, if some new piece of code is written that does not first check whether there is an ongoing transaction, it could result in a
BUG()either at the time it is written, or worse, later on when that function is called in the context of an outer transaction.
Quoted 5 lines
So I am not sure whether this patch is making things simpler or safer. Certainly the bulk-checkin API is a little simpler, since we no longer have to keep track of the nesting level within an
odb_transaction. But I think it pushes more burden onto the callers in a way that I worry could create the potential for BUG()s later on.
I've revisted this patch and I agree there is probably a better/safer way to accoplish this.
The ultimate goal of this patch is make it so invoking end_odb_transaction() on a transaction guarantees it is flushed. With this guarantee we would no longer need flush_odb_transaction() and the transaction interface eventually can be simplified to just {begin,end}_odb_transaction(). This also avoids a potential class of errors where a caller _thinks_ they have committed a transaction and that the objects should be visible, but they actually are not because it was a nested transaction.
The nice thing about the current implementation though is that nested transactions are automatically treated as noops and the caller doesn't have to check if there is already a pending transaction. This is safer and less error-prone.
Thinking about this some more though, we should be able to continue to have {begin,end}_odb_transaction() function as noops when there is already a pending transaction and also be able to drop the internal transaction nesting mechanism at the same time. To do this, instead of erroring out in begin_odb_transaction() when there is already a transaction, we can simply return NULL. If a caller wants to know if a new transaction was actually started, they can just check the return value afterwords. This removes the burden from the caller to explicitly check if there is a pending transaction beforehand. Furthermore, since an ODB only allows a single transaction at a time, it probably makes sense for operations at the ODB layer to guard against this anyway.
This change would pair nicely with the change made to end_odb_transaction() in version 2 of this series. In this version, when end_odb_transaction() is provided a NULL transaction, it now functions as a noop as well. If a transaction _is_ provided though, it is guaranteed to be flushed with addresses the main goal on this patch.
Quoted 5 lines
I think that takes us back to Patrick's question: what do we gain by simplifying the internals of the bulk-checkin API, and how does (or doesn't) that justify the added burden on callers? Looking at the newer version of this patch in [1], I see that you addressed what we gain, but I am still curious about how we justify the added cost.
Thanks Taylor for the thoughtful feedback. Much appreciated. :)
I'll plan on sending a followup version tomorrow that hopefully addresses most of these concerns.
-Justin
Re: [PATCH 1/6] bulk-checkin: remove ODB transaction nesting
Quoted 10 lines · Justin Tobler
Quoted 10 lines
In addition, it would be useful to hear from the commit message *why* this is safe to do. Justin's message suggests that nested transactions are noops, so doing something like:
begin_odb_transaction(); begin_odb_transaction(); write_object(); end_odb_transaction(); <- object not yet added to the main ODB end_odb_transaction(); <- now it is
Quoted 6 lines
Quoted 2 lines
only results in the object being added to the main ODB when the final
end_odb_transaction()is called.Yes, well said. {begin,end}_odbtransaction() operations on inner transactions are effectively a noop. They simple manage an internal counter to know when a new transaction should be started/finished.
In other words, that part of the design is inherited from the old bulk-checkin infrastructure? It was done for simplicity and I was always unsure if I made the right design decisions there, so it is nice to see that I found at least somebody who seems to agree with it well enough to inherit it into a newly reinvented subsystem ;-)
Quoted 3 lines
Yes, this patch removes the logic that manages the internal nested transaction counter in favor of requiring callers to check if a transaction has already been started or not.
OK. Is that related to those "make it a silent no-op to pass a NULL, instead of forcing the caller to check" changes?
Can all callers tell reliably if there is a transaction already going? One transaction may start somewhere and end in a far away place, and these two places may not be ancestor-decendant in the call graph but merely a distant relative that shares a grand grand parent.
Quoted 2 lines
The ultimate goal of this patch is make it so invoking
end_odb_transaction()on a transaction guarantees it is flushed.
Either flushed or a noop, I guess?
Quoted 8 lines
With
this guarantee we would no longer needflush_odb_transaction()and the transaction interface eventually can be simplified to just {begin,end}_odb_transaction(). This also avoids a potential class of errors where a caller _thinks_ they have committed a transaction and that the objects should be visible, but they actually are not because it was a nested transaction.
Quoted 4 lines
The nice thing about the current implementation though is that nested transactions are automatically treated as noops and the caller doesn't have to check if there is already a pending transaction. This is safer and less error-prone.
True.
Quoted 12 lines
Thinking about this some more though, we should be able to continue to have {begin,end}_odb_transaction() function as noops when there is already a pending transaction and also be able to drop the internal transaction nesting mechanism at the same time. To do this, instead of erroring out in
begin_odb_transaction()when there is already a transaction, we can simply return NULL. If a caller wants to know if a new transaction was actually started, they can just check the return value afterwords. This removes the burden from the caller to explicitly check if there is a pending transaction beforehand. Furthermore, since an ODB only allows a single transaction at a time, it probably makes sense for operations at the ODB layer to guard against this anyway.
Quoted 5 lines
This change would pair nicely with the change made to
end_odb_transaction()in version 2 of this series. In this version, whenend_odb_transaction()is provided a NULL transaction, it now functions as a noop as well. If a transaction _is_ provided though, it is guaranteed to be flushed with addresses the main goal on this patch.
OK.
Re: [PATCH 1/6] bulk-checkin: remove ODB transaction nesting
Quoted 7 lines · Junio C Hamano
Quoted 3 lines · Justin Tobler
Yes, this patch removes the logic that manages the internal nested transaction counter in favor of requiring callers to check if a transaction has already been started or not.
OK. Is that related to those "make it a silent no-op to pass a NULL, instead of forcing the caller to check" changes?
The first version of this patch initially required {begin,end}_odb_transaction() callers to manually ensure there was not already a pending transaction. If this was not ensured, it could BUG() out.
Going forward, I think it probably makes sense to change to a silent no-op passing a NULL transaction value around. This should keep the existing callers mostly unchanged and pose less of a burden while still being able to drop the transaction nesting mechanism.
Quoted 5 lines
Can all callers tell reliably if there is a transaction already going? One transaction may start somewhere and end in a far away place, and these two places may not be ancestor-decendant in the call graph but merely a distant relative that shares a grand grand parent.
To start a transaction, callers must have access to struct object_directory. To know if a transaction is already pending, the transaction field of this structure can be checked.
Ending a transaction does require the pending transaction be provided to it. All existing {begin,end}_odb_transaction() call sites don't currently have a problem with this though as they are relatively close together. If we wanted to end a transaction somewhere else far away from the start, the pending transaction would need to be either wired or accessed through the repository/odb.
Quoted 4 lines
Quoted 2 lines
The ultimate goal of this patch is make it so invoking
end_odb_transaction()on a transaction guarantees it is flushed.Either flushed or a noop, I guess?
Yes. If the pending transaction is specified, it should always be committed. If a NULL transaction value is passed, it should function as a no-op.
-Justin
PATCH 2/6 builtin/update-index: end ODB transaction when --verbose is specified
In reply to Justin ToblerWith 23a3a303 (update-index: use the bulk-checkin infrastructure, 2022-04-04), object database transactions were added to git-update-index(1) to facilitate writing objects in bulk. With transactions, newly added objects are instead written to a temporary object directory and migrated to the primary object database upon transaction commit.
When the --verbose option is specified, each individual object is explicitly flushed via flush_odb_transaction() prior to reporting the update. Flushing the object database transaction migrates pending objects to the primary object database without marking the transaction as complete. This is done so objects are immediately visible to git-update-index(1) callers using the --verbose option and that rely on parsing verbose output to know when objects are written.
As soon as verbose output is requested in git-update-index(1), all subsequent object writes are flushed prior to being reported and thus no longer benefit from being transactional. Furthermore, the mechanism to flush a transaction without committing is rather awkward. Drop the call to flush_odb_transaction() in favor of ending the transaction early when the --verbose flag is encountered.
--- builtin/update-index.c | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-)
builtin/update-index.c +17 -9
@@ -70,14 +70,6 @@ static void report(const char *fmt, ...)if (!verbose)return ; -/* -* It is possible, though unlikely, that a caller could use the verbose -* output to synchronize with addition of objects to the object -* database. The current implementation of ODB transactions leaves -* objects invisible while a transaction is active, so flush the -* transaction here before reporting a change made by update-index. -*/ -flush_odb_transaction (the_repository->objects->transaction);va_start (vp, fmt);vprintf (fmt, vp);putchar ('\n' );
@@ -1150,6 +1142,21 @@ int cmd_update_index(int argc,const char *path = ctx.argv[0 ];char *p; +/* +* It is possible, though unlikely, that a caller could +* use the verbose output to synchronize with addition +* of objects to the object database. The current +* implementation of ODB transactions leaves objects +* invisible while a transaction is active, so end the +* transaction here early before processing the next +* update. All further updates are performed outside of +* a transaction. +*/ +if (transaction && verbose) { +end_odb_transaction (transaction); + transaction =NULL ; + } +setup_work_tree (); p =prefix_path (prefix, prefix_length, path);update_one (p);
@@ -1214,7 +1221,8 @@ int cmd_update_index(int argc,/* * By now we have added all of the new objects */ -end_odb_transaction (transaction); +if (transaction) +end_odb_transaction (transaction);if (split_index >0 ) {if (repo_config_get_split_index (the_repository) ==0 )
Signature · 1 line
2.51.0.193.g4975ec3473b
Re: [PATCH 2/6] builtin/update-index: end ODB transaction when --verbose is specified
Quoted 15 lines · Justin Tobler
With
23a3a303(update-index: use the bulk-checkin infrastructure, 2022-04-04), object database transactions were added to git-update-index(1) to facilitate writing objects in bulk. With transactions, newly added objects are instead written to a temporary object directory and migrated to the primary object database upon transaction commit.When the --verbose option is specified, each individual object is explicitly flushed via
flush_odb_transaction()prior to reporting the update. Flushing the object database transaction migrates pending objects to the primary object database without marking the transaction as complete. This is done so objects are immediately visible to git-update-index(1) callers using the --verbose option and that rely on parsing verbose output to know when objects are written.
Quoted 6 lines
As soon as verbose output is requested in git-update-index(1), all subsequent object writes are flushed prior to being reported and thus no longer benefit from being transactional. Furthermore, the mechanism to flush a transaction without committing is rather awkward. Drop the call to
flush_odb_transaction()in favor of ending the transaction early when the --verbose flag is encountered.
Okay, this interface feels somewhat weird indeed. If we now end the transaction early, does the transaction still serve any purpose at all? Like, do we use it to batch steps _before_ we start reporting stuff?
If the answer is "no", we might be able to just not create a transaction altogether. If the answer is "yes" we should probably point out in the commit message that the transaction still has a purpose.
Patrick
Re: [PATCH 2/6] builtin/update-index: end ODB transaction when --verbose is specified
Quoted 23 lines · Patrick Steinhardt
Quoted 15 lines · Justin Tobler
With
23a3a303(update-index: use the bulk-checkin infrastructure, 2022-04-04), object database transactions were added to git-update-index(1) to facilitate writing objects in bulk. With transactions, newly added objects are instead written to a temporary object directory and migrated to the primary object database upon transaction commit.When the --verbose option is specified, each individual object is explicitly flushed via
flush_odb_transaction()prior to reporting the update. Flushing the object database transaction migrates pending objects to the primary object database without marking the transaction as complete. This is done so objects are immediately visible to git-update-index(1) callers using the --verbose option and that rely on parsing verbose output to know when objects are written.Quoted 6 lines
As soon as verbose output is requested in git-update-index(1), all subsequent object writes are flushed prior to being reported and thus no longer benefit from being transactional. Furthermore, the mechanism to flush a transaction without committing is rather awkward. Drop the call to
flush_odb_transaction()in favor of ending the transaction early when the --verbose flag is encountered.
Quoted 3 lines
Okay, this interface feels somewhat weird indeed. If we now end the transaction early, does the transaction still serve any purpose at all? Like, do we use it to batch steps _before_ we start reporting stuff?
We only start reporting updates when the --verbose option is first encountered. Options are not all processed upfront. This means in the follow example:
$ git update-index --add foo --add bar --verbose --stdin
both "foo" and "bar" are silently added via a transaction. After the --verbose option, subsequent updates are reported. At this point there is no reason for the transaction to continue as all subsequent object writes must be fully written before being reported. Thus the transaction is ended early.
Quoted 2 lines
If the answer is "no", we might be able to just not create a transaction altogether.
We still use a transaction in the normal case when objects are written without --verbose enabled. This matches the existing behavior.
Quoted 2 lines
If the answer is "yes" we should probably point out in the commit message that the transaction still has a purpose.
Yes, transactions still have a purpose, but only for object written before the --verbose option is first encountered. I'll try to clarify this in the commit message.
-Justin
Re: [PATCH 2/6] builtin/update-index: end ODB transaction when --verbose is specified
Quoted 32 lines · Justin Tobler
Quoted 23 lines · Patrick Steinhardt
Quoted 15 lines · Justin Tobler
With
23a3a303(update-index: use the bulk-checkin infrastructure, 2022-04-04), object database transactions were added to git-update-index(1) to facilitate writing objects in bulk. With transactions, newly added objects are instead written to a temporary object directory and migrated to the primary object database upon transaction commit.When the --verbose option is specified, each individual object is explicitly flushed via
flush_odb_transaction()prior to reporting the update. Flushing the object database transaction migrates pending objects to the primary object database without marking the transaction as complete. This is done so objects are immediately visible to git-update-index(1) callers using the --verbose option and that rely on parsing verbose output to know when objects are written.Quoted 6 lines
As soon as verbose output is requested in git-update-index(1), all subsequent object writes are flushed prior to being reported and thus no longer benefit from being transactional. Furthermore, the mechanism to flush a transaction without committing is rather awkward. Drop the call to
flush_odb_transaction()in favor of ending the transaction early when the --verbose flag is encountered.Quoted 3 lines
Okay, this interface feels somewhat weird indeed. If we now end the transaction early, does the transaction still serve any purpose at all? Like, do we use it to batch steps _before_ we start reporting stuff?
We only start reporting updates when the --verbose option is first encountered. Options are not all processed upfront. This means in the follow example:
Quoted 7 lines
$ git update-index --add foo --add bar --verbose --stdinboth "foo" and "bar" are silently added via a transaction. After the --verbose option, subsequent updates are reported. At this point there is no reason for the transaction to continue as all subsequent object writes must be fully written before being reported. Thus the transaction is ended early.
That's... huh. I really have no idea, but is this design intentional or an accident?
Patrick
Re: [PATCH 2/6] builtin/update-index: end ODB transaction when --verbose is specified
Quoted 7 lines · Patrick Steinhardt
Quoted 6 lines · Justin Tobler
We only start reporting updates when the --verbose option is first encountered. Options are not all processed upfront. This means in the follow example:
$ git update-index --add foo --add bar --verbose --stdin
Quoted 8 lines
Quoted 5 lines
both "foo" and "bar" are silently added via a transaction. After the --verbose option, subsequent updates are reported. At this point there is no reason for the transaction to continue as all subsequent object writes must be fully written before being reported. Thus the transaction is ended early.
That's... huh. I really have no idea, but is this design intentional or an accident?
I think it is likely that the behavior of the verbose flag here is just a side effect of how options parsing is handled by git-update-index(1). Instead of just using parse_options() to parse the options in one go, parse_options_step() is used to handle filename arguments as the come.
Looking into this a bit further, I originally thought this was so you could do something like:
$ git update-index --remove foo --add bar
but this doesn't work because as soon as the --remove option is encountered, all subsequent file arguments are treated as a removed. This does mean though something like the following _does_ work:
$ git update-index --add foo --remove bar
This is probably unintentional though and rather awkward. Due to the nature of argument parsing here, this interface has several other order related quirks like this.
Looking at the history, this appears to come from 309be813c9 (update-index: migrate to parse-options API, 2010-12-01). I might be missing something, but I'm not entirely sure why though we need to process the filename arguments as the come instead of waiting to end to process them all. We could explore tighening this interface by updating to use the normal parse_options(), but I do have some concerns though that there may be users out there relying on this quirky interface.
-Justin
Re: [PATCH 2/6] builtin/update-index: end ODB transaction when --verbose is specified
Quoted 10 lines · Justin Tobler
Looking into this a bit further, I originally thought this was so you could do something like:
$ git update-index --remove foo --add barbut this doesn't work because as soon as the --remove option is encountered, all subsequent file arguments are treated as a removed. This does mean though something like the following _does_ work:
$ git update-index --add foo --remove bar
Whoops, in both of these examples I meant to use the --force-remove option instead of --remove. Once the --force-remove option is encountered, its codepath is used for all remaining filepaths. This leads to the behavior discrepancy between the above two examples.
Quoted 3 lines
This is probably unintentional though and rather awkward. Due to the nature of argument parsing here, this interface has several other order related quirks like this.
Grepping through the codebase shows that we also have existing test cases that rely on the existing git-update-index(1) option order based behavior in both t1001-read-tree-m-2way.sh and t1002-read-tree-m-u-2way.sh. Still not sure if it was intentional, but it does seem to be used.
-Justin
PATCH 3/6 bulk-checkin: drop flush_odb_transaction()
In reply to Justin ToblerObject database transactions can be explicitly flushed via flush_odb_transaction() without actually completing the transaction. This makes the provided transactional interface a bit awkward. Now that there are no longer any flush_odb_transaction() call sites, drop the function to simplify the interface and further ensure that a transaction is only finalized when end_odb_transaction() is invoked.
--- bulk-checkin.c | 10 +--------- bulk-checkin.h | 7 ------- 2 files changed, 1 insertion(+), 16 deletions(-)
bulk-checkin.c +1 -9
@@ -376,18 +376,10 @@ struct odb_transaction *begin_odb_transaction(struct object_database *odb)return odb->transaction; } -void flush_odb_transaction (struct odb_transaction *transaction) +void end_odb_transaction (struct odb_transaction *transaction) { -if (!transaction) -return ; -flush_batch_fsync (transaction);flush_bulk_checkin_packfile (transaction); -} - -void end_odb_transaction (struct odb_transaction *transaction) -{ -flush_odb_transaction (transaction); transaction->odb->transaction =NULL ;free (transaction); }
bulk-checkin.h +0 -7
@@ -44,13 +44,6 @@ int index_blob_bulk_checkin(struct odb_transaction *transaction,*/ struct odb_transaction *begin_odb_transaction (struct object_database *odb); -/* -* Make any objects that are currently part of a pending object -* database transaction visible. It is valid to call this function -* even if no transaction is active. -*/ -void flush_odb_transaction (struct odb_transaction *transaction); -/* * Tell the object database to make any objects from the * current transaction visible.
Signature · 1 line
2.51.0.193.g4975ec3473b
PATCH 4/6 object-file: relocate ODB transaction code
In reply to Justin ToblerThe bulk-checkin subsystem provides various functions to manage ODB transactions. Apart from {begin,end}_odb_transaction(), these functions are only used by the object-file subsystem to manage aspects of a transaction implementation specific to the files object source.
Relocate all the transaction code in in bulk-checkin to object-file. This simplifies the exposed transaction interface by reducing it to only {begin,end}_odb_transaction(). Function and type names are adjusted in the subsequent commit to better fit the new location.
--- Makefile | 1 - builtin/add.c | 2 +- builtin/unpack-objects.c | 1 - builtin/update-index.c | 1 - bulk-checkin.c | 385 -------------------------------------- bulk-checkin.h | 53 ------ cache-tree.c | 1 - meson.build | 1 - object-file.c | 386 ++++++++++++++++++++++++++++++++++++++- object-file.h | 17 ++ read-cache.c | 1 - 11 files changed, 403 insertions(+), 446 deletions(-) delete mode 100644 bulk-checkin.c delete mode 100644 bulk-checkin.h
Makefile +0 -1
@@ -974,7 +974,6 @@ LIB_OBJS += blame.o LIB_OBJS += blob.o LIB_OBJS += bloom.o LIB_OBJS += branch.o -LIB_OBJS += bulk-checkin.o LIB_OBJS += bundle-uri.o LIB_OBJS += bundle.o LIB_OBJS += cache-tree.o
builtin/add.c +1 -1
@@ -14,13 +14,13 @@#include "gettext.h" #include "pathspec.h" #include "run-command.h" +#include "object-file.h" #include "parse-options.h" #include "path.h" #include "preload-index.h" #include "diff.h" #include "read-cache.h" #include "revision.h" -#include "bulk-checkin.h" #include "strvec.h" #include "submodule.h" #include "add-interactive.h"
builtin/unpack-objects.c +0 -1
@@ -2,7 +2,6 @@#define DISABLE_SIGN_COMPARE_WARNINGS #include "builtin.h" -#include "bulk-checkin.h" #include "config.h" #include "environment.h" #include "gettext.h"
builtin/update-index.c +0 -1
@@ -8,7 +8,6 @@#define DISABLE_SIGN_COMPARE_WARNINGS #include "builtin.h" -#include "bulk-checkin.h" #include "config.h" #include "environment.h" #include "gettext.h"
bulk-checkin.c deleted +0 -385
385 lines not on this page · raw message
bulk-checkin.h deleted +0 -53
53 lines not on this page · raw message
cache-tree.c +0 -1
@@ -8,7 +8,6 @@#include "tree.h" #include "tree-walk.h" #include "cache-tree.h" -#include "bulk-checkin.h" #include "object-file.h" #include "odb.h" #include "read-cache-ll.h"
meson.build +0 -1
@@ -287,7 +287,6 @@ libgit_sources = [ 'blob.c', 'bloom.c', 'branch.c', - 'bulk-checkin.c', 'bundle-uri.c', 'bundle.c', 'cache-tree.c',
object-file.c +385 -1
413 lines not on this page · raw message
object-file.h +17 -0
@@ -218,4 +218,21 @@ int read_loose_object(struct repository *repo,void **contents, struct object_info *oi); +struct odb_transaction ; + +/* +* Tell the object database to optimize for adding +* multiple objects. end_odb_transaction must be called +* to make new objects visible. Only a single transaction +* can be pending at a time and must be ended before +* beginning another. +*/ +struct odb_transaction *begin_odb_transaction (struct object_database *odb); + +/* +* Tell the object database to make any objects from the +* current transaction visible. +*/ +void end_odb_transaction (struct odb_transaction *transaction); +#endif /* OBJECT_FILE_H */
read-cache.c +0 -1
@@ -8,7 +8,6 @@#define DISABLE_SIGN_COMPARE_WARNINGS #include "git-compat-util.h" -#include "bulk-checkin.h" #include "config.h" #include "date.h" #include "diff.h"
Signature · 1 line
2.51.0.193.g4975ec3473b
PATCH 5/6 object-file: update naming from bulk-checkin
In reply to Justin ToblerUpdate the names of several functions and types relocated from the bulk-checkin subsystem for better clarity. Also drop finish_tmp_packfile() as a standalone function in favor of embedding it in flush_packfile_transaction() directly.
--- object-file.c | 80 +++++++++++++++++++++++---------------------------- 1 file changed, 36 insertions(+), 44 deletions(-)
object-file.c +36 -44
@@ -667,7 +667,7 @@ void hash_object_file(const struct git_hash_algo *algo, const void *buf,write_object_file_prepare (algo, buf, len, type, oid, hdr, &hdrlen); } -struct bulk_checkin_packfile { +struct transaction_packfile {char *pack_tmp_name;struct hashfile *f;off_t offset;
@@ -682,10 +682,10 @@ struct odb_transaction {struct object_database *odb;struct tmp_objdir *objdir; -struct bulk_checkin_packfile packfile; +struct transaction_packfile packfile; }; -static void prepare_loose_object_bulk_checkin (struct odb_transaction *transaction) +static void prepare_loose_object_transaction (struct odb_transaction *transaction) {/* * We lazily create the temporary object directory
@@ -701,7 +701,7 @@ static void prepare_loose_object_bulk_checkin(struct odb_transaction *transactiotmp_objdir_replace_primary_odb (transaction->objdir,0 ); } -static void fsync_loose_object_bulk_checkin (struct odb_transaction *transaction, +static void fsync_loose_object_transaction (struct odb_transaction *transaction,int fd,const char *filename) {/*
@@ -722,7 +722,7 @@ static void fsync_loose_object_bulk_checkin(struct odb_transaction *transaction,/* * Cleanup after batch-mode fsync_object_files. */ -static void flush_batch_fsync (struct odb_transaction *transaction) +static void flush_loose_object_transaction (struct odb_transaction *transaction) {struct strbuf temp_path =STRBUF_INIT ;struct tempfile *temp;
@@ -733,7 +733,7 @@ static void flush_batch_fsync(struct odb_transaction *transaction)/* * Issue a full hardware flush against a temporary file to ensure * that all objects are durable before any renames occur. The code in -* fsync_loose_object_bulk_checkin has already issued a writeout +* fsync_loose_object_transaction has already issued a writeout * request, but it has not flushed any writeback cache in the storage * hardware or any filesystem logs. This fsync call acts as a barrier * to ensure that the data in each new object file is durable before
@@ -762,7 +762,7 @@ static void close_loose_object(struct odb_source *source, goto out;if (batch_fsync_enabled (FSYNC_COMPONENT_LOOSE_OBJECT )) -fsync_loose_object_bulk_checkin (source->odb->transaction, fd, filename); +fsync_loose_object_transaction (source->odb->transaction, fd, filename);else if (fsync_object_files >0 )fsync_or_die (fd, filename);else
@@ -940,7 +940,7 @@ static int write_loose_object(struct odb_source *source,static struct strbuf filename =STRBUF_INIT ;if (batch_fsync_enabled (FSYNC_COMPONENT_LOOSE_OBJECT )) -prepare_loose_object_bulk_checkin (source->odb->transaction); +prepare_loose_object_transaction (source->odb->transaction);odb_loose_path (source, &filename, oid);
@@ -1029,7 +1029,7 @@ int stream_loose_object(struct odb_source *source,int hdrlen;if (batch_fsync_enabled (FSYNC_COMPONENT_LOOSE_OBJECT )) -prepare_loose_object_bulk_checkin (source->odb->transaction); +prepare_loose_object_transaction (source->odb->transaction);/* Since oid is not determined, save tmp file to odb path. */ strbuf_addf (&filename,"%s/" , source->path);
@@ -1349,10 +1349,10 @@ static int already_written(struct odb_transaction *transaction, }/* Lazily create backing packfile for the state */ -static void prepare_to_stream (struct odb_transaction *transaction, -unsigned flags) +static void prepare_packfile_transaction (struct odb_transaction *transaction, +unsigned flags) { -struct bulk_checkin_packfile *state = &transaction->packfile; +struct transaction_packfile *state = &transaction->packfile;if (!(flags &INDEX_WRITE_OBJECT ) || state->f)return ;
@@ -1381,7 +1381,7 @@ static void prepare_to_stream(struct odb_transaction *transaction,* status before calling us just in case we ask it to call us again * with a new pack. */ -static int stream_blob_to_pack (struct bulk_checkin_packfile *state, +static int stream_blob_to_pack (struct transaction_packfile *state,struct git_hash_ctx *ctx,off_t *already_hashed_to,int fd,size_t size,const char *path,unsigned flags)
@@ -1457,28 +1457,13 @@ static int stream_blob_to_pack(struct bulk_checkin_packfile *state,return 0 ; } -static void finish_tmp_packfile (struct odb_transaction *transaction, -struct strbuf *basename, -unsigned char hash[]) +static void flush_packfile_transaction (struct odb_transaction *transaction) { -struct bulk_checkin_packfile *state = &transaction->packfile; -struct repository *repo = transaction->odb->repo; -char *idx_tmp_name =NULL ; - -stage_tmp_packfiles (repo, basename, state->pack_tmp_name, - state->written, state->nr_written,NULL , - &state->pack_idx_opts, hash, &idx_tmp_name); -rename_tmp_packfile_idx (repo, basename, &idx_tmp_name); - -free (idx_tmp_name); -} - -static void flush_bulk_checkin_packfile (struct odb_transaction *transaction) -{ -struct bulk_checkin_packfile *state = &transaction->packfile; +struct transaction_packfile *state = &transaction->packfile;struct repository *repo = transaction->odb->repo;unsigned char hash[GIT_MAX_RAWSZ ];struct strbuf packname =STRBUF_INIT ; +char *idx_tmp_name =NULL ;if (!state->f)return ;
@@ -1503,11 +1488,16 @@ static void flush_bulk_checkin_packfile(struct odb_transaction *transaction)repo_get_object_directory (transaction->odb->repo), hash_to_hex_algop(hash, repo->hash_algo)); -finish_tmp_packfile (transaction, &packname, hash); +stage_tmp_packfiles (repo, &packname, state->pack_tmp_name, + state->written, state->nr_written,NULL , + &state->pack_idx_opts, hash, &idx_tmp_name); +rename_tmp_packfile_idx (repo, &packname, &idx_tmp_name); +for (uint32_t i =0 ; i < state->nr_written; i++)free (state->written[i]); clear_exit: +free (idx_tmp_name);free (state->pack_tmp_name);free (state->written);memset (state,0 ,sizeof (*state));
@@ -1535,11 +1525,12 @@ static void flush_bulk_checkin_packfile(struct odb_transaction *transaction)* binary blobs, they generally do not want to get any conversion, and * callers should avoid this code path when filters are requested. */ -static int index_blob_bulk_checkin (struct odb_transaction *transaction, -struct object_id *result_oid,int fd,size_t size, -const char *path,unsigned flags) +static int index_blob_packfile_transaction (struct odb_transaction *transaction, +struct object_id *result_oid,int fd, +size_t size,const char *path, +unsigned flags) { -struct bulk_checkin_packfile *state = &transaction->packfile; +struct transaction_packfile *state = &transaction->packfile;off_t seekback, already_hashed_to;struct git_hash_ctx ctx;unsigned char obuf[16384 ];
@@ -1560,14 +1551,14 @@ static int index_blob_bulk_checkin(struct odb_transaction *transaction,if ((flags &INDEX_WRITE_OBJECT ) !=0 ) {CALLOC_ARRAY (idx,1 ); -prepare_to_stream (transaction, flags); +prepare_packfile_transaction (transaction, flags);hashfile_checkpoint_init (state->f, &checkpoint); } already_hashed_to =0 ;while (1 ) { -prepare_to_stream (transaction, flags); +prepare_packfile_transaction (transaction, flags);if (idx) {hashfile_checkpoint (state->f, &checkpoint); idx->offset = state->offset;
@@ -1585,7 +1576,7 @@ static int index_blob_bulk_checkin(struct odb_transaction *transaction,BUG ("should not happen" );hashfile_truncate (state->f, &checkpoint); state->offset = checkpoint.offset; -flush_bulk_checkin_packfile (transaction); +flush_packfile_transaction (transaction);if (lseek (fd, seekback,SEEK_SET ) == (off_t)-1 )return error ("cannot seek back" ); }
@@ -1634,9 +1625,10 @@ int index_fd(struct index_state *istate, struct object_id *oid,if (!the_repository->objects->transaction) transaction =begin_odb_transaction (the_repository->objects); - ret =index_blob_bulk_checkin (the_repository->objects->transaction, - oid, fd,xsize_t (st->st_size), - path, flags); + ret =index_blob_packfile_transaction (the_repository->objects->transaction, + oid, fd, +xsize_t (st->st_size), + path, flags);if (transaction)end_odb_transaction (transaction);
@@ -1992,8 +1984,8 @@ struct odb_transaction *begin_odb_transaction(struct object_database *odb)void end_odb_transaction (struct odb_transaction *transaction) { -flush_batch_fsync (transaction); -flush_bulk_checkin_packfile (transaction); +flush_loose_object_transaction (transaction); +flush_packfile_transaction (transaction); transaction->odb->transaction =NULL ;free (transaction); }
Signature · 1 line
2.51.0.193.g4975ec3473b
PATCH 6/6 odb: add transaction interface
In reply to Justin ToblerTransactions are managed via the {begin,end}_odb_transaction() function in the object-file subsystem and its implementation is specific to the files object source. Introduce odb_transaction_{begin,commit}() in the odb subsystem to provide an eventual object source agnostic means to manage transactions.
Update call sites to instead manage transactions through the odb subsystem. Also rename {begin,end}_odb_transaction() functions to object_file_transaction_{begin,end}() to clarify the object source it supports.
--- builtin/add.c | 5 +++-- builtin/unpack-objects.c | 4 ++-- builtin/update-index.c | 7 ++++--- cache-tree.c | 4 ++-- object-file.c | 10 +++++----- object-file.h | 6 +++--- odb.c | 10 ++++++++++ odb.h | 3 +++ read-cache.c | 4 ++-- 9 files changed, 34 insertions(+), 19 deletions(-)
builtin/add.c +3 -2
@@ -15,6 +15,7 @@#include "pathspec.h" #include "run-command.h" #include "object-file.h" +#include "odb.h" #include "parse-options.h" #include "path.h" #include "preload-index.h"
@@ -575,7 +576,7 @@ int cmd_add(int argc,string_list_clear (&only_match_skip_worktree,0 ); } - transaction =begin_odb_transaction (repo->objects); + transaction =odb_transaction_begin (repo->objects); ps_matched =xcalloc (pathspec.nr,1 );if (add_renormalize)
@@ -594,7 +595,7 @@ int cmd_add(int argc,if (chmod_arg && pathspec.nr) exit_status |=chmod_pathspec (repo, &pathspec, chmod_arg[0 ], show_only); -end_odb_transaction (transaction); +odb_transaction_commit (transaction); finish: if (write_locked_index(repo->index, &lock_file,
builtin/unpack-objects.c +2 -2
@@ -599,12 +599,12 @@ static void unpack_all(void) progress =start_progress (the_repository,_ ("Unpacking objects" ), nr_objects);CALLOC_ARRAY (obj_list, nr_objects); - transaction =begin_odb_transaction (the_repository->objects); + transaction =odb_transaction_begin (the_repository->objects);for (i =0 ; i < nr_objects; i++) {unpack_one (i);display_progress (progress, i +1 ); } -end_odb_transaction (transaction); +odb_transaction_commit (transaction);stop_progress (&progress);if (delta_list)
builtin/update-index.c +4 -3
@@ -18,6 +18,7 @@#include "cache-tree.h" #include "tree-walk.h" #include "object-file.h" +#include "odb.h" #include "refs.h" #include "resolve-undo.h" #include "parse-options.h"
@@ -1122,7 +1123,7 @@ int cmd_update_index(int argc,* Allow the object layer to optimize adding multiple objects in * a batch. */ - transaction =begin_odb_transaction (the_repository->objects); + transaction =odb_transaction_begin (the_repository->objects);while (ctx.argc) {if (parseopt_state !=PARSE_OPT_DONE ) parseopt_state = parse_options_step(&ctx, options,
@@ -1152,7 +1153,7 @@ int cmd_update_index(int argc,* a transaction. */ if (transaction && verbose) { -end_odb_transaction (transaction); +odb_transaction_commit (transaction); transaction =NULL ; }
@@ -1221,7 +1222,7 @@ int cmd_update_index(int argc,* By now we have added all of the new objects */ if (transaction) -end_odb_transaction (transaction); +odb_transaction_commit (transaction);if (split_index >0 ) {if (repo_config_get_split_index (the_repository) ==0 )
cache-tree.c +2 -2
@@ -491,13 +491,13 @@ int cache_tree_update(struct index_state *istate, int flags)trace2_region_enter ("cache_tree" ,"update" , the_repository);if (!the_repository->objects->transaction) - transaction =begin_odb_transaction (the_repository->objects); + transaction =odb_transaction_begin (the_repository->objects); i =update_one (istate->cache_tree, istate->cache, istate->cache_nr,"" ,0 , &skip, flags);if (transaction) -end_odb_transaction (transaction); +odb_transaction_commit (transaction);trace2_region_leave ("cache_tree" ,"update" , the_repository);trace_performance_leave ("cache_tree_update" );
object-file.c +5 -5
@@ -691,7 +691,7 @@ static void prepare_loose_object_transaction(struct odb_transaction *transaction* We lazily create the temporary object directory * the first time an object might be added, since * callers may not know whether any objects will be -* added at the time they call begin_odb_transaction. +* added at the time they call object_file_transaction_begin. */ if (!transaction || transaction->objdir)return ;
@@ -1623,7 +1623,7 @@ int index_fd(struct index_state *istate, struct object_id *oid,struct odb_transaction *transaction =NULL ;if (!the_repository->objects->transaction) - transaction =begin_odb_transaction (the_repository->objects); + transaction =odb_transaction_begin (the_repository->objects); ret = index_blob_packfile_transaction(the_repository->objects->transaction, oid, fd,
@@ -1631,7 +1631,7 @@ int index_fd(struct index_state *istate, struct object_id *oid, path, flags);if (transaction) -end_odb_transaction (transaction); +odb_transaction_commit (transaction); }close (fd);
@@ -1971,7 +1971,7 @@ int read_loose_object(struct repository *repo,return ret; } -struct odb_transaction *begin_odb_transaction (struct object_database *odb) +struct odb_transaction *object_file_transaction_begin (struct object_database *odb) {if (odb->transaction)BUG ("ODB transaction already started" );
@@ -1982,7 +1982,7 @@ struct odb_transaction *begin_odb_transaction(struct object_database *odb)return odb->transaction; } -void end_odb_transaction (struct odb_transaction *transaction) +void object_file_transaction_end (struct odb_transaction *transaction) {flush_loose_object_transaction (transaction);flush_packfile_transaction (transaction);
object-file.h +3 -3
@@ -222,17 +222,17 @@ struct odb_transaction;/* * Tell the object database to optimize for adding -* multiple objects. end_odb_transaction must be called +* multiple objects. object_file_transaction_end must be called * to make new objects visible. Only a single transaction * can be pending at a time and must be ended before * beginning another. */ -struct odb_transaction *begin_odb_transaction (struct object_database *odb); +struct odb_transaction *object_file_transaction_begin (struct object_database *odb);/* * Tell the object database to make any objects from the * current transaction visible. */ -void end_odb_transaction (struct odb_transaction *transaction); +void object_file_transaction_end (struct odb_transaction *transaction);#endif /* OBJECT_FILE_H */
odb.c +10 -0
@@ -1051,3 +1051,13 @@ void odb_clear(struct object_database *o)hashmap_clear (&o->pack_map);string_list_clear (&o->submodule_source_paths,0 ); } + +struct odb_transaction *odb_transaction_begin (struct object_database *odb) +{ +return object_file_transaction_begin (odb); +} + +void odb_transaction_commit (struct odb_transaction *transaction) +{ +object_file_transaction_end (transaction); +}
odb.h +3 -0
@@ -185,6 +185,9 @@ struct object_database {struct object_database *odb_new (struct repository *repo);void odb_clear (struct object_database *o); +struct odb_transaction *odb_transaction_begin (struct object_database *odb); +void odb_transaction_commit (struct odb_transaction *transaction); +/* * Find source by its object directory path. Dies in case the source couldn't * be found.
read-cache.c +2 -2
@@ -3973,12 +3973,12 @@ int add_files_to_cache(struct repository *repo, const char *prefix,* may not have their own transaction active. */ if (!repo->objects->transaction) - transaction =begin_odb_transaction (repo->objects); + transaction =odb_transaction_begin (repo->objects);run_diff_files (&rev,DIFF_RACY_IS_MODIFIED );if (transaction) -end_odb_transaction (transaction); +odb_transaction_commit (transaction);release_revisions (&rev);return !!data.add_errors;
Signature · 1 line
2.51.0.193.g4975ec3473b
Re: [PATCH 6/6] odb: add transaction interface
Quoted 10 lines · Justin Tobler
Transactions are managed via the {begin,end}_odb_transaction() function in the object-file subsystem and its implementation is specific to the files object source. Introduce odb_transaction_{begin,commit}() in the odb subsystem to provide an eventual object source agnostic means to manage transactions.
Update call sites to instead manage transactions through the odb subsystem. Also rename {begin,end}_odb_transaction() functions to object_file_transaction_{begin,end}() to clarify the object source it supports.
Makes sense.
object-file.c int index_fd(struct index_state *istate, struct object_id *oid, · line 1629
Quoted 13 lines
object-file.c +1
-1@@ -1623,7 +1623,7 @@ int index_fd(struct index_state *istate, struct object_id *oid,struct odb_transaction *transaction =NULL ;if (!the_repository->objects->transaction) - transaction =begin_odb_transaction (the_repository->objects); + transaction =odb_transaction_begin (the_repository->objects); ret = index_blob_packfile_transaction(the_repository->objects->transaction, oid, fd,
This function is a bit of an outlier, as it is weird that we call odb_transaction_begin() instead of the specific function.
But "object-file.c" currently contains two different parts: logic that is related to reading and writing objects in general, and logic that provides the actual object source implementation. This will be split up eventually once we carve out the actual "files" backend, so meanwhile we have to live with this seemingly-unclean separation of concerns.
object-file.c int read_loose_object(struct repository *repo, · line 1977
Quoted 9 lines
object-file.c +1
-1@@ -1971,7 +1971,7 @@ int read_loose_object(struct repository *repo,return ret; } -struct odb_transaction *begin_odb_transaction (struct object_database *odb) +struct odb_transaction *object_file_transaction_begin (struct object_database *odb) {if (odb->transaction)BUG ("ODB transaction already started" );
I would have expected that this function now gets as input an struct odb_source instead of the whole object database. After all, the ODB layer is the one coordinating the sources and managing which sources to tap into for a specific use case. But the actual business logic to read or write objects should then be handled on the source level, shouldn't it?
object-file.c struct odb_transaction *begin_odb_transaction(struct object_database *odb) · line 1988
Quoted 9 lines
object-file.c +1
-1@@ -1982,7 +1982,7 @@ struct odb_transaction *begin_odb_transaction(struct object_database *odb)return odb->transaction; } -void end_odb_transaction (struct odb_transaction *transaction) +void object_file_transaction_end (struct odb_transaction *transaction) {flush_loose_object_transaction (transaction);flush_packfile_transaction (transaction);
Shouldn't this also be called object_file_transaction_commit() to match the ODB layer?
odb.c void odb_clear(struct object_database *o) · line 1058
Quoted 13 lines
odb.c +5
-0@@ -1051,3 +1051,13 @@ void odb_clear(struct object_database *o)hashmap_clear (&o->pack_map);string_list_clear (&o->submodule_source_paths,0 ); } + +struct odb_transaction *odb_transaction_begin (struct object_database *odb) +{ +return object_file_transaction_begin (odb); +}
So with the above, I would expect that we pick the source to create the transaction for here and then call object_file_transaction_begin() on that source. Eventually, once we have pluggable object databases, we would then not call object_file_transaction_start() directly anymore, but instead we'd call e.g. source->backend.transaction_start().
odb.h struct object_database { · line 189
Quoted 10 lines
odb.h +2
-0@@ -185,6 +185,9 @@ struct object_database {struct object_database *odb_new (struct repository *repo);void odb_clear (struct object_database *o); +struct odb_transaction *odb_transaction_begin (struct object_database *odb); +void odb_transaction_commit (struct odb_transaction *transaction);
Let's add some documentation here what these functions do and why you'd want to use them.
Patrick
Re: [PATCH 6/6] odb: add transaction interface
Quoted 18 lines · Patrick Steinhardt
object-file.c int index_fd(struct index_state *istate, struct object_id *oid, · line 1629
Quoted 13 lines · Justin Tobler
object-file.c +1
-1@@ -1623,7 +1623,7 @@ int index_fd(struct index_state *istate, struct object_id *oid,struct odb_transaction *transaction =NULL ;if (!the_repository->objects->transaction) - transaction =begin_odb_transaction (the_repository->objects); + transaction =odb_transaction_begin (the_repository->objects); ret = index_blob_packfile_transaction(the_repository->objects->transaction, oid, fd,This function is a bit of an outlier, as it is weird that we call
odb_transaction_begin()instead of the specific function.
Quoted 5 lines
But "
object-file.c" currently contains two different parts: logic that is related to reading and writing objects in general, and logic that provides the actual object source implementation. This will be split up eventually once we carve out the actual "files" backend, so meanwhile we have to live with this seemingly-unclean separation of concerns.
Ya, this one is a bit strange indeed. Writing a blob directly to a packfile is done via index_blob_packfile_transaction() and currently requires a transaction. Concerns regarding how exactly an object is written should probably be transparently be handled by source.
In this case, writing a blob directly to a packfile should probably be eventually moved down a layer behind the ODB object write interface. Especially now that index_blob_packfile_transaction() is really just an internal detail to the files object source and not part of the generic transactional interface.
Quoted 4 lines
Quoted 4 lines
object-file.c +0
-0@@ -1971,7 +1971,7 @@ int read_loose_object(struct repository *repo,return ret; }
Quoted 12 lines
object-file.c int read_loose_object(struct repository *repo, · line 1977
Quoted 5 lines
object-file.c +1
-1-struct odb_transaction *begin_odb_transaction (struct object_database *odb) +struct odb_transaction *object_file_transaction_begin (struct object_database *odb) {if (odb->transaction)BUG ("ODB transaction already started" );I would have expected that this function now gets as input an
struct odb_sourceinstead of the whole object database. After all, the ODB layer is the one coordinating the sources and managing which sources to tap into for a specific use case. But the actual business logic to read or write objects should then be handled on the source level, shouldn't it?
Good point, I'll update this in the next version.
Quoted 4 lines
Quoted 4 lines
object-file.c +0
-0@@ -1982,7 +1982,7 @@ struct odb_transaction *begin_odb_transaction(struct object_database *odb)return odb->transaction; }
Quoted 8 lines
object-file.c struct odb_transaction *begin_odb_transaction(struct object_database *odb) · line 1988
Quoted 5 lines
object-file.c +1
-1-void end_odb_transaction (struct odb_transaction *transaction) +void object_file_transaction_end (struct odb_transaction *transaction) {flush_loose_object_transaction (transaction);flush_packfile_transaction (transaction);Shouldn't this also be called
object_file_transaction_commit()to match the ODB layer?
Ya, its probably best to be consistent here. Will update.
Quoted 11 lines
Quoted 11 lines
odb.c +3
-0@@ -1051,3 +1051,13 @@ void odb_clear(struct object_database *o)hashmap_clear (&o->pack_map);string_list_clear (&o->submodule_source_paths,0 ); } + +struct odb_transaction *odb_transaction_begin (struct object_database *odb) +{
Quoted 8 lines
odb.c void odb_clear(struct object_database *o) · line 1058
Quoted 2 lines
odb.c +2
-0+return object_file_transaction_begin (odb); +}So with the above, I would expect that we pick the source to create the transaction for here and then call
object_file_transaction_begin()on that source. Eventually, once we have pluggable object databases, we would then not callobject_file_transaction_start()directly anymore, but instead we'd call e.g.source->backend.transaction_start().
Yup, I figure we will eventually have a function table for each source that maps the corresponding operations similar to how its done for reference backends.
Quoted 8 lines
Quoted 8 lines
odb.h +0
-0@@ -185,6 +185,9 @@ struct object_database {struct object_database *odb_new (struct repository *repo);void odb_clear (struct object_database *o);
Quoted 5 lines
odb.h struct object_database { · line 189
Quoted 2 lines
odb.h +2
-0+struct odb_transaction *odb_transaction_begin (struct object_database *odb); +void odb_transaction_commit (struct odb_transaction *transaction);Let's add some documentation here what these functions do and why you'd want to use them.
Will do.
Thanks for the review,
-Justin
v2 cover odb: add transaction interfaces to ODB subsystem
In reply to Justin ToblerGreetings,
This series is a followup to [1] and continues iterating on the ODB transaction interfaces.
The bulk-checkin subsystem provides an interface to manage ODB transactions. Apart from {begin,end}_odb_transaction(), these functions are only used by the object-file subsystem to manage aspects of a transaction implementation specific to the files object source.
In a pluggable object database future where we could have different types of object database sources, transaction handling will have to be implemented separately per source. Thus, the primary focus of this series is to simplify the existing ODB transaction interface and provide a means to manage transactions via the ODB subsystem in an object source agnostic manner eventually.
This series is built on top of 4975ec3473b (The seventh batch, 2025-09-08) with jt/de-global-bulk-checkin merged into it at ddc0b56ad77 (bulk-checkin: use repository variable from transaction, 2025-08-22).
Changes since V1:
- end_odb_transaction() is now a no-op when no transaction is specified and also guards against the ending a transaction not set in the object database.
- The object_file_transaction_begin() now accepts a struct odb_source instead of struct odbject_database. Which is more in line with it being a transaction implementation specific to an object source.
- Further clarified some commit messages.
- Renamed object_file_transaction_end() to object_file_transaction_commit() to match the corresponding function in the ODB section.
- Add some missing documentation for odb_transaction_{begin,commit}().
Note I've opted to leave transaction handling in git-update-index(1) the same as the previous version as sorting out the quirky options parsing behavior is probably out of scope for this change.
Thanks,
-Justin
[1]: <[email protected]>
Justin Tobler (6):
bulk-checkin: remove ODB transaction nesting builtin/update-index: end ODB transaction when --verbose is specified
bulk-checkin: drop flush_odb_transaction()
object-file: relocate ODB transaction code
object-file: update naming from bulk-checkin
odb: add transaction interface
Makefile | 1 - builtin/add.c | 7 +- builtin/unpack-objects.c | 5 +- builtin/update-index.c | 29 +-- bulk-checkin.c | 403 -------------------------------------- bulk-checkin.h | 61 ------ cache-tree.c | 12 +- meson.build | 1 - object-file.c | 409 ++++++++++++++++++++++++++++++++++++++- object-file.h | 17 ++ odb.c | 10 + odb.h | 14 ++ read-cache.c | 10 +- 13 files changed, 478 insertions(+), 501 deletions(-) delete mode 100644 bulk-checkin.c delete mode 100644 bulk-checkin.h
Range-diff against v1:
1: d5a3035a00 ! 1: 217f3eb6d2 bulk-checkin: remove ODB transaction nesting
@@ Commit message
optimized via ODB transactions without having to worry if a transaction
has already been started or not. This can make the interface a bit
awkward to use, as calling {begin,end}_odb_transaction() does not
- guarantee that a transaction is actually started or ended.
+ guarantee that a transaction is actually started or ended. Thus, in
+ situations where a transaction must be explicitly flushed,
+ flush_odb_transaction() must be used.
- Instead, be more explicit and require callers who use ODB transactions
- internally to ensure there is not already a pending transaction before
- beginning or ending a transaction.
+ To better clarify ownership sematics around a transaction and further
+ remove the need for flush_odb_transaction() as part of the transaction
+ interface, instead be more explicit and require callers who use ODB
+ transactions internally to ensure there is not already a pending
+ transaction before beginning or ending a transaction.
Signed-off-by: Justin Tobler <[email protected]>
@@ bulk-checkin.c: void flush_odb_transaction(struct odb_transaction *transaction)
- transaction->nesting -= 1;
-
- if (transaction->nesting)
-- return;
--
++ if (!transaction)
+ return;
+
++ /*
++ * Ensure the transaction ending matches the pending transaction.
++ */
++ ASSERT(transaction == transaction->odb->transaction);
++
flush_odb_transaction(transaction);
transaction->odb->transaction = NULL;
free(transaction);
@@ cache-tree.c: int cache_tree_update(struct index_state *istate, int flags)
+
i = update_one(istate->cache_tree, istate->cache, istate->cache_nr,
"", 0, &skip, flags);
-- end_odb_transaction(transaction);
+
-+ if (transaction)
-+ end_odb_transaction(transaction);
+ end_odb_transaction(transaction);
+
trace2_region_leave("cache_tree", "update", the_repository);
trace_performance_leave("cache_tree_update");
@@ object-file.c: int index_fd(struct index_state *istate, struct object_id *oid,
+ ret = index_blob_bulk_checkin(the_repository->objects->transaction,
oid, fd, xsize_t(st->st_size),
path, flags);
-- end_odb_transaction(transaction);
+
-+ if (transaction)
-+ end_odb_transaction(transaction);
+ end_odb_transaction(transaction);
}
- close(fd);
## read-cache.c ##
@@ read-cache.c: int add_files_to_cache(struct repository *repo, const char *prefix,
@@ read-cache.c: int add_files_to_cache(struct repository *repo, const char *prefix
+ transaction = begin_odb_transaction(repo->objects);
+
run_diff_files(&rev, DIFF_RACY_IS_MODIFIED);
-- end_odb_transaction(transaction);
+
-+ if (transaction)
-+ end_odb_transaction(transaction);
+ end_odb_transaction(transaction);
release_revisions(&rev);
- return !!data.add_errors;2: 6d9cb6c9f7 ! 2: 16af9f1169 builtin/update-index: end ODB transaction when --verbose is specified
@@ Commit message
object directory and migrated to the primary object database upon transaction commit.
- When the --verbose option is specified, each individual object is
+ When the --verbose option is specified, each of the following objects is
explicitly flushed via flush_odb_transaction() prior to reporting the update. Flushing the object database transaction migrates pending objects to the primary object database without marking the transaction
@@ Commit message
git-update-index(1) callers using the --verbose option and that rely on parsing verbose output to know when objects are written.
- As soon as verbose output is requested in git-update-index(1), all
+ Due to how git-update-index(1) parses options, each filename argument is
+ evaluated with only the set of options that precede it. Therefore, it is
+ possible for an initial set of objects to be written in a transaction
+ before a --verbose option is encountered.
+
+ As soon as the --verbose option is parsed in git-update-index(1), all
subsequent object writes are flushed prior to being reported and thus no longer benefit from being transactional. Furthermore, the mechanism to flush a transaction without committing is rather awkward. Drop the call
@@ builtin/update-index.c: int cmd_update_index(int argc,
setup_work_tree();
p = prefix_path(prefix, prefix_length, path); update_one(p);
-@@ builtin/update-index.c: int cmd_update_index(int argc,
- /*
- * By now we have added all of the new objects
- */
-- end_odb_transaction(transaction);
-+ if (transaction)
-+ end_odb_transaction(transaction);
-
- if (split_index > 0) {
- if (repo_config_get_split_index(the_repository) == 0)
3: 9b290c72fa ! 3: ae6199a3c8 bulk-checkin: drop flush_odb_transaction()
@@ bulk-checkin.c: struct odb_transaction *begin_odb_transaction(struct object_data
}
-void flush_odb_transaction(struct odb_transaction *transaction)
-+void end_odb_transaction(struct odb_transaction *transaction)
- {
+-{
- if (!transaction)
- return;
-
- flush_batch_fsync(transaction);
- flush_bulk_checkin_packfile(transaction);
+- flush_batch_fsync(transaction);
+- flush_bulk_checkin_packfile(transaction);
-}
-
--void end_odb_transaction(struct odb_transaction *transaction)
--{
+ void end_odb_transaction(struct odb_transaction *transaction)
+ {
+ if (!transaction)
+@@ bulk-checkin.c: void end_odb_transaction(struct odb_transaction *transaction)
+ */
+ ASSERT(transaction == transaction->odb->transaction);
+
- flush_odb_transaction(transaction);
++ flush_batch_fsync(transaction);
++ flush_bulk_checkin_packfile(transaction);
transaction->odb->transaction = NULL;
free(transaction);
}
4: 390a4e177e ! 4: 01f5485441 object-file: relocate ODB transaction code
@@ bulk-checkin.c (deleted)
-
-void end_odb_transaction(struct odb_transaction *transaction)
-{
+- if (!transaction)
+- return;
+-
+- /*
+- * Ensure the transaction ending matches the pending transaction.
+- */
+- ASSERT(transaction == transaction->odb->transaction);
+-
- flush_batch_fsync(transaction);
- flush_bulk_checkin_packfile(transaction);
- transaction->odb->transaction = NULL;
@@ object-file.c: int read_loose_object(struct repository *repo,
+
+void end_odb_transaction(struct odb_transaction *transaction)
+{
++ if (!transaction)
++ return;
++
++ /*
++ * Ensure the transaction ending matches the pending transaction.
++ */
++ ASSERT(transaction == transaction->odb->transaction);
++
+ flush_batch_fsync(transaction);
+ flush_bulk_checkin_packfile(transaction);
+ transaction->odb->transaction = NULL;5: cad54137b7 ! 5: 333319a63d object-file: update naming from bulk-checkin
@@ object-file.c: int index_fd(struct index_state *istate, struct object_id *oid,
+ xsize_t(st->st_size),
+ path, flags);
- if (transaction)
- end_odb_transaction(transaction);
-@@ object-file.c: struct odb_transaction *begin_odb_transaction(struct object_database *odb)
+ end_odb_transaction(transaction);
+ }
+@@ object-file.c: void end_odb_transaction(struct odb_transaction *transaction)
+ */
+ ASSERT(transaction == transaction->odb->transaction);
- void end_odb_transaction(struct odb_transaction *transaction)
- {
- flush_batch_fsync(transaction);
- flush_bulk_checkin_packfile(transaction);
+ flush_loose_object_transaction(transaction);
6: b74fe6c392 ! 6: 30759bbd0d odb: add transaction interface
@@ Commit message
Update call sites to instead manage transactions through the odb
subsystem. Also rename {begin,end}_odb_transaction() functions to
- object_file_transaction_{begin,end}() to clarify the object source it
+ object_file_transaction_{begin,commit}() to clarify the object source it
supports.
Signed-off-by: Justin Tobler <[email protected]>
@@ builtin/update-index.c: int cmd_update_index(int argc,
}
@@ builtin/update-index.c: int cmd_update_index(int argc,
+ /*
* By now we have added all of the new objects
*/
- if (transaction)
-- end_odb_transaction(transaction);
-+ odb_transaction_commit(transaction);
+- end_odb_transaction(transaction);
++ odb_transaction_commit(transaction);
if (split_index > 0) {
if (repo_config_get_split_index(the_repository) == 0)
@@ cache-tree.c: int cache_tree_update(struct index_state *istate, int flags)
i = update_one(istate->cache_tree, istate->cache, istate->cache_nr,
"", 0, &skip, flags);
- if (transaction)
-- end_odb_transaction(transaction);
-+ odb_transaction_commit(transaction);
+- end_odb_transaction(transaction);
++ odb_transaction_commit(transaction);
trace2_region_leave("cache_tree", "update", the_repository);
trace_performance_leave("cache_tree_update");
@@ object-file.c: int index_fd(struct index_state *istate, struct object_id *oid,
ret = index_blob_packfile_transaction(the_repository->objects->transaction,
oid, fd,
-@@ object-file.c: int index_fd(struct index_state *istate, struct object_id *oid,
+ xsize_t(st->st_size),
path, flags);
- if (transaction)
-- end_odb_transaction(transaction);
-+ odb_transaction_commit(transaction);
+- end_odb_transaction(transaction);
++ odb_transaction_commit(transaction);
}
close(fd);
@@ object-file.c: int read_loose_object(struct repository *repo,
}
-struct odb_transaction *begin_odb_transaction(struct object_database *odb)
-+struct odb_transaction *object_file_transaction_begin(struct object_database *odb)
++struct odb_transaction *object_file_transaction_begin(struct odb_source *source)
{
++ struct object_database *odb = source->odb;
++
if (odb->transaction)
BUG("ODB transaction already started");
+
@@ object-file.c: struct odb_transaction *begin_odb_transaction(struct object_database *odb)
return odb->transaction;
}
-void end_odb_transaction(struct odb_transaction *transaction)
-+void object_file_transaction_end(struct odb_transaction *transaction)
++void object_file_transaction_commit(struct odb_transaction *transaction)
{
- flush_loose_object_transaction(transaction);
- flush_packfile_transaction(transaction);
+ if (!transaction)
+ return;
## object-file.h ##
@@ object-file.h: struct odb_transaction;
@@ object-file.h: struct odb_transaction;
/*
* Tell the object database to optimize for adding
- * multiple objects. end_odb_transaction must be called
-+ * multiple objects. object_file_transaction_end must be called
++ * multiple objects. object_file_transaction_commit must be called
* to make new objects visible. Only a single transaction
* can be pending at a time and must be ended before
* beginning another.
*/
-struct odb_transaction *begin_odb_transaction(struct object_database *odb);
-+struct odb_transaction *object_file_transaction_begin(struct object_database *odb);
++struct odb_transaction *object_file_transaction_begin(struct odb_source *source);
/*
* Tell the object database to make any objects from the
* current transaction visible.
*/
-void end_odb_transaction(struct odb_transaction *transaction);
-+void object_file_transaction_end(struct odb_transaction *transaction);
++void object_file_transaction_commit(struct odb_transaction *transaction);
#endif /* OBJECT_FILE_H */
@@ odb.c: void odb_clear(struct object_database *o)
+
+struct odb_transaction *odb_transaction_begin(struct object_database *odb)
+{
-+ return object_file_transaction_begin(odb);
++ return object_file_transaction_begin(odb->sources);
+}
+
+void odb_transaction_commit(struct odb_transaction *transaction)
+{
-+ object_file_transaction_end(transaction);
++ object_file_transaction_commit(transaction);
+}
## odb.h ##
@@ odb.h: struct object_database {
struct object_database *odb_new(struct repository *repo);
void odb_clear(struct object_database *o);
++/*
++ * Starts an ODB transaction. Subsequent objects are written to the transaction
++ * and not committed until odb_transaction_commit() is invoked on the
++ * transaction. Caller are responsible to ensure there is only a single ODB
++ * transaction pending at a time.
++ */
+struct odb_transaction *odb_transaction_begin(struct object_database *odb);
++
++/*
++ * Commits an ODB transaction making the written objects visible. If the
++ * specified transaction is NULL, the function is a no-op.
++ */
+void odb_transaction_commit(struct odb_transaction *transaction);
+
/*
@@ read-cache.c: int add_files_to_cache(struct repository *repo, const char *prefix
run_diff_files(&rev, DIFF_RACY_IS_MODIFIED);
- if (transaction)
-- end_odb_transaction(transaction);
-+ odb_transaction_commit(transaction);
+- end_odb_transaction(transaction);
++ odb_transaction_commit(transaction);
release_revisions(&rev);
return !!data.add_errors;Signature · 1 line
2.51.0.193.g4975ec3473b
PATCH v2 1/6 bulk-checkin: remove ODB transaction nesting
ODB transactions support being nested. Only the outermost {begin,end}_odb_transaction() start and finish a transaction. This is done so that certain object write codepaths that occur internally can be optimized via ODB transactions without having to worry if a transaction has already been started or not. This can make the interface a bit awkward to use, as calling {begin,end}_odb_transaction() does not guarantee that a transaction is actually started or ended. Thus, in situations where a transaction must be explicitly flushed, flush_odb_transaction() must be used.
To better clarify ownership sematics around a transaction and further remove the need for flush_odb_transaction() as part of the transaction interface, instead be more explicit and require callers who use ODB transactions internally to ensure there is not already a pending transaction before beginning or ending a transaction.
--- bulk-checkin.c | 22 ++++++++++------------ bulk-checkin.h | 9 ++++----- cache-tree.c | 9 +++++++-- object-file.c | 9 ++++++--- read-cache.c | 7 +++++-- 5 files changed, 32 insertions(+), 24 deletions(-)
bulk-checkin.c +10 -12
@@ -33,7 +33,6 @@ struct bulk_checkin_packfile {struct odb_transaction {struct object_database *odb; -int nesting;struct tmp_objdir *objdir;struct bulk_checkin_packfile packfile; };
@@ -368,12 +367,11 @@ void fsync_loose_object_bulk_checkin(struct odb_transaction *transaction,struct odb_transaction *begin_odb_transaction (struct object_database *odb) { -if (!odb->transaction) { -CALLOC_ARRAY (odb->transaction,1 ); - odb->transaction->odb = odb; - } +if (odb->transaction) +BUG ("ODB transaction already started" ); - odb->transaction->nesting +=1 ; +CALLOC_ARRAY (odb->transaction,1 ); + odb->transaction->odb = odb;return odb->transaction; }
@@ -389,14 +387,14 @@ void flush_odb_transaction(struct odb_transaction *transaction)void end_odb_transaction (struct odb_transaction *transaction) { -if (!transaction || transaction->nesting ==0 ) -BUG ("Unbalanced ODB transaction nesting" ); - - transaction->nesting -=1 ; - -if (transaction->nesting) +if (!transaction)return ; +/* +* Ensure the transaction ending matches the pending transaction. +*/ +ASSERT (transaction == transaction->odb->transaction); +flush_odb_transaction (transaction); transaction->odb->transaction =NULL ;free (transaction);
bulk-checkin.h +4 -5
@@ -38,9 +38,9 @@ int index_blob_bulk_checkin(struct odb_transaction *transaction,/* * Tell the object database to optimize for adding * multiple objects. end_odb_transaction must be called -* to make new objects visible. Transactions can be nested, -* and objects are only visible after the outermost transaction -* is complete or the transaction is flushed. +* to make new objects visible. Only a single transaction +* can be pending at a time and must be ended before +* beginning another. */ struct odb_transaction *begin_odb_transaction (struct object_database *odb);
@@ -53,8 +53,7 @@ void flush_odb_transaction(struct odb_transaction *transaction);/* * Tell the object database to make any objects from the -* current transaction visible if this is the final nested -* transaction. +* current transaction visible. */ void end_odb_transaction (struct odb_transaction *transaction);
cache-tree.c +7 -2
@@ -474,7 +474,7 @@ static int update_one(struct cache_tree *it,int cache_tree_update (struct index_state *istate,int flags) { -struct odb_transaction *transaction; +struct odb_transaction *transaction =NULL ;int skip, i; i =verify_cache (istate, flags);
@@ -490,10 +490,15 @@ int cache_tree_update(struct index_state *istate, int flags)trace_performance_enter ();trace2_region_enter ("cache_tree" ,"update" , the_repository); - transaction =begin_odb_transaction (the_repository->objects); + +if (!the_repository->objects->transaction) + transaction =begin_odb_transaction (the_repository->objects); + i =update_one (istate->cache_tree, istate->cache, istate->cache_nr,"" ,0 , &skip, flags); +end_odb_transaction (transaction); +trace2_region_leave ("cache_tree" ,"update" , the_repository);trace_performance_leave ("cache_tree_update" );if (i <0 )
object-file.c +6 -3
@@ -1264,12 +1264,15 @@ int index_fd(struct index_state *istate, struct object_id *oid, ret =index_core (istate, oid, fd,xsize_t (st->st_size), type, path, flags); }else { -struct odb_transaction *transaction; +struct odb_transaction *transaction =NULL ; - transaction =begin_odb_transaction (the_repository->objects); - ret =index_blob_bulk_checkin (transaction, +if (!the_repository->objects->transaction) + transaction =begin_odb_transaction (the_repository->objects); + + ret =index_blob_bulk_checkin (the_repository->objects->transaction, oid, fd,xsize_t (st->st_size), path, flags); +end_odb_transaction (transaction); }
read-cache.c +5 -2
@@ -3947,7 +3947,7 @@ int add_files_to_cache(struct repository *repo, const char *prefix, const struct pathspec *pathspec, char *ps_matched, int include_sparse, int flags) { - struct odb_transaction *transaction; + struct odb_transaction *transaction = NULL;struct update_callback_data data;struct rev_info rev;
@@ -3973,8 +3973,11 @@ int add_files_to_cache(struct repository *repo, const char *prefix,* This function is invoked from commands other than 'add', which * may not have their own transaction active. */ - transaction =begin_odb_transaction (repo->objects); +if (!repo->objects->transaction) + transaction =begin_odb_transaction (repo->objects); +run_diff_files (&rev,DIFF_RACY_IS_MODIFIED ); +end_odb_transaction (transaction);release_revisions (&rev);
Signature · 1 line
2.51.0.193.g4975ec3473b
Re: [PATCH v2 1/6] bulk-checkin: remove ODB transaction nesting
Quoted 11 lines · Justin Tobler
ODB transactions support being nested. Only the outermost {begin,end}_odb_transaction() start and finish a transaction. This is done so that certain object write codepaths that occur internally can be optimized via ODB transactions without having to worry if a transaction has already been started or not. This can make the interface a bit awkward to use, as calling {begin,end}_odb_transaction() does not guarantee that a transaction is actually started or ended. Thus, in situations where a transaction must be explicitly flushed,
flush_odb_transaction()must be used.To better clarify ownership sematics around a transaction and further
s/smatics/semantics
Quoted 2 lines
remove the need for
flush_odb_transaction()as part of the transaction interface, instead be more explicit and require callers who use ODB
The first sentence doesn't flow into the second here. Perhaps s/instead//
Quoted 2 lines
transactions internally to ensure there is not already a pending transaction before beginning or ending a transaction.
[snip]
Quoted 21 lines
cache-tree.c +3
-2@@ -474,7 +474,7 @@ static int update_one(struct cache_tree *it,int cache_tree_update (struct index_state *istate,int flags) { -struct odb_transaction *transaction; +struct odb_transaction *transaction =NULL ;int skip, i; i =verify_cache (istate, flags);@@ -490,10 +490,15 @@ int cache_tree_update(struct index_state *istate, int flags)trace_performance_enter ();trace2_region_enter ("cache_tree" ,"update" , the_repository); - transaction =begin_odb_transaction (the_repository->objects); + +if (!the_repository->objects->transaction)
cache-tree.c int cache_tree_update(struct index_state *istate, int flags) · line 504
Quoted 10 lines
cache-tree.c +4
-0+ transaction =begin_odb_transaction (the_repository->objects); + i =update_one (istate->cache_tree, istate->cache, istate->cache_nr,"" ,0 , &skip, flags); +end_odb_transaction (transaction); +trace2_region_leave ("cache_tree" ,"update" , the_repository);trace_performance_leave ("cache_tree_update" );if (i <0 )
If there is an ongoing transaction, we don't create a new one. If we create a new transaction, we ensure we also close it. Makes sense.
I wish the parent transaction would be passed through to make it easier to understand, instead of deriving from a global variable. Nevertheless, this is a great improvement.
[snip]
Re: [PATCH v2 1/6] bulk-checkin: remove ODB transaction nesting
Quoted 12 lines · Karthik Nayak
Quoted 10 lines · Justin Tobler
ODB transactions support being nested. Only the outermost {begin,end}_odb_transaction() start and finish a transaction. This is done so that certain object write codepaths that occur internally can be optimized via ODB transactions without having to worry if a transaction has already been started or not. This can make the interface a bit awkward to use, as calling {begin,end}_odb_transaction() does not guarantee that a transaction is actually started or ended. Thus, in situations where a transaction must be explicitly flushed,
flush_odb_transaction()must be used.
Quoted 8 lines
Quoted 1 line
To better clarify ownership sematics around a transaction and further
s/smatics/semantics
Quoted 2 lines
remove the need for
flush_odb_transaction()as part of the transaction interface, instead be more explicit and require callers who use ODBThe first sentence doesn't flow into the second here. Perhaps s/instead//
Thanks. I'll fix these in the next version.
Quoted 26 lines
Quoted 26 lines
cache-tree.c +6
-2@@ -474,7 +474,7 @@ static int update_one(struct cache_tree *it,int cache_tree_update (struct index_state *istate,int flags) { -struct odb_transaction *transaction; +struct odb_transaction *transaction =NULL ;int skip, i; i =verify_cache (istate, flags);@@ -490,10 +490,15 @@ int cache_tree_update(struct index_state *istate, int flags)trace_performance_enter ();trace2_region_enter ("cache_tree" ,"update" , the_repository); - transaction =begin_odb_transaction (the_repository->objects); + +if (!the_repository->objects->transaction) + transaction =begin_odb_transaction (the_repository->objects); + i =update_one (istate->cache_tree, istate->cache, istate->cache_nr,"" ,0 , &skip, flags); +end_odb_transaction (transaction);
Quoted 8 lines
cache-tree.c int cache_tree_update(struct index_state *istate, int flags) · line 504
Quoted 5 lines
cache-tree.c +1
-0+trace2_region_leave ("cache_tree" ,"update" , the_repository);trace_performance_leave ("cache_tree_update" );if (i <0 )I wish the parent transaction would be passed through to make it easier to understand, instead of deriving from a global variable. Nevertheless, this is a great improvement.
This should be cleaned up a bit in the next version by making begin_odb_transaction() function as a noop that returns NULL when the ODB already has a pending transaction. This allow us to at least avoid checking the transaction state from the global here. We will still have to derive the ODB from the_repository global for now though.
-Justin
PATCH v2 2/6 builtin/update-index: end ODB transaction when --verbose is specified
In reply to Justin ToblerWith 23a3a303 (update-index: use the bulk-checkin infrastructure, 2022-04-04), object database transactions were added to git-update-index(1) to facilitate writing objects in bulk. With transactions, newly added objects are instead written to a temporary object directory and migrated to the primary object database upon transaction commit.
When the --verbose option is specified, each of the following objects is explicitly flushed via flush_odb_transaction() prior to reporting the update. Flushing the object database transaction migrates pending objects to the primary object database without marking the transaction as complete. This is done so objects are immediately visible to git-update-index(1) callers using the --verbose option and that rely on parsing verbose output to know when objects are written.
Due to how git-update-index(1) parses options, each filename argument is evaluated with only the set of options that precede it. Therefore, it is possible for an initial set of objects to be written in a transaction before a --verbose option is encountered.
As soon as the --verbose option is parsed in git-update-index(1), all subsequent object writes are flushed prior to being reported and thus no longer benefit from being transactional. Furthermore, the mechanism to flush a transaction without committing is rather awkward. Drop the call to flush_odb_transaction() in favor of ending the transaction early when the --verbose flag is encountered.
--- builtin/update-index.c | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-)
builtin/update-index.c +15 -8
@@ -70,14 +70,6 @@ static void report(const char *fmt, ...)if (!verbose)return ; -/* -* It is possible, though unlikely, that a caller could use the verbose -* output to synchronize with addition of objects to the object -* database. The current implementation of ODB transactions leaves -* objects invisible while a transaction is active, so flush the -* transaction here before reporting a change made by update-index. -*/ -flush_odb_transaction (the_repository->objects->transaction);va_start (vp, fmt);vprintf (fmt, vp);putchar ('\n' );
@@ -1150,6 +1142,21 @@ int cmd_update_index(int argc,const char *path = ctx.argv[0 ];char *p; +/* +* It is possible, though unlikely, that a caller could +* use the verbose output to synchronize with addition +* of objects to the object database. The current +* implementation of ODB transactions leaves objects +* invisible while a transaction is active, so end the +* transaction here early before processing the next +* update. All further updates are performed outside of +* a transaction. +*/ +if (transaction && verbose) { +end_odb_transaction (transaction); + transaction =NULL ; + } +setup_work_tree (); p =prefix_path (prefix, prefix_length, path);update_one (p);
Signature · 1 line
2.51.0.193.g4975ec3473b
Re: [PATCH v2 2/6] builtin/update-index: end ODB transaction when --verbose is specified
Quoted 60 lines · Justin Tobler
With
23a3a303(update-index: use the bulk-checkin infrastructure, 2022-04-04), object database transactions were added to git-update-index(1) to facilitate writing objects in bulk. With transactions, newly added objects are instead written to a temporary object directory and migrated to the primary object database upon transaction commit.When the --verbose option is specified, each of the following objects is explicitly flushed via
flush_odb_transaction()prior to reporting the update. Flushing the object database transaction migrates pending objects to the primary object database without marking the transaction as complete. This is done so objects are immediately visible to git-update-index(1) callers using the --verbose option and that rely on parsing verbose output to know when objects are written.Due to how git-update-index(1) parses options, each filename argument is evaluated with only the set of options that precede it. Therefore, it is possible for an initial set of objects to be written in a transaction before a --verbose option is encountered.
As soon as the --verbose option is parsed in git-update-index(1), all subsequent object writes are flushed prior to being reported and thus no longer benefit from being transactional. Furthermore, the mechanism to flush a transaction without committing is rather awkward. Drop the call to
flush_odb_transaction()in favor of ending the transaction early when the --verbose flag is encountered.Signed-off-by Justin Tobler <[email protected]>--- builtin/update-index.c | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-)builtin/update-index.c +5
-8@@ -70,14 +70,6 @@ static void report(const char *fmt, ...)if (!verbose)return ; -/* -* It is possible, though unlikely, that a caller could use the verbose -* output to synchronize with addition of objects to the object -* database. The current implementation of ODB transactions leaves -* objects invisible while a transaction is active, so flush the -* transaction here before reporting a change made by update-index. -*/ -flush_odb_transaction (the_repository->objects->transaction);va_start (vp, fmt);vprintf (fmt, vp);putchar ('\n' );@@ -1150,6 +1142,21 @@ int cmd_update_index(int argc,const char *path = ctx.argv[0 ];char *p; +/* +* It is possible, though unlikely, that a caller could +* use the verbose output to synchronize with addition +* of objects to the object database. The current +* implementation of ODB transactions leaves objects
builtin/update-index.c int cmd_update_index(int argc, · line 1159
Quoted 10 lines
builtin/update-index.c +10
-0+* invisible while a transaction is active, so end the +* transaction here early before processing the next +* update. All further updates are performed outside of +* a transaction. +*/ +if (transaction && verbose) { +end_odb_transaction (transaction); + transaction =NULL ; + } +
So with this change, we now have all objects updated before the --verbose flag updated via a single transaction. Updates after the --verbose flag will no longer use a transaction.
The older version would flush the transaction on every report, is there is any benefits to the new flow with regards to performance?
Quoted 5 lines
builtin/update-index.c +0
-1setup_work_tree (); p =prefix_path (prefix, prefix_length, path);update_one (p); --2.51.0.193.g4975ec3473b
Re: [PATCH v2 2/6] builtin/update-index: end ODB transaction when --verbose is specified
Quoted 21 lines · Karthik Nayak
Quoted 5 lines · Justin Tobler
builtin/update-index.c +5
-0+/* +* It is possible, though unlikely, that a caller could +* use the verbose output to synchronize with addition +* of objects to the object database. The current +* implementation of ODB transactions leaves objects builtin/update-index.c int cmd_update_index(int argc, · line 1159
Quoted 10 lines
builtin/update-index.c +10
-0+* invisible while a transaction is active, so end the +* transaction here early before processing the next +* update. All further updates are performed outside of +* a transaction. +*/ +if (transaction && verbose) { +end_odb_transaction (transaction); + transaction =NULL ; + } +So with this change, we now have all objects updated before the
--verboseflag updated via a single transaction. Updates after the--verboseflag will no longer use a transaction.
Quoted 2 lines
The older version would flush the transaction on every report, is there is any benefits to the new flow with regards to performance?
The older version would only flush transaction during a report if the --verbose option is enabled. Object written without --verbose would not be immediately flushed on report.
In this version we have functionally the same thing, but instead of flushing the transaction every report, the transaction is ended and subsequent object writes are written outside of a transaction instead.
I'll try to clarify this in the commit message.
-Justin
PATCH v2 3/6 bulk-checkin: drop flush_odb_transaction()
In reply to Justin ToblerObject database transactions can be explicitly flushed via flush_odb_transaction() without actually completing the transaction. This makes the provided transactional interface a bit awkward. Now that there are no longer any flush_odb_transaction() call sites, drop the function to simplify the interface and further ensure that a transaction is only finalized when end_odb_transaction() is invoked.
--- bulk-checkin.c | 12 ++---------- bulk-checkin.h | 7 ------- 2 files changed, 2 insertions(+), 17 deletions(-)
bulk-checkin.c +2 -10
@@ -376,15 +376,6 @@ struct odb_transaction *begin_odb_transaction(struct object_database *odb)return odb->transaction; } -void flush_odb_transaction (struct odb_transaction *transaction) -{ -if (!transaction) -return ; - -flush_batch_fsync (transaction); -flush_bulk_checkin_packfile (transaction); -} -void end_odb_transaction (struct odb_transaction *transaction) {if (!transaction)
@@ -395,7 +386,8 @@ void end_odb_transaction(struct odb_transaction *transaction)*/ ASSERT (transaction == transaction->odb->transaction); -flush_odb_transaction (transaction); +flush_batch_fsync (transaction); +flush_bulk_checkin_packfile (transaction); transaction->odb->transaction =NULL ;free (transaction); }
bulk-checkin.h +0 -7
@@ -44,13 +44,6 @@ int index_blob_bulk_checkin(struct odb_transaction *transaction,*/ struct odb_transaction *begin_odb_transaction (struct object_database *odb); -/* -* Make any objects that are currently part of a pending object -* database transaction visible. It is valid to call this function -* even if no transaction is active. -*/ -void flush_odb_transaction (struct odb_transaction *transaction); -/* * Tell the object database to make any objects from the * current transaction visible.
Signature · 1 line
2.51.0.193.g4975ec3473b
PATCH v2 4/6 object-file: relocate ODB transaction code
In reply to Justin ToblerThe bulk-checkin subsystem provides various functions to manage ODB transactions. Apart from {begin,end}_odb_transaction(), these functions are only used by the object-file subsystem to manage aspects of a transaction implementation specific to the files object source.
Relocate all the transaction code in in bulk-checkin to object-file. This simplifies the exposed transaction interface by reducing it to only {begin,end}_odb_transaction(). Function and type names are adjusted in the subsequent commit to better fit the new location.
--- Makefile | 1 - builtin/add.c | 2 +- builtin/unpack-objects.c | 1 - builtin/update-index.c | 1 - bulk-checkin.c | 393 -------------------------------------- bulk-checkin.h | 53 ------ cache-tree.c | 1 - meson.build | 1 - object-file.c | 394 ++++++++++++++++++++++++++++++++++++++- object-file.h | 17 ++ read-cache.c | 1 - 11 files changed, 411 insertions(+), 454 deletions(-) delete mode 100644 bulk-checkin.c delete mode 100644 bulk-checkin.h
Makefile +0 -1
@@ -974,7 +974,6 @@ LIB_OBJS += blame.o LIB_OBJS += blob.o LIB_OBJS += bloom.o LIB_OBJS += branch.o -LIB_OBJS += bulk-checkin.o LIB_OBJS += bundle-uri.o LIB_OBJS += bundle.o LIB_OBJS += cache-tree.o
builtin/add.c +1 -1
@@ -14,13 +14,13 @@#include "gettext.h" #include "pathspec.h" #include "run-command.h" +#include "object-file.h" #include "parse-options.h" #include "path.h" #include "preload-index.h" #include "diff.h" #include "read-cache.h" #include "revision.h" -#include "bulk-checkin.h" #include "strvec.h" #include "submodule.h" #include "add-interactive.h"
builtin/unpack-objects.c +0 -1
@@ -2,7 +2,6 @@#define DISABLE_SIGN_COMPARE_WARNINGS #include "builtin.h" -#include "bulk-checkin.h" #include "config.h" #include "environment.h" #include "gettext.h"
builtin/update-index.c +0 -1
@@ -8,7 +8,6 @@#define DISABLE_SIGN_COMPARE_WARNINGS #include "builtin.h" -#include "bulk-checkin.h" #include "config.h" #include "environment.h" #include "gettext.h"
bulk-checkin.c deleted +0 -393
393 lines not on this page · raw message
bulk-checkin.h deleted +0 -53
53 lines not on this page · raw message
cache-tree.c +0 -1
@@ -8,7 +8,6 @@#include "tree.h" #include "tree-walk.h" #include "cache-tree.h" -#include "bulk-checkin.h" #include "object-file.h" #include "odb.h" #include "read-cache-ll.h"
meson.build +0 -1
@@ -287,7 +287,6 @@ libgit_sources = [ 'blob.c', 'bloom.c', 'branch.c', - 'bulk-checkin.c', 'bundle-uri.c', 'bundle.c', 'cache-tree.c',
object-file.c +393 -1
421 lines not on this page · raw message
object-file.h +17 -0
@@ -218,4 +218,21 @@ int read_loose_object(struct repository *repo,void **contents, struct object_info *oi); +struct odb_transaction ; + +/* +* Tell the object database to optimize for adding +* multiple objects. end_odb_transaction must be called +* to make new objects visible. Only a single transaction +* can be pending at a time and must be ended before +* beginning another. +*/ +struct odb_transaction *begin_odb_transaction (struct object_database *odb); + +/* +* Tell the object database to make any objects from the +* current transaction visible. +*/ +void end_odb_transaction (struct odb_transaction *transaction); +#endif /* OBJECT_FILE_H */
read-cache.c +0 -1
@@ -8,7 +8,6 @@#define DISABLE_SIGN_COMPARE_WARNINGS #include "git-compat-util.h" -#include "bulk-checkin.h" #include "config.h" #include "date.h" #include "diff.h"
Signature · 1 line
2.51.0.193.g4975ec3473b
PATCH v2 5/6 object-file: update naming from bulk-checkin
In reply to Justin ToblerUpdate the names of several functions and types relocated from the bulk-checkin subsystem for better clarity. Also drop finish_tmp_packfile() as a standalone function in favor of embedding it in flush_packfile_transaction() directly.
--- object-file.c | 80 +++++++++++++++++++++++---------------------------- 1 file changed, 36 insertions(+), 44 deletions(-)
object-file.c +36 -44
@@ -667,7 +667,7 @@ void hash_object_file(const struct git_hash_algo *algo, const void *buf,write_object_file_prepare (algo, buf, len, type, oid, hdr, &hdrlen); } -struct bulk_checkin_packfile { +struct transaction_packfile {char *pack_tmp_name;struct hashfile *f;off_t offset;
@@ -682,10 +682,10 @@ struct odb_transaction {struct object_database *odb;struct tmp_objdir *objdir; -struct bulk_checkin_packfile packfile; +struct transaction_packfile packfile; }; -static void prepare_loose_object_bulk_checkin (struct odb_transaction *transaction) +static void prepare_loose_object_transaction (struct odb_transaction *transaction) {/* * We lazily create the temporary object directory
@@ -701,7 +701,7 @@ static void prepare_loose_object_bulk_checkin(struct odb_transaction *transactiotmp_objdir_replace_primary_odb (transaction->objdir,0 ); } -static void fsync_loose_object_bulk_checkin (struct odb_transaction *transaction, +static void fsync_loose_object_transaction (struct odb_transaction *transaction,int fd,const char *filename) {/*
@@ -722,7 +722,7 @@ static void fsync_loose_object_bulk_checkin(struct odb_transaction *transaction,/* * Cleanup after batch-mode fsync_object_files. */ -static void flush_batch_fsync (struct odb_transaction *transaction) +static void flush_loose_object_transaction (struct odb_transaction *transaction) {struct strbuf temp_path =STRBUF_INIT ;struct tempfile *temp;
@@ -733,7 +733,7 @@ static void flush_batch_fsync(struct odb_transaction *transaction)/* * Issue a full hardware flush against a temporary file to ensure * that all objects are durable before any renames occur. The code in -* fsync_loose_object_bulk_checkin has already issued a writeout +* fsync_loose_object_transaction has already issued a writeout * request, but it has not flushed any writeback cache in the storage * hardware or any filesystem logs. This fsync call acts as a barrier * to ensure that the data in each new object file is durable before
@@ -762,7 +762,7 @@ static void close_loose_object(struct odb_source *source, goto out;if (batch_fsync_enabled (FSYNC_COMPONENT_LOOSE_OBJECT )) -fsync_loose_object_bulk_checkin (source->odb->transaction, fd, filename); +fsync_loose_object_transaction (source->odb->transaction, fd, filename);else if (fsync_object_files >0 )fsync_or_die (fd, filename);else
@@ -940,7 +940,7 @@ static int write_loose_object(struct odb_source *source,static struct strbuf filename =STRBUF_INIT ;if (batch_fsync_enabled (FSYNC_COMPONENT_LOOSE_OBJECT )) -prepare_loose_object_bulk_checkin (source->odb->transaction); +prepare_loose_object_transaction (source->odb->transaction);odb_loose_path (source, &filename, oid);
@@ -1029,7 +1029,7 @@ int stream_loose_object(struct odb_source *source,int hdrlen;if (batch_fsync_enabled (FSYNC_COMPONENT_LOOSE_OBJECT )) -prepare_loose_object_bulk_checkin (source->odb->transaction); +prepare_loose_object_transaction (source->odb->transaction);/* Since oid is not determined, save tmp file to odb path. */ strbuf_addf (&filename,"%s/" , source->path);
@@ -1349,10 +1349,10 @@ static int already_written(struct odb_transaction *transaction, }/* Lazily create backing packfile for the state */ -static void prepare_to_stream (struct odb_transaction *transaction, -unsigned flags) +static void prepare_packfile_transaction (struct odb_transaction *transaction, +unsigned flags) { -struct bulk_checkin_packfile *state = &transaction->packfile; +struct transaction_packfile *state = &transaction->packfile;if (!(flags &INDEX_WRITE_OBJECT ) || state->f)return ;
@@ -1381,7 +1381,7 @@ static void prepare_to_stream(struct odb_transaction *transaction,* status before calling us just in case we ask it to call us again * with a new pack. */ -static int stream_blob_to_pack (struct bulk_checkin_packfile *state, +static int stream_blob_to_pack (struct transaction_packfile *state,struct git_hash_ctx *ctx,off_t *already_hashed_to,int fd,size_t size,const char *path,unsigned flags)
@@ -1457,28 +1457,13 @@ static int stream_blob_to_pack(struct bulk_checkin_packfile *state,return 0 ; } -static void finish_tmp_packfile (struct odb_transaction *transaction, -struct strbuf *basename, -unsigned char hash[]) +static void flush_packfile_transaction (struct odb_transaction *transaction) { -struct bulk_checkin_packfile *state = &transaction->packfile; -struct repository *repo = transaction->odb->repo; -char *idx_tmp_name =NULL ; - -stage_tmp_packfiles (repo, basename, state->pack_tmp_name, - state->written, state->nr_written,NULL , - &state->pack_idx_opts, hash, &idx_tmp_name); -rename_tmp_packfile_idx (repo, basename, &idx_tmp_name); - -free (idx_tmp_name); -} - -static void flush_bulk_checkin_packfile (struct odb_transaction *transaction) -{ -struct bulk_checkin_packfile *state = &transaction->packfile; +struct transaction_packfile *state = &transaction->packfile;struct repository *repo = transaction->odb->repo;unsigned char hash[GIT_MAX_RAWSZ ];struct strbuf packname =STRBUF_INIT ; +char *idx_tmp_name =NULL ;if (!state->f)return ;
@@ -1503,11 +1488,16 @@ static void flush_bulk_checkin_packfile(struct odb_transaction *transaction)repo_get_object_directory (transaction->odb->repo), hash_to_hex_algop(hash, repo->hash_algo)); -finish_tmp_packfile (transaction, &packname, hash); +stage_tmp_packfiles (repo, &packname, state->pack_tmp_name, + state->written, state->nr_written,NULL , + &state->pack_idx_opts, hash, &idx_tmp_name); +rename_tmp_packfile_idx (repo, &packname, &idx_tmp_name); +for (uint32_t i =0 ; i < state->nr_written; i++)free (state->written[i]); clear_exit: +free (idx_tmp_name);free (state->pack_tmp_name);free (state->written);memset (state,0 ,sizeof (*state));
@@ -1535,11 +1525,12 @@ static void flush_bulk_checkin_packfile(struct odb_transaction *transaction)* binary blobs, they generally do not want to get any conversion, and * callers should avoid this code path when filters are requested. */ -static int index_blob_bulk_checkin (struct odb_transaction *transaction, -struct object_id *result_oid,int fd,size_t size, -const char *path,unsigned flags) +static int index_blob_packfile_transaction (struct odb_transaction *transaction, +struct object_id *result_oid,int fd, +size_t size,const char *path, +unsigned flags) { -struct bulk_checkin_packfile *state = &transaction->packfile; +struct transaction_packfile *state = &transaction->packfile;off_t seekback, already_hashed_to;struct git_hash_ctx ctx;unsigned char obuf[16384 ];
@@ -1560,14 +1551,14 @@ static int index_blob_bulk_checkin(struct odb_transaction *transaction,if ((flags &INDEX_WRITE_OBJECT ) !=0 ) {CALLOC_ARRAY (idx,1 ); -prepare_to_stream (transaction, flags); +prepare_packfile_transaction (transaction, flags);hashfile_checkpoint_init (state->f, &checkpoint); } already_hashed_to =0 ;while (1 ) { -prepare_to_stream (transaction, flags); +prepare_packfile_transaction (transaction, flags);if (idx) {hashfile_checkpoint (state->f, &checkpoint); idx->offset = state->offset;
@@ -1585,7 +1576,7 @@ static int index_blob_bulk_checkin(struct odb_transaction *transaction,BUG ("should not happen" );hashfile_truncate (state->f, &checkpoint); state->offset = checkpoint.offset; -flush_bulk_checkin_packfile (transaction); +flush_packfile_transaction (transaction);if (lseek (fd, seekback,SEEK_SET ) == (off_t)-1 )return error ("cannot seek back" ); }
@@ -1634,9 +1625,10 @@ int index_fd(struct index_state *istate, struct object_id *oid,if (!the_repository->objects->transaction) transaction =begin_odb_transaction (the_repository->objects); - ret =index_blob_bulk_checkin (the_repository->objects->transaction, - oid, fd,xsize_t (st->st_size), - path, flags); + ret =index_blob_packfile_transaction (the_repository->objects->transaction, + oid, fd, +xsize_t (st->st_size), + path, flags);end_odb_transaction (transaction); }
@@ -1999,8 +1991,8 @@ void end_odb_transaction(struct odb_transaction *transaction)*/ ASSERT (transaction == transaction->odb->transaction); -flush_batch_fsync (transaction); -flush_bulk_checkin_packfile (transaction); +flush_loose_object_transaction (transaction); +flush_packfile_transaction (transaction); transaction->odb->transaction =NULL ;free (transaction); }
Signature · 1 line
2.51.0.193.g4975ec3473b
PATCH v2 6/6 odb: add transaction interface
In reply to Justin ToblerTransactions are managed via the {begin,end}_odb_transaction() function in the object-file subsystem and its implementation is specific to the files object source. Introduce odb_transaction_{begin,commit}() in the odb subsystem to provide an eventual object source agnostic means to manage transactions.
Update call sites to instead manage transactions through the odb subsystem. Also rename {begin,end}_odb_transaction() functions to object_file_transaction_{begin,commit}() to clarify the object source it supports.
--- builtin/add.c | 5 +++-- builtin/unpack-objects.c | 4 ++-- builtin/update-index.c | 7 ++++--- cache-tree.c | 4 ++-- object-file.c | 12 +++++++----- object-file.h | 6 +++--- odb.c | 10 ++++++++++ odb.h | 14 ++++++++++++++ read-cache.c | 4 ++-- 9 files changed, 47 insertions(+), 19 deletions(-)
builtin/add.c +3 -2
@@ -15,6 +15,7 @@#include "pathspec.h" #include "run-command.h" #include "object-file.h" +#include "odb.h" #include "parse-options.h" #include "path.h" #include "preload-index.h"
@@ -575,7 +576,7 @@ int cmd_add(int argc,string_list_clear (&only_match_skip_worktree,0 ); } - transaction =begin_odb_transaction (repo->objects); + transaction =odb_transaction_begin (repo->objects); ps_matched =xcalloc (pathspec.nr,1 );if (add_renormalize)
@@ -594,7 +595,7 @@ int cmd_add(int argc,if (chmod_arg && pathspec.nr) exit_status |=chmod_pathspec (repo, &pathspec, chmod_arg[0 ], show_only); -end_odb_transaction (transaction); +odb_transaction_commit (transaction); finish: if (write_locked_index(repo->index, &lock_file,
builtin/unpack-objects.c +2 -2
@@ -599,12 +599,12 @@ static void unpack_all(void) progress =start_progress (the_repository,_ ("Unpacking objects" ), nr_objects);CALLOC_ARRAY (obj_list, nr_objects); - transaction =begin_odb_transaction (the_repository->objects); + transaction =odb_transaction_begin (the_repository->objects);for (i =0 ; i < nr_objects; i++) {unpack_one (i);display_progress (progress, i +1 ); } -end_odb_transaction (transaction); +odb_transaction_commit (transaction);stop_progress (&progress);if (delta_list)
builtin/update-index.c +4 -3
@@ -18,6 +18,7 @@#include "cache-tree.h" #include "tree-walk.h" #include "object-file.h" +#include "odb.h" #include "refs.h" #include "resolve-undo.h" #include "parse-options.h"
@@ -1122,7 +1123,7 @@ int cmd_update_index(int argc,* Allow the object layer to optimize adding multiple objects in * a batch. */ - transaction =begin_odb_transaction (the_repository->objects); + transaction =odb_transaction_begin (the_repository->objects);while (ctx.argc) {if (parseopt_state !=PARSE_OPT_DONE ) parseopt_state = parse_options_step(&ctx, options,
@@ -1152,7 +1153,7 @@ int cmd_update_index(int argc,* a transaction. */ if (transaction && verbose) { -end_odb_transaction (transaction); +odb_transaction_commit (transaction); transaction =NULL ; }
@@ -1220,7 +1221,7 @@ int cmd_update_index(int argc,/* * By now we have added all of the new objects */ -end_odb_transaction (transaction); +odb_transaction_commit (transaction);if (split_index >0 ) {if (repo_config_get_split_index (the_repository) ==0 )
cache-tree.c +2 -2
@@ -491,12 +491,12 @@ int cache_tree_update(struct index_state *istate, int flags)trace2_region_enter ("cache_tree" ,"update" , the_repository);if (!the_repository->objects->transaction) - transaction =begin_odb_transaction (the_repository->objects); + transaction =odb_transaction_begin (the_repository->objects); i =update_one (istate->cache_tree, istate->cache, istate->cache_nr,"" ,0 , &skip, flags); -end_odb_transaction (transaction); +odb_transaction_commit (transaction);trace2_region_leave ("cache_tree" ,"update" , the_repository);trace_performance_leave ("cache_tree_update" );
object-file.c +7 -5
@@ -691,7 +691,7 @@ static void prepare_loose_object_transaction(struct odb_transaction *transaction* We lazily create the temporary object directory * the first time an object might be added, since * callers may not know whether any objects will be -* added at the time they call begin_odb_transaction. +* added at the time they call object_file_transaction_begin. */ if (!transaction || transaction->objdir)return ;
@@ -1623,14 +1623,14 @@ int index_fd(struct index_state *istate, struct object_id *oid,struct odb_transaction *transaction =NULL ;if (!the_repository->objects->transaction) - transaction =begin_odb_transaction (the_repository->objects); + transaction =odb_transaction_begin (the_repository->objects); ret =index_blob_packfile_transaction (the_repository->objects->transaction, oid, fd,xsize_t (st->st_size), path, flags); -end_odb_transaction (transaction); +odb_transaction_commit (transaction); }close (fd);
@@ -1970,8 +1970,10 @@ int read_loose_object(struct repository *repo,return ret; } -struct odb_transaction *begin_odb_transaction (struct object_database *odb) +struct odb_transaction *object_file_transaction_begin (struct odb_source *source) { +struct object_database *odb = source->odb; +if (odb->transaction)BUG ("ODB transaction already started" );
@@ -1981,7 +1983,7 @@ struct odb_transaction *begin_odb_transaction(struct object_database *odb)return odb->transaction; } -void end_odb_transaction (struct odb_transaction *transaction) +void object_file_transaction_commit (struct odb_transaction *transaction) {if (!transaction)return ;
object-file.h +3 -3
@@ -222,17 +222,17 @@ struct odb_transaction;/* * Tell the object database to optimize for adding -* multiple objects. end_odb_transaction must be called +* multiple objects. object_file_transaction_commit must be called * to make new objects visible. Only a single transaction * can be pending at a time and must be ended before * beginning another. */ -struct odb_transaction *begin_odb_transaction (struct object_database *odb); +struct odb_transaction *object_file_transaction_begin (struct odb_source *source);/* * Tell the object database to make any objects from the * current transaction visible. */ -void end_odb_transaction (struct odb_transaction *transaction); +void object_file_transaction_commit (struct odb_transaction *transaction);#endif /* OBJECT_FILE_H */
odb.c +10 -0
@@ -1051,3 +1051,13 @@ void odb_clear(struct object_database *o)hashmap_clear (&o->pack_map);string_list_clear (&o->submodule_source_paths,0 ); } + +struct odb_transaction *odb_transaction_begin (struct object_database *odb) +{ +return object_file_transaction_begin (odb->sources); +} + +void odb_transaction_commit (struct odb_transaction *transaction) +{ +object_file_transaction_commit (transaction); +}
odb.h +14 -0
@@ -185,6 +185,20 @@ struct object_database {struct object_database *odb_new (struct repository *repo);void odb_clear (struct object_database *o); +/* +* Starts an ODB transaction. Subsequent objects are written to the transaction +* and not committed until odb_transaction_commit() is invoked on the +* transaction. Caller are responsible to ensure there is only a single ODB +* transaction pending at a time. +*/ +struct odb_transaction *odb_transaction_begin (struct object_database *odb); + +/* +* Commits an ODB transaction making the written objects visible. If the +* specified transaction is NULL, the function is a no-op. +*/ +void odb_transaction_commit (struct odb_transaction *transaction); +/* * Find source by its object directory path. Dies in case the source couldn't * be found.
read-cache.c +2 -2
@@ -3973,11 +3973,11 @@ int add_files_to_cache(struct repository *repo, const char *prefix,* may not have their own transaction active. */ if (!repo->objects->transaction) - transaction =begin_odb_transaction (repo->objects); + transaction =odb_transaction_begin (repo->objects);run_diff_files (&rev,DIFF_RACY_IS_MODIFIED ); -end_odb_transaction (transaction); +odb_transaction_commit (transaction);release_revisions (&rev);return !!data.add_errors;
Signature · 1 line
2.51.0.193.g4975ec3473b
v3 cover odb: add transaction interfaces to ODB subsystem
In reply to Justin ToblerGreetings,
This series is a followup to [1] and continues iterating on the ODB transaction interfaces.
The bulk-checkin subsystem provides an interface to manage ODB transactions. Apart from {begin,end}_odb_transaction(), these functions are only used by the object-file subsystem to manage aspects of a transaction implementation specific to the files object source.
In a pluggable object database future where we could have different types of object database sources, transaction handling will have to be implemented separately per source. Thus, the primary focus of this series is to simplify the existing ODB transaction interface and provide a means to manage transactions via the ODB subsystem in an object source agnostic manner eventually.
This series is built on top of 4975ec3473b (The seventh batch, 2025-09-08) with jt/de-global-bulk-checkin merged into it at ddc0b56ad77 (bulk-checkin: use repository variable from transaction, 2025-08-22).
Changes since V1:
- end_odb_transaction() is now a no-op when no transaction is specified and also guards against the ending a transaction not set in the object database.
- The object_file_transaction_begin() now accepts a struct odb_source instead of struct odbject_database. Which is more in line with it being a transaction implementation specific to an object source.
- Further clarified some commit messages.
- Renamed object_file_transaction_end() to object_file_transaction_commit() to match the corresponding function in the ODB section.
- Add some missing documentation for odb_transaction_{begin,commit}().
Change since V2:
- begin_odb_transaction() now behaves as a no-op and returns a NULL transaction value when the ODB already has an active transaction. Callers are no longer required to manually check if there is an active transaction beforehand.
- Reworded several commit messages.
Thanks,
-Justin
[1]: <[email protected]>
Justin Tobler (6):
bulk-checkin: remove ODB transaction nesting builtin/update-index: end ODB transaction when --verbose is specified
bulk-checkin: drop flush_odb_transaction()
object-file: relocate ODB transaction code
object-file: update naming from bulk-checkin
odb: add transaction interface
Makefile | 1 - builtin/add.c | 7 +- builtin/unpack-objects.c | 5 +- builtin/update-index.c | 29 +-- bulk-checkin.c | 403 -------------------------------------- bulk-checkin.h | 61 ------ cache-tree.c | 5 +- meson.build | 1 - object-file.c | 404 ++++++++++++++++++++++++++++++++++++++- object-file.h | 16 ++ odb.c | 10 + odb.h | 13 ++ read-cache.c | 5 +- 13 files changed, 462 insertions(+), 498 deletions(-) delete mode 100644 bulk-checkin.c delete mode 100644 bulk-checkin.h
Range-diff against v2:
1: 217f3eb6d2 ! 1: 9fe2ab4198 bulk-checkin: remove ODB transaction nesting
@@ Commit message
bulk-checkin: remove ODB transaction nesting
ODB transactions support being nested. Only the outermost
- {begin,end}_odb_transaction() start and finish a transaction. This is
- done so that certain object write codepaths that occur internally can be
- optimized via ODB transactions without having to worry if a transaction
- has already been started or not. This can make the interface a bit
- awkward to use, as calling {begin,end}_odb_transaction() does not
- guarantee that a transaction is actually started or ended. Thus, in
- situations where a transaction must be explicitly flushed,
- flush_odb_transaction() must be used.
+ {begin,end}_odb_transaction() start and finish a transaction. This
+ allows internal object write codepaths to be optimized with ODB
+ transactions without worrying about whether a transaction is already
+ active. When {begin,end}_odb_transaction() is invoked during an active
+ transaction, these operations are essentially treated as no-ops. This
+ can make the interface a bit awkward to use, as calling
+ end_odb_transaction() does not guarantee that a transaction is actually
+ ended. Thus, in situations where a transaction needs to be explicitly
+ flushed, flush_odb_transaction() must be used.
- To better clarify ownership sematics around a transaction and further
- remove the need for flush_odb_transaction() as part of the transaction
- interface, instead be more explicit and require callers who use ODB
- transactions internally to ensure there is not already a pending
- transaction before beginning or ending a transaction.
+ To remove the need for an explicit transaction flush operation via
+ flush_odb_transaction() and better clarify transaction semantics, drop
+ the transaction nesting mechanism in favor of begin_odb_transaction()
+ returning a NULL transaction value to signal it was a no-op, and
+ end_odb_transaction() behaving as a no-op when a NULL transaction value
+ is passed. This is safe for existing callers as the transaction value
+ wired to end_odb_transaction() already comes from
+ begin_odb_transaction() and thus continues the same no-op behavior when
+ a transaction is already pending. With this model, passing a pending
+ transaction to end_odb_transaction() ensures it is committed at that
+ point in time.
@@ bulk-checkin.c: void fsync_loose_object_bulk_checkin(struct odb_transaction *tra
- odb->transaction->odb = odb;
- }
+ if (odb->transaction)
-+ BUG("ODB transaction already started");
++ return NULL;
- odb->transaction->nesting += 1;
+ CALLOC_ARRAY(odb->transaction, 1);
@@ bulk-checkin.h: int index_blob_bulk_checkin(struct odb_transaction *transaction,
- * to make new objects visible. Transactions can be nested,
- * and objects are only visible after the outermost transaction
- * is complete or the transaction is flushed.
-+ * to make new objects visible. Only a single transaction
-+ * can be pending at a time and must be ended before
-+ * beginning another.
++ * to make new objects visible. If a transaction is already
++ * pending, NULL is returned.
*/
struct odb_transaction *begin_odb_transaction(struct object_database *odb);
@@ bulk-checkin.h: void flush_odb_transaction(struct odb_transaction *transaction);
void end_odb_transaction(struct odb_transaction *transaction);
- ## cache-tree.c ##
-@@ cache-tree.c: static int update_one(struct cache_tree *it,
-
- int cache_tree_update(struct index_state *istate, int flags)
- {
-- struct odb_transaction *transaction;
-+ struct odb_transaction *transaction = NULL;
- int skip, i;
-
- i = verify_cache(istate, flags);
-@@ cache-tree.c: int cache_tree_update(struct index_state *istate, int flags) -
- trace_performance_enter();
- trace2_region_enter("cache_tree", "update", the_repository);
-- transaction = begin_odb_transaction(the_repository->objects);
-+
-+ if (!the_repository->objects->transaction)
-+ transaction = begin_odb_transaction(the_repository->objects);
-+
- i = update_one(istate->cache_tree, istate->cache, istate->cache_nr,
- "", 0, &skip, flags);
-+
- end_odb_transaction(transaction);
-+
- trace2_region_leave("cache_tree", "update", the_repository);
- trace_performance_leave("cache_tree_update");
- if (i < 0)
-
## object-file.c ##
@@ object-file.c: int index_fd(struct index_state *istate, struct object_id *oid,
- ret = index_core(istate, oid, fd, xsize_t(st->st_size),
- type, path, flags);
- } else {
-- struct odb_transaction *transaction;
-+ struct odb_transaction *transaction = NULL;
+ struct odb_transaction *transaction;
-- transaction = begin_odb_transaction(the_repository->objects);
+ transaction = begin_odb_transaction(the_repository->objects);
- ret = index_blob_bulk_checkin(transaction,
-+ if (!the_repository->objects->transaction)
-+ transaction = begin_odb_transaction(the_repository->objects);
-+
+ ret = index_blob_bulk_checkin(the_repository->objects->transaction,
oid, fd, xsize_t(st->st_size),
path, flags);
-+
end_odb_transaction(transaction);
- }
-
-
- ## read-cache.c ##
-@@ read-cache.c: int add_files_to_cache(struct repository *repo, const char *prefix,
- const struct pathspec *pathspec, char *ps_matched,
- int include_sparse, int flags)
- {
-- struct odb_transaction *transaction;
-+ struct odb_transaction *transaction = NULL;
- struct update_callback_data data;
- struct rev_info rev;
-
-@@ read-cache.c: int add_files_to_cache(struct repository *repo, const char *prefix,
- * This function is invoked from commands other than 'add', which
- * may not have their own transaction active.
- */
-- transaction = begin_odb_transaction(repo->objects);
-+ if (!repo->objects->transaction)
-+ transaction = begin_odb_transaction(repo->objects);
-+
- run_diff_files(&rev, DIFF_RACY_IS_MODIFIED);
-+
- end_odb_transaction(transaction);
-
- release_revisions(&rev);
2: 16af9f1169 ! 2: f24c28ec56 builtin/update-index: end ODB transaction when --verbose is specified
@@ Commit message
object directory and migrated to the primary object database upon transaction commit.
- When the --verbose option is specified, each of the following objects is
- explicitly flushed via flush_odb_transaction() prior to reporting the
- update. Flushing the object database transaction migrates pending
- objects to the primary object database without marking the transaction
- as complete. This is done so objects are immediately visible to
- git-update-index(1) callers using the --verbose option and that rely on
- parsing verbose output to know when objects are written.
+ When the --verbose option is specified, the subsequent set of objects
+ written are explicitly flushed via flush_odb_transaction() prior to
+ reporting the update. Flushing the object database transaction migrates
+ pending objects to the primary object database without marking the
+ transaction as complete. This is done so objects are immediately visible
+ to git-update-index(1) callers using the --verbose option and that rely
+ on parsing verbose output to know when objects are written.
- Due to how git-update-index(1) parses options, each filename argument is
- evaluated with only the set of options that precede it. Therefore, it is
- possible for an initial set of objects to be written in a transaction
- before a --verbose option is encountered.
+ Due to how git-update-index(1) parses arguments, options that come after
+ a filename are not considered during the object update. Therefore, it
+ may not be known ahead of time whether the --verbose option is present
+ and thus object writes are considered transactional by default until a
+ --verbose option is parsed.
- As soon as the --verbose option is parsed in git-update-index(1), all
- subsequent object writes are flushed prior to being reported and thus no
- longer benefit from being transactional. Furthermore, the mechanism to
- flush a transaction without committing is rather awkward. Drop the call
- to flush_odb_transaction() in favor of ending the transaction early when
- the --verbose flag is encountered.
+ Flushing a transaction after individual object writes negates the
+ benefit of writing objects to a transaction in the first place.
+ Furthermore, the mechanism to flush a transaction without actually
+ committing is rather awkward. Drop the call to flush_odb_transaction()
+ in favor of ending the transaction altogether when the --verbose flag is
+ encountered. Subsequent object writes occur outside of a transaction and
+ are therefore immediately visible which matches the current behavior.
3: ae6199a3c8 = 3: 6268b2934a bulk-checkin: drop flush_odb_transaction() 4: 01f5485441 ! 4: e5f1d93797 object-file: relocate ODB transaction code
@@ Commit message
are only used by the object-file subsystem to manage aspects of a transaction implementation specific to the files object source.
- Relocate all the transaction code in in bulk-checkin to object-file.
- This simplifies the exposed transaction interface by reducing it to only
+ Relocate all the transaction code in bulk-checkin to object-file. This
+ simplifies the exposed transaction interface by reducing it to only
{begin,end}_odb_transaction(). Function and type names are adjusted in the subsequent commit to better fit the new location.
@@ bulk-checkin.c (deleted)
-struct odb_transaction *begin_odb_transaction(struct object_database *odb) -{
- if (odb->transaction)
-- BUG("ODB transaction already started");
+- return NULL;
-
- CALLOC_ARRAY(odb->transaction, 1);
- odb->transaction->odb = odb;
@@ bulk-checkin.h (deleted)
-/*
- * Tell the object database to optimize for adding
- * multiple objects. end_odb_transaction must be called
-- * to make new objects visible. Only a single transaction
-- * can be pending at a time and must be ended before
-- * beginning another.
+- * to make new objects visible. If a transaction is already
+- * pending, NULL is returned.
- */
-struct odb_transaction *begin_odb_transaction(struct object_database *odb); -
@@ object-file.c: int read_loose_object(struct repository *repo,
+struct odb_transaction *begin_odb_transaction(struct object_database *odb) +{
+ if (odb->transaction)
-+ BUG("ODB transaction already started");
++ return NULL;
+
+ CALLOC_ARRAY(odb->transaction, 1);
+ odb->transaction->odb = odb;
@@ object-file.h: int read_loose_object(struct repository *repo,
+/*
+ * Tell the object database to optimize for adding
+ * multiple objects. end_odb_transaction must be called
-+ * to make new objects visible. Only a single transaction
-+ * can be pending at a time and must be ended before
-+ * beginning another.
++ * to make new objects visible. If a transaction is already
++ * pending, NULL is returned.
+ */
+struct odb_transaction *begin_odb_transaction(struct object_database *odb); +
5: 333319a63d ! 5: 76eabfb6a1 object-file: update naming from bulk-checkin
@@ object-file.c: static int index_blob_bulk_checkin(struct odb_transaction *transa
return error("cannot seek back");
}
@@ object-file.c: int index_fd(struct index_state *istate, struct object_id *oid,
- if (!the_repository->objects->transaction)
- transaction = begin_odb_transaction(the_repository->objects);
+ struct odb_transaction *transaction;
+ transaction = begin_odb_transaction(the_repository->objects);
- ret = index_blob_bulk_checkin(the_repository->objects->transaction,
- oid, fd, xsize_t(st->st_size),
- path, flags);
@@ object-file.c: int index_fd(struct index_state *istate, struct object_id *oid,
+ oid, fd,
+ xsize_t(st->st_size),
+ path, flags);
-
end_odb_transaction(transaction);
}
+
@@ object-file.c: void end_odb_transaction(struct odb_transaction *transaction)
*/
ASSERT(transaction == transaction->odb->transaction);
6: 30759bbd0d ! 6: 9accbfdbf0 odb: add transaction interface
@@ builtin/update-index.c: int cmd_update_index(int argc,
## cache-tree.c ##
@@ cache-tree.c: int cache_tree_update(struct index_state *istate, int flags)
- trace2_region_enter("cache_tree", "update", the_repository);
-
- if (!the_repository->objects->transaction)
-- transaction = begin_odb_transaction(the_repository->objects);
-+ transaction = odb_transaction_begin(the_repository->objects);
+ trace_performance_enter();
+ trace2_region_enter("cache_tree", "update", the_repository);
+- transaction = begin_odb_transaction(the_repository->objects);
++ transaction = odb_transaction_begin(the_repository->objects);
i = update_one(istate->cache_tree, istate->cache, istate->cache_nr,
"", 0, &skip, flags);
-
- end_odb_transaction(transaction);
+ odb_transaction_commit(transaction);
-
trace2_region_leave("cache_tree", "update", the_repository);
trace_performance_leave("cache_tree_update");
+ if (i < 0)
## object-file.c ##
@@ object-file.c: static void prepare_loose_object_transaction(struct odb_transaction *transaction
@@ object-file.c: static void prepare_loose_object_transaction(struct odb_transacti
if (!transaction || transaction->objdir)
return;
@@ object-file.c: int index_fd(struct index_state *istate, struct object_id *oid,
- struct odb_transaction *transaction = NULL;
-
- if (!the_repository->objects->transaction)
-- transaction = begin_odb_transaction(the_repository->objects);
-+ transaction = odb_transaction_begin(the_repository->objects);
+ } else {
+ struct odb_transaction *transaction;
+- transaction = begin_odb_transaction(the_repository->objects);
++ transaction = odb_transaction_begin(the_repository->objects);
ret = index_blob_packfile_transaction(the_repository->objects->transaction,
oid, fd,
xsize_t(st->st_size),
path, flags);
-
- end_odb_transaction(transaction);
+ odb_transaction_commit(transaction);
}
@@ object-file.c: int read_loose_object(struct repository *repo,
+ struct object_database *odb = source->odb;
+
if (odb->transaction)
- BUG("ODB transaction already started");
+ return NULL;
@@ object-file.c: struct odb_transaction *begin_odb_transaction(struct object_database *odb)
return odb->transaction;
@@ object-file.h: struct odb_transaction;
* Tell the object database to optimize for adding
- * multiple objects. end_odb_transaction must be called
+ * multiple objects. object_file_transaction_commit must be called
- * to make new objects visible. Only a single transaction
- * can be pending at a time and must be ended before
- * beginning another.
+ * to make new objects visible. If a transaction is already
+ * pending, NULL is returned.
*/
-struct odb_transaction *begin_odb_transaction(struct object_database *odb);
+struct odb_transaction *object_file_transaction_begin(struct odb_source *source);
@@ odb.h: struct object_database {
+/*
+ * Starts an ODB transaction. Subsequent objects are written to the transaction
+ * and not committed until odb_transaction_commit() is invoked on the
-+ * transaction. Caller are responsible to ensure there is only a single ODB
-+ * transaction pending at a time.
++ * transaction. If the ODB already has a pending transaction, NULL is returned.
+ */
+struct odb_transaction *odb_transaction_begin(struct object_database *odb);
+
@@ odb.h: struct object_database {
## read-cache.c ##
@@ read-cache.c: int add_files_to_cache(struct repository *repo, const char *prefix,
+ * This function is invoked from commands other than 'add', which
* may not have their own transaction active.
*/
- if (!repo->objects->transaction)
-- transaction = begin_odb_transaction(repo->objects);
-+ transaction = odb_transaction_begin(repo->objects);
-
+- transaction = begin_odb_transaction(repo->objects);
++ transaction = odb_transaction_begin(repo->objects);
run_diff_files(&rev, DIFF_RACY_IS_MODIFIED);
-
- end_odb_transaction(transaction);
+ odb_transaction_commit(transaction);Signature · 1 line
2.51.0.193.g4975ec3473b
PATCH v3 1/6 bulk-checkin: remove ODB transaction nesting
ODB transactions support being nested. Only the outermost {begin,end}_odb_transaction() start and finish a transaction. This allows internal object write codepaths to be optimized with ODB transactions without worrying about whether a transaction is already active. When {begin,end}_odb_transaction() is invoked during an active transaction, these operations are essentially treated as no-ops. This can make the interface a bit awkward to use, as calling end_odb_transaction() does not guarantee that a transaction is actually ended. Thus, in situations where a transaction needs to be explicitly flushed, flush_odb_transaction() must be used.
To remove the need for an explicit transaction flush operation via flush_odb_transaction() and better clarify transaction semantics, drop the transaction nesting mechanism in favor of begin_odb_transaction() returning a NULL transaction value to signal it was a no-op, and end_odb_transaction() behaving as a no-op when a NULL transaction value is passed. This is safe for existing callers as the transaction value wired to end_odb_transaction() already comes from begin_odb_transaction() and thus continues the same no-op behavior when a transaction is already pending. With this model, passing a pending transaction to end_odb_transaction() ensures it is committed at that point in time.
--- bulk-checkin.c | 22 ++++++++++------------ bulk-checkin.h | 8 +++----- object-file.c | 2 +- 3 files changed, 14 insertions(+), 18 deletions(-)
bulk-checkin.c +10 -12
@@ -33,7 +33,6 @@ struct bulk_checkin_packfile {struct odb_transaction {struct object_database *odb; -int nesting;struct tmp_objdir *objdir;struct bulk_checkin_packfile packfile; };
@@ -368,12 +367,11 @@ void fsync_loose_object_bulk_checkin(struct odb_transaction *transaction,struct odb_transaction *begin_odb_transaction (struct object_database *odb) { -if (!odb->transaction) { -CALLOC_ARRAY (odb->transaction,1 ); - odb->transaction->odb = odb; - } +if (odb->transaction) +return NULL ; - odb->transaction->nesting +=1 ; +CALLOC_ARRAY (odb->transaction,1 ); + odb->transaction->odb = odb;return odb->transaction; }
@@ -389,14 +387,14 @@ void flush_odb_transaction(struct odb_transaction *transaction)void end_odb_transaction (struct odb_transaction *transaction) { -if (!transaction || transaction->nesting ==0 ) -BUG ("Unbalanced ODB transaction nesting" ); - - transaction->nesting -=1 ; - -if (transaction->nesting) +if (!transaction)return ; +/* +* Ensure the transaction ending matches the pending transaction. +*/ +ASSERT (transaction == transaction->odb->transaction); +flush_odb_transaction (transaction); transaction->odb->transaction =NULL ;free (transaction);
bulk-checkin.h +3 -5
@@ -38,9 +38,8 @@ int index_blob_bulk_checkin(struct odb_transaction *transaction,/* * Tell the object database to optimize for adding * multiple objects. end_odb_transaction must be called -* to make new objects visible. Transactions can be nested, -* and objects are only visible after the outermost transaction -* is complete or the transaction is flushed. +* to make new objects visible. If a transaction is already +* pending, NULL is returned. */ struct odb_transaction *begin_odb_transaction (struct object_database *odb);
@@ -53,8 +52,7 @@ void flush_odb_transaction(struct odb_transaction *transaction);/* * Tell the object database to make any objects from the -* current transaction visible if this is the final nested -* transaction. +* current transaction visible. */ void end_odb_transaction (struct odb_transaction *transaction);
object-file.c +1 -1
@@ -1267,7 +1267,7 @@ int index_fd(struct index_state *istate, struct object_id *oid,struct odb_transaction *transaction; transaction =begin_odb_transaction (the_repository->objects); - ret =index_blob_bulk_checkin (transaction, + ret =index_blob_bulk_checkin (the_repository->objects->transaction, oid, fd,xsize_t (st->st_size), path, flags);end_odb_transaction (transaction);
Signature · 1 line
2.51.0.193.g4975ec3473b
PATCH v3 2/6 builtin/update-index: end ODB transaction when --verbose is specified
In reply to Justin ToblerWith 23a3a303 (update-index: use the bulk-checkin infrastructure, 2022-04-04), object database transactions were added to git-update-index(1) to facilitate writing objects in bulk. With transactions, newly added objects are instead written to a temporary object directory and migrated to the primary object database upon transaction commit.
When the --verbose option is specified, the subsequent set of objects written are explicitly flushed via flush_odb_transaction() prior to reporting the update. Flushing the object database transaction migrates pending objects to the primary object database without marking the transaction as complete. This is done so objects are immediately visible to git-update-index(1) callers using the --verbose option and that rely on parsing verbose output to know when objects are written.
Due to how git-update-index(1) parses arguments, options that come after a filename are not considered during the object update. Therefore, it may not be known ahead of time whether the --verbose option is present and thus object writes are considered transactional by default until a --verbose option is parsed.
Flushing a transaction after individual object writes negates the benefit of writing objects to a transaction in the first place. Furthermore, the mechanism to flush a transaction without actually committing is rather awkward. Drop the call to flush_odb_transaction() in favor of ending the transaction altogether when the --verbose flag is encountered. Subsequent object writes occur outside of a transaction and are therefore immediately visible which matches the current behavior.
--- builtin/update-index.c | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-)
builtin/update-index.c +15 -8
@@ -70,14 +70,6 @@ static void report(const char *fmt, ...)if (!verbose)return ; -/* -* It is possible, though unlikely, that a caller could use the verbose -* output to synchronize with addition of objects to the object -* database. The current implementation of ODB transactions leaves -* objects invisible while a transaction is active, so flush the -* transaction here before reporting a change made by update-index. -*/ -flush_odb_transaction (the_repository->objects->transaction);va_start (vp, fmt);vprintf (fmt, vp);putchar ('\n' );
@@ -1150,6 +1142,21 @@ int cmd_update_index(int argc,const char *path = ctx.argv[0 ];char *p; +/* +* It is possible, though unlikely, that a caller could +* use the verbose output to synchronize with addition +* of objects to the object database. The current +* implementation of ODB transactions leaves objects +* invisible while a transaction is active, so end the +* transaction here early before processing the next +* update. All further updates are performed outside of +* a transaction. +*/ +if (transaction && verbose) { +end_odb_transaction (transaction); + transaction =NULL ; + } +setup_work_tree (); p =prefix_path (prefix, prefix_length, path);update_one (p);
Signature · 1 line
2.51.0.193.g4975ec3473b
PATCH v3 3/6 bulk-checkin: drop flush_odb_transaction()
In reply to Justin ToblerObject database transactions can be explicitly flushed via flush_odb_transaction() without actually completing the transaction. This makes the provided transactional interface a bit awkward. Now that there are no longer any flush_odb_transaction() call sites, drop the function to simplify the interface and further ensure that a transaction is only finalized when end_odb_transaction() is invoked.
--- bulk-checkin.c | 12 ++---------- bulk-checkin.h | 7 ------- 2 files changed, 2 insertions(+), 17 deletions(-)
bulk-checkin.c +2 -10
@@ -376,15 +376,6 @@ struct odb_transaction *begin_odb_transaction(struct object_database *odb)return odb->transaction; } -void flush_odb_transaction (struct odb_transaction *transaction) -{ -if (!transaction) -return ; - -flush_batch_fsync (transaction); -flush_bulk_checkin_packfile (transaction); -} -void end_odb_transaction (struct odb_transaction *transaction) {if (!transaction)
@@ -395,7 +386,8 @@ void end_odb_transaction(struct odb_transaction *transaction)*/ ASSERT (transaction == transaction->odb->transaction); -flush_odb_transaction (transaction); +flush_batch_fsync (transaction); +flush_bulk_checkin_packfile (transaction); transaction->odb->transaction =NULL ;free (transaction); }
bulk-checkin.h +0 -7
@@ -43,13 +43,6 @@ int index_blob_bulk_checkin(struct odb_transaction *transaction,*/ struct odb_transaction *begin_odb_transaction (struct object_database *odb); -/* -* Make any objects that are currently part of a pending object -* database transaction visible. It is valid to call this function -* even if no transaction is active. -*/ -void flush_odb_transaction (struct odb_transaction *transaction); -/* * Tell the object database to make any objects from the * current transaction visible.
Signature · 1 line
2.51.0.193.g4975ec3473b
PATCH v3 4/6 object-file: relocate ODB transaction code
In reply to Justin ToblerThe bulk-checkin subsystem provides various functions to manage ODB transactions. Apart from {begin,end}_odb_transaction(), these functions are only used by the object-file subsystem to manage aspects of a transaction implementation specific to the files object source.
Relocate all the transaction code in bulk-checkin to object-file. This simplifies the exposed transaction interface by reducing it to only {begin,end}_odb_transaction(). Function and type names are adjusted in the subsequent commit to better fit the new location.
--- Makefile | 1 - builtin/add.c | 2 +- builtin/unpack-objects.c | 1 - builtin/update-index.c | 1 - bulk-checkin.c | 393 -------------------------------------- bulk-checkin.h | 52 ------ cache-tree.c | 1 - meson.build | 1 - object-file.c | 394 ++++++++++++++++++++++++++++++++++++++- object-file.h | 16 ++ read-cache.c | 1 - 11 files changed, 410 insertions(+), 453 deletions(-) delete mode 100644 bulk-checkin.c delete mode 100644 bulk-checkin.h
Makefile +0 -1
@@ -974,7 +974,6 @@ LIB_OBJS += blame.o LIB_OBJS += blob.o LIB_OBJS += bloom.o LIB_OBJS += branch.o -LIB_OBJS += bulk-checkin.o LIB_OBJS += bundle-uri.o LIB_OBJS += bundle.o LIB_OBJS += cache-tree.o
builtin/add.c +1 -1
@@ -14,13 +14,13 @@#include "gettext.h" #include "pathspec.h" #include "run-command.h" +#include "object-file.h" #include "parse-options.h" #include "path.h" #include "preload-index.h" #include "diff.h" #include "read-cache.h" #include "revision.h" -#include "bulk-checkin.h" #include "strvec.h" #include "submodule.h" #include "add-interactive.h"
builtin/unpack-objects.c +0 -1
@@ -2,7 +2,6 @@#define DISABLE_SIGN_COMPARE_WARNINGS #include "builtin.h" -#include "bulk-checkin.h" #include "config.h" #include "environment.h" #include "gettext.h"
builtin/update-index.c +0 -1
@@ -8,7 +8,6 @@#define DISABLE_SIGN_COMPARE_WARNINGS #include "builtin.h" -#include "bulk-checkin.h" #include "config.h" #include "environment.h" #include "gettext.h"
bulk-checkin.c deleted +0 -393
393 lines not on this page · raw message
bulk-checkin.h deleted +0 -52
52 lines not on this page · raw message
cache-tree.c +0 -1
@@ -8,7 +8,6 @@#include "tree.h" #include "tree-walk.h" #include "cache-tree.h" -#include "bulk-checkin.h" #include "object-file.h" #include "odb.h" #include "read-cache-ll.h"
meson.build +0 -1
@@ -287,7 +287,6 @@ libgit_sources = [ 'blob.c', 'bloom.c', 'branch.c', - 'bulk-checkin.c', 'bundle-uri.c', 'bundle.c', 'cache-tree.c',
object-file.c +393 -1
421 lines not on this page · raw message
object-file.h +16 -0
@@ -218,4 +218,20 @@ int read_loose_object(struct repository *repo,void **contents, struct object_info *oi); +struct odb_transaction ; + +/* +* Tell the object database to optimize for adding +* multiple objects. end_odb_transaction must be called +* to make new objects visible. If a transaction is already +* pending, NULL is returned. +*/ +struct odb_transaction *begin_odb_transaction (struct object_database *odb); + +/* +* Tell the object database to make any objects from the +* current transaction visible. +*/ +void end_odb_transaction (struct odb_transaction *transaction); +#endif /* OBJECT_FILE_H */
read-cache.c +0 -1
@@ -8,7 +8,6 @@#define DISABLE_SIGN_COMPARE_WARNINGS #include "git-compat-util.h" -#include "bulk-checkin.h" #include "config.h" #include "date.h" #include "diff.h"
Signature · 1 line
2.51.0.193.g4975ec3473b
PATCH v3 5/6 object-file: update naming from bulk-checkin
In reply to Justin ToblerUpdate the names of several functions and types relocated from the bulk-checkin subsystem for better clarity. Also drop finish_tmp_packfile() as a standalone function in favor of embedding it in flush_packfile_transaction() directly.
--- object-file.c | 80 +++++++++++++++++++++++---------------------------- 1 file changed, 36 insertions(+), 44 deletions(-)
object-file.c +36 -44
@@ -667,7 +667,7 @@ void hash_object_file(const struct git_hash_algo *algo, const void *buf,write_object_file_prepare (algo, buf, len, type, oid, hdr, &hdrlen); } -struct bulk_checkin_packfile { +struct transaction_packfile {char *pack_tmp_name;struct hashfile *f;off_t offset;
@@ -682,10 +682,10 @@ struct odb_transaction {struct object_database *odb;struct tmp_objdir *objdir; -struct bulk_checkin_packfile packfile; +struct transaction_packfile packfile; }; -static void prepare_loose_object_bulk_checkin (struct odb_transaction *transaction) +static void prepare_loose_object_transaction (struct odb_transaction *transaction) {/* * We lazily create the temporary object directory
@@ -701,7 +701,7 @@ static void prepare_loose_object_bulk_checkin(struct odb_transaction *transactiotmp_objdir_replace_primary_odb (transaction->objdir,0 ); } -static void fsync_loose_object_bulk_checkin (struct odb_transaction *transaction, +static void fsync_loose_object_transaction (struct odb_transaction *transaction,int fd,const char *filename) {/*
@@ -722,7 +722,7 @@ static void fsync_loose_object_bulk_checkin(struct odb_transaction *transaction,/* * Cleanup after batch-mode fsync_object_files. */ -static void flush_batch_fsync (struct odb_transaction *transaction) +static void flush_loose_object_transaction (struct odb_transaction *transaction) {struct strbuf temp_path =STRBUF_INIT ;struct tempfile *temp;
@@ -733,7 +733,7 @@ static void flush_batch_fsync(struct odb_transaction *transaction)/* * Issue a full hardware flush against a temporary file to ensure * that all objects are durable before any renames occur. The code in -* fsync_loose_object_bulk_checkin has already issued a writeout +* fsync_loose_object_transaction has already issued a writeout * request, but it has not flushed any writeback cache in the storage * hardware or any filesystem logs. This fsync call acts as a barrier * to ensure that the data in each new object file is durable before
@@ -762,7 +762,7 @@ static void close_loose_object(struct odb_source *source, goto out;if (batch_fsync_enabled (FSYNC_COMPONENT_LOOSE_OBJECT )) -fsync_loose_object_bulk_checkin (source->odb->transaction, fd, filename); +fsync_loose_object_transaction (source->odb->transaction, fd, filename);else if (fsync_object_files >0 )fsync_or_die (fd, filename);else
@@ -940,7 +940,7 @@ static int write_loose_object(struct odb_source *source,static struct strbuf filename =STRBUF_INIT ;if (batch_fsync_enabled (FSYNC_COMPONENT_LOOSE_OBJECT )) -prepare_loose_object_bulk_checkin (source->odb->transaction); +prepare_loose_object_transaction (source->odb->transaction);odb_loose_path (source, &filename, oid);
@@ -1029,7 +1029,7 @@ int stream_loose_object(struct odb_source *source,int hdrlen;if (batch_fsync_enabled (FSYNC_COMPONENT_LOOSE_OBJECT )) -prepare_loose_object_bulk_checkin (source->odb->transaction); +prepare_loose_object_transaction (source->odb->transaction);/* Since oid is not determined, save tmp file to odb path. */ strbuf_addf (&filename,"%s/" , source->path);
@@ -1349,10 +1349,10 @@ static int already_written(struct odb_transaction *transaction, }/* Lazily create backing packfile for the state */ -static void prepare_to_stream (struct odb_transaction *transaction, -unsigned flags) +static void prepare_packfile_transaction (struct odb_transaction *transaction, +unsigned flags) { -struct bulk_checkin_packfile *state = &transaction->packfile; +struct transaction_packfile *state = &transaction->packfile;if (!(flags &INDEX_WRITE_OBJECT ) || state->f)return ;
@@ -1381,7 +1381,7 @@ static void prepare_to_stream(struct odb_transaction *transaction,* status before calling us just in case we ask it to call us again * with a new pack. */ -static int stream_blob_to_pack (struct bulk_checkin_packfile *state, +static int stream_blob_to_pack (struct transaction_packfile *state,struct git_hash_ctx *ctx,off_t *already_hashed_to,int fd,size_t size,const char *path,unsigned flags)
@@ -1457,28 +1457,13 @@ static int stream_blob_to_pack(struct bulk_checkin_packfile *state,return 0 ; } -static void finish_tmp_packfile (struct odb_transaction *transaction, -struct strbuf *basename, -unsigned char hash[]) +static void flush_packfile_transaction (struct odb_transaction *transaction) { -struct bulk_checkin_packfile *state = &transaction->packfile; -struct repository *repo = transaction->odb->repo; -char *idx_tmp_name =NULL ; - -stage_tmp_packfiles (repo, basename, state->pack_tmp_name, - state->written, state->nr_written,NULL , - &state->pack_idx_opts, hash, &idx_tmp_name); -rename_tmp_packfile_idx (repo, basename, &idx_tmp_name); - -free (idx_tmp_name); -} - -static void flush_bulk_checkin_packfile (struct odb_transaction *transaction) -{ -struct bulk_checkin_packfile *state = &transaction->packfile; +struct transaction_packfile *state = &transaction->packfile;struct repository *repo = transaction->odb->repo;unsigned char hash[GIT_MAX_RAWSZ ];struct strbuf packname =STRBUF_INIT ; +char *idx_tmp_name =NULL ;if (!state->f)return ;
@@ -1503,11 +1488,16 @@ static void flush_bulk_checkin_packfile(struct odb_transaction *transaction)repo_get_object_directory (transaction->odb->repo), hash_to_hex_algop(hash, repo->hash_algo)); -finish_tmp_packfile (transaction, &packname, hash); +stage_tmp_packfiles (repo, &packname, state->pack_tmp_name, + state->written, state->nr_written,NULL , + &state->pack_idx_opts, hash, &idx_tmp_name); +rename_tmp_packfile_idx (repo, &packname, &idx_tmp_name); +for (uint32_t i =0 ; i < state->nr_written; i++)free (state->written[i]); clear_exit: +free (idx_tmp_name);free (state->pack_tmp_name);free (state->written);memset (state,0 ,sizeof (*state));
@@ -1535,11 +1525,12 @@ static void flush_bulk_checkin_packfile(struct odb_transaction *transaction)* binary blobs, they generally do not want to get any conversion, and * callers should avoid this code path when filters are requested. */ -static int index_blob_bulk_checkin (struct odb_transaction *transaction, -struct object_id *result_oid,int fd,size_t size, -const char *path,unsigned flags) +static int index_blob_packfile_transaction (struct odb_transaction *transaction, +struct object_id *result_oid,int fd, +size_t size,const char *path, +unsigned flags) { -struct bulk_checkin_packfile *state = &transaction->packfile; +struct transaction_packfile *state = &transaction->packfile;off_t seekback, already_hashed_to;struct git_hash_ctx ctx;unsigned char obuf[16384 ];
@@ -1560,14 +1551,14 @@ static int index_blob_bulk_checkin(struct odb_transaction *transaction,if ((flags &INDEX_WRITE_OBJECT ) !=0 ) {CALLOC_ARRAY (idx,1 ); -prepare_to_stream (transaction, flags); +prepare_packfile_transaction (transaction, flags);hashfile_checkpoint_init (state->f, &checkpoint); } already_hashed_to =0 ;while (1 ) { -prepare_to_stream (transaction, flags); +prepare_packfile_transaction (transaction, flags);if (idx) {hashfile_checkpoint (state->f, &checkpoint); idx->offset = state->offset;
@@ -1585,7 +1576,7 @@ static int index_blob_bulk_checkin(struct odb_transaction *transaction,BUG ("should not happen" );hashfile_truncate (state->f, &checkpoint); state->offset = checkpoint.offset; -flush_bulk_checkin_packfile (transaction); +flush_packfile_transaction (transaction);if (lseek (fd, seekback,SEEK_SET ) == (off_t)-1 )return error ("cannot seek back" ); }
@@ -1632,9 +1623,10 @@ int index_fd(struct index_state *istate, struct object_id *oid,struct odb_transaction *transaction; transaction =begin_odb_transaction (the_repository->objects); - ret =index_blob_bulk_checkin (the_repository->objects->transaction, - oid, fd,xsize_t (st->st_size), - path, flags); + ret =index_blob_packfile_transaction (the_repository->objects->transaction, + oid, fd, +xsize_t (st->st_size), + path, flags);end_odb_transaction (transaction); }
@@ -1996,8 +1988,8 @@ void end_odb_transaction(struct odb_transaction *transaction)*/ ASSERT (transaction == transaction->odb->transaction); -flush_batch_fsync (transaction); -flush_bulk_checkin_packfile (transaction); +flush_loose_object_transaction (transaction); +flush_packfile_transaction (transaction); transaction->odb->transaction =NULL ;free (transaction); }
Signature · 1 line
2.51.0.193.g4975ec3473b
PATCH v3 6/6 odb: add transaction interface
In reply to Justin ToblerTransactions are managed via the {begin,end}_odb_transaction() function in the object-file subsystem and its implementation is specific to the files object source. Introduce odb_transaction_{begin,commit}() in the odb subsystem to provide an eventual object source agnostic means to manage transactions.
Update call sites to instead manage transactions through the odb subsystem. Also rename {begin,end}_odb_transaction() functions to object_file_transaction_{begin,commit}() to clarify the object source it supports.
--- builtin/add.c | 5 +++-- builtin/unpack-objects.c | 4 ++-- builtin/update-index.c | 7 ++++--- cache-tree.c | 4 ++-- object-file.c | 12 +++++++----- object-file.h | 6 +++--- odb.c | 10 ++++++++++ odb.h | 13 +++++++++++++ read-cache.c | 4 ++-- 9 files changed, 46 insertions(+), 19 deletions(-)
builtin/add.c +3 -2
@@ -15,6 +15,7 @@#include "pathspec.h" #include "run-command.h" #include "object-file.h" +#include "odb.h" #include "parse-options.h" #include "path.h" #include "preload-index.h"
@@ -575,7 +576,7 @@ int cmd_add(int argc,string_list_clear (&only_match_skip_worktree,0 ); } - transaction =begin_odb_transaction (repo->objects); + transaction =odb_transaction_begin (repo->objects); ps_matched =xcalloc (pathspec.nr,1 );if (add_renormalize)
@@ -594,7 +595,7 @@ int cmd_add(int argc,if (chmod_arg && pathspec.nr) exit_status |=chmod_pathspec (repo, &pathspec, chmod_arg[0 ], show_only); -end_odb_transaction (transaction); +odb_transaction_commit (transaction); finish: if (write_locked_index(repo->index, &lock_file,
builtin/unpack-objects.c +2 -2
@@ -599,12 +599,12 @@ static void unpack_all(void) progress =start_progress (the_repository,_ ("Unpacking objects" ), nr_objects);CALLOC_ARRAY (obj_list, nr_objects); - transaction =begin_odb_transaction (the_repository->objects); + transaction =odb_transaction_begin (the_repository->objects);for (i =0 ; i < nr_objects; i++) {unpack_one (i);display_progress (progress, i +1 ); } -end_odb_transaction (transaction); +odb_transaction_commit (transaction);stop_progress (&progress);if (delta_list)
builtin/update-index.c +4 -3
@@ -18,6 +18,7 @@#include "cache-tree.h" #include "tree-walk.h" #include "object-file.h" +#include "odb.h" #include "refs.h" #include "resolve-undo.h" #include "parse-options.h"
@@ -1122,7 +1123,7 @@ int cmd_update_index(int argc,* Allow the object layer to optimize adding multiple objects in * a batch. */ - transaction =begin_odb_transaction (the_repository->objects); + transaction =odb_transaction_begin (the_repository->objects);while (ctx.argc) {if (parseopt_state !=PARSE_OPT_DONE ) parseopt_state = parse_options_step(&ctx, options,
@@ -1152,7 +1153,7 @@ int cmd_update_index(int argc,* a transaction. */ if (transaction && verbose) { -end_odb_transaction (transaction); +odb_transaction_commit (transaction); transaction =NULL ; }
@@ -1220,7 +1221,7 @@ int cmd_update_index(int argc,/* * By now we have added all of the new objects */ -end_odb_transaction (transaction); +odb_transaction_commit (transaction);if (split_index >0 ) {if (repo_config_get_split_index (the_repository) ==0 )
cache-tree.c +2 -2
@@ -489,10 +489,10 @@ int cache_tree_update(struct index_state *istate, int flags)trace_performance_enter ();trace2_region_enter ("cache_tree" ,"update" , the_repository); - transaction =begin_odb_transaction (the_repository->objects); + transaction =odb_transaction_begin (the_repository->objects); i =update_one (istate->cache_tree, istate->cache, istate->cache_nr,"" ,0 , &skip, flags); -end_odb_transaction (transaction); +odb_transaction_commit (transaction);trace2_region_leave ("cache_tree" ,"update" , the_repository);trace_performance_leave ("cache_tree_update" );if (i <0 )
object-file.c +7 -5
@@ -691,7 +691,7 @@ static void prepare_loose_object_transaction(struct odb_transaction *transaction* We lazily create the temporary object directory * the first time an object might be added, since * callers may not know whether any objects will be -* added at the time they call begin_odb_transaction. +* added at the time they call object_file_transaction_begin. */ if (!transaction || transaction->objdir)return ;
@@ -1622,12 +1622,12 @@ int index_fd(struct index_state *istate, struct object_id *oid, }else {struct odb_transaction *transaction; - transaction =begin_odb_transaction (the_repository->objects); + transaction =odb_transaction_begin (the_repository->objects); ret =index_blob_packfile_transaction (the_repository->objects->transaction, oid, fd,xsize_t (st->st_size), path, flags); -end_odb_transaction (transaction); +odb_transaction_commit (transaction); }close (fd);
@@ -1967,8 +1967,10 @@ int read_loose_object(struct repository *repo,return ret; } -struct odb_transaction *begin_odb_transaction (struct object_database *odb) +struct odb_transaction *object_file_transaction_begin (struct odb_source *source) { +struct object_database *odb = source->odb; +if (odb->transaction)return NULL ;
@@ -1978,7 +1980,7 @@ struct odb_transaction *begin_odb_transaction(struct object_database *odb)return odb->transaction; } -void end_odb_transaction (struct odb_transaction *transaction) +void object_file_transaction_commit (struct odb_transaction *transaction) {if (!transaction)return ;
object-file.h +3 -3
@@ -222,16 +222,16 @@ struct odb_transaction;/* * Tell the object database to optimize for adding -* multiple objects. end_odb_transaction must be called +* multiple objects. object_file_transaction_commit must be called * to make new objects visible. If a transaction is already * pending, NULL is returned. */ -struct odb_transaction *begin_odb_transaction (struct object_database *odb); +struct odb_transaction *object_file_transaction_begin (struct odb_source *source);/* * Tell the object database to make any objects from the * current transaction visible. */ -void end_odb_transaction (struct odb_transaction *transaction); +void object_file_transaction_commit (struct odb_transaction *transaction);#endif /* OBJECT_FILE_H */
odb.c +10 -0
@@ -1051,3 +1051,13 @@ void odb_clear(struct object_database *o)hashmap_clear (&o->pack_map);string_list_clear (&o->submodule_source_paths,0 ); } + +struct odb_transaction *odb_transaction_begin (struct object_database *odb) +{ +return object_file_transaction_begin (odb->sources); +} + +void odb_transaction_commit (struct odb_transaction *transaction) +{ +object_file_transaction_commit (transaction); +}
odb.h +13 -0
@@ -185,6 +185,19 @@ struct object_database {struct object_database *odb_new (struct repository *repo);void odb_clear (struct object_database *o); +/* +* Starts an ODB transaction. Subsequent objects are written to the transaction +* and not committed until odb_transaction_commit() is invoked on the +* transaction. If the ODB already has a pending transaction, NULL is returned. +*/ +struct odb_transaction *odb_transaction_begin (struct object_database *odb); + +/* +* Commits an ODB transaction making the written objects visible. If the +* specified transaction is NULL, the function is a no-op. +*/ +void odb_transaction_commit (struct odb_transaction *transaction); +/* * Find source by its object directory path. Dies in case the source couldn't * be found.
read-cache.c +2 -2
@@ -3972,9 +3972,9 @@ int add_files_to_cache(struct repository *repo, const char *prefix,* This function is invoked from commands other than 'add', which * may not have their own transaction active. */ - transaction =begin_odb_transaction (repo->objects); + transaction =odb_transaction_begin (repo->objects);run_diff_files (&rev,DIFF_RACY_IS_MODIFIED ); -end_odb_transaction (transaction); +odb_transaction_commit (transaction);release_revisions (&rev);return !!data.add_errors;
Signature · 1 line
2.51.0.193.g4975ec3473b