firestore 0.61.1

Library provides a simple API for Google Firestore and own Serde serializer based on efficient gRPC API
Documentation
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
//! Waiting on long-running Firestore admin operations, shared by index sync and bulk delete.
//!
//! One wait serves both: it polls every still-pending operation once per round against one
//! [`OperationDeadline`], polls again after a retryable poll error or a poll that did not answer
//! in time, judges every outcome of a round before it stops at the first operation that failed,
//! and gives up once the deadline has passed. A caller that needs more than the done/error state,
//! such as bulk delete's progress metadata, observes every polled [`Operation`] through the
//! `on_poll` hook.

use crate::errors::{FirestoreError, FirestoreErrorPublicGenericDetails, FirestoreSystemError};
use crate::{FirestoreDb, FirestoreOperationWaitOptions, FirestoreResult};
use futures::StreamExt;
use gcloud_sdk::google::longrunning::operation::Result as LroResult;
use gcloud_sdk::google::longrunning::{GetOperationRequest, Operation};
use gcloud_sdk::tonic::Code;
use gcloud_sdk::RequestTimeouts;
use std::fmt::{Display, Formatter};
use std::time::Duration;
use tokio::time::error::Elapsed;
use tokio::time::Instant;
use tracing::*;

/// The longest one `GetOperation` poll may take before it is abandoned and sent again in the
/// next round, unless the wait's whole timeout is shorter. A poll is one small read that a
/// healthy service answers in well under a second, so ten seconds only ever cuts off a call that
/// is stuck, and a stuck call then costs one round instead of the rest of the wait.
const POLL_TIMEOUT: Duration = Duration::from_secs(10);

/// How many `GetOperation` polls one round keeps in flight at once. The admin channel is the
/// same HTTP/2 connection the data API uses, where Google's front end allows 100 concurrent
/// streams; 16 leaves most of those to the application's own requests, while a sync that started
/// a few hundred operations still polls all of them in a few dozen round trips.
const MAX_CONCURRENT_POLLS: usize = 16;

/// Longer than any wait a caller can mean. A timeout above it, such as `Duration::MAX`, is
/// treated as this, since a deadline that far out does not fit in an [`Instant`].
const NO_PRACTICAL_DEADLINE: Duration = Duration::from_secs(30 * 365 * 24 * 60 * 60);

/// What a started operation carries out. `Display` is the human-readable form a log line or a
/// timeout error shows, such as `create index (a DESC, tags CONTAINS)`.
pub(crate) trait OperationAction: Display {
    /// A short, stable identifier for the action's kind, independent of its target, logged as a
    /// field a caller can filter or group on without parsing the `Display` text.
    fn kind(&self) -> &'static str;
}

/// A long-running operation this crate started: its server-assigned resource name, and the
/// action it carries out.
pub(crate) struct StartedOperation<A> {
    pub name: String,
    pub action: A,
}

impl<A: Display> Display for StartedOperation<A> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{} ({})", self.action, self.name)
    }
}

/// The one deadline every wait of a call works to, and how those waits poll meanwhile.
///
/// Fixed once, when the call starts, so several waits in a row, such as an index sync's waits
/// between dependent writes and its final wait, all end by the same instant instead of each
/// getting the full timeout again. The last round of polls goes out no later than the deadline,
/// and each poll may take the poll timeout to answer, so a wait returns at most one poll timeout
/// after the deadline.
#[derive(Debug, Clone)]
pub(crate) struct OperationDeadline {
    at: Instant,
    timeout: Duration,
    poll_interval: Duration,
    poll_timeout: Duration,
}

impl OperationDeadline {
    /// A deadline `options.timeout` from now, polled every `options.poll_interval`. Each poll
    /// may take [`POLL_TIMEOUT`], or the whole timeout when that is shorter.
    pub(crate) fn from_now(options: &FirestoreOperationWaitOptions) -> Self {
        Self {
            at: Instant::now() + options.timeout.min(NO_PRACTICAL_DEADLINE),
            timeout: options.timeout,
            poll_interval: options.poll_interval,
            poll_timeout: options.timeout.min(POLL_TIMEOUT),
        }
    }

    #[cfg(test)]
    pub(crate) fn with_poll_timeout(self, poll_timeout: Duration) -> Self {
        Self {
            poll_timeout,
            ..self
        }
    }

