restic/internal/repository/index.go

628 lines
14 KiB
Go
Raw Normal View History

package repository
2015-04-26 17:10:31 +02:00
import (
2017-06-04 11:16:55 +02:00
"context"
2015-04-26 17:10:31 +02:00
"encoding/json"
"io"
"sync"
2015-10-12 23:59:17 +02:00
"time"
2015-04-26 17:10:31 +02:00
2017-07-23 14:21:03 +02:00
"github.com/restic/restic/internal/errors"
2017-07-24 17:42:25 +02:00
"github.com/restic/restic/internal/restic"
2017-07-23 14:21:03 +02:00
"github.com/restic/restic/internal/debug"
2015-04-26 17:10:31 +02:00
)
// In large repositories, millions of blobs are stored in the repository
// and restic needs to store an index entry for each blob in memory for
// most operations.
// Hence the index data structure defined here is one of the main contributions
// to the total memory requirements of restic.
//
// We use two maps to store each index entry.
// The first map stores the first entry of a blobtype/blobID
// The key of the map is a BlobHandle
// The entries are the actual index entries.
// In the second map we store duplicate index entries, i.e. entries with same
// blobtype/blobID
// In the index entries, we need to reference the packID. As one pack may
// contain many blobs the packIDs are saved in a separate array and only the index
// within this array is saved in the indexEntry
//
// To compute the needed amount of memory, we need some assumptions.
// Maps need an overhead of allocated but not needed elements.
// For computations, we assume an overhead of 50% and use OF=1.5 (overhead factor)
// As duplicates are only present in edge cases and are also removed by prune runs,
// we assume that there are no significant duplicates and omit them in the calculations.
// Moreover we asssume on average a minimum of 8 blobs per pack; BP=8
// (Note that for large files there should be 3 blobs per pack as the average chunk
// size is 1.5 MB and the minimum pack size is 4 MB)
//
// We have the following sizes:
// key: 32 + 1 = 33 bytes
// indexEntry: 8 + 4 + 4 = 16 bytes
// each packID: 32 bytes
//
// To save N index entries, we therefore need:
// N * OF * (33 + 16) bytes + N * 32 bytes / BP = N * 78 bytes
// Index holds lookup tables for id -> pack.
2015-04-26 17:10:31 +02:00
type Index struct {
m sync.Mutex
blob map[restic.BlobHandle]indexEntry
duplicates map[restic.BlobHandle][]indexEntry
packs restic.IDs
treePacks restic.IDs
2016-08-31 20:29:54 +02:00
final bool // set to true for all indexes read from the backend ("finalized")
id restic.ID // set to the ID of the index when it's finalized
supersedes restic.IDs
2015-10-12 23:59:17 +02:00
created time.Time
2015-04-26 17:10:31 +02:00
}
type indexEntry struct {
// only save index do packs; i.e. packs[packindex] yields the packID
packIndex int
offset uint32
length uint32
2015-04-26 17:10:31 +02:00
}
// NewIndex returns a new index.
func NewIndex() *Index {
return &Index{
blob: make(map[restic.BlobHandle]indexEntry),
duplicates: make(map[restic.BlobHandle][]indexEntry),
created: time.Now(),
2015-04-26 17:10:31 +02:00
}
}
// withDuplicates returns the list of all entries for the given blob handle
func (idx *Index) withDuplicates(h restic.BlobHandle, entry indexEntry) []indexEntry {
entries, ok := idx.duplicates[h]
if ok {
all := make([]indexEntry, len(entries)+1)
all[0] = entry
copy(all[1:], entries)
return all
}
return []indexEntry{entry}
}
// addToPacks saves the given pack ID and return the index.
// This procedere allows to use pack IDs which can be easily garbage collected after.
func (idx *Index) addToPacks(id restic.ID) int {
idx.packs = append(idx.packs, id)
return len(idx.packs) - 1
}
2020-06-12 08:17:14 +02:00
const maxuint32 = 1<<32 - 1
func (idx *Index) store(packIndex int, blob restic.Blob) {
2020-06-12 08:17:14 +02:00
// assert that offset and length fit into uint32!
if blob.Offset > maxuint32 || blob.Length > maxuint32 {
panic("offset or length does not fit in uint32. You have packs > 4GB!")
}
newEntry := indexEntry{
packIndex: packIndex,
offset: uint32(blob.Offset),
length: uint32(blob.Length),
}
2016-08-31 20:58:57 +02:00
h := restic.BlobHandle{ID: blob.ID, Type: blob.Type}
if _, ok := idx.blob[h]; ok {
idx.duplicates[h] = append(idx.duplicates[h], newEntry)
} else {
idx.blob[h] = newEntry
}
2015-04-26 17:10:31 +02:00
}
2015-10-12 22:34:12 +02:00
// Final returns true iff the index is already written to the repository, it is
// finalized.
func (idx *Index) Final() bool {
idx.m.Lock()
defer idx.m.Unlock()
return idx.final
}
2015-10-12 23:59:17 +02:00
const (
2020-05-24 09:19:42 +02:00
indexMaxBlobs = 50000
indexMaxAge = 10 * time.Minute
2015-10-12 23:59:17 +02:00
)
// IndexFull returns true iff the index is "full enough" to be saved as a preliminary index.
var IndexFull = func(idx *Index) bool {
2015-10-12 23:59:17 +02:00
idx.m.Lock()
defer idx.m.Unlock()
2016-09-27 22:35:08 +02:00
debug.Log("checking whether index %p is full", idx)
2015-10-12 23:59:17 +02:00
blobs := len(idx.blob)
2015-10-12 23:59:17 +02:00
age := time.Now().Sub(idx.created)
2020-05-24 09:19:42 +02:00
switch {
case age >= indexMaxAge:
2016-09-27 22:35:08 +02:00
debug.Log("index %p is old enough", idx, age)
2015-10-12 23:59:17 +02:00
return true
2020-05-24 09:19:42 +02:00
case blobs >= indexMaxBlobs:
debug.Log("index %p has %d blobs", idx, blobs)
2015-10-12 23:59:17 +02:00
return true
}
2020-05-24 09:19:42 +02:00
debug.Log("index %p only has %d blobs and is too young (%v)", idx, blobs, age)
2015-10-12 23:59:17 +02:00
return false
2020-05-24 09:19:42 +02:00
2015-10-12 23:59:17 +02:00
}
// Store remembers the id and pack in the index.
2016-08-31 22:39:36 +02:00
func (idx *Index) Store(blob restic.PackedBlob) {
2015-04-26 17:10:31 +02:00
idx.m.Lock()
defer idx.m.Unlock()
2015-10-12 22:34:12 +02:00
if idx.final {
panic("store new item in finalized index")
}
2016-09-27 22:35:08 +02:00
debug.Log("%v", blob)
2015-04-26 17:10:31 +02:00
idx.store(idx.addToPacks(blob.PackID), blob.Blob)
2015-04-26 17:10:31 +02:00
}
// StorePack remembers the ids of all blobs of a given pack
// in the index
func (idx *Index) StorePack(id restic.ID, blobs []restic.Blob) {
idx.m.Lock()
defer idx.m.Unlock()
if idx.final {
panic("store new item in finalized index")
}
debug.Log("%v", blobs)
packIndex := idx.addToPacks(id)
for _, blob := range blobs {
idx.store(packIndex, blob)
}
}
2020-02-08 20:51:50 +01:00
// ListPack returns a list of blobs contained in a pack.
func (idx *Index) indexEntryToPackedBlob(h restic.BlobHandle, entry indexEntry) restic.PackedBlob {
2020-02-08 20:51:50 +01:00
return restic.PackedBlob{
Blob: restic.Blob{
ID: h.ID,
Type: h.Type,
2020-06-12 08:17:14 +02:00
Length: uint(entry.length),
Offset: uint(entry.offset),
2020-02-08 20:51:50 +01:00
},
PackID: idx.packs[entry.packIndex],
2020-02-08 20:51:50 +01:00
}
}
2016-08-31 22:39:36 +02:00
// Lookup queries the index for the blob ID and returns a restic.PackedBlob.
func (idx *Index) Lookup(id restic.ID, tpe restic.BlobType) (blobs []restic.PackedBlob, found bool) {
2015-04-26 17:10:31 +02:00
idx.m.Lock()
defer idx.m.Unlock()
2016-08-31 20:58:57 +02:00
h := restic.BlobHandle{ID: id, Type: tpe}
blob, ok := idx.blob[h]
if ok {
blobList := idx.withDuplicates(h, blob)
blobs = make([]restic.PackedBlob, 0, len(blobList))
for _, p := range blobList {
blobs = append(blobs, idx.indexEntryToPackedBlob(h, p))
}
return blobs, true
2015-04-26 17:10:31 +02:00
}
return nil, false
2015-04-26 17:10:31 +02:00
}
2015-11-01 22:32:28 +01:00
// ListPack returns a list of blobs contained in a pack.
2016-08-31 22:39:36 +02:00
func (idx *Index) ListPack(id restic.ID) (list []restic.PackedBlob) {
2015-11-01 22:32:28 +01:00
idx.m.Lock()
defer idx.m.Unlock()
for h, entry := range idx.blob {
for _, blob := range idx.withDuplicates(h, entry) {
if idx.packs[blob.packIndex] == id {
list = append(list, idx.indexEntryToPackedBlob(h, blob))
2016-08-03 20:03:52 +02:00
}
2015-11-01 22:32:28 +01:00
}
}
return list
}
2015-04-26 17:10:31 +02:00
// Has returns true iff the id is listed in the index.
2016-08-31 20:58:57 +02:00
func (idx *Index) Has(id restic.ID, tpe restic.BlobType) bool {
idx.m.Lock()
defer idx.m.Unlock()
2015-04-26 17:10:31 +02:00
h := restic.BlobHandle{ID: id, Type: tpe}
_, ok := idx.blob[h]
return ok
2015-04-26 17:10:31 +02:00
}
// LookupSize returns the length of the plaintext content of the blob with the
// given id.
func (idx *Index) LookupSize(id restic.ID, tpe restic.BlobType) (plaintextLength uint, found bool) {
blobs, found := idx.Lookup(id, tpe)
if !found {
return 0, found
2015-07-19 15:16:16 +02:00
}
return uint(restic.PlaintextLength(int(blobs[0].Length))), true
2015-07-19 15:16:16 +02:00
}
// Supersedes returns the list of indexes this index supersedes, if any.
2016-08-31 20:29:54 +02:00
func (idx *Index) Supersedes() restic.IDs {
return idx.supersedes
}
2015-10-25 17:06:56 +01:00
// AddToSupersedes adds the ids to the list of indexes superseded by this
// index. If the index has already been finalized, an error is returned.
2016-08-31 20:29:54 +02:00
func (idx *Index) AddToSupersedes(ids ...restic.ID) error {
2015-10-25 17:06:56 +01:00
idx.m.Lock()
defer idx.m.Unlock()
if idx.final {
return errors.New("index already finalized")
}
idx.supersedes = append(idx.supersedes, ids...)
return nil
}
2017-06-18 14:45:02 +02:00
// Each returns a channel that yields all blobs known to the index. When the
// context is cancelled, the background goroutine terminates. This blocks any
// modification of the index.
func (idx *Index) Each(ctx context.Context) <-chan restic.PackedBlob {
2015-04-26 17:10:31 +02:00
idx.m.Lock()
2016-08-31 22:39:36 +02:00
ch := make(chan restic.PackedBlob)
2015-04-26 17:10:31 +02:00
go func() {
defer idx.m.Unlock()
defer func() {
close(ch)
}()
for h, entry := range idx.blob {
for _, blob := range idx.withDuplicates(h, entry) {
2016-08-03 20:03:52 +02:00
select {
2017-06-18 14:45:02 +02:00
case <-ctx.Done():
2016-08-03 20:03:52 +02:00
return
case ch <- idx.indexEntryToPackedBlob(h, blob):
2016-08-03 20:03:52 +02:00
}
2015-04-26 17:10:31 +02:00
}
}
}()
return ch
}
2015-10-25 15:28:01 +01:00
// Packs returns all packs in this index
2016-08-31 20:29:54 +02:00
func (idx *Index) Packs() restic.IDSet {
2015-10-25 15:28:01 +01:00
idx.m.Lock()
defer idx.m.Unlock()
2016-08-31 20:29:54 +02:00
packs := restic.NewIDSet()
for _, packID := range idx.packs {
packs.Insert(packID)
2015-10-25 15:28:01 +01:00
}
return packs
}
// Count returns the number of blobs of type t in the index.
2016-08-31 20:58:57 +02:00
func (idx *Index) Count(t restic.BlobType) (n uint) {
2016-09-27 22:35:08 +02:00
debug.Log("counting blobs of type %v", t)
idx.m.Lock()
defer idx.m.Unlock()
for h := range idx.blob {
if h.Type != t {
continue
}
n++
}
for h, dups := range idx.duplicates {
if h.Type != t {
continue
}
n += uint(len(dups))
}
return
}
2015-04-26 17:10:31 +02:00
type packJSON struct {
2016-08-31 20:29:54 +02:00
ID restic.ID `json:"id"`
2015-04-26 17:10:31 +02:00
Blobs []blobJSON `json:"blobs"`
}
type blobJSON struct {
2016-08-31 20:58:57 +02:00
ID restic.ID `json:"id"`
Type restic.BlobType `json:"type"`
Offset uint `json:"offset"`
Length uint `json:"length"`
2015-04-26 17:10:31 +02:00
}
// generatePackList returns a list of packs.
func (idx *Index) generatePackList() ([]*packJSON, error) {
2015-04-26 17:10:31 +02:00
list := []*packJSON{}
2016-08-31 20:29:54 +02:00
packs := make(map[restic.ID]*packJSON)
2015-04-26 17:10:31 +02:00
for h, entry := range idx.blob {
for _, blob := range idx.withDuplicates(h, entry) {
packID := idx.packs[blob.packIndex]
if packID.IsNull() {
2016-08-03 20:03:52 +02:00
panic("null pack id")
}
2016-09-27 22:35:08 +02:00
debug.Log("handle blob %v", h)
2016-08-03 20:03:52 +02:00
// see if pack is already in map
p, ok := packs[packID]
2016-08-03 20:03:52 +02:00
if !ok {
// else create new pack
p = &packJSON{ID: packID}
2015-04-26 17:10:31 +02:00
2016-08-03 20:03:52 +02:00
// and append it to the list and map
list = append(list, p)
packs[p.ID] = p
}
2015-04-26 17:10:31 +02:00
2016-08-03 20:03:52 +02:00
// add blob
p.Blobs = append(p.Blobs, blobJSON{
ID: h.ID,
Type: h.Type,
2020-06-12 08:17:14 +02:00
Offset: uint(blob.offset),
Length: uint(blob.length),
2016-08-03 20:03:52 +02:00
})
}
2015-04-26 17:10:31 +02:00
}
2016-09-27 22:35:08 +02:00
debug.Log("done")
2015-05-16 14:05:19 +02:00
return list, nil
}
2015-07-26 00:40:00 +02:00
type jsonIndex struct {
2016-08-31 20:29:54 +02:00
Supersedes restic.IDs `json:"supersedes,omitempty"`
Packs []*packJSON `json:"packs"`
2015-07-26 00:40:00 +02:00
}
2015-10-12 22:34:12 +02:00
// Encode writes the JSON serialization of the index to the writer w.
func (idx *Index) Encode(w io.Writer) error {
2016-09-27 22:35:08 +02:00
debug.Log("encoding index")
2015-10-12 22:34:12 +02:00
idx.m.Lock()
defer idx.m.Unlock()
return idx.encode(w)
}
// encode writes the JSON serialization of the index to the writer w.
func (idx *Index) encode(w io.Writer) error {
2016-09-27 22:35:08 +02:00
debug.Log("encoding index")
2015-10-12 22:34:12 +02:00
list, err := idx.generatePackList()
2015-05-16 14:05:19 +02:00
if err != nil {
return err
}
2015-04-26 17:10:31 +02:00
enc := json.NewEncoder(w)
2015-07-26 00:40:00 +02:00
idxJSON := jsonIndex{
2015-10-12 22:34:12 +02:00
Supersedes: idx.supersedes,
2015-07-26 00:40:00 +02:00
Packs: list,
}
return enc.Encode(idxJSON)
2015-04-26 17:10:31 +02:00
}
// Finalize sets the index to final.
func (idx *Index) Finalize() {
debug.Log("finalizing index")
2015-05-16 14:05:19 +02:00
idx.m.Lock()
defer idx.m.Unlock()
2015-10-12 22:34:12 +02:00
idx.final = true
2015-05-16 14:05:19 +02:00
}
2015-11-02 18:51:45 +01:00
// ID returns the ID of the index, if available. If the index is not yet
// finalized, an error is returned.
2016-08-31 20:29:54 +02:00
func (idx *Index) ID() (restic.ID, error) {
2015-11-02 18:51:45 +01:00
idx.m.Lock()
defer idx.m.Unlock()
if !idx.final {
2016-08-31 20:29:54 +02:00
return restic.ID{}, errors.New("index not finalized")
2015-11-02 18:51:45 +01:00
}
return idx.id, nil
}
// SetID sets the ID the index has been written to. This requires that
// Finalize() has been called before, otherwise an error is returned.
2016-08-31 20:29:54 +02:00
func (idx *Index) SetID(id restic.ID) error {
2015-11-02 18:51:45 +01:00
idx.m.Lock()
defer idx.m.Unlock()
if !idx.final {
2017-02-09 00:43:10 +01:00
return errors.New("index is not final")
2015-11-02 18:51:45 +01:00
}
if !idx.id.IsNull() {
return errors.New("ID already set")
}
2018-01-25 20:49:41 +01:00
debug.Log("ID set to %v", id)
2015-11-02 18:51:45 +01:00
idx.id = id
return nil
}
2015-05-16 14:05:19 +02:00
// Dump writes the pretty-printed JSON representation of the index to w.
func (idx *Index) Dump(w io.Writer) error {
2016-09-27 22:35:08 +02:00
debug.Log("dumping index")
2015-05-16 14:05:19 +02:00
idx.m.Lock()
defer idx.m.Unlock()
list, err := idx.generatePackList()
2015-05-16 14:05:19 +02:00
if err != nil {
return err
}
2015-11-02 19:28:30 +01:00
outer := jsonIndex{
Supersedes: idx.Supersedes(),
Packs: list,
}
buf, err := json.MarshalIndent(outer, "", " ")
2015-05-16 14:05:19 +02:00
if err != nil {
return err
}
_, err = w.Write(append(buf, '\n'))
if err != nil {
2016-08-29 22:16:58 +02:00
return errors.Wrap(err, "Write")
2015-05-16 14:05:19 +02:00
}
2016-09-27 22:35:08 +02:00
debug.Log("done")
2015-05-16 14:05:19 +02:00
return nil
}
2017-09-23 14:47:40 +02:00
// TreePacks returns a list of packs that contain only tree blobs.
func (idx *Index) TreePacks() restic.IDs {
return idx.treePacks
}
2015-07-26 21:58:03 +02:00
// isErrOldIndex returns true if the error may be caused by an old index
// format.
func isErrOldIndex(err error) bool {
if e, ok := err.(*json.UnmarshalTypeError); ok && e.Value == "array" {
return true
}
return false
}
// ErrOldIndexFormat means an index with the old format was detected.
var ErrOldIndexFormat = errors.New("index has old format")
2015-04-26 17:10:31 +02:00
// DecodeIndex loads and unserializes an index from rd.
func DecodeIndex(buf []byte) (idx *Index, err error) {
2016-09-27 22:35:08 +02:00
debug.Log("Start decoding index")
idxJSON := &jsonIndex{}
2015-07-26 00:40:00 +02:00
err = json.Unmarshal(buf, idxJSON)
2015-07-26 00:40:00 +02:00
if err != nil {
2016-09-27 22:35:08 +02:00
debug.Log("Error %v", err)
2015-07-26 21:58:03 +02:00
if isErrOldIndex(err) {
2016-09-27 22:35:08 +02:00
debug.Log("index is probably old format, trying that")
2015-07-26 21:58:03 +02:00
err = ErrOldIndexFormat
}
2016-08-29 22:16:58 +02:00
return nil, errors.Wrap(err, "Decode")
2015-07-26 00:40:00 +02:00
}
idx = NewIndex()
2015-07-26 00:40:00 +02:00
for _, pack := range idxJSON.Packs {
2017-09-23 14:47:40 +02:00
var data, tree bool
packID := idx.addToPacks(pack.ID)
2017-09-23 14:47:40 +02:00
2015-07-26 00:40:00 +02:00
for _, blob := range pack.Blobs {
idx.store(packID, restic.Blob{
Type: blob.Type,
ID: blob.ID,
Offset: blob.Offset,
Length: blob.Length,
})
2017-09-23 14:47:40 +02:00
switch blob.Type {
case restic.DataBlob:
data = true
case restic.TreeBlob:
tree = true
}
}
if !data && tree {
idx.treePacks = append(idx.treePacks, pack.ID)
2015-07-26 00:40:00 +02:00
}
}
idx.supersedes = idxJSON.Supersedes
2015-10-12 22:34:12 +02:00
idx.final = true
2015-07-26 00:40:00 +02:00
2016-09-27 22:35:08 +02:00
debug.Log("done")
2016-08-29 22:16:58 +02:00
return idx, nil
2015-07-26 00:40:00 +02:00
}
// DecodeOldIndex loads and unserializes an index in the old format from rd.
func DecodeOldIndex(buf []byte) (idx *Index, err error) {
2016-09-27 22:35:08 +02:00
debug.Log("Start decoding old index")
2015-04-26 17:10:31 +02:00
list := []*packJSON{}
err = json.Unmarshal(buf, &list)
2015-04-26 17:10:31 +02:00
if err != nil {
2016-09-27 22:35:08 +02:00
debug.Log("Error %#v", err)
2016-08-29 22:16:58 +02:00
return nil, errors.Wrap(err, "Decode")
2015-04-26 17:10:31 +02:00
}
idx = NewIndex()
2015-04-26 17:10:31 +02:00
for _, pack := range list {
2017-09-23 14:47:40 +02:00
var data, tree bool
packID := idx.addToPacks(pack.ID)
2017-09-23 14:47:40 +02:00
2015-04-26 17:10:31 +02:00
for _, blob := range pack.Blobs {
idx.store(packID, restic.Blob{
Type: blob.Type,
ID: blob.ID,
Offset: blob.Offset,
Length: blob.Length,
})
2017-09-23 14:47:40 +02:00
switch blob.Type {
case restic.DataBlob:
data = true
case restic.TreeBlob:
tree = true
}
}
if !data && tree {
idx.treePacks = append(idx.treePacks, pack.ID)
2015-04-26 17:10:31 +02:00
}
}
idx.final = true
2015-04-26 17:10:31 +02:00
2016-09-27 22:35:08 +02:00
debug.Log("done")
2016-08-29 22:16:58 +02:00
return idx, nil
}
2015-11-02 18:51:24 +01:00
// LoadIndexWithDecoder loads the index and decodes it with fn.
2019-03-24 22:12:38 +01:00
func LoadIndexWithDecoder(ctx context.Context, repo restic.Repository, buf []byte, id restic.ID, fn func([]byte) (*Index, error)) (*Index, []byte, error) {
2018-01-25 20:49:41 +01:00
debug.Log("Loading index %v", id)
2015-11-02 18:51:24 +01:00
2019-03-24 22:12:38 +01:00
buf, err := repo.LoadAndDecrypt(ctx, buf[:0], restic.IndexFile, id)
2015-11-02 18:51:24 +01:00
if err != nil {
2019-03-24 22:12:38 +01:00
return nil, buf[:0], err
2015-11-02 18:51:24 +01:00
}
2019-03-24 22:12:38 +01:00
idx, err := fn(buf)
2015-11-02 18:51:24 +01:00
if err != nil {
2016-09-27 22:35:08 +02:00
debug.Log("error while decoding index %v: %v", id, err)
2019-03-24 22:12:38 +01:00
return nil, buf[:0], err
2015-11-02 18:51:24 +01:00
}
idx.id = id
2015-11-02 18:51:24 +01:00
2019-03-24 22:12:38 +01:00
return idx, buf, nil
2015-11-02 18:51:24 +01:00
}