Skip to content

Commit

Permalink
feature(worker): limit rsync memory using cgroup
Browse files Browse the repository at this point in the history
  • Loading branch information
bigeagle committed May 8, 2016
1 parent ccc31d9 commit 28c8145
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -11,7 +11,7 @@ os:
- linux

before_script:
- sudo cgcreate -t travis -a travis -g cpu:tunasync
- sudo cgcreate -t travis -a travis -g memory:tunasync

script:
- ./.testandcover.bash
Expand Down
4 changes: 2 additions & 2 deletions systemd/tunasync-worker.service
Expand Up @@ -6,10 +6,10 @@ After=network.target
Type=simple
User=tunasync
PermissionsStartOnly=true
ExecStartPre=/usr/bin/cgcreate -t tunasync -a tunasync -g cpu:tunasync
ExecStartPre=/usr/bin/cgcreate -t tunasync -a tunasync -g memory:tunasync
ExecStart=/home/bin/tunasync worker -c /etc/tunasync/worker.conf --with-systemd
ExecReload=/bin/kill -SIGHUP $MAINPID
ExecStopPost=/usr/bin/cgdelete cpu:tunasync
ExecStopPost=/usr/bin/cgdelete memory:tunasync

[Install]
WantedBy=multi-user.target
28 changes: 25 additions & 3 deletions worker/cgroup.go
Expand Up @@ -13,6 +13,8 @@ import (
"github.com/codeskyblue/go-sh"
)

var cgSubsystem string = "cpu"

type cgroupHook struct {
emptyHook
provider mirrorProvider
Expand All @@ -21,6 +23,14 @@ type cgroupHook struct {
created bool
}

func initCgroup(basePath string) {
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 == "" {
basePath = "/sys/fs/cgroup"
Expand All @@ -37,7 +47,19 @@ func newCgroupHook(p mirrorProvider, basePath, baseGroup string) *cgroupHook {

func (c *cgroupHook) preExec() error {
c.created = true
return sh.Command("cgcreate", "-g", c.Cgroup()).Run()
if err := sh.Command("cgcreate", "-g", c.Cgroup()).Run(); err != nil {
return err
}
if cgSubsystem != "memory" {
return nil
}
if c.provider.Type() == provRsync || c.provider.Type() == provTwoStageRsync {
gname := fmt.Sprintf("%s/%s", c.baseGroup, c.provider.Name())
return sh.Command(
"cgset", "-r", "memory.limit_in_bytes=128M", gname,
).Run()
}
return nil
}

func (c *cgroupHook) postExec() error {
Expand All @@ -52,15 +74,15 @@ func (c *cgroupHook) postExec() error {

func (c *cgroupHook) Cgroup() string {
name := c.provider.Name()
return fmt.Sprintf("cpu:%s/%s", c.baseGroup, name)
return fmt.Sprintf("%s:%s/%s", cgSubsystem, c.baseGroup, name)
}

func (c *cgroupHook) killAll() error {
if !c.created {
return nil
}
name := c.provider.Name()
taskFile, err := os.Open(filepath.Join(c.basePath, "cpu", c.baseGroup, name, "tasks"))
taskFile, err := os.Open(filepath.Join(c.basePath, cgSubsystem, c.baseGroup, name, "tasks"))
if err != nil {
return err
}
Expand Down
36 changes: 36 additions & 0 deletions worker/cgroup_test.go
Expand Up @@ -4,6 +4,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -71,6 +72,7 @@ sleep 30
provider, err := newCmdProvider(c)
So(err, ShouldBeNil)

initCgroup("/sys/fs/cgroup")
cg := newCgroupHook(provider, "/sys/fs/cgroup", "tunasync")
provider.AddHook(cg)

Expand Down Expand Up @@ -105,4 +107,38 @@ sleep 30
So(os.IsNotExist(err), ShouldBeTrue)

})

Convey("Rsync Memory Should Be Limited", t, func() {
tmpDir, err := ioutil.TempDir("", "tunasync")
defer os.RemoveAll(tmpDir)
So(err, ShouldBeNil)
scriptFile := filepath.Join(tmpDir, "myrsync")
tmpFile := filepath.Join(tmpDir, "log_file")

c := rsyncConfig{
name: "tuna-cgroup",
upstreamURL: "rsync://rsync.tuna.moe/tuna/",
rsyncCmd: scriptFile,
workingDir: tmpDir,
logDir: tmpDir,
logFile: tmpFile,
useIPv6: true,
interval: 600 * time.Second,
}

provider, err := newRsyncProvider(c)
So(err, ShouldBeNil)

initCgroup("/sys/fs/cgroup")
cg := newCgroupHook(provider, "/sys/fs/cgroup", "tunasync")
provider.AddHook(cg)

cg.preExec()
if cgSubsystem == "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(128*1024*1024))
}
cg.postExec()
})
}
1 change: 1 addition & 0 deletions worker/worker.go
Expand Up @@ -53,6 +53,7 @@ func GetTUNASyncWorker(cfg *Config) *Worker {
w.httpClient = httpClient
}

initCgroup(cfg.Cgroup.BasePath)
w.initJobs()
w.makeHTTPServer()
tunasyncWorker = w
Expand Down

0 comments on commit 28c8145

Please sign in to comment.