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
use super::*;
impl_veilid_log_facility!("stor");
/// The fully parsed descriptor
struct InspectDescriptorInfo {
/// The descriptor itself
descriptor: Arc<SignedValueDescriptor>,
/// The in-schema subkeys that overlap the inspected range
subkeys: ValueSubkeyRangeSet,
}
impl InspectDescriptorInfo {
pub fn new(
descriptor: Arc<SignedValueDescriptor>,
subkeys: &ValueSubkeyRangeSet,
) -> VeilidAPIResult<Self> {
let schema = descriptor.schema().map_err(RPCError::invalid_format)?;
let subkeys = schema.truncate_subkeys(subkeys, Some(DHTSchema::MAX_SUBKEY_COUNT));
Ok(Self {
descriptor,
subkeys,
})
}
}
/// Info tracked per subkey
struct SubkeySeqCount {
/// The newest sequence number found for a subkey
pub seq: ValueSeqNum,
/// The set of nodes that had the most recent value for this subkey
pub consensus_nodes: Vec<NodeRef>,
/// The set of nodes that had any value for this subkey
pub value_nodes: Vec<NodeRef>,
}
/// The context of the outbound_inspect_value operation
struct OutboundInspectValueContext {
/// The combined sequence numbers and result counts so far
pub seqcounts: Vec<SubkeySeqCount>,
/// The descriptor if we got a fresh one or empty if no descriptor was needed
pub opt_descriptor_info: Option<InspectDescriptorInfo>,
}
/// The result of the outbound_inspect_value operation
#[derive(Debug, Clone)]
pub(in crate::storage_manager) struct OutboundInspectValueResult {
/// Fanout results for each subkey
pub subkey_fanout_results: Vec<FanoutResult>,
/// The inspection that was retrieved
pub inspect_result: InspectResult,
}
/// The result of the inbound_inspect_value operation
#[derive(Clone, Debug)]
pub(crate) enum InboundInspectValueResult {
/// Value inspected successfully
Success(InspectResult),
}
impl StorageManager {
/// Inspect an opened DHT record for its subkey sequence numbers
#[cfg_attr(
feature = "instrument",
instrument(level = "trace", target = "stor", skip_all, fields(__VEILID_LOG_KEY = self.log_key()))
)]
pub async fn inspect_record(
&self,
record_key: RecordKey,
subkeys: ValueSubkeyRangeSet,
scope: DHTReportScope,
) -> VeilidAPIResult<DHTRecordReport> {
let Ok(_guard) = self.startup_lock.enter() else {
apibail_not_initialized!();
};
let opaque_record_key = record_key.opaque();
let subkeys = if subkeys.is_empty() {
ValueSubkeyRangeSet::full()
} else {
subkeys
};
let peek_lock = self
.record_lock_table
.peek_lock(opaque_record_key.clone())
.measure_debug(
TimestampDuration::new_ms(200),
veilid_log_dbg!(self, "StorageManager::inspect_record lock"),
)
.await;
let safety_selection = {
let inner = self.inner.lock();
let Some(opened_record) = inner.opened_records.get(&opaque_record_key) else {
apibail_generic!("record not open");
};
opened_record.safety_selection()
};
// See if the requested record is our local record store
let mut local_inspect_result = self
.handle_inspect_local_values_with_peek_lock(&peek_lock, subkeys.clone(), true)
.await?;
// Get the offline subkeys for this record still only returning the ones we're inspecting
// Merge in the currently offline in-flight records and the actively-being-written subkeys as well
let offline_subkey_writes = {
let inner = self.inner.lock();
// Get actively-being-written subkeys
let purpose_state = self
.record_lock_table
.get_record_lock_purpose_state(&opaque_record_key);
let mut active_subkey_writes = ValueSubkeyRangeSet::new();
if purpose_state.whole_record_lock_purpose.is_none() {
for (subkey, purpose) in purpose_state.subkey_lock_purpose.into_iter() {
if matches!(
purpose,
StorageManagerSubkeyLockPurpose::Set
| StorageManagerSubkeyLockPurpose::TransactSet
) {
active_subkey_writes.insert(subkey);
}
}
};
// Merge offline subkeys + offline-in-flight subkeys + actively-being-written subkeys
inner
.offline_subkey_writes
.get(&opaque_record_key)
.map(|o| o.subkeys.union(&o.subkeys_in_flight))
.unwrap_or_default()
.union(&active_subkey_writes)
.intersect(&subkeys)
};
// If this is the maximum scope we're interested in, return the report
if matches!(scope, DHTReportScope::Local) {
return DHTRecordReport::new(
local_inspect_result.subkeys().clone(),
offline_subkey_writes,
local_inspect_result.seqs().to_vec(),
vec![ValueSeqNum::NONE; local_inspect_result.seqs().len()],
)
.inspect_err(|e| {
veilid_log!(self error "invalid record report generated: {}", e);
});
}
// Get rpc processor and drop mutex so we don't block while getting the value from the network
if !self.dht_is_online() {
apibail_try_again!("offline, try again later");
};
// If we're simulating a set, increase the previous sequence number we have by 1
if matches!(scope, DHTReportScope::UpdateSet) {
for seq in local_inspect_result.seqs_mut() {
if let Ok(next) = seq.next() {
*seq = next;
}
}
}
// Get the inspect record report from the network
let outbound_inspect_result = self
.outbound_inspect_value(
&opaque_record_key,
subkeys,
safety_selection,
if matches!(scope, DHTReportScope::SyncGet | DHTReportScope::SyncSet) {
InspectResult::default()
} else {
local_inspect_result.clone()
},
matches!(scope, DHTReportScope::UpdateSet | DHTReportScope::SyncSet),
)
.await?;
// Keep the list of nodes that returned a value for later reference
let results_iter = outbound_inspect_result
.inspect_result
.subkeys()
.iter()
.map(ValueSubkeyRangeSet::single)
.zip(outbound_inspect_result.subkey_fanout_results.into_iter());
let existed = self.process_fanout_results(
opaque_record_key.clone(),
results_iter,
false,
self.config().network.dht.consensus_width as usize,
)?;
if !existed {
apibail_internal!(
"record was locked for inspect but is now missing: {}",
opaque_record_key
);
}
if outbound_inspect_result.inspect_result.subkeys().is_empty() {
DHTRecordReport::new(
local_inspect_result.subkeys().clone(),
offline_subkey_writes,
local_inspect_result.seqs().to_vec(),
vec![ValueSeqNum::NONE; local_inspect_result.seqs().len()],
)
} else {
DHTRecordReport::new(
outbound_inspect_result.inspect_result.subkeys().clone(),
offline_subkey_writes,
local_inspect_result.seqs().to_vec(),
outbound_inspect_result.inspect_result.seqs().to_vec(),
)
}
}
////////////////////////////////////////////////////////////////////////
/// Perform a 'inspect value' query on the network
/// Performs the work without a transaction
#[cfg_attr(
feature = "instrument",
instrument(level = "trace", target = "dht", skip_all, err, fields(__VEILID_LOG_KEY = self.log_key()))
)]
pub(super) async fn outbound_inspect_value(
&self,
opaque_record_key: &OpaqueRecordKey,
subkeys: ValueSubkeyRangeSet,
safety_selection: SafetySelection,
local_inspect_result: InspectResult,
use_set_scope: bool,
) -> VeilidAPIResult<OutboundInspectValueResult> {
let routing_domain = RoutingDomain::PublicInternet;
let requested_subkeys = subkeys.clone();
// Get the DHT parameters for 'InspectValue'
// Can use either 'get scope' or 'set scope' depending on the purpose of the inspection
let (node_count, consensus_count, consensus_width, fanout_tasks, timeout) = if use_set_scope
{
let config = self.config();
(
config.network.dht.max_find_node_count as usize,
config.network.dht.set_value_count as usize,
config.network.dht.consensus_width as usize,
config.network.dht.set_value_fanout as usize,
TimestampDuration::from(ms_to_us(config.network.dht.set_value_timeout_ms)),
)
} else {
let config = self.config();
(
config.network.dht.max_find_node_count as usize,
config.network.dht.get_value_count as usize,
config.network.dht.consensus_width as usize,
config.network.dht.get_value_fanout as usize,
TimestampDuration::from(ms_to_us(config.network.dht.get_value_timeout_ms)),
)
};
// Get the nodes we know are caching this value to seed the fanout
let init_fanout_queue = self
.get_value_nodes(opaque_record_key)?
.unwrap_or_default()
.into_iter()
.filter(|x| {
x.node_info(routing_domain)
.map(|ni| ni.has_all_capabilities(&[VEILID_CAPABILITY_DHT]))
.unwrap_or_default()
})
.collect();
// Make operation context
let opt_descriptor_info = if let Some(descriptor) = local_inspect_result.opt_descriptor() {
// Get the descriptor info. This also truncates the subkeys list to what can be returned from the network.
Some(InspectDescriptorInfo::new(descriptor, &subkeys)?)
} else {
None
};
let context = Arc::new(Mutex::new(OutboundInspectValueContext {
seqcounts: local_inspect_result
.seqs()
.iter()
.map(|s| SubkeySeqCount {
seq: *s,
consensus_nodes: vec![],
value_nodes: vec![],
})
.collect(),
opt_descriptor_info,
}));
// Routine to call to generate fanout
let call_routine = {
let context = context.clone();
let registry = self.registry();
let opaque_record_key = opaque_record_key.clone();
let safety_selection = safety_selection.clone();
Arc::new(
move |next_node: NodeRef| -> PinBoxFutureStatic<FanoutCallResult> {
let context = context.clone();
let registry = registry.clone();
let subkeys = subkeys.clone();
let opaque_record_key = opaque_record_key.clone();
let safety_selection = safety_selection.clone();
Box::pin(async move {
let rpc_processor = registry.rpc_processor();
let descriptor_mode = GetDescriptorMode::new(context.lock().opt_descriptor_info.as_ref().map(|x| x.descriptor.clone()));
let iva = match
rpc_processor
.rpc_call_inspect_value(
Destination::direct(next_node.routing_domain_filtered(routing_domain), Some(safety_selection)),
opaque_record_key.clone(),
subkeys.clone(),
descriptor_mode,
)
.await? {
NetworkResult::Timeout => {
return Ok(FanoutCallOutput{peer_info_list: vec![], disposition: FanoutCallDisposition::Timeout});
}
NetworkResult::ServiceUnavailable(_) |
NetworkResult::NoConnection(_) |
NetworkResult::AlreadyExists(_) |
NetworkResult::InvalidMessage(_) => {
return Ok(FanoutCallOutput{peer_info_list: vec![], disposition: FanoutCallDisposition::Invalid});
}
NetworkResult::Value(v) => v
};
let answer = iva.answer;
// Check if we got an accepted result
if !answer.accepted {
// Return peers if we have some
veilid_log!(registry debug target:"network_result", "InspectValue missed, fanout call returned peers {}", answer.peers.len());
return Ok(FanoutCallOutput{peer_info_list: answer.peers, disposition: FanoutCallDisposition::Rejected});
}
// Keep the descriptor if we got one. If we had a last_descriptor it will
// already be validated by rpc_call_inspect_value
if let Some(descriptor) = answer.descriptor {
let mut ctx = context.lock();
if ctx.opt_descriptor_info.is_none() {
// Get the descriptor info. This also truncates the subkeys list to what can be returned from the network.
let descriptor_info =
match InspectDescriptorInfo::new(Arc::new(descriptor.clone()), &subkeys) {
Ok(v) => v,
Err(e) => {
veilid_log!(registry debug target:"network_result", "InspectValue returned an invalid descriptor: {}", e);
return Ok(FanoutCallOutput{peer_info_list: vec![], disposition: FanoutCallDisposition::Invalid});
}
};
ctx.opt_descriptor_info = Some(descriptor_info);
}
}
// Keep the value if we got one and it is newer and it passes schema validation
if answer.seqs.is_empty() {
veilid_log!(registry debug target:"network_result", "InspectValue returned no seq, fanout call returned peers {}", answer.peers.len());
return Ok(FanoutCallOutput{peer_info_list: answer.peers, disposition: FanoutCallDisposition::Rejected});
}
veilid_log!(registry debug target:"network_result", "Got seqs back: len={}", answer.seqs.len());
let mut ctx = context.lock();
// Ensure we have a schema and descriptor etc
let Some(descriptor_info) = &ctx.opt_descriptor_info else {
// Got a value but no descriptor for it
// Move to the next node
veilid_log!(registry debug target:"network_result", "InspectValue returned a value with no descriptor invalid descriptor");
return Ok(FanoutCallOutput{peer_info_list: vec![], disposition: FanoutCallDisposition::Invalid});
};
// Get number of subkeys from schema and ensure we are getting the
// right number of sequence numbers betwen that and what we asked for
#[allow(clippy::unnecessary_cast)]
if answer.seqs.len() as u64 != descriptor_info.subkeys.len() as u64 {
// Not the right number of sequence numbers
// Move to the next node
veilid_log!(registry debug target:"network_result", "wrong number of seqs returned {} (wanted {})",
answer.seqs.len(),
descriptor_info.subkeys.len());
return Ok(FanoutCallOutput{peer_info_list: vec![], disposition: FanoutCallDisposition::Invalid});
}
// If we have a prior seqs list, merge in the new seqs
if ctx.seqcounts.is_empty() {
ctx.seqcounts = answer
.seqs
.iter()
.map(|s| SubkeySeqCount {
seq: *s,
// One node has shown us the newest sequence numbers so far
consensus_nodes: vec![next_node.clone()],
value_nodes: vec![next_node.clone()],
})
.collect();
} else {
if ctx.seqcounts.len() != answer.seqs.len() {
veilid_log!(registry debug target:"network_result", "seqs list length should always be equal by now: {} (wanted {})",
answer.seqs.len(),
ctx.seqcounts.len());
return Ok(FanoutCallOutput{peer_info_list: vec![], disposition: FanoutCallDisposition::Invalid});
}
for pair in ctx.seqcounts.iter_mut().zip(answer.seqs.iter()) {
let ctx_seqcnt = pair.0;
let answer_seq = *pair.1;
// If we already have consensus for this subkey, don't bother updating it any more
// While we may find a better sequence number if we keep looking, this does not mimic the behavior
// of get and set unless we stop here
if ctx_seqcnt.consensus_nodes.len() >= consensus_count {
continue;
}
// If the new seq isn't undefined and is better than the old seq (either greater or old is undefined)
// Then take that sequence number and note that we have gotten newer sequence numbers so we keep
// looking for consensus
// If the sequence number matches the old sequence number, then we keep the value node for reference later
if answer_seq.is_some() {
if answer_seq > ctx_seqcnt.seq {
// One node has shown us the latest sequence numbers so far
ctx_seqcnt.seq = answer_seq;
ctx_seqcnt.consensus_nodes = vec![next_node.clone()];
} else if answer_seq == ctx_seqcnt.seq {
// Keep the nodes that showed us the latest values
ctx_seqcnt.consensus_nodes.push(next_node.clone());
}
}
ctx_seqcnt.value_nodes.push(next_node.clone());
}
}
// Return peers if we have some
veilid_log!(registry debug target:"network_result", "InspectValue fanout call returned peers {}", answer.peers.len());
// Inspect doesn't actually use the fanout queue consensus tracker
Ok(FanoutCallOutput { peer_info_list: answer.peers, disposition: FanoutCallDisposition::Accepted})
}.instrument(tracing::trace_span!("outbound_inspect_value fanout call"))) as PinBoxFuture<FanoutCallResult>
},
)
};
// Routine to call to check if we're done at each step
// For inspect, we are tracking consensus externally from the FanoutCall,
// for each subkey, rather than a single consensus, so the single fanoutresult
// that is passed in here is ignored in favor of our own per-subkey tracking
let check_done = {
let context = context.clone();
Arc::new(move |_: &FanoutResult| {
// If we have reached sufficient consensus on all subkeys, return done
let ctx = context.lock();
let mut has_consensus = true;
for cs in ctx.seqcounts.iter() {
if cs.consensus_nodes.len() < consensus_count {
has_consensus = false;
break;
}
}
if !ctx.seqcounts.is_empty() && ctx.opt_descriptor_info.is_some() && has_consensus {
FanoutDoneDisposition::DoneEarly
} else {
FanoutDoneDisposition::NotDone
}
})
};
// Call the fanout
let routing_table = self.routing_table();
let hash_coordinate = opaque_record_key.to_hash_coordinate();
let fanout_call = FanoutCall::new(
&routing_table,
FanoutCallParams {
name: format!("outbound_inspect_value({})", Timestamp::now_increasing()),
hash_coordinate,
node_count,
fanout_tasks,
consensus_count,
consensus_width,
timeout,
},
capability_fanout_peer_info_filter(vec![VEILID_CAPABILITY_DHT]),
call_routine,
check_done,
);
let fanout_result = fanout_call
.run(init_fanout_queue, FanoutQueueMode::Unthrottled)
.await?;
let ctx = context.lock();
let mut subkey_fanout_results = vec![];
for cs in &ctx.seqcounts {
let has_consensus = cs.consensus_nodes.len() >= consensus_count;
let subkey_fanout_result = FanoutResult {
kind: if has_consensus {
FanoutResultKind::Consensus
} else {
fanout_result.kind
},
consensus_nodes: cs.consensus_nodes.clone(),
value_nodes: cs.value_nodes.clone(),
};
subkey_fanout_results.push(subkey_fanout_result);
}
if subkey_fanout_results.len() == 1 {
veilid_log!(self debug "InspectValue Fanout: {:#}\n{:#}", fanout_result, subkey_fanout_results.first().unwrap_or_log());
} else {
veilid_log!(self debug "InspectValue Fanout: {:#}:\n{}", fanout_result, debug_fanout_results(&subkey_fanout_results));
}
let result = OutboundInspectValueResult {
subkey_fanout_results,
inspect_result: InspectResult::new(
self,
requested_subkeys,
"outbound_inspect_value",
ctx.opt_descriptor_info
.as_ref()
.map(|d| d.subkeys.clone())
.unwrap_or_default(),
ctx.seqcounts.iter().map(|cs| cs.seq).collect(),
ctx.opt_descriptor_info
.as_ref()
.map(|d| d.descriptor.clone()),
)?,
};
#[allow(clippy::unnecessary_cast)]
{
if result.inspect_result.subkeys().len() as u64
!= result.subkey_fanout_results.len() as u64
{
veilid_log!(self error "mismatch between subkeys returned and fanout results returned: {}!={}", result.inspect_result.subkeys().len(), result.subkey_fanout_results.len());
apibail_internal!("subkey and fanout list length mismatched");
}
}
Ok(result)
}
/// Handle a received 'Inspect Value' query
#[cfg_attr(
feature = "instrument",
instrument(level = "trace", target = "dht", skip_all, fields(__VEILID_LOG_KEY = self.log_key()))
)]
pub async fn inbound_inspect_value(
&self,
opaque_record_key: &OpaqueRecordKey,
subkeys: ValueSubkeyRangeSet,
want_descriptor: bool,
) -> VeilidAPIResult<NetworkResult<InboundInspectValueResult>> {
let subkeys = if subkeys.is_empty() {
ValueSubkeyRangeSet::full()
} else {
subkeys
};
// See if the subkey we are getting has a last known remote value
let remote_record_store = self.get_remote_record_store()?;
let inspect_result = remote_record_store
.inspect_record(opaque_record_key, &subkeys, want_descriptor)
.await?
.unwrap_or_default();
Ok(NetworkResult::value(InboundInspectValueResult::Success(
inspect_result,
)))
}
}