Skip to content

Commit

Permalink
fix high IO after sudden filebeat stop (#35893)
Browse files Browse the repository at this point in the history
In case of corrupted log file (which has good chances to happen in case
of sudden unclean system shutdown), we set a flag which causes us to
checkpoint immediately, but never do anything else besides that. This
causes filebeat to just checkpoint on each log operation (therefore
causing a high IO load on the server and also causing filebeat to fall
behind).

This change resets the logInvalid flag after a successful checkpointing.
  • Loading branch information
emmanueltouzery committed May 19, 2024
1 parent 4e6d762 commit 55f74ed
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
1 change: 1 addition & 0 deletions libbeat/statestore/backend/memlog/diskstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ func (s *diskstore) checkpointClearLog() {
err := s.logFile.Truncate(0)
if err == nil {
_, err = s.logFile.Seek(0, io.SeekStart)
s.logInvalid = false
}

if err != nil {
Expand Down
53 changes: 53 additions & 0 deletions libbeat/statestore/backend/memlog/store_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// 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 memlog

import (
"os"
"testing"

"github.com/stretchr/testify/assert"

"github.com/elastic/elastic-agent-libs/logp"
"github.com/elastic/elastic-agent-libs/mapstr"
)

func init() {
logp.DevelopmentSetup()
}

func TestRecoverFromCorruption(t *testing.T) {
path := t.TempDir()
defer os.RemoveAll(path)

if err := copyPath(path, "testdata/1/logfile_incomplete/"); err != nil {
t.Fatalf("Failed to copy test file to the temporary directory: %v", err)
}

store, err := openStore(logp.NewLogger("test"), path, 0660, 4096, false, func(_ uint64) bool {

Check failure on line 42 in libbeat/statestore/backend/memlog/store_test.go

View workflow job for this annotation

GitHub Actions / lint (windows)

ineffectual assignment to err (ineffassign)
return false
})

assert.Equal(t, true, store.disk.logInvalid)

err = store.logOperation(&opSet{K: "key", V: mapstr.M{
"field": 42,
}})
assert.NoError(t, err)
assert.Equal(t, false, store.disk.logInvalid)
}

0 comments on commit 55f74ed

Please sign in to comment.