Skip to content

Commit b0362dc

Browse files
committed
fix: resolve go vet errors by making fadvise platform-specific
- Split fadviseNotUsed function into platform-specific implementations - fadvise_linux.go: Linux-specific implementation using unix.Fadvise - fadvise_other.go: No-op implementation for non-Linux platforms - Remove unused imports (os, path/filepath) from cmd.go - Remove unix import from cmd.go as it's now only used in Linux-specific file This resolves the issue where unix.Fadvise and unix.FADV_DONTNEED are not available on macOS and other non-Linux platforms. The build tags ensure the correct implementation is compiled for each target OS.
1 parent c61e310 commit b0362dc

File tree

3 files changed

+84
-30
lines changed

3 files changed

+84
-30
lines changed

pkg/walarchive/cmd.go

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,13 @@ import (
2020
"context"
2121
"fmt"
2222
"math"
23-
"os"
2423
"os/exec"
25-
"path/filepath"
2624
"sync"
2725
"time"
2826

2927
"github.com/cloudnative-pg/machinery/pkg/execlog"
3028
"github.com/cloudnative-pg/machinery/pkg/fileutils"
3129
"github.com/cloudnative-pg/machinery/pkg/log"
32-
"golang.org/x/sys/unix"
3330

3431
"github.com/cloudnative-pg/barman-cloud/pkg/utils"
3532
)
@@ -192,30 +189,3 @@ func (archiver *BarmanArchiver) CheckWalArchiveDestination(ctx context.Context,
192189

193190
return nil
194191
}
195-
196-
// fadviseNotUsed issues an fadvise to the OS to inform that the file is not needed anymore.
197-
// This is necessary because we run in a separate container from PostgreSQL in Kubernetes.
198-
// Without this hint, archived WALs accumulate in page cache since memory pressure on large
199-
// machines is typically insufficient to evict them, wasting memory that could be used for
200-
// active workloads. PostgreSQL handles its own cache management, but our archiver sidecar
201-
// needs to do the same for the files it processes.
202-
func (archiver *BarmanArchiver) fadviseNotUsed(fileName string) (err error) {
203-
file, err := os.Open(filepath.Clean(fileName))
204-
if err != nil {
205-
return fmt.Errorf("error opening file %s for fadvise: %w", fileName, err)
206-
}
207-
208-
defer func(file *os.File) {
209-
closeErr := file.Close()
210-
if closeErr != nil && err == nil {
211-
err = fmt.Errorf("error closing file %s for fadvise: %w", fileName, closeErr)
212-
}
213-
}(file)
214-
215-
fd := int(file.Fd())
216-
if fadviseErr := unix.Fadvise(fd, 0, 0, unix.FADV_DONTNEED); fadviseErr != nil {
217-
return fmt.Errorf("error issuing fadvise on file %s: %w", fileName, fadviseErr)
218-
}
219-
220-
return nil
221-
}

pkg/walarchive/fadvise_linux.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//go:build linux
2+
3+
/*
4+
Copyright The CloudNativePG Contributors
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
19+
package walarchive
20+
21+
import (
22+
"fmt"
23+
"os"
24+
"path/filepath"
25+
26+
"golang.org/x/sys/unix"
27+
)
28+
29+
// fadviseNotUsed issues an fadvise to the OS to inform that the file is not needed anymore.
30+
// This is necessary because we run in a separate container from PostgreSQL in Kubernetes.
31+
// Without this hint, archived WALs accumulate in page cache since memory pressure on large
32+
// machines is typically insufficient to evict them, wasting memory that could be used for
33+
// active workloads. PostgreSQL handles its own cache management, but our archiver sidecar
34+
// needs to do the same for the files it processes.
35+
func (archiver *BarmanArchiver) fadviseNotUsed(fileName string) (err error) {
36+
file, err := os.Open(filepath.Clean(fileName))
37+
if err != nil {
38+
return fmt.Errorf("error opening file %s for fadvise: %w", fileName, err)
39+
}
40+
41+
defer func(file *os.File) {
42+
closeErr := file.Close()
43+
if closeErr != nil && err == nil {
44+
err = fmt.Errorf("error closing file %s for fadvise: %w", fileName, closeErr)
45+
}
46+
}(file)
47+
48+
fd := int(file.Fd())
49+
if fadviseErr := unix.Fadvise(fd, 0, 0, unix.FADV_DONTNEED); fadviseErr != nil {
50+
return fmt.Errorf("error issuing fadvise on file %s: %w", fileName, fadviseErr)
51+
}
52+
53+
return nil
54+
}

pkg/walarchive/fadvise_other.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//go:build !linux
2+
3+
/*
4+
Copyright The CloudNativePG Contributors
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
19+
package walarchive
20+
21+
// fadviseNotUsed is a no-op on non-Linux platforms where fadvise is not available.
22+
// On Linux, this function issues an fadvise to the OS to inform that the file is not needed anymore.
23+
// This is necessary because we run in a separate container from PostgreSQL in Kubernetes.
24+
// Without this hint, archived WALs accumulate in page cache since memory pressure on large
25+
// machines is typically insufficient to evict them, wasting memory that could be used for
26+
// active workloads. PostgreSQL handles its own cache management, but our archiver sidecar
27+
// needs to do the same for the files it processes.
28+
func (archiver *BarmanArchiver) fadviseNotUsed(fileName string) error {
29+
return nil
30+
}

0 commit comments

Comments
 (0)