-
Notifications
You must be signed in to change notification settings - Fork 199
/
config.go
122 lines (107 loc) · 4.29 KB
/
config.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
package txcache
import (
"encoding/json"
"fmt"
"github.com/ElrondNetwork/elrond-go/storage"
)
const numChunksLowerBound = 1
const numChunksUpperBound = 128
const maxNumItemsLowerBound = 4
const maxNumBytesLowerBound = maxNumItemsLowerBound * 1
const maxNumBytesUpperBound = 1_073_741_824 // one GB
const maxNumItemsPerSenderLowerBound = 1
const maxNumBytesPerSenderLowerBound = maxNumItemsPerSenderLowerBound * 1
const maxNumBytesPerSenderUpperBound = 33_554_432 // 32 MB
const numTxsToPreemptivelyEvictLowerBound = 1
const numSendersToPreemptivelyEvictLowerBound = 1
// ConfigSourceMe holds cache configuration
type ConfigSourceMe struct {
Name string
NumChunks uint32
EvictionEnabled bool
NumBytesThreshold uint32
NumBytesPerSenderThreshold uint32
CountThreshold uint32
CountPerSenderThreshold uint32
NumSendersToPreemptivelyEvict uint32
}
type senderConstraints struct {
maxNumTxs uint32
maxNumBytes uint32
}
// TODO: Upon further analysis and brainstorming, add some sensible minimum accepted values for the appropriate fields.
func (config *ConfigSourceMe) verify() error {
if len(config.Name) == 0 {
return fmt.Errorf("%w: config.Name is invalid", storage.ErrInvalidConfig)
}
if config.NumChunks < numChunksLowerBound || config.NumChunks > numChunksUpperBound {
return fmt.Errorf("%w: config.NumChunks is invalid", storage.ErrInvalidConfig)
}
if config.NumBytesPerSenderThreshold < maxNumBytesPerSenderLowerBound || config.NumBytesPerSenderThreshold > maxNumBytesPerSenderUpperBound {
return fmt.Errorf("%w: config.NumBytesPerSenderThreshold is invalid", storage.ErrInvalidConfig)
}
if config.CountPerSenderThreshold < maxNumItemsPerSenderLowerBound {
return fmt.Errorf("%w: config.CountPerSenderThreshold is invalid", storage.ErrInvalidConfig)
}
if config.EvictionEnabled {
if config.NumBytesThreshold < maxNumBytesLowerBound || config.NumBytesThreshold > maxNumBytesUpperBound {
return fmt.Errorf("%w: config.NumBytesThreshold is invalid", storage.ErrInvalidConfig)
}
if config.CountThreshold < maxNumItemsLowerBound {
return fmt.Errorf("%w: config.CountThreshold is invalid", storage.ErrInvalidConfig)
}
if config.NumSendersToPreemptivelyEvict < numSendersToPreemptivelyEvictLowerBound {
return fmt.Errorf("%w: config.NumSendersToPreemptivelyEvict is invalid", storage.ErrInvalidConfig)
}
}
return nil
}
func (config *ConfigSourceMe) getSenderConstraints() senderConstraints {
return senderConstraints{
maxNumBytes: config.NumBytesPerSenderThreshold,
maxNumTxs: config.CountPerSenderThreshold,
}
}
// String returns a readable representation of the object
func (config *ConfigSourceMe) String() string {
bytes, err := json.Marshal(config)
if err != nil {
log.Error("ConfigSourceMe.String()", "err", err)
}
return string(bytes)
}
// ConfigDestinationMe holds cache configuration
type ConfigDestinationMe struct {
Name string
NumChunks uint32
MaxNumItems uint32
MaxNumBytes uint32
NumItemsToPreemptivelyEvict uint32
}
// TODO: Upon further analysis and brainstorming, add some sensible minimum accepted values for the appropriate fields.
func (config *ConfigDestinationMe) verify() error {
if len(config.Name) == 0 {
return fmt.Errorf("%w: config.Name is invalid", storage.ErrInvalidConfig)
}
if config.NumChunks < numChunksLowerBound || config.NumChunks > numChunksUpperBound {
return fmt.Errorf("%w: config.NumChunks is invalid", storage.ErrInvalidConfig)
}
if config.MaxNumItems < maxNumItemsLowerBound {
return fmt.Errorf("%w: config.MaxNumItems is invalid", storage.ErrInvalidConfig)
}
if config.MaxNumBytes < maxNumBytesLowerBound || config.MaxNumBytes > maxNumBytesUpperBound {
return fmt.Errorf("%w: config.MaxNumBytes is invalid", storage.ErrInvalidConfig)
}
if config.NumItemsToPreemptivelyEvict < numTxsToPreemptivelyEvictLowerBound {
return fmt.Errorf("%w: config.NumItemsToPreemptivelyEvict is invalid", storage.ErrInvalidConfig)
}
return nil
}
// String returns a readable representation of the object
func (config *ConfigDestinationMe) String() string {
bytes, err := json.Marshal(config)
if err != nil {
log.Error("ConfigDestinationMe.String()", "err", err)
}
return string(bytes)
}