Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[dbnode] Add process limits validation (and docs) #1118

Merged
merged 7 commits into from
Nov 16, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 1 addition & 14 deletions docs/how_to/cluster_hard_way.md
Original file line number Diff line number Diff line change
@@ -50,20 +50,7 @@ M3DB_HOST_ID=m3db001 m3dbnode -f config.yml
```

### Kernel
`m3dbnode` uses a lot of mmap-ed files for performance, as a result, you might need to bump `vm.max_map_count`. We suggest setting this value to 262,144 when provisioning your VM, so you don’t have to come back and debug issues later.

### OS
`m3dbnode` also can use a high number of files and we suggest setting a high max open number of files due to per partition fileset volumes.

On linux you can set a high limit for number of max open files in `/etc/security/limits.conf`:
```
your_m3db_user hard nofile 500000
your_m3db_user soft nofile 500000
```

Alternatively, if you wish to have `m3dbnode` run under `systemd` you can use our [service example](https://github.com/m3db/m3/tree/master/integrations/systemd/m3dbnode.service) which will set sane defaults.

Before running the process make sure the limits are set, if running manually you can raise the limit for the current user with `ulimit -n 500000`.
Ensure you review our [recommended kernel configuration](../operational_guide/kernel_configuration.md) before running M3DB in production as M3DB may exceed the default limits for some default kernel values.

## Config files
We wouldn’t feel right to call this guide, “The Hard Way” and not require you to change some configs by hand.
40 changes: 40 additions & 0 deletions docs/operational_guide/kernel_configuration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
Kernel Configuration
====================

This document lists the Kernel tweaks M3DB needs to run well.

## vm.max_map_count
M3DB uses a lot of mmap-ed files for performance, as a result, you might need to bump `vm.max_map_count`. We suggest setting this value to `262144`, so you don’t have to come back and debug issues later.

On Linux, you can increase the limits by running the following command as root:
```
sysctl -w vm.max_map_count=262144
```

To set this value permanently, update the `vm.max_map_count` setting in `/etc/sysctl.conf`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


## vm.swappiness
`vm.swappiness` controls how much the virtual memory subsystem will try to swap to disk. By default, the kernel configures this value to `60`, and will try to swap out items in memory even when there is plenty of RAM available to the system.

We recommend sizing clusters such that M3DB is running on a substrate (hosts/containers) such that no-swapping is necessary, i.e. the process is only using 30-50% of the maximum available memory. And therefore recommend setting the value of `vm.swappiness` to `1`. This tells the kernel to swap as little as possible, without altogether disabling swapping.

On Linux, you can configure this by running the following as root:
```
sysctl -w vm.swappiness=1
```

To set this value permanently, update the `vm.swappiness` setting in `/etc/sysctl.conf`.


## rlimits
M3DB also can use a high number of files and we suggest setting a high max open number of files due to per partition fileset volumes.

On Linux you can set a high limit for maximum number of open files in `/etc/security/limits.conf`:
```
your_m3db_user hard nofile 500000
your_m3db_user soft nofile 500000
```

Alternatively, if you wish to have M3DB run under `systemd` you can use our [service example](https://github.com/m3db/m3/tree/master/integrations/systemd/m3dbnode.service) which will set sane defaults.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh ok I see it is here. Can any of the other values go in there?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

naw the rest of it is specific to systemd


Before running the process make sure the limits are set, if running manually you can raise the limit for the current user with `ulimit -n 500000`.
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
@@ -67,6 +67,7 @@ pages:
- "Placement/Topology Configuration": "operational_guide/placement_configuration.md"
- "Namespace Configuration": "operational_guide/namespace_configuration.md"
- "Bootstrapping": "operational_guide/bootstrapping.md"
- "Kernel Configuration": "operational_guide/kernel_configuration.md"
- "Integrations":
- "Prometheus": "integrations/prometheus.md"
- "Troubleshooting": "troubleshooting/index.md"
73 changes: 73 additions & 0 deletions src/dbnode/server/limits.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright (c) 2018 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package server

import (
"fmt"

xos "github.com/m3db/m3/src/x/os"
xerror "github.com/m3db/m3x/errors"
)

const (
// TODO: determine these values based on topology/namespace configuration.
minNoFile = 500000
minVMMapCount = 262144
maxSwappiness = 1
)

func validateProcessLimits() error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought we were gonna emit these log messages at regular intervals as well if they were misconfigured? Seems like a long-lived struct with an internal goroutine that might be useful

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

limits, err := xos.GetProcessLimits()
if err != nil {
return fmt.Errorf("unable to determine process limits: %v", err)
}

var multiErr xerror.MultiError
if limits.NoFileCurr < minNoFile {
multiErr = multiErr.Add(fmt.Errorf(
"current value for RLIMIT_NOFILE(%d) is below recommended threshold(%d)",
limits.NoFileCurr, minNoFile,
))
}

if limits.NoFileMax < minNoFile {
multiErr = multiErr.Add(fmt.Errorf(
"max value for RLIMIT_NOFILE(%d) is below recommended threshold(%d)",
limits.NoFileMax, minNoFile,
))
}

if limits.VMMaxMapCount < minVMMapCount {
multiErr = multiErr.Add(fmt.Errorf(
"current value for vm.max_map_count(%d) is below recommended threshold(%d)",
limits.VMMaxMapCount, minVMMapCount,
))
}

if limits.VMSwappiness > maxSwappiness {
multiErr = multiErr.Add(fmt.Errorf(
"current value for vm.swappiness(%d) is above recommended threshold(%d)",
limits.VMSwappiness, maxSwappiness,
))
}

return multiErr.FinalError()
}
31 changes: 29 additions & 2 deletions src/dbnode/server/server.go
Original file line number Diff line number Diff line change
@@ -65,6 +65,7 @@ import (
"github.com/m3db/m3/src/dbnode/ts"
"github.com/m3db/m3/src/dbnode/x/tchannel"
"github.com/m3db/m3/src/dbnode/x/xio"
xdocs "github.com/m3db/m3/src/x/docs"
"github.com/m3db/m3/src/x/mmap"
"github.com/m3db/m3/src/x/serialize"
xconfig "github.com/m3db/m3x/config"
@@ -81,8 +82,10 @@ import (
)

const (
bootstrapConfigInitTimeout = 10 * time.Second
serverGracefulCloseTimeout = 10 * time.Second
bootstrapConfigInitTimeout = 10 * time.Second
serverGracefulCloseTimeout = 10 * time.Second
bgProcessLimitInterval = 10 * time.Second
maxBgProcessLimitMonitorDuration = 5 * time.Minute
)

// RunOptions provides options for running the server
@@ -134,6 +137,7 @@ func Run(runOpts RunOptions) {
os.Exit(1)
}

go bgValidateProcessLimits(logger)
debug.SetGCPercent(cfg.GCPercentage)

scope, _, err := cfg.Metrics.NewRootScope()
@@ -632,6 +636,29 @@ func interrupt() <-chan os.Signal {
return c
}

func bgValidateProcessLimits(logger xlog.Logger) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is annoying, but should we add a signal to stop this goroutine at some point?

Just because if we start using this more in integration tests having a dangling goroutine may become annoying to deal with.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

talked offline: added max monitor duration to control this

start := time.Now()
t := time.NewTicker(bgProcessLimitInterval)
defer t.Stop()
for {
// only monitor for first `maxBgProcessLimitMonitorDuration` of process lifetime
if time.Since(start) > maxBgProcessLimitMonitorDuration {
return
}

err := validateProcessLimits()
if err == nil {
return
}

logger.WithFields(
xlog.NewField("url", xdocs.Path("operational_guide/kernel_configuration")),
).Warnf(`invalid configuration found [%v], refer to linked documentation for more information`, err)

<-t.C
}
}

func kvWatchNewSeriesLimitPerShard(
store kv.Store,
logger xlog.Logger,
30 changes: 30 additions & 0 deletions src/x/docs/docs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (c) 2018 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package docs

import (
"fmt"
)

// Path returns the url to the given section of documentation.
func Path(section string) string {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lol nice

return fmt.Sprintf("https://m3db.github.io/m3/%s", section)
}
29 changes: 29 additions & 0 deletions src/x/os/limits.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) 2018 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package xos

// ProcessLimits captures the different process limits.
type ProcessLimits struct {
NoFileCurr uint64 // RLIMIT_NOFILE Current
NoFileMax uint64 // RLIMIT_NOFILE Max
VMMaxMapCount int64 // corresponds to /proc/sys/vm/max_map_count
VMSwappiness int64 // corresponds to /proc/sys/vm/swappiness
}
84 changes: 84 additions & 0 deletions src/x/os/limits_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Copyright (c) 2018 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package xos

import (
"fmt"
"io/ioutil"
"strconv"
"strings"
"syscall"
)

const (
sysctlDir = "/proc/sys/"
vmMaxMapCountKey = "vm.max_map_count"
vmSwappinessKey = "vm.swappiness"
)

// GetProcessLimits returns the known process limits.
func GetProcessLimits() (ProcessLimits, error) {
var noFile syscall.Rlimit
err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &noFile)
if err != nil {
return ProcessLimits{}, err
}

maxMap, err := sysctlInt64(vmMaxMapCountKey)
if err != nil {
return ProcessLimits{}, err
}

swap, err := sysctlInt64(vmSwappinessKey)
if err != nil {
return ProcessLimits{}, err
}

return ProcessLimits{
NoFileCurr: noFile.Cur,
NoFileMax: noFile.Max,
VMMaxMapCount: maxMap,
VMSwappiness: swap,
}, nil
}

func sysctlInt64(key string) (int64, error) {
str, err := sysctl(key)
if err != nil {
return 0, err
}

num, err := strconv.Atoi(str)
if err != nil {
return 0, err
}

return int64(num), nil
}

func sysctl(key string) (string, error) {
path := sysctlDir + strings.Replace(key, ".", "/", -1)
data, err := ioutil.ReadFile(path)
if err != nil {
return "", fmt.Errorf("could not find the given sysctl file: %v, err: %v", path, err)
}
return strings.TrimSpace(string(data)), nil
}
34 changes: 34 additions & 0 deletions src/x/os/limits_other.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// +build !linux
//
// Copyright (c) 2018 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package xos

import "errors"

var (
errUnableToDetermineProcessLimits = errors.New("unable to determine process limits on non-linux os")
)

// GetProcessLimits returns the known process limits.
func GetProcessLimits() (ProcessLimits, error) {
return ProcessLimits{}, errUnableToDetermineProcessLimits
}