Skip to content

Commit

Permalink
feat: auto set GOMEMLIMIT when memory is limited (#14882)
Browse files Browse the repository at this point in the history
* feat: auto set GOMEMLIMIT when memory is limited

use automemlimit lib to set memory limit
run check every 30s

* lint: run make update

* feat: fallback to system if cgroup is not available

(cherry picked from commit 4940a7e)

# Conflicts:
#	NOTICE.txt
#	go.mod
#	go.sum
  • Loading branch information
kruskall authored and mergify[bot] committed Jan 9, 2025
1 parent 41eb467 commit 8eb527a
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 0 deletions.
80 changes: 80 additions & 0 deletions NOTICE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,37 @@ Third party libraries used by the Elastic APM Server project:
================================================================================


--------------------------------------------------------------------------------
Dependency : github.com/KimMachineGun/automemlimit
Version: v0.7.0-pre.3
Licence type (autodetected): MIT
--------------------------------------------------------------------------------

Contents of probable licence file $GOMODCACHE/github.com/!kim!machine!gun/[email protected]/LICENSE:

MIT License

Copyright (c) 2022 Geon Kim

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.


--------------------------------------------------------------------------------
Dependency : github.com/cespare/xxhash/v2
Version: v2.3.0
Expand Down Expand Up @@ -6278,6 +6309,35 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.


--------------------------------------------------------------------------------
Dependency : go.uber.org/zap/exp
Version: v0.3.0
Licence type (autodetected): MIT
--------------------------------------------------------------------------------

Contents of probable licence file $GOMODCACHE/go.uber.org/zap/[email protected]/LICENSE:

Copyright (c) 2016-2024 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.


--------------------------------------------------------------------------------
Dependency : golang.org/x/net
Version: v0.34.0
Expand Down Expand Up @@ -13800,6 +13860,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


--------------------------------------------------------------------------------
<<<<<<< HEAD
Dependency : github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal
Version: v0.116.0
Licence type (autodetected): Apache-2.0
Expand Down Expand Up @@ -14019,6 +14080,18 @@ Licence type (autodetected): BSD-3-Clause
Contents of probable licence file $GOMODCACHE/github.com/pierrec/[email protected]+incompatible/LICENSE:

Copyright (c) 2015, Pierre Curto
=======
Dependency : github.com/pbnjay/memory
Version: v0.0.0-20210728143218-7b4eea64cf58
Licence type (autodetected): BSD-3-Clause
--------------------------------------------------------------------------------

Contents of probable licence file $GOMODCACHE/github.com/pbnjay/[email protected]/LICENSE:

BSD 3-Clause License

Copyright (c) 2017, Jeremy Jay
>>>>>>> 4940a7ea (feat: auto set GOMEMLIMIT when memory is limited (#14882))
All rights reserved.

Redistribution and use in source and binary forms, with or without
Expand All @@ -14031,7 +14104,11 @@ modification, are permitted provided that the following conditions are met:
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

<<<<<<< HEAD
* Neither the name of xxHash nor the names of its
=======
* Neither the name of the copyright holder nor the names of its
>>>>>>> 4940a7ea (feat: auto set GOMEMLIMIT when memory is limited (#14882))
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

Expand All @@ -14047,7 +14124,10 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


<<<<<<< HEAD

=======
>>>>>>> 4940a7ea (feat: auto set GOMEMLIMIT when memory is limited (#14882))
--------------------------------------------------------------------------------
Dependency : github.com/pierrec/lz4/v4
Version: v4.1.21
Expand Down
10 changes: 10 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/elastic/apm-server
go 1.23.0

require (
github.com/KimMachineGun/automemlimit v0.7.0-pre.3
github.com/cespare/xxhash/v2 v2.3.0
github.com/dgraph-io/badger/v2 v2.2007.4
github.com/dustin/go-humanize v1.0.1
Expand Down Expand Up @@ -49,7 +50,12 @@ require (
go.opentelemetry.io/otel/sdk/metric v1.33.0
go.uber.org/automaxprocs v1.6.0
go.uber.org/zap v1.27.0
<<<<<<< HEAD
golang.org/x/net v0.34.0
=======
go.uber.org/zap/exp v0.3.0
golang.org/x/net v0.32.0
>>>>>>> 4940a7ea (feat: auto set GOMEMLIMIT when memory is limited (#14882))
golang.org/x/sync v0.10.0
golang.org/x/term v0.28.0
golang.org/x/time v0.9.0
Expand Down Expand Up @@ -123,8 +129,12 @@ require (
github.com/mitchellh/hashstructure v1.1.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
<<<<<<< HEAD
github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal v0.116.0 // indirect
github.com/pierrec/lz4 v2.6.1+incompatible // indirect
=======
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58 // indirect
>>>>>>> 4940a7ea (feat: auto set GOMEMLIMIT when memory is limited (#14882))
github.com/pierrec/lz4/v4 v4.1.21 // indirect
github.com/planetscale/vtprotobuf v0.6.1-0.20240319094008-0393e58bdf10 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
Expand Down
11 changes: 11 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ github.com/DataDog/zstd v1.5.6 h1:LbEglqepa/ipmmQJUDnSsfvA8e8IStVcGaFWDuxvGOY=
github.com/DataDog/zstd v1.5.6/go.mod h1:g4AWEaM3yOg3HYfnJ3YIawPnVdXJh9QME85blwSAmyw=
github.com/HdrHistogram/hdrhistogram-go v1.1.2 h1:5IcZpTvzydCQeHzK4Ef/D5rrSqwxob0t8PQPMybUNFM=
github.com/HdrHistogram/hdrhistogram-go v1.1.2/go.mod h1:yDgFjdqOqDEKOvasDdhWNXYg9BVp4O+o5f6V/ehm6Oo=
<<<<<<< HEAD
=======
github.com/IBM/sarama v1.43.3 h1:Yj6L2IaNvb2mRBop39N7mmJAHBVY3dTPncr3qGVkxPA=
github.com/IBM/sarama v1.43.3/go.mod h1:FVIRaLrhK3Cla/9FfRF5X9Zua2KpS3SYIXxhac1H+FQ=
github.com/KimMachineGun/automemlimit v0.7.0-pre.3 h1:aZVmBE7SmIsRoVHIzpgCJ6rvwnKRnguDZv5pAZWcZyQ=
github.com/KimMachineGun/automemlimit v0.7.0-pre.3/go.mod h1:QZxpHaGOQoYvFhv/r4u3U0JTC2ZcOwbSr11UZF46UBM=
>>>>>>> 4940a7ea (feat: auto set GOMEMLIMIT when memory is limited (#14882))
github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERoyfY=
github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
Expand Down Expand Up @@ -348,6 +355,8 @@ github.com/opencontainers/image-spec v1.1.0/go.mod h1:W4s4sFTMaBeK1BQLXbG4AdM2sz
github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o=
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58 h1:onHthvaw9LFnH4t2DcNVpwGmV9E1BkGknEliJkfwQj0=
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58/go.mod h1:DXv8WO4yhMYhSNPKjeNKa5WY9YCIEBRbNzFFPJbWO6Y=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/pierrec/lz4 v2.6.0+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY=
github.com/pierrec/lz4 v2.6.1+incompatible h1:9UY3+iC23yxF0UfGaYrGplQ+79Rg+h/q9FV9ix19jjM=
Expand Down Expand Up @@ -504,6 +513,8 @@ go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q=
go.uber.org/zap v1.24.0/go.mod h1:2kMP+WWQ8aoFoedH3T2sq6iJ2yDWpHbP0f6MQbS9Gkg=
go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
go.uber.org/zap/exp v0.3.0 h1:6JYzdifzYkGmTdRR59oYH+Ng7k49H9qVpWwNSsGJj3U=
go.uber.org/zap/exp v0.3.0/go.mod h1:5I384qq7XGxYyByIhHm6jg5CHkGY0nsTfbDLgDDlgJQ=
golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
Expand Down
7 changes: 7 additions & 0 deletions internal/beatcmd/beat.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"errors"
"fmt"
"io"
"log/slog"
"os"
"os/user"
"runtime"
Expand All @@ -33,6 +34,7 @@ import (

"github.com/gofrs/uuid/v5"
"go.uber.org/zap"
"go.uber.org/zap/exp/zapslog"
"golang.org/x/sync/errgroup"

"github.com/elastic/beats/v7/libbeat/api"
Expand Down Expand Up @@ -348,6 +350,11 @@ func (b *Beat) Run(ctx context.Context) error {
return adjustMaxProcs(ctx, 30*time.Second, logger)
})

slogger := slog.New(zapslog.NewHandler(logger.Core()))
if err := adjustMemlimit(1*time.Second, slogger); err != nil {
return err
}

logSystemInfo(b.Info)

cleanup, err := b.registerElasticsearchVersionCheck()
Expand Down
43 changes: 43 additions & 0 deletions internal/beatcmd/memlimit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package beatcmd

import (
"fmt"
"log/slog"
"time"

"github.com/KimMachineGun/automemlimit/memlimit"
)

func adjustMemlimit(d time.Duration, logger *slog.Logger) error {
if _, err := memlimit.SetGoMemLimitWithOpts(
memlimit.WithProvider(
memlimit.ApplyFallback(
memlimit.FromCgroup,
memlimit.FromSystem,
),
),
memlimit.WithLogger(logger),
memlimit.WithRefreshInterval(d),
memlimit.WithRatio(0.9),
); err != nil {
return fmt.Errorf("failed to set go memlimit: %w", err)
}
return nil
}

0 comments on commit 8eb527a

Please sign in to comment.