-
Notifications
You must be signed in to change notification settings - Fork 23
/
restore.go
306 lines (256 loc) · 7.7 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
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
package exchange
import (
"bytes"
"context"
"errors"
"runtime/trace"
"time"
"github.com/alcionai/clues"
"github.com/cenkalti/backoff/v4"
"github.com/microsoftgraph/msgraph-sdk-go/models"
"github.com/alcionai/corso/src/internal/common/ptr"
"github.com/alcionai/corso/src/internal/data"
"github.com/alcionai/corso/src/internal/diagnostics"
"github.com/alcionai/corso/src/internal/m365/support"
"github.com/alcionai/corso/src/internal/observe"
"github.com/alcionai/corso/src/pkg/backup/details"
"github.com/alcionai/corso/src/pkg/control"
"github.com/alcionai/corso/src/pkg/count"
"github.com/alcionai/corso/src/pkg/errs/core"
"github.com/alcionai/corso/src/pkg/fault"
"github.com/alcionai/corso/src/pkg/logger"
"github.com/alcionai/corso/src/pkg/path"
"github.com/alcionai/corso/src/pkg/services/m365/api/graph"
)
// RestoreCollection handles restoration of an individual collection.
func RestoreCollection(
ctx context.Context,
ir itemRestorer,
dc data.RestoreCollection,
resourceID, destinationID string,
collisionKeyToItemID map[string]string,
collisionPolicy control.CollisionPolicy,
deets *details.Builder,
errs *fault.Bus,
ctr *count.Bus,
) (support.CollectionMetrics, error) {
ctx, end := diagnostics.Span(ctx, "m365:exchange:restoreCollection", diagnostics.Label("path", dc.FullPath()))
defer end()
var (
el = errs.Local()
metrics support.CollectionMetrics
items = dc.Items(ctx, errs)
fullPath = dc.FullPath()
category = fullPath.Category()
)
progressMessage := observe.CollectionProgress(
ctx,
category.HumanString(),
fullPath.Folder(false))
defer close(progressMessage)
for {
select {
case <-ctx.Done():
return metrics, clues.WrapWC(ctx, ctx.Err(), "context cancelled")
case itemData, ok := <-items:
if !ok || el.Failure() != nil {
return metrics, el.Failure()
}
ictx := clues.Add(ctx, "item_id", itemData.ID())
trace.Log(ictx, "m365:exchange:restoreCollection:item", itemData.ID())
metrics.Objects++
buf := &bytes.Buffer{}
_, err := buf.ReadFrom(itemData.ToReader())
if err != nil {
el.AddRecoverable(ictx, clues.WrapWC(ictx, err, "reading item bytes"))
continue
}
body := buf.Bytes()
info, err := ir.restore(
ictx,
body,
resourceID,
destinationID,
collisionKeyToItemID,
collisionPolicy,
errs,
ctr)
if err != nil {
if !errors.Is(err, core.ErrAlreadyExists) {
el.AddRecoverable(ictx, clues.Wrap(err, "restoring item"))
}
continue
}
metrics.Bytes += int64(len(body))
metrics.Successes++
// FIXME: this may be the incorrect path. If we restored within a top-level
// destination folder, then the restore path no longer matches the fullPath.
itemPath, err := fullPath.AppendItem(itemData.ID())
if err != nil {
el.AddRecoverable(ictx, clues.WrapWC(ictx, err, "adding item to collection path"))
continue
}
locationRef := path.Builder{}.Append(itemPath.Folders()...)
err = deets.Add(
itemPath,
locationRef,
details.ItemInfo{
Exchange: info,
})
if err != nil {
// These deets additions are for cli display purposes only.
// no need to fail out on error.
logger.Ctx(ictx).Infow("accounting for restored item", "error", err)
}
progressMessage <- struct{}{}
}
}
}
// CreateDestination creates folders in sequence
// [root leaf1 leaf2] similar to a linked list.
// @param directory is the desired path from the root to the container
// that the items will be restored into.
func CreateDestination(
ctx context.Context,
ca containerAPI,
destination *path.Builder,
resourceID string,
gcr graph.ContainerResolver,
errs *fault.Bus,
) (string, graph.ContainerResolver, error) {
var (
cache = gcr
restoreLoc = &path.Builder{}
containerParentID string
)
for _, container := range destination.Elements() {
restoreLoc = restoreLoc.Append(container)
ictx := clues.Add(
ctx,
"container_parent_id", containerParentID,
"container_name", container,
"restore_location", restoreLoc)
containerID, err := getOrPopulateContainer(
ictx,
ca,
cache,
restoreLoc,
resourceID,
containerParentID,
container,
errs)
if err != nil {
return "", cache, clues.Stack(err)
}
containerParentID = containerID
}
// containerParentID now identifies the last created container,
// not its parent.
return containerParentID, cache, nil
}
func getOrPopulateContainer(
ctx context.Context,
ca containerAPI,
gcr graph.ContainerResolver,
restoreLoc *path.Builder,
resourceID, containerParentID, containerName string,
errs *fault.Bus,
) (string, error) {
cached, ok := gcr.LocationInCache(restoreLoc.String())
if ok {
return cached, nil
}
c, err := ca.CreateContainer(ctx, resourceID, containerParentID, containerName)
// 409 handling case:
// attempt to fetch the container by name and add that result to the cache.
// This is rare, but may happen if CreateContainer() POST fails with 5xx:
// sometimes the backend will create the folder despite the 5xx response,
// leaving our local containerResolver with inconsistent state.
if graph.IsErrFolderExists(err) {
cc, e := ca.GetContainerByName(ctx, resourceID, containerParentID, containerName)
if e != nil {
err = clues.Stack(err, e)
} else {
c = cc
err = nil
}
}
if err != nil {
return "", clues.Wrap(err, "creating restore container")
}
folderID := ptr.Val(c.GetId())
if err = gcr.AddToCache(ctx, c); err != nil {
return "", clues.Wrap(err, "adding container to cache")
}
return folderID, nil
}
const (
maxRetries = 3
retryInterval = 750 * time.Millisecond
retryMultiplier = 2
retryMaxInterval = 5 * time.Second
retryMaxElapsedTime = 0
)
func uploadAttachments(
ctx context.Context,
ap attachmentPoster,
as []models.Attachmentable,
resourceID, destinationID, itemID string,
errs *fault.Bus,
) error {
el := errs.Local()
// Manual testing showed that even small delays between retries could get us
// past the issues. Use an exponential backoff that will allow us to keep
// runtimes low if there's just one failure for a given attachment but still
// waits a reasonable amount of time if there's multiple failures.
retryBackoff := backoff.NewExponentialBackOff()
retryBackoff.InitialInterval = retryInterval
retryBackoff.Multiplier = retryMultiplier
retryBackoff.MaxInterval = retryMaxInterval
retryBackoff.MaxElapsedTime = retryMaxElapsedTime
for i, a := range as {
if el.Failure() != nil {
return el.Failure()
}
actx := clues.Add(ctx, "attachment_num", i)
var (
retry int
err error
)
retryBackoff.Reset()
for (retry == 0 || errors.Is(err, core.ErrNotFound)) && retry <= maxRetries {
retry++
ictx := clues.Add(actx, "attempt_num", retry)
err = uploadAttachment(
ictx,
ap,
resourceID,
destinationID,
itemID,
a)
// Sometimes graph returns a 404 when we try to post the attachment.
// We're not sure why, but maybe it has to do with attaching many items.
// In any case, wait a little while and try again.
if errors.Is(err, core.ErrNotFound) && retry <= maxRetries {
waitTime := retryBackoff.NextBackOff()
logger.Ctx(ictx).Infow(
"error uploading attachment, retrying",
"retry_after", waitTime)
time.Sleep(waitTime)
}
}
if err != nil {
// FIXME: I don't know why we're swallowing this error case.
// It needs investigation: https://github.com/alcionai/corso/issues/3498
if ptr.Val(a.GetOdataType()) == "#microsoft.graph.itemAttachment" {
name := ptr.Val(a.GetName())
logger.CtxErr(ctx, err).
With("attachment_name", name).
Info("mail upload failed")
continue
}
el.AddRecoverable(actx, clues.WrapWC(ctx, err, "uploading mail attachment"))
}
}
return el.Failure()
}