-
Notifications
You must be signed in to change notification settings - Fork 307
/
recovery.go
84 lines (68 loc) · 1.81 KB
/
recovery.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
package service
import (
"context"
"fmt"
"sync"
"github.com/rudderlabs/rudder-server/warehouse/internal/model"
warehouseutils "github.com/rudderlabs/rudder-server/warehouse/utils"
"golang.org/x/exp/slices"
)
// TODO: Remove warehouseutils.POSTGRES once the postgres new implementation is stable
var crashRecoverWarehouses = []string{
warehouseutils.RS,
warehouseutils.MSSQL,
warehouseutils.AZURE_SYNAPSE,
warehouseutils.DELTALAKE,
warehouseutils.POSTGRES,
}
type repo interface {
InterruptedDestinations(ctx context.Context, destinationType string) ([]string, error)
}
type destination interface {
CrashRecover()
}
type Recovery struct {
detectOnce sync.Once
detectErr error
destinationType string
repo repo
inRecovery map[string]*sync.Once
}
func NewRecovery(destinationType string, repo repo) *Recovery {
return &Recovery{
destinationType: destinationType,
repo: repo,
inRecovery: make(map[string]*sync.Once),
}
}
// Detect detects if there are any warehouses that need to be recovered.
func (r *Recovery) detect(ctx context.Context) error {
if !slices.Contains(crashRecoverWarehouses, r.destinationType) {
return nil
}
destIDs, err := r.repo.InterruptedDestinations(ctx, r.destinationType)
if err != nil {
return fmt.Errorf("repo interrupted destinations: %w", err)
}
for _, destID := range destIDs {
r.inRecovery[destID] = &sync.Once{}
}
return nil
}
// Recover recovers a warehouse, for a non-graceful shutdown.
func (r *Recovery) Recover(ctx context.Context, whManager destination, wh model.Warehouse) error {
r.detectOnce.Do(func() {
r.detectErr = r.detect(ctx)
})
if r.detectErr != nil {
return r.detectErr
}
once, ok := r.inRecovery[wh.Destination.ID]
if !ok {
return nil
}
once.Do(func() {
whManager.CrashRecover()
})
return nil
}