Skip to content

Commit ef94f2a

Browse files
committed
proxy can set max log file size, default is 1GB
1 parent 407563e commit ef94f2a

4 files changed

Lines changed: 148 additions & 3 deletions

File tree

‎cmd/proxy/main.go‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919

2020
"github.com/wandoulabs/codis/pkg/proxy/router"
2121
"github.com/wandoulabs/codis/pkg/utils"
22+
"github.com/wandoulabs/codis/pkg/utils/bytesize"
2223
"github.com/wandoulabs/codis/pkg/utils/log"
2324
)
2425

@@ -29,12 +30,13 @@ var (
2930
configFile = "config.ini"
3031
)
3132

32-
var usage = `usage: proxy [-c <config_file>] [-L <log_file>] [--log-level=<loglevel>] [--cpu=<cpu_num>] [--addr=<proxy_listen_addr>] [--http-addr=<debug_http_server_addr>]
33+
var usage = `usage: proxy [-c <config_file>] [-L <log_file>] [--log-level=<loglevel>] [--log-filesize=<filesize>] [--cpu=<cpu_num>] [--addr=<proxy_listen_addr>] [--http-addr=<debug_http_server_addr>]
3334
3435
options:
3536
-c set config file
3637
-L set output log file, default is stdout
3738
--log-level=<loglevel> set log level: info, warn, error, debug [default: info]
39+
--log-filesize=<maxsize> set max log file size, suffixes "KB", "MB", "GB" are allowed, 1KB=1024 bytes, etc. Default is 1GB.
3840
--cpu=<cpu_num> num of cpu cores that proxy can use
3941
--addr=<proxy_listen_addr> proxy listen address, example: 0.0.0.0:9000
4042
--http-addr=<debug_http_server_addr> debug vars http server
@@ -110,10 +112,18 @@ func main() {
110112
configFile = args["-c"].(string)
111113
}
112114

