Skip to content

Commit efb5dba

Browse files
jltoblergitster
authored andcommitted
object-file: avoid fd seekback by checking object size upfront
In certain scenarios, Git handles writing blobs that exceed "core.bigFileThreshold" differently by streaming the object directly into a packfile. When there is an active ODB transaction, these blobs are streamed to the same packfile instead of using a separate packfile for each. If "pack.packSizeLimit" is configured and streaming another object causes the packfile to exceed the configured limit, the packfile is truncated back to the previous object and the object write is restarted in a new packfile. This works fine, but requires the fd being read from to save a checkpoint so it becomes possible to rewind the input source via seeking back to a known offset at the beginning. In a subsequent commit, blob streaming is converted to use `struct odb_write_stream` as a more generic input source instead of an fd which doesn't provide a mechanism for rewinding. For this use case though, rewinding the fd is not strictly necessary because the inflated size of the object is known and can be used to approximate whether writing the object would cause the packfile to exceed the configured limit prior to writing anything. These blobs written to the packfile are never deltified thus the size difference between what is written versus the inflated size is due to zlib compression. While this does prevent packfiles from being filled to the potential maximum is some cases, it should be good enough and still prevents the packfile from exceeding any configured limit. Use the inflated blob size to determine whether writing an object to a packfile will exceed the configured "pack.packSizeLimit". Signed-off-by: Justin Tobler <jltobler@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent c39bf30 commit efb5dba

1 file changed

Lines changed: 25 additions & 61 deletions

File tree

object-file.c

