Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix corrupt Winlogbeat registry occurring on power failure #2434

Merged
merged 1 commit into from
Sep 2, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ https://github.com/elastic/beats/compare/v5.0.0-alpha5...master[Check the HEAD d
- Fix processor failure in Filebeat when using regex, contain, or equals with the message field. {issue}2178[2178]

*Winlogbeat*
- Fix corrupt registry file that occurs on power loss by disabling file write caching. {issue}2313[2313]

==== Added

Expand Down
5 changes: 2 additions & 3 deletions winlogbeat/checkpoint/checkpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ func NewCheckpoint(file string, maxUpdates int, interval time.Duration) (*Checkp
// the amount of time since the last disk write reaches flushInterval.
func (c *Checkpoint) run() {
defer c.wg.Done()
defer c.persist()

flushTimer := time.NewTimer(c.flushInterval)
defer flushTimer.Stop()
Expand All @@ -127,8 +128,6 @@ loop:
c.persist()
flushTimer.Reset(c.flushInterval)
}

c.persist()
}

// Shutdown stops the checkpoint worker (which persists any state to disk as
Expand Down Expand Up @@ -185,7 +184,7 @@ func (c *Checkpoint) persist() bool {
// flush writes the current state to disk.
func (c *Checkpoint) flush() error {
tempFile := c.file + ".new"
file, err := os.Create(tempFile)
file, err := create(tempFile)
if os.IsNotExist(err) {
// Try to create directory if it does not exist.
if createDirErr := c.createDir(); createDirErr == nil {
Expand Down
9 changes: 9 additions & 0 deletions winlogbeat/checkpoint/file_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// +build !windows

package checkpoint

import "os"

func create(path string) (*os.File, error) {
return os.Create(path)
}
37 changes: 37 additions & 0 deletions winlogbeat/checkpoint/file_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package checkpoint

import (
"os"
"syscall"
)

const (
_FILE_FLAG_WRITE_THROUGH = 0x80000000
)

func create(path string) (*os.File, error) {
return createWriteThroughFile(path)
}

// createWriteThroughFile creates a file whose write operations do not go
// through any intermediary cache, they go directly to disk.
func createWriteThroughFile(path string) (*os.File, error) {
if len(path) == 0 {
return nil, syscall.ERROR_FILE_NOT_FOUND
}
pathp, err := syscall.UTF16PtrFromString(path)
if err != nil {
return nil, err
}

h, err := syscall.CreateFile(
pathp, // Path
syscall.GENERIC_READ|syscall.GENERIC_WRITE, // Access Mode
uint32(syscall.FILE_SHARE_READ|syscall.FILE_SHARE_WRITE), // Share Mode
nil, // Security Attributes
syscall.CREATE_ALWAYS, // Create Mode
uint32(syscall.FILE_ATTRIBUTE_NORMAL|_FILE_FLAG_WRITE_THROUGH), // Flags and Attributes
0) // Template File

return os.NewFile(uintptr(h), path), err
}