    /// When a poll sent now is abandoned. A poll sent after the deadline gets only what is left
    /// of the poll timeout past it, so a late wait cannot extend the call further. Shared with
    /// index sync's `GetField` read-back after a refused write, the only other per-poll read this
    /// crate bounds against the same deadline.
    pub(super) fn poll_cutoff(&self) -> Instant {
        Instant::now().min(self.at) + self.poll_timeout
    }

    pub(super) fn has_passed(&self) -> bool {
        Instant::now() >= self.at
    }

    /// The error a wait that reached this deadline fails with, still `waiting_for` something.
    pub(super) fn timed_out(&self, waiting_for: &str) -> FirestoreError {
        FirestoreError::SystemError(FirestoreSystemError::new(
            FirestoreErrorPublicGenericDetails::new("OPERATION_WAIT_TIMEOUT".to_string()),
            format!(
                "timed out after {:?} waiting for: {waiting_for}",
                self.timeout
            ),
        ))
    }

    /// Sleeps one poll interval, or only until the deadline when that comes first.
    pub(super) async fn sleep_until_next_round(&self) {
        let remaining = self.at.saturating_duration_since(Instant::now());
        tokio::time::sleep(self.poll_interval.min(remaining)).await;
    }
}

/// What one poll decided about its operation.
enum PollVerdict {
    Pending,
    Done,
    Failed(FirestoreError),
}

/// One operation's progress through a wait, and the span its polls are logged in.
struct TrackedOperation<'a, A> {
    operation: &'a StartedOperation<A>,
    span: Span,
    polls: u32,
}

impl<A: OperationAction> TrackedOperation<'_, A> {
    fn finish(&self, state: &'static str, began: Instant) {
        self.span.record("/firestore/polls", self.polls);
        self.span.record("/firestore/state", state);
        self.span
            .record("/firestore/response_time", began.elapsed().as_millis());
    }

    /// Judges one poll's `result`, logging it and, once the operation is settled, recording its
    /// final state on its span.
    fn judge<F>(
        &mut self,
        result: Result<FirestoreResult<Operation>, Elapsed>,
        deadline: &OperationDeadline,
        began: Instant,
        on_poll: &mut F,
    ) -> PollVerdict
    where
        F: FnMut(&StartedOperation<A>, &Operation),
    {
        self.polls += 1;
        let span = self.span.clone();
        let _entered = span.enter();
        let operation = self.operation;
        let polled = match result {
            Err(_) => {
                warn!(
                    poll = self.polls,
                    operation = operation.name.as_str(),
                    action = operation.action.kind(),
                    poll_timeout_ms = deadline.poll_timeout.as_millis(),
                    "Polling an operation did not answer in time; polling it again.",
                );
                return PollVerdict::Pending;
            }
            Ok(Err(FirestoreError::DatabaseError(err))) if err.retry_possible => {
                warn!(
                    %err,
                    poll = self.polls,
                    operation = operation.name.as_str(),
                    action = operation.action.kind(),
                    "Polling an operation failed with a retryable error; polling it again.",
                );
                return PollVerdict::Pending;
            }
            Ok(Err(err)) => {
                error!(
                    %err,
                    operation = operation.name.as_str(),
                    action = operation.action.kind(),
                    label = %operation.action,
                    "Polling an operation failed.",
                );
                self.finish("poll_failed", began);
                return PollVerdict::Failed(err);
            }
            Ok(Ok(polled)) => polled,
        };

        on_poll(operation, &polled);
        debug!(
            poll = self.polls,
            operation = operation.name.as_str(),
            action = operation.action.kind(),
            label = %operation.action,
            done = polled.done,
            "Polled a pending operation.",
        );
        if !polled.done {
            return PollVerdict::Pending;
        }
        if let Some(LroResult::Error(status)) = polled.result {
            error!(
                operation = operation.name.as_str(),
                action = operation.action.kind(),
                label = %operation.action,
                code = status.code,
                message = status.message.as_str(),
                "Operation failed.",
            );
            self.finish("failed", began);
            // The server's message never says which operation it is about, and a sync can have
            // several failing at once.
            let status = gcloud_sdk::google::rpc::Status {
                message: format!("{operation} failed: {}", status.message),
                ..status
            };
            return PollVerdict::Failed(FirestoreError::from(status));
        }
        info!(
            operation = operation.name.as_str(),
            action = operation.action.kind(),
            label = %operation.action,
            polls = self.polls,
            elapsed_ms = began.elapsed().as_millis(),
            "Operation reached a terminal state.",
        );
        self.finish("done", began);
        PollVerdict::Done
    }
}

