denokv_remote 0.8.4

Remote (KV Connect) backend for Deno KV
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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
// Copyright 2023 the Deno authors. All rights reserved. MIT license.

mod time;

use std::io;
use std::ops::Sub;
use std::pin::pin;
use std::pin::Pin;
use std::sync::Arc;
use std::time::Duration;

use anyhow::Context;
use async_stream::try_stream;
use async_trait::async_trait;
use bytes::Bytes;
use chrono::DateTime;
use chrono::Utc;
use denokv_proto::decode_value;
use denokv_proto::AtomicWrite;
use denokv_proto::CommitResult;
use denokv_proto::Consistency;
use denokv_proto::Database;
use denokv_proto::DatabaseMetadata;
use denokv_proto::KvEntry;
use denokv_proto::KvValue;
use denokv_proto::MetadataExchangeRequest;
use denokv_proto::QueueMessageHandle;
use denokv_proto::ReadRange;
use denokv_proto::ReadRangeOutput;
use denokv_proto::SnapshotReadOptions;
use denokv_proto::WatchKeyOutput;
use futures::Future;
use futures::Stream;
use futures::StreamExt;
use futures::TryStreamExt;
use http::HeaderMap;
use http::HeaderValue;
use http::StatusCode;
use log::debug;
use log::error;
use log::warn;
use prost::Message;
use rand::Rng;
use serde::Deserialize;
use time::utc_now;
use tokio::sync::watch;
use tokio::task::JoinHandle;
use tokio_util::codec::LengthDelimitedCodec;
use tokio_util::io::StreamReader;
use url::Url;
use uuid::Uuid;

use denokv_proto::datapath as pb;

const DATAPATH_BACKOFF_BASE: Duration = Duration::from_millis(200);
const METADATA_BACKOFF_BASE: Duration = Duration::from_secs(5);

pub struct MetadataEndpoint {
  pub url: Url,
  pub access_token: String,
}

impl MetadataEndpoint {
  pub fn headers(&self) -> HeaderMap {
    let mut headers = HeaderMap::with_capacity(2);
    headers.insert(
      "authorization",
      format!("Bearer {}", self.access_token).try_into().unwrap(),
    );
    headers.insert("content-type", "application/json".try_into().unwrap());
    headers
  }
}

#[derive(Clone, Copy, PartialEq, Eq)]
enum ProtocolVersion {
  V1,
  V2,
  V3,
}

#[derive(PartialEq, Eq)]
enum DataPathConsistency {
  Strong,
  Eventual,
}

struct DataPathEndpoint {
  url: Url,
  consistency: DataPathConsistency,
}

struct Metadata {
  version: ProtocolVersion,
  database_id: Uuid,
  endpoints: Vec<DataPathEndpoint>,
  token: String,
  expires_at: DateTime<Utc>,
}

impl Metadata {
  pub fn headers(&self) -> HeaderMap {
    let mut headers = HeaderMap::with_capacity(3);
    headers.insert(
      "authorization",
      format!("Bearer {}", self.token).try_into().unwrap(),
    );
    match self.version {
      ProtocolVersion::V1 => {
        headers.insert(
          "x-transaction-domain-id",
          self.database_id.to_string().try_into().unwrap(),
        );
      }
      ProtocolVersion::V2 => {
        headers.insert(
          "x-denokv-database-id",
          self.database_id.to_string().try_into().unwrap(),
        );
        headers.insert("x-denokv-version", HeaderValue::from_static("2"));
      }
      ProtocolVersion::V3 => {
        headers.insert(
          "x-denokv-database-id",
          self.database_id.to_string().try_into().unwrap(),
        );
        headers.insert("x-denokv-version", HeaderValue::from_static("3"));
      }
    };
    headers
  }
}

#[derive(Clone)]
enum MetadataState {
  Pending,
  Ok(Arc<Metadata>),
  Error(Arc<String>),
}

pub trait RemotePermissions: Clone + 'static {
  fn check_net_url(&self, url: &Url) -> Result<(), anyhow::Error>;
}

