-
Notifications
You must be signed in to change notification settings - Fork 901
Expand file tree
/
Copy pathDataReaderHistory.hpp
More file actions
482 lines (427 loc) · 18.2 KB
/
DataReaderHistory.hpp
File metadata and controls
482 lines (427 loc) · 18.2 KB
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
// Copyright 2021 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @file DataReaderHistory.hpp
*/
#ifndef _FASTDDS_SUBSCRIBER_HISTORY_DATAREADERHISTORY_HPP_
#define _FASTDDS_SUBSCRIBER_HISTORY_DATAREADERHISTORY_HPP_
#include <chrono>
#include <functional>
#include <map>
#include <memory>
#include <utility>
#include <fastcdr/cdr/fixed_size_string.hpp>
#include <fastdds/dds/core/policy/QosPolicies.hpp>
#include <fastdds/dds/subscriber/qos/DataReaderQos.hpp>
#include <fastdds/dds/subscriber/SampleInfo.hpp>
#include <fastdds/dds/topic/TopicDataType.hpp>
#include <fastdds/dds/topic/TopicDescription.hpp>
#include <fastdds/dds/topic/TypeSupport.hpp>
#include <fastdds/rtps/attributes/ResourceManagement.hpp>
#include <fastdds/rtps/common/CacheChange.hpp>
#include <fastdds/rtps/common/Guid.hpp>
#include <fastdds/rtps/common/InstanceHandle.hpp>
#include <fastdds/rtps/common/SequenceNumber.hpp>
#include <fastdds/rtps/history/ReaderHistory.hpp>
#include <fastdds/subscriber/DataReaderImpl/StateFilter.hpp>
#include <fastdds/utils/collections/ResourceLimitedContainerConfig.hpp>
#include "DataReaderHistoryCounters.hpp"
#include "DataReaderInstance.hpp"
namespace eprosima {
namespace fastdds {
namespace dds {
namespace detail {
/**
* Class DataReaderHistory, container of the different CacheChanges of a DataReader
*/
class DataReaderHistory : public eprosima::fastdds::rtps::ReaderHistory
{
public:
using MemoryManagementPolicy_t = eprosima::fastdds::rtps::MemoryManagementPolicy_t;
using InstanceHandle_t = eprosima::fastdds::rtps::InstanceHandle_t;
using CacheChange_t = eprosima::fastdds::rtps::CacheChange_t;
using GUID_t = eprosima::fastdds::rtps::GUID_t;
using SequenceNumber_t = eprosima::fastdds::rtps::SequenceNumber_t;
using InstanceCollection = std::map<InstanceHandle_t, std::shared_ptr<DataReaderInstance>>;
using instance_info = InstanceCollection::iterator;
/**
* Constructor.
* Requires information about the DataReader.
*
* @param type Type information. Needed to know if the type is keyed, as long as the maximum serialized size.
* @param topic Topic description. Topic and type name are used on debug messages.
* @param qos DataReaderQoS policy. History related limits are taken from here.
*/
DataReaderHistory(
const TypeSupport& type,
const TopicDescription& topic,
const DataReaderQos& qos);
~DataReaderHistory() override;
/**
* Remove a specific change from the history.
* No Thread Safe.
*
* @param removal iterator to the CacheChange_t to remove.
* @param release defaults to true and hints if the CacheChange_t should return to the pool
*
* @return iterator to the next CacheChange_t or end iterator.
*/
iterator remove_change_nts(
const_iterator removal,
bool release = true) override;
/**
* Remove a specific change from the history.
* No Thread Safe.
*
* @param removal iterator to the CacheChange_t to remove.
* @param [in] max_blocking_time Maximum time this method has to complete the task.
* @param release defaults to true and hints if the CacheChange_t should return to the pool
*
* @return iterator to the next CacheChange_t or end iterator.
*/
iterator remove_change_nts(
const_iterator removal,
const std::chrono::time_point<std::chrono::steady_clock>& max_blocking_time,
bool release = true) override;
/**
* Check if a new change can be added to this history.
*
* @param [in] writer_guid GUID of the writer where the change came from.
* @param [in] total_payload_size Total payload size of the incoming change.
* @param [in] unknown_missing_changes_up_to The number of changes from the same writer with a lower sequence
* number that could potentially be received in the future.
* @param [out] will_never_be_accepted When the method returns @c false, this parameter will inform
* whether the change could be accepted in the future or not.
*
* @pre change should not be present in the history
*
* @return Whether a call to received_change will succeed when called with the same arguments.
*/
bool can_change_be_added_nts(
const GUID_t& writer_guid,
uint32_t total_payload_size,
size_t unknown_missing_changes_up_to,
bool& will_never_be_accepted) const override;
/**
* Called when a change is received by the RTPS reader.
* Will add the change to the history.
*
* @pre Change should not be already present in the history.
*
* @param [in] change The received change
* @param unknown_missing_changes_up_to Number of missing changes before this one
*
* @return Whether the operation succeeded.
*/
bool received_change(
CacheChange_t* change,
size_t unknown_missing_changes_up_to) override;
/**
* Called when a change is received by the RTPS reader.
* Will add the change to the history.
*
* @pre Change should not be already present in the history.
*
* @param [in] change The received change
* @param [in] unknown_missing_changes_up_to Number of missing changes before this one
* @param [out] rejection_reason In case of been rejected the sample, it will contain the reason of the rejection.
*
* @return Whether the operation succeeded.
*/
bool received_change(
CacheChange_t* change,
size_t unknown_missing_changes_up_to,
SampleRejectedStatusKind& rejection_reason) override;
/**
* Called when a fragmented change is received completely by the RTPS reader.
* Will find its instance and store it.
*
* @pre Change should be already present in the history.
*
* @param [in] change The received change
*
* @return Whether the operation succeeded.
*/
bool completed_change(
CacheChange_t* change) override;
/**
* Called when a fragmented change is received completely by the RTPS reader.
* Will find its instance and store it.
*
* @pre Change should be already present in the history.
*
* @param [in] change The received change
* @param [in] unknown_missing_changes_up_to Number of missing changes before this one
* @param [out] rejection_reason In case of been rejected the sample, it will contain the reason of the rejection.
*
* @return Whether the operation succeeded.
*/
bool completed_change(
CacheChange_t* change,
size_t unknown_missing_changes_up_to,
SampleRejectedStatusKind& rejection_reason) override;
/**
* @brief Returns information about the first untaken sample.
*
* @param [out] info SampleInfo structure to store first untaken sample information.
*
* @return true if sample info was returned, false if there is no sample to take.
*/
bool get_first_untaken_info(
SampleInfo& info);
/**
* This method is called to remove a change from the DataReaderHistory.
*
* @param change Pointer to the CacheChange_t.
*
* @return True if removed.
*/
bool remove_change_sub(
CacheChange_t* change);
/**
* This method is called to remove a change from the DataReaderHistory.
*
* @param [in] change Pointer to the CacheChange_t.
* @param [in,out] it Iterator pointing to change on input. Will point to next valid change on output.
*
* @return True if removed.
*/
bool remove_change_sub(
CacheChange_t* change,
DataReaderInstance::ChangeCollection::iterator& it);
/**
* Called when a writer is unmatched from the reader holding this history.
*
* This method will remove all the changes on the history that came from the writer being unmatched and which have
* not yet been notified to the user.
*
* @param writer_guid GUID of the writer being unmatched.
* @param last_notified_seq Last sequence number from the specified writer that was notified to the user.
*/
void writer_unmatched(
const GUID_t& writer_guid,
const SequenceNumber_t& last_notified_seq) override;
/**
* @brief A method to set the next deadline for the given instance.
*
* @param handle The handle to the instance
* @param next_deadline_us The time point when the deadline will occur
* @param [in] deadline_missed true value when is called because the deadline was missed.
*
* @return True if the deadline was set correctly
*/
bool set_next_deadline(
const InstanceHandle_t& handle,
const std::chrono::steady_clock::time_point& next_deadline_us,
bool deadline_missed = false);
/**
* @brief A method to get the next instance handle that will miss the deadline and the time when the deadline will occur.
*
* @param handle The handle to the instance
* @param next_deadline_us The time point when the instance will miss the deadline
*
* @return True if the deadline was retrieved successfully
*/
bool get_next_deadline(
InstanceHandle_t& handle,
std::chrono::steady_clock::time_point& next_deadline_us);
/**
* Get the number of samples pending to be read.
*
* @param mark_as_read Whether the unread samples should be marked as read or not.
*
* @return the number of samples on the reader history that have never been read.
*/
uint64_t get_unread_count(
bool mark_as_read);
/**
* @brief Check whether an instance handle is present in the history.
*
* @param handle The handle of the instance to check.
*
* @return true when the topic has keys and the handle corresponds to an instance present in the history.
* @return false otherwise.
*/
bool is_instance_present(
const InstanceHandle_t& handle) const;
/**
* @brief Get an iterator to an instance with available data.
*
* @param handle The handle to the instance.
* @param exact Indicates if the handle should match exactly (true) or if the first instance greater than the
* input handle should be returned.
*
* @return A pair where:
* - @c first is a boolean indicating if an instance was found
* - @c second is an iterator to the data available instances collection
*
* @remarks When used on a NO_KEY topic, an instance will only be returned when called with
* `handle = HANDLE_NIL` and `exact = false`.
*/
std::pair<bool, instance_info> lookup_available_instance(
const InstanceHandle_t& handle,
bool exact);
/**
* @brief Given an instance advance the iterator to the next instance with available data.
*
* @param handle The handle of the instance returned by a previous call to lookup_available_instance or
* next_available_instance_nts.
* @param current_info The iterator to be advanced.
*
* @return A pair where:
* - @c first is a boolean indicating if another instance with available data is present
* - @c second is an iterator pointing to the next instance with available data
*/
std::pair<bool, instance_info> next_available_instance_nts(
const InstanceHandle_t& handle,
const instance_info& current_info);
/**
* This method is meant to be called just before calling @c end_sample_access_nts on the RTPS reader.
* It will update the internal counters of unread and read samples.
*
* @param change Pointer to the cache change that has been processed.
* @param is_going_to_be_mark_as_read Whether the change is going to be marked as read.
*/
void change_was_processed_nts(
CacheChange_t* const change,
bool is_going_to_be_mark_as_read);
/**
* Mark that a DataReaderInstance has been viewed.
*
* @param instance Instance on which the view state should be modified.
*/
void instance_viewed_nts(
const InstanceCollection::mapped_type& instance);
/*!
* @brief Updates instance's information and also decides whether the sample is finally accepted or denied depending
* on the Ownership strength.
*
* @param [in] change Sample received by DataReader.
* @return true is returned when the sample is accepted and false when the sample is denied.
*/
bool update_instance_nts(
CacheChange_t* const change);
/*!
* @brief Inform the history that a writer should be considered as not alive.
*
* @param [in] writer_guid GUID of the writer that should be considered as not alive.
*
* @return true if a state notification sample was added to at least one instance.
* This would mean that DATA_AVAILABLE status (and listener) shall be notified.
*/
bool writer_not_alive(
const fastdds::rtps::GUID_t& writer_guid);
void check_and_remove_instance(
instance_info& instance_info);
StateFilter get_mask_status() const noexcept;
/*!
* @brief This function should be called by reader if a writer updates its ownership strength.
*
* @param [in] writer_guid Guid of the writer which changes its ownership strength.
* @param [out] ownership_strength New value of the writer's Ownership strength.
*/
void writer_update_its_ownership_strength_nts(
const GUID_t& writer_guid,
const uint32_t ownership_strength) override;
private:
//!Resource limits for allocating the array of changes per instance
eprosima::fastdds::ResourceLimitedContainerConfig key_changes_allocation_;
//!Resource limits for allocating the array of alive writers per instance
eprosima::fastdds::ResourceLimitedContainerConfig key_writers_allocation_;
//!Collection of DataReaderInstance objects accessible by their handle
InstanceCollection instances_;
//!Collection of DataReaderInstance objects with available data, accessible by their handle
InstanceCollection data_available_instances_;
//!HistoryQosPolicy values.
HistoryQosPolicy history_qos_;
//!ResourceLimitsQosPolicy values.
ResourceLimitsQosPolicy resource_limited_qos_;
//!Topic name
fastcdr::string_255 topic_name_;
//!Type name
fastcdr::string_255 type_name_;
//!Whether the type has keys
bool has_keys_;
//!TopicDataType
fastdds::dds::TopicDataType* type_;
/// Function to compute the instance handle of a received change
std::function<bool(CacheChange_t*)> compute_key_for_change_fn_;
/// Function processing a received change
std::function<bool(CacheChange_t*, size_t, SampleRejectedStatusKind&)> receive_fn_;
/// Function processing a completed fragmented change
std::function<bool(CacheChange_t*, DataReaderInstance&, size_t, SampleRejectedStatusKind&)> complete_fn_;
/// Book-keeping counters for ReadCondition support
DataReaderHistoryCounters counters_;
/**
* @brief Method that finds a key in m_keyedChanges or tries to add it if not found
* @param a_change The change to get the key from
* @param map_it A map iterator to the given key
* @return True if it was found or could be added to the map
*/
bool find_key(
const InstanceHandle_t& handle,
InstanceCollection::iterator& map_it);
/**
* @name Variants of incoming change processing.
* Will be called with the history mutex taken.
* @param [in] change The received change
* @param unknown_missing_changes_up_to Number of missing changes before this one
* @return
*/
///@{
bool received_change_keep_all(
CacheChange_t* change,
size_t unknown_missing_changes_up_to,
SampleRejectedStatusKind& rejection_reason);
bool received_change_keep_last(
CacheChange_t* change,
size_t unknown_missing_changes_up_to,
SampleRejectedStatusKind& rejection_reason);
///@}
/**
* @name Variants of change reconstruction completion processing.
* Will be called with the history mutex taken.
* @param change The change for which the last missing fragment has been processed.
* @param instance Instance where the change should be added.
* @return true when the change was added to the instance.
* @return false when the change could not be added to the instance and has been removed from the history.
*/
///@{
bool completed_change_keep_all(
CacheChange_t* change,
DataReaderInstance& instance,
size_t unknown_missing_changes_up_to,
SampleRejectedStatusKind&);
bool completed_change_keep_last(
CacheChange_t* change,
DataReaderInstance& instance,
size_t unknown_missing_changes_up_to,
SampleRejectedStatusKind&);
///@}
bool add_received_change_with_key(
CacheChange_t* a_change,
DataReaderInstance& instance,
SampleRejectedStatusKind& rejection_reason);
bool add_to_reader_history_if_not_full(
CacheChange_t* a_change,
SampleRejectedStatusKind& rejection_reason);
void add_to_instance(
CacheChange_t* a_change,
DataReaderInstance& instance);
};
} // namespace detail
} // namespace dds
} // namespace fastdds
} // namespace eprosima
#endif // _FASTDDS_SUBSCRIBER_HISTORY_DATAREADERHISTORY_HPP_