-
Notifications
You must be signed in to change notification settings - Fork 17
/
statetracker.go
173 lines (153 loc) · 3.72 KB
/
statetracker.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
/*************************************************************************
* Copyright 2019 Gravwell, Inc. All rights reserved.
* Contact: <legal@gravwell.io>
*
* This software may be modified and distributed under the terms of the
* BSD 2-clause license. See the LICENSE file for details.
**************************************************************************/
package main
import (
"encoding/gob"
"errors"
"fmt"
"os"
"sync"
"time"
"github.com/gravwell/gravwell/v4/ingest"
)
type stateTracker struct {
sync.Mutex
igst *ingest.IngestMuxer
stateMap map[string]time.Time
tempMap map[string]time.Time
horizon time.Duration // how long should we wait before pulling stuff out of the state map
filePath string
stateFout *os.File
}
func NewTracker(statePath string, horizon time.Duration, igst *ingest.IngestMuxer) (*stateTracker, error) {
st := &stateTracker{
filePath: statePath,
igst: igst,
horizon: horizon,
}
st.initStates()
return st, nil
}
func (st *stateTracker) initStates() error {
var fi os.FileInfo
st.stateMap = map[string]time.Time{}
st.tempMap = map[string]time.Time{}
//attempt to open state file
fi, err := os.Stat(st.filePath)
if err != nil {
//ensure error is a "not found" error
if !os.IsNotExist(err) {
return fmt.Errorf("state file path is invalid: %v", err)
}
//attempt to create the file and get a handle, states will be empty
st.stateFout, err = os.OpenFile(st.filePath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0660)
if err != nil {
return err
}
return nil
}
//check that is a regular file
if !fi.Mode().IsRegular() {
return ErrInvalidStateFile
}
//is a regular file, attempt to open it RW
st.stateFout, err = os.OpenFile(st.filePath, os.O_RDWR, 0550) //u+rw and g+rw but no nothing else
if err != nil {
return fmt.Errorf("Failed to open state file RW: %v", err)
}
//we have a valid file, attempt to load states if the file isn't empty
fi, err = st.stateFout.Stat()
if err != nil {
return fmt.Errorf("Failed to stat open file: %v", err)
}
if fi.Size() > 0 {
if err = gob.NewDecoder(st.stateFout).Decode(&st.stateMap); err != nil {
return fmt.Errorf("Failed to load existing states: %v", err)
}
}
return nil
}
func (st *stateTracker) dumpStatesNoLock() error {
if st.stateFout == nil {
return nil
}
n, err := st.stateFout.Seek(0, 0)
if err != nil {
return err
}
if n != 0 {
return ErrFailedSeek
}
if err := st.stateFout.Truncate(0); err != nil {
return err
}
if err := gob.NewEncoder(st.stateFout).Encode(st.stateMap); err != nil {
return err
}
return nil
}
func (st *stateTracker) cleanStatesNoLock() {
now := time.Now()
// purge anything that's too old
for k, v := range st.stateMap {
if now.Sub(v) > st.horizon {
delete(st.stateMap, k)
}
}
}
func (st *stateTracker) tickNoLock() {
// Sync the muxer while we're here
err := st.igst.Sync(2 * time.Second)
if err != nil {
return
}
// transfer from temp map to state map
for k, v := range st.tempMap {
st.stateMap[k] = v
}
// clean the state map
st.cleanStatesNoLock()
// reset the temp map
st.tempMap = make(map[string]time.Time)
// dump the map
st.dumpStatesNoLock()
}
func (st *stateTracker) IdExists(id string) bool {
st.Lock()
defer st.Unlock()
_, ok := st.stateMap[id]
if ok {
return true
}
_, ok = st.tempMap[id]
return ok
}
func (st *stateTracker) RecordId(id string, t time.Time) error {
st.Lock()
defer st.Unlock()
if st.tempMap == nil {
return errors.New("not yet initialized")
}
st.tempMap[id] = t
return nil
}
func (st *stateTracker) Start() {
go func() {
t := time.Tick(30 * time.Second)
for range t {
st.Lock()
st.tickNoLock()
st.Unlock()
}
}()
}
func (st *stateTracker) Close() {
st.Lock()
st.tickNoLock()
st.Unlock()
}