diff --git a/libbeat/statestore/backend/memlog/diskstore.go b/libbeat/statestore/backend/memlog/diskstore.go index 6adde0b25ec5..39ac216b0d40 100644 --- a/libbeat/statestore/backend/memlog/diskstore.go +++ b/libbeat/statestore/backend/memlog/diskstore.go @@ -217,7 +217,7 @@ func (s *diskstore) Close() error { // always sync log file on ordinary shutdown. err := s.logBuf.Flush() if err == nil { - err = syncFile(s.logFile) + err = s.logFile.Sync() } s.logFile.Close() s.logFile = nil @@ -380,7 +380,7 @@ func (s *diskstore) checkpointTmpFile(tempfile string, states map[string]entry) return "", err } - if err = syncFile(f); err != nil { + if err = f.Sync(); err != nil { return "", err } @@ -658,7 +658,7 @@ func writeMetaFile(home string, mode os.FileMode) error { return err } - if err := syncFile(f); err != nil { + if err := f.Sync(); err != nil { return err } diff --git a/libbeat/statestore/backend/memlog/util.go b/libbeat/statestore/backend/memlog/util.go index ae6ec609dfc3..4b70e9b97a3a 100644 --- a/libbeat/statestore/backend/memlog/util.go +++ b/libbeat/statestore/backend/memlog/util.go @@ -18,11 +18,9 @@ package memlog import ( - "errors" "io" "os" "runtime" - "syscall" ) // countWriter keeps track of the amount of bytes written over time. @@ -37,12 +35,6 @@ func (c *countWriter) Write(p []byte) (int, error) { return n, err } -var _ = isRetryErr - -func isRetryErr(err error) bool { - return errors.Is(err, syscall.EINTR) || errors.Is(err, syscall.EAGAIN) -} - // trySyncPath provides a best-effort fsync on path (directory). The fsync is required by some // filesystems, so to update the parents directory metadata to actually // contain the new file being rotated in. @@ -53,7 +45,7 @@ func trySyncPath(path string) { } defer f.Close() //nolint:errcheck // ignore error - syncFile(f) + f.Sync() } // pathEnsurePermissions checks if the file permissions for the given file match wantPerm. diff --git a/libbeat/statestore/backend/memlog/util_darwin.go b/libbeat/statestore/backend/memlog/util_darwin.go deleted file mode 100644 index 21861d471afb..000000000000 --- a/libbeat/statestore/backend/memlog/util_darwin.go +++ /dev/null @@ -1,74 +0,0 @@ -// 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" - "syscall" - - "golang.org/x/sys/unix" -) - -var errno0 = syscall.Errno(0) - -// syncFile implements the fsync operation for darwin. On darwin fsync is not -// reliable, instead the fcntl syscall with F_FULLFSYNC must be used. -func syncFile(f *os.File) error { - for { - _, err := unix.FcntlInt(f.Fd(), unix.F_FULLFSYNC, 0) - rootCause := errorRootCause(err) - if err == nil || isIOError(rootCause) { - return err - } - - if isRetryErr(err) { - continue - } - - err = f.Sync() - if isRetryErr(err) { - continue - } - return err - } -} - -func isIOError(err error) bool { - return err == unix.EIO || - // space/quota - err == unix.ENOSPC || err == unix.EDQUOT || err == unix.EFBIG || - // network - err == unix.ECONNRESET || err == unix.ENETDOWN || err == unix.ENETUNREACH -} - -// normalizeSysError returns the underlying error or nil, if the underlying -// error indicates it is no error. -func errorRootCause(err error) error { - for err != nil { - u, ok := err.(interface{ Unwrap() error }) - if !ok { - break - } - err = u.Unwrap() - } - - if err == nil || err == errno0 { - return nil - } - return err -} diff --git a/libbeat/statestore/backend/memlog/util_other.go b/libbeat/statestore/backend/memlog/util_other.go deleted file mode 100644 index d765a72d97d7..000000000000 --- a/libbeat/statestore/backend/memlog/util_other.go +++ /dev/null @@ -1,36 +0,0 @@ -// 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. - -//go:build linux || dragonfly || freebsd || netbsd || openbsd || solaris || aix - -package memlog - -import ( - "os" -) - -// syncFile implements the fsync operation for most *nix systems. -// The call is retried if EINTR or EAGAIN is returned. -func syncFile(f *os.File) error { - // best effort. - for { - err := f.Sync() - if err == nil || !isRetryErr(err) { - return err - } - } -} diff --git a/libbeat/statestore/backend/memlog/util_windows.go b/libbeat/statestore/backend/memlog/util_windows.go deleted file mode 100644 index ce0d1080b724..000000000000 --- a/libbeat/statestore/backend/memlog/util_windows.go +++ /dev/null @@ -1,26 +0,0 @@ -// 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" - -// syncFile implements the fsync operation for Windows. Internally -// FlushFileBuffers will be used. -func syncFile(f *os.File) error { - return f.Sync() // stdlib already uses FlushFileBuffers, yay -}