mirror of
https://github.com/tuna/tunasync.git
synced 2025-06-14 13:32:43 +00:00
fix(worker): improved cgroup creation
This commit is contained in:
parent
9645fd44ec
commit
7601e5793f
@ -17,8 +17,8 @@ services:
|
|||||||
- docker
|
- docker
|
||||||
|
|
||||||
before_script:
|
before_script:
|
||||||
- sudo mount -t memory -o memory memory /sys/fs/cgroup/memory
|
- lssubsys -am
|
||||||
- mount
|
- sudo cgcreate -a $USER -t $USER -g cpu:tunasync
|
||||||
- sudo cgcreate -a $USER -t $USER -g memory:tunasync
|
- sudo cgcreate -a $USER -t $USER -g memory:tunasync
|
||||||
- docker pull alpine
|
- docker pull alpine
|
||||||
|
|
||||||
|
@ -15,35 +15,31 @@ import (
|
|||||||
"github.com/codeskyblue/go-sh"
|
"github.com/codeskyblue/go-sh"
|
||||||
)
|
)
|
||||||
|
|
||||||
var cgSubsystem = "cpuset"
|
|
||||||
|
|
||||||
type cgroupHook struct {
|
type cgroupHook struct {
|
||||||
emptyHook
|
emptyHook
|
||||||
provider mirrorProvider
|
provider mirrorProvider
|
||||||
basePath string
|
basePath string
|
||||||
baseGroup string
|
baseGroup string
|
||||||
created bool
|
created bool
|
||||||
|
subsystem string
|
||||||
|
memLimit string
|
||||||
}
|
}
|
||||||
|
|
||||||
func initCgroup(basePath string) {
|
func newCgroupHook(p mirrorProvider, basePath, baseGroup, subsystem, memLimit string) *cgroupHook {
|
||||||
if _, err := os.Stat(filepath.Join(basePath, "memory")); err == nil {
|
|
||||||
cgSubsystem = "memory"
|
|
||||||
return
|
|
||||||
}
|
|
||||||
logger.Warning("Memory subsystem of cgroup not enabled, fallback to cpu")
|
|
||||||
}
|
|
||||||
|
|
||||||
func newCgroupHook(p mirrorProvider, basePath, baseGroup string) *cgroupHook {
|
|
||||||
if basePath == "" {
|
if basePath == "" {
|
||||||
basePath = "/sys/fs/cgroup"
|
basePath = "/sys/fs/cgroup"
|
||||||
}
|
}
|
||||||
if baseGroup == "" {
|
if baseGroup == "" {
|
||||||
baseGroup = "tunasync"
|
baseGroup = "tunasync"
|
||||||
}
|
}
|
||||||
|
if subsystem == "" {
|
||||||
|
subsystem = "cpu"
|
||||||
|
}
|
||||||
return &cgroupHook{
|
return &cgroupHook{
|
||||||
provider: p,
|
provider: p,
|
||||||
basePath: basePath,
|
basePath: basePath,
|
||||||
baseGroup: baseGroup,
|
baseGroup: baseGroup,
|
||||||
|
subsystem: subsystem,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -52,15 +48,17 @@ func (c *cgroupHook) preExec() error {
|
|||||||
if err := sh.Command("cgcreate", "-g", c.Cgroup()).Run(); err != nil {
|
if err := sh.Command("cgcreate", "-g", c.Cgroup()).Run(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// if cgSubsystem != "memory" {
|
if c.subsystem != "memory" {
|
||||||
// return nil
|
return nil
|
||||||
// }
|
}
|
||||||
// if c.provider.Type() == provRsync || c.provider.Type() == provTwoStageRsync {
|
if c.memLimit != "" {
|
||||||
// gname := fmt.Sprintf("%s/%s", c.baseGroup, c.provider.Name())
|
gname := fmt.Sprintf("%s/%s", c.baseGroup, c.provider.Name())
|
||||||
// return sh.Command(
|
return sh.Command(
|
||||||
// "cgset", "-r", "memory.limit_in_bytes=512M", gname,
|
"cgset", "-r",
|
||||||
// ).Run()
|
fmt.Sprintf("memory.limit_in_bytes=%s", c.memLimit),
|
||||||
// }
|
gname,
|
||||||
|
).Run()
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -76,7 +74,7 @@ func (c *cgroupHook) postExec() error {
|
|||||||
|
|
||||||
func (c *cgroupHook) Cgroup() string {
|
func (c *cgroupHook) Cgroup() string {
|
||||||
name := c.provider.Name()
|
name := c.provider.Name()
|
||||||
return fmt.Sprintf("%s:%s/%s", cgSubsystem, c.baseGroup, name)
|
return fmt.Sprintf("%s:%s/%s", c.subsystem, c.baseGroup, name)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *cgroupHook) killAll() error {
|
func (c *cgroupHook) killAll() error {
|
||||||
@ -87,7 +85,7 @@ func (c *cgroupHook) killAll() error {
|
|||||||
|
|
||||||
readTaskList := func() ([]int, error) {
|
readTaskList := func() ([]int, error) {
|
||||||
taskList := []int{}
|
taskList := []int{}
|
||||||
taskFile, err := os.Open(filepath.Join(c.basePath, cgSubsystem, c.baseGroup, name, "tasks"))
|
taskFile, err := os.Open(filepath.Join(c.basePath, c.subsystem, c.baseGroup, name, "tasks"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return taskList, err
|
return taskList, err
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
@ -71,11 +72,14 @@ sleep 30
|
|||||||
provider, err := newCmdProvider(c)
|
provider, err := newCmdProvider(c)
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
|
|
||||||
initCgroup("/sys/fs/cgroup")
|
cg := newCgroupHook(provider, "/sys/fs/cgroup", "tunasync", "cpu", "")
|
||||||
cg := newCgroupHook(provider, "/sys/fs/cgroup", "tunasync")
|
|
||||||
provider.AddHook(cg)
|
provider.AddHook(cg)
|
||||||
|
|
||||||
err = cg.preExec()
|
err = cg.preExec()
|
||||||
|
if err != nil {
|
||||||
|
logger.Errorf("Failed to create cgroup")
|
||||||
|
return
|
||||||
|
}
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
@ -128,16 +132,19 @@ sleep 30
|
|||||||
provider, err := newRsyncProvider(c)
|
provider, err := newRsyncProvider(c)
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
|
|
||||||
initCgroup("/sys/fs/cgroup")
|
cg := newCgroupHook(provider, "/sys/fs/cgroup", "tunasync", "cpu", "512M")
|
||||||
cg := newCgroupHook(provider, "/sys/fs/cgroup", "tunasync")
|
|
||||||
provider.AddHook(cg)
|
provider.AddHook(cg)
|
||||||
|
|
||||||
cg.preExec()
|
err = cg.preExec()
|
||||||
//if cgSubsystem == "memory" {
|
if err != nil {
|
||||||
// memoLimit, err := ioutil.ReadFile(filepath.Join(cg.basePath, "memory", cg.baseGroup, provider.Name(), "memory.limit_in_bytes"))
|
logger.Errorf("Failed to create cgroup")
|
||||||
// So(err, ShouldBeNil)
|
return
|
||||||
// So(strings.Trim(string(memoLimit), "\n"), ShouldEqual, strconv.Itoa(512*1024*1024))
|
}
|
||||||
//}
|
if cg.subsystem == "memory" {
|
||||||
|
memoLimit, err := ioutil.ReadFile(filepath.Join(cg.basePath, "memory", cg.baseGroup, provider.Name(), "memory.limit_in_bytes"))
|
||||||
|
So(err, ShouldBeNil)
|
||||||
|
So(strings.Trim(string(memoLimit), "\n"), ShouldEqual, strconv.Itoa(512*1024*1024))
|
||||||
|
}
|
||||||
cg.postExec()
|
cg.postExec()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -70,9 +70,10 @@ type serverConfig struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type cgroupConfig struct {
|
type cgroupConfig struct {
|
||||||
Enable bool `toml:"enable"`
|
Enable bool `toml:"enable"`
|
||||||
BasePath string `toml:"base_path"`
|
BasePath string `toml:"base_path"`
|
||||||
Group string `toml:"group"`
|
Group string `toml:"group"`
|
||||||
|
Subsystem string `toml:"subsystem"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type dockerConfig struct {
|
type dockerConfig struct {
|
||||||
@ -119,6 +120,8 @@ type mirrorConfig struct {
|
|||||||
Password string `toml:"password"`
|
Password string `toml:"password"`
|
||||||
Stage1Profile string `toml:"stage1_profile"`
|
Stage1Profile string `toml:"stage1_profile"`
|
||||||
|
|
||||||
|
MemoryLimit string `toml:"memory_limit"`
|
||||||
|
|
||||||
DockerImage string `toml:"docker_image"`
|
DockerImage string `toml:"docker_image"`
|
||||||
DockerVolumes []string `toml:"docker_volumes"`
|
DockerVolumes []string `toml:"docker_volumes"`
|
||||||
DockerOptions []string `toml:"docker_options"`
|
DockerOptions []string `toml:"docker_options"`
|
||||||
|
@ -178,7 +178,10 @@ func newMirrorProvider(mirror mirrorConfig, cfg *Config) mirrorProvider {
|
|||||||
} else if cfg.Cgroup.Enable {
|
} else if cfg.Cgroup.Enable {
|
||||||
// Add Cgroup Hook
|
// Add Cgroup Hook
|
||||||
provider.AddHook(
|
provider.AddHook(
|
||||||
newCgroupHook(provider, cfg.Cgroup.BasePath, cfg.Cgroup.Group),
|
newCgroupHook(
|
||||||
|
provider, cfg.Cgroup.BasePath, cfg.Cgroup.Group,
|
||||||
|
cfg.Cgroup.Subsystem, mirror.MemoryLimit,
|
||||||
|
),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -55,9 +55,6 @@ func GetTUNASyncWorker(cfg *Config) *Worker {
|
|||||||
w.httpClient = httpClient
|
w.httpClient = httpClient
|
||||||
}
|
}
|
||||||
|
|
||||||
if cfg.Cgroup.Enable {
|
|
||||||
initCgroup(cfg.Cgroup.BasePath)
|
|
||||||
}
|
|
||||||
w.initJobs()
|
w.initJobs()
|
||||||
w.makeHTTPServer()
|
w.makeHTTPServer()
|
||||||
tunasyncWorker = w
|
tunasyncWorker = w
|
||||||
|
Loading…
x
Reference in New Issue
Block a user