Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion examples/git/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ credentials for its disposable MinIO instance.

Each Git publication includes the reachable catalog and its separately stored
objects. The default WAL bound therefore limits total repository state, not
just one push. Compressed objects larger than the 512-byte inline threshold,
just one push. Compressed objects larger than the 2 KiB inline threshold,
retained deltas, and catalog nodes use WAL objects; a repository with around
100,000 such objects may reach the bound sooner because the root and shared
paths also count. That is a sizing estimate, not a tested repository capacity. Set
Expand All @@ -143,6 +143,14 @@ Spin/MinIO run, simultaneous 64 MiB delta pushes to one, two, and four separate
repositories peaked at about 578 MiB, 928 MiB, and 1.59 GiB of process memory.
These are observations, not an upper bound. Size the host for its admitted push
concurrency, with headroom for other requests.
Pending full inline payload uses at most the smaller of `git_max_catalog_bytes`
and 512 bytes times `git_max_pack_objects`; each active compression sink keeps
at most another 2 KiB of payload before spilling. Retained delta data has a separate
`git_max_catalog_bytes` allowance, so these two pending payload pools total at
most 128 MiB under the defaults. This excludes slice capacity, maps, allocator
overhead, and importer and storage buffers; it is not a process-memory ceiling.
New catalog leaves target 128 items. Inline objects and deltas share a fixed
512 KiB payload allowance per leaf; readers still accept up to 1,024 items.
When upgrading an existing repository containing larger objects, retain a
configured limit high enough to read them; lowering it blocks those objects.

Expand Down
4 changes: 2 additions & 2 deletions examples/git/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,12 @@ func validObjectMeta(item objectMeta, format config.ObjectFormat, prefix string)
item.Kind == plumbing.CommitObject || item.Kind == plumbing.TagObject
validStorage := item.validInline() || len(item.Inline) == 0 && item.Encoding == "zlib" && item.StoredSize > 0
validDelta := item.Delta == nil || item.Delta.valid() && validID(format, item.Delta.Base) &&
item.Delta.Base != item.ID && len(item.Inline) == 0
item.Delta.Base != item.ID && (len(item.Inline) == 0 || item.Delta.StoredSize == 0)
return validID(format, item.ID) && strings.HasPrefix(item.ID, prefix) && validKind && item.Size >= 0 && validStorage && validDelta
}

// Keep full catalog leaves below the WAL node limit, including base64 encoding.
const inlineObjectLimit = 512
const inlineObjectLimit = 2 << 10

