-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsimulation_state.go
More file actions
184 lines (152 loc) · 4.85 KB
/
Copy pathsimulation_state.go
File metadata and controls
184 lines (152 loc) · 4.85 KB
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
package master
import (
"encoding/gob"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strconv"
"sync"
"time"
proto "github.com/codeuniversity/al-proto"
)
//SimulationState ...
type SimulationState struct {
CellBuckets Buckets
TimeStep uint64
currentBucketRequestsInflight map[BucketKey]bool
nextBucketRequestsInflight map[BucketKey]bool
currentReturnedBatchChan chan *proto.CellComputeBatch
nextReturnedBatchChan chan *proto.CellComputeBatch
currentWaitGroup *sync.WaitGroup
nextWaitGroup *sync.WaitGroup
}
//NewSimulationState with internal fields initialized
func NewSimulationState(buckets Buckets) *SimulationState {
state := &SimulationState{
CellBuckets: buckets,
TimeStep: 0,
}
state.intializeComplexFields()
return state
}
//LoadSimulationState from file
func LoadSimulationState(statePath string) (*SimulationState, error) {
file, err := os.Open(statePath)
if err != nil {
return nil, err
}
s := &SimulationState{}
decoder := gob.NewDecoder(file)
err = decoder.Decode(s)
if err != nil {
return nil, err
}
s.intializeComplexFields()
return s, file.Close()
}
//LoadLatestSimulationState from file
func LoadLatestSimulationState() (*SimulationState, error) {
files, err := ioutil.ReadDir(filepath.Join(statesFolderName))
if err != nil {
return nil, err
}
latestStateName := nameOfLatestState(files)
return LoadSimulationState(filepath.Join(statesFolderName, latestStateName))
}
//MarkRequestInflight will panic if the key was set to inflight before
func (s *SimulationState) MarkRequestInflight(key BucketKey) {
alreadyMarked := s.nextBucketRequestsInflight[key]
if alreadyMarked {
panic(fmt.Sprintf("bucket request state was request to be marked as inflight but was already marked before"))
}
s.nextBucketRequestsInflight[key] = true
}
//RequestInflightFromLastStep returns wether or not a request for a bucket key was already made in the last step for this point in time
func (s *SimulationState) RequestInflightFromLastStep(key BucketKey) bool {
return s.currentBucketRequestsInflight[key]
}
//RequestInflight returns wether or not a request for a bucket key was already made
func (s *SimulationState) RequestInflight(key BucketKey) bool {
return s.nextBucketRequestsInflight[key]
}
//CurrentReturnedBatchChan ...
func (s *SimulationState) CurrentReturnedBatchChan() chan *proto.CellComputeBatch {
return s.currentReturnedBatchChan
}
//NextReturnedBatchChan ...
func (s *SimulationState) NextReturnedBatchChan() chan *proto.CellComputeBatch {
return s.nextReturnedBatchChan
}
//CurrentWaitGroup ...
func (s *SimulationState) CurrentWaitGroup() *sync.WaitGroup {
return s.currentWaitGroup
}
//NextWaitGroup ...
func (s *SimulationState) NextWaitGroup() *sync.WaitGroup {
return s.nextWaitGroup
}
//Cycle closes the current channel, set it and the current waitgroup to the next one and create a new next ones
func (s *SimulationState) Cycle() {
close(s.currentReturnedBatchChan)
s.currentBucketRequestsInflight = s.nextBucketRequestsInflight
s.nextBucketRequestsInflight = map[BucketKey]bool{}
s.currentReturnedBatchChan = s.nextReturnedBatchChan
s.nextReturnedBatchChan = make(chan *proto.CellComputeBatch)
}
func (s *SimulationState) intializeComplexFields() {
s.currentBucketRequestsInflight = map[BucketKey]bool{}
s.nextBucketRequestsInflight = map[BucketKey]bool{}
s.currentReturnedBatchChan = make(chan *proto.CellComputeBatch)
s.nextReturnedBatchChan = make(chan *proto.CellComputeBatch)
s.currentWaitGroup = &sync.WaitGroup{}
s.nextWaitGroup = &sync.WaitGroup{}
}
func (s *SimulationState) saveState() error {
saveTime := time.Now()
err := os.MkdirAll(statesFolderName, 0755)
if err != nil {
return err
}
temporaryPath := buildTemporaryStateFilePath(saveTime)
file, err := os.Create(temporaryPath)
if err != nil {
return err
}
encoder := gob.NewEncoder(file)
err = encoder.Encode(s)
if err != nil {
return err
}
err = file.Close()
if err != nil {
return err
}
return os.Rename(temporaryPath, buildStateFilePath(saveTime))
}
func nameOfLatestState(files []os.FileInfo) (latestStateName string) {
var latestStateInt int64
for _, f := range files {
stateName := f.Name()
stateInt, _ := stateNameToInt(stateName)
if stateNameValid(stateName) && stateInt > latestStateInt {
latestStateName = stateName
latestStateInt = stateInt
}
}
return
}
func stateNameValid(stateName string) bool {
var validStateName = regexp.MustCompile(`STATE_\d+`)
return validStateName.MatchString(stateName)
}
func stateNameToInt(stateName string) (int64, error) {
return strconv.ParseInt(stateName[6:], 10, 64)
}
func buildStateFilePath(saveTime time.Time) string {
return filepath.Join(statesFolderName, "STATE_"+string(saveTime.Format("20060102150405")))
}
func buildTemporaryStateFilePath(saveTime time.Time) string {
return filepath.Join(statesFolderName, "SAVING_"+string(saveTime.Format("20060102150405")))
}