/// Implements a transport that can POST bytes to a remote service.
pub trait RemoteTransport: Clone + Send + Sync + 'static {
  type Response: RemoteResponse;

  /// Perform an HTTP POST with the given body and headers, returning the final URL,
  /// status code and response object.
  fn post(
    &self,
    url: Url,
    headers: http::HeaderMap,
    body: Bytes,
  ) -> impl Future<
    Output = Result<(Url, http::StatusCode, Self::Response), anyhow::Error>,
  > + Send
       + Sync;
}

/// A response object.
pub trait RemoteResponse: Send + Sync {
  /// The bytes associated with this response.
  fn bytes(
    self,
  ) -> impl Future<Output = Result<Bytes, anyhow::Error>> + Send + Sync;
  /// The text associated with this response.
  fn text(
    self,
  ) -> impl Future<Output = Result<String, anyhow::Error>> + Send + Sync;
  /// The stream of bytes associated with this response.
  fn stream(
    self,
  ) -> impl Stream<Item = Result<Bytes, anyhow::Error>> + Send + Sync;
}

enum RetryableResult<T, E> {
  Ok(T),
  Retry,
  Err(E),
}

#[derive(Clone)]
pub struct Remote<P: RemotePermissions, T: RemoteTransport> {
  permissions: P,
  client: T,
  metadata_refresher: Arc<JoinHandle<()>>,
  metadata: watch::Receiver<MetadataState>,
}

impl<P: RemotePermissions, T: RemoteTransport> Remote<P, T> {
  pub fn new(
    client: T,
    permissions: P,
    metadata_endpoint: MetadataEndpoint,
  ) -> Self {
    let (tx, rx) = watch::channel(MetadataState::Pending);
    let metadata_refresher = tokio::spawn(metadata_refresh_task(
      client.clone(),
      metadata_endpoint,
      tx,
    ));
    Self {
      client,
      permissions,
      metadata_refresher: Arc::new(metadata_refresher),
      metadata: rx,
    }
  }

  async fn call_raw<Req: prost::Message>(
    &self,
    method: &'static str,
    req: Req,
  ) -> Result<(T::Response, ProtocolVersion), anyhow::Error> {
    let attempt = 0;
    let req_body = Bytes::from(req.encode_to_vec());
    loop {
      let metadata = loop {
        let mut metadata_rx = self.metadata.clone();
        match &*metadata_rx.borrow() {
          MetadataState::Pending => {}
          MetadataState::Ok(metadata) => break metadata.clone(),
          MetadataState::Error(e) => {
            return Err(anyhow::anyhow!("{}", e));
          }
        };
        if metadata_rx.changed().await.is_err() {
          return Err(anyhow::anyhow!("Database is closed."));
        }
      };

      let endpoint = match metadata
        .endpoints
        .iter()
        .find(|endpoint| endpoint.consistency == DataPathConsistency::Strong)
      {
        Some(endpoint) => endpoint,
        None => {
          return Err(anyhow::anyhow!(
            "No strong consistency endpoints available."
          ));
        }
      };

      let url = Url::parse(&format!("{}/{}", endpoint.url, method))?;
      self.permissions.check_net_url(&url)?;

      let req = self
        .client
        .post(url.clone(), metadata.headers(), req_body.clone())
        .await;

      let resp = match req {
        Ok(resp) if resp.1 == StatusCode::OK => resp,
        Ok(resp) if resp.1.is_server_error() => {
          let status = resp.1;
          let b = resp.2.bytes().await.unwrap_or_default();
          let body = String::from_utf8_lossy(&b);
          error!(
            "KV Connect failed to call '{}' (status={}): {}",
            url, status, body
          );
          randomized_exponential_backoff(DATAPATH_BACKOFF_BASE, attempt).await;
          continue;
        }
        Ok(resp) => {
          let status = resp.1;
          let b = resp.2.bytes().await.unwrap_or_default();
          let body = String::from_utf8_lossy(&b);
          return Err(anyhow::anyhow!(
            "KV Connect failed to call '{}' (status={}): {}",
            url,
            status,
            body
          ));
        }
        Err(err) => {
          error!("KV Connect failed to call '{}': {}", url, err);
          randomized_exponential_backoff(DATAPATH_BACKOFF_BASE, attempt).await;
          continue;
        }
      };

      return Ok((resp.2, metadata.version));
    }
  }

