FSTRemoteStore.mm 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  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 "Firestore/Source/Remote/FSTRemoteStore.h"
  17. #include <cinttypes>
  18. #import "Firestore/Source/Core/FSTQuery.h"
  19. #import "Firestore/Source/Core/FSTSnapshotVersion.h"
  20. #import "Firestore/Source/Core/FSTTransaction.h"
  21. #import "Firestore/Source/Local/FSTLocalStore.h"
  22. #import "Firestore/Source/Local/FSTQueryData.h"
  23. #import "Firestore/Source/Model/FSTDocument.h"
  24. #import "Firestore/Source/Model/FSTMutation.h"
  25. #import "Firestore/Source/Model/FSTMutationBatch.h"
  26. #import "Firestore/Source/Remote/FSTDatastore.h"
  27. #import "Firestore/Source/Remote/FSTExistenceFilter.h"
  28. #import "Firestore/Source/Remote/FSTOnlineStateTracker.h"
  29. #import "Firestore/Source/Remote/FSTRemoteEvent.h"
  30. #import "Firestore/Source/Remote/FSTStream.h"
  31. #import "Firestore/Source/Remote/FSTWatchChange.h"
  32. #import "Firestore/Source/Util/FSTAssert.h"
  33. #import "Firestore/Source/Util/FSTLogger.h"
  34. #include "Firestore/core/src/firebase/firestore/auth/user.h"
  35. #include "Firestore/core/src/firebase/firestore/model/document_key.h"
  36. #include "Firestore/core/src/firebase/firestore/util/string_apple.h"
  37. namespace util = firebase::firestore::util;
  38. using firebase::firestore::auth::User;
  39. using firebase::firestore::model::DocumentKey;
  40. NS_ASSUME_NONNULL_BEGIN
  41. /**
  42. * The maximum number of pending writes to allow.
  43. * TODO(bjornick): Negotiate this value with the backend.
  44. */
  45. static const int kMaxPendingWrites = 10;
  46. #pragma mark - FSTRemoteStore
  47. @interface FSTRemoteStore () <FSTWatchStreamDelegate, FSTWriteStreamDelegate>
  48. /**
  49. * The local store, used to fill the write pipeline with outbound mutations and resolve existence
  50. * filter mismatches. Immutable after initialization.
  51. */
  52. @property(nonatomic, strong, readonly) FSTLocalStore *localStore;
  53. /** The client-side proxy for interacting with the backend. Immutable after initialization. */
  54. @property(nonatomic, strong, readonly) FSTDatastore *datastore;
  55. #pragma mark Watch Stream
  56. // The watchStream is null when the network is disabled. The non-null check is performed by
  57. // isNetworkEnabled.
  58. @property(nonatomic, strong, nullable) FSTWatchStream *watchStream;
  59. /**
  60. * A mapping of watched targets that the client cares about tracking and the
  61. * user has explicitly called a 'listen' for this target.
  62. *
  63. * These targets may or may not have been sent to or acknowledged by the
  64. * server. On re-establishing the listen stream, these targets should be sent
  65. * to the server. The targets removed with unlistens are removed eagerly
  66. * without waiting for confirmation from the listen stream. */
  67. @property(nonatomic, strong, readonly)
  68. NSMutableDictionary<FSTBoxedTargetID *, FSTQueryData *> *listenTargets;
  69. /**
  70. * A mapping of targetId to pending acks needed.
  71. *
  72. * If a targetId is present in this map, then we're waiting for watch to
  73. * acknowledge a removal or addition of the target. If a target is not in this
  74. * mapping, and it's in the listenTargets map, then we consider the target to
  75. * be active.
  76. *
  77. * We increment the count here everytime we issue a request over the stream to
  78. * watch or unwatch. We then decrement the count everytime we get a target
  79. * added or target removed message from the server. Once the count is equal to
  80. * 0 we know that the client and server are in the same state (once this state
  81. * is reached the targetId is removed from the map to free the memory).
  82. */
  83. @property(nonatomic, strong, readonly)
  84. NSMutableDictionary<FSTBoxedTargetID *, NSNumber *> *pendingTargetResponses;
  85. @property(nonatomic, strong) NSMutableArray<FSTWatchChange *> *accumulatedChanges;
  86. @property(nonatomic, assign) FSTBatchID lastBatchSeen;
  87. @property(nonatomic, strong, readonly) FSTOnlineStateTracker *onlineStateTracker;
  88. #pragma mark Write Stream
  89. // The writeStream is null when the network is disabled. The non-null check is performed by
  90. // isNetworkEnabled.
  91. @property(nonatomic, strong, nullable) FSTWriteStream *writeStream;
  92. /**
  93. * A FIFO queue of in-flight writes. This is in-flight from the point of view of the caller of
  94. * writeMutations, not from the point of view from the Datastore itself. In particular, these
  95. * requests may not have been sent to the Datastore server if the write stream is not yet running.
  96. */
  97. @property(nonatomic, strong, readonly) NSMutableArray<FSTMutationBatch *> *pendingWrites;
  98. @end
  99. @implementation FSTRemoteStore
  100. - (instancetype)initWithLocalStore:(FSTLocalStore *)localStore
  101. datastore:(FSTDatastore *)datastore
  102. workerDispatchQueue:(FSTDispatchQueue *)queue {
  103. if (self = [super init]) {
  104. _localStore = localStore;
  105. _datastore = datastore;
  106. _listenTargets = [NSMutableDictionary dictionary];
  107. _pendingTargetResponses = [NSMutableDictionary dictionary];
  108. _accumulatedChanges = [NSMutableArray array];
  109. _lastBatchSeen = kFSTBatchIDUnknown;
  110. _pendingWrites = [NSMutableArray array];
  111. _onlineStateTracker = [[FSTOnlineStateTracker alloc] initWithWorkerDispatchQueue:queue];
  112. }
  113. return self;
  114. }
  115. - (void)start {
  116. // For now, all setup is handled by enableNetwork(). We might expand on this in the future.
  117. [self enableNetwork];
  118. }
  119. @dynamic onlineStateDelegate;
  120. - (nullable id<FSTOnlineStateDelegate>)onlineStateDelegate {
  121. return self.onlineStateTracker.onlineStateDelegate;
  122. }
  123. - (void)setOnlineStateDelegate:(nullable id<FSTOnlineStateDelegate>)delegate {
  124. self.onlineStateTracker.onlineStateDelegate = delegate;
  125. }
  126. #pragma mark Online/Offline state
  127. - (BOOL)isNetworkEnabled {
  128. FSTAssert((self.watchStream == nil) == (self.writeStream == nil),
  129. @"WatchStream and WriteStream should both be null or non-null");
  130. return self.watchStream != nil;
  131. }
  132. - (void)enableNetwork {
  133. if ([self isNetworkEnabled]) {
  134. return;
  135. }
  136. // Create new streams (but note they're not started yet).
  137. self.watchStream = [self.datastore createWatchStream];
  138. self.writeStream = [self.datastore createWriteStream];
  139. // Load any saved stream token from persistent storage
  140. self.writeStream.lastStreamToken = [self.localStore lastStreamToken];
  141. if ([self shouldStartWatchStream]) {
  142. [self startWatchStream];
  143. } else {
  144. [self.onlineStateTracker updateState:FSTOnlineStateUnknown];
  145. }
  146. [self fillWritePipeline]; // This may start the writeStream.
  147. }
  148. - (void)disableNetwork {
  149. [self disableNetworkInternal];
  150. // Set the FSTOnlineState to Offline so get()s return from cache, etc.
  151. [self.onlineStateTracker updateState:FSTOnlineStateOffline];
  152. }
  153. /** Disables the network, setting the FSTOnlineState to the specified targetOnlineState. */
  154. - (void)disableNetworkInternal {
  155. if ([self isNetworkEnabled]) {
  156. // NOTE: We're guaranteed not to get any further events from these streams (not even a close
  157. // event).
  158. [self.watchStream stop];
  159. [self.writeStream stop];
  160. [self cleanUpWatchStreamState];
  161. [self cleanUpWriteStreamState];
  162. self.writeStream = nil;
  163. self.watchStream = nil;
  164. }
  165. }
  166. #pragma mark Shutdown
  167. - (void)shutdown {
  168. FSTLog(@"FSTRemoteStore %p shutting down", (__bridge void *)self);
  169. [self disableNetworkInternal];
  170. // Set the FSTOnlineState to Unknown (rather than Offline) to avoid potentially triggering
  171. // spurious listener events with cached data, etc.
  172. [self.onlineStateTracker updateState:FSTOnlineStateUnknown];
  173. }
  174. - (void)userDidChange:(const User &)user {
  175. FSTLog(@"FSTRemoteStore %p changing users: %s", (__bridge void *)self, user.uid().c_str());
  176. if ([self isNetworkEnabled]) {
  177. // Tear down and re-create our network streams. This will ensure we get a fresh auth token
  178. // for the new user and re-fill the write pipeline with new mutations from the LocalStore
  179. // (since mutations are per-user).
  180. [self disableNetworkInternal];
  181. [self.onlineStateTracker updateState:FSTOnlineStateUnknown];
  182. [self enableNetwork];
  183. }
  184. }
  185. #pragma mark Watch Stream
  186. - (void)startWatchStream {
  187. FSTAssert([self shouldStartWatchStream],
  188. @"startWatchStream: called when shouldStartWatchStream: is false.");
  189. [self.watchStream startWithDelegate:self];
  190. [self.onlineStateTracker handleWatchStreamStart];
  191. }
  192. - (void)listenToTargetWithQueryData:(FSTQueryData *)queryData {
  193. NSNumber *targetKey = @(queryData.targetID);
  194. FSTAssert(!self.listenTargets[targetKey], @"listenToQuery called with duplicate target id: %@",
  195. targetKey);
  196. self.listenTargets[targetKey] = queryData;
  197. if ([self shouldStartWatchStream]) {
  198. [self startWatchStream];
  199. } else if ([self isNetworkEnabled] && [self.watchStream isOpen]) {
  200. [self sendWatchRequestWithQueryData:queryData];
  201. }
  202. }
  203. - (void)sendWatchRequestWithQueryData:(FSTQueryData *)queryData {
  204. [self recordPendingRequestForTargetID:@(queryData.targetID)];
  205. [self.watchStream watchQuery:queryData];
  206. }
  207. - (void)stopListeningToTargetID:(FSTTargetID)targetID {
  208. FSTBoxedTargetID *targetKey = @(targetID);
  209. FSTQueryData *queryData = self.listenTargets[targetKey];
  210. FSTAssert(queryData, @"unlistenToTarget: target not currently watched: %@", targetKey);
  211. [self.listenTargets removeObjectForKey:targetKey];
  212. if ([self isNetworkEnabled] && [self.watchStream isOpen]) {
  213. [self sendUnwatchRequestForTargetID:targetKey];
  214. if ([self.listenTargets count] == 0) {
  215. [self.watchStream markIdle];
  216. }
  217. }
  218. }
  219. - (void)sendUnwatchRequestForTargetID:(FSTBoxedTargetID *)targetID {
  220. [self recordPendingRequestForTargetID:targetID];
  221. [self.watchStream unwatchTargetID:[targetID intValue]];
  222. }
  223. - (void)recordPendingRequestForTargetID:(FSTBoxedTargetID *)targetID {
  224. NSNumber *count = [self.pendingTargetResponses objectForKey:targetID];
  225. count = @([count intValue] + 1);
  226. [self.pendingTargetResponses setObject:count forKey:targetID];
  227. }
  228. /**
  229. * Returns YES if the network is enabled, the watch stream has not yet been started and there are
  230. * active watch targets.
  231. */
  232. - (BOOL)shouldStartWatchStream {
  233. return [self isNetworkEnabled] && ![self.watchStream isStarted] && self.listenTargets.count > 0;
  234. }
  235. - (void)cleanUpWatchStreamState {
  236. // If the connection is closed then we'll never get a snapshot version for the accumulated
  237. // changes and so we'll never be able to complete the batch. When we start up again the server
  238. // is going to resend these changes anyway, so just toss the accumulated state.
  239. [self.accumulatedChanges removeAllObjects];
  240. [self.pendingTargetResponses removeAllObjects];
  241. }
  242. - (void)watchStreamDidOpen {
  243. // Restore any existing watches.
  244. for (FSTQueryData *queryData in [self.listenTargets objectEnumerator]) {
  245. [self sendWatchRequestWithQueryData:queryData];
  246. }
  247. }
  248. - (void)watchStreamDidChange:(FSTWatchChange *)change
  249. snapshotVersion:(FSTSnapshotVersion *)snapshotVersion {
  250. // Mark the connection as Online because we got a message from the server.
  251. [self.onlineStateTracker updateState:FSTOnlineStateOnline];
  252. FSTWatchTargetChange *watchTargetChange =
  253. [change isKindOfClass:[FSTWatchTargetChange class]] ? (FSTWatchTargetChange *)change : nil;
  254. if (watchTargetChange && watchTargetChange.state == FSTWatchTargetChangeStateRemoved &&
  255. watchTargetChange.cause) {
  256. // There was an error on a target, don't wait for a consistent snapshot to raise events
  257. [self processTargetErrorForWatchChange:(FSTWatchTargetChange *)change];
  258. } else {
  259. // Accumulate watch changes but don't process them if there's no snapshotVersion or it's
  260. // older than a previous snapshot we've processed (can happen after we resume a target
  261. // using a resume token).
  262. [self.accumulatedChanges addObject:change];
  263. FSTAssert(snapshotVersion, @"snapshotVersion must not be nil.");
  264. if ([snapshotVersion isEqual:[FSTSnapshotVersion noVersion]] ||
  265. [snapshotVersion compare:[self.localStore lastRemoteSnapshotVersion]] ==
  266. NSOrderedAscending) {
  267. return;
  268. }
  269. // Create a batch, giving it the accumulatedChanges array.
  270. NSArray<FSTWatchChange *> *changes = self.accumulatedChanges;
  271. self.accumulatedChanges = [NSMutableArray array];
  272. [self processBatchedWatchChanges:changes snapshotVersion:snapshotVersion];
  273. }
  274. }
  275. - (void)watchStreamWasInterruptedWithError:(nullable NSError *)error {
  276. FSTAssert([self isNetworkEnabled],
  277. @"watchStreamWasInterruptedWithError: should only be called when the network is "
  278. "enabled");
  279. [self cleanUpWatchStreamState];
  280. [self.onlineStateTracker handleWatchStreamFailure];
  281. // If the watch stream closed due to an error, retry the connection if there are any active
  282. // watch targets.
  283. if ([self shouldStartWatchStream]) {
  284. [self startWatchStream];
  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.onlineStateTracker updateState: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. const DocumentKey key{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 = [[FSTQueryData alloc] initWithQuery:query
  350. targetID:targetID
  351. listenSequenceNumber:queryData.sequenceNumber
  352. purpose:queryData.purpose];
  353. self.listenTargets[target] = queryData;
  354. // Cause a hard reset by unwatching and rewatching immediately, but deliberately don't
  355. // send a resume token so that we get a full update.
  356. [self sendUnwatchRequestForTargetID:@(targetID)];
  357. // Mark the query we send as being on behalf of an existence filter mismatch, but don't
  358. // actually retain that in listenTargets. This ensures that we flag the first re-listen
  359. // this way without impacting future listens of this target (that might happen e.g. on
  360. // reconnect).
  361. FSTQueryData *requestQueryData =
  362. [[FSTQueryData alloc] initWithQuery:query
  363. targetID:targetID
  364. listenSequenceNumber:queryData.sequenceNumber
  365. purpose:FSTQueryPurposeExistenceFilterMismatch];
  366. [self sendWatchRequestWithQueryData:requestQueryData];
  367. }
  368. }
  369. }];
  370. // Update in-memory resume tokens. FSTLocalStore will update the persistent view of these when
  371. // applying the completed FSTRemoteEvent.
  372. [remoteEvent.targetChanges enumerateKeysAndObjectsUsingBlock:^(
  373. FSTBoxedTargetID *target, FSTTargetChange *change, BOOL *stop) {
  374. NSData *resumeToken = change.resumeToken;
  375. if (resumeToken.length > 0) {
  376. FSTQueryData *queryData = self->_listenTargets[target];
  377. // A watched target might have been removed already.
  378. if (queryData) {
  379. self->_listenTargets[target] =
  380. [queryData queryDataByReplacingSnapshotVersion:change.snapshotVersion
  381. resumeToken:resumeToken];
  382. }
  383. }
  384. }];
  385. // Finally handle remote event
  386. [self.syncEngine applyRemoteEvent:remoteEvent];
  387. }
  388. /** Process a target error and passes the error along to SyncEngine. */
  389. - (void)processTargetErrorForWatchChange:(FSTWatchTargetChange *)change {
  390. FSTAssert(change.cause, @"Handling target error without a cause");
  391. // Ignore targets that have been removed already.
  392. for (FSTBoxedTargetID *targetID in change.targetIDs) {
  393. if (self.listenTargets[targetID]) {
  394. [self.listenTargets removeObjectForKey:targetID];
  395. [self.syncEngine rejectListenWithTargetID:[targetID intValue] error:change.cause];
  396. }
  397. }
  398. }
  399. #pragma mark Write Stream
  400. /**
  401. * Returns YES if the network is enabled, the write stream has not yet been started and there are
  402. * pending writes.
  403. */
  404. - (BOOL)shouldStartWriteStream {
  405. return [self isNetworkEnabled] && ![self.writeStream isStarted] && self.pendingWrites.count > 0;
  406. }
  407. - (void)startWriteStream {
  408. FSTAssert([self shouldStartWriteStream],
  409. @"startWriteStream: called when shouldStartWriteStream: is false.");
  410. [self.writeStream startWithDelegate:self];
  411. }
  412. - (void)cleanUpWriteStreamState {
  413. self.lastBatchSeen = kFSTBatchIDUnknown;
  414. FSTLog(@"Stopping write stream with %lu pending writes",
  415. (unsigned long)[self.pendingWrites count]);
  416. [self.pendingWrites removeAllObjects];
  417. }
  418. - (void)fillWritePipeline {
  419. if ([self isNetworkEnabled]) {
  420. while ([self canWriteMutations]) {
  421. FSTMutationBatch *batch = [self.localStore nextMutationBatchAfterBatchID:self.lastBatchSeen];
  422. if (!batch) {
  423. break;
  424. }
  425. [self commitBatch:batch];
  426. }
  427. if ([self.pendingWrites count] == 0) {
  428. [self.writeStream markIdle];
  429. }
  430. }
  431. }
  432. /**
  433. * Returns YES if the backend can accept additional write requests.
  434. *
  435. * When sending mutations to the write stream (e.g. in -fillWritePipeline), call this method first
  436. * to check if more mutations can be sent.
  437. *
  438. * Currently the only thing that can prevent the backend from accepting write requests is if
  439. * there are too many requests already outstanding. As writes complete the backend will be able
  440. * to accept more.
  441. */
  442. - (BOOL)canWriteMutations {
  443. return [self isNetworkEnabled] && self.pendingWrites.count < kMaxPendingWrites;
  444. }
  445. /** Given mutations to commit, actually commits them to the backend. */
  446. - (void)commitBatch:(FSTMutationBatch *)batch {
  447. FSTAssert([self canWriteMutations], @"commitBatch called when mutations can't be written");
  448. self.lastBatchSeen = batch.batchID;
  449. [self.pendingWrites addObject:batch];
  450. if ([self shouldStartWriteStream]) {
  451. [self startWriteStream];
  452. } else if ([self isNetworkEnabled] && self.writeStream.handshakeComplete) {
  453. [self.writeStream writeMutations:batch.mutations];
  454. }
  455. }
  456. - (void)writeStreamDidOpen {
  457. [self.writeStream writeHandshake];
  458. }
  459. /**
  460. * Handles a successful handshake response from the server, which is our cue to send any pending
  461. * writes.
  462. */
  463. - (void)writeStreamDidCompleteHandshake {
  464. // Record the stream token.
  465. [self.localStore setLastStreamToken:self.writeStream.lastStreamToken];
  466. // Drain any pending writes.
  467. //
  468. // Note that at this point pendingWrites contains mutations that have already been accepted by
  469. // fillWritePipeline/commitBatch. If the pipeline is full, canWriteMutations will be NO, despite
  470. // the fact that we actually need to send mutations over.
  471. //
  472. // This also means that this method indirectly respects the limits imposed by canWriteMutations
  473. // since writes can't be added to the pendingWrites array when canWriteMutations is NO. If the
  474. // limits imposed by canWriteMutations actually protect us from DOSing ourselves then those limits
  475. // won't be exceeded here and we'll continue to make progress.
  476. for (FSTMutationBatch *write in self.pendingWrites) {
  477. [self.writeStream writeMutations:write.mutations];
  478. }
  479. }
  480. /** Handles a successful StreamingWriteResponse from the server that contains a mutation result. */
  481. - (void)writeStreamDidReceiveResponseWithVersion:(FSTSnapshotVersion *)commitVersion
  482. mutationResults:(NSArray<FSTMutationResult *> *)results {
  483. // This is a response to a write containing mutations and should be correlated to the first
  484. // pending write.
  485. NSMutableArray *pendingWrites = self.pendingWrites;
  486. FSTMutationBatch *batch = pendingWrites[0];
  487. [pendingWrites removeObjectAtIndex:0];
  488. FSTMutationBatchResult *batchResult =
  489. [FSTMutationBatchResult resultWithBatch:batch
  490. commitVersion:commitVersion
  491. mutationResults:results
  492. streamToken:self.writeStream.lastStreamToken];
  493. [self.syncEngine applySuccessfulWriteWithResult:batchResult];
  494. // It's possible that with the completion of this mutation another slot has freed up.
  495. [self fillWritePipeline];
  496. }
  497. /**
  498. * Handles the closing of the StreamingWrite RPC, either because of an error or because the RPC
  499. * has been terminated by the client or the server.
  500. */
  501. - (void)writeStreamWasInterruptedWithError:(nullable NSError *)error {
  502. FSTAssert([self isNetworkEnabled],
  503. @"writeStreamDidClose: should only be called when the network is enabled");
  504. // If the write stream closed due to an error, invoke the error callbacks if there are pending
  505. // writes.
  506. if (error != nil && self.pendingWrites.count > 0) {
  507. if (self.writeStream.handshakeComplete) {
  508. // This error affects the actual writes.
  509. [self handleWriteError:error];
  510. } else {
  511. // If there was an error before the handshake finished, it's possible that the server is
  512. // unable to process the stream token we're sending. (Perhaps it's too old?)
  513. [self handleHandshakeError:error];
  514. }
  515. }
  516. // The write stream might have been started by refilling the write pipeline for failed writes
  517. if ([self shouldStartWriteStream]) {
  518. [self startWriteStream];
  519. }
  520. }
  521. - (void)handleHandshakeError:(NSError *)error {
  522. // Reset the token if it's a permanent error or the error code is ABORTED, signaling the write
  523. // stream is no longer valid.
  524. if ([FSTDatastore isPermanentWriteError:error] || [FSTDatastore isAbortedError:error]) {
  525. NSString *token = [self.writeStream.lastStreamToken base64EncodedStringWithOptions:0];
  526. FSTLog(@"FSTRemoteStore %p error before completed handshake; resetting stream token %@: %@",
  527. (__bridge void *)self, token, error);
  528. self.writeStream.lastStreamToken = nil;
  529. [self.localStore setLastStreamToken:nil];
  530. }
  531. }
  532. - (void)handleWriteError:(NSError *)error {
  533. // Only handle permanent error. If it's transient, just let the retry logic kick in.
  534. if (![FSTDatastore isPermanentWriteError:error]) {
  535. return;
  536. }
  537. // If this was a permanent error, the request itself was the problem so it's not going to
  538. // succeed if we resend it.
  539. FSTMutationBatch *batch = self.pendingWrites[0];
  540. [self.pendingWrites removeObjectAtIndex:0];
  541. // In this case it's also unlikely that the server itself is melting down--this was just a
  542. // bad request so inhibit backoff on the next restart.
  543. [self.writeStream inhibitBackoff];
  544. [self.syncEngine rejectFailedWriteWithBatchID:batch.batchID error:error];
  545. // It's possible that with the completion of this mutation another slot has freed up.
  546. [self fillWritePipeline];
  547. }
  548. - (FSTTransaction *)transaction {
  549. return [FSTTransaction transactionWithDatastore:self.datastore];
  550. }
  551. @end
  552. NS_ASSUME_NONNULL_END