2019-11-07 19:05:39 +00:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-09-18 14:10:16 +00:00
|
|
|
"path/filepath"
|
2019-11-07 19:05:39 +00:00
|
|
|
"regexp"
|
|
|
|
"sort"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2023-09-18 14:16:11 +00:00
|
|
|
"sync/atomic"
|
2019-11-07 19:05:39 +00:00
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Part is an atomic unit for transfer during backup / restore.
|
|
|
|
//
|
|
|
|
// Each source file can be split into parts with up to MaxPartSize sizes.
|
|
|
|
type Part struct {
|
|
|
|
// Path is the path to file for backup.
|
2023-09-18 14:10:16 +00:00
|
|
|
//
|
|
|
|
// Path must consistently use `/` as directory separator.
|
|
|
|
// Use ToCanonicalPath() function for converting local directory separators to `/`.
|
2019-11-07 19:05:39 +00:00
|
|
|
Path string
|
|
|
|
|
|
|
|
// FileSize is the size of the whole file for the given part.
|
|
|
|
FileSize uint64
|
|
|
|
|
|
|
|
// Offset is offset in the file to backup.
|
|
|
|
Offset uint64
|
|
|
|
|
|
|
|
// Size is the size of the part to backup starting from Offset.
|
|
|
|
Size uint64
|
|
|
|
|
|
|
|
// ActualSize is the actual size of the part.
|
|
|
|
//
|
|
|
|
// The part is considered broken if it isn't equal to Size.
|
|
|
|
// Such a part must be removed from remote storage.
|
|
|
|
ActualSize uint64
|
|
|
|
}
|
|
|
|
|
2023-09-18 14:16:11 +00:00
|
|
|
// key returns a string, which uniquely identifies p.
|
2019-11-07 19:05:39 +00:00
|
|
|
func (p *Part) key() string {
|
2023-09-21 09:25:19 +00:00
|
|
|
if strings.HasSuffix(p.Path, "/parts.json") ||
|
|
|
|
strings.HasSuffix(p.Path, "/appliedRetention.txt") {
|
|
|
|
// parts.json and appliedRetention.txt files contents changes over time,
|
|
|
|
// so it must have an unique key in order to always copy it during
|
|
|
|
// backup, restore and server-side copy.
|
2023-09-18 14:16:11 +00:00
|
|
|
// See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/5005
|
2024-02-24 00:07:51 +00:00
|
|
|
id := uniqueKeyID.Add(1)
|
2023-09-18 14:16:11 +00:00
|
|
|
return fmt.Sprintf("unique-%016X", id)
|
|
|
|
}
|
2021-12-01 09:01:28 +00:00
|
|
|
// Do not use p.FileSize in the key, since it cannot be properly initialized when resuming the restore for partially restored file
|
|
|
|
return fmt.Sprintf("%s%016X%016X%016X", p.Path, p.Offset, p.Size, p.ActualSize)
|
2019-11-07 19:05:39 +00:00
|
|
|
}
|
|
|
|
|
2024-02-24 00:07:51 +00:00
|
|
|
var uniqueKeyID atomic.Uint64
|
2023-09-18 14:16:11 +00:00
|
|
|
|
2019-11-07 19:05:39 +00:00
|
|
|
// String returns human-readable representation of the part.
|
|
|
|
func (p *Part) String() string {
|
|
|
|
return fmt.Sprintf("part{path: %q, file_size: %d, offset: %d, size: %d}", p.Path, p.FileSize, p.Offset, p.Size)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemotePath returns remote path for the part p and the given prefix.
|
|
|
|
func (p *Part) RemotePath(prefix string) string {
|
|
|
|
for strings.HasSuffix(prefix, "/") {
|
|
|
|
prefix = prefix[:len(prefix)-1]
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%s/%s/%016X_%016X_%016X", prefix, p.Path, p.FileSize, p.Offset, p.Size)
|
|
|
|
}
|
|
|
|
|
2023-09-18 14:10:16 +00:00
|
|
|
// LocalPath returns local path for p at the given dir.
|
|
|
|
func (p *Part) LocalPath(dir string) string {
|
|
|
|
path := p.Path
|
|
|
|
if filepath.Separator != '/' {
|
|
|
|
path = strings.ReplaceAll(path, "/", string(filepath.Separator))
|
|
|
|
}
|
|
|
|
return filepath.Join(dir, path)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToCanonicalPath returns canonical path by replacing local directory separators with `/`.
|
|
|
|
func ToCanonicalPath(path string) string {
|
|
|
|
if filepath.Separator == '/' {
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
return strings.ReplaceAll(path, string(filepath.Separator), "/")
|
|
|
|
}
|
|
|
|
|
2023-05-03 08:48:53 +00:00
|
|
|
var partNameRegexp = regexp.MustCompile(`^(.+)[/\\]([0-9A-F]{16})_([0-9A-F]{16})_([0-9A-F]{16})$`)
|
2019-11-07 19:05:39 +00:00
|
|
|
|
|
|
|
// ParseFromRemotePath parses p from remotePath.
|
|
|
|
//
|
|
|
|
// Returns true on success.
|
2023-09-18 14:10:16 +00:00
|
|
|
//
|
|
|
|
// remotePath must be in canonical form received from ToCanonicalPath().
|
2019-11-07 19:05:39 +00:00
|
|
|
func (p *Part) ParseFromRemotePath(remotePath string) bool {
|
|
|
|
tmp := partNameRegexp.FindStringSubmatch(remotePath)
|
|
|
|
if len(tmp) != 5 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
path := tmp[1]
|
|
|
|
for strings.HasPrefix(path, "/") {
|
|
|
|
path = path[1:]
|
|
|
|
}
|
|
|
|
fileSize, err := strconv.ParseUint(tmp[2], 16, 64)
|
|
|
|
if err != nil {
|
|
|
|
logger.Panicf("BUG: cannot parse fileSize from %q: %s", tmp[2], err)
|
|
|
|
}
|
|
|
|
offset, err := strconv.ParseUint(tmp[3], 16, 64)
|
|
|
|
if err != nil {
|
|
|
|
logger.Panicf("BUG: cannot parse offset from %q: %s", tmp[3], err)
|
|
|
|
}
|
|
|
|
size, err := strconv.ParseUint(tmp[4], 16, 64)
|
|
|
|
if err != nil {
|
|
|
|
logger.Panicf("BUG: cannot parse size from %q: %s", tmp[4], err)
|
|
|
|
}
|
|
|
|
p.Path = path
|
|
|
|
p.FileSize = fileSize
|
|
|
|
p.Offset = offset
|
|
|
|
p.Size = size
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// MaxPartSize is the maximum size for each part.
|
|
|
|
//
|
|
|
|
// The MaxPartSize reduces bandwidth usage during retires on network errors
|
2019-11-11 22:16:42 +00:00
|
|
|
// when transferring multi-TB files.
|
2021-01-13 10:15:36 +00:00
|
|
|
const MaxPartSize = 1024 * 1024 * 1024
|
2019-11-07 19:05:39 +00:00
|
|
|
|
|
|
|
// SortParts sorts parts by (Path, Offset)
|
|
|
|
func SortParts(parts []Part) {
|
|
|
|
sort.Slice(parts, func(i, j int) bool {
|
|
|
|
a := parts[i]
|
|
|
|
b := parts[j]
|
|
|
|
if a.Path != b.Path {
|
|
|
|
return a.Path < b.Path
|
|
|
|
}
|
|
|
|
return a.Offset < b.Offset
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// PartsDifference returns a - b
|
|
|
|
func PartsDifference(a, b []Part) []Part {
|
|
|
|
m := make(map[string]bool, len(b))
|
|
|
|
for _, p := range b {
|
|
|
|
k := p.key()
|
|
|
|
m[k] = true
|
|
|
|
}
|
|
|
|
var d []Part
|
|
|
|
for _, p := range a {
|
|
|
|
k := p.key()
|
|
|
|
if !m[k] {
|
|
|
|
d = append(d, p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
|
|
|
// PartsIntersect returns the intersection of a and b
|
|
|
|
func PartsIntersect(a, b []Part) []Part {
|
|
|
|
m := make(map[string]bool, len(a))
|
|
|
|
for _, p := range a {
|
|
|
|
k := p.key()
|
|
|
|
m[k] = true
|
|
|
|
}
|
|
|
|
var d []Part
|
|
|
|
for _, p := range b {
|
|
|
|
k := p.key()
|
|
|
|
if m[k] {
|
|
|
|
d = append(d, p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return d
|
|
|
|
}
|