This repository has been archived by the owner on Jun 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.go
119 lines (103 loc) · 3.14 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
/*
* Copyright (c) 2019. Temple3x ([email protected])
* Copyright (c) 2014 Nate Finch
*
* Use of this source code is governed by the MIT License
* that can be found in the LICENSE file.
*/
package logro
// Config of logro.
type Config struct {
// OutputPath is the log file path.
OutputPath string `json:"output_path" toml:"output_path"`
// MaxSize is the maximum size of a log file before it gets rotated.
// Unit: MB.
// Default: 128 (128MB).
MaxSize int64 `json:"max_size_mb" toml:"max_size_mb"`
// MaxBackups is the maximum number of backup log files to retain.
MaxBackups int `json:"max_backups" toml:"max_backups"`
// LocalTime is the timestamp in backup log file. Default is to use UTC time.
// If true, use local time.
LocalTime bool `json:"local_time" toml:"local_time"`
// BufItem is the number of logro's write buffer items,
// logro buffers can hold write input up to BufItem.
// Default: 2048. 2048 is enough for 1 million IOPS.
//
// Buffer will overwrite data on writes in lieu of blocking.
// Losing data will be up to BufItem.
BufItem int `json:"buf_item" toml:"buf_item"`
// PerWriteSize is logro's write size,
// logro writes data to page cache every PerWriteSize.
// Unit: KB.
// Default: 64 (64KB).
//
// It's used for combining writes.
// The size of it should be aligned to page size,
// and it shouldn't be too large, because that may block logro write.
PerWriteSize int64 `json:"per_write_size" toml:"per_write_size"`
// PerSyncSize is logro's sync size,
// logro flushes data to storage media(hint) every PerSyncSize.
// Unit: MB.
// Default: 16 (16MB).
//
// Sync is working in background.
// The size of it should be aligned to page size,
// and it shouldn't be too large, avoiding burst I/O.
PerSyncSize int64 `json:"per_sync_size" toml:"per_sync_size"`
// Develop mode. Default is false.
// It' used for testing, if it's true, the page cache control unit could not be aligned to page cache size.
Developed bool `json:"developed" toml:"developed"`
}
const (
kb int64 = 1024
mb = 1024 * kb
)
// Default configs.
var (
defaultBufItem = 2048
defaultPerWriteSize = 64 * kb
defaultPerSyncSize = 16 * mb
// We don't need to keep too many backups,
// in practice, log shipper will collect the logs.
defaultMaxSize = 128 * mb
defaultMaxBackups = 4
)
func (c *Config) adjust() {
k, m := kb, mb
if c.Developed {
k, m = 1, 1
}
if c.MaxSize <= 0 {
c.MaxSize = defaultMaxSize
} else {
c.MaxSize = c.MaxSize * m
}
if c.MaxBackups <= 0 {
c.MaxBackups = defaultMaxBackups
}
if c.BufItem <= 0 {
c.BufItem = defaultBufItem
}
if c.PerWriteSize <= 0 {
c.PerWriteSize = defaultPerWriteSize
} else {
c.PerWriteSize = c.PerWriteSize * k
}
if c.PerSyncSize <= 0 {
c.PerSyncSize = defaultPerSyncSize
} else {
c.PerSyncSize = c.PerSyncSize * m
}
if !c.Developed {
if c.PerSyncSize < 2*c.PerWriteSize {
c.PerSyncSize = 2 * c.PerWriteSize
}
c.MaxSize = alignToPage(c.MaxSize)
c.PerWriteSize = alignToPage(c.PerWriteSize)
c.PerSyncSize = alignToPage(c.PerSyncSize)
}
}
const pageSize = 1 << 12 // 4KB.
func alignToPage(n int64) int64 {
return (n + pageSize - 1) &^ (pageSize - 1)
}