impl FirestoreDb {
    /// Polls one operation. `GetOperation` only reads, so a poll answered `DEADLINE_EXCEEDED`,
    /// or `NOT_FOUND` for an operation too new to be visible yet, is marked retryable here even
    /// though the crate-wide classification, which also covers writes, marks neither so.
    async fn get_operation(&self, name: &str) -> FirestoreResult<Operation> {
        self.operations_client()
            .get_operation(self.request_with_timeout(
                GetOperationRequest {
                    name: name.to_string(),
                },
                RequestTimeouts::with_total,
            ))
            .await
            .map(|response| response.into_inner())
            .map_err(|status| match status.code() {
                Code::NotFound => FirestoreError::retryable_read(status),
                _ => FirestoreError::from_read_status(status),
            })
    }

    /// Polls `operations` until every one reaches a terminal state or `deadline` passes.
    ///
    /// Each round polls every still-pending operation, at most [`MAX_CONCURRENT_POLLS`] at once,
    /// and then sleeps the poll interval, or only until the deadline when that comes first. So
    /// one slow operation never uses up the deadline before the others are checked, and the last
    /// round goes out at the deadline itself. A poll that does not answer within the poll
    /// timeout, or fails with an error marked `retry_possible`, is logged and sent again next
    /// round. Each operation gets a `Wait For Operation` span, a child of the caller's current
    /// span, that records its poll count, final state (`done`, `failed`, `poll_failed`,
    /// `pending` at the deadline, or `abandoned` when another operation's failure ended the
    /// wait) and elapsed time.
    ///
    /// `on_poll` sees every successfully polled [`Operation`], done or not, before it is judged,
    /// and every poll of a round is judged before the wait returns.
    ///
    /// # Errors
    /// - The first operation, in the order given, that finished with an error in a round, as that
    ///   error, naming the operation. The others keep running on the server.
    /// - A poll that fails with an error not marked `retry_possible`.
    /// - Once the deadline passes, an `OPERATION_WAIT_TIMEOUT` system error naming the action and
    ///   operation name of each operation still pending at its last poll, and only those.
    pub(crate) async fn wait_for_operations<A, F>(
        &self,
        operations: &[&StartedOperation<A>],
        deadline: &OperationDeadline,
        mut on_poll: F,
    ) -> FirestoreResult<()>
    where
        A: OperationAction,
        F: FnMut(&StartedOperation<A>, &Operation),
    {
        let began = Instant::now();
        let mut pending: Vec<TrackedOperation<A>> = operations
            .iter()
            .map(|operation| TrackedOperation {
                operation,
                span: span!(
                    Level::INFO,
                    "Wait For Operation",
                    "/firestore/operation" = operation.name.as_str(),
                    "/firestore/action" = operation.action.kind(),
                    "/firestore/polls" = field::Empty,
                    "/firestore/state" = field::Empty,
                    "/firestore/response_time" = field::Empty,
                ),
                polls: 0,
            })
            .collect();

        loop {
            // Collected before streaming: a stream over a lazy `map` makes the future
            // higher-ranked over the borrow, which the `Send` bound of an `async_trait` caller
            // cannot prove.
            let polls: Vec<_> = pending
                .iter()
                .map(|tracked| {
                    // The cutoff is taken when the poll is actually sent, which can be later
                    // than the start of the round when more operations are pending than may be
                    // polled at once.
                    async move {
                        tokio::time::timeout_at(
                            deadline.poll_cutoff(),
                            self.get_operation(&tracked.operation.name),
                        )
                        .await
                    }
                    .instrument(tracked.span.clone())
                })
                .collect();
            let polled: Vec<_> = futures::stream::iter(polls)
                .buffered(MAX_CONCURRENT_POLLS)
                .collect()
                .await;

            let mut first_failure = None;
            let mut still_pending = Vec::with_capacity(pending.len());
            for (mut tracked, result) in pending.into_iter().zip(polled) {
                match tracked.judge(result, deadline, began, &mut on_poll) {
                    PollVerdict::Pending => still_pending.push(tracked),
                    PollVerdict::Done => {}
                    PollVerdict::Failed(err) => {
                        first_failure.get_or_insert(err);
                    }
                }
            }
            pending = still_pending;

            if let Some(err) = first_failure {
                for tracked in &pending {
                    tracked.finish("abandoned", began);
                }
                return Err(err);
            }
            if pending.is_empty() {
                return Ok(());
            }
            if deadline.has_passed() {
                let names: Vec<String> = pending
                    .iter()
                    .map(|tracked| tracked.operation.to_string())
                    .collect();
                for tracked in &pending {
                    tracked.finish("pending", began);
                }
                warn!(
                    pending = names.join(", "),
                    timeout_ms = deadline.timeout.as_millis(),
                    "Timed out waiting for operations to finish.",
                );
                return Err(deadline.timed_out(&names.join(", ")));
            }
            deadline.sleep_until_next_round().await;
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::db::fake_firestore::{
        done_operation_response, failed_operation_response, pending_operation_response,
        FakeFirestore, FakeResponse,
    };
    use gcloud_sdk::prost::Message as _;
    use std::collections::HashMap;
    use std::sync::Mutex;
    use std::time::Duration;

    const GET_OPERATION: &str = "/google.longrunning.Operations/GetOperation";

    struct TestAction;

    impl Display for TestAction {
        fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
            write!(f, "test action")
        }
    }

    impl OperationAction for TestAction {
        fn kind(&self) -> &'static str {
            "test"
        }
    }

