-
Notifications
You must be signed in to change notification settings - Fork 7
/
counter.go
170 lines (155 loc) · 5.36 KB
/
counter.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
// Copyright 2019-present Open Networking Foundation.
//
// 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 counter
import (
"context"
"github.com/atomix/atomix-api/go/atomix/primitive/counter"
"github.com/atomix/atomix-go-framework/pkg/atomix/meta"
"github.com/atomix/atomix-go-framework/pkg/atomix/time"
"sync"
)
func init() {
registerService(func(protocol GossipProtocol) (Service, error) {
service := &counterService{
protocol: protocol,
state: &CounterState{
ObjectMeta: meta.NewTimestamped(protocol.Clock().Scheme().NewClock().Get()).Proto(),
Increments: make(map[string]int64),
Decrements: make(map[string]int64),
},
}
if err := protocol.Server().Register(&counterHandler{service}); err != nil {
return nil, err
}
return service, nil
})
}
type counterHandler struct {
service *counterService
}
func (s *counterHandler) Read(ctx context.Context) (*CounterState, error) {
s.service.mu.RLock()
defer s.service.mu.RUnlock()
return s.service.state, nil
}
func (s *counterHandler) Update(ctx context.Context, state *CounterState) error {
s.service.mu.Lock()
defer s.service.mu.Unlock()
if meta.FromProto(state.ObjectMeta).Equal(meta.FromProto(s.service.state.ObjectMeta)) {
log.Debugf("Update %s", s.service.protocol.Group().MemberID())
for memberID, delta := range state.Increments {
if delta > s.service.state.Increments[memberID] {
log.Debugf("Update %s Increments[%s] = %d", s.service.protocol.Group().MemberID(), memberID, delta)
s.service.state.Increments[memberID] = delta
}
}
for memberID, delta := range state.Decrements {
if delta > s.service.state.Decrements[memberID] {
log.Debugf("Update %s Decrements[%s] = %d", s.service.protocol.Group().MemberID(), memberID, delta)
s.service.state.Decrements[memberID] = delta
}
}
} else if meta.FromProto(state.ObjectMeta).After(meta.FromProto(s.service.state.ObjectMeta)) {
log.Debugf("Update %s state=%+v", s.service.protocol.Group().MemberID(), state)
s.service.state = state
}
return nil
}
var _ GossipHandler = &counterHandler{}
type counterService struct {
protocol GossipProtocol
state *CounterState
mu sync.RWMutex
}
func (s *counterService) Protocol() GossipProtocol {
return s.protocol
}
func (s *counterService) getValue() int64 {
var value int64
for _, delta := range s.state.Increments {
value += delta
}
for _, delta := range s.state.Decrements {
value -= delta
}
return value
}
func (s *counterService) Set(ctx context.Context, request *counter.SetRequest) (*counter.SetResponse, error) {
s.mu.Lock()
defer s.mu.Unlock()
if time.NewTimestamp(*request.Headers.Timestamp).After(time.NewTimestamp(*s.state.Timestamp)) {
s.state = &CounterState{
ObjectMeta: meta.NewTimestamped(time.NewTimestamp(*request.Headers.Timestamp)).Proto(),
Increments: make(map[string]int64),
Decrements: make(map[string]int64),
}
if request.Value > 0 {
s.state.Increments[s.protocol.Group().MemberID().String()] = request.Value
} else {
s.state.Decrements[s.protocol.Group().MemberID().String()] = request.Value * -1
}
if err := s.protocol.Group().Update(ctx, s.state); err != nil {
return nil, err
}
}
return &counter.SetResponse{
Value: s.getValue(),
}, nil
}
func (s *counterService) Get(ctx context.Context, request *counter.GetRequest) (*counter.GetResponse, error) {
s.mu.RLock()
defer s.mu.RUnlock()
var value int64
for _, delta := range s.state.Increments {
value += delta
}
for _, delta := range s.state.Decrements {
value -= delta
}
return &counter.GetResponse{
Value: value,
}, nil
}
func (s *counterService) Increment(ctx context.Context, request *counter.IncrementRequest) (*counter.IncrementResponse, error) {
s.mu.Lock()
defer s.mu.Unlock()
if request.Delta > 0 {
s.state.Increments[s.protocol.Group().MemberID().String()] = s.state.Increments[s.protocol.Group().MemberID().String()] + request.Delta
} else {
s.state.Decrements[s.protocol.Group().MemberID().String()] = s.state.Decrements[s.protocol.Group().MemberID().String()] + request.Delta
}
if err := s.protocol.Group().Update(ctx, s.state); err != nil {
return nil, err
}
return &counter.IncrementResponse{
Value: s.getValue(),
}, nil
}
func (s *counterService) Decrement(ctx context.Context, request *counter.DecrementRequest) (*counter.DecrementResponse, error) {
s.mu.Lock()
defer s.mu.Unlock()
log.Infof("Decrement %s", s.protocol.Group().MemberID())
if request.Delta > 0 {
s.state.Decrements[s.protocol.Group().MemberID().String()] = s.state.Decrements[s.protocol.Group().MemberID().String()] + request.Delta
} else {
s.state.Increments[s.protocol.Group().MemberID().String()] = s.state.Increments[s.protocol.Group().MemberID().String()] + request.Delta
}
if err := s.protocol.Group().Update(ctx, s.state); err != nil {
return nil, err
}
return &counter.DecrementResponse{
Value: s.getValue(),
}, nil
}
var _ Service = &counterService{}