  async fn call_stream<Req: prost::Message>(
    &self,
    method: &'static str,
    req: Req,
  ) -> Result<
    (
      impl Stream<Item = Result<Bytes, io::Error>>,
      ProtocolVersion,
    ),
    anyhow::Error,
  > {
    let (resp, version) = self.call_raw(method, req).await?;
    let stream = resp
      .stream()
      .map_err(|e| io::Error::new(io::ErrorKind::Other, e));
    Ok((stream, version))
  }

  async fn call_data<Req: prost::Message, Resp: prost::Message + Default>(
    &self,
    method: &'static str,
    req: Req,
  ) -> Result<(Resp, ProtocolVersion), anyhow::Error> {
    let (resp, version) = self.call_raw(method, req).await?;

    let resp_body = match resp.bytes().await {
      Ok(resp_body) => resp_body,
      Err(err) => {
        return Err(anyhow::anyhow!(
          "KV Connect failed to read response body: {}",
          err
        ));
      }
    };

    let resp = Resp::decode(resp_body)
      .context("KV Connect failed to decode response")?;

    Ok((resp, version))
  }
}

impl<P: RemotePermissions, T: RemoteTransport> Drop for Remote<P, T> {
  fn drop(&mut self) {
    self.metadata_refresher.abort();
  }
}

async fn randomized_exponential_backoff(base: Duration, attempt: u64) {
  let attempt = attempt.min(12);
  let delay = base.as_millis() as u64 + (2 << attempt);
  let delay = delay + rand::thread_rng().gen_range(0..(delay / 2) + 1);
  tokio::time::sleep(std::time::Duration::from_millis(delay)).await;
}

async fn metadata_refresh_task<T: RemoteTransport>(
  client: T,
  metadata_endpoint: MetadataEndpoint,
  tx: watch::Sender<MetadataState>,
) {
  let mut attempts = 0;
  loop {
    let res = fetch_metadata(&client, &metadata_endpoint).await;
    match res {
      RetryableResult::Ok(metadata) => {
        attempts = 0;
        let expires_in = metadata.expires_at.signed_duration_since(utc_now());

        if tx.send(MetadataState::Ok(Arc::new(metadata))).is_err() {
          // The receiver has been dropped, so we can stop now.
          return;
        }

        // Sleep until the token expires, minus a 10 minute buffer, but at
        // minimum one minute.
        let sleep_time = expires_in
          .sub(chrono::Duration::seconds(10))
          .to_std()
          .unwrap_or_default()
          .min(Duration::from_secs(60));

        tokio::time::sleep(sleep_time).await;
      }
      RetryableResult::Retry => {
        attempts += 1;
        if tx.is_closed() {
          // The receiver has been dropped, so we can stop now.
          return;
        }
        randomized_exponential_backoff(METADATA_BACKOFF_BASE, attempts).await;
      }
      RetryableResult::Err(err) => {
        attempts += 1;
        if tx.send(MetadataState::Error(Arc::new(err))).is_err() {
          // The receiver has been dropped, so we can stop now.
          return;
        }
        randomized_exponential_backoff(METADATA_BACKOFF_BASE, attempts).await;
      }
    }
  }
}

async fn fetch_metadata<T: RemoteTransport>(
  client: &T,
  metadata_endpoint: &MetadataEndpoint,
) -> RetryableResult<Metadata, String> {
  let body = serde_json::to_vec(&MetadataExchangeRequest {
    supported_versions: vec![1, 2, 3],
  })
  .unwrap();
  let res = match client
    .post(
      metadata_endpoint.url.clone(),
      metadata_endpoint.headers(),
      body.into(),
    )
    .await
  {
    Ok(res) => res,
    Err(err) => {
      error!(
        "KV Connect to '{}' failed to fetch metadata: {}",
        metadata_endpoint.url, err
      );
      return RetryableResult::Retry;
    }
  };

  let res = match res.1 {
    StatusCode::OK => res,
    status if status.is_client_error() => {
      let body = res.2.text().await.unwrap_or_else(|_| String::new());
      return RetryableResult::Err(format!(
        "Failed to fetch metadata: {}",
        body
      ));
    }
    status if status.is_server_error() => {
      let body = res.2.text().await.unwrap_or_else(|_| String::new());
      error!(
        "KV Connect to '{}' failed to fetch metadata (status={}): {}",
        metadata_endpoint.url, status, body
      );
      return RetryableResult::Retry;
    }
    status => {
      return RetryableResult::Err(format!(
        "Failed to fetch metadata (status={})",
        status
      ));
    }
  };

  let base_url = res.0;

  let body = match res.2.text().await {
    Ok(body) => body,
    Err(err) => {
      return RetryableResult::Err(format!(
        "Metadata response body invalid: {}",
        err
      ));
    }
  };

  let metadata = match parse_metadata(&base_url, &body) {
    Ok(metadata) => metadata,
    Err(err) => {
      return RetryableResult::Err(format!(
        "Failed to parse metadata: {}",
        err
      ));
    }
  };

  RetryableResult::Ok(metadata)
}

