2021-10-07 20:25:13 +00:00
|
|
|
/*
|
|
|
|
Copyright 2020 Google LLC
|
|
|
|
|
|
|
|
Use of this source code is governed by a BSD-style
|
|
|
|
license that can be found in the LICENSE file or at
|
|
|
|
https://developers.google.com/open-source/licenses/bsd
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "stack.h"
|
|
|
|
|
2024-01-23 18:51:10 +00:00
|
|
|
#include "../write-or-die.h"
|
2021-10-07 20:25:13 +00:00
|
|
|
#include "system.h"
|
|
|
|
#include "merged.h"
|
|
|
|
#include "reader.h"
|
|
|
|
#include "refname.h"
|
|
|
|
#include "reftable-error.h"
|
|
|
|
#include "reftable-record.h"
|
|
|
|
#include "reftable-merged.h"
|
|
|
|
#include "writer.h"
|
2023-12-11 09:07:54 +00:00
|
|
|
#include "tempfile.h"
|
|
|
|
|
2021-10-07 20:25:13 +00:00
|
|
|
static int stack_try_add(struct reftable_stack *st,
|
|
|
|
int (*write_table)(struct reftable_writer *wr,
|
|
|
|
void *arg),
|
|
|
|
void *arg);
|
|
|
|
static int stack_write_compact(struct reftable_stack *st,
|
2024-02-06 06:35:41 +00:00
|
|
|
struct reftable_writer *wr,
|
|
|
|
size_t first, size_t last,
|
2021-10-07 20:25:13 +00:00
|
|
|
struct reftable_log_expiry_config *config);
|
|
|
|
static int stack_check_addition(struct reftable_stack *st,
|
|
|
|
const char *new_tab_name);
|
|
|
|
static void reftable_addition_close(struct reftable_addition *add);
|
|
|
|
static int reftable_stack_reload_maybe_reuse(struct reftable_stack *st,
|
|
|
|
int reuse_open);
|
|
|
|
|
|
|
|
static void stack_filename(struct strbuf *dest, struct reftable_stack *st,
|
|
|
|
const char *name)
|
|
|
|
{
|
|
|
|
strbuf_reset(dest);
|
|
|
|
strbuf_addstr(dest, st->reftable_dir);
|
|
|
|
strbuf_addstr(dest, "/");
|
|
|
|
strbuf_addstr(dest, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t reftable_fd_write(void *arg, const void *data, size_t sz)
|
|
|
|
{
|
|
|
|
int *fdp = (int *)arg;
|
2023-12-11 09:07:38 +00:00
|
|
|
return write_in_full(*fdp, data, sz);
|
2021-10-07 20:25:13 +00:00
|
|
|
}
|
|
|
|
|
2024-01-23 18:51:10 +00:00
|
|
|
static int reftable_fd_flush(void *arg)
|
|
|
|
{
|
|
|
|
int *fdp = (int *)arg;
|
|
|
|
|
|
|
|
return fsync_component(FSYNC_COMPONENT_REFERENCE, *fdp);
|
|
|
|
}
|
|
|
|
|
2021-10-07 20:25:13 +00:00
|
|
|
int reftable_new_stack(struct reftable_stack **dest, const char *dir,
|
|
|
|
struct reftable_write_options config)
|
|
|
|
{
|
2024-02-06 06:35:27 +00:00
|
|
|
struct reftable_stack *p = reftable_calloc(1, sizeof(*p));
|
2021-10-07 20:25:13 +00:00
|
|
|
struct strbuf list_file_name = STRBUF_INIT;
|
|
|
|
int err = 0;
|
|
|
|
|
|
|
|
if (config.hash_id == 0) {
|
|
|
|
config.hash_id = GIT_SHA1_FORMAT_ID;
|
|
|
|
}
|
|
|
|
|
|
|
|
*dest = NULL;
|
|
|
|
|
|
|
|
strbuf_reset(&list_file_name);
|
|
|
|
strbuf_addstr(&list_file_name, dir);
|
|
|
|
strbuf_addstr(&list_file_name, "/tables.list");
|
|
|
|
|
|
|
|
p->list_file = strbuf_detach(&list_file_name, NULL);
|
reftable/stack: fix race in up-to-date check
In 6fdfaf15a0 (reftable/stack: use stat info to avoid re-reading stack
list, 2024-01-11) we have introduced a new mechanism to avoid re-reading
the table list in case stat(3P) figures out that the stack didn't change
since the last time we read it.
While this change significantly improved performance when writing many
refs, it can unfortunately lead to false negatives in very specific
scenarios. Given two processes A and B, there is a feasible sequence of
events that cause us to accidentally treat the table list as up-to-date
even though it changed:
1. A reads the reftable stack and caches its stat info.
2. B updates the stack, appending a new table to "tables.list". This
will both use a new inode and result in a different file size, thus
invalidating A's cache in theory.
3. B decides to auto-compact the stack and merges two tables. The file
size now matches what A has cached again. Furthermore, the
filesystem may decide to recycle the inode number of the file we
have replaced in (2) because it is not in use anymore.
4. A reloads the reftable stack. Neither the inode number nor the
file size changed. If the timestamps did not change either then we
think the cached copy of our stack is up-to-date.
In fact, the commit introduced three related issues:
- Non-POSIX compliant systems may not report proper `st_dev` and
`st_ino` values in stat(3P), which made us rely solely on the
file's potentially coarse-grained mtime and ctime.
- `stat_validity_check()` and friends may end up not comparing
`st_dev` and `st_ino` depending on the "core.checkstat" config,
again reducing the signal to the mtime and ctime.
- `st_ino` can be recycled, rendering the check moot even on
POSIX-compliant systems.
Given that POSIX defines that "The st_ino and st_dev fields taken
together uniquely identify the file within the system", these issues led
to the most important signal to establish file identity to be ignored or
become useless in some cases.
Refactor the code to stop using `stat_validity_check()`. Instead, we
manually stat(3P) the file descriptors to make relevant information
available. On Windows and MSYS2 the result will have both `st_dev` and
`st_ino` set to 0, which allows us to address the first issue by not
using the stat-based cache in that case. It also allows us to make sure
that we always compare `st_dev` and `st_ino`, addressing the second
issue.
The third issue of inode recycling can be addressed by keeping the file
descriptor of "files.list" open during the lifetime of the reftable
stack. As the file will still exist on disk even though it has been
unlinked it is impossible for its inode to be recycled as long as the
file descriptor is still open.
This should address the race in a POSIX-compliant way. The only real
downside is that this mechanism cannot be used on non-POSIX-compliant
systems like Windows. But we at least have the second-level caching
mechanism in place that compares contents of "files.list" with the
currently loaded list of tables.
This new mechanism performs roughly the same as the previous one that
relied on `stat_validity_check()`:
Benchmark 1: update-ref: create many refs (HEAD~)
Time (mean ± σ): 4.754 s ± 0.026 s [User: 2.204 s, System: 2.549 s]
Range (min … max): 4.694 s … 4.802 s 20 runs
Benchmark 2: update-ref: create many refs (HEAD)
Time (mean ± σ): 4.721 s ± 0.020 s [User: 2.194 s, System: 2.527 s]
Range (min … max): 4.691 s … 4.753 s 20 runs
Summary
update-ref: create many refs (HEAD~) ran
1.01 ± 0.01 times faster than update-ref: create many refs (HEAD)
Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2024-01-18 13:41:56 +00:00
|
|
|
p->list_fd = -1;
|
2021-10-07 20:25:13 +00:00
|
|
|
p->reftable_dir = xstrdup(dir);
|
|
|
|
p->config = config;
|
|
|
|
|
|
|
|
err = reftable_stack_reload_maybe_reuse(p, 1);
|
|
|
|
if (err < 0) {
|
|
|
|
reftable_stack_destroy(p);
|
|
|
|
} else {
|
|
|
|
*dest = p;
|
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int fd_read_lines(int fd, char ***namesp)
|
|
|
|
{
|
|
|
|
off_t size = lseek(fd, 0, SEEK_END);
|
|
|
|
char *buf = NULL;
|
|
|
|
int err = 0;
|
|
|
|
if (size < 0) {
|
|
|
|
err = REFTABLE_IO_ERROR;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
err = lseek(fd, 0, SEEK_SET);
|
|
|
|
if (err < 0) {
|
|
|
|
err = REFTABLE_IO_ERROR;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2024-02-06 06:35:27 +00:00
|
|
|
REFTABLE_ALLOC_ARRAY(buf, size + 1);
|
2023-12-11 09:07:34 +00:00
|
|
|
if (read_in_full(fd, buf, size) != size) {
|
2021-10-07 20:25:13 +00:00
|
|
|
err = REFTABLE_IO_ERROR;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
buf[size] = 0;
|
|
|
|
|
|
|
|
parse_names(buf, size, namesp);
|
|
|
|
|
|
|
|
done:
|
|
|
|
reftable_free(buf);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
int read_lines(const char *filename, char ***namesp)
|
|
|
|
{
|
|
|
|
int fd = open(filename, O_RDONLY);
|
|
|
|
int err = 0;
|
|
|
|
if (fd < 0) {
|
|
|
|
if (errno == ENOENT) {
|
2024-02-06 06:35:27 +00:00
|
|
|
REFTABLE_CALLOC_ARRAY(*namesp, 1);
|
2021-10-07 20:25:13 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return REFTABLE_IO_ERROR;
|
|
|
|
}
|
|
|
|
err = fd_read_lines(fd, namesp);
|
|
|
|
close(fd);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct reftable_merged_table *
|
|
|
|
reftable_stack_merged_table(struct reftable_stack *st)
|
|
|
|
{
|
|
|
|
return st->merged;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int has_name(char **names, const char *name)
|
|
|
|
{
|
|
|
|
while (*names) {
|
|
|
|
if (!strcmp(*names, name))
|
|
|
|
return 1;
|
|
|
|
names++;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Close and free the stack */
|
|
|
|
void reftable_stack_destroy(struct reftable_stack *st)
|
|
|
|
{
|
|
|
|
char **names = NULL;
|
|
|
|
int err = 0;
|
|
|
|
if (st->merged) {
|
|
|
|
reftable_merged_table_free(st->merged);
|
|
|
|
st->merged = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = read_lines(st->list_file, &names);
|
|
|
|
if (err < 0) {
|
|
|
|
FREE_AND_NULL(names);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (st->readers) {
|
|
|
|
int i = 0;
|
|
|
|
struct strbuf filename = STRBUF_INIT;
|
|
|
|
for (i = 0; i < st->readers_len; i++) {
|
|
|
|
const char *name = reader_name(st->readers[i]);
|
|
|
|
strbuf_reset(&filename);
|
|
|
|
if (names && !has_name(names, name)) {
|
|
|
|
stack_filename(&filename, st, name);
|
|
|
|
}
|
|
|
|
reftable_reader_free(st->readers[i]);
|
|
|
|
|
|
|
|
if (filename.len) {
|
|
|
|
/* On Windows, can only unlink after closing. */
|
|
|
|
unlink(filename.buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
strbuf_release(&filename);
|
|
|
|
st->readers_len = 0;
|
|
|
|
FREE_AND_NULL(st->readers);
|
|
|
|
}
|
reftable/stack: fix race in up-to-date check
In 6fdfaf15a0 (reftable/stack: use stat info to avoid re-reading stack
list, 2024-01-11) we have introduced a new mechanism to avoid re-reading
the table list in case stat(3P) figures out that the stack didn't change
since the last time we read it.
While this change significantly improved performance when writing many
refs, it can unfortunately lead to false negatives in very specific
scenarios. Given two processes A and B, there is a feasible sequence of
events that cause us to accidentally treat the table list as up-to-date
even though it changed:
1. A reads the reftable stack and caches its stat info.
2. B updates the stack, appending a new table to "tables.list". This
will both use a new inode and result in a different file size, thus
invalidating A's cache in theory.
3. B decides to auto-compact the stack and merges two tables. The file
size now matches what A has cached again. Furthermore, the
filesystem may decide to recycle the inode number of the file we
have replaced in (2) because it is not in use anymore.
4. A reloads the reftable stack. Neither the inode number nor the
file size changed. If the timestamps did not change either then we
think the cached copy of our stack is up-to-date.
In fact, the commit introduced three related issues:
- Non-POSIX compliant systems may not report proper `st_dev` and
`st_ino` values in stat(3P), which made us rely solely on the
file's potentially coarse-grained mtime and ctime.
- `stat_validity_check()` and friends may end up not comparing
`st_dev` and `st_ino` depending on the "core.checkstat" config,
again reducing the signal to the mtime and ctime.
- `st_ino` can be recycled, rendering the check moot even on
POSIX-compliant systems.
Given that POSIX defines that "The st_ino and st_dev fields taken
together uniquely identify the file within the system", these issues led
to the most important signal to establish file identity to be ignored or
become useless in some cases.
Refactor the code to stop using `stat_validity_check()`. Instead, we
manually stat(3P) the file descriptors to make relevant information
available. On Windows and MSYS2 the result will have both `st_dev` and
`st_ino` set to 0, which allows us to address the first issue by not
using the stat-based cache in that case. It also allows us to make sure
that we always compare `st_dev` and `st_ino`, addressing the second
issue.
The third issue of inode recycling can be addressed by keeping the file
descriptor of "files.list" open during the lifetime of the reftable
stack. As the file will still exist on disk even though it has been
unlinked it is impossible for its inode to be recycled as long as the
file descriptor is still open.
This should address the race in a POSIX-compliant way. The only real
downside is that this mechanism cannot be used on non-POSIX-compliant
systems like Windows. But we at least have the second-level caching
mechanism in place that compares contents of "files.list" with the
currently loaded list of tables.
This new mechanism performs roughly the same as the previous one that
relied on `stat_validity_check()`:
Benchmark 1: update-ref: create many refs (HEAD~)
Time (mean ± σ): 4.754 s ± 0.026 s [User: 2.204 s, System: 2.549 s]
Range (min … max): 4.694 s … 4.802 s 20 runs
Benchmark 2: update-ref: create many refs (HEAD)
Time (mean ± σ): 4.721 s ± 0.020 s [User: 2.194 s, System: 2.527 s]
Range (min … max): 4.691 s … 4.753 s 20 runs
Summary
update-ref: create many refs (HEAD~) ran
1.01 ± 0.01 times faster than update-ref: create many refs (HEAD)
Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2024-01-18 13:41:56 +00:00
|
|
|
|
|
|
|
if (st->list_fd >= 0) {
|
|
|
|
close(st->list_fd);
|
|
|
|
st->list_fd = -1;
|
|
|
|
}
|
|
|
|
|
2021-10-07 20:25:13 +00:00
|
|
|
FREE_AND_NULL(st->list_file);
|
|
|
|
FREE_AND_NULL(st->reftable_dir);
|
|
|
|
reftable_free(st);
|
|
|
|
free_names(names);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct reftable_reader **stack_copy_readers(struct reftable_stack *st,
|
|
|
|
int cur_len)
|
|
|
|
{
|
2024-02-06 06:35:27 +00:00
|
|
|
struct reftable_reader **cur = reftable_calloc(cur_len, sizeof(*cur));
|
2021-10-07 20:25:13 +00:00
|
|
|
int i = 0;
|
|
|
|
for (i = 0; i < cur_len; i++) {
|
|
|
|
cur[i] = st->readers[i];
|
|
|
|
}
|
|
|
|
return cur;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int reftable_stack_reload_once(struct reftable_stack *st, char **names,
|
|
|
|
int reuse_open)
|
|
|
|
{
|
2024-02-06 06:35:46 +00:00
|
|
|
size_t cur_len = !st->merged ? 0 : st->merged->stack_len;
|
2021-10-07 20:25:13 +00:00
|
|
|
struct reftable_reader **cur = stack_copy_readers(st, cur_len);
|
2024-02-06 06:35:46 +00:00
|
|
|
size_t names_len = names_length(names);
|
2021-10-07 20:25:13 +00:00
|
|
|
struct reftable_reader **new_readers =
|
2024-02-06 06:35:27 +00:00
|
|
|
reftable_calloc(names_len, sizeof(*new_readers));
|
2021-10-07 20:25:13 +00:00
|
|
|
struct reftable_table *new_tables =
|
2024-02-06 06:35:27 +00:00
|
|
|
reftable_calloc(names_len, sizeof(*new_tables));
|
2024-02-06 06:35:46 +00:00
|
|
|
size_t new_readers_len = 0;
|
2021-10-07 20:25:13 +00:00
|
|
|
struct reftable_merged_table *new_merged = NULL;
|
2023-12-11 09:07:50 +00:00
|
|
|
struct strbuf table_path = STRBUF_INIT;
|
2024-02-06 06:35:46 +00:00
|
|
|
int err = 0;
|
|
|
|
size_t i;
|
2021-10-07 20:25:13 +00:00
|
|
|
|
|
|
|
while (*names) {
|
|
|
|
struct reftable_reader *rd = NULL;
|
|
|
|
char *name = *names++;
|
|
|
|
|
|
|
|
/* this is linear; we assume compaction keeps the number of
|
|
|
|
tables under control so this is not quadratic. */
|
2024-02-06 06:35:46 +00:00
|
|
|
for (i = 0; reuse_open && i < cur_len; i++) {
|
|
|
|
if (cur[i] && 0 == strcmp(cur[i]->name, name)) {
|
|
|
|
rd = cur[i];
|
|
|
|
cur[i] = NULL;
|
2021-10-07 20:25:13 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!rd) {
|
|
|
|
struct reftable_block_source src = { NULL };
|
|
|
|
stack_filename(&table_path, st, name);
|
|
|
|
|
|
|
|
err = reftable_block_source_from_file(&src,
|
|
|
|
table_path.buf);
|
|
|
|
if (err < 0)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
err = reftable_new_reader(&rd, &src, name);
|
|
|
|
if (err < 0)
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
new_readers[new_readers_len] = rd;
|
|
|
|
reftable_table_from_reader(&new_tables[new_readers_len], rd);
|
|
|
|
new_readers_len++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* success! */
|
|
|
|
err = reftable_new_merged_table(&new_merged, new_tables,
|
|
|
|
new_readers_len, st->config.hash_id);
|
|
|
|
if (err < 0)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
new_tables = NULL;
|
|
|
|
st->readers_len = new_readers_len;
|
|
|
|
if (st->merged) {
|
|
|
|
merged_table_release(st->merged);
|
|
|
|
reftable_merged_table_free(st->merged);
|
|
|
|
}
|
|
|
|
if (st->readers) {
|
|
|
|
reftable_free(st->readers);
|
|
|
|
}
|
|
|
|
st->readers = new_readers;
|
|
|
|
new_readers = NULL;
|
|
|
|
new_readers_len = 0;
|
|
|
|
|
|
|
|
new_merged->suppress_deletions = 1;
|
|
|
|
st->merged = new_merged;
|
|
|
|
for (i = 0; i < cur_len; i++) {
|
|
|
|
if (cur[i]) {
|
|
|
|
const char *name = reader_name(cur[i]);
|
2023-12-11 09:07:50 +00:00
|
|
|
stack_filename(&table_path, st, name);
|
2021-10-07 20:25:13 +00:00
|
|
|
|
|
|
|
reader_close(cur[i]);
|
|
|
|
reftable_reader_free(cur[i]);
|
|
|
|
|
|
|
|
/* On Windows, can only unlink after closing. */
|
2023-12-11 09:07:50 +00:00
|
|
|
unlink(table_path.buf);
|
2021-10-07 20:25:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
for (i = 0; i < new_readers_len; i++) {
|
|
|
|
reader_close(new_readers[i]);
|
|
|
|
reftable_reader_free(new_readers[i]);
|
|
|
|
}
|
|
|
|
reftable_free(new_readers);
|
|
|
|
reftable_free(new_tables);
|
|
|
|
reftable_free(cur);
|
2023-12-11 09:07:50 +00:00
|
|
|
strbuf_release(&table_path);
|
2021-10-07 20:25:13 +00:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* return negative if a before b. */
|
|
|
|
static int tv_cmp(struct timeval *a, struct timeval *b)
|
|
|
|
{
|
|
|
|
time_t diff = a->tv_sec - b->tv_sec;
|
|
|
|
int udiff = a->tv_usec - b->tv_usec;
|
|
|
|
|
|
|
|
if (diff != 0)
|
|
|
|
return diff;
|
|
|
|
|
|
|
|
return udiff;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int reftable_stack_reload_maybe_reuse(struct reftable_stack *st,
|
|
|
|
int reuse_open)
|
|
|
|
{
|
2024-01-11 10:06:39 +00:00
|
|
|
char **names = NULL, **names_after = NULL;
|
|
|
|
struct timeval deadline;
|
2021-10-07 20:25:13 +00:00
|
|
|
int64_t delay = 0;
|
2024-01-11 10:06:39 +00:00
|
|
|
int tries = 0, err;
|
2024-01-11 10:06:43 +00:00
|
|
|
int fd = -1;
|
2021-10-07 20:25:13 +00:00
|
|
|
|
2024-01-11 10:06:39 +00:00
|
|
|
err = gettimeofday(&deadline, NULL);
|
|
|
|
if (err < 0)
|
|
|
|
goto out;
|
2021-10-07 20:25:13 +00:00
|
|
|
deadline.tv_sec += 3;
|
2024-01-11 10:06:39 +00:00
|
|
|
|
2021-10-07 20:25:13 +00:00
|
|
|
while (1) {
|
2024-01-11 10:06:39 +00:00
|
|
|
struct timeval now;
|
2021-10-07 20:25:13 +00:00
|
|
|
|
2024-01-11 10:06:39 +00:00
|
|
|
err = gettimeofday(&now, NULL);
|
|
|
|
if (err < 0)
|
|
|
|
goto out;
|
2021-10-07 20:25:13 +00:00
|
|
|
|
2024-01-11 10:06:39 +00:00
|
|
|
/*
|
|
|
|
* Only look at deadlines after the first few times. This
|
|
|
|
* simplifies debugging in GDB.
|
|
|
|
*/
|
2021-10-07 20:25:13 +00:00
|
|
|
tries++;
|
2024-01-11 10:06:39 +00:00
|
|
|
if (tries > 3 && tv_cmp(&now, &deadline) >= 0)
|
|
|
|
goto out;
|
2021-10-07 20:25:13 +00:00
|
|
|
|
2024-01-11 10:06:43 +00:00
|
|
|
fd = open(st->list_file, O_RDONLY);
|
|
|
|
if (fd < 0) {
|
|
|
|
if (errno != ENOENT) {
|
|
|
|
err = REFTABLE_IO_ERROR;
|
|
|
|
goto out;
|
|
|
|
}
|
2021-10-07 20:25:13 +00:00
|
|
|
|
2024-02-06 06:35:27 +00:00
|
|
|
REFTABLE_CALLOC_ARRAY(names, 1);
|
2024-01-11 10:06:43 +00:00
|
|
|
} else {
|
|
|
|
err = fd_read_lines(fd, &names);
|
|
|
|
if (err < 0)
|
|
|
|
goto out;
|
2021-10-07 20:25:13 +00:00
|
|
|
}
|
2024-01-11 10:06:39 +00:00
|
|
|
|
2021-10-07 20:25:13 +00:00
|
|
|
err = reftable_stack_reload_once(st, names, reuse_open);
|
2024-01-11 10:06:39 +00:00
|
|
|
if (!err)
|
2021-10-07 20:25:13 +00:00
|
|
|
break;
|
2024-01-11 10:06:39 +00:00
|
|
|
if (err != REFTABLE_NOT_EXIST_ERROR)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* REFTABLE_NOT_EXIST_ERROR can be caused by a concurrent
|
|
|
|
* writer. Check if there was one by checking if the name list
|
|
|
|
* changed.
|
|
|
|
*/
|
|
|
|
err = read_lines(st->list_file, &names_after);
|
|
|
|
if (err < 0)
|
|
|
|
goto out;
|
2021-10-07 20:25:13 +00:00
|
|
|
if (names_equal(names_after, names)) {
|
2024-01-11 10:06:39 +00:00
|
|
|
err = REFTABLE_NOT_EXIST_ERROR;
|
|
|
|
goto out;
|
2021-10-07 20:25:13 +00:00
|
|
|
}
|
2024-01-11 10:06:39 +00:00
|
|
|
|
2021-10-07 20:25:13 +00:00
|
|
|
free_names(names);
|
2024-01-11 10:06:39 +00:00
|
|
|
names = NULL;
|
2021-10-07 20:25:13 +00:00
|
|
|
free_names(names_after);
|
2024-01-11 10:06:39 +00:00
|
|
|
names_after = NULL;
|
2024-01-11 10:06:43 +00:00
|
|
|
close(fd);
|
|
|
|
fd = -1;
|
2021-10-07 20:25:13 +00:00
|
|
|
|
|
|
|
delay = delay + (delay * rand()) / RAND_MAX + 1;
|
|
|
|
sleep_millisec(delay);
|
|
|
|
}
|
|
|
|
|
2024-01-11 10:06:39 +00:00
|
|
|
out:
|
reftable/stack: fix race in up-to-date check
In 6fdfaf15a0 (reftable/stack: use stat info to avoid re-reading stack
list, 2024-01-11) we have introduced a new mechanism to avoid re-reading
the table list in case stat(3P) figures out that the stack didn't change
since the last time we read it.
While this change significantly improved performance when writing many
refs, it can unfortunately lead to false negatives in very specific
scenarios. Given two processes A and B, there is a feasible sequence of
events that cause us to accidentally treat the table list as up-to-date
even though it changed:
1. A reads the reftable stack and caches its stat info.
2. B updates the stack, appending a new table to "tables.list". This
will both use a new inode and result in a different file size, thus
invalidating A's cache in theory.
3. B decides to auto-compact the stack and merges two tables. The file
size now matches what A has cached again. Furthermore, the
filesystem may decide to recycle the inode number of the file we
have replaced in (2) because it is not in use anymore.
4. A reloads the reftable stack. Neither the inode number nor the
file size changed. If the timestamps did not change either then we
think the cached copy of our stack is up-to-date.
In fact, the commit introduced three related issues:
- Non-POSIX compliant systems may not report proper `st_dev` and
`st_ino` values in stat(3P), which made us rely solely on the
file's potentially coarse-grained mtime and ctime.
- `stat_validity_check()` and friends may end up not comparing
`st_dev` and `st_ino` depending on the "core.checkstat" config,
again reducing the signal to the mtime and ctime.
- `st_ino` can be recycled, rendering the check moot even on
POSIX-compliant systems.
Given that POSIX defines that "The st_ino and st_dev fields taken
together uniquely identify the file within the system", these issues led
to the most important signal to establish file identity to be ignored or
become useless in some cases.
Refactor the code to stop using `stat_validity_check()`. Instead, we
manually stat(3P) the file descriptors to make relevant information
available. On Windows and MSYS2 the result will have both `st_dev` and
`st_ino` set to 0, which allows us to address the first issue by not
using the stat-based cache in that case. It also allows us to make sure
that we always compare `st_dev` and `st_ino`, addressing the second
issue.
The third issue of inode recycling can be addressed by keeping the file
descriptor of "files.list" open during the lifetime of the reftable
stack. As the file will still exist on disk even though it has been
unlinked it is impossible for its inode to be recycled as long as the
file descriptor is still open.
This should address the race in a POSIX-compliant way. The only real
downside is that this mechanism cannot be used on non-POSIX-compliant
systems like Windows. But we at least have the second-level caching
mechanism in place that compares contents of "files.list" with the
currently loaded list of tables.
This new mechanism performs roughly the same as the previous one that
relied on `stat_validity_check()`:
Benchmark 1: update-ref: create many refs (HEAD~)
Time (mean ± σ): 4.754 s ± 0.026 s [User: 2.204 s, System: 2.549 s]
Range (min … max): 4.694 s … 4.802 s 20 runs
Benchmark 2: update-ref: create many refs (HEAD)
Time (mean ± σ): 4.721 s ± 0.020 s [User: 2.194 s, System: 2.527 s]
Range (min … max): 4.691 s … 4.753 s 20 runs
Summary
update-ref: create many refs (HEAD~) ran
1.01 ± 0.01 times faster than update-ref: create many refs (HEAD)
Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2024-01-18 13:41:56 +00:00
|
|
|
/*
|
|
|
|
* Invalidate the stat cache. It is sufficient to only close the file
|
|
|
|
* descriptor and keep the cached stat info because we never use the
|
|
|
|
* latter when the former is negative.
|
|
|
|
*/
|
|
|
|
if (st->list_fd >= 0) {
|
|
|
|
close(st->list_fd);
|
|
|
|
st->list_fd = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Cache stat information in case it provides a useful signal to us.
|
|
|
|
* According to POSIX, "The st_ino and st_dev fields taken together
|
|
|
|
* uniquely identify the file within the system." That being said,
|
|
|
|
* Windows is not POSIX compliant and we do not have these fields
|
|
|
|
* available. So the information we have there is insufficient to
|
|
|
|
* determine whether two file descriptors point to the same file.
|
|
|
|
*
|
|
|
|
* While we could fall back to using other signals like the file's
|
|
|
|
* mtime, those are not sufficient to avoid races. We thus refrain from
|
|
|
|
* using the stat cache on such systems and fall back to the secondary
|
|
|
|
* caching mechanism, which is to check whether contents of the file
|
|
|
|
* have changed.
|
|
|
|
*
|
|
|
|
* On other systems which are POSIX compliant we must keep the file
|
|
|
|
* descriptor open. This is to avoid a race condition where two
|
|
|
|
* processes access the reftable stack at the same point in time:
|
|
|
|
*
|
|
|
|
* 1. A reads the reftable stack and caches its stat info.
|
|
|
|
*
|
|
|
|
* 2. B updates the stack, appending a new table to "tables.list".
|
|
|
|
* This will both use a new inode and result in a different file
|
|
|
|
* size, thus invalidating A's cache in theory.
|
|
|
|
*
|
|
|
|
* 3. B decides to auto-compact the stack and merges two tables. The
|
|
|
|
* file size now matches what A has cached again. Furthermore, the
|
|
|
|
* filesystem may decide to recycle the inode number of the file
|
|
|
|
* we have replaced in (2) because it is not in use anymore.
|
|
|
|
*
|
|
|
|
* 4. A reloads the reftable stack. Neither the inode number nor the
|
|
|
|
* file size changed. If the timestamps did not change either then
|
|
|
|
* we think the cached copy of our stack is up-to-date.
|
|
|
|
*
|
|
|
|
* By keeping the file descriptor open the inode number cannot be
|
|
|
|
* recycled, mitigating the race.
|
|
|
|
*/
|
|
|
|
if (!err && fd >= 0 && !fstat(fd, &st->list_st) &&
|
|
|
|
st->list_st.st_dev && st->list_st.st_ino) {
|
|
|
|
st->list_fd = fd;
|
|
|
|
fd = -1;
|
|
|
|
}
|
|
|
|
|
2024-01-11 10:06:43 +00:00
|
|
|
if (fd >= 0)
|
|
|
|
close(fd);
|
2024-01-11 10:06:39 +00:00
|
|
|
free_names(names);
|
|
|
|
free_names(names_after);
|
|
|
|
return err;
|
2021-10-07 20:25:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* -1 = error
|
|
|
|
0 = up to date
|
|
|
|
1 = changed. */
|
|
|
|
static int stack_uptodate(struct reftable_stack *st)
|
|
|
|
{
|
|
|
|
char **names = NULL;
|
reftable/stack: use stat info to avoid re-reading stack list
Whenever we call into the refs interfaces we potentially have to reload
refs in case they have been concurrently modified, either in-process or
externally. While this happens somewhat automatically for loose refs
because we simply try to re-read the files, the "packed" backend will
reload its snapshot of the packed-refs file in case its stat info has
changed since last reading it.
In the reftable backend we have a similar mechanism that is provided by
`reftable_stack_reload()`. This function will read the list of stacks
from "tables.list" and, if they have changed from the currently stored
list, reload the stacks. This is heavily inefficient though, as we have
to check whether the stack is up-to-date on basically every read and
thus keep on re-reading the file all the time even if it didn't change
at all.
We can do better and use the same stat(3P)-based mechanism that the
"packed" backend uses. Instead of reading the file, we will only open
the file descriptor, fstat(3P) it, and then compare the info against the
cached value from the last time we have updated the stack. This should
always work alright because "tables.list" is updated atomically via a
rename, so even if the ctime or mtime wasn't granular enough to identify
a change, at least the inode number or file size should have changed.
This change significantly speeds up operations where many refs are read,
like when using git-update-ref(1). The following benchmark creates N
refs in an otherwise-empty repository via `git update-ref --stdin`:
Benchmark 1: update-ref: create many refs (refcount = 1, revision = HEAD~)
Time (mean ± σ): 5.1 ms ± 0.2 ms [User: 2.4 ms, System: 2.6 ms]
Range (min … max): 4.8 ms … 7.2 ms 109 runs
Benchmark 2: update-ref: create many refs (refcount = 100, revision = HEAD~)
Time (mean ± σ): 19.1 ms ± 0.9 ms [User: 8.9 ms, System: 9.9 ms]
Range (min … max): 18.4 ms … 26.7 ms 72 runs
Benchmark 3: update-ref: create many refs (refcount = 10000, revision = HEAD~)
Time (mean ± σ): 1.336 s ± 0.018 s [User: 0.590 s, System: 0.724 s]
Range (min … max): 1.314 s … 1.373 s 10 runs
Benchmark 4: update-ref: create many refs (refcount = 1, revision = HEAD)
Time (mean ± σ): 5.1 ms ± 0.2 ms [User: 2.4 ms, System: 2.6 ms]
Range (min … max): 4.8 ms … 7.2 ms 109 runs
Benchmark 5: update-ref: create many refs (refcount = 100, revision = HEAD)
Time (mean ± σ): 14.8 ms ± 0.2 ms [User: 7.1 ms, System: 7.5 ms]
Range (min … max): 14.2 ms … 15.2 ms 82 runs
Benchmark 6: update-ref: create many refs (refcount = 10000, revision = HEAD)
Time (mean ± σ): 927.6 ms ± 5.3 ms [User: 437.8 ms, System: 489.5 ms]
Range (min … max): 919.4 ms … 936.4 ms 10 runs
Summary
update-ref: create many refs (refcount = 1, revision = HEAD) ran
1.00 ± 0.07 times faster than update-ref: create many refs (refcount = 1, revision = HEAD~)
2.89 ± 0.14 times faster than update-ref: create many refs (refcount = 100, revision = HEAD)
3.74 ± 0.25 times faster than update-ref: create many refs (refcount = 100, revision = HEAD~)
181.26 ± 8.30 times faster than update-ref: create many refs (refcount = 10000, revision = HEAD)
261.01 ± 12.35 times faster than update-ref: create many refs (refcount = 10000, revision = HEAD~)
Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2024-01-11 10:06:48 +00:00
|
|
|
int err;
|
2021-10-07 20:25:13 +00:00
|
|
|
int i = 0;
|
reftable/stack: use stat info to avoid re-reading stack list
Whenever we call into the refs interfaces we potentially have to reload
refs in case they have been concurrently modified, either in-process or
externally. While this happens somewhat automatically for loose refs
because we simply try to re-read the files, the "packed" backend will
reload its snapshot of the packed-refs file in case its stat info has
changed since last reading it.
In the reftable backend we have a similar mechanism that is provided by
`reftable_stack_reload()`. This function will read the list of stacks
from "tables.list" and, if they have changed from the currently stored
list, reload the stacks. This is heavily inefficient though, as we have
to check whether the stack is up-to-date on basically every read and
thus keep on re-reading the file all the time even if it didn't change
at all.
We can do better and use the same stat(3P)-based mechanism that the
"packed" backend uses. Instead of reading the file, we will only open
the file descriptor, fstat(3P) it, and then compare the info against the
cached value from the last time we have updated the stack. This should
always work alright because "tables.list" is updated atomically via a
rename, so even if the ctime or mtime wasn't granular enough to identify
a change, at least the inode number or file size should have changed.
This change significantly speeds up operations where many refs are read,
like when using git-update-ref(1). The following benchmark creates N
refs in an otherwise-empty repository via `git update-ref --stdin`:
Benchmark 1: update-ref: create many refs (refcount = 1, revision = HEAD~)
Time (mean ± σ): 5.1 ms ± 0.2 ms [User: 2.4 ms, System: 2.6 ms]
Range (min … max): 4.8 ms … 7.2 ms 109 runs
Benchmark 2: update-ref: create many refs (refcount = 100, revision = HEAD~)
Time (mean ± σ): 19.1 ms ± 0.9 ms [User: 8.9 ms, System: 9.9 ms]
Range (min … max): 18.4 ms … 26.7 ms 72 runs
Benchmark 3: update-ref: create many refs (refcount = 10000, revision = HEAD~)
Time (mean ± σ): 1.336 s ± 0.018 s [User: 0.590 s, System: 0.724 s]
Range (min … max): 1.314 s … 1.373 s 10 runs
Benchmark 4: update-ref: create many refs (refcount = 1, revision = HEAD)
Time (mean ± σ): 5.1 ms ± 0.2 ms [User: 2.4 ms, System: 2.6 ms]
Range (min … max): 4.8 ms … 7.2 ms 109 runs
Benchmark 5: update-ref: create many refs (refcount = 100, revision = HEAD)
Time (mean ± σ): 14.8 ms ± 0.2 ms [User: 7.1 ms, System: 7.5 ms]
Range (min … max): 14.2 ms … 15.2 ms 82 runs
Benchmark 6: update-ref: create many refs (refcount = 10000, revision = HEAD)
Time (mean ± σ): 927.6 ms ± 5.3 ms [User: 437.8 ms, System: 489.5 ms]
Range (min … max): 919.4 ms … 936.4 ms 10 runs
Summary
update-ref: create many refs (refcount = 1, revision = HEAD) ran
1.00 ± 0.07 times faster than update-ref: create many refs (refcount = 1, revision = HEAD~)
2.89 ± 0.14 times faster than update-ref: create many refs (refcount = 100, revision = HEAD)
3.74 ± 0.25 times faster than update-ref: create many refs (refcount = 100, revision = HEAD~)
181.26 ± 8.30 times faster than update-ref: create many refs (refcount = 10000, revision = HEAD)
261.01 ± 12.35 times faster than update-ref: create many refs (refcount = 10000, revision = HEAD~)
Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2024-01-11 10:06:48 +00:00
|
|
|
|
reftable/stack: fix race in up-to-date check
In 6fdfaf15a0 (reftable/stack: use stat info to avoid re-reading stack
list, 2024-01-11) we have introduced a new mechanism to avoid re-reading
the table list in case stat(3P) figures out that the stack didn't change
since the last time we read it.
While this change significantly improved performance when writing many
refs, it can unfortunately lead to false negatives in very specific
scenarios. Given two processes A and B, there is a feasible sequence of
events that cause us to accidentally treat the table list as up-to-date
even though it changed:
1. A reads the reftable stack and caches its stat info.
2. B updates the stack, appending a new table to "tables.list". This
will both use a new inode and result in a different file size, thus
invalidating A's cache in theory.
3. B decides to auto-compact the stack and merges two tables. The file
size now matches what A has cached again. Furthermore, the
filesystem may decide to recycle the inode number of the file we
have replaced in (2) because it is not in use anymore.
4. A reloads the reftable stack. Neither the inode number nor the
file size changed. If the timestamps did not change either then we
think the cached copy of our stack is up-to-date.
In fact, the commit introduced three related issues:
- Non-POSIX compliant systems may not report proper `st_dev` and
`st_ino` values in stat(3P), which made us rely solely on the
file's potentially coarse-grained mtime and ctime.
- `stat_validity_check()` and friends may end up not comparing
`st_dev` and `st_ino` depending on the "core.checkstat" config,
again reducing the signal to the mtime and ctime.
- `st_ino` can be recycled, rendering the check moot even on
POSIX-compliant systems.
Given that POSIX defines that "The st_ino and st_dev fields taken
together uniquely identify the file within the system", these issues led
to the most important signal to establish file identity to be ignored or
become useless in some cases.
Refactor the code to stop using `stat_validity_check()`. Instead, we
manually stat(3P) the file descriptors to make relevant information
available. On Windows and MSYS2 the result will have both `st_dev` and
`st_ino` set to 0, which allows us to address the first issue by not
using the stat-based cache in that case. It also allows us to make sure
that we always compare `st_dev` and `st_ino`, addressing the second
issue.
The third issue of inode recycling can be addressed by keeping the file
descriptor of "files.list" open during the lifetime of the reftable
stack. As the file will still exist on disk even though it has been
unlinked it is impossible for its inode to be recycled as long as the
file descriptor is still open.
This should address the race in a POSIX-compliant way. The only real
downside is that this mechanism cannot be used on non-POSIX-compliant
systems like Windows. But we at least have the second-level caching
mechanism in place that compares contents of "files.list" with the
currently loaded list of tables.
This new mechanism performs roughly the same as the previous one that
relied on `stat_validity_check()`:
Benchmark 1: update-ref: create many refs (HEAD~)
Time (mean ± σ): 4.754 s ± 0.026 s [User: 2.204 s, System: 2.549 s]
Range (min … max): 4.694 s … 4.802 s 20 runs
Benchmark 2: update-ref: create many refs (HEAD)
Time (mean ± σ): 4.721 s ± 0.020 s [User: 2.194 s, System: 2.527 s]
Range (min … max): 4.691 s … 4.753 s 20 runs
Summary
update-ref: create many refs (HEAD~) ran
1.01 ± 0.01 times faster than update-ref: create many refs (HEAD)
Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2024-01-18 13:41:56 +00:00
|
|
|
/*
|
|
|
|
* When we have cached stat information available then we use it to
|
|
|
|
* verify whether the file has been rewritten.
|
|
|
|
*
|
|
|
|
* Note that we explicitly do not want to use `stat_validity_check()`
|
|
|
|
* and friends here because they may end up not comparing the `st_dev`
|
|
|
|
* and `st_ino` fields. These functions thus cannot guarantee that we
|
|
|
|
* indeed still have the same file.
|
|
|
|
*/
|
|
|
|
if (st->list_fd >= 0) {
|
|
|
|
struct stat list_st;
|
|
|
|
|
|
|
|
if (stat(st->list_file, &list_st) < 0) {
|
|
|
|
/*
|
|
|
|
* It's fine for "tables.list" to not exist. In that
|
|
|
|
* case, we have to refresh when the loaded stack has
|
|
|
|
* any readers.
|
|
|
|
*/
|
|
|
|
if (errno == ENOENT)
|
|
|
|
return !!st->readers_len;
|
|
|
|
return REFTABLE_IO_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* When "tables.list" refers to the same file we can assume
|
|
|
|
* that it didn't change. This is because we always use
|
|
|
|
* rename(3P) to update the file and never write to it
|
|
|
|
* directly.
|
|
|
|
*/
|
|
|
|
if (st->list_st.st_dev == list_st.st_dev &&
|
|
|
|
st->list_st.st_ino == list_st.st_ino)
|
|
|
|
return 0;
|
|
|
|
}
|
reftable/stack: use stat info to avoid re-reading stack list
Whenever we call into the refs interfaces we potentially have to reload
refs in case they have been concurrently modified, either in-process or
externally. While this happens somewhat automatically for loose refs
because we simply try to re-read the files, the "packed" backend will
reload its snapshot of the packed-refs file in case its stat info has
changed since last reading it.
In the reftable backend we have a similar mechanism that is provided by
`reftable_stack_reload()`. This function will read the list of stacks
from "tables.list" and, if they have changed from the currently stored
list, reload the stacks. This is heavily inefficient though, as we have
to check whether the stack is up-to-date on basically every read and
thus keep on re-reading the file all the time even if it didn't change
at all.
We can do better and use the same stat(3P)-based mechanism that the
"packed" backend uses. Instead of reading the file, we will only open
the file descriptor, fstat(3P) it, and then compare the info against the
cached value from the last time we have updated the stack. This should
always work alright because "tables.list" is updated atomically via a
rename, so even if the ctime or mtime wasn't granular enough to identify
a change, at least the inode number or file size should have changed.
This change significantly speeds up operations where many refs are read,
like when using git-update-ref(1). The following benchmark creates N
refs in an otherwise-empty repository via `git update-ref --stdin`:
Benchmark 1: update-ref: create many refs (refcount = 1, revision = HEAD~)
Time (mean ± σ): 5.1 ms ± 0.2 ms [User: 2.4 ms, System: 2.6 ms]
Range (min … max): 4.8 ms … 7.2 ms 109 runs
Benchmark 2: update-ref: create many refs (refcount = 100, revision = HEAD~)
Time (mean ± σ): 19.1 ms ± 0.9 ms [User: 8.9 ms, System: 9.9 ms]
Range (min … max): 18.4 ms … 26.7 ms 72 runs
Benchmark 3: update-ref: create many refs (refcount = 10000, revision = HEAD~)
Time (mean ± σ): 1.336 s ± 0.018 s [User: 0.590 s, System: 0.724 s]
Range (min … max): 1.314 s … 1.373 s 10 runs
Benchmark 4: update-ref: create many refs (refcount = 1, revision = HEAD)
Time (mean ± σ): 5.1 ms ± 0.2 ms [User: 2.4 ms, System: 2.6 ms]
Range (min … max): 4.8 ms … 7.2 ms 109 runs
Benchmark 5: update-ref: create many refs (refcount = 100, revision = HEAD)
Time (mean ± σ): 14.8 ms ± 0.2 ms [User: 7.1 ms, System: 7.5 ms]
Range (min … max): 14.2 ms … 15.2 ms 82 runs
Benchmark 6: update-ref: create many refs (refcount = 10000, revision = HEAD)
Time (mean ± σ): 927.6 ms ± 5.3 ms [User: 437.8 ms, System: 489.5 ms]
Range (min … max): 919.4 ms … 936.4 ms 10 runs
Summary
update-ref: create many refs (refcount = 1, revision = HEAD) ran
1.00 ± 0.07 times faster than update-ref: create many refs (refcount = 1, revision = HEAD~)
2.89 ± 0.14 times faster than update-ref: create many refs (refcount = 100, revision = HEAD)
3.74 ± 0.25 times faster than update-ref: create many refs (refcount = 100, revision = HEAD~)
181.26 ± 8.30 times faster than update-ref: create many refs (refcount = 10000, revision = HEAD)
261.01 ± 12.35 times faster than update-ref: create many refs (refcount = 10000, revision = HEAD~)
Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2024-01-11 10:06:48 +00:00
|
|
|
|
|
|
|
err = read_lines(st->list_file, &names);
|
2021-10-07 20:25:13 +00:00
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
for (i = 0; i < st->readers_len; i++) {
|
|
|
|
if (!names[i]) {
|
|
|
|
err = 1;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(st->readers[i]->name, names[i])) {
|
|
|
|
err = 1;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (names[st->merged->stack_len]) {
|
|
|
|
err = 1;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
free_names(names);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
int reftable_stack_reload(struct reftable_stack *st)
|
|
|
|
{
|
|
|
|
int err = stack_uptodate(st);
|
|
|
|
if (err > 0)
|
|
|
|
return reftable_stack_reload_maybe_reuse(st, 1);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
int reftable_stack_add(struct reftable_stack *st,
|
|
|
|
int (*write)(struct reftable_writer *wr, void *arg),
|
|
|
|
void *arg)
|
|
|
|
{
|
|
|
|
int err = stack_try_add(st, write, arg);
|
|
|
|
if (err < 0) {
|
2024-03-25 10:02:42 +00:00
|
|
|
if (err == REFTABLE_OUTDATED_ERROR) {
|
2021-10-07 20:25:13 +00:00
|
|
|
/* Ignore error return, we want to propagate
|
2024-03-25 10:02:42 +00:00
|
|
|
REFTABLE_OUTDATED_ERROR.
|
2021-10-07 20:25:13 +00:00
|
|
|
*/
|
|
|
|
reftable_stack_reload(st);
|
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void format_name(struct strbuf *dest, uint64_t min, uint64_t max)
|
|
|
|
{
|
|
|
|
char buf[100];
|
2023-12-11 09:07:59 +00:00
|
|
|
uint32_t rnd = (uint32_t)git_rand();
|
2021-10-07 20:25:13 +00:00
|
|
|
snprintf(buf, sizeof(buf), "0x%012" PRIx64 "-0x%012" PRIx64 "-%08x",
|
|
|
|
min, max, rnd);
|
|
|
|
strbuf_reset(dest);
|
|
|
|
strbuf_addstr(dest, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct reftable_addition {
|
2023-12-11 09:07:54 +00:00
|
|
|
struct tempfile *lock_file;
|
2021-10-07 20:25:13 +00:00
|
|
|
struct reftable_stack *stack;
|
|
|
|
|
|
|
|
char **new_tables;
|
reftable: introduce macros to grow arrays
Throughout the reftable library we have many cases where we need to grow
arrays. In order to avoid too many reallocations, we roughly double the
capacity of the array on each iteration. The resulting code pattern is
duplicated across many sites.
We have similar patterns in our main codebase, which is why we have
eventually introduced an `ALLOC_GROW()` macro to abstract it away and
avoid some code duplication. We cannot easily reuse this macro here
though because `ALLOC_GROW()` uses `REALLOC_ARRAY()`, which in turn will
call realloc(3P) to grow the array. The reftable code is structured as a
library though (even if the boundaries are fuzzy), and one property this
brings with it is that it is possible to plug in your own allocators. So
instead of using realloc(3P), we need to use `reftable_realloc()` that
knows to use the user-provided implementation.
So let's introduce two new macros `REFTABLE_REALLOC_ARRAY()` and
`REFTABLE_ALLOC_GROW()` that mirror what we do in our main codebase,
with two modifications:
- They use `reftable_realloc()`, as explained above.
- They use a different growth factor of `2 * cap + 1` instead of `(cap
+ 16) * 3 / 2`.
The second change is because we know a bit more about the allocation
patterns in the reftable library. In most cases, we end up only having a
handful of items in the array and don't end up growing them. The initial
capacity that our normal growth factor uses (which is 24) would thus end
up over-allocating in a lot of code paths. This effect is measurable:
- Before change:
HEAP SUMMARY:
in use at exit: 671,983 bytes in 152 blocks
total heap usage: 3,843,446 allocs, 3,843,294 frees, 223,761,402 bytes allocated
- After change with a growth factor of `(2 * alloc + 1)`:
HEAP SUMMARY:
in use at exit: 671,983 bytes in 152 blocks
total heap usage: 3,843,446 allocs, 3,843,294 frees, 223,761,410 bytes allocated
- After change with a growth factor of `(alloc + 16)* 2 / 3`:
HEAP SUMMARY:
in use at exit: 671,983 bytes in 152 blocks
total heap usage: 3,833,673 allocs, 3,833,521 frees, 4,728,251,742 bytes allocated
While the total heap usage is roughly the same, we do end up allocating
significantly more bytes with our usual growth factor (in fact, roughly
21 times as many).
Convert the reftable library to use these new macros.
Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2024-02-06 06:35:23 +00:00
|
|
|
size_t new_tables_len, new_tables_cap;
|
2021-10-07 20:25:13 +00:00
|
|
|
uint64_t next_update_index;
|
|
|
|
};
|
|
|
|
|
2023-12-11 09:07:54 +00:00
|
|
|
#define REFTABLE_ADDITION_INIT {0}
|
2021-10-07 20:25:13 +00:00
|
|
|
|
|
|
|
static int reftable_stack_init_addition(struct reftable_addition *add,
|
|
|
|
struct reftable_stack *st)
|
|
|
|
{
|
2023-12-11 09:07:54 +00:00
|
|
|
struct strbuf lock_file_name = STRBUF_INIT;
|
2021-10-07 20:25:13 +00:00
|
|
|
int err = 0;
|
|
|
|
add->stack = st;
|
|
|
|
|
2023-12-11 09:07:54 +00:00
|
|
|
strbuf_addf(&lock_file_name, "%s.lock", st->list_file);
|
2021-10-07 20:25:13 +00:00
|
|
|
|
2023-12-11 09:07:54 +00:00
|
|
|
add->lock_file = create_tempfile(lock_file_name.buf);
|
|
|
|
if (!add->lock_file) {
|
2021-10-07 20:25:13 +00:00
|
|
|
if (errno == EEXIST) {
|
|
|
|
err = REFTABLE_LOCK_ERROR;
|
|
|
|
} else {
|
|
|
|
err = REFTABLE_IO_ERROR;
|
|
|
|
}
|
|
|
|
goto done;
|
|
|
|
}
|
2021-12-23 19:29:50 +00:00
|
|
|
if (st->config.default_permissions) {
|
2023-12-11 09:07:54 +00:00
|
|
|
if (chmod(add->lock_file->filename.buf, st->config.default_permissions) < 0) {
|
2021-12-23 19:29:50 +00:00
|
|
|
err = REFTABLE_IO_ERROR;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-07 20:25:13 +00:00
|
|
|
err = stack_uptodate(st);
|
|
|
|
if (err < 0)
|
|
|
|
goto done;
|
2024-03-25 10:02:38 +00:00
|
|
|
if (err > 0) {
|
2024-03-25 10:02:42 +00:00
|
|
|
err = REFTABLE_OUTDATED_ERROR;
|
2021-10-07 20:25:13 +00:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
add->next_update_index = reftable_stack_next_update_index(st);
|
|
|
|
done:
|
|
|
|
if (err) {
|
|
|
|
reftable_addition_close(add);
|
|
|
|
}
|
2023-12-11 09:07:54 +00:00
|
|
|
strbuf_release(&lock_file_name);
|
2021-10-07 20:25:13 +00:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void reftable_addition_close(struct reftable_addition *add)
|
|
|
|
{
|
|
|
|
struct strbuf nm = STRBUF_INIT;
|
reftable: introduce macros to grow arrays
Throughout the reftable library we have many cases where we need to grow
arrays. In order to avoid too many reallocations, we roughly double the
capacity of the array on each iteration. The resulting code pattern is
duplicated across many sites.
We have similar patterns in our main codebase, which is why we have
eventually introduced an `ALLOC_GROW()` macro to abstract it away and
avoid some code duplication. We cannot easily reuse this macro here
though because `ALLOC_GROW()` uses `REALLOC_ARRAY()`, which in turn will
call realloc(3P) to grow the array. The reftable code is structured as a
library though (even if the boundaries are fuzzy), and one property this
brings with it is that it is possible to plug in your own allocators. So
instead of using realloc(3P), we need to use `reftable_realloc()` that
knows to use the user-provided implementation.
So let's introduce two new macros `REFTABLE_REALLOC_ARRAY()` and
`REFTABLE_ALLOC_GROW()` that mirror what we do in our main codebase,
with two modifications:
- They use `reftable_realloc()`, as explained above.
- They use a different growth factor of `2 * cap + 1` instead of `(cap
+ 16) * 3 / 2`.
The second change is because we know a bit more about the allocation
patterns in the reftable library. In most cases, we end up only having a
handful of items in the array and don't end up growing them. The initial
capacity that our normal growth factor uses (which is 24) would thus end
up over-allocating in a lot of code paths. This effect is measurable:
- Before change:
HEAP SUMMARY:
in use at exit: 671,983 bytes in 152 blocks
total heap usage: 3,843,446 allocs, 3,843,294 frees, 223,761,402 bytes allocated
- After change with a growth factor of `(2 * alloc + 1)`:
HEAP SUMMARY:
in use at exit: 671,983 bytes in 152 blocks
total heap usage: 3,843,446 allocs, 3,843,294 frees, 223,761,410 bytes allocated
- After change with a growth factor of `(alloc + 16)* 2 / 3`:
HEAP SUMMARY:
in use at exit: 671,983 bytes in 152 blocks
total heap usage: 3,833,673 allocs, 3,833,521 frees, 4,728,251,742 bytes allocated
While the total heap usage is roughly the same, we do end up allocating
significantly more bytes with our usual growth factor (in fact, roughly
21 times as many).
Convert the reftable library to use these new macros.
Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2024-02-06 06:35:23 +00:00
|
|
|
size_t i;
|
|
|
|
|
2021-10-07 20:25:13 +00:00
|
|
|
for (i = 0; i < add->new_tables_len; i++) {
|
|
|
|
stack_filename(&nm, add->stack, add->new_tables[i]);
|
|
|
|
unlink(nm.buf);
|
|
|
|
reftable_free(add->new_tables[i]);
|
|
|
|
add->new_tables[i] = NULL;
|
|
|
|
}
|
|
|
|
reftable_free(add->new_tables);
|
|
|
|
add->new_tables = NULL;
|
|
|
|
add->new_tables_len = 0;
|
reftable: introduce macros to grow arrays
Throughout the reftable library we have many cases where we need to grow
arrays. In order to avoid too many reallocations, we roughly double the
capacity of the array on each iteration. The resulting code pattern is
duplicated across many sites.
We have similar patterns in our main codebase, which is why we have
eventually introduced an `ALLOC_GROW()` macro to abstract it away and
avoid some code duplication. We cannot easily reuse this macro here
though because `ALLOC_GROW()` uses `REALLOC_ARRAY()`, which in turn will
call realloc(3P) to grow the array. The reftable code is structured as a
library though (even if the boundaries are fuzzy), and one property this
brings with it is that it is possible to plug in your own allocators. So
instead of using realloc(3P), we need to use `reftable_realloc()` that
knows to use the user-provided implementation.
So let's introduce two new macros `REFTABLE_REALLOC_ARRAY()` and
`REFTABLE_ALLOC_GROW()` that mirror what we do in our main codebase,
with two modifications:
- They use `reftable_realloc()`, as explained above.
- They use a different growth factor of `2 * cap + 1` instead of `(cap
+ 16) * 3 / 2`.
The second change is because we know a bit more about the allocation
patterns in the reftable library. In most cases, we end up only having a
handful of items in the array and don't end up growing them. The initial
capacity that our normal growth factor uses (which is 24) would thus end
up over-allocating in a lot of code paths. This effect is measurable:
- Before change:
HEAP SUMMARY:
in use at exit: 671,983 bytes in 152 blocks
total heap usage: 3,843,446 allocs, 3,843,294 frees, 223,761,402 bytes allocated
- After change with a growth factor of `(2 * alloc + 1)`:
HEAP SUMMARY:
in use at exit: 671,983 bytes in 152 blocks
total heap usage: 3,843,446 allocs, 3,843,294 frees, 223,761,410 bytes allocated
- After change with a growth factor of `(alloc + 16)* 2 / 3`:
HEAP SUMMARY:
in use at exit: 671,983 bytes in 152 blocks
total heap usage: 3,833,673 allocs, 3,833,521 frees, 4,728,251,742 bytes allocated
While the total heap usage is roughly the same, we do end up allocating
significantly more bytes with our usual growth factor (in fact, roughly
21 times as many).
Convert the reftable library to use these new macros.
Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2024-02-06 06:35:23 +00:00
|
|
|
add->new_tables_cap = 0;
|
2021-10-07 20:25:13 +00:00
|
|
|
|
2023-12-11 09:07:54 +00:00
|
|
|
delete_tempfile(&add->lock_file);
|
2021-10-07 20:25:13 +00:00
|
|
|
strbuf_release(&nm);
|
|
|
|
}
|
|
|
|
|
|
|
|
void reftable_addition_destroy(struct reftable_addition *add)
|
|
|
|
{
|
|
|
|
if (!add) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
reftable_addition_close(add);
|
|
|
|
reftable_free(add);
|
|
|
|
}
|
|
|
|
|
|
|
|
int reftable_addition_commit(struct reftable_addition *add)
|
|
|
|
{
|
|
|
|
struct strbuf table_list = STRBUF_INIT;
|
2023-12-11 09:07:54 +00:00
|
|
|
int lock_file_fd = get_tempfile_fd(add->lock_file);
|
2021-10-07 20:25:13 +00:00
|
|
|
int err = 0;
|
reftable: introduce macros to grow arrays
Throughout the reftable library we have many cases where we need to grow
arrays. In order to avoid too many reallocations, we roughly double the
capacity of the array on each iteration. The resulting code pattern is
duplicated across many sites.
We have similar patterns in our main codebase, which is why we have
eventually introduced an `ALLOC_GROW()` macro to abstract it away and
avoid some code duplication. We cannot easily reuse this macro here
though because `ALLOC_GROW()` uses `REALLOC_ARRAY()`, which in turn will
call realloc(3P) to grow the array. The reftable code is structured as a
library though (even if the boundaries are fuzzy), and one property this
brings with it is that it is possible to plug in your own allocators. So
instead of using realloc(3P), we need to use `reftable_realloc()` that
knows to use the user-provided implementation.
So let's introduce two new macros `REFTABLE_REALLOC_ARRAY()` and
`REFTABLE_ALLOC_GROW()` that mirror what we do in our main codebase,
with two modifications:
- They use `reftable_realloc()`, as explained above.
- They use a different growth factor of `2 * cap + 1` instead of `(cap
+ 16) * 3 / 2`.
The second change is because we know a bit more about the allocation
patterns in the reftable library. In most cases, we end up only having a
handful of items in the array and don't end up growing them. The initial
capacity that our normal growth factor uses (which is 24) would thus end
up over-allocating in a lot of code paths. This effect is measurable:
- Before change:
HEAP SUMMARY:
in use at exit: 671,983 bytes in 152 blocks
total heap usage: 3,843,446 allocs, 3,843,294 frees, 223,761,402 bytes allocated
- After change with a growth factor of `(2 * alloc + 1)`:
HEAP SUMMARY:
in use at exit: 671,983 bytes in 152 blocks
total heap usage: 3,843,446 allocs, 3,843,294 frees, 223,761,410 bytes allocated
- After change with a growth factor of `(alloc + 16)* 2 / 3`:
HEAP SUMMARY:
in use at exit: 671,983 bytes in 152 blocks
total heap usage: 3,833,673 allocs, 3,833,521 frees, 4,728,251,742 bytes allocated
While the total heap usage is roughly the same, we do end up allocating
significantly more bytes with our usual growth factor (in fact, roughly
21 times as many).
Convert the reftable library to use these new macros.
Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2024-02-06 06:35:23 +00:00
|
|
|
size_t i;
|
2023-12-11 09:07:54 +00:00
|
|
|
|
2021-10-07 20:25:13 +00:00
|
|
|
if (add->new_tables_len == 0)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
for (i = 0; i < add->stack->merged->stack_len; i++) {
|
|
|
|
strbuf_addstr(&table_list, add->stack->readers[i]->name);
|
|
|
|
strbuf_addstr(&table_list, "\n");
|
|
|
|
}
|
|
|
|
for (i = 0; i < add->new_tables_len; i++) {
|
|
|
|
strbuf_addstr(&table_list, add->new_tables[i]);
|
|
|
|
strbuf_addstr(&table_list, "\n");
|
|
|
|
}
|
|
|
|
|
2023-12-11 09:07:54 +00:00
|
|
|
err = write_in_full(lock_file_fd, table_list.buf, table_list.len);
|
2021-10-07 20:25:13 +00:00
|
|
|
strbuf_release(&table_list);
|
|
|
|
if (err < 0) {
|
|
|
|
err = REFTABLE_IO_ERROR;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2024-01-23 18:51:10 +00:00
|
|
|
fsync_component_or_die(FSYNC_COMPONENT_REFERENCE, lock_file_fd,
|
|
|
|
get_tempfile_path(add->lock_file));
|
|
|
|
|
2023-12-11 09:07:54 +00:00
|
|
|
err = rename_tempfile(&add->lock_file, add->stack->list_file);
|
2021-10-07 20:25:13 +00:00
|
|
|
if (err < 0) {
|
|
|
|
err = REFTABLE_IO_ERROR;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* success, no more state to clean up. */
|
reftable: introduce macros to grow arrays
Throughout the reftable library we have many cases where we need to grow
arrays. In order to avoid too many reallocations, we roughly double the
capacity of the array on each iteration. The resulting code pattern is
duplicated across many sites.
We have similar patterns in our main codebase, which is why we have
eventually introduced an `ALLOC_GROW()` macro to abstract it away and
avoid some code duplication. We cannot easily reuse this macro here
though because `ALLOC_GROW()` uses `REALLOC_ARRAY()`, which in turn will
call realloc(3P) to grow the array. The reftable code is structured as a
library though (even if the boundaries are fuzzy), and one property this
brings with it is that it is possible to plug in your own allocators. So
instead of using realloc(3P), we need to use `reftable_realloc()` that
knows to use the user-provided implementation.
So let's introduce two new macros `REFTABLE_REALLOC_ARRAY()` and
`REFTABLE_ALLOC_GROW()` that mirror what we do in our main codebase,
with two modifications:
- They use `reftable_realloc()`, as explained above.
- They use a different growth factor of `2 * cap + 1` instead of `(cap
+ 16) * 3 / 2`.
The second change is because we know a bit more about the allocation
patterns in the reftable library. In most cases, we end up only having a
handful of items in the array and don't end up growing them. The initial
capacity that our normal growth factor uses (which is 24) would thus end
up over-allocating in a lot of code paths. This effect is measurable:
- Before change:
HEAP SUMMARY:
in use at exit: 671,983 bytes in 152 blocks
total heap usage: 3,843,446 allocs, 3,843,294 frees, 223,761,402 bytes allocated
- After change with a growth factor of `(2 * alloc + 1)`:
HEAP SUMMARY:
in use at exit: 671,983 bytes in 152 blocks
total heap usage: 3,843,446 allocs, 3,843,294 frees, 223,761,410 bytes allocated
- After change with a growth factor of `(alloc + 16)* 2 / 3`:
HEAP SUMMARY:
in use at exit: 671,983 bytes in 152 blocks
total heap usage: 3,833,673 allocs, 3,833,521 frees, 4,728,251,742 bytes allocated
While the total heap usage is roughly the same, we do end up allocating
significantly more bytes with our usual growth factor (in fact, roughly
21 times as many).
Convert the reftable library to use these new macros.
Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2024-02-06 06:35:23 +00:00
|
|
|
for (i = 0; i < add->new_tables_len; i++)
|
2021-10-07 20:25:13 +00:00
|
|
|
reftable_free(add->new_tables[i]);
|
|
|
|
reftable_free(add->new_tables);
|
|
|
|
add->new_tables = NULL;
|
|
|
|
add->new_tables_len = 0;
|
reftable: introduce macros to grow arrays
Throughout the reftable library we have many cases where we need to grow
arrays. In order to avoid too many reallocations, we roughly double the
capacity of the array on each iteration. The resulting code pattern is
duplicated across many sites.
We have similar patterns in our main codebase, which is why we have
eventually introduced an `ALLOC_GROW()` macro to abstract it away and
avoid some code duplication. We cannot easily reuse this macro here
though because `ALLOC_GROW()` uses `REALLOC_ARRAY()`, which in turn will
call realloc(3P) to grow the array. The reftable code is structured as a
library though (even if the boundaries are fuzzy), and one property this
brings with it is that it is possible to plug in your own allocators. So
instead of using realloc(3P), we need to use `reftable_realloc()` that
knows to use the user-provided implementation.
So let's introduce two new macros `REFTABLE_REALLOC_ARRAY()` and
`REFTABLE_ALLOC_GROW()` that mirror what we do in our main codebase,
with two modifications:
- They use `reftable_realloc()`, as explained above.
- They use a different growth factor of `2 * cap + 1` instead of `(cap
+ 16) * 3 / 2`.
The second change is because we know a bit more about the allocation
patterns in the reftable library. In most cases, we end up only having a
handful of items in the array and don't end up growing them. The initial
capacity that our normal growth factor uses (which is 24) would thus end
up over-allocating in a lot of code paths. This effect is measurable:
- Before change:
HEAP SUMMARY:
in use at exit: 671,983 bytes in 152 blocks
total heap usage: 3,843,446 allocs, 3,843,294 frees, 223,761,402 bytes allocated
- After change with a growth factor of `(2 * alloc + 1)`:
HEAP SUMMARY:
in use at exit: 671,983 bytes in 152 blocks
total heap usage: 3,843,446 allocs, 3,843,294 frees, 223,761,410 bytes allocated
- After change with a growth factor of `(alloc + 16)* 2 / 3`:
HEAP SUMMARY:
in use at exit: 671,983 bytes in 152 blocks
total heap usage: 3,833,673 allocs, 3,833,521 frees, 4,728,251,742 bytes allocated
While the total heap usage is roughly the same, we do end up allocating
significantly more bytes with our usual growth factor (in fact, roughly
21 times as many).
Convert the reftable library to use these new macros.
Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2024-02-06 06:35:23 +00:00
|
|
|
add->new_tables_cap = 0;
|
2021-10-07 20:25:13 +00:00
|
|
|
|
2024-01-18 13:41:51 +00:00
|
|
|
err = reftable_stack_reload_maybe_reuse(add->stack, 1);
|
2023-12-11 09:07:46 +00:00
|
|
|
if (err)
|
|
|
|
goto done;
|
|
|
|
|
2024-03-25 10:02:50 +00:00
|
|
|
if (!add->stack->disable_auto_compact) {
|
|
|
|
/*
|
|
|
|
* Auto-compact the stack to keep the number of tables in
|
|
|
|
* control. It is possible that a concurrent writer is already
|
|
|
|
* trying to compact parts of the stack, which would lead to a
|
|
|
|
* `REFTABLE_LOCK_ERROR` because parts of the stack are locked
|
|
|
|
* already. This is a benign error though, so we ignore it.
|
|
|
|
*/
|
2023-12-11 09:07:46 +00:00
|
|
|
err = reftable_stack_auto_compact(add->stack);
|
2024-03-25 10:02:50 +00:00
|
|
|
if (err < 0 && err != REFTABLE_LOCK_ERROR)
|
|
|
|
goto done;
|
|
|
|
err = 0;
|
|
|
|
}
|
2023-12-11 09:07:46 +00:00
|
|
|
|
2021-10-07 20:25:13 +00:00
|
|
|
done:
|
|
|
|
reftable_addition_close(add);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
int reftable_stack_new_addition(struct reftable_addition **dest,
|
|
|
|
struct reftable_stack *st)
|
|
|
|
{
|
|
|
|
int err = 0;
|
|
|
|
struct reftable_addition empty = REFTABLE_ADDITION_INIT;
|
2024-02-06 06:35:27 +00:00
|
|
|
REFTABLE_CALLOC_ARRAY(*dest, 1);
|
2021-10-07 20:25:13 +00:00
|
|
|
**dest = empty;
|
|
|
|
err = reftable_stack_init_addition(*dest, st);
|
|
|
|
if (err) {
|
|
|
|
reftable_free(*dest);
|
|
|
|
*dest = NULL;
|
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int stack_try_add(struct reftable_stack *st,
|
|
|
|
int (*write_table)(struct reftable_writer *wr,
|
|
|
|
void *arg),
|
|
|
|
void *arg)
|
|
|
|
{
|
|
|
|
struct reftable_addition add = REFTABLE_ADDITION_INIT;
|
|
|
|
int err = reftable_stack_init_addition(&add, st);
|
|
|
|
if (err < 0)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
err = reftable_addition_add(&add, write_table, arg);
|
|
|
|
if (err < 0)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
err = reftable_addition_commit(&add);
|
|
|
|
done:
|
|
|
|
reftable_addition_close(&add);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
int reftable_addition_add(struct reftable_addition *add,
|
|
|
|
int (*write_table)(struct reftable_writer *wr,
|
|
|
|
void *arg),
|
|
|
|
void *arg)
|
|
|
|
{
|
|
|
|
struct strbuf temp_tab_file_name = STRBUF_INIT;
|
|
|
|
struct strbuf tab_file_name = STRBUF_INIT;
|
|
|
|
struct strbuf next_name = STRBUF_INIT;
|
|
|
|
struct reftable_writer *wr = NULL;
|
2024-03-07 13:10:35 +00:00
|
|
|
struct tempfile *tab_file = NULL;
|
2021-10-07 20:25:13 +00:00
|
|
|
int err = 0;
|
2024-03-07 13:10:35 +00:00
|
|
|
int tab_fd;
|
2021-10-07 20:25:13 +00:00
|
|
|
|
|
|
|
strbuf_reset(&next_name);
|
|
|
|
format_name(&next_name, add->next_update_index, add->next_update_index);
|
|
|
|
|
|
|
|
stack_filename(&temp_tab_file_name, add->stack, next_name.buf);
|
|
|
|
strbuf_addstr(&temp_tab_file_name, ".temp.XXXXXX");
|
|
|
|
|
2024-03-07 13:10:35 +00:00
|
|
|
tab_file = mks_tempfile(temp_tab_file_name.buf);
|
|
|
|
if (!tab_file) {
|
2021-10-07 20:25:13 +00:00
|
|
|
err = REFTABLE_IO_ERROR;
|
|
|
|
goto done;
|
|
|
|
}
|
2021-12-23 19:29:50 +00:00
|
|
|
if (add->stack->config.default_permissions) {
|
2024-03-07 13:10:35 +00:00
|
|
|
if (chmod(get_tempfile_path(tab_file),
|
|
|
|
add->stack->config.default_permissions)) {
|
2021-12-23 19:29:50 +00:00
|
|
|
err = REFTABLE_IO_ERROR;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
2024-03-07 13:10:35 +00:00
|
|
|
tab_fd = get_tempfile_fd(tab_file);
|
|
|
|
|
2024-01-23 18:51:10 +00:00
|
|
|
wr = reftable_new_writer(reftable_fd_write, reftable_fd_flush, &tab_fd,
|
2021-10-07 20:25:13 +00:00
|
|
|
&add->stack->config);
|
|
|
|
err = write_table(wr, arg);
|
|
|
|
if (err < 0)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
err = reftable_writer_close(wr);
|
|
|
|
if (err == REFTABLE_EMPTY_TABLE_ERROR) {
|
|
|
|
err = 0;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
if (err < 0)
|
|
|
|
goto done;
|
|
|
|
|
2024-03-07 13:10:35 +00:00
|
|
|
err = close_tempfile_gently(tab_file);
|
2021-10-07 20:25:13 +00:00
|
|
|
if (err < 0) {
|
|
|
|
err = REFTABLE_IO_ERROR;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2024-03-07 13:10:35 +00:00
|
|
|
err = stack_check_addition(add->stack, get_tempfile_path(tab_file));
|
2021-10-07 20:25:13 +00:00
|
|
|
if (err < 0)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
if (wr->min_update_index < add->next_update_index) {
|
|
|
|
err = REFTABLE_API_ERROR;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
format_name(&next_name, wr->min_update_index, wr->max_update_index);
|
|
|
|
strbuf_addstr(&next_name, ".ref");
|
|
|
|
stack_filename(&tab_file_name, add->stack, next_name.buf);
|
|
|
|
|
|
|
|
/*
|
|
|
|
On windows, this relies on rand() picking a unique destination name.
|
|
|
|
Maybe we should do retry loop as well?
|
|
|
|
*/
|
2024-03-07 13:10:35 +00:00
|
|
|
err = rename_tempfile(&tab_file, tab_file_name.buf);
|
2021-10-07 20:25:13 +00:00
|
|
|
if (err < 0) {
|
|
|
|
err = REFTABLE_IO_ERROR;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
reftable: introduce macros to grow arrays
Throughout the reftable library we have many cases where we need to grow
arrays. In order to avoid too many reallocations, we roughly double the
capacity of the array on each iteration. The resulting code pattern is
duplicated across many sites.
We have similar patterns in our main codebase, which is why we have
eventually introduced an `ALLOC_GROW()` macro to abstract it away and
avoid some code duplication. We cannot easily reuse this macro here
though because `ALLOC_GROW()` uses `REALLOC_ARRAY()`, which in turn will
call realloc(3P) to grow the array. The reftable code is structured as a
library though (even if the boundaries are fuzzy), and one property this
brings with it is that it is possible to plug in your own allocators. So
instead of using realloc(3P), we need to use `reftable_realloc()` that
knows to use the user-provided implementation.
So let's introduce two new macros `REFTABLE_REALLOC_ARRAY()` and
`REFTABLE_ALLOC_GROW()` that mirror what we do in our main codebase,
with two modifications:
- They use `reftable_realloc()`, as explained above.
- They use a different growth factor of `2 * cap + 1` instead of `(cap
+ 16) * 3 / 2`.
The second change is because we know a bit more about the allocation
patterns in the reftable library. In most cases, we end up only having a
handful of items in the array and don't end up growing them. The initial
capacity that our normal growth factor uses (which is 24) would thus end
up over-allocating in a lot of code paths. This effect is measurable:
- Before change:
HEAP SUMMARY:
in use at exit: 671,983 bytes in 152 blocks
total heap usage: 3,843,446 allocs, 3,843,294 frees, 223,761,402 bytes allocated
- After change with a growth factor of `(2 * alloc + 1)`:
HEAP SUMMARY:
in use at exit: 671,983 bytes in 152 blocks
total heap usage: 3,843,446 allocs, 3,843,294 frees, 223,761,410 bytes allocated
- After change with a growth factor of `(alloc + 16)* 2 / 3`:
HEAP SUMMARY:
in use at exit: 671,983 bytes in 152 blocks
total heap usage: 3,833,673 allocs, 3,833,521 frees, 4,728,251,742 bytes allocated
While the total heap usage is roughly the same, we do end up allocating
significantly more bytes with our usual growth factor (in fact, roughly
21 times as many).
Convert the reftable library to use these new macros.
Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2024-02-06 06:35:23 +00:00
|
|
|
REFTABLE_ALLOC_GROW(add->new_tables, add->new_tables_len + 1,
|
|
|
|
add->new_tables_cap);
|
|
|
|
add->new_tables[add->new_tables_len++] = strbuf_detach(&next_name, NULL);
|
2021-10-07 20:25:13 +00:00
|
|
|
done:
|
2024-03-07 13:10:35 +00:00
|
|
|
delete_tempfile(&tab_file);
|
2021-10-07 20:25:13 +00:00
|
|
|
strbuf_release(&temp_tab_file_name);
|
|
|
|
strbuf_release(&tab_file_name);
|
|
|
|
strbuf_release(&next_name);
|
|
|
|
reftable_writer_free(wr);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t reftable_stack_next_update_index(struct reftable_stack *st)
|
|
|
|
{
|
|
|
|
int sz = st->merged->stack_len;
|
|
|
|
if (sz > 0)
|
|
|
|
return reftable_reader_max_update_index(st->readers[sz - 1]) +
|
|
|
|
1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2024-02-06 06:35:41 +00:00
|
|
|
static int stack_compact_locked(struct reftable_stack *st,
|
|
|
|
size_t first, size_t last,
|
2024-03-07 13:10:43 +00:00
|
|
|
struct reftable_log_expiry_config *config,
|
|
|
|
struct tempfile **tab_file_out)
|
2021-10-07 20:25:13 +00:00
|
|
|
{
|
|
|
|
struct strbuf next_name = STRBUF_INIT;
|
2024-03-07 13:10:43 +00:00
|
|
|
struct strbuf tab_file_path = STRBUF_INIT;
|
2021-10-07 20:25:13 +00:00
|
|
|
struct reftable_writer *wr = NULL;
|
2024-03-07 13:10:43 +00:00
|
|
|
struct tempfile *tab_file;
|
|
|
|
int tab_fd, err = 0;
|
2021-10-07 20:25:13 +00:00
|
|
|
|
|
|
|
format_name(&next_name,
|
|
|
|
reftable_reader_min_update_index(st->readers[first]),
|
|
|
|
reftable_reader_max_update_index(st->readers[last]));
|
2024-03-07 13:10:43 +00:00
|
|
|
stack_filename(&tab_file_path, st, next_name.buf);
|
|
|
|
strbuf_addstr(&tab_file_path, ".temp.XXXXXX");
|
2021-10-07 20:25:13 +00:00
|
|
|
|
2024-03-07 13:10:43 +00:00
|
|
|
tab_file = mks_tempfile(tab_file_path.buf);
|
|
|
|
if (!tab_file) {
|
|
|
|
err = REFTABLE_IO_ERROR;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
tab_fd = get_tempfile_fd(tab_file);
|
2021-10-07 20:25:13 +00:00
|
|
|
|
2024-01-26 10:09:10 +00:00
|
|
|
if (st->config.default_permissions &&
|
2024-03-07 13:10:43 +00:00
|
|
|
chmod(get_tempfile_path(tab_file), st->config.default_permissions) < 0) {
|
2024-01-26 10:09:10 +00:00
|
|
|
err = REFTABLE_IO_ERROR;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2024-03-07 13:10:43 +00:00
|
|
|
wr = reftable_new_writer(reftable_fd_write, reftable_fd_flush,
|
|
|
|
&tab_fd, &st->config);
|
2021-10-07 20:25:13 +00:00
|
|
|
err = stack_write_compact(st, wr, first, last, config);
|
|
|
|
if (err < 0)
|
|
|
|
goto done;
|
2024-03-07 13:10:43 +00:00
|
|
|
|
2021-10-07 20:25:13 +00:00
|
|
|
err = reftable_writer_close(wr);
|
|
|
|
if (err < 0)
|
|
|
|
goto done;
|
|
|
|
|
2024-03-07 13:10:43 +00:00
|
|
|
err = close_tempfile_gently(tab_file);
|
|
|
|
if (err < 0)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
*tab_file_out = tab_file;
|
|
|
|
tab_file = NULL;
|
2021-10-07 20:25:13 +00:00
|
|
|
|
|
|
|
done:
|
2024-03-07 13:10:43 +00:00
|
|
|
delete_tempfile(&tab_file);
|
2021-10-07 20:25:13 +00:00
|
|
|
reftable_writer_free(wr);
|
|
|
|
strbuf_release(&next_name);
|
2024-03-07 13:10:43 +00:00
|
|
|
strbuf_release(&tab_file_path);
|
2021-10-07 20:25:13 +00:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int stack_write_compact(struct reftable_stack *st,
|
2024-02-06 06:35:41 +00:00
|
|
|
struct reftable_writer *wr,
|
|
|
|
size_t first, size_t last,
|
2021-10-07 20:25:13 +00:00
|
|
|
struct reftable_log_expiry_config *config)
|
|
|
|
{
|
2024-02-06 06:35:46 +00:00
|
|
|
size_t subtabs_len = last - first + 1;
|
2021-10-07 20:25:13 +00:00
|
|
|
struct reftable_table *subtabs = reftable_calloc(
|
2024-02-06 06:35:27 +00:00
|
|
|
last - first + 1, sizeof(*subtabs));
|
2021-10-07 20:25:13 +00:00
|
|
|
struct reftable_merged_table *mt = NULL;
|
|
|
|
struct reftable_iterator it = { NULL };
|
|
|
|
struct reftable_ref_record ref = { NULL };
|
|
|
|
struct reftable_log_record log = { NULL };
|
|
|
|
uint64_t entries = 0;
|
2024-02-06 06:35:41 +00:00
|
|
|
int err = 0;
|
2021-10-07 20:25:13 +00:00
|
|
|
|
2024-02-06 06:35:41 +00:00
|
|
|
for (size_t i = first, j = 0; i <= last; i++) {
|
2021-10-07 20:25:13 +00:00
|
|
|
struct reftable_reader *t = st->readers[i];
|
|
|
|
reftable_table_from_reader(&subtabs[j++], t);
|
|
|
|
st->stats.bytes += t->size;
|
|
|
|
}
|
|
|
|
reftable_writer_set_limits(wr, st->readers[first]->min_update_index,
|
|
|
|
st->readers[last]->max_update_index);
|
|
|
|
|
|
|
|
err = reftable_new_merged_table(&mt, subtabs, subtabs_len,
|
|
|
|
st->config.hash_id);
|
|
|
|
if (err < 0) {
|
|
|
|
reftable_free(subtabs);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = reftable_merged_table_seek_ref(mt, &it, "");
|
|
|
|
if (err < 0)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
err = reftable_iterator_next_ref(&it, &ref);
|
|
|
|
if (err > 0) {
|
|
|
|
err = 0;
|
|
|
|
break;
|
|
|
|
}
|
2024-01-03 06:22:13 +00:00
|
|
|
if (err < 0)
|
|
|
|
goto done;
|
2021-10-07 20:25:13 +00:00
|
|
|
|
|
|
|
if (first == 0 && reftable_ref_record_is_deletion(&ref)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = reftable_writer_add_ref(wr, &ref);
|
2024-01-03 06:22:13 +00:00
|
|
|
if (err < 0)
|
|
|
|
goto done;
|
2021-10-07 20:25:13 +00:00
|
|
|
entries++;
|
|
|
|
}
|
|
|
|
reftable_iterator_destroy(&it);
|
|
|
|
|
|
|
|
err = reftable_merged_table_seek_log(mt, &it, "");
|
|
|
|
if (err < 0)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
err = reftable_iterator_next_log(&it, &log);
|
|
|
|
if (err > 0) {
|
|
|
|
err = 0;
|
|
|
|
break;
|
|
|
|
}
|
2024-01-03 06:22:13 +00:00
|
|
|
if (err < 0)
|
|
|
|
goto done;
|
2021-10-07 20:25:13 +00:00
|
|
|
if (first == 0 && reftable_log_record_is_deletion(&log)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (config && config->min_update_index > 0 &&
|
|
|
|
log.update_index < config->min_update_index) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (config && config->time > 0 &&
|
|
|
|
log.value.update.time < config->time) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = reftable_writer_add_log(wr, &log);
|
2024-01-03 06:22:13 +00:00
|
|
|
if (err < 0)
|
|
|
|
goto done;
|
2021-10-07 20:25:13 +00:00
|
|
|
entries++;
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
reftable_iterator_destroy(&it);
|
|
|
|
if (mt) {
|
|
|
|
merged_table_release(mt);
|
|
|
|
reftable_merged_table_free(mt);
|
|
|
|
}
|
|
|
|
reftable_ref_record_release(&ref);
|
|
|
|
reftable_log_record_release(&log);
|
|
|
|
st->stats.entries_written += entries;
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2024-03-25 10:02:46 +00:00
|
|
|
/*
|
|
|
|
* Compact all tables in the range `[first, last)` into a single new table.
|
|
|
|
*
|
|
|
|
* This function returns `0` on success or a code `< 0` on failure. When the
|
|
|
|
* stack or any of the tables in the specified range are already locked then
|
|
|
|
* this function returns `REFTABLE_LOCK_ERROR`. This is a benign error that
|
|
|
|
* callers can either ignore, or they may choose to retry compaction after some
|
|
|
|
* amount of time.
|
|
|
|
*/
|
2024-02-06 06:35:41 +00:00
|
|
|
static int stack_compact_range(struct reftable_stack *st,
|
|
|
|
size_t first, size_t last,
|
2021-10-07 20:25:13 +00:00
|
|
|
struct reftable_log_expiry_config *expiry)
|
|
|
|
{
|
2024-03-07 13:10:39 +00:00
|
|
|
struct strbuf tables_list_buf = STRBUF_INIT;
|
2021-10-07 20:25:13 +00:00
|
|
|
struct strbuf new_table_name = STRBUF_INIT;
|
|
|
|
struct strbuf new_table_path = STRBUF_INIT;
|
2024-03-07 13:10:39 +00:00
|
|
|
struct strbuf table_name = STRBUF_INIT;
|
|
|
|
struct lock_file tables_list_lock = LOCK_INIT;
|
|
|
|
struct lock_file *table_locks = NULL;
|
2024-03-07 13:10:43 +00:00
|
|
|
struct tempfile *new_table = NULL;
|
2024-03-07 13:10:39 +00:00
|
|
|
int is_empty_table = 0, err = 0;
|
|
|
|
size_t i;
|
2021-10-07 20:25:13 +00:00
|
|
|
|
|
|
|
if (first > last || (!expiry && first == last)) {
|
|
|
|
err = 0;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
st->stats.attempts++;
|
|
|
|
|
2024-03-07 13:10:39 +00:00
|
|
|
/*
|
|
|
|
* Hold the lock so that we can read "tables.list" and lock all tables
|
|
|
|
* which are part of the user-specified range.
|
|
|
|
*/
|
|
|
|
err = hold_lock_file_for_update(&tables_list_lock, st->list_file,
|
|
|
|
LOCK_NO_DEREF);
|
|
|
|
if (err < 0) {
|
|
|
|
if (errno == EEXIST)
|
2024-03-25 10:02:46 +00:00
|
|
|
err = REFTABLE_LOCK_ERROR;
|
2024-03-07 13:10:39 +00:00
|
|
|
else
|
2021-10-07 20:25:13 +00:00
|
|
|
err = REFTABLE_IO_ERROR;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = stack_uptodate(st);
|
2024-03-07 13:10:39 +00:00
|
|
|
if (err)
|
2021-10-07 20:25:13 +00:00
|
|
|
goto done;
|
|
|
|
|
2024-03-07 13:10:39 +00:00
|
|
|
/*
|
|
|
|
* Lock all tables in the user-provided range. This is the slice of our
|
|
|
|
* stack which we'll compact.
|
|
|
|
*/
|
|
|
|
REFTABLE_CALLOC_ARRAY(table_locks, last - first + 1);
|
|
|
|
for (i = first; i <= last; i++) {
|
|
|
|
stack_filename(&table_name, st, reader_name(st->readers[i]));
|
2021-10-07 20:25:13 +00:00
|
|
|
|
2024-03-07 13:10:39 +00:00
|
|
|
err = hold_lock_file_for_update(&table_locks[i - first],
|
|
|
|
table_name.buf, LOCK_NO_DEREF);
|
|
|
|
if (err < 0) {
|
|
|
|
if (errno == EEXIST)
|
2024-03-25 10:02:46 +00:00
|
|
|
err = REFTABLE_LOCK_ERROR;
|
2024-03-07 13:10:39 +00:00
|
|
|
else
|
2021-10-07 20:25:13 +00:00
|
|
|
err = REFTABLE_IO_ERROR;
|
2024-03-07 13:10:39 +00:00
|
|
|
goto done;
|
2021-10-07 20:25:13 +00:00
|
|
|
}
|
|
|
|
|
2024-03-07 13:10:39 +00:00
|
|
|
/*
|
|
|
|
* We need to close the lockfiles as we might otherwise easily
|
|
|
|
* run into file descriptor exhaustion when we compress a lot
|
|
|
|
* of tables.
|
|
|
|
*/
|
|
|
|
err = close_lock_file_gently(&table_locks[i - first]);
|
|
|
|
if (err < 0) {
|
|
|
|
err = REFTABLE_IO_ERROR;
|
2021-10-07 20:25:13 +00:00
|
|
|
goto done;
|
2024-03-07 13:10:39 +00:00
|
|
|
}
|
2021-10-07 20:25:13 +00:00
|
|
|
}
|
|
|
|
|
2024-03-07 13:10:39 +00:00
|
|
|
/*
|
|
|
|
* We have locked all tables in our range and can thus release the
|
|
|
|
* "tables.list" lock while compacting the locked tables. This allows
|
|
|
|
* concurrent updates to the stack to proceed.
|
|
|
|
*/
|
|
|
|
err = rollback_lock_file(&tables_list_lock);
|
|
|
|
if (err < 0) {
|
|
|
|
err = REFTABLE_IO_ERROR;
|
2021-10-07 20:25:13 +00:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2024-03-07 13:10:39 +00:00
|
|
|
/*
|
|
|
|
* Compact the now-locked tables into a new table. Note that compacting
|
|
|
|
* these tables may end up with an empty new table in case tombstones
|
|
|
|
* end up cancelling out all refs in that range.
|
|
|
|
*/
|
2024-03-07 13:10:43 +00:00
|
|
|
err = stack_compact_locked(st, first, last, expiry, &new_table);
|
2024-03-07 13:10:39 +00:00
|
|
|
if (err < 0) {
|
|
|
|
if (err != REFTABLE_EMPTY_TABLE_ERROR)
|
|
|
|
goto done;
|
|
|
|
is_empty_table = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now that we have written the new, compacted table we need to re-lock
|
|
|
|
* "tables.list". We'll then replace the compacted range of tables with
|
|
|
|
* the new table.
|
|
|
|
*/
|
|
|
|
err = hold_lock_file_for_update(&tables_list_lock, st->list_file,
|
|
|
|
LOCK_NO_DEREF);
|
|
|
|
if (err < 0) {
|
|
|
|
if (errno == EEXIST)
|
2024-03-25 10:02:46 +00:00
|
|
|
err = REFTABLE_LOCK_ERROR;
|
2024-03-07 13:10:39 +00:00
|
|
|
else
|
2021-10-07 20:25:13 +00:00
|
|
|
err = REFTABLE_IO_ERROR;
|
|
|
|
goto done;
|
|
|
|
}
|
2024-03-07 13:10:39 +00:00
|
|
|
|
2021-12-23 19:29:50 +00:00
|
|
|
if (st->config.default_permissions) {
|
2024-03-07 13:10:39 +00:00
|
|
|
if (chmod(get_lock_file_path(&tables_list_lock),
|
|
|
|
st->config.default_permissions) < 0) {
|
2021-12-23 19:29:50 +00:00
|
|
|
err = REFTABLE_IO_ERROR;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
2021-10-07 20:25:13 +00:00
|
|
|
|
2024-03-07 13:10:39 +00:00
|
|
|
/*
|
|
|
|
* If the resulting compacted table is not empty, then we need to move
|
|
|
|
* it into place now.
|
|
|
|
*/
|
2021-10-07 20:25:13 +00:00
|
|
|
if (!is_empty_table) {
|
2024-03-07 13:10:39 +00:00
|
|
|
format_name(&new_table_name, st->readers[first]->min_update_index,
|
|
|
|
st->readers[last]->max_update_index);
|
|
|
|
strbuf_addstr(&new_table_name, ".ref");
|
|
|
|
stack_filename(&new_table_path, st, new_table_name.buf);
|
|
|
|
|
2024-03-07 13:10:43 +00:00
|
|
|
err = rename_tempfile(&new_table, new_table_path.buf);
|
2021-10-07 20:25:13 +00:00
|
|
|
if (err < 0) {
|
|
|
|
err = REFTABLE_IO_ERROR;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-07 13:10:39 +00:00
|
|
|
/*
|
|
|
|
* Write the new "tables.list" contents with the compacted table we
|
|
|
|
* have just written. In case the compacted table became empty we
|
|
|
|
* simply skip writing it.
|
|
|
|
*/
|
|
|
|
for (i = 0; i < first; i++)
|
|
|
|
strbuf_addf(&tables_list_buf, "%s\n", st->readers[i]->name);
|
|
|
|
if (!is_empty_table)
|
|
|
|
strbuf_addf(&tables_list_buf, "%s\n", new_table_name.buf);
|
|
|
|
for (i = last + 1; i < st->merged->stack_len; i++)
|
|
|
|
strbuf_addf(&tables_list_buf, "%s\n", st->readers[i]->name);
|
|
|
|
|
|
|
|
err = write_in_full(get_lock_file_fd(&tables_list_lock),
|
|
|
|
tables_list_buf.buf, tables_list_buf.len);
|
2024-01-30 05:22:47 +00:00
|
|
|
if (err < 0) {
|
|
|
|
err = REFTABLE_IO_ERROR;
|
|
|
|
unlink(new_table_path.buf);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2024-03-07 13:10:39 +00:00
|
|
|
err = fsync_component(FSYNC_COMPONENT_REFERENCE, get_lock_file_fd(&tables_list_lock));
|
2021-10-07 20:25:13 +00:00
|
|
|
if (err < 0) {
|
|
|
|
err = REFTABLE_IO_ERROR;
|
|
|
|
unlink(new_table_path.buf);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2024-03-07 13:10:39 +00:00
|
|
|
err = commit_lock_file(&tables_list_lock);
|
2021-10-07 20:25:13 +00:00
|
|
|
if (err < 0) {
|
|
|
|
err = REFTABLE_IO_ERROR;
|
|
|
|
unlink(new_table_path.buf);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2024-03-07 13:10:39 +00:00
|
|
|
/*
|
|
|
|
* Reload the stack before deleting the compacted tables. We can only
|
|
|
|
* delete the files after we closed them on Windows, so this needs to
|
|
|
|
* happen first.
|
|
|
|
*/
|
2021-10-07 20:25:13 +00:00
|
|
|
err = reftable_stack_reload_maybe_reuse(st, first < last);
|
2024-03-07 13:10:39 +00:00
|
|
|
if (err < 0)
|
|
|
|
goto done;
|
2021-10-07 20:25:13 +00:00
|
|
|
|
2024-03-07 13:10:39 +00:00
|
|
|
/*
|
|
|
|
* Delete the old tables. They may still be in use by concurrent
|
|
|
|
* readers, so it is expected that unlinking tables may fail.
|
|
|
|
*/
|
|
|
|
for (i = first; i <= last; i++) {
|
|
|
|
struct lock_file *table_lock = &table_locks[i - first];
|
|
|
|
char *table_path = get_locked_file_path(table_lock);
|
|
|
|
unlink(table_path);
|
|
|
|
free(table_path);
|
2021-10-07 20:25:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
2024-03-07 13:10:39 +00:00
|
|
|
rollback_lock_file(&tables_list_lock);
|
|
|
|
for (i = first; table_locks && i <= last; i++)
|
|
|
|
rollback_lock_file(&table_locks[i - first]);
|
|
|
|
reftable_free(table_locks);
|
2021-10-07 20:25:13 +00:00
|
|
|
|
2024-03-07 13:10:43 +00:00
|
|
|
delete_tempfile(&new_table);
|
2021-10-07 20:25:13 +00:00
|
|
|
strbuf_release(&new_table_name);
|
|
|
|
strbuf_release(&new_table_path);
|
2024-03-07 13:10:43 +00:00
|
|
|
|
2024-03-07 13:10:39 +00:00
|
|
|
strbuf_release(&tables_list_buf);
|
|
|
|
strbuf_release(&table_name);
|
2021-10-07 20:25:13 +00:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
int reftable_stack_compact_all(struct reftable_stack *st,
|
|
|
|
struct reftable_log_expiry_config *config)
|
|
|
|
{
|
2024-02-06 06:35:41 +00:00
|
|
|
return stack_compact_range(st, 0, st->merged->stack_len ?
|
|
|
|
st->merged->stack_len - 1 : 0, config);
|
2021-10-07 20:25:13 +00:00
|
|
|
}
|
|
|
|
|
2024-02-06 06:35:41 +00:00
|
|
|
static int stack_compact_range_stats(struct reftable_stack *st,
|
|
|
|
size_t first, size_t last,
|
2021-10-07 20:25:13 +00:00
|
|
|
struct reftable_log_expiry_config *config)
|
|
|
|
{
|
|
|
|
int err = stack_compact_range(st, first, last, config);
|
2024-03-25 10:02:46 +00:00
|
|
|
if (err == REFTABLE_LOCK_ERROR)
|
2021-10-07 20:25:13 +00:00
|
|
|
st->stats.failures++;
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int segment_size(struct segment *s)
|
|
|
|
{
|
|
|
|
return s->end - s->start;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fastlog2(uint64_t sz)
|
|
|
|
{
|
|
|
|
int l = 0;
|
|
|
|
if (sz == 0)
|
|
|
|
return 0;
|
|
|
|
for (; sz; sz /= 2) {
|
|
|
|
l++;
|
|
|
|
}
|
|
|
|
return l - 1;
|
|
|
|
}
|
|
|
|
|
2024-02-06 06:35:35 +00:00
|
|
|
struct segment *sizes_to_segments(size_t *seglen, uint64_t *sizes, size_t n)
|
2021-10-07 20:25:13 +00:00
|
|
|
{
|
2024-02-06 06:35:27 +00:00
|
|
|
struct segment *segs = reftable_calloc(n, sizeof(*segs));
|
2021-10-07 20:25:13 +00:00
|
|
|
struct segment cur = { 0 };
|
2024-02-06 06:35:35 +00:00
|
|
|
size_t next = 0, i;
|
2021-10-07 20:25:13 +00:00
|
|
|
|
|
|
|
if (n == 0) {
|
|
|
|
*seglen = 0;
|
|
|
|
return segs;
|
|
|
|
}
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
int log = fastlog2(sizes[i]);
|
|
|
|
if (cur.log != log && cur.bytes > 0) {
|
|
|
|
struct segment fresh = {
|
|
|
|
.start = i,
|
|
|
|
};
|
|
|
|
|
|
|
|
segs[next++] = cur;
|
|
|
|
cur = fresh;
|
|
|
|
}
|
|
|
|
|
|
|
|
cur.log = log;
|
|
|
|
cur.end = i + 1;
|
|
|
|
cur.bytes += sizes[i];
|
|
|
|
}
|
|
|
|
segs[next++] = cur;
|
|
|
|
*seglen = next;
|
|
|
|
return segs;
|
|
|
|
}
|
|
|
|
|
2024-02-06 06:35:35 +00:00
|
|
|
struct segment suggest_compaction_segment(uint64_t *sizes, size_t n)
|
2021-10-07 20:25:13 +00:00
|
|
|
{
|
|
|
|
struct segment min_seg = {
|
|
|
|
.log = 64,
|
|
|
|
};
|
2024-02-06 06:35:35 +00:00
|
|
|
struct segment *segs;
|
|
|
|
size_t seglen = 0, i;
|
|
|
|
|
|
|
|
segs = sizes_to_segments(&seglen, sizes, n);
|
2021-10-07 20:25:13 +00:00
|
|
|
for (i = 0; i < seglen; i++) {
|
2024-02-06 06:35:35 +00:00
|
|
|
if (segment_size(&segs[i]) == 1)
|
2021-10-07 20:25:13 +00:00
|
|
|
continue;
|
|
|
|
|
2024-02-06 06:35:35 +00:00
|
|
|
if (segs[i].log < min_seg.log)
|
2021-10-07 20:25:13 +00:00
|
|
|
min_seg = segs[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
while (min_seg.start > 0) {
|
2024-02-06 06:35:35 +00:00
|
|
|
size_t prev = min_seg.start - 1;
|
|
|
|
if (fastlog2(min_seg.bytes) < fastlog2(sizes[prev]))
|
2021-10-07 20:25:13 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
min_seg.start = prev;
|
|
|
|
min_seg.bytes += sizes[prev];
|
|
|
|
}
|
|
|
|
|
|
|
|
reftable_free(segs);
|
|
|
|
return min_seg;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint64_t *stack_table_sizes_for_compaction(struct reftable_stack *st)
|
|
|
|
{
|
|
|
|
uint64_t *sizes =
|
2024-02-06 06:35:27 +00:00
|
|
|
reftable_calloc(st->merged->stack_len, sizeof(*sizes));
|
2021-10-07 20:25:13 +00:00
|
|
|
int version = (st->config.hash_id == GIT_SHA1_FORMAT_ID) ? 1 : 2;
|
|
|
|
int overhead = header_size(version) - 1;
|
|
|
|
int i = 0;
|
|
|
|
for (i = 0; i < st->merged->stack_len; i++) {
|
|
|
|
sizes[i] = st->readers[i]->size - overhead;
|
|
|
|
}
|
|
|
|
return sizes;
|
|
|
|
}
|
|
|
|
|
|
|
|
int reftable_stack_auto_compact(struct reftable_stack *st)
|
|
|
|
{
|
|
|
|
uint64_t *sizes = stack_table_sizes_for_compaction(st);
|
|
|
|
struct segment seg =
|
|
|
|
suggest_compaction_segment(sizes, st->merged->stack_len);
|
|
|
|
reftable_free(sizes);
|
|
|
|
if (segment_size(&seg) > 0)
|
|
|
|
return stack_compact_range_stats(st, seg.start, seg.end - 1,
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct reftable_compaction_stats *
|
|
|
|
reftable_stack_compaction_stats(struct reftable_stack *st)
|
|
|
|
{
|
|
|
|
return &st->stats;
|
|
|
|
}
|
|
|
|
|
|
|
|
int reftable_stack_read_ref(struct reftable_stack *st, const char *refname,
|
|
|
|
struct reftable_ref_record *ref)
|
|
|
|
{
|
|
|
|
struct reftable_table tab = { NULL };
|
|
|
|
reftable_table_from_merged_table(&tab, reftable_stack_merged_table(st));
|
|
|
|
return reftable_table_read_ref(&tab, refname, ref);
|
|
|
|
}
|
|
|
|
|
|
|
|
int reftable_stack_read_log(struct reftable_stack *st, const char *refname,
|
|
|
|
struct reftable_log_record *log)
|
|
|
|
{
|
|
|
|
struct reftable_iterator it = { NULL };
|
|
|
|
struct reftable_merged_table *mt = reftable_stack_merged_table(st);
|
|
|
|
int err = reftable_merged_table_seek_log(mt, &it, refname);
|
|
|
|
if (err)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
err = reftable_iterator_next_log(&it, log);
|
|
|
|
if (err)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
if (strcmp(log->refname, refname) ||
|
|
|
|
reftable_log_record_is_deletion(log)) {
|
|
|
|
err = 1;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
if (err) {
|
|
|
|
reftable_log_record_release(log);
|
|
|
|
}
|
|
|
|
reftable_iterator_destroy(&it);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int stack_check_addition(struct reftable_stack *st,
|
|
|
|
const char *new_tab_name)
|
|
|
|
{
|
|
|
|
int err = 0;
|
|
|
|
struct reftable_block_source src = { NULL };
|
|
|
|
struct reftable_reader *rd = NULL;
|
|
|
|
struct reftable_table tab = { NULL };
|
|
|
|
struct reftable_ref_record *refs = NULL;
|
|
|
|
struct reftable_iterator it = { NULL };
|
|
|
|
int cap = 0;
|
|
|
|
int len = 0;
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
if (st->config.skip_name_check)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
err = reftable_block_source_from_file(&src, new_tab_name);
|
|
|
|
if (err < 0)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
err = reftable_new_reader(&rd, &src, new_tab_name);
|
|
|
|
if (err < 0)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
err = reftable_reader_seek_ref(rd, &it, "");
|
|
|
|
if (err > 0) {
|
|
|
|
err = 0;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
if (err < 0)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
struct reftable_ref_record ref = { NULL };
|
|
|
|
err = reftable_iterator_next_ref(&it, &ref);
|
reftable: introduce macros to grow arrays
Throughout the reftable library we have many cases where we need to grow
arrays. In order to avoid too many reallocations, we roughly double the
capacity of the array on each iteration. The resulting code pattern is
duplicated across many sites.
We have similar patterns in our main codebase, which is why we have
eventually introduced an `ALLOC_GROW()` macro to abstract it away and
avoid some code duplication. We cannot easily reuse this macro here
though because `ALLOC_GROW()` uses `REALLOC_ARRAY()`, which in turn will
call realloc(3P) to grow the array. The reftable code is structured as a
library though (even if the boundaries are fuzzy), and one property this
brings with it is that it is possible to plug in your own allocators. So
instead of using realloc(3P), we need to use `reftable_realloc()` that
knows to use the user-provided implementation.
So let's introduce two new macros `REFTABLE_REALLOC_ARRAY()` and
`REFTABLE_ALLOC_GROW()` that mirror what we do in our main codebase,
with two modifications:
- They use `reftable_realloc()`, as explained above.
- They use a different growth factor of `2 * cap + 1` instead of `(cap
+ 16) * 3 / 2`.
The second change is because we know a bit more about the allocation
patterns in the reftable library. In most cases, we end up only having a
handful of items in the array and don't end up growing them. The initial
capacity that our normal growth factor uses (which is 24) would thus end
up over-allocating in a lot of code paths. This effect is measurable:
- Before change:
HEAP SUMMARY:
in use at exit: 671,983 bytes in 152 blocks
total heap usage: 3,843,446 allocs, 3,843,294 frees, 223,761,402 bytes allocated
- After change with a growth factor of `(2 * alloc + 1)`:
HEAP SUMMARY:
in use at exit: 671,983 bytes in 152 blocks
total heap usage: 3,843,446 allocs, 3,843,294 frees, 223,761,410 bytes allocated
- After change with a growth factor of `(alloc + 16)* 2 / 3`:
HEAP SUMMARY:
in use at exit: 671,983 bytes in 152 blocks
total heap usage: 3,833,673 allocs, 3,833,521 frees, 4,728,251,742 bytes allocated
While the total heap usage is roughly the same, we do end up allocating
significantly more bytes with our usual growth factor (in fact, roughly
21 times as many).
Convert the reftable library to use these new macros.
Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2024-02-06 06:35:23 +00:00
|
|
|
if (err > 0)
|
2021-10-07 20:25:13 +00:00
|
|
|
break;
|
|
|
|
if (err < 0)
|
|
|
|
goto done;
|
|
|
|
|
reftable: introduce macros to grow arrays
Throughout the reftable library we have many cases where we need to grow
arrays. In order to avoid too many reallocations, we roughly double the
capacity of the array on each iteration. The resulting code pattern is
duplicated across many sites.
We have similar patterns in our main codebase, which is why we have
eventually introduced an `ALLOC_GROW()` macro to abstract it away and
avoid some code duplication. We cannot easily reuse this macro here
though because `ALLOC_GROW()` uses `REALLOC_ARRAY()`, which in turn will
call realloc(3P) to grow the array. The reftable code is structured as a
library though (even if the boundaries are fuzzy), and one property this
brings with it is that it is possible to plug in your own allocators. So
instead of using realloc(3P), we need to use `reftable_realloc()` that
knows to use the user-provided implementation.
So let's introduce two new macros `REFTABLE_REALLOC_ARRAY()` and
`REFTABLE_ALLOC_GROW()` that mirror what we do in our main codebase,
with two modifications:
- They use `reftable_realloc()`, as explained above.
- They use a different growth factor of `2 * cap + 1` instead of `(cap
+ 16) * 3 / 2`.
The second change is because we know a bit more about the allocation
patterns in the reftable library. In most cases, we end up only having a
handful of items in the array and don't end up growing them. The initial
capacity that our normal growth factor uses (which is 24) would thus end
up over-allocating in a lot of code paths. This effect is measurable:
- Before change:
HEAP SUMMARY:
in use at exit: 671,983 bytes in 152 blocks
total heap usage: 3,843,446 allocs, 3,843,294 frees, 223,761,402 bytes allocated
- After change with a growth factor of `(2 * alloc + 1)`:
HEAP SUMMARY:
in use at exit: 671,983 bytes in 152 blocks
total heap usage: 3,843,446 allocs, 3,843,294 frees, 223,761,410 bytes allocated
- After change with a growth factor of `(alloc + 16)* 2 / 3`:
HEAP SUMMARY:
in use at exit: 671,983 bytes in 152 blocks
total heap usage: 3,833,673 allocs, 3,833,521 frees, 4,728,251,742 bytes allocated
While the total heap usage is roughly the same, we do end up allocating
significantly more bytes with our usual growth factor (in fact, roughly
21 times as many).
Convert the reftable library to use these new macros.
Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2024-02-06 06:35:23 +00:00
|
|
|
REFTABLE_ALLOC_GROW(refs, len + 1, cap);
|
2021-10-07 20:25:13 +00:00
|
|
|
refs[len++] = ref;
|
|
|
|
}
|
|
|
|
|
|
|
|
reftable_table_from_merged_table(&tab, reftable_stack_merged_table(st));
|
|
|
|
|
|
|
|
err = validate_ref_record_addition(tab, refs, len);
|
|
|
|
|
|
|
|
done:
|
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
reftable_ref_record_release(&refs[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(refs);
|
|
|
|
reftable_iterator_destroy(&it);
|
|
|
|
reftable_reader_free(rd);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int is_table_name(const char *s)
|
|
|
|
{
|
|
|
|
const char *dot = strrchr(s, '.');
|
|
|
|
return dot && !strcmp(dot, ".ref");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void remove_maybe_stale_table(struct reftable_stack *st, uint64_t max,
|
|
|
|
const char *name)
|
|
|
|
{
|
|
|
|
int err = 0;
|
|
|
|
uint64_t update_idx = 0;
|
|
|
|
struct reftable_block_source src = { NULL };
|
|
|
|
struct reftable_reader *rd = NULL;
|
|
|
|
struct strbuf table_path = STRBUF_INIT;
|
|
|
|
stack_filename(&table_path, st, name);
|
|
|
|
|
|
|
|
err = reftable_block_source_from_file(&src, table_path.buf);
|
|
|
|
if (err < 0)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
err = reftable_new_reader(&rd, &src, name);
|
|
|
|
if (err < 0)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
update_idx = reftable_reader_max_update_index(rd);
|
|
|
|
reftable_reader_free(rd);
|
|
|
|
|
|
|
|
if (update_idx <= max) {
|
|
|
|
unlink(table_path.buf);
|
|
|
|
}
|
|
|
|
done:
|
|
|
|
strbuf_release(&table_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int reftable_stack_clean_locked(struct reftable_stack *st)
|
|
|
|
{
|
|
|
|
uint64_t max = reftable_merged_table_max_update_index(
|
|
|
|
reftable_stack_merged_table(st));
|
|
|
|
DIR *dir = opendir(st->reftable_dir);
|
|
|
|
struct dirent *d = NULL;
|
|
|
|
if (!dir) {
|
|
|
|
return REFTABLE_IO_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((d = readdir(dir))) {
|
|
|
|
int i = 0;
|
|
|
|
int found = 0;
|
|
|
|
if (!is_table_name(d->d_name))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (i = 0; !found && i < st->readers_len; i++) {
|
|
|
|
found = !strcmp(reader_name(st->readers[i]), d->d_name);
|
|
|
|
}
|
|
|
|
if (found)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
remove_maybe_stale_table(st, max, d->d_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
closedir(dir);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int reftable_stack_clean(struct reftable_stack *st)
|
|
|
|
{
|
|
|
|
struct reftable_addition *add = NULL;
|
|
|
|
int err = reftable_stack_new_addition(&add, st);
|
|
|
|
if (err < 0) {
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = reftable_stack_reload(st);
|
|
|
|
if (err < 0) {
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = reftable_stack_clean_locked(st);
|
|
|
|
|
|
|
|
done:
|
|
|
|
reftable_addition_destroy(add);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
int reftable_stack_print_directory(const char *stackdir, uint32_t hash_id)
|
|
|
|
{
|
|
|
|
struct reftable_stack *stack = NULL;
|
|
|
|
struct reftable_write_options cfg = { .hash_id = hash_id };
|
|
|
|
struct reftable_merged_table *merged = NULL;
|
|
|
|
struct reftable_table table = { NULL };
|
|
|
|
|
|
|
|
int err = reftable_new_stack(&stack, stackdir, cfg);
|
|
|
|
if (err < 0)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
merged = reftable_stack_merged_table(stack);
|
|
|
|
reftable_table_from_merged_table(&table, merged);
|
|
|
|
err = reftable_table_print(&table);
|
|
|
|
done:
|
|
|
|
if (stack)
|
|
|
|
reftable_stack_destroy(stack);
|
|
|
|
return err;
|
|
|
|
}
|