FSTRemoteStore.mm 25 KB

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