dyniak 1.4.1

Riak-compatible protocol surface (HTTP + PBC) and storage bridge for the Dynomite Rust port
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
//! End-to-end tests for Riak object links across HTTP and PBC.
//!
//! Covers the link feature's storage path: parsing `Link:` request
//! headers on an HTTP `PUT`, re-emitting them on the matching `GET`,
//! and keeping the storage form compatible across the two transports
//! so a link attached over HTTP is visible over PBC and a value
//! stored over PBC is fetchable over HTTP.
//!
//! Gated on the `noxu` feature: the real object store is
//! `NoxuDatastore`, the only backend with a K/V object layer.
#![cfg(feature = "noxu")]

use std::sync::Arc;
use std::time::Duration;

use prost::Message as _;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::{TcpListener, TcpStream};

use dyniak::datastore::NoxuDatastore;
use dyniak::proto::http::object::HttpObject;
use dyniak::proto::pb::{
    read_frame, write_frame, Frame, MessageCode, RpbContent, RpbGetReq, RpbGetResp, RpbLink,
    RpbPutReq, RpbPutResp,
};
use dyniak::{serve_http, serve_pbc};
use dynomite::embed::Datastore;
use tempfile::TempDir;

/// Spawn an HTTP gateway and a PBC server over the same backing
/// [`NoxuDatastore`], returning both bound addresses and the store
/// handle so a test can read the raw storage form directly.
async fn spawn_dual() -> (
    std::net::SocketAddr,
    std::net::SocketAddr,
    Arc<NoxuDatastore>,
    TempDir,
) {
    let dir = TempDir::new().expect("tempdir");
    let ds = Arc::new(NoxuDatastore::open_transactional(dir.path()).expect("open transactional"));

    let http_listener = TcpListener::bind("127.0.0.1:0").await.expect("bind http");
    let http_addr = http_listener.local_addr().expect("http addr");
    let http_ds: Arc<dyn Datastore> = ds.clone();
    tokio::spawn(async move { serve_http(http_listener, http_ds).await });

    let pbc_listener = TcpListener::bind("127.0.0.1:0").await.expect("bind pbc");
    let pbc_addr = pbc_listener.local_addr().expect("pbc addr");
    let pbc_ds: Arc<dyn Datastore> = ds.clone();
    tokio::spawn(async move {
        let _ = serve_pbc(pbc_listener, pbc_ds).await;
    });

    tokio::time::sleep(Duration::from_millis(15)).await;
    (http_addr, pbc_addr, ds, dir)
}

/// Send a raw HTTP request and read the full response to EOF.
async fn send_raw(addr: std::net::SocketAddr, request: &str) -> Vec<u8> {
    let mut stream = TcpStream::connect(addr).await.expect("connect");
    stream
        .write_all(request.as_bytes())
        .await
        .expect("write request");
    stream.flush().await.expect("flush");
    let mut buf = Vec::with_capacity(4096);
    let read = tokio::time::timeout(Duration::from_secs(5), stream.read_to_end(&mut buf))
        .await
        .expect("response timeout");
    let _ = read.expect("read response");
    buf
}

/// Split a raw HTTP/1.1 response into status code and the list of
/// `Link:` header values (one entry per header line).
fn parse_status_and_links(resp: &[u8]) -> (u16, Vec<String>) {
    let sep = find_subslice(resp, b"\r\n\r\n").expect("end of headers");
    let head = std::str::from_utf8(&resp[..sep]).expect("ascii headers");
    let mut lines = head.split("\r\n");
    let status = lines
        .next()
        .expect("status line")
        .split_whitespace()
        .nth(1)
        .expect("status code")
        .parse()
        .expect("numeric status");
    let mut link_values = Vec::new();
    for line in lines {
        if let Some((key, value)) = line.split_once(':') {
            if key.eq_ignore_ascii_case("link") {
                link_values.push(value.trim().to_string());
            }
        }
    }
    (status, link_values)
}

fn find_subslice(haystack: &[u8], needle: &[u8]) -> Option<usize> {
    haystack
        .windows(needle.len())
        .position(|window| window == needle)
}

