actordb 0.11.0

An actor-first distributed database
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
//! Framework-neutral application service for any unary RPC adapter.
//! Authentication and authorization are intentionally absent. Peer networking
//! and actor execution are independent of this service.

use crate::actor::{OpPriority, VersionedValue};
use crate::node::{Node, NodeError, Table};
pub use actordb_protocol::rpc::{RpcError, RpcFuture, RpcReply, RpcRequest, RpcService};
use actordb_protocol::{client as wire, MAX_RPC_MESSAGE_BYTES};
use prost::Message;
use std::collections::HashSet;
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::{Notify, Semaphore};
mod batch;
pub(crate) use batch::BatchReply;

#[derive(Debug, Clone, bon::Builder)]
#[non_exhaustive]
pub struct RpcServiceOptions {
    #[builder(default = 64)]
    pub max_in_flight_requests: usize,
    #[builder(default = 64 * 1024 * 1024)]
    pub max_request_bytes: usize,
    #[builder(default = Duration::from_secs(30))]
    pub timeout: Duration,
}
impl Default for RpcServiceOptions {
    fn default() -> Self {
        Self::builder().build()
    }
}
struct Inner {
    node: Arc<Node>,
    options: RpcServiceOptions,
    requests: Arc<Semaphore>,
    bytes: Arc<Semaphore>,
    idle: Arc<Notify>,
}
#[derive(Clone)]
pub struct NodeRpcService {
    inner: Arc<Inner>,
}
struct RequestLease {
    count: Option<tokio::sync::OwnedSemaphorePermit>,
    bytes: Option<tokio::sync::OwnedSemaphorePermit>,
    idle: Arc<Notify>,
}
impl Drop for RequestLease {
    fn drop(&mut self) {
        self.bytes.take();
        self.count.take();
        self.idle.notify_waiters();
    }
}
impl NodeRpcService {
    pub fn new(node: Arc<Node>, options: RpcServiceOptions) -> Result<Self, NodeError> {
        if options.max_in_flight_requests == 0
            || options.max_in_flight_requests > Semaphore::MAX_PERMITS
            || options.max_request_bytes < MAX_RPC_MESSAGE_BYTES
            || options.max_request_bytes > u32::MAX as usize
            || options.max_request_bytes > Semaphore::MAX_PERMITS
            || options.timeout.is_zero()
            || tokio::time::Instant::now()
                .checked_add(options.timeout)
                .is_none()
        {
            return Err(NodeError::Codec("invalid RPC service limits".into()));
        }
        Ok(Self {
            inner: Arc::new(Inner {
                requests: Arc::new(Semaphore::new(options.max_in_flight_requests)),
                bytes: Arc::new(Semaphore::new(options.max_request_bytes)),
                idle: Arc::new(Notify::new()),
                node,
                options,
            }),
        })
    }
    /// Stop service admission and join request/response ownership, not the Node.
    /// A timed-out caller may retry shutdown; admitted mutation jobs stay owned.
    pub async fn shutdown(&self, budget: Duration) -> Result<(), NodeError> {
        self.inner.requests.close();
        tokio::time::timeout(budget, async {
            loop {
                let idle = self.inner.idle.notified();
                if self.inner.requests.available_permits()
                    == self.inner.options.max_in_flight_requests
                {
                    break;
                }
                idle.await;
            }
        })
        .await
        .map_err(|_| NodeError::Actor("RPC shutdown still owns requests".into()))
    }
}
fn rejected(code: wire::ErrorCode, message: &str) -> RpcReply {
    RpcReply::new(wire::Response {
        error: Some(error(code, message)),
        ..Default::default()
    })
}
impl RpcService for NodeRpcService {
    fn call(&self, envelope: RpcRequest) -> RpcFuture<'_, Result<RpcReply, RpcError>> {
        let inner = self.inner.clone();
        Box::pin(async move {
            // Frameworks must bound materialization before constructing this
            // envelope. The core adds count/encoded-byte admission and semantic
            // validation, without interpreting transport/authentication state.
            let (request, receive_ownership) = envelope.into_parts();
            if request.table.is_empty() || request.table.len() > 255 || request.operation.is_none()
            {
                return Ok(rejected(
                    wire::ErrorCode::Invalid,
                    "table and operation required",
                ));
            }
            let size = request.encoded_len();
            if size > MAX_RPC_MESSAGE_BYTES {
                return Ok(rejected(
                    wire::ErrorCode::ResourceLimit,
                    "RPC request exceeds byte limit",
                ));
            }
            let count = match inner.requests.clone().try_acquire_owned() {
                Ok(count) => count,
                Err(_) => {
                    return Ok(rejected(
                        wire::ErrorCode::Unavailable,
                        "RPC service admission closed or full",
                    ))
                }
            };
            let mut lease = RequestLease {
                count: Some(count),
                bytes: None,
                idle: inner.idle.clone(),
            };
            let bytes = match inner.bytes.clone().try_acquire_many_owned(size as u32) {
                Ok(bytes) => bytes,
                Err(_) => {
                    return Ok(rejected(
                        wire::ErrorCode::Backpressure,
                        "RPC request byte admission full",
                    ))
                }
            };
            lease.bytes = Some(bytes);
            let operation = match inner.node.admit_client_request_for_table(&request.table) {
                Ok(operation) => operation,
                Err(_) => {
                    return Ok(rejected(
                        wire::ErrorCode::Unavailable,
                        "node is shutting down",
                    ))
                }
            };
            let mutation = matches!(
                request.operation,
                Some(
                    wire::request::Operation::Put(_)
                        | wire::request::Operation::Delete(_)
                        | wire::request::Operation::BatchPut(_)
                )
            );
            let (mut send, receive) = tokio::sync::oneshot::channel();
            // This detached *owned* task holds all admission leases and the
            // Node operation permit. Dropping an HTTP/Thrift waiter cannot
            // interrupt an accepted mutation's apply and fan-out.
            tokio::spawn(async move {
                let _receive_ownership = receive_ownership;
                let mut reply_ownership = Vec::new();
                let mut response = if mutation {
                    dispatch(&inner.node, request, &mut reply_ownership).await
                } else {
                    let result = tokio::select! {
                        biased;
                        _ = send.closed() => return,
                        result = tokio::time::timeout(inner.options.timeout, dispatch(&inner.node, request, &mut reply_ownership)) => result,
                    };
                    match result {
                        Ok(response) => response,
                        Err(_) => wire::Response {
                            error: Some(error(
                                wire::ErrorCode::Unavailable,
                                "RPC read deadline elapsed",
                            )),
                            ..Default::default()
                        },
                    }
                };
                if response.encoded_len() > MAX_RPC_MESSAGE_BYTES
                    || !response_counts_valid(&response)
                {
                    response = wire::Response {
                        error: Some(error(
                            if mutation {
                                wire::ErrorCode::UnknownOutcome
                            } else {
                                wire::ErrorCode::ResourceLimit
                            },
                            "complete RPC response exceeds byte or count limit",
                        )),
                        ..Default::default()
                    };
                }
                operation.mark_responding();
                let mut reply = RpcReply::from_parts(response, reply_ownership);
                reply.retain(operation);
                reply.retain(lease);
                let _ = send.send(reply);
            });
            receive
                .await
                .map_err(|_| RpcError::Failure("RPC execution owner failed".into()))
        })
    }
}

