/
rsmap.go
429 lines (373 loc) · 12.3 KB
/
rsmap.go
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
package rsmap
import (
"context"
"errors"
"fmt"
"net/http"
"os"
"path/filepath"
"runtime"
"strconv"
"sync"
"time"
"github.com/lestrrat-go/backoff/v2"
"github.com/lestrrat-go/option"
"go.etcd.io/bbolt"
logsv1 "github.com/daichitakahashi/rsmap/internal/proto/logs/v1"
resource_mapv1 "github.com/daichitakahashi/rsmap/internal/proto/resource_map/v1"
"github.com/daichitakahashi/rsmap/logs"
)
type (
// Map is the controller for external resource usage.
Map struct {
_callers logs.CallerContext
_cfg config
_mu sync.RWMutex
_rm resourceMap
_stop func()
}
// Resource brings an ability of acquire/release lock for the dedicated resource.
Resource struct {
_callers logs.CallerContext
_m *Map
_max int64
_name string
}
)
type (
NewOption struct {
option.Interface
}
identOptionRetryPolicy struct{}
identOptionHTTPClient struct{}
)
// WithRetryPolicy specifies a retry policy of each operations(resource initializations, lock acquisitions).
// For example, interval, exponential-backoff and max retry.
// For detailed settings, see [backoff.NewExponentialPolicy] or [backoff.NewConstantPolicy].
func WithRetryPolicy(p backoff.Policy) *NewOption {
return &NewOption{
Interface: option.New(identOptionRetryPolicy{}, p),
}
}
// WithHTTPClient specifies [http.Client] used for communication with server process.
// If your process launches server, this client may not be used.
func WithHTTPClient(c *http.Client) *NewOption {
return &NewOption{
Interface: option.New(identOptionHTTPClient{}, c),
}
}
const (
EnvExecutionID = "RSMAP_EXECUTION_ID"
)
func logsDir(base string) (string, error) {
if !filepath.IsAbs(base) {
wd, err := os.Getwd()
if err != nil {
return "", err
}
base = filepath.Join(wd, base)
}
// Get execution ID.
executionID := strconv.Itoa(os.Getppid()) // The process of `go test`
if id, ok := os.LookupEnv(EnvExecutionID); ok {
executionID = id
}
dir := filepath.Join(base, executionID)
// Check logs.db and addr not exists as directory.
logsFilename := filepath.Join(dir, "logs.db")
info, err := os.Stat(logsFilename)
if err == nil && info.IsDir() {
return "", fmt.Errorf("logs.db already exists as a directory: %s", logsFilename)
}
addrFilename := filepath.Join(dir, "addr")
info, err = os.Stat(addrFilename)
if err == nil && info.IsDir() {
return "", fmt.Errorf("addr already exists as a directory: %s", addrFilename)
}
return dir, nil
}
// New creates an instance of [Map] that enables us to reuse external resources with thread safety.
// Most common use-case is Go's parallelized testing of multiple packages (`go test -p=N ./...`.)
//
// Map has server mode and client mode.
// If Map has initialized in server mode, it creates database `logs.db` and write server address to `addr` under `${rsmapDir}/${executionID}/`.
// Other Map reads address of the server, and requests the server to acquire locks.
// So, every Go packages(directories) must specify same location as an argument. Otherwise, we cannot provide correct control.
// It's user's responsibility.
//
// `executionID` is the identifier of the execution of `go test`. In default, we use the value of [os.Getppid()].
// If you want to specify the id explicitly, set the value to `RSMAP_EXECUTION_ID` environment variable.
//
// In almost cases, following code can be helpful.
//
// p, _ := exec.Command("go", "mod", "GOMOD").Output() // Get file path of "go.mod".
// m, _ := rsmap.New(filepath.Join(filepath.Dir(strings.TrimSpace(string(p))), ".rsmap"))
func New(rsmapDir string, opts ...*NewOption) (*Map, error) {
_, file, line, _ := runtime.Caller(1)
var callers logs.CallerContext
callers = callers.Append(file, line)
// Get directory for `logs.db` and `addr`.
dir, err := logsDir(rsmapDir)
if err != nil {
return nil, err
}
// Create directory if not exists.
if err = os.MkdirAll(dir, 0755); err != nil {
return nil, fmt.Errorf("rsmap: failed to prepare directory(rsmapDir): %w", err)
}
cfg := config{
dbFile: filepath.Join(dir, "logs.db"),
addrFile: filepath.Join(dir, "addr"),
retryPolicy: backoff.NewConstantPolicy(
// FIXME: Reconsider default policy.
backoff.WithMaxRetries(200),
backoff.WithInterval(time.Millisecond*200),
),
httpCli: &http.Client{},
}
// Apply options.
for _, opt := range opts {
switch opt.Ident() {
case identOptionRetryPolicy{}:
cfg.retryPolicy = opt.Value().(backoff.Policy)
case identOptionHTTPClient{}:
cfg.httpCli = opt.Value().(*http.Client)
}
}
m := &Map{
_callers: callers,
_cfg: cfg,
_rm: newClientSideMap(cfg),
}
// Start server launch process, and set release function.
m._stop = m.launchServer(dir, m._callers)
return m, nil
}
func (m *Map) Close() {
m._stop()
}
func (m *Map) resourceMap() resourceMap {
m._mu.RLock()
defer m._mu.RUnlock()
return m._rm
}
type (
// ResourceOption represents option for [Resource]
ResourceOption struct {
option.Interface
}
identOptionParallelism struct{}
identOptionInit struct{}
)
// WithMaxParallelism specifies max parallelism of the resource usage.
func WithMaxParallelism(n int64) *ResourceOption {
return &ResourceOption{
Interface: option.New(identOptionParallelism{}, n),
}
}
type InitFunc func(ctx context.Context) error
// WithInit specifies InitFunc for resource initialization.
//
// InitFunc will be called only once globally, at first declaration by [Resource].
// Other process waits until the completion of this initialization.
// So, if Resource is called without this option, we cannot perform initializations with concurrency safety.
func WithInit(init InitFunc) *ResourceOption {
return &ResourceOption{
Interface: option.New(identOptionInit{}, init),
}
}
// Resource creates [Resource] object that provides control for resource usage.
//
// Resource has a setting for max parallelism, you can specify the value by [WithMaxParallelism](default value is 5.)
// And you want to perform an initialization of the resource, use [WithInit].
func (m *Map) Resource(ctx context.Context, name string, opts ...*ResourceOption) (*Resource, error) {
_, file, line, _ := runtime.Caller(1)
callers := m._callers.Append(file, line)
var (
n = int64(5)
init InitFunc = func(ctx context.Context) error {
// Do nothing.
return nil
}
)
// Apply options.
for _, opt := range opts {
switch opt.Ident() {
case identOptionParallelism{}:
n = opt.Value().(int64)
case identOptionInit{}:
init = opt.Value().(InitFunc)
}
}
m._mu.RLock()
rm := m._rm
m._mu.RUnlock()
try, err := rm.tryInit(ctx, name, callers)
if err != nil {
return nil, err
}
if try {
// Initialization of the resource.
err = func() (err error) {
var notPanicked bool
defer func() {
m._mu.RLock()
rm := m._rm
m._mu.RUnlock()
// If init succeeds, mark as complete.
if notPanicked && err == nil {
err = rm.completeInit(ctx, name, callers)
return
}
// Mark as failed when error or panic occurred.
// CAUTION: Do not recover panic to preserve stacktrace.
err = errors.Join(
err,
rm.failInit(ctx, name, callers),
)
}()
err = init(ctx)
notPanicked = true
return
}()
if err != nil {
return nil, err
}
}
return &Resource{
_callers: callers,
_m: m,
_max: n,
_name: name,
}, nil
}
// RLock acquires shared lock of the Resource.
// The instance of Resource can acquire only one lock.
// Consecutive acquisition without unlock doesn't fail, but do nothing.
//
// To release lock, use [UnlockAny].
func (r *Resource) RLock(ctx context.Context) error {
return r._m.resourceMap().acquire(ctx, r._name, r._callers, r._max, false)
}
// Lock acquires exclusive lock of the Resource.
// The instance of Resource can acquire only one lock.
// Consecutive acquisition without unlock doesn't fail, but do nothing.
//
// To release lock, use [UnlockAny].
func (r *Resource) Lock(ctx context.Context) error {
return r._m.resourceMap().acquire(ctx, r._name, r._callers, r._max, true)
}
// UnlockAny releases acquired shared/exclusive lock by the Resource.
func (r *Resource) UnlockAny() error {
return r._m.resourceMap().release(context.Background(), r._name, r._callers)
}
type ResourceLocker struct {
_r *Resource
_exclusive bool
}
// EXclusive returns ResourceLocker for Resource.
func (r *Resource) Exclusive() *ResourceLocker {
return &ResourceLocker{
_r: r,
_exclusive: true,
}
}
// Shared returns ResourceLocker for Resource.
func (r *Resource) Shared() *ResourceLocker {
return &ResourceLocker{
_r: r,
_exclusive: false,
}
}
// LockResources acquires exclusive/shared locks for multiple resources.
// Returned function releases all locks acquired.
func LockResources(ctx context.Context, resources ...*ResourceLocker) (func() error, error) {
var m resourceMap
acquireEntries := make([]*resource_mapv1.AcquireMultiEntry, 0, len(resources))
releaseEntries := make([]*resource_mapv1.ReleaseMultiEntry, 0, len(resources))
for _, r := range resources {
mm := r._r._m.resourceMap()
if m == nil {
m = mm
} else if m != mm {
return nil, errors.New("rsmap: all ResourceLocker must be derived from same Map")
}
acquireEntries = append(acquireEntries, &resource_mapv1.AcquireMultiEntry{
ResourceName: r._r._name,
Context: r._r._callers,
MaxParallelism: r._r._max,
Exclusive: r._exclusive,
})
releaseEntries = append(releaseEntries, &resource_mapv1.ReleaseMultiEntry{
ResourceName: r._r._name,
Context: r._r._callers,
})
}
err := m.acquireMulti(ctx, acquireEntries)
if err != nil {
return nil, err
}
return func() error {
return m.releaseMulti(context.Background(), releaseEntries)
}, nil
}
// Core interface for control operations for both server and client side.
type resourceMap interface {
tryInit(ctx context.Context, resourceName string, operator logs.CallerContext) (bool, error)
completeInit(ctx context.Context, resourceName string, operator logs.CallerContext) error
failInit(ctx context.Context, resourceName string, operator logs.CallerContext) error
acquire(ctx context.Context, resourceName string, operator logs.CallerContext, max int64, exclusive bool) error
acquireMulti(ctx context.Context, resources []*resource_mapv1.AcquireMultiEntry) error
release(ctx context.Context, resourceName string, operator logs.CallerContext) error
releaseMulti(ctx context.Context, resources []*resource_mapv1.ReleaseMultiEntry) error
}
type serverSideMap struct {
_init *initController
_acquire *acquireController
}
// Create resourceMap for server side.
// This map reads and updates bbolt.DB directly.
func newServerSideMap(db *bbolt.DB, closing <-chan struct{}) (*serverSideMap, error) {
initRecordStore, err := logs.NewResourceRecordStore[logsv1.InitRecord](db)
if err != nil {
return nil, err
}
acquireRecordStore, err := logs.NewResourceRecordStore[logsv1.AcquisitionRecord](db)
if err != nil {
return nil, err
}
init, err := loadInitController(initRecordStore, closing)
if err != nil {
return nil, err
}
acquire, err := loadAcquireController(acquireRecordStore, time.Second*2, closing)
if err != nil {
return nil, err
}
return &serverSideMap{
_init: init,
_acquire: acquire,
}, nil
}
func (m *serverSideMap) tryInit(ctx context.Context, resourceName string, operator logs.CallerContext) (bool, error) {
return m._init.tryInit(ctx, resourceName, operator)
}
func (m *serverSideMap) completeInit(_ context.Context, resourceName string, operator logs.CallerContext) error {
return m._init.complete(resourceName, operator)
}
func (m *serverSideMap) failInit(_ context.Context, resourceName string, operator logs.CallerContext) error {
return m._init.fail(resourceName, operator)
}
func (m *serverSideMap) acquire(ctx context.Context, resourceName string, operator logs.CallerContext, max int64, exclusive bool) error {
return m._acquire.acquire(ctx, resourceName, operator, max, exclusive)
}
func (m *serverSideMap) acquireMulti(ctx context.Context, resources []*resource_mapv1.AcquireMultiEntry) error {
return m._acquire.acquireMulti(ctx, resources)
}
func (m *serverSideMap) release(_ context.Context, resourceName string, operator logs.CallerContext) error {
return m._acquire.release(resourceName, operator)
}
func (m *serverSideMap) releaseMulti(_ context.Context, resources []*resource_mapv1.ReleaseMultiEntry) error {
return m._acquire.releaseMulti(resources)
}
var _ resourceMap = (*serverSideMap)(nil)