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
// OPCUA for Rust
// SPDX-License-Identifier: MPL-2.0
// Copyright (C) 2017-2024 Adam Lock
use std::{
collections::{BTreeMap, VecDeque},
time::Duration,
};
use crate::types::{
service_types::{NotificationMessage, PublishRequest, PublishResponse, ServiceFault},
status_code::StatusCode,
*,
};
use crate::server::{
address_space::types::AddressSpace,
subscriptions::{
subscription::{Subscription, TickReason},
PublishRequestEntry, PublishResponseEntry,
},
};
/// The `Subscriptions` manages zero or more subscriptions, pairing publish requests coming from
/// the client with notifications coming from the subscriptions. Therefore the subscriptions has
/// an incoming queue of publish requests and an outgoing queue of publish responses. The transport
/// layer adds to the one and removes from the other.
///
/// Subscriptions are processed inside `tick()` which is called periodically from a timer. Each
/// tick produces notifications which are ready to publish via a transmission queue. Once a
/// notification is published, it is held in a retransmission queue until it is acknowledged by the
/// client, or purged.
pub(crate) struct Subscriptions {
/// The publish request queue (requests by the client on the session)
publish_request_queue: VecDeque<PublishRequestEntry>,
/// The publish response queue arranged oldest to latest
publish_response_queue: VecDeque<PublishResponseEntry>,
// Timeout period for requests in ms
publish_request_timeout: i64,
/// Subscriptions associated with the session
subscriptions: BTreeMap<u32, Subscription>,
// Notifications waiting to be sent - Value is subscription id and notification message.
transmission_queue: VecDeque<(u32, PublishRequestEntry, NotificationMessage)>,
// Notifications that have been sent but have yet to be acknowledged (retransmission queue).
// Key is (subscription_id, sequence_number). Value is notification message.
retransmission_queue: BTreeMap<(u32, u32), NotificationMessage>,
}
#[derive(Serialize)]
pub struct Metrics {
pub subscriptions: Vec<Subscription>,
pub publish_request_queue_len: usize,
pub publish_response_queue_len: usize,
pub transmission_queue_len: usize,
pub retransmission_queue_len: usize,
}
impl Subscriptions {
pub fn new(max_subscriptions: usize, publish_request_timeout: i64) -> Subscriptions {
let max_publish_requests = if max_subscriptions > 0 {
2 * max_subscriptions
} else {
100
};
Subscriptions {
publish_request_queue: VecDeque::with_capacity(max_publish_requests),
publish_response_queue: VecDeque::with_capacity(max_publish_requests),
publish_request_timeout,
subscriptions: BTreeMap::new(),
transmission_queue: VecDeque::with_capacity(max_publish_requests),
retransmission_queue: BTreeMap::new(),
}
}
pub(crate) fn metrics(&self) -> Metrics {
// Subscriptions
let subscriptions = self
.subscriptions()
.iter()
.map(|subscription_pair| {
let mut subscription = subscription_pair.1.clone();
subscription.set_diagnostics_on_drop(false);
subscription
})
.collect();
Metrics {
subscriptions,
publish_request_queue_len: self.publish_request_queue.len(),
publish_response_queue_len: self.publish_response_queue.len(),
transmission_queue_len: self.transmission_queue.len(),
retransmission_queue_len: self.retransmission_queue.len(),
}
}
#[cfg(test)]
pub(crate) fn publish_request_queue(&mut self) -> &mut VecDeque<PublishRequestEntry> {
&mut self.publish_request_queue
}
#[cfg(test)]
pub(crate) fn publish_response_queue(&mut self) -> &mut VecDeque<PublishResponseEntry> {
&mut self.publish_response_queue
}
#[cfg(test)]
pub(crate) fn retransmission_queue(
&mut self,
) -> &mut BTreeMap<(u32, u32), NotificationMessage> {
&mut self.retransmission_queue
}
/// Takes the publish responses which are queued for the client and returns them to the caller,
/// or returns None if there are none to process.
pub fn take_publish_responses(&mut self) -> Option<VecDeque<PublishResponseEntry>> {
if self.publish_response_queue.is_empty() {
None
} else {
// Take the publish responses from the subscriptions
let mut publish_responses = VecDeque::with_capacity(self.publish_response_queue.len());
publish_responses.append(&mut self.publish_response_queue);
Some(publish_responses)
}
}
/// Returns the number of maxmimum publish requests allowable for the current number of subscriptions
pub fn max_publish_requests(&self) -> usize {
// Allow for two requests per subscription
self.subscriptions.len() * 2
}
/// Places a new publish request onto the queue of publish requests.
///
/// If the queue is full this call will pop the oldest and generate a service fault
/// for that before pushing the new one.
pub(crate) fn enqueue_publish_request(
&mut self,
now: &DateTimeUtc,
request_id: u32,
request: PublishRequest,
address_space: &AddressSpace,
) -> Result<(), StatusCode> {
// Check if we have too requests waiting already
let max_publish_requests = self.max_publish_requests();
if self.publish_request_queue.len() >= max_publish_requests {
// Tick to trigger publish, maybe remove a request to make space for new one
let _ = self.tick(now, address_space, TickReason::ReceivePublishRequest);
}
// Enqueue request or return error
if self.publish_request_queue.len() >= max_publish_requests {
error!(
"Too many publish requests {} for capacity {}",
self.publish_request_queue.len(),
max_publish_requests
);
Err(StatusCode::BadTooManyPublishRequests)
} else {
// Add to the front of the queue - older items are popped from the back
let results = self.process_subscription_acknowledgements(&request);
self.publish_request_queue.push_front(PublishRequestEntry {
request_id,
request,
results,
});
// Tick to trigger publish
self.tick(now, address_space, TickReason::ReceivePublishRequest)
}
}
/// Tests if there are no subscriptions/
pub fn is_empty(&self) -> bool {
self.subscriptions.is_empty()
}
/// Returns the length of subscriptions.
pub fn len(&self) -> usize {
self.subscriptions.len()
}
/// Returns a reference to the collection holding the subscriptions.
pub fn subscriptions(&self) -> &BTreeMap<u32, Subscription> {
&self.subscriptions
}
/// Tests if the subscriptions contain the supplied subscription id.
pub fn contains(&self, subscription_id: u32) -> bool {
self.subscriptions.contains_key(&subscription_id)
}
pub fn insert(&mut self, subscription_id: u32, subscription: Subscription) {
self.subscriptions.insert(subscription_id, subscription);
}
pub fn remove(&mut self, subscription_id: u32) -> Option<Subscription> {
self.subscriptions.remove(&subscription_id)
}
pub fn get_mut(&mut self, subscription_id: u32) -> Option<&mut Subscription> {
self.subscriptions.get_mut(&subscription_id)
}
/// The tick causes the subscription manager to iterate through individual subscriptions calling tick
/// on each in order of priority. In each case this could generate data change notifications. Data change
/// notifications will be attached to the next available publish response and queued for sending
/// to the client.
pub(crate) fn tick(
&mut self,
now: &DateTimeUtc,
address_space: &AddressSpace,
tick_reason: TickReason,
) -> Result<(), StatusCode> {
let subscription_ids = {
// Sort subscriptions by priority
let mut subscription_priority: Vec<(u32, u8)> = self
.subscriptions
.values()
.map(|v| (v.subscription_id(), v.priority()))
.collect();
subscription_priority.sort_by(|s1, s2| s1.1.cmp(&s2.1));
subscription_priority
.iter()
.map(|s| s.0)
.collect::<Vec<u32>>()
};
// Iterate through all subscriptions. If there is a publish request it will be used to
// acknowledge notifications and the response to return new notifications.
// Now tick over the subscriptions
for subscription_id in subscription_ids {
let publishing_req_queued = !self.publish_request_queue.is_empty();
let subscription = self.subscriptions.get_mut(&subscription_id).unwrap();
// Now tick the subscription to see if it has any notifications. If there are
// notifications then the publish response will be associated with his subscription
// and ready to go.
subscription.tick(now, address_space, tick_reason, publishing_req_queued);
// Process any notifications
loop {
if !self.publish_request_queue.is_empty() {
if let Some(notification_message) = subscription.take_notification() {
let publish_request = self.publish_request_queue.pop_back().unwrap();
// Consume the publish request and queue the notification onto the transmission queue
self.transmission_queue.push_front((
subscription_id,
publish_request,
notification_message,
));
} else {
break;
}
} else {
break;
}
}
// Remove the subscription if it is done
if subscription.ready_to_remove() {
self.subscriptions.remove(&subscription_id);
}
}
// Iterate through notifications from oldest to latest in the transmission making publish
// responses.
while !self.transmission_queue.is_empty() {
// Get the oldest notification to send
let (subscription_id, publish_request, notification_message) =
self.transmission_queue.pop_back().unwrap();
// Search the transmission queue for more notifications from this same subscription
let more_notifications = self.more_notifications(subscription_id);
// Get a list of available sequence numbers
let available_sequence_numbers = self.available_sequence_numbers(subscription_id);
// The notification to be sent is now put into the retransmission queue
self.retransmission_queue.insert(
(subscription_id, notification_message.sequence_number),
notification_message.clone(),
);
// Enqueue a publish response
let response = self.make_publish_response(
publish_request,
subscription_id,
now,
notification_message,
more_notifications,
available_sequence_numbers,
);
self.publish_response_queue.push_back(response);
}
// Clean up the retransmission queue
self.remove_old_unacknowledged_notifications();
Ok(())
}
/// Iterates through the existing queued publish requests and creates a timeout
/// publish response any that have expired.
pub fn expire_stale_publish_requests(&mut self, now: &DateTimeUtc) {
if self.publish_request_queue.is_empty() {
return;
}
// Remove publish requests that have expired
let publish_request_timeout = self.publish_request_timeout;
// Create timeout responses for each expired publish request
let mut expired_publish_responses =
VecDeque::with_capacity(self.publish_request_queue.len());
self.publish_request_queue.retain(|request| {
let request_header = &request.request.request_header;
let request_timestamp: DateTimeUtc = request_header.timestamp.into();
let publish_request_timeout = Duration::from_millis(if request_header.timeout_hint > 0 && (request_header.timeout_hint as i64) < publish_request_timeout {
request_header.timeout_hint as u64
} else {
publish_request_timeout as u64
});
// The request has timed out if the timestamp plus hint exceeds the input time
// TODO unwrap logic needs to change
let signed_duration_since: Duration = now.signed_duration_since(request_timestamp).to_std().unwrap();
if signed_duration_since > publish_request_timeout {
debug!("Publish request {} has expired - timestamp = {:?}, expiration hint = {}, publish timeout = {:?}, time now = {:?}, ", request_header.request_handle, request_timestamp, request_timestamp, publish_request_timeout, now);
expired_publish_responses.push_front(PublishResponseEntry {
request_id: request.request_id,
response: ServiceFault {
response_header: ResponseHeader::new_timestamped_service_result(DateTime::now(), &request.request.request_header, StatusCode::BadTimeout),
}.into(),
});
false
} else {
true
}
});
// Queue responses for each expired request
self.publish_response_queue
.append(&mut expired_publish_responses);
}
/// Deletes the acknowledged notifications, returning a list of status code for each according
/// to whether it was found or not.
///
/// Good - deleted notification
/// BadSubscriptionIdInvalid - Subscription doesn't exist
/// BadSequenceNumberUnknown - Sequence number doesn't exist
///
fn process_subscription_acknowledgements(
&mut self,
request: &PublishRequest,
) -> Option<Vec<StatusCode>> {
trace!("Processing subscription acknowledgements");
if let Some(ref subscription_acknowledgements) = request.subscription_acknowledgements {
let results = subscription_acknowledgements.iter()
.map(|subscription_acknowledgement| {
let subscription_id = subscription_acknowledgement.subscription_id;
let sequence_number = subscription_acknowledgement.sequence_number;
// Check the subscription id exists
if self.subscriptions.contains_key(&subscription_id) {
// Clear notification by its sequence number
if self.retransmission_queue.remove(&(subscription_id, sequence_number)).is_some() {
trace!("Removing subscription {} sequence number {} from retransmission queue", subscription_id, sequence_number);
StatusCode::Good
} else {
error!("Cannot find acknowledged notification with sequence number {}", sequence_number);
StatusCode::BadSequenceNumberUnknown
}
} else {
error!("Cannot find acknowledged notification subscription id {}", subscription_id);
StatusCode::BadSubscriptionIdInvalid
}
})
.collect();
Some(results)
} else {
None
}
}
/// Searches the transmission queue to see if there are more notifications for the specified
/// subscription id
fn more_notifications(&self, subscription_id: u32) -> bool {
// At least one match means more notifications
self.transmission_queue
.iter()
.any(|v| v.0 == subscription_id)
}
/// Returns the array of available sequence numbers in the retransmission queue for the specified subscription
fn available_sequence_numbers(&self, subscription_id: u32) -> Option<Vec<u32>> {
if self.retransmission_queue.is_empty() {
None
} else {
// Find the notifications matching this subscription id in the retransmission queue
let sequence_numbers: Vec<u32> = self
.retransmission_queue
.iter()
.filter(|&(k, _)| k.0 == subscription_id)
.map(|(k, _)| k.1)
.collect();
if sequence_numbers.is_empty() {
None
} else {
Some(sequence_numbers)
}
}
}
fn make_publish_response(
&self,
publish_request: PublishRequestEntry,
subscription_id: u32,
now: &DateTimeUtc,
notification_message: NotificationMessage,
more_notifications: bool,
available_sequence_numbers: Option<Vec<u32>>,
) -> PublishResponseEntry {
let now = DateTime::from(*now);
PublishResponseEntry {
request_id: publish_request.request_id,
response: PublishResponse {
response_header: ResponseHeader::new_timestamped_service_result(
now,
&publish_request.request.request_header,
StatusCode::Good,
),
subscription_id,
available_sequence_numbers,
more_notifications,
notification_message,
results: publish_request.results,
diagnostic_infos: None,
}
.into(),
}
}
/// Finds a notification message in the retransmission queue matching the supplied subscription id
/// and sequence number. Returns `BadSubscriptionIdInvalid` or `BadMessageNotAvailable` if a matching
/// notification is not found.
pub fn find_notification_message(
&self,
subscription_id: u32,
sequence_number: u32,
) -> Result<NotificationMessage, StatusCode> {
// Look for the subscription
if self.subscriptions.get(&subscription_id).is_some() {
// Look for the sequence number
if let Some(notification_message) = self
.retransmission_queue
.get(&(subscription_id, sequence_number))
{
Ok((*notification_message).clone())
} else {
Err(StatusCode::BadMessageNotAvailable)
}
} else {
Err(StatusCode::BadSubscriptionIdInvalid)
}
}
fn remove_notifications(&mut self, sequence_nrs_to_remove: &[(u32, u32)]) {
sequence_nrs_to_remove.iter().for_each(|n| {
trace!(
"Removing notification for subscription {}, sequence nr {}",
n.0,
n.1
);
let _ = self.retransmission_queue.remove(n);
});
}
/// Purges notifications waiting for acknowledgement if they are stale or the max permissible
/// is exceeded.
fn remove_old_unacknowledged_notifications(&mut self) {
// Strip out notifications for subscriptions that no longer exist
let sequence_nrs_to_remove = self
.retransmission_queue
.iter()
.filter(|(k, _)| !self.subscriptions.contains_key(&k.0))
.map(|(k, _)| *k)
.collect::<Vec<_>>();
self.remove_notifications(&sequence_nrs_to_remove);
// Compare number of items in retransmission queue to max permissible and remove the older
// notifications.
let max_retransmission_queue = self.max_publish_requests() * 2;
if self.retransmission_queue.len() > max_retransmission_queue {
let remove_count = self.retransmission_queue.len() - max_retransmission_queue;
let sequence_nrs_to_remove = self
.retransmission_queue
.iter()
.take(remove_count)
.map(|(k, _)| *k)
.collect::<Vec<_>>();
self.remove_notifications(&sequence_nrs_to_remove);
}
}
}