async fn pbc_get(addr: std::net::SocketAddr, bucket: &[u8], key: &[u8]) -> Vec<RpbContent> {
    let mut stream = TcpStream::connect(addr).await.expect("pbc connect");
    let req = RpbGetReq {
        bucket: bucket.to_vec(),
        key: key.to_vec(),
        ..RpbGetReq::default()
    };
    let frame = Frame::new(MessageCode::GetReq.as_u8(), req.encode_to_vec());
    let (mut r, mut w) = tokio::io::split(&mut stream);
    write_frame(&mut w, &frame).await.expect("send get");
    let resp = read_frame(&mut r).await.expect("recv get");
    assert_eq!(resp.code, MessageCode::GetResp.as_u8());
    RpbGetResp::decode(resp.body.as_slice())
        .expect("decode get")
        .content
}

async fn pbc_put(addr: std::net::SocketAddr, bucket: &[u8], key: &[u8], value: &[u8]) {
    pbc_put_content(
        addr,
        bucket,
        key,
        RpbContent {
            value: value.to_vec(),
            ..RpbContent::default()
        },
    )
    .await;
}

/// Put an object whose `RpbContent` carries arbitrary metadata
/// (links, content-type, indexes).
async fn pbc_put_content(
    addr: std::net::SocketAddr,
    bucket: &[u8],
    key: &[u8],
    content: RpbContent,
) {
    let mut stream = TcpStream::connect(addr).await.expect("pbc connect");
    let req = RpbPutReq {
        bucket: bucket.to_vec(),
        key: Some(key.to_vec()),
        content: Some(content),
        ..RpbPutReq::default()
    };
    let frame = Frame::new(MessageCode::PutReq.as_u8(), req.encode_to_vec());
    let (mut r, mut w) = tokio::io::split(&mut stream);
    write_frame(&mut w, &frame).await.expect("send put");
    let resp = read_frame(&mut r).await.expect("recv put");
    assert_eq!(resp.code, MessageCode::PutResp.as_u8());
    let _ = RpbPutResp::decode(resp.body.as_slice()).expect("decode put");
}

#[tokio::test]
async fn http_link_headers_round_trip() {
    let (http_addr, _pbc_addr, ds, _dir) = spawn_dual().await;

    // PUT an object carrying two links (one per tag) split across two
    // Link header lines, plus a comma-separated value on the first.
    let body = "{\"value\":[104,105]}"; // {"value": "hi"} -> bytes
    let put = format!(
        "PUT /buckets/people/keys/alice HTTP/1.1\r\n\
         Host: localhost\r\n\
         Content-Type: application/json\r\n\
         Link: </buckets/people/keys/bob>; riaktag=\"friend\"\r\n\
         Link: </buckets/work/keys/acme>; riaktag=\"employer\"\r\n\
         Content-Length: {}\r\n\
         Connection: close\r\n\r\n{body}",
        body.len()
    );
    let resp = send_raw(http_addr, &put).await;
    assert_eq!(parse_status_and_links(&resp).0, 204, "put status");

    // The stored envelope carries both links.
    let stored = ds
        .get_object(b"people", b"alice")
        .expect("get_object")
        .expect("present");
    let obj = HttpObject::from_storage_bytes(&stored).expect("decode envelope");
    assert_eq!(obj.links.len(), 2, "two links stored");
    assert!(obj
        .links
        .iter()
        .any(|l| l.bucket == "people" && l.key == "bob" && l.tag == "friend"));
    assert!(obj
        .links
        .iter()
        .any(|l| l.bucket == "work" && l.key == "acme" && l.tag == "employer"));

    // GET re-emits both links as response headers, plus the bucket-up
    // link Riak always adds.
    let get = "GET /buckets/people/keys/alice HTTP/1.1\r\n\
         Host: localhost\r\nAccept: application/json\r\nConnection: close\r\n\r\n";
    let resp = send_raw(http_addr, get).await;
    let (status, links) = parse_status_and_links(&resp);
    assert_eq!(status, 200, "get status");
    assert!(
        links
            .iter()
            .any(|l| l.contains("/buckets/people/keys/bob") && l.contains("friend")),
        "friend link re-emitted: {links:?}"
    );
    assert!(
        links
            .iter()
            .any(|l| l.contains("/buckets/work/keys/acme") && l.contains("employer")),
        "employer link re-emitted: {links:?}"
    );
    assert!(
        links.iter().any(|l| l.contains("rel=\"up\"")),
        "bucket-up link present: {links:?}"
    );
}