115+
var maxFileFrag = 10
116+
var maxFragSize int64 = bytesize.GB * 1
117+
if s, ok := args["--log-filesize"].(string); ok && s != "" {
118+
v, err := bytesize.Parse(s)
119+
if err != nil {
120+
log.PanicErrorf(err, "invalid max log file size = %s", s)
121+
}
122+
maxFragSize = v
123+
}
124+
113125
// set output log file
114126
if s, ok := args["-L"].(string); ok && s != "" {
115-
const maxFileFrag = 10
116-
const maxFragSize = 1024 * 1024 * 32
117127
f, err := log.NewRollingFile(s, maxFileFrag, maxFragSize)
118128
if err != nil {
119129
log.PanicErrorf(err, "open rolling log file failed: %s", s)

‎pkg/proxy/router/stats.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Copyright 2014 Wandoujia Inc. All Rights Reserved.
2+
// Licensed under the MIT (MIT-LICENSE.txt) license.
3+
14
package router
25

36
import (

‎pkg/utils/bytesize/bytesize.go‎

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// Copyright 2014 Wandoujia Inc. All Rights Reserved.
2+
// Licensed under the MIT (MIT-LICENSE.txt) license.
3+
4+
package bytesize
5+
6+
import (
7+
"regexp"
8+
"strconv"
9+
"strings"
10+
11+
"github.com/spinlock/go-libs/errors"
12+
"github.com/spinlock/go-libs/log"
13+
)
14+
15+
const (
16+
B = 1 << (10 * iota)
17+
KB
18+
MB
19+
GB
20+
TB
21+
PB
22+
)
23+
24+
var (
25+
BytesizeRegexp = regexp.MustCompile(`(?i)^\s*(\-?[\d\.]+)\s*([KMGTP]?B|[BKMGTP]|)\s*$`)
26+
digitsRegexp = regexp.MustCompile(`^\-?\d+$`)
27+
)
28+
29+
var (
30+
ErrBadBytesize = errors.New("invalid byte size")
31+
ErrBadBytesizeUnit = errors.New("invalid byte size unit")
32+
)
33+
34+
func Parse(s string) (int64, error) {
35+
if !BytesizeRegexp.MatchString(s) {
36+
return 0, errors.Trace(ErrBadBytesize)
37+
}
38+
39+
subs := BytesizeRegexp.FindStringSubmatch(s)
40+
if len(subs) != 3 {
41+
return 0, errors.Trace(ErrBadBytesize)
42+
}
43+
44+
size := int64(0)
45+
switch strings.ToUpper(string(subs[2])) {
46+
case "B", "":
47+
size = 1
48+
case "KB", "K":
49+
size = KB
50+
case "MB", "M":
51+
size = MB
52+
case "GB", "G":
53+
size = GB
54+
case "TB", "T":
55+
size = TB
56+
case "PB", "P":
57+
size = PB
58+
default:
59+
return 0, errors.Trace(ErrBadBytesizeUnit)
60+
}
61+
62+
text := subs[1]
63+
if digitsRegexp.MatchString(text) {
64+
n, err := strconv.ParseInt(text, 10, 64)
65+
if err != nil {
66+
return 0, errors.Trace(ErrBadBytesize)
67+
}
68+
size *= n
69+
} else {
70+
n, err := strconv.ParseFloat(text, 64)
71+
if err != nil {
72+
return 0, errors.Trace(ErrBadBytesize)
73+
}
74+
size = int64(float64(size) * n)
75+
}
76+
return size, nil
77+
}
78+
79+
func MustParse(s string) int64 {
80+
v, err := Parse(s)
81+
if err != nil {
82+
log.PanicError(err, "parse bytesize failed")
83+
}
84+
return v
85+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright 2014 Wandoujia Inc. All Rights Reserved.
2+
// Licensed under the MIT (MIT-LICENSE.txt) license.
3+
4+
package bytesize_test
5+
6+
import (
7+
"testing"
8+
9+
"github.com/spinlock/go-libs/assert"
10+
. "github.com/spinlock/go-libs/bytesize"
11+
"github.com/spinlock/go-libs/errors"
12+
)
13+
14+
func TestBytesize(t *testing.T) {
15+
assert.Must(MustParse("1") == 1)
16+
assert.Must(MustParse("1B") == 1)
17+
assert.Must(MustParse("1K") == KB)
18+
assert.Must(MustParse("1M") == MB)
19+
assert.Must(MustParse("1G") == GB)
20+
assert.Must(MustParse("1T") == TB)
21+
assert.Must(MustParse("1P") == PB)
22+
23+
assert.Must(MustParse(" -1") == -1)
24+
assert.Must(MustParse(" -1 b") == -1)
25+
assert.Must(MustParse(" -1 kb ") == -1*KB)
26+
assert.Must(MustParse(" -1 mb ") == -1*MB)
27+
assert.Must(MustParse(" -1 gb ") == -1*GB)
28+
assert.Must(MustParse(" -1 tb ") == -1*TB)
29+
assert.Must(MustParse(" -1 pb ") == -1*PB)
30+
31+
assert.Must(MustParse(" 1.5") == 1)
32+
assert.Must(MustParse(" 1.5 kb ") == 1.5*KB)
33+
assert.Must(MustParse(" 1.5 mb ") == 1.5*MB)
34+
assert.Must(MustParse(" 1.5 gb ") == 1.5*GB)
35+
assert.Must(MustParse(" 1.5 tb ") == 1.5*TB)
36+
assert.Must(MustParse(" 1.5 pb ") == 1.5*PB)
37+
}
38+
39+
func TestBytesizeError(t *testing.T) {
40+
var err error
41+
_, err = Parse("--1")
42+
assert.Must(errors.Equal(err, ErrBadBytesize))
43+
_, err = Parse("hello world")
44+
assert.Must(errors.Equal(err, ErrBadBytesize))
45+
_, err = Parse("123.132.32")
46+
assert.Must(errors.Equal(err, ErrBadBytesize))
47+
}

0 commit comments

Comments
 (0)