/
writecontroller.go
116 lines (92 loc) · 2.9 KB
/
writecontroller.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
//
// Copyright 2019 Insolar Technologies GmbH
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package executor
import (
"context"
"fmt"
"sync"
"github.com/insolar/insolar/insolar"
)
//go:generate minimock -i github.com/insolar/insolar/ledger/light/executor.WriteAccessor -o ./ -s _mock.go -g
type WriteAccessor interface {
// Begin requests writing access for pulse number. If requested pulse is closed, ErrWriteClosed will be returned.
// The caller must call returned "done" function when finished writing.
Begin(context.Context, insolar.PulseNumber) (done func(), err error)
}
//go:generate minimock -i github.com/insolar/insolar/ledger/light/executor.WriteManager -o ./ -s _mock.go -g
type WriteManager interface {
// Open marks pulse number as opened for writing. It can be used later by Begin from accessor.
Open(context.Context, insolar.PulseNumber) error
// CloseAndWait immediately marks pulse number as closed for writing and blocks until all writes are done.
CloseAndWait(context.Context, insolar.PulseNumber) error
}
type WriteController struct {
lock sync.RWMutex
current insolar.PulseNumber
closed bool
wg sync.WaitGroup
}
func NewWriteController() *WriteController {
return &WriteController{
current: 0,
closed: true,
}
}
func (m *WriteController) Begin(ctx context.Context, pulse insolar.PulseNumber) (func(), error) {
m.lock.RLock()
defer m.lock.RUnlock()
if pulse != m.current {
return nil, ErrWriteClosed
}
if m.closed {
return nil, ErrWriteClosed
}
m.wg.Add(1)
return func() { m.wg.Done() }, nil
}
func (m *WriteController) Open(ctx context.Context, pulse insolar.PulseNumber) error {
m.lock.Lock()
defer m.lock.Unlock()
if pulse < m.current {
return fmt.Errorf("can't open past pulse for writing: %v", pulse)
}
if pulse == m.current {
return fmt.Errorf("requested pulse already opened for writing: %v", pulse)
}
m.wg = sync.WaitGroup{}
m.current = pulse
m.closed = false
return nil
}
func (m *WriteController) CloseAndWait(ctx context.Context, pulse insolar.PulseNumber) error {
m.lock.Lock()
if m.current == 0 {
m.lock.Unlock()
return nil
}
if pulse != m.current {
m.lock.Unlock()
return fmt.Errorf("wrong pulse for closing: opened - %v, requested = %v", m.current, pulse)
}
if m.closed {
m.lock.Unlock()
return fmt.Errorf("requested pulse already closed for writing: %v", pulse)
}
m.closed = true
m.lock.Unlock()
m.wg.Wait()
return nil
}