Lines changed: 25 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1445,29 +1445,17 @@ static int hash_blob_stream(struct odb_write_stream *stream,
14451445

14461446
/*
14471447
* Read the contents from fd for size bytes, streaming it to the
1448-
* packfile in state while updating the hash in ctx. Signal a failure
1449-
* by returning a negative value when the resulting pack would exceed
1450-
* the pack size limit and this is not the first object in the pack,
1451-
* so that the caller can discard what we wrote from the current pack
1452-
* by truncating it and opening a new one. The caller will then call
1453-
* us again after rewinding the input fd.
1454-
*
1455-
* The already_hashed_to pointer is kept untouched by the caller to
1456-
* make sure we do not hash the same byte when we are called
1457-
* again. This way, the caller does not have to checkpoint its hash
1458-
* status before calling us just in case we ask it to call us again
1459-
* with a new pack.
1448+
* packfile in state while updating the hash in ctx.
14601449
*/
1461-
static int stream_blob_to_pack(struct transaction_packfile *state,
1462-
struct git_hash_ctx *ctx, off_t *already_hashed_to,
1463-
int fd, size_t size, const char *path)
1450+
static void stream_blob_to_pack(struct transaction_packfile *state,
1451+
struct git_hash_ctx *ctx, int fd, size_t size,
1452+
const char *path)
14641453
{
14651454
git_zstream s;
14661455
unsigned char ibuf[16384];
14671456
unsigned char obuf[16384];
14681457
unsigned hdrlen;
14691458
int status = Z_OK;
1470-
off_t offset = 0;
14711459

14721460
git_deflate_init(&s, pack_compression_level);
14731461

@@ -1484,15 +1472,9 @@ static int stream_blob_to_pack(struct transaction_packfile *state,
14841472
if ((size_t)read_result != rsize)
14851473
die("failed to read %u bytes from '%s'",
14861474
(unsigned)rsize, path);
1487-
offset += rsize;
1488-
if (*already_hashed_to < offset) {
1489-
size_t hsize = offset - *already_hashed_to;
1490-
if (rsize < hsize)
1491-
hsize = rsize;
1492-
if (hsize)
1493-
git_hash_update(ctx, ibuf, hsize);
1494-
*already_hashed_to = offset;
1495-
}
1475+
1476+
git_hash_update(ctx, ibuf, rsize);
1477+
14961478
s.next_in = ibuf;
14971479
s.avail_in = rsize;
14981480
size -= rsize;
@@ -1503,14 +1485,6 @@ static int stream_blob_to_pack(struct transaction_packfile *state,
15031485
if (!s.avail_out || status == Z_STREAM_END) {
15041486
size_t written = s.next_out - obuf;
15051487

1506-
/* would we bust the size limit? */
1507-
if (state->nr_written &&
1508-
pack_size_limit_cfg &&
1509-
pack_size_limit_cfg < state->offset + written) {
1510-
git_deflate_abort(&s);
1511-
return -1;
1512-
}
1513-
15141488
hashwrite(state->f, obuf, written);
15151489
state->offset += written;
15161490
s.next_out = obuf;
@@ -1527,7 +1501,6 @@ static int stream_blob_to_pack(struct transaction_packfile *state,
15271501
}
15281502
}
15291503
git_deflate_end(&s);
1530-
return 0;
15311504
}
15321505

15331506
static void flush_packfile_transaction(struct odb_transaction_files *transaction)
@@ -1603,48 +1576,39 @@ static int index_blob_packfile_transaction(struct odb_transaction_files *transac
16031576
size_t size, const char *path)
16041577
{
16051578
struct transaction_packfile *state = &transaction->packfile;
1606-
off_t seekback, already_hashed_to;
16071579
struct git_hash_ctx ctx;
16081580
unsigned char obuf[16384];
16091581
unsigned header_len;
16101582
struct hashfile_checkpoint checkpoint;
16111583
struct pack_idx_entry *idx;
16121584

1613-
seekback = lseek(fd, 0, SEEK_CUR);
1614-
if (seekback == (off_t)-1)
1615-
return error("cannot find the current offset");
1616-
16171585
header_len = format_object_header((char *)obuf, sizeof(obuf),
16181586
OBJ_BLOB, size);
16191587
transaction->base.source->odb->repo->hash_algo->init_fn(&ctx);
16201588
git_hash_update(&ctx, obuf, header_len);
16211589

1590+
/*
1591+
* If writing another object to the packfile could result in it
1592+
* exceeding the configured size limit, flush the current packfile
1593+
* transaction.
1594+
*
1595+
* Note that this uses the inflated object size as an approximation.
1596+
* Blob objects written in this manner are not delta-compressed, so
1597+
* the difference between the inflated and on-disk size is limited
1598+
* to zlib compression and is sufficient for this check.
1599+
*/
1600+
if (state->nr_written && pack_size_limit_cfg &&
1601+
pack_size_limit_cfg < state->offset + size)
1602+
flush_packfile_transaction(transaction);
1603+
16221604
CALLOC_ARRAY(idx, 1);
16231605
prepare_packfile_transaction(transaction);
16241606
hashfile_checkpoint_init(state->f, &checkpoint);
16251607

1626-
already_hashed_to = 0;
1627-
1628-
while (1) {
1629-
prepare_packfile_transaction(transaction);
1630-
hashfile_checkpoint(state->f, &checkpoint);
1631-
idx->offset = state->offset;
1632-
crc32_begin(state->f);
1633-
1634-
if (!stream_blob_to_pack(state, &ctx, &already_hashed_to,
1635-
fd, size, path))
1636-
break;
1637-
/*
1638-
* Writing this object to the current pack will make
1639-
* it too big; we need to truncate it, start a new
1640-
* pack, and write into it.
1641-
*/
1642-
hashfile_truncate(state->f, &checkpoint);
1643-
state->offset = checkpoint.offset;
1644-
flush_packfile_transaction(transaction);
1645-
if (lseek(fd, seekback, SEEK_SET) == (off_t)-1)
1646-
return error("cannot seek back");
1647-
}
1608+
hashfile_checkpoint(state->f, &checkpoint);
1609+
idx->offset = state->offset;
1610+
crc32_begin(state->f);
1611+
stream_blob_to_pack(state, &ctx, fd, size, path);
16481612
git_hash_final_oid(result_oid, &ctx);
16491613

16501614
idx->crc32 = crc32_end(state->f);

0 commit comments

Comments
 (0)