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

transaction.go « transaction « praefect « internal - gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2ddff2180c35022b93d06e5f8cf66ad054510779 (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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package transaction

import (
	"context"
	"sync"
)

// Repository represents the identity and location of a repository as requested
// by the client
type Repository struct {
	ProjectHash string
	StorageLoc  string // storage location
}

// State represents the current state of a backend node
// Note: in the future this may be extended to include refs
type State struct {
	Checksum []byte
}

type RPC struct {
	// @zj how do we abstract an RPC invocation? 🤔
}

type Node interface {
	Storage() string // storage location the node hosts
	ForwardRPC(ctx context.Context, rpc *RPC) error

	CheckSum(context.Context, Repository) ([]byte, error)
	WriteRef(ctx context.Context, ref, value string) error
}

// MutateTx represents the resources available during a mutator transaction
type MutateTx interface {
	AccessTx
	// LockRef acquires a write lock on a reference
	LockRef(context.Context, string) error
	Primary() Node
}

// AccessTx represents the resources available during an accessor transaction
type AccessTx interface {
	// Replicas returns all replicas without distinguishing the primary
	Replicas() map[string]Node
	// RLockRef acquires a read lock on a reference
	RLockRef(context.Context, string) error
}

type transaction struct {
	shard *Shard

	replicas map[string]Node // only nodes verified to be consistent

	// unlocks contains callbacks to unlock all locks acquired
	unlocks struct {
		*sync.RWMutex
		m map[string]func()
	}

	// refRollbacks contains all reference values before modification
	refRollbacks map[string][]byte
}

func newTransaction(shard *Shard, good []Node) transaction {
	replicas := map[string]Node{}
	for _, node := range good {
		replicas[node.Storage()] = node
	}

	return transaction{
		shard:    shard,
		replicas: replicas,
		unlocks: struct {
			*sync.RWMutex
			m map[string]func()
		}{
			RWMutex: new(sync.RWMutex),
			m:       make(map[string]func()),
		},
	}
}

// LockRef attempts to acquire a write lock on the reference name provided.
// If the context is cancelled first, the lock acquisition will be
// aborted.
func (t transaction) LockRef(ctx context.Context, ref string) error {
	lockQ := make(chan *sync.RWMutex)

	go func() {
		t.shard.refLocks.RLock()
		l, ok := t.shard.refLocks.m[ref]
		t.shard.refLocks.RUnlock()

		if !ok {
			l = new(sync.RWMutex)
			t.shard.refLocks.Lock()
			t.shard.refLocks.m[ref] = l
			t.shard.refLocks.Unlock()
		}

		l.Lock()
		lockQ <- l
	}()

	// ensure lockQ is consumed in all code paths so that goroutine doesn't
	// stray
	select {

	case <-ctx.Done():
		l := <-lockQ
		l.Unlock()
		return ctx.Err()

	case l := <-lockQ:
		t.unlocks.Lock()
		t.unlocks.m[ref] = func() { l.Unlock() }
		t.unlocks.Unlock()

		return nil
	}
}

// unlockAll will unlock all acquired locks at the end of a transaction
func (t transaction) unlockAll() {
	// unlock all refs
	t.unlocks.RLock()
	for _, unlock := range t.unlocks.m {
		unlock()
	}
	t.unlocks.RUnlock()
}

func (t transaction) rollback(ctx context.Context) error {
	// for ref, value := range t.refRollbacks {
	//
	// }
	return nil
}

func (t transaction) Replicas() map[string]Node {
	return t.replicas
}

func (t transaction) Primary() Node {
	return t.replicas[t.shard.primary]
}

// RLockRef attempts to acquire a read lock on the reference name provided
// (ref). If the context is cancelled first, the lock acquisition will be
// aborted.
func (t transaction) RLockRef(ctx context.Context, ref string) error {
	lockQ := make(chan *sync.RWMutex)

	go func() {
		t.shard.refLocks.RLock()
		l, ok := t.shard.refLocks.m[ref]
		t.shard.refLocks.RUnlock()

		if !ok {
			l = new(sync.RWMutex)
			t.shard.refLocks.Lock()
			t.shard.refLocks.m[ref] = l
			t.shard.refLocks.Unlock()
		}

		l.RLock()
		lockQ <- l
	}()

	// ensure lockQ is consumed in all code paths so that goroutine doesn't
	// stray
	select {

	case <-ctx.Done():
		// unlock before aborting
		l := <-lockQ
		l.RUnlock()

		return ctx.Err()

	case l := <-lockQ:
		t.unlocks.Lock()
		t.unlocks.m[ref] = func() { l.RUnlock() }
		t.unlocks.Unlock()

		return nil

	}
}