Skip to content

Commit 4657cdd

Browse files
craig[bot]msbutler
andcommitted
Merge #155647
155647: backup: introduce unified WriteBackupMetadata helper r=kev-cao a=msbutler This helper will be used by full, inc and compacted backups. This helper will make it much easier to support locality aware compacted backups. Epic: none Release note: none Co-authored-by: Michael Butler <[email protected]>
2 parents 6289bde + c87fa68 commit 4657cdd

File tree

6 files changed

+106
-133
lines changed

6 files changed

+106
-133
lines changed

pkg/backup/backup_job.go

Lines changed: 1 addition & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"strings"
1616
"time"
1717

18-
"github.com/cockroachdb/cockroach/pkg/backup/backupbase"
1918
"github.com/cockroachdb/cockroach/pkg/backup/backupdest"
2019
"github.com/cockroachdb/cockroach/pkg/backup/backupencryption"
2120
"github.com/cockroachdb/cockroach/pkg/backup/backupinfo"
@@ -24,7 +23,6 @@ import (
2423
"github.com/cockroachdb/cockroach/pkg/build"
2524
"github.com/cockroachdb/cockroach/pkg/ccl/kvccl/kvfollowerreadsccl"
2625
"github.com/cockroachdb/cockroach/pkg/cloud"
27-
"github.com/cockroachdb/cockroach/pkg/cloud/cloudpb"
2826
"github.com/cockroachdb/cockroach/pkg/clusterversion"
2927
"github.com/cockroachdb/cockroach/pkg/jobs"
3028
"github.com/cockroachdb/cockroach/pkg/jobs/joberror"
@@ -124,10 +122,8 @@ func backup(
124122
details jobspb.BackupDetails,
125123
settings *cluster.Settings,
126124
defaultStore cloud.ExternalStorage,
127-
storageByLocalityKV map[string]*cloudpb.ExternalStorage,
128125
resumer *backupResumer,
129126
backupManifest *backuppb.BackupManifest,
130-
makeExternalStorage cloud.ExternalStorageFactory,
131127
) (_ roachpb.RowCount, numBackupInstances int, _ error) {
132128
resumerSpan := tracing.SpanFromContext(ctx)
133129
var lastCheckpoint time.Time
@@ -384,77 +380,11 @@ func backup(
384380
}
385381
}
386382

387-
backupID := uuid.MakeV4()
388-
backupManifest.ID = backupID
389-
// Write additional partial descriptors to each node for partitioned backups.
390-
if len(storageByLocalityKV) > 0 {
391-
resumerSpan.RecordStructured(&types.StringValue{Value: "writing partition descriptors for partitioned backup"})
392-
filesByLocalityKV := make(map[string][]backuppb.BackupManifest_File)
393-
for _, file := range backupManifest.Files {
394-
filesByLocalityKV[file.LocalityKV] = append(filesByLocalityKV[file.LocalityKV], file)
395-
}
396-
397-
nextPartitionedDescFilenameID := 1
398-
for kv, conf := range storageByLocalityKV {
399-
backupManifest.LocalityKVs = append(backupManifest.LocalityKVs, kv)
400-
// Set a unique filename for each partition backup descriptor. The ID
401-
// ensures uniqueness, and the kv string appended to the end is for
402-
// readability.
403-
filename := fmt.Sprintf("%s_%d_%s", backupPartitionDescriptorPrefix,
404-
nextPartitionedDescFilenameID, backupinfo.SanitizeLocalityKV(kv))
405-
nextPartitionedDescFilenameID++
406-
backupManifest.PartitionDescriptorFilenames = append(backupManifest.PartitionDescriptorFilenames, filename)
407-
desc := backuppb.BackupPartitionDescriptor{
408-
LocalityKV: kv,
409-
Files: filesByLocalityKV[kv],
410-
BackupID: backupID,
411-
}
412-
413-
if err := func() error {
414-
store, err := makeExternalStorage(ctx, *conf)
415-
if err != nil {
416-
return err
417-
}
418-
defer store.Close()
419-
return backupinfo.WriteBackupPartitionDescriptor(ctx, store, filename,
420-
encryption, &kmsEnv, &desc)
421-
}(); err != nil {
422-
return roachpb.RowCount{}, 0, err
423-
}
424-
}
425-
}
426-
427-
// TODO(msbutler): version gate writing the old manifest once we can guarantee
428-
// a cluster version that will not read the old manifest. This will occur when we delete
429-
// LegacyFindPriorBackups and the fallback path in
430-
// ListFullBackupsInCollection, which can occur when we completely rely on the
431-
// backup index.
432-
if err := backupinfo.WriteBackupManifest(ctx, defaultStore, backupbase.DeprecatedBackupManifestName,
433-
encryption, &kmsEnv, backupManifest); err != nil {
434-
return roachpb.RowCount{}, 0, err
435-
}
436-
437-
if err := backupinfo.WriteMetadataWithExternalSSTs(ctx, defaultStore, encryption,
438-
&kmsEnv, backupManifest); err != nil {
439-
return roachpb.RowCount{}, 0, err
440-
}
441-
442383
statsTable := getTableStatsForBackup(ctx, execCtx.ExecCfg().InternalDB.Executor(), backupManifest.Descriptors)
443-
if err := backupinfo.WriteTableStatistics(ctx, defaultStore, encryption, &kmsEnv, &statsTable); err != nil {
384+
if err := backupinfo.WriteBackupMetadata(ctx, execCtx, defaultStore, details, &kmsEnv, backupManifest, statsTable); err != nil {
444385
return roachpb.RowCount{}, 0, err
445386
}
446387

447-
if err := backupinfo.WriteBackupIndexMetadata(
448-
ctx,
449-
execCtx.ExecCfg(),
450-
execCtx.User(),
451-
execCtx.ExecCfg().DistSQLSrv.ExternalStorageFromURI,
452-
details,
453-
backupManifest.RevisionStartTime,
454-
); err != nil {
455-
return roachpb.RowCount{}, 0, errors.Wrapf(err, "writing backup index metadata")
456-
}
457-
458388
return backupManifest.EntryCounts, numBackupInstances, nil
459389
}
460390

@@ -692,15 +622,6 @@ func (b *backupResumer) Resume(ctx context.Context, execCtx interface{}) error {
692622
}
693623
}
694624

