Skip to content

Commit f3c1db4

Browse files
jltoblergitster
authored andcommitted
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. Signed-off-by: Justin Tobler <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4a3422b commit f3c1db4

File tree

3 files changed

+14
-18
lines changed

3 files changed

+14
-18
lines changed

bulk-checkin.c

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ struct bulk_checkin_packfile {
3333
struct odb_transaction {
3434
struct object_database *odb;
3535

36-
int nesting;
3736
struct tmp_objdir *objdir;
3837
struct bulk_checkin_packfile packfile;
3938
};
@@ -368,12 +367,11 @@ void fsync_loose_object_bulk_checkin(struct odb_transaction *transaction,
368367

369368
struct odb_transaction *begin_odb_transaction(struct object_database *odb)
370369
{
371-
if (!odb->transaction) {
372-
CALLOC_ARRAY(odb->transaction, 1);
373-
odb->transaction->odb = odb;
374-
}
370+
if (odb->transaction)
371+
return NULL;
375372

376-
odb->transaction->nesting += 1;
373+
CALLOC_ARRAY(odb->transaction, 1);
374+
odb->transaction->odb = odb;
377375

378376
return odb->transaction;
379377
}
@@ -389,14 +387,14 @@ void flush_odb_transaction(struct odb_transaction *transaction)
389387

390388
void end_odb_transaction(struct odb_transaction *transaction)
391389
{
392-
if (!transaction || transaction->nesting == 0)
393-
BUG("Unbalanced ODB transaction nesting");
394-
395-
transaction->nesting -= 1;
396-
397-
if (transaction->nesting)
390+
if (!transaction)
398391
return;
399392

393+
/*
394+
* Ensure the transaction ending matches the pending transaction.
395+
*/
396+
ASSERT(transaction == transaction->odb->transaction);
397+
400398
flush_odb_transaction(transaction);
401399
transaction->odb->transaction = NULL;
402400
free(transaction);

bulk-checkin.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,8 @@ int index_blob_bulk_checkin(struct odb_transaction *transaction,
3838
/*
3939
* Tell the object database to optimize for adding
4040
* multiple objects. end_odb_transaction must be called
41-
* to make new objects visible. Transactions can be nested,
42-
* and objects are only visible after the outermost transaction
43-
* is complete or the transaction is flushed.
41+
* to make new objects visible. If a transaction is already
42+
* pending, NULL is returned.
4443
*/
4544
struct odb_transaction *begin_odb_transaction(struct object_database *odb);
4645

@@ -53,8 +52,7 @@ void flush_odb_transaction(struct odb_transaction *transaction);
5352

5453
/*
5554
* Tell the object database to make any objects from the
56-
* current transaction visible if this is the final nested
57-
* transaction.
55+
* current transaction visible.
5856
*/
5957
void end_odb_transaction(struct odb_transaction *transaction);
6058

object-file.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1267,7 +1267,7 @@ int index_fd(struct index_state *istate, struct object_id *oid,
12671267
struct odb_transaction *transaction;
12681268

12691269
transaction = begin_odb_transaction(the_repository->objects);
1270-
ret = index_blob_bulk_checkin(transaction,
1270+
ret = index_blob_bulk_checkin(the_repository->objects->transaction,
12711271
oid, fd, xsize_t(st->st_size),
12721272
path, flags);
12731273
end_odb_transaction(transaction);

0 commit comments

Comments
 (0)