fn response_counts_valid(response: &wire::Response) -> bool {
    response.reads.len() <= actordb_protocol::MAX_BATCH_ITEMS
        && response.mutations.len() <= actordb_protocol::MAX_BATCH_ITEMS
        && response.scans.len() <= 100_000
        && response.query.as_ref().is_none_or(|query| {
            query.columns.len() <= 32
                && query.rows.len() <= 100_000
                && query.rows.iter().all(|row| row.cells.len() <= 32)
        })
        && response.inspection.as_ref().is_none_or(|inspection| {
            inspection.health.len() <= 100_000
                && inspection.placement.len() <= 100_000
                && inspection.health.iter().all(|health| {
                    health.worker_names.len() <= actordb_protocol::MAX_BATCH_ITEMS
                        && health.restarts.len() <= actordb_protocol::MAX_BATCH_ITEMS
                })
                && inspection
                    .placement
                    .iter()
                    .all(|row| row.replica_ids.len() <= actordb_protocol::MAX_BATCH_ITEMS)
        })
}

enum DispatchTarget<'a> {
    Table(Table<'a>),
}

impl DispatchTarget<'_> {
    async fn get(
        &self,
        key: String,
        priority: crate::actor::OpPriority,
    ) -> Result<Option<VersionedValue>, NodeError> {
        let Self::Table(table) = self;
        table.get_with_priority(key, priority).await
    }

    async fn put(
        &self,
        key: String,
        value: Vec<u8>,
        ttl: Option<Duration>,
        priority: crate::actor::OpPriority,
    ) -> Result<(), NodeError> {
        let Self::Table(table) = self;
        table.put_with_options(key, value, ttl, priority).await
    }

    async fn put_with_concern(
        &self,
        key: String,
        value: Vec<u8>,
        ttl: Option<Duration>,
        priority: crate::actor::OpPriority,
        options: crate::write_concern::WriteConcernOptions,
    ) -> Result<crate::write_concern::WriteConcernReceipt, crate::write_concern::WriteConcernError>
    {
        let Self::Table(table) = self;
        table
            .put_with_write_concern_options(key, value, ttl, priority, options)
            .await
    }

    async fn delete(
        &self,
        key: String,
        priority: crate::actor::OpPriority,
    ) -> Result<(), NodeError> {
        let Self::Table(table) = self;
        table.delete_with_priority(key, priority).await
    }

    async fn delete_with_concern(
        &self,
        key: String,
        priority: crate::actor::OpPriority,
        options: crate::write_concern::WriteConcernOptions,
    ) -> Result<crate::write_concern::WriteConcernReceipt, crate::write_concern::WriteConcernError>
    {
        let Self::Table(table) = self;
        table
            .delete_with_write_concern_options(key, priority, options)
            .await
    }

    async fn query(
        &self,
        sql: &str,
        params: &[actordb_query::QueryValue],
    ) -> Result<actordb_query::QueryResult, crate::query::QueryExecutionError> {
        let Self::Table(table) = self;
        table.query_with_params(sql, params).await
    }

    async fn scan(
        &self,
        spec: &crate::actor::ScanSpec,
    ) -> Result<Vec<crate::actor::ScanMatch>, crate::node::ScanError> {
        let Self::Table(table) = self;
        table.scan(spec).await
    }
}

