Originally, when designing pluggable object databases the goal was that
the object database can have multiple sources, and every source attached
to it could use a different backend. This would have allowed for quite a
lot of flexibility, as you could trivially mix and match different kinds
of object storages in whatever way you like.
But while well-intentioned, this design led to a bunch of conceptual
problems:
- We're now trying to read objects in source order, whereas we
previously tried to read objects via packfiles before trying to read
them via loose objects. This led to a performance regression when
using alternates or when using a quarantine directory.
- Some data structures are supposed to only ever exist once, like for
example bitmaps and commit graphs. At the same time, those data
structures also span across the union of all objects, so they may
cross sources.
- It is unclear how we can extend GIT_OBJECT_DIRECTORY or
GIT_ALTERNATE_OBJECT_DIRECTORIES to become backend-agnostic in a
backwards-compatible way. In general, introducing an object storage
extension into the current status quo where alternates may have to
be extended to become generic was proving to be painful.
- Some mechanisms of alternates assume way too much about how exactly
their backends work. Alternate refs for example assume that the
alternate is backed by a filesystem path, and that this filesystem
path may also allow us to read references. This is not a given
though, as backends may not even have local data at all.
In short, there are a bunch of conceptual mismatches when we have
alternates and pluggable object databases coexist. So while the original
idea was nice, it does not result in a system that is easy to reason
about.
Correct course by moving alternates into the "files" source itself so
that it becomes an implementation detail thereof so that we can avoid
all of these shortcomings. While it's unfortunate that we cannot easily
mix and match sources now, that ability doesn't go away. It's still very
much feasible to introduce a new backend that allows for exactly that
use case, and such a backend may also be a lot more flexible as we can
now add new logic to determine which objects should be stored where. So
the original motivation for having per-source backends can still be
realized with the new architecture.
Note that as part of this move, we also handle the GIT_OBJECT_DIRECTORY
and GIT_ALTERNATE_OBJECT_DIRECTORIES environment variables in the
"files" backend. This may be surprising at first, but object directories
are very much a concept of that backend, too. So these variables would
have bad interactions with other backends, and they create a bit of a
mismatch with the eventual object storage extension that we plan to
introduce.
Note that this commit is way larger than I'd like it to be. I'm sorry, I
couldn't find a way to split it up further. That being said, most of the
changes are straight-forward conversions that go from iterating over
sources to iterating over object directories. The more involved changes
are moving the infrastructure to track individual sources from "odb.c"
and moving them into "odb/source-files.c".
This also serves to show a bit of a who's-who of commands that don't
work properly with pluggable object databases. Almost all of these
commands are related to housekeeping though -- some of them will be
converted eventually, like for example commit graphs or MIDXs. But many
of them will stay incompatible going forward as they are simply too
specific to the "files" backend.
Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
283 lines
6.8 KiB
C
283 lines
6.8 KiB
C
#include "git-compat-util.h"
|
|
#include "diagnose.h"
|
|
#include "compat/disk.h"
|
|
#include "archive.h"
|
|
#include "dir.h"
|
|
#include "help.h"
|
|
#include "gettext.h"
|
|
#include "hex.h"
|
|
#include "strvec.h"
|
|
#include "odb.h"
|
|
#include "packfile.h"
|
|
#include "parse-options.h"
|
|
#include "repository.h"
|
|
#include "write-or-die.h"
|
|
|
|
struct archive_dir {
|
|
const char *path;
|
|
int recursive;
|
|
};
|
|
|
|
struct diagnose_option {
|
|
enum diagnose_mode mode;
|
|
const char *option_name;
|
|
};
|
|
|
|
static struct diagnose_option diagnose_options[] = {
|
|
{ DIAGNOSE_STATS, "stats" },
|
|
{ DIAGNOSE_ALL, "all" },
|
|
};
|
|
|
|
int option_parse_diagnose(const struct option *opt, const char *arg, int unset)
|
|
{
|
|
enum diagnose_mode *diagnose = opt->value;
|
|
|
|
if (!arg) {
|
|
*diagnose = unset ? DIAGNOSE_NONE : DIAGNOSE_STATS;
|
|
return 0;
|
|
}
|
|
|
|
for (size_t i = 0; i < ARRAY_SIZE(diagnose_options); i++) {
|
|
if (!strcmp(arg, diagnose_options[i].option_name)) {
|
|
*diagnose = diagnose_options[i].mode;
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
return error(_("invalid --%s value '%s'"), opt->long_name, arg);
|
|
}
|
|
|
|
static void dir_file_stats_objects(const char *full_path,
|
|
size_t full_path_len UNUSED,
|
|
const char *file_name, void *data)
|
|
{
|
|
struct strbuf *buf = data;
|
|
struct stat st;
|
|
|
|
if (!stat(full_path, &st))
|
|
strbuf_addf(buf, "%-70s %16" PRIuMAX "\n", file_name,
|
|
(uintmax_t)st.st_size);
|
|
}
|
|
|
|
static int dir_file_stats(struct odb_files_dir *source, void *data)
|
|
{
|
|
struct strbuf *buf = data;
|
|
|
|
strbuf_addf(buf, "Contents of %s:\n", source->abspath);
|
|
|
|
for_each_file_in_pack_dir(source->abspath, dir_file_stats_objects,
|
|
data);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int count_files(struct strbuf *path)
|
|
{
|
|
DIR *dir = opendir(path->buf);
|
|
struct dirent *e;
|
|
int count = 0;
|
|
|
|
if (!dir)
|
|
return 0;
|
|
|
|
while ((e = readdir_skip_dot_and_dotdot(dir)) != NULL)
|
|
if (get_dtype(e, path, 0) == DT_REG)
|
|
count++;
|
|
|
|
closedir(dir);
|
|
return count;
|
|
}
|
|
|
|
static void loose_objs_stats(struct strbuf *buf, const char *path)
|
|
{
|
|
DIR *dir = opendir(path);
|
|
struct dirent *e;
|
|
int count;
|
|
int total = 0;
|
|
unsigned char c;
|
|
struct strbuf count_path = STRBUF_INIT;
|
|
size_t base_path_len;
|
|
|
|
if (!dir)
|
|
return;
|
|
|
|
strbuf_addstr(buf, "Object directory stats for ");
|
|
strbuf_add_absolute_path(buf, path);
|
|
strbuf_addstr(buf, ":\n");
|
|
|
|
strbuf_add_absolute_path(&count_path, path);
|
|
strbuf_addch(&count_path, '/');
|
|
base_path_len = count_path.len;
|
|
|
|
while ((e = readdir_skip_dot_and_dotdot(dir)) != NULL)
|
|
if (get_dtype(e, &count_path, 0) == DT_DIR &&
|
|
strlen(e->d_name) == 2 &&
|
|
!hex_to_bytes(&c, e->d_name, 1)) {
|
|
strbuf_setlen(&count_path, base_path_len);
|
|
strbuf_addf(&count_path, "%s/", e->d_name);
|
|
total += (count = count_files(&count_path));
|
|
strbuf_addf(buf, "%s : %7d files\n", e->d_name, count);
|
|
}
|
|
|
|
strbuf_addf(buf, "Total: %d loose objects", total);
|
|
|
|
strbuf_release(&count_path);
|
|
closedir(dir);
|
|
}
|
|
|
|
static int add_directory_to_archiver(struct strvec *archiver_args,
|
|
const char *path, int recurse)
|
|
{
|
|
int at_root = !*path;
|
|
DIR *dir;
|
|
struct dirent *e;
|
|
struct strbuf buf = STRBUF_INIT;
|
|
size_t len;
|
|
int res = 0;
|
|
|
|
dir = opendir(at_root ? "." : path);
|
|
if (!dir) {
|
|
if (errno == ENOENT) {
|
|
warning(_("could not archive missing directory '%s'"), path);
|
|
return 0;
|
|
}
|
|
return error_errno(_("could not open directory '%s'"), path);
|
|
}
|
|
|
|
if (!at_root)
|
|
strbuf_addf(&buf, "%s/", path);
|
|
len = buf.len;
|
|
strvec_pushf(archiver_args, "--prefix=%s", buf.buf);
|
|
|
|
while (!res && (e = readdir_skip_dot_and_dotdot(dir))) {
|
|
struct strbuf abspath = STRBUF_INIT;
|
|
unsigned char dtype;
|
|
|
|
strbuf_add_absolute_path(&abspath, at_root ? "." : path);
|
|
strbuf_addch(&abspath, '/');
|
|
dtype = get_dtype(e, &abspath, 0);
|
|
|
|
strbuf_setlen(&buf, len);
|
|
strbuf_addstr(&buf, e->d_name);
|
|
|
|
if (dtype == DT_REG)
|
|
strvec_pushf(archiver_args, "--add-file=%s", buf.buf);
|
|
else if (dtype != DT_DIR)
|
|
warning(_("skipping '%s', which is neither file nor "
|
|
"directory"), buf.buf);
|
|
else if (recurse &&
|
|
add_directory_to_archiver(archiver_args,
|
|
buf.buf, recurse) < 0)
|
|
res = -1;
|
|
|
|
strbuf_release(&abspath);
|
|
}
|
|
|
|
closedir(dir);
|
|
strbuf_release(&buf);
|
|
return res;
|
|
}
|
|
|
|
int create_diagnostics_archive(struct repository *r,
|
|
struct strbuf *zip_path,
|
|
enum diagnose_mode mode)
|
|
{
|
|
struct strvec archiver_args = STRVEC_INIT;
|
|
char **argv_copy = NULL;
|
|
int stdout_fd = -1, archiver_fd = -1;
|
|
struct strbuf buf = STRBUF_INIT;
|
|
int res;
|
|
struct archive_dir archive_dirs[] = {
|
|
{ ".git", 0 },
|
|
{ ".git/hooks", 0 },
|
|
{ ".git/info", 0 },
|
|
{ ".git/logs", 1 },
|
|
{ ".git/objects/info", 0 }
|
|
};
|
|
|
|
if (mode == DIAGNOSE_NONE) {
|
|
res = 0;
|
|
goto diagnose_cleanup;
|
|
}
|
|
|
|
stdout_fd = dup(STDOUT_FILENO);
|
|
if (stdout_fd < 0) {
|
|
res = error_errno(_("could not duplicate stdout"));
|
|
goto diagnose_cleanup;
|
|
}
|
|
|
|
archiver_fd = xopen(zip_path->buf, O_CREAT | O_WRONLY | O_TRUNC, 0666);
|
|
if (dup2(archiver_fd, STDOUT_FILENO) < 0) {
|
|
res = error_errno(_("could not redirect output"));
|
|
goto diagnose_cleanup;
|
|
}
|
|
|
|
init_zip_archiver();
|
|
strvec_pushl(&archiver_args, "git-diagnose", "--format=zip", NULL);
|
|
|
|
strbuf_reset(&buf);
|
|
strbuf_addstr(&buf, "Collecting diagnostic info\n\n");
|
|
get_version_info(&buf, 1);
|
|
|
|
strbuf_addf(&buf, "Repository root: %s\n", r->worktree);
|
|
get_disk_info(&buf);
|
|
write_or_die(stdout_fd, buf.buf, buf.len);
|
|
strvec_pushf(&archiver_args,
|
|
"--add-virtual-file=diagnostics.log:%.*s",
|
|
(int)buf.len, buf.buf);
|
|
|
|
strbuf_reset(&buf);
|
|
strbuf_addstr(&buf, "--add-virtual-file=packs-local.txt:");
|
|
dir_file_stats(odb_source_files_downcast(r->objects->source)->dirs, &buf);
|
|
odb_for_each_alternate(r->objects, dir_file_stats, &buf);
|
|
strvec_push(&archiver_args, buf.buf);
|
|
|
|
strbuf_reset(&buf);
|
|
strbuf_addstr(&buf, "--add-virtual-file=objects-local.txt:");
|
|
loose_objs_stats(&buf, ".git/objects");
|
|
strvec_push(&archiver_args, buf.buf);
|
|
|
|
/* Only include this if explicitly requested */
|
|
if (mode == DIAGNOSE_ALL) {
|
|
for (size_t i = 0; i < ARRAY_SIZE(archive_dirs); i++) {
|
|
if (add_directory_to_archiver(&archiver_args,
|
|
archive_dirs[i].path,
|
|
archive_dirs[i].recursive)) {
|
|
res = error_errno(_("could not add directory '%s' to archiver"),
|
|
archive_dirs[i].path);
|
|
goto diagnose_cleanup;
|
|
}
|
|
}
|
|
}
|
|
|
|
strvec_pushl(&archiver_args, "--prefix=",
|
|
oid_to_hex(r->hash_algo->empty_tree), "--", NULL);
|
|
|
|
/* `write_archive()` modifies the `argv` passed to it. Let it. */
|
|
argv_copy = xmemdupz(archiver_args.v,
|
|
sizeof(char *) * archiver_args.nr);
|
|
res = write_archive(archiver_args.nr, (const char **)argv_copy, NULL,
|
|
r, NULL, 0);
|
|
if (res) {
|
|
error(_("failed to write archive"));
|
|
goto diagnose_cleanup;
|
|
}
|
|
|
|
fprintf(stderr, "\n"
|
|
"Diagnostics complete.\n"
|
|
"All of the gathered info is captured in '%s'\n",
|
|
zip_path->buf);
|
|
|
|
diagnose_cleanup:
|
|
if (archiver_fd >= 0) {
|
|
dup2(stdout_fd, STDOUT_FILENO);
|
|
close(stdout_fd);
|
|
close(archiver_fd);
|
|
}
|
|
free(argv_copy);
|
|
strvec_clear(&archiver_args);
|
|
strbuf_release(&buf);
|
|
|
|
return res;
|
|
}
|