-
Notifications
You must be signed in to change notification settings - Fork 347
Expand file tree
/
Copy pathserver.go
More file actions
581 lines (486 loc) · 14.9 KB
/
server.go
File metadata and controls
581 lines (486 loc) · 14.9 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
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
package litestream
import (
"context"
"encoding/json"
"errors"
"fmt"
"log/slog"
"net"
"net/http"
"net/http/pprof"
"os"
"sync"
"time"
)
// SocketConfig configures the Unix socket for control commands.
type SocketConfig struct {
Enabled bool `yaml:"enabled"`
Path string `yaml:"path"`
Permissions uint32 `yaml:"permissions"`
}
// DefaultSocketConfig returns the default socket configuration.
func DefaultSocketConfig() SocketConfig {
return SocketConfig{
Enabled: false,
Path: "/var/run/litestream.sock",
Permissions: 0600,
}
}
// Server manages runtime control via Unix socket using HTTP.
type Server struct {
store *Store
// SocketPath is the path to the Unix socket.
SocketPath string
// SocketPerms is the file permissions for the socket.
SocketPerms uint32
// PathExpander optionally expands paths (e.g., ~ expansion).
// If nil, paths are used as-is.
PathExpander func(string) (string, error)
// Version is the version string to report in /info.
Version string
// startedAt is set when the server starts.
startedAt time.Time
socketListener net.Listener
httpServer *http.Server
ctx context.Context
cancel context.CancelFunc
wg sync.WaitGroup
logger *slog.Logger
}
// NewServer creates a new Server instance.
func NewServer(store *Store) *Server {
ctx, cancel := context.WithCancel(context.Background())
s := &Server{
store: store,
SocketPerms: 0600,
ctx: ctx,
cancel: cancel,
logger: slog.Default().With(LogKeySystem, LogSystemServer),
}
mux := http.NewServeMux()
mux.HandleFunc("POST /start", s.handleStart)
mux.HandleFunc("POST /stop", s.handleStop)
mux.HandleFunc("GET /txid", s.handleTXID)
mux.HandleFunc("POST /register", s.handleRegister)
mux.HandleFunc("POST /unregister", s.handleUnregister)
mux.HandleFunc("POST /sync", s.handleSync)
mux.HandleFunc("GET /list", s.handleList)
mux.HandleFunc("GET /info", s.handleInfo)
// pprof endpoints
mux.HandleFunc("GET /debug/pprof/", pprof.Index)
mux.HandleFunc("GET /debug/pprof/cmdline", pprof.Cmdline)
mux.HandleFunc("GET /debug/pprof/profile", pprof.Profile)
mux.HandleFunc("GET /debug/pprof/symbol", pprof.Symbol)
mux.HandleFunc("GET /debug/pprof/trace", pprof.Trace)
s.httpServer = &http.Server{Handler: mux}
return s
}
// Start begins listening for control connections.
func (s *Server) Start() error {
if s.SocketPath == "" {
return fmt.Errorf("socket path required")
}
// Check if socket file exists and is actually a socket before removing
if info, err := os.Lstat(s.SocketPath); err == nil {
if info.Mode()&os.ModeSocket != 0 {
if err := os.Remove(s.SocketPath); err != nil {
return fmt.Errorf("remove existing socket: %w", err)
}
} else {
return fmt.Errorf("socket path exists but is not a socket: %s", s.SocketPath)
}
} else if !os.IsNotExist(err) {
return fmt.Errorf("check socket path: %w", err)
}
listener, err := net.Listen("unix", s.SocketPath)
if err != nil {
return fmt.Errorf("listen on unix socket: %w", err)
}
s.socketListener = listener
if err := os.Chmod(s.SocketPath, os.FileMode(s.SocketPerms)); err != nil {
listener.Close()
return fmt.Errorf("chmod socket: %w", err)
}
// Set startedAt after successful socket setup to ensure uptime reflects
// the actual time the server became available.
s.startedAt = time.Now()
s.logger.Info("control socket listening", "path", s.SocketPath)
s.wg.Add(1)
go func() {
defer s.wg.Done()
if err := s.httpServer.Serve(listener); err != nil && err != http.ErrServerClosed {
s.logger.Error("http server error", "error", err)
}
}()
return nil
}
// Close gracefully shuts down the control server.
func (s *Server) Close() error {
s.cancel()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if s.httpServer != nil {
if err := s.httpServer.Shutdown(ctx); err != nil {
s.logger.Error("http server shutdown error", "error", err)
}
}
s.wg.Wait()
return nil
}
// expandPath expands the path using PathExpander if set.
func (s *Server) expandPath(path string) (string, error) {
if s.PathExpander != nil {
return s.PathExpander(path)
}
return path, nil
}
func (s *Server) handleStart(w http.ResponseWriter, r *http.Request) {
var req StartRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
writeJSONError(w, http.StatusBadRequest, "invalid request body", err.Error())
return
}
if req.Path == "" {
writeJSONError(w, http.StatusBadRequest, "path required", nil)
return
}
expandedPath, err := s.expandPath(req.Path)
if err != nil {
writeJSONError(w, http.StatusBadRequest, fmt.Sprintf("invalid path: %v", err), nil)
return
}
ctx := s.ctx
if req.Timeout > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(s.ctx, time.Duration(req.Timeout)*time.Second)
defer cancel()
}
if err := s.store.EnableDB(ctx, expandedPath); err != nil {
writeJSONError(w, http.StatusInternalServerError, err.Error(), nil)
return
}
writeJSON(w, http.StatusOK, StartResponse{
Status: "started",
Path: expandedPath,
})
}
func (s *Server) handleStop(w http.ResponseWriter, r *http.Request) {
var req StopRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
writeJSONError(w, http.StatusBadRequest, "invalid request body", err.Error())
return
}
if req.Path == "" {
writeJSONError(w, http.StatusBadRequest, "path required", nil)
return
}
expandedPath, err := s.expandPath(req.Path)
if err != nil {
writeJSONError(w, http.StatusBadRequest, fmt.Sprintf("invalid path: %v", err), nil)
return
}
timeout := req.Timeout
if timeout == 0 {
timeout = 30
}
ctx, cancel := context.WithTimeout(s.ctx, time.Duration(timeout)*time.Second)
defer cancel()
if err := s.store.DisableDB(ctx, expandedPath); err != nil {
writeJSONError(w, http.StatusInternalServerError, err.Error(), nil)
return
}
writeJSON(w, http.StatusOK, StopResponse{
Status: "stopped",
Path: expandedPath,
})
}
func (s *Server) handleTXID(w http.ResponseWriter, r *http.Request) {
path := r.URL.Query().Get("path")
if path == "" {
writeJSONError(w, http.StatusBadRequest, "path required", nil)
return
}
expandedPath, err := s.expandPath(path)
if err != nil {
writeJSONError(w, http.StatusBadRequest, fmt.Sprintf("invalid path: %v", err), nil)
return
}
db := s.store.FindDB(expandedPath)
if db == nil {
writeJSONError(w, http.StatusNotFound, "database not found", nil)
return
}
pos, err := db.Pos()
if err != nil {
writeJSONError(w, http.StatusInternalServerError, err.Error(), nil)
return
}
writeJSON(w, http.StatusOK, TXIDResponse{
TXID: uint64(pos.TXID),
})
}
func writeJSON(w http.ResponseWriter, status int, v interface{}) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
json.NewEncoder(w).Encode(v)
}
func writeJSONError(w http.ResponseWriter, status int, message string, details interface{}) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
json.NewEncoder(w).Encode(ErrorResponse{
Error: message,
Details: details,
})
}
// StartRequest is the request body for the /start endpoint.
type StartRequest struct {
Path string `json:"path"`
Timeout int `json:"timeout,omitempty"`
}
// StartResponse is the response body for the /start endpoint.
type StartResponse struct {
Status string `json:"status"`
Path string `json:"path"`
}
// StopRequest is the request body for the /stop endpoint.
type StopRequest struct {
Path string `json:"path"`
Timeout int `json:"timeout,omitempty"`
}
// StopResponse is the response body for the /stop endpoint.
type StopResponse struct {
Status string `json:"status"`
Path string `json:"path"`
}
// ErrorResponse is returned when an error occurs.
type ErrorResponse struct {
Error string `json:"error"`
Details interface{} `json:"details,omitempty"`
}
// TXIDResponse is the response body for the /txid endpoint.
type TXIDResponse struct {
TXID uint64 `json:"txid"`
}
func (s *Server) handleSync(w http.ResponseWriter, r *http.Request) {
var req SyncRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
writeJSONError(w, http.StatusBadRequest, "invalid request body", err.Error())
return
}
if req.Path == "" {
writeJSONError(w, http.StatusBadRequest, "path required", nil)
return
}
expandedPath, err := s.expandPath(req.Path)
if err != nil {
writeJSONError(w, http.StatusBadRequest, fmt.Sprintf("invalid path: %v", err), nil)
return
}
ctx := s.ctx
if req.Wait && req.Timeout == 0 {
req.Timeout = 30
}
if req.Timeout > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(s.ctx, time.Duration(req.Timeout)*time.Second)
defer cancel()
}
result, err := s.store.SyncDB(ctx, expandedPath, req.Wait)
if err != nil {
switch {
case errors.Is(err, ErrDatabaseNotFound):
writeJSONError(w, http.StatusNotFound, err.Error(), nil)
case errors.Is(err, ErrDatabaseNotOpen):
writeJSONError(w, http.StatusConflict, err.Error(), nil)
default:
writeJSONError(w, http.StatusInternalServerError, err.Error(), nil)
}
return
}
var status string
if !result.Changed {
status = "no_change"
} else if req.Wait {
status = "synced"
} else {
status = "synced_local"
}
writeJSON(w, http.StatusOK, SyncResponse{
Status: status,
Path: expandedPath,
TXID: result.TXID,
ReplicatedTXID: result.ReplicatedTXID,
})
}
// SyncRequest is the request body for the /sync endpoint.
type SyncRequest struct {
Path string `json:"path"`
Wait bool `json:"wait,omitempty"`
Timeout int `json:"timeout,omitempty"`
}
// SyncResponse is the response body for the /sync endpoint.
type SyncResponse struct {
Status string `json:"status"`
Path string `json:"path"`
TXID uint64 `json:"txid"`
ReplicatedTXID uint64 `json:"replicated_txid"`
}
func (s *Server) handleList(w http.ResponseWriter, _ *http.Request) {
dbs := s.store.DBs()
resp := ListResponse{
Databases: make([]DatabaseSummary, 0, len(dbs)),
}
for _, db := range dbs {
var status string
if db.IsOpen() {
if db.Replica != nil && db.Replica.MonitorEnabled {
status = "replicating"
} else {
status = "open"
}
} else {
status = "stopped"
}
summary := DatabaseSummary{
Path: db.Path(),
Status: status,
}
if t := db.LastSuccessfulSyncAt(); !t.IsZero() {
summary.LastSyncAt = &t
}
resp.Databases = append(resp.Databases, summary)
}
writeJSON(w, http.StatusOK, resp)
}
func (s *Server) handleInfo(w http.ResponseWriter, _ *http.Request) {
resp := InfoResponse{
Version: s.Version,
PID: os.Getpid(),
StartedAt: s.startedAt,
UptimeSeconds: int64(time.Since(s.startedAt).Seconds()),
DatabaseCount: len(s.store.DBs()),
}
writeJSON(w, http.StatusOK, resp)
}
// ListResponse is the response body for the /list endpoint.
type ListResponse struct {
Databases []DatabaseSummary `json:"databases"`
}
// DatabaseSummary contains summary information about a database.
type DatabaseSummary struct {
Path string `json:"path"`
Status string `json:"status"`
// LastSyncAt is the timestamp of the last successful replica sync.
// This reflects when data was last successfully uploaded to the replica
// storage backend, not just when the local WAL was processed.
LastSyncAt *time.Time `json:"last_sync_at,omitempty"`
}
// InfoResponse is the response body for the /info endpoint.
type InfoResponse struct {
Version string `json:"version"`
PID int `json:"pid"`
UptimeSeconds int64 `json:"uptime_seconds"`
StartedAt time.Time `json:"started_at"`
DatabaseCount int `json:"database_count"`
}
// RegisterDatabaseRequest is the request body for the /register endpoint.
type RegisterDatabaseRequest struct {
Path string `json:"path"`
ReplicaURL string `json:"replica_url"`
}
// RegisterDatabaseResponse is the response body for the /register endpoint.
type RegisterDatabaseResponse struct {
Status string `json:"status"`
Path string `json:"path"`
}
// UnregisterDatabaseRequest is the request body for the /unregister endpoint.
type UnregisterDatabaseRequest struct {
Path string `json:"path"`
Timeout int `json:"timeout,omitempty"`
}
// UnregisterDatabaseResponse is the response body for the /unregister endpoint.
type UnregisterDatabaseResponse struct {
Status string `json:"status"`
Path string `json:"path"`
}
func (s *Server) handleRegister(w http.ResponseWriter, r *http.Request) {
var req RegisterDatabaseRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
writeJSONError(w, http.StatusBadRequest, "invalid request body", err.Error())
return
}
if req.Path == "" {
writeJSONError(w, http.StatusBadRequest, "path required", nil)
return
}
if req.ReplicaURL == "" {
writeJSONError(w, http.StatusBadRequest, "replica_url required", nil)
return
}
expandedPath, err := s.expandPath(req.Path)
if err != nil {
writeJSONError(w, http.StatusBadRequest, fmt.Sprintf("invalid path: %v", err), nil)
return
}
// Check if database already exists.
if existing := s.store.FindDB(expandedPath); existing != nil {
writeJSON(w, http.StatusOK, RegisterDatabaseResponse{
Status: "already_exists",
Path: expandedPath,
})
return
}
// Create replica client from URL.
client, err := NewReplicaClientFromURL(req.ReplicaURL)
if err != nil {
writeJSONError(w, http.StatusBadRequest, fmt.Sprintf("invalid replica url: %v", err), nil)
return
}
// Create new database.
db := NewDB(expandedPath)
// Create replica and attach client.
replica := NewReplica(db)
replica.Client = client
db.Replica = replica
// Register database with store (this also opens the database).
if err := s.store.RegisterDB(db); err != nil {
writeJSONError(w, http.StatusInternalServerError, fmt.Sprintf("failed to register database: %v", err), nil)
return
}
writeJSON(w, http.StatusOK, RegisterDatabaseResponse{
Status: "registered",
Path: expandedPath,
})
}
func (s *Server) handleUnregister(w http.ResponseWriter, r *http.Request) {
var req UnregisterDatabaseRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
writeJSONError(w, http.StatusBadRequest, "invalid request body", err.Error())
return
}
if req.Path == "" {
writeJSONError(w, http.StatusBadRequest, "path required", nil)
return
}
expandedPath, err := s.expandPath(req.Path)
if err != nil {
writeJSONError(w, http.StatusBadRequest, fmt.Sprintf("invalid path: %v", err), nil)
return
}
// Set up timeout context. Treat non-positive values as default.
timeout := req.Timeout
if timeout <= 0 {
timeout = 30
}
ctx, cancel := context.WithTimeout(s.ctx, time.Duration(timeout)*time.Second)
defer cancel()
// Remove database from store (this also closes it).
if err := s.store.UnregisterDB(ctx, expandedPath); err != nil {
writeJSONError(w, http.StatusInternalServerError, fmt.Sprintf("failed to unregister database: %v", err), nil)
return
}
writeJSON(w, http.StatusOK, UnregisterDatabaseResponse{
Status: "unregistered",
Path: expandedPath,
})
}