/
restore.go
200 lines (176 loc) · 5.53 KB
/
restore.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
// Copyright 2014 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package state
import (
"github.com/juju/errors"
jujutxn "github.com/juju/txn"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
"gopkg.in/mgo.v2/txn"
)
// RestoreStatus is the type of the statuses
type RestoreStatus string
// Validate returns an errors if status' value is not known.
func (status RestoreStatus) Validate() error {
switch status {
case RestorePending, RestoreInProgress, RestoreFinished:
case RestoreChecked, RestoreFailed, RestoreNotActive:
default:
return errors.Errorf("unknown restore status: %v", status)
}
return nil
}
const (
currentRestoreId = "current"
// RestoreNotActive is not persisted in the database, and is
// used to indicate the absence of a current restore doc.
RestoreNotActive RestoreStatus = "NOT-RESTORING"
// RestorePending is a status to signal that a restore is about
// to start any change done in this status will be lost.
RestorePending RestoreStatus = "PENDING"
// RestoreInProgress indicates that a Restore is in progress.
RestoreInProgress RestoreStatus = "RESTORING"
// RestoreFinished it is set by restore upon a succesful run.
RestoreFinished RestoreStatus = "RESTORED"
// RestoreChecked is set when the server comes up after a
// succesful restore.
RestoreChecked RestoreStatus = "CHECKED"
// RestoreFailed indicates that the process failed in a
// recoverable step.
RestoreFailed RestoreStatus = "FAILED"
)
// RestoreInfo exposes restore status.
func (st *State) RestoreInfo() *RestoreInfo {
return &RestoreInfo{st: st}
}
// RestoreInfo exposes restore status.
type RestoreInfo struct {
st *State
}
// Status returns the current Restore doc status
func (info *RestoreInfo) Status() (RestoreStatus, error) {
restoreInfo, closer := info.st.getCollection(restoreInfoC)
defer closer()
var doc struct {
Status RestoreStatus `bson:"status"`
}
err := restoreInfo.FindId(currentRestoreId).One(&doc)
switch errors.Cause(err) {
case nil:
case mgo.ErrNotFound:
return RestoreNotActive, nil
default:
return "", errors.Annotate(err, "cannot read restore info")
}
if err := doc.Status.Validate(); err != nil {
return "", errors.Trace(err)
}
return doc.Status, nil
}
// PurgeTxn purges missing transation from restoreInfoC collection.
// These can be caused because this collection is heavy use while backing
// up and mongo 3.2 does not like this.
func (info *RestoreInfo) PurgeTxn() error {
restoreInfo, closer := info.st.getRawCollection(restoreInfoC)
defer closer()
r := txn.NewRunner(restoreInfo)
return r.PurgeMissing(restoreInfoC)
}
// SetStatus sets the status of the current restore. Checks are made
// to ensure that status changes are performed in the correct order.
func (info *RestoreInfo) SetStatus(status RestoreStatus) error {
if err := status.Validate(); err != nil {
return errors.Trace(err)
}
buildTxn := func(_ int) ([]txn.Op, error) {
current, err := info.Status()
if err != nil {
return nil, errors.Annotate(err, "cannot read current status")
}
if current == status {
return nil, jujutxn.ErrNoOperations
}
ops, err := setRestoreStatusOps(current, status)
if err != nil {
return nil, errors.Trace(err)
}
return ops, nil
}
if err := info.st.run(buildTxn); err != nil {
return errors.Annotatef(err, "setting status %q", status)
}
return nil
}
// setRestoreStatusOps checks the validity of the supplied transition,
// and returns either an error or a list of transaction operations that
// will apply the transition.
func setRestoreStatusOps(before, after RestoreStatus) ([]txn.Op, error) {
errInvalid := errors.Errorf("invalid restore transition: %s => %s", before, after)
switch after {
case RestorePending:
switch before {
case RestoreNotActive:
return createRestoreStatusPendingOps(), nil
case RestoreFailed, RestoreChecked:
default:
return nil, errInvalid
}
case RestoreFailed:
switch before {
case RestoreNotActive, RestoreChecked:
return nil, errInvalid
}
case RestoreInProgress:
if before != RestorePending {
return nil, errInvalid
}
case RestoreFinished:
// RestoreFinished is set after a restore so we cannot ensure
// what will be on the db state since it will deppend on
// what was set during backup.
switch before {
case RestoreNotActive:
return createRestoreStatusFinishedOps(), nil
case RestoreFailed:
// except for the case of Failed,this is most likely a race condition.
return nil, errInvalid
}
case RestoreChecked:
if before != RestoreFinished {
return nil, errInvalid
}
default:
return nil, errInvalid
}
return updateRestoreStatusChangeOps(before, after), nil
}
// createRestoreStatusFinishedOps is useful when setting finished on
// a non initated restore document.
func createRestoreStatusFinishedOps() []txn.Op {
return []txn.Op{{
C: restoreInfoC,
Id: currentRestoreId,
Assert: txn.DocMissing,
Insert: bson.D{{"status", RestoreFinished}},
}}
}
// createRestoreStatusPendingOps is the only valid way to create a
// restore document.
func createRestoreStatusPendingOps() []txn.Op {
return []txn.Op{{
C: restoreInfoC,
Id: currentRestoreId,
Assert: txn.DocMissing,
Insert: bson.D{{"status", RestorePending}},
}}
}
// updateRestoreStatusChangeOps will set the restore doc status to
// after, so long as the doc's status is before.
func updateRestoreStatusChangeOps(before, after RestoreStatus) []txn.Op {
return []txn.Op{{
C: restoreInfoC,
Id: currentRestoreId,
Assert: bson.D{{"status", before}},
Update: bson.D{{"$set", bson.D{{"status", after}}}},
}}
}