695-
storageByLocalityKV := make(map[string]*cloudpb.ExternalStorage)
696-
for kv, uri := range details.URIsByLocalityKV {
697-
conf, err := cloud.ExternalStorageConfFromURI(uri, p.User())
698-
if err != nil {
699-
return err
700-
}
701-
storageByLocalityKV[kv] = &conf
702-
}
703-
704625
mem := p.ExecCfg().RootMemoryMonitor.MakeBoundAccount()
705626
defer mem.Close(ctx)
706627
var memSize int64
@@ -742,10 +663,8 @@ func (b *backupResumer) Resume(ctx context.Context, execCtx interface{}) error {
742663
details,
743664
p.ExecCfg().Settings,
744665
defaultStore,
745-
storageByLocalityKV,
746666
b,
747667
backupManifest,
748-
p.ExecCfg().DistSQLSrv.ExternalStorage,
749668
)
750669
if err == nil {
751670
break

pkg/backup/backup_planning.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,6 @@ import (
3838
)
3939

4040
const (
41-
// backupPartitionDescriptorPrefix is the file name prefix for serialized
42-
// BackupPartitionDescriptor protos.
43-
backupPartitionDescriptorPrefix = "BACKUP_PART"
44-
4541
deprecatedPrivilegesBackupPreamble = "The existing privileges are being deprecated " +
4642
"in favour of a fine-grained privilege model explained here " +
4743
"https://www.cockroachlabs.com/docs/stable/backup.html#required-privileges. In a future release, to run"

pkg/backup/backupbase/constants.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ const (
1919
// LATEST files will be stored as we no longer want to overwrite it.
2020
LatestHistoryDirectory = backupMetadataDirectory + "/" + "latest"
2121

22+
// BackupPartitionDescriptorPrefix is the file name prefix for serialized
23+
// BackupPartitionDescriptor protos.
24+
BackupPartitionDescriptorPrefix = "BACKUP_PART"
25+
2226
// DateBasedIncFolderName is the date format used when creating sub-directories
2327
// storing incremental backups for auto-appendable backups.
2428
// It is exported for testing backup inspection tooling.

pkg/backup/backupinfo/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ go_library(
5252
"//pkg/util/syncutil",
5353
"//pkg/util/timeutil",
5454
"//pkg/util/tracing",
55+
"//pkg/util/uuid",
5556
"@com_github_cockroachdb_errors//:errors",
5657
"@com_github_cockroachdb_pebble//objstorage",
5758
"@com_github_klauspost_compress//gzip",

pkg/backup/backupinfo/manifest_handling.go

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ import (
5353
"github.com/cockroachdb/cockroach/pkg/util/syncutil"
5454
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
5555
"github.com/cockroachdb/cockroach/pkg/util/tracing"
56+
"github.com/cockroachdb/cockroach/pkg/util/uuid"
5657
"github.com/cockroachdb/errors"
5758
gzip "github.com/klauspost/compress/gzip"
5859
)
@@ -1776,3 +1777,99 @@ func ConstructDateBasedIncrementalFolderName(start, end time.Time) string {
17761777
start.Format(backupbase.DateBasedIncFolderNameSuffix),
17771778
)
17781779
}
1780+
1781+
// WriteBackupMetadata writes the manifest, backup index, and statistics to
1782+
// external storage.
1783+
func WriteBackupMetadata(
1784+
ctx context.Context,
1785+
execCtx sql.JobExecContext,
1786+
store cloud.ExternalStorage,
1787+
details jobspb.BackupDetails,
1788+
kmsEnv cloud.KMSEnv,
1789+
backupManifest *backuppb.BackupManifest,
1790+
statsTable backuppb.StatsTable,
1791+
) error {
1792+
backupID := uuid.MakeV4()
1793+
backupManifest.ID = backupID
1794+
1795+
// Write additional partial descriptors to each node for partitioned backups.
1796+
//
1797+
// TODO(msbutler): put this locality logic in a helper.
1798+
if len(details.URIsByLocalityKV) > 0 {
1799+
1800+
storageByLocalityKV := make(map[string]*cloudpb.ExternalStorage)
1801+
for kv, uri := range details.URIsByLocalityKV {
1802+
conf, err := cloud.ExternalStorageConfFromURI(uri, execCtx.User())
1803+
if err != nil {
1804+
return err
1805+
}
1806+
storageByLocalityKV[kv] = &conf
1807+
}
1808+
1809+
filesByLocalityKV := make(map[string][]backuppb.BackupManifest_File)
1810+
for _, file := range backupManifest.Files {
1811+
filesByLocalityKV[file.LocalityKV] = append(filesByLocalityKV[file.LocalityKV], file)
1812+
}
1813+
1814+
nextPartitionedDescFilenameID := 1
1815+
for kv, conf := range storageByLocalityKV {
1816+
backupManifest.LocalityKVs = append(backupManifest.LocalityKVs, kv)
1817+
// Set a unique filename for each partition backup descriptor. The ID
1818+
// ensures uniqueness, and the kv string appended to the end is for
1819+
// readability.
1820+
filename := fmt.Sprintf("%s_%d_%s", backupbase.BackupPartitionDescriptorPrefix,
1821+
nextPartitionedDescFilenameID, SanitizeLocalityKV(kv))
1822+
nextPartitionedDescFilenameID++
1823+
backupManifest.PartitionDescriptorFilenames = append(backupManifest.PartitionDescriptorFilenames, filename)
1824+
desc := backuppb.BackupPartitionDescriptor{
1825+
LocalityKV: kv,
1826+
Files: filesByLocalityKV[kv],
1827+
BackupID: backupID,
1828+
}
1829+
1830+
if err := func() error {
1831+
localityStore, err := execCtx.ExecCfg().DistSQLSrv.ExternalStorage(ctx, *conf)
1832+
if err != nil {
1833+
return err
1834+
}
1835+
defer localityStore.Close()
1836+
return WriteBackupPartitionDescriptor(ctx, localityStore, filename,
1837+
details.EncryptionOptions, kmsEnv, &desc)
1838+
}(); err != nil {
1839+
return err
1840+
}
1841+
}
1842+
}
1843+
1844+
// TODO(msbutler): version gate writing the old manifest once we can guarantee
1845+
// a cluster version that will not read the old manifest. This will occur when we delete
1846+
// LegacyFindPriorBackups and the fallback path in
1847+
// ListFullBackupsInCollection, which can occur when we completely rely on the
1848+
// backup index.
1849+
if err := WriteBackupManifest(ctx, store, backupbase.DeprecatedBackupManifestName,
1850+
details.EncryptionOptions, kmsEnv, backupManifest); err != nil {
1851+
return err
1852+
}
1853+
if err := WriteMetadataWithExternalSSTs(ctx, store, details.EncryptionOptions,
1854+
kmsEnv, backupManifest); err != nil {
1855+
return err
1856+
}
1857+
1858+
if err := WriteTableStatistics(
1859+
ctx, store, details.EncryptionOptions, kmsEnv, &statsTable,
1860+
); err != nil {
1861+
return errors.Wrapf(err, "writing table statistics")
1862+
}
1863+
1864+
return errors.Wrapf(
1865+
WriteBackupIndexMetadata(
1866+
ctx,
1867+
execCtx.ExecCfg(),
1868+
execCtx.User(),
1869+
execCtx.ExecCfg().DistSQLSrv.ExternalStorageFromURI,
1870+
details,
1871+
backupManifest.RevisionStartTime,
1872+
),
1873+
"writing backup index metadata",
1874+
)
1875+
}

pkg/backup/compaction_job.go

Lines changed: 3 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -778,50 +778,6 @@ func getBackupChain(
778778
return manifests, localityInfo, baseEncryptionInfo, allIters, nil
779779
}
780780

781-
// concludeBackupCompaction completes the backup compaction process after the backup has been
782-
// completed by writing the manifest and associated metadata to the backup destination.
783-
//
784-
// TODO (kev-cao): Can move this helper to the backup code at some point.
785-
func concludeBackupCompaction(
786-
ctx context.Context,
787-
execCtx sql.JobExecContext,
788-
store cloud.ExternalStorage,
789-
details jobspb.BackupDetails,
790-
kmsEnv cloud.KMSEnv,
791-
backupManifest *backuppb.BackupManifest,
792-
) error {
793-
backupID := uuid.MakeV4()
794-
backupManifest.ID = backupID
795-
796-
if err := backupinfo.WriteBackupManifest(ctx, store, backupbase.DeprecatedBackupManifestName,
797-
details.EncryptionOptions, kmsEnv, backupManifest); err != nil {
798-
return err
799-
}
800-
if err := backupinfo.WriteMetadataWithExternalSSTs(ctx, store, details.EncryptionOptions,
801-
kmsEnv, backupManifest); err != nil {
802-
return err
803-
}
804-
805-
statsTable := getTableStatsForBackup(ctx, execCtx.ExecCfg().InternalDB.Executor(), backupManifest.Descriptors)
806-
if err := backupinfo.WriteTableStatistics(
807-
ctx, store, details.EncryptionOptions, kmsEnv, &statsTable,
808-
); err != nil {
809-
return errors.Wrapf(err, "writing table statistics")
810-
}
811-
812-
return errors.Wrapf(
813-
backupinfo.WriteBackupIndexMetadata(
814-
ctx,
815-
execCtx.ExecCfg(),
816-
execCtx.User(),
817-
execCtx.ExecCfg().DistSQLSrv.ExternalStorageFromURI,
818-
details,
819-
backupManifest.RevisionStartTime,
820-
),
821-
"writing backup index metadata",
822-
)
823-
}
824-
825781
// processProgress processes progress updates from the bulk processor for a backup and updates
826782
// the associated manifest.
827783
func processProgress(
@@ -922,9 +878,9 @@ func doCompaction(
922878
); err != nil {
923879
return err
924880
}
925-
926-
return concludeBackupCompaction(
927-
ctx, execCtx, defaultStore, details, kmsEnv, manifest,
881+
statsTable := getTableStatsForBackup(ctx, execCtx.ExecCfg().InternalDB.Executor(), manifest.Descriptors)
882+
return backupinfo.WriteBackupMetadata(
883+
ctx, execCtx, defaultStore, details, kmsEnv, manifest, statsTable,
928884
)
929885
}
930886

0 commit comments

Comments
 (0)