FSTRemoteStore.m 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "FSTRemoteStore.h"
  17. #import "FSTAssert.h"
  18. #import "FSTDatastore.h"
  19. #import "FSTDocument.h"
  20. #import "FSTDocumentKey.h"
  21. #import "FSTExistenceFilter.h"
  22. #import "FSTLocalStore.h"
  23. #import "FSTLogger.h"
  24. #import "FSTMutation.h"
  25. #import "FSTMutationBatch.h"
  26. #import "FSTQuery.h"
  27. #import "FSTQueryData.h"
  28. #import "FSTRemoteEvent.h"
  29. #import "FSTSnapshotVersion.h"
  30. #import "FSTTransaction.h"
  31. #import "FSTWatchChange.h"
  32. NS_ASSUME_NONNULL_BEGIN
  33. /**
  34. * The maximum number of pending writes to allow.
  35. * TODO(bjornick): Negotiate this value with the backend.
  36. */
  37. static const NSUInteger kMaxPendingWrites = 10;
  38. #pragma mark - FSTRemoteStore
  39. @interface FSTRemoteStore () <FSTWatchStreamDelegate, FSTWriteStreamDelegate>
  40. - (instancetype)initWithLocalStore:(FSTLocalStore *)localStore
  41. datastore:(FSTDatastore *)datastore NS_DESIGNATED_INITIALIZER;
  42. /**
  43. * The local store, used to fill the write pipeline with outbound mutations and resolve existence
  44. * filter mismatches. Immutable after initialization.
  45. */
  46. @property(nonatomic, strong, readonly) FSTLocalStore *localStore;
  47. /** The client-side proxy for interacting with the backend. Immutable after initialization. */
  48. @property(nonatomic, strong, readonly) FSTDatastore *datastore;
  49. #pragma mark Watch Stream
  50. // The watchStream is null when the network is disabled. The non-null check is performed by
  51. // isNetworkEnabled.
  52. @property(nonatomic, strong, nullable) FSTWatchStream *watchStream;
  53. /**
  54. * A mapping of watched targets that the client cares about tracking and the
  55. * user has explicitly called a 'listen' for this target.
  56. *
  57. * These targets may or may not have been sent to or acknowledged by the
  58. * server. On re-establishing the listen stream, these targets should be sent
  59. * to the server. The targets removed with unlistens are removed eagerly
  60. * without waiting for confirmation from the listen stream. */
  61. @property(nonatomic, strong, readonly)
  62. NSMutableDictionary<FSTBoxedTargetID *, FSTQueryData *> *listenTargets;
  63. /**
  64. * A mapping of targetId to pending acks needed.
  65. *
  66. * If a targetId is present in this map, then we're waiting for watch to
  67. * acknowledge a removal or addition of the target. If a target is not in this
  68. * mapping, and it's in the listenTargets map, then we consider the target to
  69. * be active.
  70. *
  71. * We increment the count here everytime we issue a request over the stream to
  72. * watch or unwatch. We then decrement the count everytime we get a target
  73. * added or target removed message from the server. Once the count is equal to
  74. * 0 we know that the client and server are in the same state (once this state
  75. * is reached the targetId is removed from the map to free the memory).
  76. */
  77. @property(nonatomic, strong, readonly)
  78. NSMutableDictionary<FSTBoxedTargetID *, NSNumber *> *pendingTargetResponses;
  79. @property(nonatomic, strong) NSMutableArray<FSTWatchChange *> *accumulatedChanges;
  80. @property(nonatomic, assign) FSTBatchID lastBatchSeen;
  81. /**
  82. * The online state of the watch stream. The state is set to healthy if and only if there are
  83. * messages received by the backend.
  84. */
  85. @property(nonatomic, assign) FSTOnlineState watchStreamOnlineState;
  86. #pragma mark Write Stream
  87. // The writeStream is null when the network is disabled. The non-null check is performed by
  88. // isNetworkEnabled.
  89. @property(nonatomic, strong, nullable) FSTWriteStream *writeStream;
  90. /**
  91. * The approximate time the StreamingWrite stream was opened. Used to estimate if stream was
  92. * closed due to an auth expiration (a recoverable error) or some other more permanent error.
  93. */
  94. @property(nonatomic, strong, nullable) NSDate *writeStreamOpenTime;
  95. /**
  96. * A FIFO queue of in-flight writes. This is in-flight from the point of view of the caller of
  97. * writeMutations, not from the point of view from the Datastore itself. In particular, these
  98. * requests may not have been sent to the Datastore server if the write stream is not yet running.
  99. */
  100. @property(nonatomic, strong, readonly) NSMutableArray<FSTMutationBatch *> *pendingWrites;
  101. @end
  102. @implementation FSTRemoteStore
  103. + (instancetype)remoteStoreWithLocalStore:(FSTLocalStore *)localStore
  104. datastore:(FSTDatastore *)datastore {
  105. return [[FSTRemoteStore alloc] initWithLocalStore:localStore datastore:datastore];
  106. }
  107. - (instancetype)initWithLocalStore:(FSTLocalStore *)localStore datastore:(FSTDatastore *)datastore {
  108. if (self = [super init]) {
  109. _localStore = localStore;
  110. _datastore = datastore;
  111. _listenTargets = [NSMutableDictionary dictionary];
  112. _pendingTargetResponses = [NSMutableDictionary dictionary];
  113. _accumulatedChanges = [NSMutableArray array];
  114. _lastBatchSeen = kFSTBatchIDUnknown;
  115. _watchStreamOnlineState = FSTOnlineStateUnknown;
  116. _pendingWrites = [NSMutableArray array];
  117. }
  118. return self;
  119. }
  120. - (void)start {
  121. // For now, all setup is handled by enableNetwork(). We might expand on this in the future.
  122. [self enableNetwork];
  123. }
  124. - (void)updateAndNotifyAboutOnlineState:(FSTOnlineState)watchStreamOnlineState {
  125. BOOL didChange = (watchStreamOnlineState != self.watchStreamOnlineState);
  126. self.watchStreamOnlineState = watchStreamOnlineState;
  127. if (didChange) {
  128. [self.onlineStateDelegate watchStreamDidChangeOnlineState:watchStreamOnlineState];
  129. }
  130. }
  131. #pragma mark Online/Offline state
  132. - (BOOL)isNetworkEnabled {
  133. FSTAssert((self.watchStream == nil) == (self.writeStream == nil),
  134. @"WatchStream and WriteStream should both be null or non-null");
  135. return self.watchStream != nil;
  136. }
  137. - (void)enableNetwork {
  138. FSTAssert(self.watchStream == nil, @"enableNetwork: called with non-null watchStream.");
  139. FSTAssert(self.writeStream == nil, @"enableNetwork: called with non-null writeStream.");
  140. // Create new streams (but note they're not started yet).
  141. self.watchStream = [self.datastore createWatchStreamWithDelegate:self];
  142. self.writeStream = [self.datastore createWriteStreamWithDelegate:self];
  143. // Load any saved stream token from persistent storage
  144. self.writeStream.lastStreamToken = [self.localStore lastStreamToken];
  145. if ([self shouldStartWatchStream]) {
  146. [self startWatchStream];
  147. }
  148. [self fillWritePipeline]; // This may start the writeStream.
  149. // We move back to the unknown state because we might not want to re-open the stream
  150. [self updateAndNotifyAboutOnlineState:FSTOnlineStateUnknown];
  151. }
  152. - (void)disableNetwork {
  153. [self updateAndNotifyAboutOnlineState:FSTOnlineStateFailed];
  154. // NOTE: We're guaranteed not to get any further events from these streams (not even a close
  155. // event).
  156. [self.watchStream stop];
  157. [self.writeStream stop];
  158. [self cleanUpWatchStreamState];
  159. [self cleanUpWriteStreamState];
  160. self.writeStream = nil;
  161. self.watchStream = nil;
  162. }
  163. #pragma mark Shutdown
  164. - (void)shutdown {
  165. FSTLog(@"FSTRemoteStore %p shutting down", (__bridge void *)self);
  166. // Don't fire initial listener callbacks on shutdown.
  167. self.onlineStateDelegate = nil;
  168. // For now, all shutdown logic is handled by disableNetwork(). We might expand on this in the
  169. // future.
  170. if ([self isNetworkEnabled]) {
  171. [self disableNetwork];
  172. }
  173. }
  174. - (void)userDidChange:(FSTUser *)user {
  175. FSTLog(@"FSTRemoteStore %p changing users: %@", (__bridge void *)self, user);
  176. // Tear down and re-create our network streams. This will ensure we get a fresh auth token
  177. // for the new user and re-fill the write pipeline with new mutations from the LocalStore
  178. // (since mutations are per-user).
  179. [self disableNetwork];
  180. [self enableNetwork];
  181. }
  182. #pragma mark Watch Stream
  183. - (void)startWatchStream {
  184. FSTAssert([self shouldStartWatchStream],
  185. @"startWatchStream: called when shouldStartWatchStream: is false.");
  186. [self.watchStream start];
  187. }
  188. - (void)listenToTargetWithQueryData:(FSTQueryData *)queryData {
  189. NSNumber *targetKey = @(queryData.targetID);
  190. FSTAssert(!self.listenTargets[targetKey], @"listenToQuery called with duplicate target id: %@",
  191. targetKey);
  192. self.listenTargets[targetKey] = queryData;
  193. if ([self shouldStartWatchStream]) {
  194. [self.watchStream start];
  195. } else if ([self isNetworkEnabled] && [self.watchStream isOpen]) {
  196. [self sendWatchRequestWithQueryData:queryData];
  197. }
  198. }
  199. - (void)sendWatchRequestWithQueryData:(FSTQueryData *)queryData {
  200. [self recordPendingRequestForTargetID:@(queryData.targetID)];
  201. [self.watchStream watchQuery:queryData];
  202. }
  203. - (void)stopListeningToTargetID:(FSTTargetID)targetID {
  204. FSTBoxedTargetID *targetKey = @(targetID);
  205. FSTQueryData *queryData = self.listenTargets[targetKey];
  206. FSTAssert(queryData, @"unlistenToTarget: target not currently watched: %@", targetKey);
  207. [self.listenTargets removeObjectForKey:targetKey];
  208. if ([self isNetworkEnabled] && [self.watchStream isOpen]) {
  209. [self sendUnwatchRequestForTargetID:targetKey];
  210. }
  211. }
  212. - (void)sendUnwatchRequestForTargetID:(FSTBoxedTargetID *)targetID {
  213. [self recordPendingRequestForTargetID:targetID];
  214. [self.watchStream unwatchTargetID:[targetID intValue]];
  215. }
  216. - (void)recordPendingRequestForTargetID:(FSTBoxedTargetID *)targetID {
  217. NSNumber *count = [self.pendingTargetResponses objectForKey:targetID];
  218. count = @([count intValue] + 1);
  219. [self.pendingTargetResponses setObject:count forKey:targetID];
  220. }
  221. /**
  222. * Returns YES if the network is enabled, the watch stream has not yet been started and there are
  223. * active watch targets.
  224. */
  225. - (BOOL)shouldStartWatchStream {
  226. return [self isNetworkEnabled] && ![self.watchStream isStarted] && self.listenTargets.count > 0;
  227. }
  228. - (void)cleanUpWatchStreamState {
  229. // If the connection is closed then we'll never get a snapshot version for the accumulated
  230. // changes and so we'll never be able to complete the batch. When we start up again the server
  231. // is going to resend these changes anyway, so just toss the accumulated state.
  232. [self.accumulatedChanges removeAllObjects];
  233. [self.pendingTargetResponses removeAllObjects];
  234. }
  235. - (void)watchStreamDidOpen {
  236. // Restore any existing watches.
  237. for (FSTQueryData *queryData in [self.listenTargets objectEnumerator]) {
  238. [self sendWatchRequestWithQueryData:queryData];
  239. }
  240. }
  241. - (void)watchStreamDidChange:(FSTWatchChange *)change
  242. snapshotVersion:(FSTSnapshotVersion *)snapshotVersion {
  243. // Mark the connection as healthy because we got a message from the server.
  244. [self updateAndNotifyAboutOnlineState:FSTOnlineStateHealthy];
  245. FSTWatchTargetChange *watchTargetChange =
  246. [change isKindOfClass:[FSTWatchTargetChange class]] ? (FSTWatchTargetChange *)change : nil;
  247. if (watchTargetChange && watchTargetChange.state == FSTWatchTargetChangeStateRemoved &&
  248. watchTargetChange.cause) {
  249. // There was an error on a target, don't wait for a consistent snapshot to raise events
  250. [self processTargetErrorForWatchChange:(FSTWatchTargetChange *)change];
  251. } else {
  252. // Accumulate watch changes but don't process them if there's no snapshotVersion or it's
  253. // older than a previous snapshot we've processed (can happen after we resume a target
  254. // using a resume token).
  255. [self.accumulatedChanges addObject:change];
  256. FSTAssert(snapshotVersion, @"snapshotVersion must not be nil.");
  257. if ([snapshotVersion isEqual:[FSTSnapshotVersion noVersion]] ||
  258. [snapshotVersion compare:[self.localStore lastRemoteSnapshotVersion]] ==
  259. NSOrderedAscending) {
  260. return;
  261. }
  262. // Create a batch, giving it the accumulatedChanges array.
  263. NSArray<FSTWatchChange *> *changes = self.accumulatedChanges;
  264. self.accumulatedChanges = [NSMutableArray array];
  265. [self processBatchedWatchChanges:changes snapshotVersion:snapshotVersion];
  266. }
  267. }
  268. - (void)watchStreamDidClose:(NSError *_Nullable)error {
  269. FSTAssert([self isNetworkEnabled],
  270. @"watchStreamDidClose should only be called when the network is enabled");
  271. [self cleanUpWatchStreamState];
  272. // If the watch stream closed due to an error, retry the connection if there are any active
  273. // watch targets.
  274. if ([self shouldStartWatchStream]) {
  275. // If the connection fails before the stream has become healthy, consider the online state
  276. // failed. Otherwise consider the online state unknown and the next connection attempt will
  277. // resolve the online state. For example, if a healthy stream is closed due to an expired token
  278. // we want to have one more try at reconnecting before we consider the connection unhealthy.
  279. if (self.watchStreamOnlineState == FSTOnlineStateHealthy) {
  280. [self updateAndNotifyAboutOnlineState:FSTOnlineStateUnknown];
  281. } else {
  282. [self updateAndNotifyAboutOnlineState:FSTOnlineStateFailed];
  283. }
  284. [self.watchStream start];
  285. } else {
  286. // We don't need to restart the watch stream because there are no active targets. The online
  287. // state is set to unknown because there is no active attempt at establishing a connection.
  288. [self updateAndNotifyAboutOnlineState:FSTOnlineStateUnknown];
  289. }
  290. }
  291. /**
  292. * Takes a batch of changes from the Datastore, repackages them as a RemoteEvent, and passes that
  293. * on to the SyncEngine.
  294. */
  295. - (void)processBatchedWatchChanges:(NSArray<FSTWatchChange *> *)changes
  296. snapshotVersion:(FSTSnapshotVersion *)snapshotVersion {
  297. FSTWatchChangeAggregator *aggregator =
  298. [[FSTWatchChangeAggregator alloc] initWithSnapshotVersion:snapshotVersion
  299. listenTargets:self.listenTargets
  300. pendingTargetResponses:self.pendingTargetResponses];
  301. [aggregator addWatchChanges:changes];
  302. FSTRemoteEvent *remoteEvent = [aggregator remoteEvent];
  303. [self.pendingTargetResponses removeAllObjects];
  304. [self.pendingTargetResponses setDictionary:aggregator.pendingTargetResponses];
  305. // Handle existence filters and existence filter mismatches
  306. [aggregator.existenceFilters enumerateKeysAndObjectsUsingBlock:^(FSTBoxedTargetID *target,
  307. FSTExistenceFilter *filter,
  308. BOOL *stop) {
  309. FSTTargetID targetID = target.intValue;
  310. FSTQueryData *queryData = self.listenTargets[target];
  311. FSTQuery *query = queryData.query;
  312. if (!queryData) {
  313. // A watched target might have been removed already.
  314. return;
  315. } else if ([query isDocumentQuery]) {
  316. if (filter.count == 0) {
  317. // The existence filter told us the document does not exist.
  318. // We need to deduce that this document does not exist and apply a deleted document to our
  319. // updates. Without applying a deleted document there might be another query that will
  320. // raise this document as part of a snapshot until it is resolved, essentially exposing
  321. // inconsistency between queries
  322. FSTDocumentKey *key = [FSTDocumentKey keyWithPath:query.path];
  323. FSTDeletedDocument *deletedDoc =
  324. [FSTDeletedDocument documentWithKey:key version:snapshotVersion];
  325. [remoteEvent addDocumentUpdate:deletedDoc];
  326. } else {
  327. FSTAssert(filter.count == 1, @"Single document existence filter with count: %" PRId32,
  328. filter.count);
  329. }
  330. } else {
  331. // Not a document query.
  332. FSTDocumentKeySet *trackedRemote = [self.localStore remoteDocumentKeysForTarget:targetID];
  333. FSTTargetMapping *mapping = remoteEvent.targetChanges[target].mapping;
  334. if (mapping) {
  335. if ([mapping isKindOfClass:[FSTUpdateMapping class]]) {
  336. FSTUpdateMapping *update = (FSTUpdateMapping *)mapping;
  337. trackedRemote = [update applyTo:trackedRemote];
  338. } else {
  339. FSTAssert([mapping isKindOfClass:[FSTResetMapping class]],
  340. @"Expected either reset or update mapping but got something else %@", mapping);
  341. trackedRemote = ((FSTResetMapping *)mapping).documents;
  342. }
  343. }
  344. if (trackedRemote.count != (NSUInteger)filter.count) {
  345. FSTLog(@"Existence filter mismatch, resetting mapping");
  346. // Make sure the mismatch is exposed in the remote event
  347. [remoteEvent handleExistenceFilterMismatchForTargetID:target];
  348. // Clear the resume token for the query, since we're in a known mismatch state.
  349. queryData =
  350. [[FSTQueryData alloc] initWithQuery:query targetID:targetID purpose:queryData.purpose];
  351. self.listenTargets[target] = queryData;
  352. // Cause a hard reset by unwatching and rewatching immediately, but deliberately don't
  353. // send a resume token so that we get a full update.
  354. [self sendUnwatchRequestForTargetID:@(targetID)];
  355. // Mark the query we send as being on behalf of an existence filter mismatch, but don't
  356. // actually retain that in listenTargets. This ensures that we flag the first re-listen
  357. // this way without impacting future listens of this target (that might happen e.g. on
  358. // reconnect).
  359. FSTQueryData *requestQueryData =
  360. [[FSTQueryData alloc] initWithQuery:query
  361. targetID:targetID
  362. purpose:FSTQueryPurposeExistenceFilterMismatch];
  363. [self sendWatchRequestWithQueryData:requestQueryData];
  364. }
  365. }
  366. }];
  367. // Update in-memory resume tokens. FSTLocalStore will update the persistent view of these when
  368. // applying the completed FSTRemoteEvent.
  369. [remoteEvent.targetChanges enumerateKeysAndObjectsUsingBlock:^(
  370. FSTBoxedTargetID *target, FSTTargetChange *change, BOOL *stop) {
  371. NSData *resumeToken = change.resumeToken;
  372. if (resumeToken.length > 0) {
  373. FSTQueryData *queryData = _listenTargets[target];
  374. // A watched target might have been removed already.
  375. if (queryData) {
  376. _listenTargets[target] =
  377. [queryData queryDataByReplacingSnapshotVersion:change.snapshotVersion
  378. resumeToken:resumeToken];
  379. }
  380. }
  381. }];
  382. // Finally handle remote event
  383. [self.syncEngine applyRemoteEvent:remoteEvent];
  384. }
  385. /** Process a target error and passes the error along to SyncEngine. */
  386. - (void)processTargetErrorForWatchChange:(FSTWatchTargetChange *)change {
  387. FSTAssert(change.cause, @"Handling target error without a cause");
  388. // Ignore targets that have been removed already.
  389. for (FSTBoxedTargetID *targetID in change.targetIDs) {
  390. if (self.listenTargets[targetID]) {
  391. [self.listenTargets removeObjectForKey:targetID];
  392. [self.syncEngine rejectListenWithTargetID:targetID error:change.cause];
  393. }
  394. }
  395. }
  396. #pragma mark Write Stream
  397. /**
  398. * Returns YES if the network is enabled, the write stream has not yet been started and there are
  399. * pending writes.
  400. */
  401. - (BOOL)shouldStartWriteStream {
  402. return [self isNetworkEnabled] && ![self.writeStream isStarted] && self.pendingWrites.count > 0;
  403. }
  404. - (void)startWriteStream {
  405. FSTAssert([self shouldStartWriteStream],
  406. @"startWriteStream: called when shouldStartWriteStream: is false.");
  407. [self.writeStream start];
  408. }
  409. - (void)cleanUpWriteStreamState {
  410. self.lastBatchSeen = kFSTBatchIDUnknown;
  411. [self.pendingWrites removeAllObjects];
  412. }
  413. - (void)fillWritePipeline {
  414. while ([self canWriteMutations]) {
  415. FSTMutationBatch *batch = [self.localStore nextMutationBatchAfterBatchID:self.lastBatchSeen];
  416. if (!batch) {
  417. break;
  418. }
  419. [self commitBatch:batch];
  420. }
  421. }
  422. /**
  423. * Returns YES if the backend can accept additional write requests.
  424. *
  425. * When sending mutations to the write stream (e.g. in -fillWritePipeline), call this method first
  426. * to check if more mutations can be sent.
  427. *
  428. * Currently the only thing that can prevent the backend from accepting write requests is if
  429. * there are too many requests already outstanding. As writes complete the backend will be able
  430. * to accept more.
  431. */
  432. - (BOOL)canWriteMutations {
  433. return [self isNetworkEnabled] && self.pendingWrites.count < kMaxPendingWrites;
  434. }
  435. /** Given mutations to commit, actually commits them to the backend. */
  436. - (void)commitBatch:(FSTMutationBatch *)batch {
  437. FSTAssert([self canWriteMutations], @"commitBatch called when mutations can't be written");
  438. self.lastBatchSeen = batch.batchID;
  439. [self.pendingWrites addObject:batch];
  440. if ([self shouldStartWriteStream]) {
  441. [self startWriteStream];
  442. } else if ([self isNetworkEnabled] && self.writeStream.handshakeComplete) {
  443. [self.writeStream writeMutations:batch.mutations];
  444. }
  445. }
  446. - (void)writeStreamDidOpen {
  447. self.writeStreamOpenTime = [NSDate date];
  448. [self.writeStream writeHandshake];
  449. }
  450. /**
  451. * Handles a successful handshake response from the server, which is our cue to send any pending
  452. * writes.
  453. */
  454. - (void)writeStreamDidCompleteHandshake {
  455. // Record the stream token.
  456. [self.localStore setLastStreamToken:self.writeStream.lastStreamToken];
  457. // Drain any pending writes.
  458. //
  459. // Note that at this point pendingWrites contains mutations that have already been accepted by
  460. // fillWritePipeline/commitBatch. If the pipeline is full, canWriteMutations will be NO, despite
  461. // the fact that we actually need to send mutations over.
  462. //
  463. // This also means that this method indirectly respects the limits imposed by canWriteMutations
  464. // since writes can't be added to the pendingWrites array when canWriteMutations is NO. If the
  465. // limits imposed by canWriteMutations actually protect us from DOSing ourselves then those limits
  466. // won't be exceeded here and we'll continue to make progress.
  467. for (FSTMutationBatch *write in self.pendingWrites) {
  468. [self.writeStream writeMutations:write.mutations];
  469. }
  470. }
  471. /** Handles a successful StreamingWriteResponse from the server that contains a mutation result. */
  472. - (void)writeStreamDidReceiveResponseWithVersion:(FSTSnapshotVersion *)commitVersion
  473. mutationResults:(NSArray<FSTMutationResult *> *)results {
  474. // This is a response to a write containing mutations and should be correlated to the first
  475. // pending write.
  476. NSMutableArray *pendingWrites = self.pendingWrites;
  477. FSTMutationBatch *batch = pendingWrites[0];
  478. [pendingWrites removeObjectAtIndex:0];
  479. FSTMutationBatchResult *batchResult =
  480. [FSTMutationBatchResult resultWithBatch:batch
  481. commitVersion:commitVersion
  482. mutationResults:results
  483. streamToken:self.writeStream.lastStreamToken];
  484. [self.syncEngine applySuccessfulWriteWithResult:batchResult];
  485. // It's possible that with the completion of this mutation another slot has freed up.
  486. [self fillWritePipeline];
  487. }
  488. /**
  489. * Handles the closing of the StreamingWrite RPC, either because of an error or because the RPC
  490. * has been terminated by the client or the server.
  491. */
  492. - (void)writeStreamDidClose:(NSError *_Nullable)error {
  493. FSTAssert([self isNetworkEnabled],
  494. @"writeStreamDidClose: should only be called when the network is enabled");
  495. NSMutableArray *pendingWrites = self.pendingWrites;
  496. // Ignore close if there are no pending writes.
  497. if (pendingWrites.count == 0) {
  498. return;
  499. }
  500. FSTAssert(error, @"There are pending writes, but the write stream closed without an error.");
  501. if ([FSTDatastore isPermanentWriteError:error]) {
  502. if (self.writeStream.handshakeComplete) {
  503. // This error affects the actual writes.
  504. [self handleWriteError:error];
  505. } else {
  506. // If there was an error before the handshake finished, it's possible that the server is
  507. // unable to process the stream token we're sending. (Perhaps it's too old?)
  508. [self handleHandshakeError:error];
  509. }
  510. }
  511. // The write stream might have been started by refilling the write pipeline for failed writes
  512. if ([self shouldStartWriteStream]) {
  513. [self.writeStream start];
  514. }
  515. }
  516. - (void)handleHandshakeError:(NSError *)error {
  517. // Reset the token if it's a permanent error or the error code is ABORTED, signaling the write
  518. // stream is no longer valid.
  519. if ([FSTDatastore isPermanentWriteError:error] || [FSTDatastore isAbortedError:error]) {
  520. NSString *token = [self.writeStream.lastStreamToken base64EncodedStringWithOptions:0];
  521. FSTLog(@"FSTRemoteStore %p error before completed handshake; resetting stream token %@: %@",
  522. (__bridge void *)self, token, error);
  523. self.writeStream.lastStreamToken = nil;
  524. [self.localStore setLastStreamToken:nil];
  525. }
  526. }
  527. - (void)handleWriteError:(NSError *)error {
  528. // Only handle permanent error. If it's transient, just let the retry logic kick in.
  529. if (![FSTDatastore isPermanentWriteError:error]) {
  530. return;
  531. }
  532. // If this was a permanent error, the request itself was the problem so it's not going to
  533. // succeed if we resend it.
  534. FSTMutationBatch *batch = self.pendingWrites[0];
  535. [self.pendingWrites removeObjectAtIndex:0];
  536. // In this case it's also unlikely that the server itself is melting down--this was just a
  537. // bad request so inhibit backoff on the next restart.
  538. [self.writeStream inhibitBackoff];
  539. [self.syncEngine rejectFailedWriteWithBatchID:batch.batchID error:error];
  540. // It's possible that with the completion of this mutation another slot has freed up.
  541. [self fillWritePipeline];
  542. }
  543. - (FSTTransaction *)transaction {
  544. return [FSTTransaction transactionWithDatastore:self.datastore];
  545. }
  546. @end
  547. NS_ASSUME_NONNULL_END