type objectMeta struct {
ID string
Expand Down
12 changes: 11 additions & 1 deletion examples/git/codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ func TestInlineMetadataBound(t *testing.T) {
}
meta.Inline = meta.Inline[:inlineObjectLimit]
meta.StoredSize--
leaf := struct{ Items []objectMeta }{Items: make([]objectMeta, indexLeafSize)}
leaf := struct{ Items []objectMeta }{Items: make([]objectMeta, indexLeafTarget)}
for i := range leaf.Items {
leaf.Items[i] = meta
}
Expand Down Expand Up @@ -327,6 +327,16 @@ func TestCatalogMetadataAdmission(t *testing.T) {
if !validObjectMeta(item, format, "ab") {
t.Fatal("rejected valid object metadata")
}
inline := item
inline.Inline = []byte{1}
inline.Delta = &deltaMeta{Base: "ab" + strings.Repeat("2", format.HexSize()-2), Size: 2, Data: []byte{1}}
if !validObjectMeta(inline, format, "ab") {
t.Fatal("inline object with inline delta rejected")
}
inline.Delta = &deltaMeta{Base: inline.Delta.Base, Size: 2, StoredSize: inlineDeltaLimit + 1}
if validObjectMeta(inline, format, "ab") {
t.Fatal("inline object with external delta accepted")
}
for name, mutate := range map[string]func(*objectMeta){
"path": func(m *objectMeta) { m.ID = "ac" + m.ID[2:] },
"hash": func(m *objectMeta) { m.ID = strings.ToUpper(m.ID) },
Expand Down
10 changes: 8 additions & 2 deletions examples/git/delta.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,16 @@ func (p packOffsets) deltas(source io.ReadSeeker, size int64, retain func(plumbi
return nil
}

const inlineLeafBytes = 512 << 10

// Inline deltas share the leaf's allowance. External deltas live below the
// object's root and do not enlarge unrelated catalog reads.
func limitDeltas(items []objectMeta) {
remaining := indexLeafSize * inlineObjectLimit
func limitDeltas(items []objectMeta) error {
remaining := inlineLeafBytes
for _, item := range items {
if len(item.Inline) > remaining {
return fmt.Errorf("inline objects exceed catalog leaf allowance")
}
remaining -= len(item.Inline)
}
for i := range items {
Expand All @@ -176,4 +181,5 @@ func limitDeltas(items []objectMeta) {
}
}
}
return nil
}
40 changes: 36 additions & 4 deletions examples/git/delta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,23 +133,24 @@ func TestDeltaCatalogBound(t *testing.T) {
for _, mixInline := range []bool{false, true} {
t.Run(fmt.Sprintf("bytes=%d/mixed=%t", size, mixInline), func(t *testing.T) {
meta := objectMeta{ID: strings.Repeat("f", 64), Kind: plumbing.BlobObject, Size: math.MaxInt64, Encoding: "zlib", StoredSize: math.MaxInt64, Delta: &deltaMeta{Base: strings.Repeat("f", 64), Size: math.MaxInt64, Data: make([]byte, size)}}
leaf := struct{ Items []objectMeta }{Items: make([]objectMeta, indexLeafSize)}
leaf := struct{ Items []objectMeta }{Items: make([]objectMeta, indexLeafTarget)}
for i := range leaf.Items {
leaf.Items[i] = meta
if mixInline && i%2 == 0 {
leaf.Items[i].Delta = nil
leaf.Items[i].Inline = make([]byte, inlineObjectLimit)
}
}
limitDeltas(leaf.Items)
if err := limitDeltas(leaf.Items); err != nil {
t.Fatal(err)
}
total := 0
for _, item := range leaf.Items {
total += len(item.Inline)
if item.Delta != nil {
total += len(item.Delta.Data)
}
}
if total > indexLeafSize*inlineObjectLimit {
if total > inlineLeafBytes {
t.Fatal("exceeded shared inline allowance")
}
data, err := json.Marshal(leaf)
Expand All @@ -164,6 +165,37 @@ func TestDeltaCatalogBound(t *testing.T) {
}
}

func TestLegalLeafPayloadBound(t *testing.T) {
items := make([]objectMeta, indexLeafSize)
for i := range items {
items[i] = objectMeta{ID: strings.Repeat("f", 64), Kind: plumbing.BlobObject, Size: math.MaxInt64, Encoding: "zlib", StoredSize: 512, Inline: make([]byte, 512)}
}
if err := limitDeltas(items); err != nil {
t.Fatal("legal full leaf rejected", err)
}
data, err := json.Marshal(struct{ Items []objectMeta }{items})
if err != nil || len(data) >= 1<<20 {
t.Fatalf("legal full leaf page: bytes=%d err=%v", len(data), err)
}
items[0].Inline = append(items[0].Inline, 0)
items[0].StoredSize++
if err := limitDeltas(items); err == nil {
t.Fatal("over-pool full objects accepted")
}
for i := range items {
items[i].Inline = nil
items[i].StoredSize = math.MaxInt64
items[i].Delta = &deltaMeta{Base: strings.Repeat("e", 64), Size: math.MaxInt64, Data: make([]byte, inlineLeafBytes/indexLeafSize)}
}
if err := limitDeltas(items); err != nil {
t.Fatal("legal external-object leaf rejected", err)
}
data, err = json.Marshal(struct{ Items []objectMeta }{items})
if err != nil || len(data) >= 1<<20 {
t.Fatalf("legal external-object/delta page: bytes=%d refs=%d err=%v", len(data), len(items), err)
}
}

func TestLargeDeltaRemainsAFullObject(t *testing.T) {
f := format.SHA256
target := make([]byte, retainedDeltaLimit+1)
Expand Down
5 changes: 4 additions & 1 deletion examples/git/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import (
// one hex digit at a time, so they have at most sixteen children.
const indexLeafSize = 1024

// Pack new leaves more tightly without changing the legal reader limit.
const indexLeafTarget = 128

type radixNode[V, H any] struct {
Items map[string]V
Children map[string]H
Expand All @@ -35,7 +38,7 @@ func updateRadix[V, H any](prefix string, node radixNode[V, H], updates map[stri
items := make(map[string]V, len(node.Items)+len(updates))
maps.Copy(items, node.Items)
maps.Copy(items, updates)
if len(items) <= indexLeafSize {
if len(items) <= indexLeafTarget {
return save(radixNode[V, H]{Items: items})
}
updates = items
Expand Down
4 changes: 3 additions & 1 deletion examples/git/index_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ func (s *store) saveBucket(node radixNode[indexed, *wal.Object]) (*wal.Object, e
}
}
}
limitDeltas(meta.Items)
if err := limitDeltas(meta.Items); err != nil {
return nil, err
}
data, err := json.Marshal(meta)
if err != nil {
return nil, err
Expand Down
42 changes: 37 additions & 5 deletions examples/git/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestSamePrefixSplitsAndPreservesOldRoot(t *testing.T) {
db := testIndex{nodes: map[int]radixNode[int, int]{}}
values := map[string]int{}
key := func(i int) string { return "aa" + fmt.Sprintf("%0*x", width-2, i) }
for i := 0; i < indexLeafSize; i++ {
for i := 0; i < indexLeafTarget; i++ {
values[key(i)] = i
}
original, err := updateRadix("aa", radixNode[int, int]{}, values, db.load, db.save)
Expand All @@ -43,15 +43,15 @@ func TestSamePrefixSplitsAndPreservesOldRoot(t *testing.T) {
t.Fatal("unnecessary branch before leaf limit")
}
updates := map[string]int{}
for i := indexLeafSize; i < 2300; i++ {
for i := indexLeafTarget; i < 2300; i++ {
updates[key(i)] = i
}
root, err := updateRadix("aa", db.nodes[original], updates, db.load, db.save)
if err != nil {
t.Fatal(err)
}
for _, node := range db.nodes {
if len(node.Items) > indexLeafSize || len(node.Children) > 16 {
if len(node.Items) > indexLeafTarget || len(node.Children) > 16 {
t.Fatal("unbounded node")
}
}
Expand All @@ -61,7 +61,7 @@ func TestSamePrefixSplitsAndPreservesOldRoot(t *testing.T) {
t.Fatalf("lookup %d: %d %v %v", i, got, ok, err)
}
}
_, found, err := lookupRadix(key(indexLeafSize), "aa", original, db.load)
_, found, err := lookupRadix(key(indexLeafTarget), "aa", original, db.load)
if found || err != nil {
t.Fatal("old root changed")
}
Expand All @@ -84,6 +84,38 @@ func TestSamePrefixSplitsAndPreservesOldRoot(t *testing.T) {
}
}

func TestLegalLargeLeafCanBeReadUpdatedAndPruned(t *testing.T) {
db := testIndex{nodes: map[int]radixNode[int, int]{}}
items := map[string]int{}
for i := range indexLeafSize {
items[fmt.Sprintf("aa%038x", i)] = i
}
root, _ := db.save(radixNode[int, int]{Items: items})
key := fmt.Sprintf("aa%038x", indexLeafSize-1)
if got, found, err := lookupRadix(key, "aa", root, db.load); err != nil || !found || got != indexLeafSize-1 {
t.Fatalf("legal reader leaf: %d %v %v", got, found, err)
}
updated, err := updateRadix("aa", db.nodes[root], map[string]int{key: -1}, db.load, db.save)
if err != nil {
t.Fatal(err)
}
for id, node := range db.nodes {
if id != root && len(node.Items) > indexLeafTarget {
t.Fatal("updated leaf exceeds packing target")
}
}
if got, _, err := lookupRadix(key, "aa", updated, db.load); err != nil || got != -1 {
t.Fatal("updated object lost", err)
}
pruned, present, err := filterRadix("aa", root, func(id string) bool { return id != key }, db.load, db.save)
if err != nil || !present || len(db.nodes[pruned].Items) != indexLeafSize-1 {
t.Fatalf("legal leaf prune: %v %v", present, err)
}
if got, found, err := lookupRadix(key, "aa", root, db.load); err != nil || !found || got != indexLeafSize-1 || len(db.nodes[root].Items) != indexLeafSize {
t.Fatal("original large leaf changed")
}
}

func TestUpdateDoesNotReadUnchangedSibling(t *testing.T) {
db := testIndex{nodes: map[int]radixNode[int, int]{}}
values := map[string]int{}
Expand Down Expand Up @@ -147,7 +179,7 @@ func TestPruneCatalogKeepsOnlyReachableObjectsAndPreservesOldRoot(t *testing.T)
db := testIndex{nodes: map[int]radixNode[int, int]{}}
items := map[string]int{}
keep := func(id string) bool { return id[:3] == "aa0" && id != "aa0"+fmt.Sprintf("%037x", 0) }
for i := 0; i < 700; i++ {
for i := 0; i < indexLeafTarget; i++ {
for _, prefix := range []string{"aa0", "aaf"} {
items[prefix+fmt.Sprintf("%037x", i)] = i
}
Expand Down
10 changes: 10 additions & 0 deletions examples/git/limits.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,13 @@ func (l requestLimits) chargeCatalog(size int) error {
*l.catalogRead += int64(size)
return nil
}

// Pending full objects keep at most the former 512 bytes per pack object,
// capped by the catalog budget. Replacing an entry releases its inline bytes.
func (l requestLimits) inlineRemaining(used int64, replaced int) int64 {
limit := l.catalogBytes
if l.packObjects <= limit/512 {
limit = l.packObjects * 512
}
return limit - (used - int64(replaced))
}
27 changes: 27 additions & 0 deletions examples/git/limits_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"io"
"math"
"net/http"
"net/http/httptest"
"strings"
Expand All @@ -29,6 +30,32 @@ func TestObjectAndMetadataLimits(t *testing.T) {
}
}

func TestPendingInlineBudget(t *testing.T) {
for _, test := range []struct {
name string
catalog, objects, used int64
replaced int
want int64
}{
{"catalog cap", 1000, 3, 0, 0, 1000},
{"pack cap", 2000, 3, 0, 0, 1536},
{"catalog remainder", 1537, 4, 0, 0, 1537},
{"exactly full", 2048, 4, 2048, 0, 0},
{"replace inline", 2048, 4, 2048, 1024, 1024},
{"replace external", 2048, 4, 1024, 0, 1024},
{"large pack count", math.MaxInt64, math.MaxInt64, 0, 0, math.MaxInt64},
{"large pack product", math.MaxInt64, math.MaxInt64 / 512, 0, 0, math.MaxInt64 / 512 * 512},
{"large full replacement", math.MaxInt64, math.MaxInt64, math.MaxInt64, 2048, 2048},
} {
t.Run(test.name, func(t *testing.T) {
limits := requestLimits{catalogBytes: test.catalog, packObjects: test.objects}
if got := limits.inlineRemaining(test.used, test.replaced); got != test.want {
t.Fatalf("available=%d want=%d", got, test.want)
}
})
}
}

func TestRequestLimits(t *testing.T) {
defaults, err := loadLimits(func(string) string { return "" })
if err != nil || defaults.pushBytes != 2<<30 || defaults.objectBytes != 64<<20 || defaults.metadataBytes != 16<<20 || defaults.packObjects != 1_000_000 || defaults.catalogBytes != 64<<20 || defaults.collectionObjects != 100_000 {
Expand Down
2 changes: 1 addition & 1 deletion examples/git/receive.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (p *incomingPack) Close() (err error) {
if err := offsets.deltas(reader, reader.size, func(id plumbing.Hash, delta *deltaMeta) error {
key := id.String()
item := p.s.pending[key]
if len(item.Inline) != 0 || item.Delta != nil || int64(len(delta.Data)) >= item.StoredSize {
if item.Delta != nil || int64(len(delta.Data)) >= item.StoredSize {
return nil
}
cost := int64(len(delta.Data) + len(delta.Base) + 64)
Expand Down
Loading
Loading