fn parse_metadata(base_url: &Url, body: &str) -> Result<Metadata, String> {
  #[derive(Deserialize)]
  struct Version {
    version: u64,
  }

  let version: Version = match serde_json::from_str(body) {
    Ok(metadata) => metadata,
    Err(err) => {
      return Err(format!("could not get 'version' field: {}", err));
    }
  };

  let version = match version.version {
    1 => ProtocolVersion::V1,
    2 => ProtocolVersion::V2,
    3 => ProtocolVersion::V3,
    version => {
      return Err(format!("unsupported metadata version: {}", version));
    }
  };

  // V1, V2, and V3 have the same shape
  let metadata: DatabaseMetadata = match serde_json::from_str(body) {
    Ok(metadata) => metadata,
    Err(err) => {
      return Err(format!("{}", err));
    }
  };

  let mut endpoints = Vec::new();
  for endpoint in metadata.endpoints {
    let url = match version {
      ProtocolVersion::V1 => Url::parse(&endpoint.url),
      ProtocolVersion::V2 | ProtocolVersion::V3 => {
        Url::options().base_url(Some(base_url)).parse(&endpoint.url)
      }
    }
    .map_err(|err| format!("invalid endpoint URL: {}", err))?;

    if endpoint.url.ends_with('/') {
      return Err(format!("endpoint URL must not end with '/': {}", url));
    }

    let consistency = match &*endpoint.consistency {
      "strong" => DataPathConsistency::Strong,
      "eventual" => DataPathConsistency::Eventual,
      consistency => {
        return Err(format!("unsupported consistency level: {}", consistency));
      }
    };

    endpoints.push(DataPathEndpoint { url, consistency });
  }

  Ok(Metadata {
    version,
    endpoints,
    database_id: metadata.database_id,
    token: metadata.token.into_owned(),
    expires_at: metadata.expires_at,
  })
}

#[async_trait(?Send)]
impl<P: RemotePermissions, T: RemoteTransport> Database for Remote<P, T> {
  type QMH = DummyQueueMessageHandle;

  async fn snapshot_read(
    &self,
    requests: Vec<ReadRange>,
    options: SnapshotReadOptions,
  ) -> Result<Vec<ReadRangeOutput>, anyhow::Error> {
    let ranges = requests
      .into_iter()
      .map(|r| pb::ReadRange {
        start: r.start,
        end: r.end,
        limit: r.limit.get() as _,
        reverse: r.reverse,
      })
      .collect();
    let req = pb::SnapshotRead { ranges };

    let (res, version): (pb::SnapshotReadOutput, _) =
      self.call_data("snapshot_read", req).await?;

    match version {
      ProtocolVersion::V1 | ProtocolVersion::V2 => {
        if res.read_disabled {
          // TODO: this should result in a retry after a forced metadata refresh.
          return Err(anyhow::anyhow!("Reads are disabled for this database."));
        }
      }
      ProtocolVersion::V3 => match res.status() {
        pb::SnapshotReadStatus::SrSuccess => {}
        pb::SnapshotReadStatus::SrReadDisabled => {
          // TODO: this should result in a retry after a forced metadata refresh.
          return Err(anyhow::anyhow!("Reads are disabled for this database."));
        }
        pb::SnapshotReadStatus::SrUnspecified => {
          Err(anyhow::anyhow!(
            "Unspecified read error (code={}).",
            res.status
          ))?;
          unreachable!();
        }
      },
    }

    if !res.read_is_strongly_consistent
      && options.consistency == Consistency::Strong
    {
      // TODO: this should result in a retry after a forced metadata refresh.
      return Err(anyhow::anyhow!(
        "Strong consistency reads are not available for this database."
      ));
    }

    let ranges = res
      .ranges
      .into_iter()
      .map(|r| {
        Ok(ReadRangeOutput {
          entries: r
            .values
            .into_iter()
            .map(|e| {
              Ok(KvEntry {
                key: e.key,
                value: decode_value(e.value, e.encoding as i64).ok_or_else(
                  || anyhow::anyhow!("Unknown encoding {}", e.encoding),
                )?,
                versionstamp: <[u8; 10]>::try_from(&e.versionstamp[..])?,
              })
            })
            .collect::<Result<_, anyhow::Error>>()?,
        })
      })
      .collect::<Result<_, anyhow::Error>>()?;

    Ok(ranges)
  }

