This repository has been archived by the owner on May 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 346
/
cache.go
152 lines (134 loc) · 3.18 KB
/
cache.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
// Copyright Monax Industries Limited
// SPDX-License-Identifier: Apache-2.0
package registry
import (
"fmt"
"sync"
"github.com/hyperledger/burrow/crypto"
)
// Cache helps prevent unnecessary IAVLTree updates and garbage generation.
type Cache struct {
sync.RWMutex
backend Reader
registry map[crypto.Address]*nodeInfo
stats NodeStats
}
type nodeInfo struct {
sync.RWMutex
node *NodeIdentity
removed bool
updated bool
}
var _ Writer = &Cache{}
// NewCache returns a Cache which can write to an output Writer via Sync.
// Not goroutine safe, use syncStateCache if you need concurrent access
func NewCache(backend Reader) *Cache {
return &Cache{
backend: backend,
registry: make(map[crypto.Address]*nodeInfo),
stats: NewNodeStats(),
}
}
func (cache *Cache) GetNodeByID(id crypto.Address) (*NodeIdentity, error) {
info, err := cache.get(id)
if err != nil {
return nil, err
}
info.RLock()
defer info.RUnlock()
if info.removed {
return nil, nil
}
return info.node, nil
}
func (cache *Cache) GetNodeIDsByAddress(net string) ([]crypto.Address, error) {
return cache.stats.GetAddresses(net), nil
}
func (cache *Cache) GetNumPeers() int {
return len(cache.registry)
}
func (cache *Cache) UpdateNode(id crypto.Address, node *NodeIdentity) error {
info, err := cache.get(id)
if err != nil {
return err
}
info.Lock()
defer info.Unlock()
if info.removed {
return fmt.Errorf("UpdateNode on a removed node: %x", id)
}
info.node = node
info.updated = true
cache.stats.Remove(info.node)
cache.stats.Insert(node.GetNetworkAddress(), id)
return nil
}
func (cache *Cache) RemoveNode(id crypto.Address) error {
info, err := cache.get(id)
if err != nil {
return err
}
info.Lock()
defer info.Unlock()
if info.removed {
return fmt.Errorf("RemoveNode on removed node: %x", id)
}
cache.stats.Remove(info.node)
info.removed = true
return nil
}
// Sync writes whatever is in the cache to the output state. Does not flush the cache, to do that call Reset()
// after Sync or use Flush if your wish to use the output state as your next backend
func (cache *Cache) Sync(state Writer) error {
cache.Lock()
defer cache.Unlock()
for id, info := range cache.registry {
info.RLock()
if info.removed {
err := state.RemoveNode(id)
if err != nil {
info.RUnlock()
return err
}
} else if info.updated {
err := state.UpdateNode(id, info.node)
if err != nil {
info.RUnlock()
return err
}
}
info.RUnlock()
}
return nil
}
// Reset the cache to empty initialising the backing map to the same size as the previous iteration
func (cache *Cache) Reset(backend Reader) {
cache.Lock()
defer cache.Unlock()
cache.backend = backend
cache.registry = make(map[crypto.Address]*nodeInfo)
}
func (cache *Cache) Backend() Reader {
return cache.backend
}
func (cache *Cache) get(id crypto.Address) (*nodeInfo, error) {
cache.RLock()
info := cache.registry[id]
cache.RUnlock()
if info == nil {
cache.Lock()
defer cache.Unlock()
info = cache.registry[id]
if info == nil {
node, err := cache.backend.GetNodeByID(id)
if err != nil {
return nil, err
}
info = &nodeInfo{
node: node,
}
cache.registry[id] = info
}
}
return info, nil
}