File: migrate.go

package info (click to toggle)
golang-github-transparency-dev-tessera 1.0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 3,612 kB
  • sloc: sql: 33; sh: 17; makefile: 12
file content (131 lines) | stat: -rw-r--r-- 4,150 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Copyright 2024 The Tessera authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package tessera

import (
	"context"
	"fmt"
	"sync/atomic"

	"github.com/cenkalti/backoff/v5"
	"github.com/transparency-dev/tessera/api/layout"
	"github.com/transparency-dev/tessera/client"
	"golang.org/x/sync/errgroup"
	"k8s.io/klog/v2"
)

type setEntryBundleFunc func(ctx context.Context, index uint64, partial uint8, bundle []byte) error

func newCopier(numWorkers uint, setEntryBundle setEntryBundleFunc, getEntryBundle client.EntryBundleFetcherFunc) *copier {
	return &copier{
		setEntryBundle: setEntryBundle,
		getEntryBundle: getEntryBundle,
		todo:           make(chan bundle, numWorkers),
	}
}

// copier controls the migration work.
type copier struct {
	setEntryBundle setEntryBundleFunc
	getEntryBundle client.EntryBundleFetcherFunc

	// todo contains work items to be completed.
	todo chan bundle

	// bundlesCopied is the number of entry bundles copied so far.
	bundlesCopied atomic.Uint64
}

// bundle represents the address of an individual entry bundle.
type bundle struct {
	Index   uint64
	Partial uint8
}

// Copy starts the work of copying sourceSize entries from the source to the target log.
//
// Only the entry bundles are copied as the target storage is expected to integrate them and recalculate the root.
// This is done to ensure the correctness of both the source log as well as the copy process itself.
//
// A call to this function will block until either the copying is done, or an error has occurred.
func (c *copier) Copy(ctx context.Context, fromSize uint64, sourceSize uint64) error {
	klog.Infof("Starting copy from %d to source size %d", fromSize, sourceSize)

	if fromSize > sourceSize {
		return fmt.Errorf("from size %d > source size %d", fromSize, sourceSize)
	}

	go c.populateWork(fromSize, sourceSize)

	// Do the copying
	eg := errgroup.Group{}
	for range cap(c.todo) {
		eg.Go(func() error {
			return c.worker(ctx)
		})
	}
	if err := eg.Wait(); err != nil {
		return fmt.Errorf("copy failed: %v", err)
	}

	return nil
}

// Progress returns the number of bundles from the source present in the target.
func (c *copier) BundlesCopied() uint64 {
	return c.bundlesCopied.Load()
}

// populateWork sends entries to the `todo` work channel.
// Each entry corresponds to an individual entryBundle which needs to be copied.
func (m *copier) populateWork(from, treeSize uint64) {
	klog.Infof("Spans for entry range [%d, %d)", from, treeSize)
	defer close(m.todo)

	for ri := range layout.Range(from, treeSize-from, treeSize) {
		m.todo <- bundle{Index: ri.Index, Partial: ri.Partial}
	}
}

// worker undertakes work items from the `todo` channel.
//
// It will attempt to retry failed operations several times before giving up, this should help
// deal with any transient errors which may occur.
func (m *copier) worker(ctx context.Context) error {
	for b := range m.todo {
		n, err := backoff.Retry(ctx, func() (uint64, error) {
			d, err := m.getEntryBundle(ctx, b.Index, uint8(b.Partial))
			if err != nil {
				wErr := fmt.Errorf("failed to fetch entrybundle %d (p=%d): %v", b.Index, b.Partial, err)
				klog.Infof("%v", wErr)
				return 0, wErr
			}
			if err := m.setEntryBundle(ctx, b.Index, b.Partial, d); err != nil {
				wErr := fmt.Errorf("failed to store entrybundle %d (p=%d): %v", b.Index, b.Partial, err)
				klog.Infof("%v", wErr)
				return 0, wErr
			}
			return 1, nil
		},
			backoff.WithMaxTries(10),
			backoff.WithBackOff(backoff.NewExponentialBackOff()))
		if err != nil {
			klog.Infof("retry: %v", err)
			return err
		}
		m.bundlesCopied.Add(n)
	}
	return nil
}