  async fn atomic_write(
    &self,
    write: AtomicWrite,
  ) -> Result<Option<CommitResult>, anyhow::Error> {
    if !write.enqueues.is_empty() {
      return Err(anyhow::anyhow!(
        "Enqueue operations are not supported in KV Connect.",
      ));
    }

    let mut checks = Vec::new();
    for check in write.checks {
      checks.push(pb::Check {
        key: check.key,
        versionstamp: check
          .versionstamp
          .map(|v| v.to_vec())
          .unwrap_or_default(),
      });
    }

    let mut mutations = Vec::new();
    for mutation in write.mutations {
      let expire_at_ms = mutation
        .expire_at
        .map(|t| {
          let ts = t.timestamp_millis();
          if ts <= 0 {
            1
          } else {
            ts
          }
        })
        .unwrap_or(0);
      match mutation.kind {
        denokv_proto::MutationKind::Set(value) => {
          mutations.push(pb::Mutation {
            key: mutation.key,
            value: Some(encode_value_to_pb(value)),
            mutation_type: pb::MutationType::MSet as _,
            expire_at_ms,
            ..Default::default()
          });
        }
        denokv_proto::MutationKind::Delete => {
          mutations.push(pb::Mutation {
            key: mutation.key,
            value: Some(encode_value_to_pb(KvValue::Bytes(vec![]))),
            mutation_type: pb::MutationType::MDelete as _,
            expire_at_ms,
            ..Default::default()
          });
        }
        denokv_proto::MutationKind::Sum {
          value,
          min_v8,
          max_v8,
          clamp,
        } => {
          mutations.push(pb::Mutation {
            key: mutation.key,
            value: Some(encode_value_to_pb(value)),
            mutation_type: pb::MutationType::MSum as _,
            expire_at_ms,
            sum_min: min_v8,
            sum_max: max_v8,
            sum_clamp: clamp,
          });
        }
        denokv_proto::MutationKind::Max(value) => {
          mutations.push(pb::Mutation {
            key: mutation.key,
            value: Some(encode_value_to_pb(value)),
            mutation_type: pb::MutationType::MMax as _,
            expire_at_ms,
            ..Default::default()
          });
        }
        denokv_proto::MutationKind::Min(value) => {
          mutations.push(pb::Mutation {
            key: mutation.key,
            value: Some(encode_value_to_pb(value)),
            mutation_type: pb::MutationType::MMin as _,
            expire_at_ms,
            ..Default::default()
          });
        }
        denokv_proto::MutationKind::SetSuffixVersionstampedKey(value) => {
          mutations.push(pb::Mutation {
            key: mutation.key,
            value: Some(encode_value_to_pb(value)),
            mutation_type: pb::MutationType::MSetSuffixVersionstampedKey as _,
            expire_at_ms,
            ..Default::default()
          });
        }
      }
    }

    assert!(write.enqueues.is_empty());

    let req = pb::AtomicWrite {
      checks,
      mutations,
      enqueues: Vec::new(),
    };

    let (res, _): (pb::AtomicWriteOutput, _) =
      self.call_data("atomic_write", req).await?;

    match res.status() {
      pb::AtomicWriteStatus::AwSuccess => Ok(Some(CommitResult {
        versionstamp: <[u8; 10]>::try_from(&res.versionstamp[..])?,
      })),
      pb::AtomicWriteStatus::AwCheckFailure => Ok(None),
      pb::AtomicWriteStatus::AwWriteDisabled => {
        Err(anyhow::anyhow!("Writes are disabled for this database."))
      }
      pb::AtomicWriteStatus::AwUnspecified => {
        Err(anyhow::anyhow!("Unspecified write error."))
      }
    }
  }