async fn dispatch(
    node: &Node,
    request: wire::Request,
    ownership: &mut actordb_protocol::rpc::RpcOwnership,
) -> wire::Response {
    use wire::request::Operation;
    let mut response = wire::Response {
        ..Default::default()
    };
    let target = match node.table(request.table.clone()) {
        Ok(table) => DispatchTarget::Table(table),
        Err(failure) => {
            response.error = Some(error(wire::ErrorCode::Invalid, &failure.to_string()));
            return response;
        }
    };
    let priority_is_valid = match &request.operation {
        Some(wire::request::Operation::Get(get)) => {
            actordb_protocol::meta::Priority::try_from(get.priority).is_ok()
        }
        Some(wire::request::Operation::Put(put)) => {
            actordb_protocol::meta::Priority::try_from(put.priority).is_ok()
        }
        Some(wire::request::Operation::Delete(delete)) => {
            actordb_protocol::meta::Priority::try_from(delete.priority).is_ok()
        }
        Some(wire::request::Operation::BatchGet(batch)) => {
            actordb_protocol::meta::Priority::try_from(batch.priority).is_ok()
        }
        Some(wire::request::Operation::BatchPut(batch)) => {
            actordb_protocol::meta::Priority::try_from(batch.priority).is_ok()
                && batch
                    .puts
                    .iter()
                    .all(|put| actordb_protocol::meta::Priority::try_from(put.priority).is_ok())
        }
        _ => true,
    };
    if !priority_is_valid {
        response.error = Some(error(wire::ErrorCode::Invalid, "unknown priority"));
        return response;
    }
    match request.operation {
        Some(Operation::Get(get)) => {
            let result = target.get(get.key.clone(), priority(get.priority)).await;
            response.reads.push(read_result(get.key, result));
        }
        Some(Operation::Put(put)) => {
            if let Some(encoded) = put.write_concern.as_deref() {
                let result = match crate::write_concern::WriteConcernOptions::decode(encoded) {
                    Ok(options) => {
                        target
                            .put_with_concern(
                                put.key.clone(),
                                put.value,
                                put.ttl_ms.map(Duration::from_millis),
                                priority(put.priority),
                                options,
                            )
                            .await
                    }
                    Err(error) => Err(error.into()),
                };
                response
                    .mutations
                    .push(concern_mutation_result(put.key, result));
                return response;
            }
            let result = target
                .put(
                    put.key.clone(),
                    put.value,
                    put.ttl_ms.map(Duration::from_millis),
                    priority(put.priority),
                )
                .await;
            response.mutations.push(mutation_result(put.key, result));
        }
        Some(Operation::Delete(delete)) => {
            if let Some(encoded) = delete.write_concern.as_deref() {
                let result = match crate::write_concern::WriteConcernOptions::decode(encoded) {
                    Ok(options) => {
                        target
                            .delete_with_concern(
                                delete.key.clone(),
                                priority(delete.priority),
                                options,
                            )
                            .await
                    }
                    Err(error) => Err(error.into()),
                };
                response
                    .mutations
                    .push(concern_mutation_result(delete.key, result));
                return response;
            }
            let result = target
                .delete(delete.key.clone(), priority(delete.priority))
                .await;
            response.mutations.push(mutation_result(delete.key, result));
        }
        Some(Operation::BatchGet(batch)) => {
            if batch.keys.len() > actordb_protocol::MAX_BATCH_ITEMS {
                response.error = Some(error(wire::ErrorCode::ResourceLimit, "too many batch keys"));
            } else {
                let DispatchTarget::Table(table) = &target;
                match node
                    .rpc_batch_get(table.id().name(), batch.keys, priority(batch.priority))
                    .await
                {
                    Ok((reads, retention)) => {
                        response.reads = reads;
                        if let Some(retention) = retention {
                            ownership.push(Box::new(retention));
                        }
                    }
                    Err(error) => response.error = Some(error),
                }
            }
        }
        Some(Operation::BatchPut(batch)) => {
            if batch.puts.iter().any(|put| put.write_concern.is_some()) {
                response.error = Some(error(
                    wire::ErrorCode::Unsupported,
                    "batch write concerns are not supported",
                ));
                return response;
            }
            if batch.puts.len() > actordb_protocol::MAX_BATCH_ITEMS {
                response.error = Some(error(wire::ErrorCode::ResourceLimit, "too many batch puts"));
            } else {
                let DispatchTarget::Table(table) = &target;
                match node
                    .rpc_batch_put(table.id().name(), batch.puts, priority(batch.priority))
                    .await
                {
                    Ok(results) => response.mutations = results,
                    Err(error) => response.error = Some(error),
                }
            }
        }
        Some(Operation::Query(query)) => {
            let params = query
                .params
                .into_iter()
                .map(decode_cell)
                .collect::<Result<Vec<_>, _>>();
            match params {
                Err(message) => response.error = Some(error(wire::ErrorCode::Invalid, message)),
                Ok(params) => match target.query(&query.sql, &params).await {
                    Ok(result) => {
                        response.query = Some(wire::QueryResult {
                            columns: result.columns,
                            rows: result
                                .rows
                                .into_iter()
                                .map(|cells| wire::QueryRow {
                                    cells: cells.into_iter().map(encode_cell).collect(),
                                })
                                .collect(),
                        })
                    }
                    Err(failure) => response.error = Some(query_error(failure)),
                },
            }
        }
        Some(Operation::Inspect(_)) => match node.health().await {
            Err(failure) => response.error = Some(node_error(failure)),
            Ok(health) => {
                let placement = node.placement_report().await;
                let mut ids: HashSet<_> = placement
                    .placement
                    .keys()
                    .chain(placement.states.keys())
                    .copied()
                    .collect();
                ids.extend(health.keys().copied());
                let mut placement_rows: Vec<_> = ids
                    .into_iter()
                    .map(|fragment| wire::Placement {
                        fragment: fragment.0,
                        replica_ids: placement
                            .placement
                            .get(&fragment)
                            .cloned()
                            .unwrap_or_default(),
                        state: placement
                            .states
                            .get(&fragment)
                            .map(|state| format!("{state:?}"))
                            .unwrap_or_else(|| {
                                if health.contains_key(&fragment) {
                                    "Serving".into()
                                } else {
                                    "NotOwned".into()
                                }
                            }),
                    })
                    .collect();
                placement_rows.sort_by_key(|row| row.fragment);
                let mut health: Vec<_> = health
                    .into_values()
                    .map(|item| wire::FragmentHealth {
                        fragment: item.fragment.0,
                        worker_count: item.worker_count as u64,
                        worker_names: item.worker_names,
                        restarts: item.restarts,
                        shed_high: item.shed_counts.0,
                        shed_default: item.shed_counts.1,
                        shed_background: item.shed_counts.2,
                    })
                    .collect();
                health.sort_by_key(|row| row.fragment);
                response.inspection = Some(wire::Inspection {
                    node_id: node.id().into(),
                    peer_addr: node.addr().to_string(),
                    peer_count: node.peer_count().await as u64,
                    health,
                    placement: placement_rows,
                    dynamic: placement.dynamic,
                });
            }
        },
        Some(Operation::Scan(scan)) => {
            let request = actordb_protocol::meta::ScanRequest {
                matcher_kind: scan.matcher_kind,
                matcher_arg: scan.prefix,
                transform_kind: scan.transform_kind,
                transform_arg: scan.prefix_bytes,
                limit: scan.limit,
                offset: scan.offset,
                ..Default::default()
            };
            match crate::actor::ScanSpec::from_scan_request(&request) {
                Err(message) => response.error = Some(error(wire::ErrorCode::Invalid, &message)),
                Ok(spec) => match target.scan(&spec).await {
                    Ok(matches) => {
                        response.scans = matches.into_iter().map(|item| item.to_proto()).collect()
                    }
                    Err(failure) => {
                        response.error =
                            Some(error(wire::ErrorCode::Unavailable, &failure.to_string()))
                    }
                },
            }
        }
        None => {
            response.error = Some(error(
                wire::ErrorCode::Unsupported,
                "missing or unsupported operation",
            ))
        }
    }
    response
}
fn priority(value: i32) -> OpPriority {
    match actordb_protocol::meta::Priority::try_from(value) {
        Ok(actordb_protocol::meta::Priority::High) => OpPriority::High,
        Ok(actordb_protocol::meta::Priority::Background) => OpPriority::Background,
        _ => OpPriority::Default,
    }
}
pub(crate) fn read_result(
    key: String,
    result: Result<Option<VersionedValue>, NodeError>,
) -> wire::ReadResult {
    match result {
        Ok(value) => wire::ReadResult {
            key,
            value: value.map(|value| wire::Value {
                bytes: value.value,
                timestamp_ms: value.timestamp_ms,
                logical_counter: value.logical_counter,
                node_id: value.node_id,
                expires_at_ms: value.expires_at_ms,
            }),
            error: None,
        },
        Err(failure) => wire::ReadResult {
            key,
            value: None,
            error: Some(node_error(failure)),
        },
    }
}
pub(crate) fn mutation_result(key: String, result: Result<(), NodeError>) -> wire::MutationResult {
    wire::MutationResult {
        key,
        error: result.err().map(|failure| {
            if matches!(
                failure,
                NodeError::Io(_) | NodeError::Network(_) | NodeError::Actor(_)
            ) {
                error(wire::ErrorCode::UnknownOutcome, &failure.to_string())
            } else {
                node_error(failure)
            }
        }),

        write_concern: None,
    }
}

