Skip to content

Commit 558f6fb

Browse files
pks-tgitster
authored andcommitted
reftable/stack: simplify tracking of table locks
When compacting tables, we store the locks of all tables we are about to compact in the `table_locks` array. As we currently only ever compact all tables in the user-provided range or none, we simply track those locks via the indices of the respective tables in the merged stack. This is about to change though, as we will introduce a mode where auto compaction gracefully handles the case of already-locked files. In this case, it may happen that we only compact a subset of the user-supplied range of tables. In this case, the indices will not necessarily match the lock indices anymore. Refactor the code such that we track the number of locks via a separate variable. The resulting code is expected to perform the same, but will make it easier to perform the described change. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5f0ed60 commit 558f6fb

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

reftable/stack.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,7 +1016,7 @@ static int stack_compact_range(struct reftable_stack *st,
10161016
struct lock_file *table_locks = NULL;
10171017
struct tempfile *new_table = NULL;
10181018
int is_empty_table = 0, err = 0;
1019-
size_t i;
1019+
size_t i, nlocks = 0;
10201020

10211021
if (first > last || (!expiry && first == last)) {
10221022
err = 0;
@@ -1051,7 +1051,7 @@ static int stack_compact_range(struct reftable_stack *st,
10511051
for (i = first; i <= last; i++) {
10521052
stack_filename(&table_name, st, reader_name(st->readers[i]));
10531053

1054-
err = hold_lock_file_for_update(&table_locks[i - first],
1054+
err = hold_lock_file_for_update(&table_locks[nlocks],
10551055
table_name.buf, LOCK_NO_DEREF);
10561056
if (err < 0) {
10571057
if (errno == EEXIST)
@@ -1066,7 +1066,7 @@ static int stack_compact_range(struct reftable_stack *st,
10661066
* run into file descriptor exhaustion when we compress a lot
10671067
* of tables.
10681068
*/
1069-
err = close_lock_file_gently(&table_locks[i - first]);
1069+
err = close_lock_file_gently(&table_locks[nlocks++]);
10701070
if (err < 0) {
10711071
err = REFTABLE_IO_ERROR;
10721072
goto done;
@@ -1183,17 +1183,17 @@ static int stack_compact_range(struct reftable_stack *st,
11831183
* Delete the old tables. They may still be in use by concurrent
11841184
* readers, so it is expected that unlinking tables may fail.
11851185
*/
1186-
for (i = first; i <= last; i++) {
1187-
struct lock_file *table_lock = &table_locks[i - first];
1186+
for (i = 0; i < nlocks; i++) {
1187+
struct lock_file *table_lock = &table_locks[i];
11881188
char *table_path = get_locked_file_path(table_lock);
11891189
unlink(table_path);
11901190
free(table_path);
11911191
}
11921192

11931193
done:
11941194
rollback_lock_file(&tables_list_lock);
1195-
for (i = first; table_locks && i <= last; i++)
1196-
rollback_lock_file(&table_locks[i - first]);
1195+
for (i = 0; table_locks && i < nlocks; i++)
1196+
rollback_lock_file(&table_locks[i]);
11971197
reftable_free(table_locks);
11981198

11991199
delete_tempfile(&new_table);

0 commit comments

Comments
 (0)