#[tokio::test]
async fn link_put_over_http_is_visible_over_pbc() {
    let (http_addr, pbc_addr, _ds, _dir) = spawn_dual().await;

    // hello -> [104,101,108,108,111]
    let body = "{\"value\":[104,101,108,108,111]}";
    let put = format!(
        "PUT /buckets/people/keys/carol HTTP/1.1\r\n\
         Host: localhost\r\n\
         Content-Type: application/json\r\n\
         Link: </buckets/people/keys/dave>; riaktag=\"friend\"\r\n\
         Content-Length: {}\r\n\
         Connection: close\r\n\r\n{body}",
        body.len()
    );
    assert_eq!(
        parse_status_and_links(&send_raw(http_addr, &put).await).0,
        204
    );

    // PBC get returns the object payload (the value), not the raw
    // envelope, proving the shared storage form is unwrapped on read.
    let content = pbc_get(pbc_addr, b"people", b"carol").await;
    assert_eq!(content.len(), 1);
    assert_eq!(content[0].value, b"hello".to_vec());
    // The link attached over HTTP is now visible in the PBC content.
    assert_eq!(content[0].links.len(), 1, "http link visible over pbc");
    assert_eq!(content[0].links[0].key.as_deref(), Some(b"dave".as_slice()));
    assert_eq!(
        content[0].links[0].tag.as_deref(),
        Some(b"friend".as_slice())
    );
}

#[tokio::test]
async fn value_put_over_pbc_is_fetchable_over_http() {
    let (http_addr, pbc_addr, _ds, _dir) = spawn_dual().await;

    pbc_put(pbc_addr, b"people", b"erin", b"world").await;

    let get = "GET /buckets/people/keys/erin HTTP/1.1\r\n\
         Host: localhost\r\nAccept: application/x-protobuf\r\nConnection: close\r\n\r\n";
    let resp = send_raw(http_addr, get).await;
    let sep = find_subslice(&resp, b"\r\n\r\n").expect("headers");
    let (status, _links) = parse_status_and_links(&resp);
    assert_eq!(status, 200, "http get status");
    let obj = HttpObject::from_storage_bytes(&resp[sep + 4..]).expect("decode protobuf body");
    assert_eq!(obj.value, b"world");
    assert!(obj.links.is_empty(), "pbc put attaches no links");
}

#[tokio::test]
async fn pbc_link_put_round_trips_over_pbc() {
    let (_http_addr, pbc_addr, _ds, _dir) = spawn_dual().await;

    // PUT over PBC with two links carrying distinct tags.
    let content = RpbContent {
        value: b"src".to_vec(),
        links: vec![
            RpbLink {
                bucket: Some(b"people".to_vec()),
                key: Some(b"bob".to_vec()),
                tag: Some(b"friend".to_vec()),
            },
            RpbLink {
                bucket: Some(b"work".to_vec()),
                key: Some(b"acme".to_vec()),
                tag: Some(b"employer".to_vec()),
            },
        ],
        ..RpbContent::default()
    };
    pbc_put_content(pbc_addr, b"people", b"frank", content).await;

    let got = pbc_get(pbc_addr, b"people", b"frank").await;
    assert_eq!(got.len(), 1);
    assert_eq!(got[0].value, b"src".to_vec());
    let links = &got[0].links;
    assert_eq!(links.len(), 2, "both links returned over pbc");
    assert!(links
        .iter()
        .any(|l| l.key.as_deref() == Some(b"bob".as_slice())
            && l.tag.as_deref() == Some(b"friend".as_slice())));
    assert!(links
        .iter()
        .any(|l| l.key.as_deref() == Some(b"acme".as_slice())
            && l.tag.as_deref() == Some(b"employer".as_slice())));
}