  async fn dequeue_next_message(
    &self,
  ) -> Result<Option<Self::QMH>, anyhow::Error> {
    warn!("KV Connect does not support queues.");
    std::future::pending().await
  }

  fn watch(
    &self,
    keys: Vec<Vec<u8>>,
  ) -> Pin<Box<dyn Stream<Item = Result<Vec<WatchKeyOutput>, anyhow::Error>>>>
  {
    let this = self.clone();
    let stream = try_stream! {
      let mut attempt = 0;
       loop {
        attempt += 1;
        let req = pb::Watch {
          keys: keys.iter().map(|key| pb::WatchKey { key: key.clone() }).collect(),
        };

        let (stream, _) = this.call_stream("watch", req).await?;
        let stream = pin!(stream);
        let reader = StreamReader::new(stream);
        let codec = LengthDelimitedCodec::builder()
          .little_endian()
          .length_field_length(4)
          .max_frame_length(16 * 1048576)
          .new_codec();

        let mut frames = tokio_util::codec::FramedRead::new(reader, codec);
        'decode: loop {
          let res = frames.next().await;
          let frame = match res {
            Some(Ok(frame)) if frame.is_empty() => continue, // ping, ignore
            Some(Ok(frame)) => frame,
            Some(Err(err)) => {
              debug!("KV Connect watch disconnected (attempt={}): {}", attempt, err);
              break 'decode;
            }
            None => {
              break 'decode;
            }
          };

          let data = pb::WatchOutput::decode(frame).context("Failed to decode watch output")?;
          match data.status() {
            pb::SnapshotReadStatus::SrSuccess => {}
            pb::SnapshotReadStatus::SrReadDisabled => {
              // TODO: this should result in a retry after a forced metadata refresh.
              Err(anyhow::anyhow!("Reads are disabled for this database."))?;
              unreachable!();
            }
            pb::SnapshotReadStatus::SrUnspecified => {
              Err(anyhow::anyhow!("Unspecified read error (code={}).", data.status))?;
              unreachable!();
            }
          }

          let mut outputs = Vec::new();
          for key in data.keys {
            if !key.changed {
              outputs.push(WatchKeyOutput::Unchanged);
            } else {
              let entry = match key.entry_if_changed {
                Some(entry) => {
                  let value = decode_value(entry.value, entry.encoding as i64)
                    .ok_or_else(|| anyhow::anyhow!("Unknown encoding {}", entry.encoding))?;
                  Some(KvEntry {
                    key: entry.key,
                    value,
                    versionstamp: <[u8; 10]>::try_from(&entry.versionstamp[..])?,
                  })
                },
                None => None,
              };
              outputs.push(WatchKeyOutput::Changed { entry });
            }
          }
          yield outputs;
        }

        // The stream disconnected, so retry after a short delay.
        randomized_exponential_backoff(DATAPATH_BACKOFF_BASE, attempt).await;
      }
    };
    Box::pin(stream)
  }

  fn close(&self) {}
}

pub struct DummyQueueMessageHandle {}

#[async_trait(?Send)]
impl QueueMessageHandle for DummyQueueMessageHandle {
  async fn take_payload(&mut self) -> Result<Vec<u8>, anyhow::Error> {
    unimplemented!()
  }

  async fn finish(&self, _success: bool) -> Result<(), anyhow::Error> {
    unimplemented!()
  }
}

fn encode_value_to_pb(value: KvValue) -> pb::KvValue {
  match value {
    KvValue::V8(data) => pb::KvValue {
      encoding: pb::ValueEncoding::VeV8 as _,
      data,
    },
    KvValue::Bytes(data) => pb::KvValue {
      encoding: pb::ValueEncoding::VeBytes as _,
      data,
    },
    KvValue::U64(x) => pb::KvValue {
      data: x.to_le_bytes().to_vec(),
      encoding: pb::ValueEncoding::VeLe64 as _,
    },
  }
}