fn concern_mutation_result(
    key: String,
    result: Result<
        crate::write_concern::WriteConcernReceipt,
        crate::write_concern::WriteConcernError,
    >,
) -> wire::MutationResult {
    use crate::write_concern::{WriteConcernError, WriteConcernOutcome};
    match result {
        Ok(receipt) => wire::MutationResult {
            key,
            error: None,
            write_concern: Some(WriteConcernOutcome::Satisfied(receipt).encode()),
        },
        Err(WriteConcernError::Operation(error)) => mutation_result(key, Err(error)),
        Err(failure) => {
            let encoded = failure.outcome().map(|outcome| outcome.encode());
            let code = match failure {
                WriteConcernError::TimedOut(_) | WriteConcernError::UnknownOutcome(_) => {
                    wire::ErrorCode::UnknownOutcome
                }
                WriteConcernError::Backpressure => wire::ErrorCode::Backpressure,
                WriteConcernError::DurabilityFailed { .. } => wire::ErrorCode::Storage,
                WriteConcernError::Unsupported(_) => wire::ErrorCode::Unsupported,
                _ => wire::ErrorCode::Invalid,
            };
            wire::MutationResult {
                key,
                error: Some(error(code, &failure.to_string())),
                write_concern: encoded,
            }
        }
    }
}
pub(crate) fn error(code: wire::ErrorCode, message: &str) -> wire::Error {
    wire::Error {
        code: code as i32,
        message: message.chars().take(1024).collect(),
        ..Default::default()
    }
}
fn node_error(failure: NodeError) -> wire::Error {
    use wire::ErrorCode;
    let code = match &failure {
        NodeError::Io(_) | NodeError::Actor(_) | NodeError::Network(_) => ErrorCode::Unavailable,
        NodeError::UnknownOutcome(_) => ErrorCode::UnknownOutcome,
        NodeError::Codec(_) => ErrorCode::Invalid,
        NodeError::Backpressure(_) => ErrorCode::Backpressure,
        NodeError::Storage(_) => ErrorCode::Storage,
        NodeError::Superseded { .. } => ErrorCode::Superseded,
        NodeError::Expired => ErrorCode::Expired,
        NodeError::ShuttingDown => ErrorCode::ShuttingDown,
    };
    let mut result = error(code, &failure.to_string());
    if let NodeError::Superseded {
        timestamp_ms,
        logical_counter,
        node_id,
        is_tombstone,
    } = failure
    {
        result.winner_timestamp_ms = timestamp_ms;
        result.winner_logical_counter = logical_counter;
        result.winner_node_id = node_id;
        result.winner_is_tombstone = is_tombstone;
    }
    result
}
fn query_error(failure: crate::query::QueryExecutionError) -> wire::Error {
    use crate::query::QueryExecutionError;
    let code = match failure {
        QueryExecutionError::InvalidQuery(_) => wire::ErrorCode::Invalid,
        QueryExecutionError::BudgetExceeded(_) => wire::ErrorCode::ResourceLimit,
        QueryExecutionError::Backpressure(_) => wire::ErrorCode::Backpressure,
        QueryExecutionError::QueryChanged(_) => wire::ErrorCode::QueryChanged,
        QueryExecutionError::ShuttingDown => wire::ErrorCode::ShuttingDown,
        _ => wire::ErrorCode::Unavailable,
    };
    error(code, &failure.to_string())
}
fn encode_cell(value: actordb_query::QueryValue) -> actordb_protocol::meta::QueryCell {
    use actordb_protocol::meta::query_cell::Value;
    actordb_protocol::meta::QueryCell {
        value: Some(match value {
            actordb_query::QueryValue::Null => Value::Null(true),
            actordb_query::QueryValue::Text(v) => Value::Text(v),
            actordb_query::QueryValue::Bytes(v) => Value::Bytes(v),
            actordb_query::QueryValue::Integer(v) => Value::Integer(v.to_string()),
        }),
    }
}
fn decode_cell(
    value: actordb_protocol::meta::QueryCell,
) -> Result<actordb_query::QueryValue, &'static str> {
    use actordb_protocol::meta::query_cell::Value;
    match value.value {
        Some(Value::Null(true)) => Ok(actordb_query::QueryValue::Null),
        Some(Value::Text(v)) => Ok(actordb_query::QueryValue::Text(v)),
        Some(Value::Bytes(v)) => Ok(actordb_query::QueryValue::Bytes(v)),
        Some(Value::Integer(v)) => {
            let integer: i128 = v.parse().map_err(|_| "invalid integer parameter")?;
            if integer.to_string() != v {
                return Err("noncanonical integer parameter");
            }
            Ok(actordb_query::QueryValue::Integer(integer))
        }
        _ => Err("invalid parameter"),
    }
}