#[tokio::test]
async fn pbc_link_put_is_visible_over_http() {
    let (http_addr, pbc_addr, ds, _dir) = spawn_dual().await;

    let content = RpbContent {
        value: b"hi".to_vec(),
        links: vec![RpbLink {
            bucket: Some(b"people".to_vec()),
            key: Some(b"dave".to_vec()),
            tag: Some(b"friend".to_vec()),
        }],
        ..RpbContent::default()
    };
    pbc_put_content(pbc_addr, b"people", b"grace", content).await;

    // The stored envelope carries the link mapped from the PBC put.
    let stored = ds
        .get_object(b"people", b"grace")
        .expect("get_object")
        .expect("present");
    let obj = HttpObject::from_storage_bytes(&stored).expect("decode envelope");
    assert_eq!(obj.links.len(), 1, "pbc link persisted on the envelope");
    assert_eq!(obj.links[0].key, "dave");
    assert_eq!(obj.links[0].tag, "friend");

    // An HTTP GET re-emits the PBC-attached link as a Link: header.
    let get = "GET /buckets/people/keys/grace HTTP/1.1\r\n\
         Host: localhost\r\nAccept: application/json\r\nConnection: close\r\n\r\n";
    let resp = send_raw(http_addr, get).await;
    let (status, links) = parse_status_and_links(&resp);
    assert_eq!(status, 200, "http get status");
    assert!(
        links
            .iter()
            .any(|l| l.contains("/buckets/people/keys/dave") && l.contains("friend")),
        "pbc-attached link visible over http: {links:?}"
    );
}

#[tokio::test]
async fn pbc_content_metadata_round_trips() {
    let (_http_addr, pbc_addr, _ds, _dir) = spawn_dual().await;

    let content = RpbContent {
        value: b"payload".to_vec(),
        content_type: Some(b"application/json".to_vec()),
        indexes: vec![
            dyniak::proto::pb::RpbPair {
                key: b"age_int".to_vec(),
                value: Some(b"42".to_vec()),
            },
            dyniak::proto::pb::RpbPair {
                key: b"city_bin".to_vec(),
                value: Some(b"seattle".to_vec()),
            },
        ],
        ..RpbContent::default()
    };
    pbc_put_content(pbc_addr, b"users", b"hank", content).await;

    let got = pbc_get(pbc_addr, b"users", b"hank").await;
    assert_eq!(got.len(), 1);
    assert_eq!(got[0].value, b"payload".to_vec());
    assert_eq!(
        got[0].content_type.as_deref(),
        Some(b"application/json".as_slice())
    );
    let mut idx: Vec<(Vec<u8>, Option<Vec<u8>>)> = got[0]
        .indexes
        .iter()
        .map(|p| (p.key.clone(), p.value.clone()))
        .collect();
    idx.sort();
    assert_eq!(
        idx,
        vec![
            (b"age_int".to_vec(), Some(b"42".to_vec())),
            (b"city_bin".to_vec(), Some(b"seattle".to_vec())),
        ]
    );
}

#[tokio::test]
async fn mapreduce_link_phase_walks_pbc_persisted_links() {
    use dyniak::mapreduce::{
        builtins::default_registry, run_job_full, Inputs, KeyDatum, MapReduceJob, Phase,
    };

    let (_http_addr, pbc_addr, ds, _dir) = spawn_dual().await;

    // Persist a source object with two friend links over PBC.
    let content = RpbContent {
        value: b"src".to_vec(),
        links: vec![
            RpbLink {
                bucket: Some(b"people".to_vec()),
                key: Some(b"bob".to_vec()),
                tag: Some(b"friend".to_vec()),
            },
            RpbLink {
                bucket: Some(b"people".to_vec()),
                key: Some(b"carol".to_vec()),
                tag: Some(b"friend".to_vec()),
            },
        ],
        ..RpbContent::default()
    };
    pbc_put_content(pbc_addr, b"people", b"alice", content).await;

    // Run a link phase over the same backing store; it must walk the
    // links the PBC put persisted onto the envelope.
    let job = MapReduceJob {
        inputs: Inputs::KeyData(vec![KeyDatum::pair("people", "alice")]),
        phases: vec![Phase::Link {
            bucket: Some("people".into()),
            tag: Some("friend".into()),
            keep: true,
        }],
        timeout_ms: None,
    };
    let store: Arc<dyn Datastore> = ds;
    let out = run_job_full(job, Arc::new(default_registry()), None, Some(store))
        .await
        .expect("link phase ok");
    let mut targets: Vec<String> = out
        .iter()
        .map(|o| o.value["key"].as_str().expect("key").to_string())
        .collect();
    targets.sort();
    assert_eq!(
        targets,
        vec!["bob".to_string(), "carol".to_string()],
        "link phase walked the two PBC-persisted friend links"
    );
}