IceFlow NDN-based stream processing library written in C++
Loading...
Searching...
No Matches
consumer.hpp
1/*
2 * Copyright 2024 The IceFlow Authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 * SPDX-License-Identifier: Apache-2.0
17 */
18
19#ifndef ICEFLOW_CONSUMER_HPP
20#define ICEFLOW_CONSUMER_HPP
21
22#include "ndn-svs/svspubsub.hpp"
23
24#include <chrono>
25#include <unordered_map>
26#include <vector>
27
28#include "congestion-reporter.hpp"
29#include "stats.hpp"
30
31namespace iceflow {
32
33typedef std::function<void(std::vector<uint8_t>)> ConsumerCallback;
34
39
40public:
42 std::shared_ptr<ndn::svs::SVSPubSub> svsPubSub,
43 const std::string &syncPrefix, const std::string &upstreamEdgeName,
44 std::optional<std::shared_ptr<CongestionReporter>> congestionReporter);
45
47
48 void setConsumerCallback(ConsumerCallback consumerCallback);
49
50 bool repartition(std::vector<uint32_t> partitions);
51
52 std::vector<u_int32_t> getPartitions();
53
54 EdgeConsumptionStats getConsumptionStats();
55
56private:
57 void validatePartitionConfiguration(uint32_t numberOfPartitions,
58 uint32_t consumerPartitionIndex,
59 uint32_t totalNumberOfConsumers);
60
61 void subscribeCallBack(const ndn::svs::SVSPubSub::SubscriptionData &subData);
62
63 ndn::Name prepareDataName(uint32_t partitionNumber);
64
71 void setTopicPartitions(uint32_t numberOfPartitions,
72 uint32_t consumerPartitionIndex,
73 uint32_t totalNumberOfConsumers);
74
75 void subscribeToTopicPartition(uint64_t topicPartition);
76
77 void unsubscribeFromAllPartitions();
78
79 void saveTimestamp();
80
81 void cleanUpTimestamps(
82 std::chrono::time_point<std::chrono::steady_clock> referenceTimepoint);
83
84 void reportCongestion(CongestionReason congestionReason);
85
86 uint64_t determineIdleTime(
87 std::chrono::time_point<std::chrono::steady_clock> referenceTimepoint);
88
89private:
90 const std::weak_ptr<ndn::svs::SVSPubSub> m_svsPubSub;
91 const std::string m_subTopic;
92
93 std::vector<uint32_t> m_partitions;
94
95 std::vector<uint32_t> m_subscriptionHandles;
96
97 std::deque<std::chrono::time_point<std::chrono::steady_clock>>
98 m_consumptionTimestamps;
99
100 // TODO: Make configurable
101 std::chrono::seconds m_maxConsumptionAge = std::chrono::seconds(1);
102
103 std::optional<ConsumerCallback> m_consumerCallback;
104
105 std::optional<std::shared_ptr<CongestionReporter>> m_congestionReporter;
106
107 const std::string &m_upstreamEdgeName;
108
109 std::chrono::time_point<std::chrono::steady_clock> m_idleSince;
110};
111} // namespace iceflow
112
113#endif // ICEFLOW_CONSUMER_HPP
Definition consumer.hpp:38
Definition stats.hpp:31