/
dmaps.go
110 lines (94 loc) · 3.56 KB
/
dmaps.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
// Copyright 2018-2021 Burak Sezer
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package config
import (
"runtime"
"time"
)
// DMaps denotes a global configuration for DMaps. You can still overwrite it by
// setting a DMap for a particular distributed map via Custom field. Most of the
// fields are related with distributed cache implementation.
type DMaps struct {
// NumEvictionWorkers denotes the number of goroutines that's used to find
// keys for eviction.
NumEvictionWorkers int64
// MaxIdleDuration denotes maximum time for each entry to stay idle in the DMap.
// It limits the lifetime of the entries relative to the time of the last
// read or write access performed on them. The entries whose idle period exceeds
// this limit are expired and evicted automatically. An entry is idle if no Get,
// GetEntry, Put, PutEx, Expire, PutIf, PutIfEx on it. Configuration of
// MaxIdleDuration feature varies by preferred deployment method.
MaxIdleDuration time.Duration
// TTLDuration is useful to set a default TTL for every key/value pair a
// distributed map instance.
TTLDuration time.Duration
// MaxKeys denotes maximum key count on a particular node. So if you have 10
// nodes with MaxKeys=100000, your key count in the cluster should be around
// MaxKeys*10=1000000
MaxKeys int
// MaxInuse denotes maximum amount of in-use memory on a particular node.
// So if you have 10 nodes with MaxInuse=100M (it has to be in bytes), amount
// of in-use memory should be around MaxInuse*10=1G
MaxInuse int
// LRUSamples denotes amount of randomly selected key count by the approximate
// LRU implementation. Lower values are better for high performance. It's
// 5 by default.
LRUSamples int
// EvictionPolicy determines the eviction policy in use. It's NONE by default.
// Set as LRU to enable LRU eviction policy.
EvictionPolicy EvictionPolicy
// Name of the storage engine. The default one is kvstore. Leave it empty if
// you want to use the default one.
StorageEngine string
// CheckEmptyFragmentsInterval is interval between two sequential call of empty
// fragment cleaner.
CheckEmptyFragmentsInterval time.Duration
// Custom is useful to set custom cache config per DMap instance.
Custom map[string]DMap
}
// Sanitize sets default values to empty configuration variables, if it's possible.
func (dm *DMaps) Sanitize() error {
if dm.Custom == nil {
dm.Custom = make(map[string]DMap)
}
if dm.EvictionPolicy == "" {
dm.EvictionPolicy = "NONE"
}
if dm.LRUSamples <= 0 {
dm.LRUSamples = DefaultLRUSamples
}
if dm.MaxInuse < 0 {
dm.MaxInuse = 0
}
if dm.MaxKeys < 0 {
dm.MaxKeys = 0
}
if dm.NumEvictionWorkers <= 0 {
dm.NumEvictionWorkers = int64(runtime.NumCPU())
}
if dm.StorageEngine == "" {
dm.StorageEngine = DefaultStorageEngine
}
if dm.CheckEmptyFragmentsInterval.Microseconds() == 0 {
dm.CheckEmptyFragmentsInterval = DefaultCheckEmptyFragmentsInterval
}
for _, d := range dm.Custom {
if err := d.Sanitize(); err != nil {
return err
}
}
return nil
}
func (dm *DMaps) Validate() error { return nil }
var _ IConfig = (*DMaps)(nil)