Skip to content

Commit

Permalink
feat: use new circular buffer compressed chunks feature
Browse files Browse the repository at this point in the history
Nothing changes from functional point of view: Talos still keeps max of
1M of logs per buffer, but the chunks after first 64k are compressed on
the fly.

Signed-off-by: Andrey Smirnov <andrey.smirnov@siderolabs.com>
  • Loading branch information
smira committed May 17, 2024
1 parent 78b48eb commit 4784da3
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 12 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ require (
github.com/jeromer/syslogparser v1.1.0
github.com/jsimonetti/rtnetlink v1.4.2
github.com/jxskiss/base62 v1.1.0
github.com/klauspost/compress v1.17.7
github.com/klauspost/compress v1.17.8
github.com/klauspost/cpuid/v2 v2.2.7
github.com/linode/go-metadata v0.2.0
github.com/martinlindhe/base36 v1.1.1
Expand Down Expand Up @@ -127,7 +127,7 @@ require (
github.com/siderolabs/go-api-signature v0.3.2
github.com/siderolabs/go-blockdevice v0.4.7
github.com/siderolabs/go-blockdevice/v2 v2.0.0-20240405165836-3265299b0192
github.com/siderolabs/go-circular v0.1.0
github.com/siderolabs/go-circular v0.2.0
github.com/siderolabs/go-cmd v0.1.1
github.com/siderolabs/go-copy v0.1.0
github.com/siderolabs/go-debug v0.3.0
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -454,8 +454,8 @@ github.com/jxskiss/base62 v1.1.0 h1:A5zbF8v8WXx2xixnAKD2w+abC+sIzYJX+nxmhA6HWFw=
github.com/jxskiss/base62 v1.1.0/go.mod h1:HhWAlUXvxKThfOlZbcuFzsqwtF5TcqS9ru3y5GfjWAc=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/klauspost/compress v1.17.7 h1:ehO88t2UGzQK66LMdE8tibEd1ErmzZjNEqWkjLAKQQg=
github.com/klauspost/compress v1.17.7/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
github.com/klauspost/compress v1.17.8 h1:YcnTYrq7MikUT7k0Yb5eceMmALQPYBW/Xltxn0NAMnU=
github.com/klauspost/compress v1.17.8/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
github.com/klauspost/cpuid/v2 v2.2.7 h1:ZWSB3igEs+d0qvnxR/ZBzXVmxkgt8DdzP6m9pfuVLDM=
github.com/klauspost/cpuid/v2 v2.2.7/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
Expand Down Expand Up @@ -656,8 +656,8 @@ github.com/siderolabs/go-blockdevice v0.4.7 h1:2bk4WpEEflGxjrNwp57ye24Pr+cYgAiAe
github.com/siderolabs/go-blockdevice v0.4.7/go.mod h1:4PeOuk71pReJj1JQEXDE7kIIQJPVe8a+HZQa+qjxSEA=
github.com/siderolabs/go-blockdevice/v2 v2.0.0-20240405165836-3265299b0192 h1:16/cHDGhTUDBtfIftOkuHWhJcQdpa/FwwWPcTq4aOxc=
github.com/siderolabs/go-blockdevice/v2 v2.0.0-20240405165836-3265299b0192/go.mod h1:UBbbc+L7hU0UggOQeKCA+Qp3ImGkSeaLfVOiCbxRxEI=
github.com/siderolabs/go-circular v0.1.0 h1:zpBJNUbCZSh0odZxA4Dcj0d3ShLLR2WxKW6hTdAtoiE=
github.com/siderolabs/go-circular v0.1.0/go.mod h1:14XnLf/I3J0VjzTgmwWNGjp58/bdIi4zXppAEx8plfw=
github.com/siderolabs/go-circular v0.2.0 h1:Xca8zrjF/YsujLbwDSojkKzJe7ngetnpuIJn8N78DJI=
github.com/siderolabs/go-circular v0.2.0/go.mod h1:rrYCwHLYWmxqrmZP+LjYtwB2a55lxzQi0Ztu1VpWZSc=
github.com/siderolabs/go-cmd v0.1.1 h1:nTouZUSxLeiiEe7hFexSVvaTsY/3O8k1s08BxPRrsps=
github.com/siderolabs/go-cmd v0.1.1/go.mod h1:6hY0JG34LxEEwYE8aH2iIHkHX/ir12VRLqfwAf2yJIY=
github.com/siderolabs/go-copy v0.1.0 h1:OIWCtSg+rhOtnIZTpT31Gfpn17rv5kwJqQHG+QUEgC8=
Expand Down
31 changes: 25 additions & 6 deletions internal/app/machined/pkg/runtime/logging/circular.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ import (
"sync"
"time"

corezstd "github.com/klauspost/compress/zstd"
"github.com/siderolabs/go-circular"
"github.com/siderolabs/go-circular/zstd"
"github.com/siderolabs/go-debug"
"github.com/siderolabs/go-tail"

Expand All @@ -24,19 +26,25 @@ import (

// These constants should some day move to config.
const (
// Overall capacity of the log buffer (in raw bytes, memory size will be smaller due to compression).
DesiredCapacity = 1048576
// Some logs are tiny, no need to reserve too much memory.
InitialCapacity = 16384
// Cap each log at 1M.
MaxCapacity = 1048576
// Safety gap to avoid buffer overruns.
SafetyGap = 2048
// Chunk capacity is the length of each chunk, it should be
// big enough for the compression to be efficient.
ChunkCapacity = 65536
// Number of zstd-compressed chunks to keep.
NumCompressedChunks = (DesiredCapacity / ChunkCapacity) - 1
// Safety gap to avoid buffer overruns, can be lowered as with compression we don't need much.
SafetyGap = 1
)

// CircularBufferLoggingManager implements logging to circular fixed size buffer.
type CircularBufferLoggingManager struct {
fallbackLogger *log.Logger

buffers sync.Map
buffers sync.Map
compressor circular.Compressor

sendersRW sync.RWMutex
senders []runtime.LogSender
Expand All @@ -45,9 +53,19 @@ type CircularBufferLoggingManager struct {

// NewCircularBufferLoggingManager initializes new CircularBufferLoggingManager.
func NewCircularBufferLoggingManager(fallbackLogger *log.Logger) *CircularBufferLoggingManager {
compressor, err := zstd.NewCompressor(
corezstd.WithEncoderConcurrency(1),
corezstd.WithWindowSize(2*corezstd.MinWindowSize),
)
if err != nil {
// should not happen
panic(fmt.Sprintf("failed to create zstd compressor: %s", err))
}

return &CircularBufferLoggingManager{
fallbackLogger: fallbackLogger,
sendersChanged: make(chan struct{}),
compressor: compressor,
}
}

Expand Down Expand Up @@ -106,7 +124,8 @@ func (manager *CircularBufferLoggingManager) getBuffer(id string, create bool) (

b, err := circular.NewBuffer(
circular.WithInitialCapacity(InitialCapacity),
circular.WithMaxCapacity(MaxCapacity),
circular.WithMaxCapacity(ChunkCapacity),
circular.WithNumCompressedChunks(NumCompressedChunks, manager.compressor),
circular.WithSafetyGap(SafetyGap))
if err != nil {
return nil, err // only configuration issue might raise error
Expand Down

0 comments on commit 4784da3

Please sign in to comment.