|
| 1 | +// Copyright 2025 The Cockroach Authors. |
| 2 | +// |
| 3 | +// Use of this software is governed by the CockroachDB Software License |
| 4 | +// included in the /LICENSE file. |
| 5 | + |
| 6 | +package jobfrontier |
| 7 | + |
| 8 | +import ( |
| 9 | + "context" |
| 10 | + "fmt" |
| 11 | + |
| 12 | + "github.com/cockroachdb/cockroach/pkg/jobs" |
| 13 | + "github.com/cockroachdb/cockroach/pkg/jobs/jobspb" |
| 14 | + "github.com/cockroachdb/cockroach/pkg/roachpb" |
| 15 | + "github.com/cockroachdb/cockroach/pkg/sql/isql" |
| 16 | + "github.com/cockroachdb/cockroach/pkg/util/hlc" |
| 17 | + "github.com/cockroachdb/cockroach/pkg/util/protoutil" |
| 18 | + "github.com/cockroachdb/cockroach/pkg/util/span" |
| 19 | + "github.com/cockroachdb/errors" |
| 20 | +) |
| 21 | + |
| 22 | +const frontierPrefix = "frontier/" |
| 23 | +const shardSep = "_" |
| 24 | + |
| 25 | +// Get loads a complete frontier from persistent storage and returns it and a |
| 26 | +// true value, or nil and false if one is not found. |
| 27 | +// |
| 28 | +// The returned frontier will contain all spans and their timestamps that were |
| 29 | +// previously stored via Store(). The spans are derived from the persisted data. |
| 30 | +func Get( |
| 31 | + ctx context.Context, txn isql.Txn, jobID jobspb.JobID, name string, |
| 32 | +) (span.Frontier, bool, error) { |
| 33 | + infoStorage := jobs.InfoStorageForJob(txn, jobID) |
| 34 | + |
| 35 | + // Read all persisted entries, both as entries and as plain spans; we need the |
| 36 | + // latter form to construct the frontier and the former to advance it. |
| 37 | + // TODO(dt): we could avoid duplicate allocation here if we added an API to |
| 38 | + // construct frontier directly from entries. |
| 39 | + var entries []frontierEntry |
| 40 | + var spans []roachpb.Span |
| 41 | + |
| 42 | + keyPrefix := frontierPrefix + name + shardSep |
| 43 | + |
| 44 | + var found bool |
| 45 | + if err := infoStorage.Iterate(ctx, keyPrefix, func(_ string, value []byte) error { |
| 46 | + found = true |
| 47 | + var r jobspb.ResolvedSpans |
| 48 | + if err := protoutil.Unmarshal(value, &r); err != nil { |
| 49 | + return err |
| 50 | + } |
| 51 | + for _, sp := range r.ResolvedSpans { |
| 52 | + entries = append(entries, frontierEntry{Span: sp.Span, Timestamp: sp.Timestamp}) |
| 53 | + spans = append(spans, sp.Span) |
| 54 | + } |
| 55 | + return nil |
| 56 | + }); err != nil || !found { |
| 57 | + return nil, false, err |
| 58 | + } |
| 59 | + |
| 60 | + // Construct frontier to track the set of spans found and advance it to their |
| 61 | + // persisted timestamps. This implies we perist zero-timestamp spans to keep |
| 62 | + // the set of tracked spans even if they do not have progress. |
| 63 | + frontier, err := span.MakeFrontier(spans...) |
| 64 | + if err != nil { |
| 65 | + return nil, false, err |
| 66 | + } |
| 67 | + for _, entry := range entries { |
| 68 | + if _, err := frontier.Forward(entry.Span, entry.Timestamp); err != nil { |
| 69 | + return nil, false, err |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + return frontier, true, nil |
| 74 | +} |
| 75 | + |
| 76 | +// Store persists a frontier's current state to storage. |
| 77 | +// |
| 78 | +// All span entries in the frontier and their current timestamps will be |
| 79 | +// persisted. Any previously stored frontier data under the same name will be |
| 80 | +// replaced. |
| 81 | +// |
| 82 | +// InfoStorage keys are prefixed with "frontier/", the passed name, and then a |
| 83 | +// chunk identifier. |
| 84 | +func Store( |
| 85 | + ctx context.Context, txn isql.Txn, jobID jobspb.JobID, name string, frontier span.Frontier, |
| 86 | +) error { |
| 87 | + return storeChunked(ctx, txn, jobID, name, frontier, 2<<20 /* 2mb */) |
| 88 | +} |
| 89 | + |
| 90 | +func storeChunked( |
| 91 | + ctx context.Context, |
| 92 | + txn isql.Txn, |
| 93 | + jobID jobspb.JobID, |
| 94 | + name string, |
| 95 | + frontier span.Frontier, |
| 96 | + chunkSize int, |
| 97 | +) error { |
| 98 | + infoStorage := jobs.InfoStorageForJob(txn, jobID) |
| 99 | + |
| 100 | + // Wipe any existing frontier shards, since we cannot rely on the shard func |
| 101 | + // to return the same set of shards to guarantee a full overwrite. Slightly |
| 102 | + // annoying that each shard's info key write will *also* issue a delete during |
| 103 | + // its write that is duplicative as we already deleted everything here, but |
| 104 | + // we don't really have a choice. We could specialize non-sharded/fixed-shard |
| 105 | + // frontiers (non-sharded is just fixed=1), where the write call would handle |
| 106 | + // deleting any prior entry, but doesn't seem worth it: you need a promise it |
| 107 | + // does not become sharded later, so would probably want to be a separate API. |
| 108 | + if err := deleteEntries(ctx, infoStorage, name); err != nil { |
| 109 | + return err |
| 110 | + } |
| 111 | + |
| 112 | + // Collect all frontier entries |
| 113 | + var all []jobspb.ResolvedSpan |
| 114 | + all = make([]jobspb.ResolvedSpan, 0, frontier.Len()) |
| 115 | + for spanEntry, timestamp := range frontier.Entries() { |
| 116 | + all = append(all, jobspb.ResolvedSpan{ |
| 117 | + Span: spanEntry, |
| 118 | + Timestamp: timestamp, |
| 119 | + }) |
| 120 | + } |
| 121 | + |
| 122 | + // Flush the frontier chunks. |
| 123 | + var chunk, size int |
| 124 | + var chunkStart int |
| 125 | + // Group entries by shard. |
| 126 | + for i, sp := range all { |
| 127 | + if size > chunkSize { |
| 128 | + if err := storeEntries(ctx, infoStorage, name, fmt.Sprintf("%d", chunk), all[chunkStart:i]); err != nil { |
| 129 | + return err |
| 130 | + } |
| 131 | + chunk++ |
| 132 | + size = 0 |
| 133 | + chunkStart = i |
| 134 | + } |
| 135 | + |
| 136 | + size += len(sp.Span.Key) + len(sp.Span.EndKey) + 16 // timestamp/overhead. |
| 137 | + } |
| 138 | + if chunkStart < len(all) { |
| 139 | + if err := storeEntries(ctx, infoStorage, name, fmt.Sprintf("%d", chunk), all[chunkStart:]); err != nil { |
| 140 | + return err |
| 141 | + } |
| 142 | + } |
| 143 | + return nil |
| 144 | +} |
| 145 | + |
| 146 | +func storeEntries( |
| 147 | + ctx context.Context, |
| 148 | + infoStorage jobs.InfoStorage, |
| 149 | + name, shard string, |
| 150 | + entries []jobspb.ResolvedSpan, |
| 151 | +) error { |
| 152 | + data, err := protoutil.Marshal(&jobspb.ResolvedSpans{ResolvedSpans: entries}) |
| 153 | + if err != nil { |
| 154 | + return errors.Wrap(err, "failed to serialize frontier entries") |
| 155 | + } |
| 156 | + key := fmt.Sprintf("%s%s%s%s", frontierPrefix, name, shardSep, shard) |
| 157 | + return infoStorage.Write(ctx, key, data) |
| 158 | +} |
| 159 | + |
| 160 | +// Delete removes a persisted frontier by the given name for the given job. |
| 161 | +func Delete(ctx context.Context, txn isql.Txn, jobID jobspb.JobID, name string) error { |
| 162 | + infoStorage := jobs.InfoStorageForJob(txn, jobID) |
| 163 | + return deleteEntries(ctx, infoStorage, name) |
| 164 | +} |
| 165 | + |
| 166 | +func deleteEntries(ctx context.Context, infoStorage jobs.InfoStorage, name string) error { |
| 167 | + startKey := frontierPrefix + name + shardSep |
| 168 | + endKey := frontierPrefix + name + string(rune(shardSep[0])+1) |
| 169 | + return infoStorage.DeleteRange(ctx, startKey, endKey, 0 /* no limit */) |
| 170 | +} |
| 171 | + |
| 172 | +// frontierEntry represents a single persisted frontier entry. |
| 173 | +// This is used internally for serialization but may be useful for testing. |
| 174 | +type frontierEntry struct { |
| 175 | + Span roachpb.Span |
| 176 | + Timestamp hlc.Timestamp |
| 177 | +} |
0 commit comments