-
Notifications
You must be signed in to change notification settings - Fork 190
/
SyncMutationToCloudOperation.swift
388 lines (346 loc) · 17.3 KB
/
SyncMutationToCloudOperation.swift
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
//
// Copyright Amazon.com Inc. or its affiliates.
// All Rights Reserved.
//
// SPDX-License-Identifier: Apache-2.0
//
import Amplify
import Combine
import Foundation
import AWSPluginsCore
/// Publishes a mutation event to the specified Cloud API. Upon receipt of the API response, validates to ensure it is
/// not a retriable error. If it is, attempts a retry until either success or terminal failure. Upon success or
/// terminal failure, publishes the event response to the appropriate ModelReconciliationQueue subject.
class SyncMutationToCloudOperation: AsynchronousOperation {
typealias MutationSyncCloudResult = GraphQLOperation<MutationSync<AnyModel>>.OperationResult
private weak var api: APICategoryGraphQLBehaviorExtended?
private let mutationEvent: MutationEvent
private let getLatestSyncMetadata: () -> MutationSyncMetadata?
private let completion: GraphQLOperation<MutationSync<AnyModel>>.ResultListener
private let requestRetryablePolicy: RequestRetryablePolicy
private let lock: NSRecursiveLock
private var networkReachabilityPublisher: AnyPublisher<ReachabilityUpdate, Never>?
private var mutationOperation: GraphQLOperation<MutationSync<AnyModel>>?
private var mutationRetryNotifier: MutationRetryNotifier?
private var currentAttemptNumber: Int
private var authTypesIterator: AWSAuthorizationTypeIterator?
init(mutationEvent: MutationEvent,
getLatestSyncMetadata: @escaping () -> MutationSyncMetadata?,
api: APICategoryGraphQLBehaviorExtended,
authModeStrategy: AuthModeStrategy,
networkReachabilityPublisher: AnyPublisher<ReachabilityUpdate, Never>? = nil,
currentAttemptNumber: Int = 1,
requestRetryablePolicy: RequestRetryablePolicy? = RequestRetryablePolicy(),
completion: @escaping GraphQLOperation<MutationSync<AnyModel>>.ResultListener) async {
self.mutationEvent = mutationEvent
self.getLatestSyncMetadata = getLatestSyncMetadata
self.api = api
self.networkReachabilityPublisher = networkReachabilityPublisher
self.completion = completion
self.currentAttemptNumber = currentAttemptNumber
self.requestRetryablePolicy = requestRetryablePolicy ?? RequestRetryablePolicy()
self.lock = NSRecursiveLock()
if let modelSchema = ModelRegistry.modelSchema(from: mutationEvent.modelName),
let mutationType = GraphQLMutationType(rawValue: mutationEvent.mutationType) {
self.authTypesIterator = await authModeStrategy.authTypesFor(schema: modelSchema,
operation: mutationType.toModelOperation())
}
super.init()
}
override func main() {
log.verbose(#function)
sendMutationToCloud(withAuthType: authTypesIterator?.next())
}
override func cancel() {
log.verbose(#function)
lock.execute {
mutationOperation?.cancel()
mutationRetryNotifier?.cancel()
mutationRetryNotifier = nil
}
let apiError = APIError(error: OperationCancelledError())
finish(result: .failure(apiError))
}
/// Does not require a locking context. Member access is read-only.
private func sendMutationToCloud(withAuthType authType: AWSAuthorizationType? = nil) {
guard !isCancelled else {
return
}
log.debug(#function)
guard let mutationType = GraphQLMutationType(rawValue: mutationEvent.mutationType) else {
let dataStoreError = DataStoreError.decodingError(
"Invalid mutation type",
"""
The incoming mutation event had a mutation type of \(mutationEvent.mutationType), which does not
match any known GraphQL mutation type. Ensure you only send valid mutation types:
\(GraphQLMutationType.allCases)
"""
)
log.error(error: dataStoreError)
let apiError = APIError.unknown("Invalid mutation type", "", dataStoreError)
finish(result: .failure(apiError))
return
}
if let apiRequest = createAPIRequest(mutationType: mutationType, authType: authType) {
sendMutation(describedBy: apiRequest)
}
}
/// Always retrieve and use the largest version when available. The source of the version comes
/// from either the MutationEvent itself, which represents the queue request, or the persisted version
/// from the metadata table.
///
/// **Version in the Mutation Event**. If there are mulitple mutation events pending, each outgoing
/// mutation processing will result in synchronously updating the pending mutation's version
/// before enqueuing the mutation response for reconciliation.
///
/// **Version persisted in the metadata table**: Reconciliation will persist the latest version in the
/// metadata table. In cases of quick consecutive updates, the MutationEvent's version could
/// be greater than the persisted since the MutationEvent is updated from the original thread that
/// processed the outgoing mutation.
private func getLatestVersion(_ mutationEvent: MutationEvent) -> Int? {
let latestSyncedMetadataVersion = getLatestSyncMetadata()?.version
let mutationEventVersion = mutationEvent.version
switch (latestSyncedMetadataVersion, mutationEventVersion) {
case let (.some(syncedVersion), .some(version)):
return max(syncedVersion, version)
case let (.some(syncedVersion), .none):
return syncedVersion
case let (.none, .some(version)):
return version
case (.none, .none):
return nil
}
}
/// Creates a GraphQLRequest based on given `mutationType`
/// - Parameters:
/// - mutationType: mutation type
/// - authType: authorization type, if provided overrides the auth used to perform the API request
/// - Returns: a GraphQL request
private func createAPIRequest(
mutationType: GraphQLMutationType,
authType: AWSAuthorizationType? = nil
) -> GraphQLRequest<MutationSync<AnyModel>>? {
let version = getLatestVersion(mutationEvent)
var request: GraphQLRequest<MutationSync<AnyModel>>
do {
var graphQLFilter: GraphQLFilter?
if let graphQLFilterJSON = mutationEvent.graphQLFilterJSON {
graphQLFilter = try GraphQLFilterConverter.fromJSON(graphQLFilterJSON)
}
switch mutationType {
case .delete:
let model = try mutationEvent.decodeModel()
guard let modelSchema = ModelRegistry.modelSchema(from: mutationEvent.modelName) else {
return Fatal.preconditionFailure("""
Could not retrieve schema for the model \(mutationEvent.modelName), verify that datastore is
initialized.
""")
}
request = GraphQLRequest<MutationSyncResult>.deleteMutation(of: model,
modelSchema: modelSchema,
where: graphQLFilter,
version: version)
case .update:
let model = try mutationEvent.decodeModel()
guard let modelSchema = ModelRegistry.modelSchema(from: mutationEvent.modelName) else {
return Fatal.preconditionFailure("""
Could not retrieve schema for the model \(mutationEvent.modelName), verify that datastore is
initialized.
""")
}
request = GraphQLRequest<MutationSyncResult>.updateMutation(of: model,
modelSchema: modelSchema,
where: graphQLFilter,
version: version)
case .create:
let model = try mutationEvent.decodeModel()
guard let modelSchema = ModelRegistry.modelSchema(from: mutationEvent.modelName) else {
return Fatal.preconditionFailure("""
Could not retrieve schema for the model \(mutationEvent.modelName), verify that datastore is
initialized.
""")
}
request = GraphQLRequest<MutationSyncResult>.createMutation(of: model,
modelSchema: modelSchema,
version: version)
}
} catch {
let apiError = APIError.unknown("Couldn't decode model", "", error)
finish(result: .failure(apiError))
return nil
}
let awsPluginOptions = AWSAPIPluginDataStoreOptions(authType: authType,
modelName: mutationEvent.modelName)
request.options = GraphQLRequest<MutationSyncResult>.Options(pluginOptions: awsPluginOptions)
return request
}
/// Creates and invokes a GraphQL mutation operation for `apiRequest` to the
/// service, and invokes `respond(toCloudResult:withAPIRequest:)` in the mutation's
/// completion handler
/// - Parameter apiRequest: The GraphQLRequest used to create the mutation operation
private func sendMutation(describedBy apiRequest: GraphQLRequest<MutationSync<AnyModel>>) {
guard let api = api else {
log.error("\(#function): API unexpectedly nil")
let apiError = APIError.unknown("API unexpectedly nil", "")
finish(result: .failure(apiError))
return
}
log.verbose("\(#function) sending mutation with sync data: \(apiRequest)")
lock.execute {
mutationOperation = api.mutate(request: apiRequest) { [weak self] result in
self?.respond(toCloudResult: result, withAPIRequest: apiRequest)
}
}
}
/// Initiates a locking context
private func respond(
toCloudResult result: GraphQLOperation<MutationSync<AnyModel>>.OperationResult,
withAPIRequest apiRequest: GraphQLRequest<MutationSync<AnyModel>>
) {
lock.execute {
guard !self.isCancelled else {
Amplify.log.debug("SyncMutationToCloudOperation cancelled, aborting")
return
}
log.verbose("GraphQL mutation operation received result: \(result)")
validate(cloudResult: result, request: apiRequest)
}
}
/// - Warning: Must be invoked from a locking context
private func validate(cloudResult: MutationSyncCloudResult,
request: GraphQLRequest<MutationSync<AnyModel>>) {
guard !isCancelled else {
return
}
if case .failure(let error) = cloudResult {
let advice = getRetryAdviceIfRetryable(error: error)
guard advice.shouldRetry else {
finish(result: .failure(error))
return
}
resolveReachabilityPublisher(request: request)
if let pluginOptions = request.options?.pluginOptions as? AWSAPIPluginDataStoreOptions, pluginOptions.authType != nil,
let nextAuthType = authTypesIterator?.next() {
scheduleRetry(advice: advice, withAuthType: nextAuthType)
} else {
scheduleRetry(advice: advice)
}
return
}
finish(result: cloudResult)
}
/// - Warning: Must be invoked from a locking context
private func resolveReachabilityPublisher(request: GraphQLRequest<MutationSync<AnyModel>>) {
if networkReachabilityPublisher == nil {
if let reachability = api as? APICategoryReachabilityBehavior {
do {
#if os(watchOS)
networkReachabilityPublisher = try reachability.reachabilityPublisher()
#else
networkReachabilityPublisher = try reachability.reachabilityPublisher(for: request.apiName)
#endif
} catch {
log.error("\(#function): Unable to listen on reachability: \(error)")
}
}
}
}
/// - Warning: Must be invoked from a locking context
func getRetryAdviceIfRetryable(error: APIError) -> RequestRetryAdvice {
var advice = RequestRetryAdvice(shouldRetry: false, retryInterval: DispatchTimeInterval.never)
switch error {
case .networkError(_, _, let error):
// currently expecting APIOperationResponse to be an URLError
let urlError = error as? URLError
advice = requestRetryablePolicy.retryRequestAdvice(urlError: urlError,
httpURLResponse: nil,
attemptNumber: currentAttemptNumber)
// we can't unify the following two cases (case 1 and case 2) as they have different associated values.
// should retry with a different authType if server returned "Unauthorized Error"
case .httpStatusError(_, let httpURLResponse) where httpURLResponse.statusCode == 401: // case 1
advice = shouldRetryWithDifferentAuthType()
case .operationError(_, _, let error): // case 2
if let authError = error as? AuthError { // case 2
// Not all AuthError's are unauthorized errors. If `AuthError.sessionExpired` or `.signedOut` then
// the request never made it to the server. We should keep trying until the user is signed in.
// Otherwise we may be making the wrong determination to remove this mutation event.
switch authError {
case .sessionExpired, .signedOut:
// use `userAuthenticationRequired` to ensure advice to retry is true.
advice = requestRetryablePolicy.retryRequestAdvice(urlError: URLError(.userAuthenticationRequired),
httpURLResponse: nil,
attemptNumber: currentAttemptNumber)
default:
// should retry with a different authType if request failed locally with any other AuthError
advice = shouldRetryWithDifferentAuthType()
}
}
case .httpStatusError(_, let httpURLResponse):
advice = requestRetryablePolicy.retryRequestAdvice(urlError: nil,
httpURLResponse: httpURLResponse,
attemptNumber: currentAttemptNumber)
default:
break
}
return advice
}
/// - Warning: Must be invoked from a locking context
private func shouldRetryWithDifferentAuthType() -> RequestRetryAdvice {
let shouldRetry = (authTypesIterator?.count ?? 0) > 0
return RequestRetryAdvice(shouldRetry: shouldRetry, retryInterval: .milliseconds(0))
}
/// - Warning: Must be invoked from a locking context
private func scheduleRetry(advice: RequestRetryAdvice,
withAuthType authType: AWSAuthorizationType? = nil) {
log.verbose("\(#function) scheduling retry for mutation \(advice)")
mutationRetryNotifier = MutationRetryNotifier(
advice: advice,
networkReachabilityPublisher: networkReachabilityPublisher
) { [weak self] in
self?.respondToMutationNotifierTriggered(withAuthType: authType)
}
currentAttemptNumber += 1
}
/// Initiates a locking context
private func respondToMutationNotifierTriggered(withAuthType authType: AWSAuthorizationType?) {
log.verbose("\(#function) mutationRetryNotifier triggered")
lock.execute {
sendMutationToCloud(withAuthType: authType)
mutationRetryNotifier = nil
}
}
/// Cleans up operation resources, finalizes AsynchronousOperation states, and invokes `completion` with `result`
/// - Parameter result: The MutationSyncCloudResult to pass to `completion`
private func finish(result: MutationSyncCloudResult) {
log.verbose(#function)
lock.execute {
mutationOperation?.removeResultListener()
mutationOperation = nil
}
DispatchQueue.global().async {
self.completion(result)
}
finish()
}
}
// MARK: - GraphQLMutationType + toModelOperation
private extension GraphQLMutationType {
func toModelOperation() -> ModelOperation {
switch self {
case .create:
return .create
case .update:
return .update
case .delete:
return .delete
}
}
}
extension SyncMutationToCloudOperation: DefaultLogger {
public static var log: Logger {
Amplify.Logging.logger(forCategory: CategoryType.dataStore.displayName, forNamespace: String(describing: self))
}
public var log: Logger {
Self.log
}
}