Debian Codegraph @

cmd/experimental/mirror/internal/mirror.go

raw
1 // Copyright 2025 The Tessera authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 // Package mirror provides support for the infrastructure-specific mirror tools.
16 package mirror
17
18 import (
19 "context"
20 "errors"
21 "fmt"
22 "iter"
23 "log"
24 "os"
25 "sync/atomic"
26
27 "github.com/avast/retry-go/v4"
28 "github.com/transparency-dev/tessera/api/layout"
29 "github.com/transparency-dev/tessera/internal/parse"
30 "golang.org/x/sync/errgroup"
31 )
32
33 // Target describes a type which can store log static resources.
34 type Target interface {
35 ReadCheckpoint(ctx context.Context) ([]byte, error)
36 WriteCheckpoint(ctx context.Context, data []byte) error
37 WriteTile(ctx context.Context, l, i uint64, p uint8, data []byte) error
38 WriteEntryBundle(ctx context.Context, i uint64, p uint8, data []byte) error
39 }
40
41 // Source describes a type which can fetch static resources from a source log, like
42 // the .*Fetcher implementations in the client package.
43 type Source interface {
44 ReadCheckpoint(ctx context.Context) ([]byte, error)
45 ReadTile(ctx context.Context, l, i uint64, p uint8) ([]byte, error)
46 ReadEntryBundle(_ context.Context, i uint64, p uint8) ([]byte, error)
47 }
48
49 // Mirror is a struct which knows how to use the Src and Store functions to copy a tlog-tiles compliant
50 // log from one location to another.
51 //
52 // The checkpoint will only be stored once all static resources have been successfully copied.
53 // Errors fetching or storing operations will cause the operation to be retried a few times before eventually giving up.
54 //
55 // Note that this function _only copies the data_; no self-consistency or correctness checking of
56 // the copied tiles/entries/checkpoint is undertaken.
57 type Mirror struct {
58 NumWorkers uint
59 Source Source
60 Target Target
61
62 totalResources uint64
63 resourcesFetched atomic.Uint64
64 }
65
66 // Run performs the copy operation.
67 //
68 // This is a long-lived operation, returning only once ctx becomes Done, the copy is completed,
69 // or an error occurs during the operation.
70 func (m *Mirror) Run(ctx context.Context) error {
71 sourceCP, sourceSize, err := fetchAndParseCP(ctx, m.Source.ReadCheckpoint)
72 if err != nil {
73 return fmt.Errorf("failed to fetch source checkpoint size: %v", err)
74 }
75 _, targetSize, err := fetchAndParseCP(ctx, m.Target.ReadCheckpoint)
76 if err != nil && !errors.Is(err, os.ErrNotExist) {
77 return fmt.Errorf("failed to read checkpoint in target: %v", err)
78 }
79
80 delta := sourceSize - targetSize
81 stride := delta / uint64(m.NumWorkers)
82 if r := stride % layout.TileWidth; r != 0 {
83 stride += (layout.TileWidth - r)
84 }
85
86 log.Printf("Source log size: %d, target log size: %d, ∆ %d, stride: %d", sourceSize, targetSize, delta, stride)
87
88 if delta == 0 {
89 return nil
90 }
91
92 m.totalResources = calcNumResources(sourceSize, targetSize, stride)
93 m.resourcesFetched = atomic.Uint64{}
94
95 work := make(chan job, m.NumWorkers)
96
97 go func() {
98 defer close(work)
99
100 for j := range jobs(sourceSize, targetSize, stride) {
101 select {
102 case <-ctx.Done():
103 return
104 case work <- j:
105 log.Printf("Job: %s", j)
106 }
107 }
108 log.Println("No more work")
109 }()
110
111 g := errgroup.Group{}
112 for i := range m.NumWorkers {
113 g.Go(func() error {
114 for j := range work {
115 log.Printf("Worker %d: working on %s", i, j)
116 for ri := range layout.Range(j.from, j.N, sourceSize>>(j.level*layout.TileHeight)) {
117 if err := retry.Do(m.copyTile(ctx, j.level, ri.Index, ri.Partial)); err != nil {
118 log.Println(err.Error())
119 return err
120 }
121
122 if j.level == 0 {
123 if err := retry.Do(m.copyBundle(ctx, ri.Index, ri.Partial)); err != nil {
124 log.Println(err.Error())
125 return err
126 }
127 }
128 }
129 }
130 return nil
131 })
132 }
133 if err := g.Wait(); err != nil {
134 return fmt.Errorf("failed to migrate static resources: %v", err)
135 }
136 return m.Target.WriteCheckpoint(ctx, sourceCP)
137 }
138
139 // Progress returns the total number of resources present in the source log, and the number of resources
140 // successfully copied to the destination so far.
141 func (m *Mirror) Progress() (uint64, uint64) {
142 return m.totalResources, m.resourcesFetched.Load()
143 }
144
145 // copyTile reads a tile from the source log and stores it into the same location in the destination log.
146 func (m *Mirror) copyTile(ctx context.Context, l, i uint64, p uint8) func() error {
147 return func() error {
148 d, err := m.Source.ReadTile(ctx, l, i, p)
149 if err != nil {
150 log.Println(err.Error())
151 return err
152 }
153 if err := m.Target.WriteTile(ctx, l, i, p, d); err != nil {
154 return err
155 }
156 m.resourcesFetched.Add(1)
157 return nil
158 }
159 }
160
161 // copyBundle reads an entry bundle from the source log and stores it into the same location in the destination log.
162 func (m *Mirror) copyBundle(ctx context.Context, i uint64, p uint8) func() error {
163 return func() error {
164 d, err := m.Source.ReadEntryBundle(ctx, i, p)
165 if err != nil {
166 log.Println(err.Error())
167 return err
168 }
169 if err := m.Target.WriteEntryBundle(ctx, i, p, d); err != nil {
170 return err
171 }
172 m.resourcesFetched.Add(1)
173 return nil
174 }
175 }
176
177 type job struct {
178 level, from, N uint64
179 }
180
181 func (j job) String() string {
182 return fmt.Sprintf("Level: %d, Range: [%d, %d)", j.level, j.from, j.from+j.N)
183 }
184
185 func jobs(srcSize, targetSize, stride uint64) iter.Seq[job] {
186 return func(yield func(job) bool) {
187 for start, ext, l := targetSize, srcSize, uint64(0); ext > 0; start, ext, l = start>>layout.TileHeight, uint64(ext>>layout.TileHeight), l+1 {
188 for from := start; from < ext; {
189 N := stride
190 // If we're starting from a partial tile, then just fetch the remainder first so we're tile-aligned from then on.
191 if r := from % layout.TileWidth; r != 0 {
192 N = layout.TileWidth - r
193 }
194 N = min(N, ext-from)
195 if !yield(job{level: l, from: from, N: N}) {
196 return
197 }
198 from = from + N
199 }
200 }
201
202 }
203 }
204
205 // calcNumResources calculates the number of new static resources which need to be mirrored, given the
206 // size of the source and target.
207 func calcNumResources(srcSize, targetSize, stride uint64) uint64 {
208 leafBundles := uint64(0)
209 tiles := uint64(0)
210
211 for j := range jobs(srcSize, targetSize, stride) {
212 nTiles := (j.N + layout.TileWidth - 1) / layout.TileWidth
213 tiles += nTiles
214 if j.level == 0 {
215 leafBundles += nTiles
216 }
217 }
218
219 return leafBundles + tiles
220 }
221
222 func fetchAndParseCP(ctx context.Context, f func(context.Context) ([]byte, error)) ([]byte, uint64, error) {
223 cp, err := f(ctx)
224 if err != nil {
225 return nil, 0, err
226 }
227 _, size, _, err := parse.CheckpointUnsafe(cp)
228 return cp, size, err
229 }