    fn started(id: &str) -> StartedOperation<TestAction> {
        StartedOperation {
            name: format!("projects/fake-firestore/databases/(default)/operations/{id}"),
            action: TestAction,
        }
    }

    /// Starts a fake whose `GetOperation` answers through `answer`, given the polled operation's
    /// ID and how many times that operation has been polled, this poll included.
    async fn fake_polls(
        answer: impl Fn(&str, &str, u32) -> FakeResponse + Send + Sync + 'static,
    ) -> FakeFirestore {
        let polls: Mutex<HashMap<String, u32>> = Mutex::default();
        FakeFirestore::start(move |method, bytes| {
            assert_eq!(method, GET_OPERATION, "unexpected RPC");
            let name = GetOperationRequest::decode(bytes).unwrap().name;
            let id = name.rsplit('/').next().unwrap().to_string();
            let count = {
                let mut polls = polls.lock().unwrap();
                let count = polls.entry(id.clone()).or_default();
                *count += 1;
                *count
            };
            (
                format!("GetOperation({id})#{count}"),
                answer(&name, &id, count),
            )
        })
        .await
    }

    #[tokio::test]
    async fn a_hung_poll_is_abandoned_while_the_other_operations_are_polled() {
        let fake = fake_polls(|name, id, count| match (id, count) {
            ("hung", 1) => FakeResponse::Hang,
            (_, 1) => pending_operation_response(name),
            _ => done_operation_response(name),
        })
        .await;
        let (hung, other) = (started("hung"), started("other"));
        let mut observed_done = Vec::new();

        let began = std::time::Instant::now();
        let result = tokio::time::timeout(
            Duration::from_secs(5),
            fake.db.wait_for_operations(
                &[&hung, &other],
                &OperationDeadline::from_now(
                    &FirestoreOperationWaitOptions::new(Duration::from_secs(3))
                        .with_poll_interval(Duration::from_millis(10)),
                )
                .with_poll_timeout(Duration::from_millis(100)),
                |operation, polled| {
                    if polled.done {
                        observed_done.push(operation.name.clone());
                    }
                },
            ),
        )
        .await
        .expect("a hung poll must not hold the wait");

        result.unwrap();
        assert!(
            began.elapsed() < Duration::from_secs(1),
            "a hung poll held the wait for {:?}",
            began.elapsed()
        );
        assert!(observed_done.contains(&other.name), "{observed_done:?}");
        assert!(observed_done.contains(&hung.name), "{observed_done:?}");
    }

    #[tokio::test]
    async fn every_outcome_of_a_round_is_observed_before_the_first_failure_returns() {
        let fake = fake_polls(|name, id, _| match id {
            "failing" => failed_operation_response(name, 9, "build failed"),
            _ => done_operation_response(name),
        })
        .await;
        let (failing, finishing) = (started("failing"), started("finishing"));
        let mut observed = Vec::new();

        let err = fake
            .db
            .wait_for_operations(
                &[&failing, &finishing],
                &OperationDeadline::from_now(
                    &FirestoreOperationWaitOptions::new(Duration::from_secs(5))
                        .with_poll_interval(Duration::from_millis(10)),
                ),
                |operation, polled| observed.push((operation.name.clone(), polled.done)),
            )
            .await
            .unwrap_err();

        assert!(err.to_string().contains("build failed"), "{err}");
        assert_eq!(
            observed,
            vec![(failing.name.clone(), true), (finishing.name.clone(), true)]
        );
    }
}