forked from FactomProject/factomd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
safeMsgMap.go
144 lines (126 loc) · 3.12 KB
/
safeMsgMap.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
package state
import (
"fmt"
"sync"
"github.com/FactomProject/factomd/common/constants"
"github.com/FactomProject/factomd/common/interfaces"
"github.com/FactomProject/factomd/util/atomic"
)
var _ = fmt.Println
// SafeMsgMap is a threadsafe map[[32]byte]interfaces.IMsg
type SafeMsgMap struct {
msgmap map[[32]byte]interfaces.IMsg
sync.RWMutex
name string
s *State
}
func NewSafeMsgMap(name string, s *State) *SafeMsgMap {
m := new(SafeMsgMap)
m.msgmap = make(map[[32]byte]interfaces.IMsg)
m.name = name
m.s = s
return m
}
func (m *SafeMsgMap) Get(key [32]byte) (msg interfaces.IMsg) {
m.RLock()
defer m.RUnlock()
return m.msgmap[key]
}
func (m *SafeMsgMap) Put(key [32]byte, msg interfaces.IMsg) {
m.Lock()
_, ok := m.msgmap[key]
if !ok {
defer m.s.LogMessage(m.name, "put", msg)
}
m.msgmap[key] = msg
m.Unlock()
}
func (m *SafeMsgMap) Delete(key [32]byte) (msg interfaces.IMsg, found bool) {
m.Lock()
msg, ok := m.msgmap[key] // return the message being deleted
if ok {
defer m.s.LogMessage(m.name, fmt.Sprintf("delete from %s", atomic.WhereAmIString(1)), msg)
delete(m.msgmap, key)
} else {
defer m.s.LogPrintf(m.name, "nodelete from %s M-%x", atomic.WhereAmIString(1), key[:3])
}
m.Unlock()
return
}
func (m *SafeMsgMap) Len() int {
m.RLock()
defer m.RUnlock()
return len(m.msgmap)
}
func (m *SafeMsgMap) Copy() *SafeMsgMap {
m2 := NewSafeMsgMap("copyOf"+m.name, m.s)
m2.s = m.s // for debug logging
m.RLock()
for k, v := range m.msgmap {
m2.msgmap[k] = v
}
m.RUnlock()
return m2
}
// Reset will delete all elements
func (m *SafeMsgMap) Reset() {
m.Lock()
if len(m.msgmap) > 0 {
m.msgmap = make(map[[32]byte]interfaces.IMsg)
}
m.Unlock()
m.s.LogPrintf(m.name, "reset")
}
//
// Used if a Commit Map
//
// Cleanup will clean old elements out from the commit map.
func (m *SafeMsgMap) Cleanup(s *State) {
m.Lock()
// Time out commits every leaderTimestamp and again. Also check for entries that have been revealed
leaderTimestamp := s.GetLeaderTimestamp()
for k, msg := range m.msgmap {
_, ok := s.Replay.Valid(constants.TIME_TEST, msg.GetRepeatHash().Fixed(), msg.GetTimestamp(), leaderTimestamp)
if !ok {
msg, ok := m.msgmap[k]
if ok {
defer m.s.LogMessage(m.name, "cleanup_timeout", msg)
}
delete(m.msgmap, k)
continue
}
ok = s.Replay.IsHashUnique(constants.REVEAL_REPLAY, k)
if !ok {
msg, ok := m.msgmap[k]
if ok {
defer m.s.LogMessage(m.name, "cleanup_replay", msg)
}
delete(m.msgmap, k)
continue
}
}
m.Unlock()
}
// RemoveExpired is used when treating this as a commit map. Do not
func (m *SafeMsgMap) RemoveExpired(s *State) {
m.Lock()
// Time out commits every now and again.
for k, v := range m.msgmap {
if v != nil {
_, ok := s.Replay.Valid(constants.TIME_TEST, v.GetRepeatHash().Fixed(), v.GetTimestamp(), s.GetLeaderTimestamp())
if !ok {
defer m.s.LogMessage(m.name, "RemoveExpired", v)
delete(m.msgmap, k)
}
}
}
m.Unlock()
}
//
// For tests
//
// GetRaw is used in testing and simcontrol. Do no use this in production
func (m *SafeMsgMap) GetRaw() map[[32]byte]interfaces.IMsg {
raw := m.Copy()
return raw.msgmap
}