forked from superfly/litefs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlitefs.go
286 lines (237 loc) · 6.54 KB
/
litefs.go
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package litefs
import (
"context"
"encoding/binary"
"errors"
"fmt"
"io"
"strconv"
"time"
)
// LiteFS errors
var (
ErrDatabaseNotFound = fmt.Errorf("database not found")
ErrDatabaseExists = fmt.Errorf("database already exists")
ErrNoPrimary = errors.New("no primary")
ErrPrimaryExists = errors.New("primary exists")
ErrLeaseExpired = errors.New("lease expired")
ErrReadOnlyReplica = fmt.Errorf("read only replica")
)
const PageSize = 4096
// SQLite constants
const (
WALHeaderSize = 32
WALFrameHeaderSize = 24
WALIndexHeaderSize = 136
)
// SQLite rollback journal lock constants.
const (
PENDING_BYTE = 0x40000000
RESERVED_BYTE = (PENDING_BYTE + 1)
SHARED_FIRST = (PENDING_BYTE + 2)
SHARED_SIZE = 510
)
// SQLite WAL lock constants.
const (
WAL_WRITE_LOCK = 120
WAL_CKPT_LOCK = 121
WAL_RECOVER_LOCK = 122
WAL_READ_LOCK0 = 123
WAL_READ_LOCK1 = 124
WAL_READ_LOCK2 = 125
WAL_READ_LOCK3 = 126
WAL_READ_LOCK4 = 127
)
// Open file description lock constants.
const (
F_OFD_GETLK = 36
F_OFD_SETLK = 37
F_OFD_SETLKW = 38
)
// JournalMode represents a SQLite journal mode.
type JournalMode string
const (
JournalModeDelete = "DELETE"
JournalModeTruncate = "TRUNCATE"
JournalModePersist = "PERSIST"
JournalModeWAL = "WAL"
)
// FileType represents a type of SQLite file.
type FileType int
// SQLite file types.
const (
FileTypeNone = FileType(iota)
FileTypeDatabase
FileTypeJournal
FileTypeWAL
FileTypeSHM
)
// IsValid returns true if t is a valid file type.
func (t FileType) IsValid() bool {
switch t {
case FileTypeDatabase, FileTypeJournal, FileTypeWAL, FileTypeSHM:
return true
default:
return false
}
}
// Pos represents the transactional position of a database.
type Pos struct {
TXID uint64
Chksum uint64
}
// IsZero returns true if the position is empty.
func (p Pos) IsZero() bool {
return p == (Pos{})
}
// FormatDBID formats id as a 16-character hex string.
func FormatDBID(id uint32) string {
return fmt.Sprintf("%08x", id)
}
// ParseDBID parses a 16-character hex string into a database ID.
func ParseDBID(s string) (uint32, error) {
if len(s) != 8 {
return 0, fmt.Errorf("invalid formatted database id length: %q", s)
}
v, err := strconv.ParseUint(s, 16, 32)
if err != nil {
return 0, fmt.Errorf("invalid database id format: %q", s)
}
return uint32(v), nil
}
// Client represents a client for connecting to other LiteFS nodes.
type Client interface {
// Stream starts a long-running connection to stream changes from another node.
Stream(ctx context.Context, rawurl string, posMap map[uint32]Pos) (StreamReader, error)
}
// StreamReader represents a stream of changes from a primary server.
type StreamReader interface {
io.ReadCloser
// NextFrame reads the next frame from the stream. After a frame is read,
// it may have a payload that can be read via Read() until io.EOF.
NextFrame() (StreamFrame, error)
}
type StreamFrameType uint32
const (
StreamFrameTypeDB = StreamFrameType(1)
StreamFrameTypeLTX = StreamFrameType(2)
)
type StreamFrame interface {
io.ReaderFrom
io.WriterTo
Type() StreamFrameType
}
// ReadStreamFrame reads a the stream type & frame from the reader.
func ReadStreamFrame(r io.Reader) (StreamFrame, error) {
var typ StreamFrameType
if err := binary.Read(r, binary.BigEndian, &typ); err != nil {
return nil, err
}
var f StreamFrame
switch typ {
case StreamFrameTypeDB:
f = &DBStreamFrame{}
case StreamFrameTypeLTX:
f = <XStreamFrame{}
default:
return nil, fmt.Errorf("invalid stream frame type: 0x%02x", typ)
}
if _, err := f.ReadFrom(r); err == io.EOF {
return nil, io.ErrUnexpectedEOF
} else if err != nil {
return nil, err
}
return f, nil
}
// WriteStreamFrame writes the stream type & frame to the writer.
func WriteStreamFrame(w io.Writer, f StreamFrame) error {
if err := binary.Write(w, binary.BigEndian, f.Type()); err != nil {
return err
}
_, err := f.WriteTo(w)
return err
}
// DBStreamFrame represents a frame with basic database information.
// This is sent at the beginning of the stream and when a new database is created.
type DBStreamFrame struct {
DBID uint32
Name string
}
// Type returns the type of stream frame.
func (*DBStreamFrame) Type() StreamFrameType { return StreamFrameTypeDB }
func (f *DBStreamFrame) ReadFrom(r io.Reader) (int64, error) {
if err := binary.Read(r, binary.BigEndian, &f.DBID); err != nil {
return 0, err
}
var nameN uint32
if err := binary.Read(r, binary.BigEndian, &nameN); err == io.EOF {
return 0, io.ErrUnexpectedEOF
} else if err != nil {
return 0, err
}
name := make([]byte, nameN)
if _, err := io.ReadFull(r, name); err == io.EOF {
return 0, io.ErrUnexpectedEOF
} else if err != nil {
return 0, err
}
f.Name = string(name)
return 0, nil
}
func (f *DBStreamFrame) WriteTo(w io.Writer) (int64, error) {
if err := binary.Write(w, binary.BigEndian, f.DBID); err != nil {
return 0, err
}
if err := binary.Write(w, binary.BigEndian, uint32(len(f.Name))); err != nil {
return 0, err
} else if _, err := w.Write([]byte(f.Name)); err != nil {
return 0, err
}
return 0, nil
}
type LTXStreamFrame struct {
Size int64
}
// Type returns the type of stream frame.
func (*LTXStreamFrame) Type() StreamFrameType { return StreamFrameTypeLTX }
func (f *LTXStreamFrame) ReadFrom(r io.Reader) (int64, error) {
if err := binary.Read(r, binary.BigEndian, &f.Size); err != nil {
return 0, err
}
return 0, nil
}
func (f *LTXStreamFrame) WriteTo(w io.Writer) (int64, error) {
if err := binary.Write(w, binary.BigEndian, f.Size); err != nil {
return 0, err
}
return 0, nil
}
// InodeNotifier is a callback for the store to use to invalidate the kernel page cache.
type InodeNotifier interface {
InodeNotify(dbID uint32, off int64, length int64) error
}
// Leaser represents an API for obtaining a lease for leader election.
type Leaser interface {
io.Closer
AdvertiseURL() string
// Acquire attempts to acquire the lease to become the primary.
Acquire(ctx context.Context) (Lease, error)
// PrimaryURL attempts to read the current primary URL.
// Returns ErrNoPrimary if no primary has the lease.
PrimaryURL(ctx context.Context) (string, error)
}
// Lease represents an acquired lease from a Leaser.
type Lease interface {
RenewedAt() time.Time
TTL() time.Duration
// Renew attempts to reset the TTL on the lease.
// Returns ErrLeaseExpired if the lease has expired or was deleted.
Renew(ctx context.Context) error
// Close attempts to remove the lease from the server.
Close() error
}
func assert(condition bool, msg string) {
if !condition {
panic("assertion failed: " + msg)
}
}