Welcome to mirror list, hosted at ThFree Co, Russian Federation.

generation_local.go « datastore « praefect « internal - gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c9985d14aa2eeac625915496bfa9d81386768574 (plain)
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package datastore

import (
	"context"
	"sync"
)

// LocalGenerationStore is an in-memory implementation of GenerationStore.
// Refer to the interface for method documentation.
type LocalGenerationStore struct {
	m sync.Mutex

	storages     map[string][]string
	generations  map[string]map[string]map[string]int
	repositories map[string]map[string]int
}

// NewLocalGenerationStore returns an in-memory implementation of GenerationStore.
func NewLocalGenerationStore(storages map[string][]string) *LocalGenerationStore {
	return &LocalGenerationStore{
		storages:     storages,
		generations:  make(map[string]map[string]map[string]int),
		repositories: make(map[string]map[string]int),
	}
}

func (l *LocalGenerationStore) IncrementGeneration(ctx context.Context, virtualStorage, storage, relativePath string) (int, error) {
	l.m.Lock()
	defer l.m.Unlock()

	nextGen := l.latestGeneration(virtualStorage, relativePath) + 1
	l.setGeneration(virtualStorage, relativePath, storage, nextGen)

	return nextGen, nil
}

func (l *LocalGenerationStore) SetGeneration(ctx context.Context, virtualStorage, storage, relativePath string, generation int) error {
	l.m.Lock()
	defer l.m.Unlock()

	l.setGeneration(virtualStorage, relativePath, storage, generation)

	return nil
}

func (l *LocalGenerationStore) DeleteRecord(ctx context.Context, virtualStorage, storage, relativePath string) error {
	l.m.Lock()
	defer l.m.Unlock()

	vs, ok := l.generations[virtualStorage]
	if !ok {
		return nil
	}

	rel, ok := vs[relativePath]
	if !ok {
		return nil
	}

	delete(rel, storage)

	return nil
}

func (l *LocalGenerationStore) EnsureUpgrade(ctx context.Context, virtualStorage, storage, relativePath string, generation int) error {
	l.m.Lock()
	defer l.m.Unlock()

	if current := l.getGeneration(virtualStorage, relativePath, storage); current != GenerationUnknown && current >= generation {
		return errDowngradeAttempted
	}

	return nil
}

func (l *LocalGenerationStore) GetOutdatedRepositories(ctx context.Context, virtualStorage string) (map[string]map[string]int, error) {
	storages, ok := l.storages[virtualStorage]
	if !ok {
		return nil, errUnknownVirtualStorage
	}

	outdatedRepos := make(map[string]map[string]int)
	repositories, ok := l.repositories[virtualStorage]
	if !ok {
		return outdatedRepos, nil
	}

	for relativePath, latestGeneration := range repositories {
		for _, storage := range storages {
			if gen := l.getGeneration(virtualStorage, relativePath, storage); gen < latestGeneration {
				if outdatedRepos[relativePath] == nil {
					outdatedRepos[relativePath] = make(map[string]int)
				}

				outdatedRepos[relativePath][storage] = latestGeneration - gen
			}
		}
	}

	return outdatedRepos, nil
}

func (l *LocalGenerationStore) latestGeneration(virtualStorage, relativePath string) int {
	vs := l.repositories[virtualStorage]
	if vs == nil {
		return GenerationUnknown
	}

	if latest, ok := vs[relativePath]; ok {
		return latest
	}

	return GenerationUnknown
}

func (l *LocalGenerationStore) getGeneration(virtualStorage, relativePath, storage string) int {
	vs := l.generations[virtualStorage]
	if vs == nil {
		return GenerationUnknown
	}

	rel := vs[relativePath]
	if rel == nil {
		return GenerationUnknown
	}

	if gen, ok := rel[storage]; ok {
		return gen
	}

	return GenerationUnknown
}

func (l *LocalGenerationStore) setGeneration(virtualStorage, relativePath, storage string, generation int) {
	if generation > l.latestGeneration(virtualStorage, relativePath) {
		if l.repositories[virtualStorage] == nil {
			l.repositories[virtualStorage] = make(map[string]int)
		}
		l.repositories[virtualStorage][relativePath] = generation
	}

	vs := l.generations[virtualStorage]
	if vs == nil {
		l.generations[virtualStorage] = map[string]map[string]int{
			relativePath: {
				storage: generation,
			},
		}

		return
	}

	rel := vs[relativePath]
	if rel == nil {
		vs[relativePath] = map[string]int{
			storage: generation,
		}

		return
	}

	rel[storage] = generation
}