forked from atomix/atomix-go-framework
/
session.go
336 lines (299 loc) · 9.35 KB
/
session.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
// 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 primitive
import (
"container/list"
"context"
api "github.com/atomix/api/proto/atomix/session"
streams "github.com/lucasbfernandes/go-framework/pkg/atomix/stream"
"github.com/lucasbfernandes/go-framework/pkg/atomix/util"
"github.com/gogo/protobuf/proto"
log "github.com/sirupsen/logrus"
"time"
)
// SessionID is a session identifier
type SessionID uint64
// Session is a service session
type Session interface {
// ID returns the session identifier
ID() SessionID
// Streams returns all open streams
Streams() []Stream
// Stream returns a stream by ID
Stream(id StreamID) Stream
// StreamsOf returns all open streams for the given operation
StreamsOf(op OperationID) []Stream
}
// newSessionManager creates a new session manager
func newSessionManager(ctx PartitionContext, timeout *time.Duration) *sessionManager {
if timeout == nil {
defaultTimeout := 30 * time.Second
timeout = &defaultTimeout
}
session := &sessionManager{
id: SessionID(ctx.Index()),
timeout: *timeout,
lastUpdated: ctx.Timestamp(),
ctx: ctx,
commandCallbacks: make(map[uint64]func()),
queryCallbacks: make(map[uint64]*list.List),
results: make(map[uint64]streams.Result),
services: make(map[ServiceID]*serviceSession),
}
util.SessionEntry(ctx.NodeID(), uint64(session.id)).
Debug("Session open")
return session
}
// sessionManager manages the ordering of request and response streams for a single client
type sessionManager struct {
id SessionID
timeout time.Duration
lastUpdated time.Time
ctx PartitionContext
commandSequence uint64
commandCallbacks map[uint64]func()
queryCallbacks map[uint64]*list.List
results map[uint64]streams.Result
services map[ServiceID]*serviceSession
}
// getService gets the service session
func (s *sessionManager) getService(id ServiceID) *serviceSession {
return s.services[id]
}
// addService adds a service session
func (s *sessionManager) addService(id ServiceID) *serviceSession {
session := newServiceSession(s, id)
s.services[id] = session
return session
}
// removeService removes a service session
func (s *sessionManager) removeService(id ServiceID) *serviceSession {
session := s.services[id]
delete(s.services, id)
return session
}
// timedOut returns a boolean indicating whether the session is timed out
func (s *sessionManager) timedOut(time time.Time) bool {
return s.lastUpdated.UnixNano() > 0 && time.Sub(s.lastUpdated) > s.timeout
}
// getResult gets a unary result
func (s *sessionManager) getUnaryResult(id uint64) (streams.Result, bool) {
result, ok := s.results[id]
return result, ok
}
// addUnaryResult adds a unary result
func (s *sessionManager) addUnaryResult(id uint64, result streams.Result) streams.Result {
bytes, err := proto.Marshal(&SessionResponse{
Response: &SessionResponse_Command{
Command: &SessionCommandResponse{
Context: &SessionResponseContext{
StreamID: uint64(id),
Index: uint64(s.ctx.Index()),
Sequence: 1,
Status: getStatus(result.Error),
Message: getMessage(result.Error),
},
Response: &ServiceCommandResponse{
Response: &ServiceCommandResponse_Operation{
Operation: &ServiceOperationResponse{
result.Value.([]byte),
},
},
},
},
},
})
result = streams.Result{
Value: bytes,
Error: err,
}
s.results[id] = result
return result
}
// scheduleQuery schedules a query to be executed after the given sequence number
func (s *sessionManager) scheduleQuery(sequenceNumber uint64, f func()) {
queries, ok := s.queryCallbacks[sequenceNumber]
if !ok {
queries = list.New()
s.queryCallbacks[sequenceNumber] = queries
}
queries.PushBack(f)
}
// scheduleCommand schedules a command to be executed at the given sequence number
func (s *sessionManager) scheduleCommand(sequenceNumber uint64, f func()) {
s.commandCallbacks[sequenceNumber] = f
}
// nextCommandSequence returns the next command sequence number for the session
func (s *sessionManager) nextCommandSequence() uint64 {
return s.commandSequence + 1
}
// completeCommand completes operations up to the given sequence number and executes commands and
// queries pending for the sequence number to be completed
func (s *sessionManager) completeCommand(sequenceNumber uint64) {
for i := s.commandSequence + 1; i <= sequenceNumber; i++ {
s.commandSequence = i
queries, ok := s.queryCallbacks[i]
if ok {
query := queries.Front()
for query != nil {
query.Value.(func())()
query = query.Next()
}
delete(s.queryCallbacks, i)
}
command, ok := s.commandCallbacks[s.nextCommandSequence()]
if ok {
command()
delete(s.commandCallbacks, i)
}
}
}
// close closes the session and completes all its streams
func (s *sessionManager) close() {
util.SessionEntry(s.ctx.NodeID(), uint64(s.id)).
Debug("Session closed")
for _, service := range s.services {
service.close()
}
}
// newServiceSession creates a new service session
func newServiceSession(session *sessionManager, service ServiceID) *serviceSession {
return &serviceSession{
sessionManager: session,
service: service,
streams: make(map[StreamID]*sessionStream),
}
}
// serviceSession manages sessions within a service
type serviceSession struct {
*sessionManager
service ServiceID
streams map[StreamID]*sessionStream
}
func (s *serviceSession) ID() SessionID {
return s.id
}
func (s *serviceSession) Streams() []Stream {
streams := make([]Stream, 0, len(s.streams))
for _, stream := range s.streams {
streams = append(streams, stream)
}
return streams
}
func (s *serviceSession) Stream(id StreamID) Stream {
return s.streams[id]
}
func (s *serviceSession) StreamsOf(op OperationID) []Stream {
streams := make([]Stream, 0, len(s.streams))
for _, stream := range s.streams {
if stream.OperationID() == op {
streams = append(streams, stream)
}
}
return streams
}
// addStream adds a stream at the given sequence number
func (s *serviceSession) addStream(id StreamID, op OperationID, outStream streams.WriteStream) Stream {
stream := &sessionStream{
id: id,
op: op,
session: s,
ctx: s.ctx,
stream: outStream,
results: list.New(),
}
s.streams[id] = stream
stream.open()
util.StreamEntry(s.ctx.NodeID(), uint64(s.ID()), uint64(id)).
Trace("Stream open")
return stream
}
// getStream returns a stream by the request sequence number
func (s *serviceSession) getStream(id StreamID) *sessionStream {
return s.streams[id]
}
// ack acknowledges response streams up to the given request sequence number
func (s *serviceSession) ack(id uint64, streams map[uint64]uint64) {
for responseID := range s.results {
if responseID > id {
continue
}
delete(s.results, responseID)
}
for streamID, stream := range s.streams {
// If the stream ID is greater than the acknowledged sequence number, skip it
if uint64(stream.ID()) > id {
continue
}
// If the stream is still held by the client, ack the stream.
// Otherwise, close the stream.
streamAck, open := streams[uint64(stream.ID())]
if open {
stream.ack(streamAck)
} else {
stream.Close()
delete(s.streams, streamID)
}
}
}
// close closes the session and completes all its streams
func (s *serviceSession) close() {
for _, stream := range s.streams {
stream.Close()
}
}
var _ Session = &serviceSession{}
// SessionServer is an implementation of SessionServiceServer for session management
type SessionServer struct {
*Server
}
// OpenSession opens a new session
func (s *SessionServer) OpenSession(ctx context.Context, request *api.OpenSessionRequest) (*api.OpenSessionResponse, error) {
log.Tracef("Received OpenSessionRequest %+v", request)
header, err := s.DoOpenSession(ctx, request.Header, request.Timeout)
if err != nil {
return nil, err
}
response := &api.OpenSessionResponse{
Header: header,
}
log.Tracef("Sending OpenSessionResponse %+v", response)
return response, nil
}
// KeepAlive keeps a session alive
func (s *SessionServer) KeepAlive(ctx context.Context, request *api.KeepAliveRequest) (*api.KeepAliveResponse, error) {
log.Tracef("Received KeepAliveRequest %+v", request)
header, err := s.DoKeepAliveSession(ctx, request.Header)
if err != nil {
return nil, err
}
response := &api.KeepAliveResponse{
Header: header,
}
log.Tracef("Sending KeepAliveResponse %+v", response)
return response, nil
}
// CloseSession closes a session
func (s *SessionServer) CloseSession(ctx context.Context, request *api.CloseSessionRequest) (*api.CloseSessionResponse, error) {
log.Tracef("Received CloseSessionRequest %+v", request)
header, err := s.DoCloseSession(ctx, request.Header)
if err != nil {
return nil, err
}
response := &api.CloseSessionResponse{
Header: header,
}
log.Tracef("Sending CloseSessionResponse %+v", response)
return response, nil
}