Skip to content

Commit

Permalink
fix: open the WAL file as O_SYNC and get rid of file.Sync() calls con…
Browse files Browse the repository at this point in the history
…suming 1-5ms
  • Loading branch information
equals215 committed Jul 27, 2024
1 parent 67c3384 commit 98ec9fa
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 5 deletions.
2 changes: 1 addition & 1 deletion internal/pkg/queue/index/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type IndexManager struct {

// NewIndexManager creates a new IndexManager instance and loads the index from the index file.
func NewIndexManager(walPath, indexPath, queueDirPath string) (*IndexManager, error) {
walFile, err := os.OpenFile(walPath, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0644)
walFile, err := os.OpenFile(walPath, os.O_APPEND|os.O_SYNC|os.O_CREATE|os.O_RDWR, 0644)
if err != nil {
return nil, fmt.Errorf("failed to open WAL file: %w", err)
}
Expand Down
6 changes: 2 additions & 4 deletions internal/pkg/queue/index/wal.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@ func (im *IndexManager) unsafeWriteToWAL(Op Operation, host, id string, position
Position: position,
Size: size,
}

if err := im.walEncoder.Encode(entry); err != nil {
return fmt.Errorf("failed to write to WAL: %w", err)
}
if err := im.walFile.Sync(); err != nil {
return fmt.Errorf("failed to sync WAL: %w", err)
}

return nil
}
Expand Down Expand Up @@ -89,7 +87,7 @@ func (im *IndexManager) unsafeTruncateWAL() error {
}

// Reopen the file with O_TRUNC flag to truncate it
walFile, err := os.OpenFile(walPath, os.O_TRUNC|os.O_APPEND|os.O_RDWR, 0644)
walFile, err := os.OpenFile(walPath, os.O_TRUNC|os.O_SYNC|os.O_APPEND|os.O_RDWR, 0644)
if err != nil {
return fmt.Errorf("failed to truncate WAL file: %w", err)
}
Expand Down

0 comments on commit 98ec9fa

Please sign in to comment.