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
|
package client
import (
"fmt"
"sync"
"github.com/mimecast/dtail/internal/mapr"
)
// SessionSnapshot captures the current client-side mapreduce session state.
type SessionSnapshot struct {
Generation uint64
Query *mapr.Query
GlobalGroup *mapr.GlobalGroupSet
LastResult string
}
// SessionState keeps the mutable mapreduce query state shared by the client
// reporter and per-server handlers.
type SessionState struct {
mu sync.RWMutex
generation uint64
query *mapr.Query
global *mapr.GlobalGroupSet
lastResult string
changedCh chan struct{}
}
// NewSessionState returns a new shared mapreduce session state.
func NewSessionState(query *mapr.Query) *SessionState {
return &SessionState{
query: query,
global: mapr.NewGlobalGroupSet(),
changedCh: make(chan struct{}, 1),
}
}
// Snapshot returns a point-in-time copy of the shared mapreduce state.
func (s *SessionState) Snapshot() SessionSnapshot {
s.mu.RLock()
defer s.mu.RUnlock()
return SessionSnapshot{
Generation: s.generation,
Query: s.query,
GlobalGroup: s.global,
LastResult: s.lastResult,
}
}
// Changes returns a channel that is signaled whenever a new generation is committed.
func (s *SessionState) Changes() <-chan struct{} {
return s.changedCh
}
// CommitQuery resets the shared aggregation state for a newly accepted query generation.
func (s *SessionState) CommitQuery(rawQuery string, generation uint64) (*mapr.Query, error) {
query, err := mapr.NewQuery(rawQuery)
if err != nil {
return nil, fmt.Errorf("parse session query: %w", err)
}
s.mu.Lock()
s.generation = generation
s.query = query
s.global = mapr.NewGlobalGroupSet()
s.lastResult = ""
s.mu.Unlock()
s.notifyChange()
return query, nil
}
// CommitRenderedResult stores the last rendered result for the active generation.
func (s *SessionState) CommitRenderedResult(generation uint64, result string) (changed bool, ok bool) {
s.mu.Lock()
defer s.mu.Unlock()
if s.generation != generation {
return false, false
}
if s.lastResult == result {
return false, true
}
s.lastResult = result
return true, true
}
func (s *SessionState) notifyChange() {
select {
case s.changedCh <- struct{}{}:
default:
}
}
|