-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsegment_fsync_process.go
More file actions
59 lines (50 loc) · 1.75 KB
/
Copy pathsegment_fsync_process.go
File metadata and controls
59 lines (50 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package zapp
import (
"fmt"
"time"
)
func (s *segment) fsyncLoop(syncInterval time.Duration) {
// zero interval means user wants not to have any sync process at all
if syncInterval == 0 {
return
}
ticker := time.NewTicker(syncInterval)
for {
select {
case <-ticker.C:
s.fsync()
case <-s.closedChan:
return
}
}
}
// fsync runs the process of persisting segment's file to disk
// The generic problem of all databases is that raw writing to underlying hardware is too expensive.
// OS buffers file changes implicitly and asynchronosly transfers the buffer to the hardware.
// Zapp uses Write Ahead Logging (WAL) to achieve consistency and durability.
// Each Write to data generates a new entry, which is appended to WAL and persisted to real disk hardware synchronosly.
// Segment's file contains the Recent Log Sequence Number (LSN) at the beginning header, which refers to one of the real existing WAL entries.
// Periodically segments file needs to be persisted to the hardware explicitly so that it is guaranteed, that a new checkpoint in WAL file can be created.
// Once the segment's file is persisted, the WAL file may be truncated because it's safe to loose actios, which are persisted to disk in segments.
// The process of safe truncation of the WAL file is called "checkpoint creation".
func (s *segment) fsync() {
s.mtx.Lock()
defer s.mtx.Unlock()
if s.closed {
return
}
s.rawFsync()
}
func (s *segment) rawFsync() {
err := s.file.Sync()
if err != nil {
panic(fmt.Errorf("tried to fsync segment's file, but got error: %w", err))
}
// we support working without WAL at all, so this is okay
if s.wal != nil {
err = s.wal.Checkpoint()
if err != nil {
panic(fmt.Errorf("can not create new checkpoint in WAL: %w", err))
}
}
}