|
| 1 | +/* |
| 2 | + * Copyright (C) 2020 HERE Europe B.V. |
| 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 | + * License-Filename: LICENSE |
| 18 | + */ |
| 19 | + |
| 20 | +#include "StreamLayerReadExample.h" |
| 21 | + |
| 22 | +#include <future> |
| 23 | +#include <iostream> |
| 24 | + |
| 25 | +#include <olp/authentication/TokenProvider.h> |
| 26 | +#include <olp/core/client/HRN.h> |
| 27 | +#include <olp/core/client/OlpClientSettings.h> |
| 28 | +#include <olp/core/client/OlpClientSettingsFactory.h> |
| 29 | +#include <olp/core/logging/Log.h> |
| 30 | +#include <olp/core/utils/Base64.h> |
| 31 | +#include <olp/dataservice/read/StreamLayerClient.h> |
| 32 | + |
| 33 | +using namespace olp::dataservice::read; |
| 34 | +namespace { |
| 35 | +constexpr auto kLogTag = "read-stream-layer-example"; |
| 36 | +constexpr auto kNumberOfThreads = 2u; |
| 37 | + |
| 38 | +bool CreateSubscription(StreamLayerClient& client, |
| 39 | + SubscribeRequest subscribe_request) { |
| 40 | + auto subscribe_future = client.Subscribe(subscribe_request); |
| 41 | + auto subscribe_response = subscribe_future.GetFuture().get(); |
| 42 | + if (!subscribe_response.IsSuccessful()) { |
| 43 | + OLP_SDK_LOG_ERROR_F( |
| 44 | + kLogTag, "Failed to create subscription - HTTP Status: %d Message: %s", |
| 45 | + subscribe_response.GetError().GetHttpStatusCode(), |
| 46 | + subscribe_response.GetError().GetMessage().c_str()); |
| 47 | + return false; |
| 48 | + } |
| 49 | + return true; |
| 50 | +} |
| 51 | + |
| 52 | +int GetDataFromMessages(StreamLayerClient& client, |
| 53 | + const model::Messages& result) { |
| 54 | + const auto& messages = result.GetMessages(); |
| 55 | + for (const auto& message : messages) { |
| 56 | + // If data is greater than 1 MB, the data handle is present. The data handle |
| 57 | + // is a unique identifier that is used to identify content and retrieve the |
| 58 | + // content using GetData. |
| 59 | + auto handle = message.GetMetaData().GetDataHandle(); |
| 60 | + if (handle) { |
| 61 | + OLP_SDK_LOG_INFO_F(kLogTag, "Message data: handle - %s, size - %lu", |
| 62 | + handle.get().c_str(), |
| 63 | + message.GetMetaData().GetDataSize().get()); |
| 64 | + // use GetData(const model::Message& message) with message instance to |
| 65 | + // request actual data with data handle. |
| 66 | + auto message_future = client.GetData(message); |
| 67 | + auto message_result = message_future.GetFuture().get(); |
| 68 | + if (!message_result.IsSuccessful()) { |
| 69 | + OLP_SDK_LOG_WARNING_F(kLogTag, |
| 70 | + "Failed to get data for data handle %s - HTTP " |
| 71 | + "Status: %d Message: %s", |
| 72 | + handle.get().c_str(), |
| 73 | + message_result.GetError().GetHttpStatusCode(), |
| 74 | + message_result.GetError().GetMessage().c_str()); |
| 75 | + continue; |
| 76 | + } |
| 77 | + auto message_data = message_result.MoveResult(); |
| 78 | + OLP_SDK_LOG_INFO_F(kLogTag, "GetData for %s successful: size - %lu", |
| 79 | + handle.get().c_str(), message_data->size()); |
| 80 | + } else { |
| 81 | + // If data is less than 1 MB, the data content published directly in the |
| 82 | + // metadata and encoded in Base64. |
| 83 | + OLP_SDK_LOG_INFO_F(kLogTag, "Message data: size - %lu", |
| 84 | + message.GetData()->size()); |
| 85 | + } |
| 86 | + } |
| 87 | + return messages.size(); |
| 88 | +} |
| 89 | + |
| 90 | +void RunPoll(StreamLayerClient& client) { |
| 91 | + unsigned int total_messages_size = 0; |
| 92 | + // Get the messages, and commit offsets till all data is consumed, or max |
| 93 | + // times 5 |
| 94 | + unsigned int max_times_to_poll = 5; |
| 95 | + while (max_times_to_poll-- > 0) { |
| 96 | + auto poll_future = client.Poll(); |
| 97 | + auto poll_response = poll_future.GetFuture().get(); |
| 98 | + if (!poll_response.IsSuccessful()) { |
| 99 | + OLP_SDK_LOG_WARNING_F(kLogTag, |
| 100 | + "Failed to poll data - HTTP Status: %d Message: %s", |
| 101 | + poll_response.GetError().GetHttpStatusCode(), |
| 102 | + poll_response.GetError().GetMessage().c_str()); |
| 103 | + continue; |
| 104 | + } |
| 105 | + |
| 106 | + auto result = poll_response.MoveResult(); |
| 107 | + auto messages_size = GetDataFromMessages(client, result); |
| 108 | + total_messages_size += messages_size; |
| 109 | + if (!messages_size) { |
| 110 | + OLP_SDK_LOG_INFO(kLogTag, "No new messages is received"); |
| 111 | + break; |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + if (total_messages_size > 0) { |
| 116 | + OLP_SDK_LOG_INFO_F(kLogTag, "Poll data - Success, messages size - %u", |
| 117 | + total_messages_size); |
| 118 | + } else { |
| 119 | + OLP_SDK_LOG_INFO(kLogTag, "No messages is received"); |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +bool DeleteSubscription(StreamLayerClient& client) { |
| 124 | + auto unsubscribe_future = client.Unsubscribe(); |
| 125 | + auto unsubscribe_response = unsubscribe_future.GetFuture().get(); |
| 126 | + if (!unsubscribe_response.IsSuccessful()) { |
| 127 | + OLP_SDK_LOG_ERROR_F(kLogTag, |
| 128 | + "Failed to unsubscribe - HTTP Status: %d Message: %s", |
| 129 | + unsubscribe_response.GetError().GetHttpStatusCode(), |
| 130 | + unsubscribe_response.GetError().GetMessage().c_str()); |
| 131 | + return false; |
| 132 | + } |
| 133 | + return true; |
| 134 | +} |
| 135 | +} // namespace |
| 136 | + |
| 137 | +int RunStreamLayerExampleRead( |
| 138 | + const AccessKey& access_key, const std::string& catalog, |
| 139 | + const std::string& layer_id, |
| 140 | + SubscribeRequest::SubscriptionMode subscription_mode) { |
| 141 | + // Create a task scheduler instance |
| 142 | + std::shared_ptr<olp::thread::TaskScheduler> task_scheduler = |
| 143 | + olp::client::OlpClientSettingsFactory::CreateDefaultTaskScheduler(); |
| 144 | + // Create a network client |
| 145 | + auto http_client = olp::client::OlpClientSettingsFactory:: |
| 146 | + CreateDefaultNetworkRequestHandler(); |
| 147 | + |
| 148 | + // Initialize authentication settings |
| 149 | + olp::authentication::Settings settings({access_key.id, access_key.secret}); |
| 150 | + settings.task_scheduler = task_scheduler; |
| 151 | + settings.network_request_handler = http_client; |
| 152 | + // Setup AuthenticationSettings with a default token provider that will |
| 153 | + // retrieve an OAuth 2.0 token from OLP. |
| 154 | + olp::client::AuthenticationSettings auth_settings; |
| 155 | + auth_settings.provider = |
| 156 | + olp::authentication::TokenProviderDefault(std::move(settings)); |
| 157 | + |
| 158 | + // Setup OlpClientSettings and provide it to the StreamLayerClient. |
| 159 | + olp::client::OlpClientSettings client_settings; |
| 160 | + client_settings.authentication_settings = auth_settings; |
| 161 | + client_settings.network_request_handler = std::move(http_client); |
| 162 | + client_settings.task_scheduler = task_scheduler; |
| 163 | + |
| 164 | + // Set consumer configuration options. Other options ant its default values |
| 165 | + // described here: |
| 166 | + // https://developer.here.com/olp/documentation/data-api/api-reference-stream.html |
| 167 | + ConsumerOptions expected_options = {{"auto.offset.reset", "earliest"}, |
| 168 | + {"enable.auto.commit", "false"}, |
| 169 | + {"group.id", "group_id_1"}}; |
| 170 | + |
| 171 | + // Create subscription, used kSerial or kParallel subscription mode |
| 172 | + SubscribeRequest subscribe_request = |
| 173 | + SubscribeRequest() |
| 174 | + .WithSubscriptionMode(subscription_mode) |
| 175 | + .WithConsumerProperties(ConsumerProperties(expected_options)); |
| 176 | + |
| 177 | + // value accumulate result |
| 178 | + std::atomic<int> value(0); |
| 179 | + auto read_from_stream_layer = [&]() { |
| 180 | + // Create stream layer client with settings and catalog, layer specified |
| 181 | + StreamLayerClient client(olp::client::HRN{catalog}, layer_id, |
| 182 | + client_settings); |
| 183 | + if (!CreateSubscription(client, subscribe_request)) { |
| 184 | + value.store(-1); |
| 185 | + } |
| 186 | + |
| 187 | + RunPoll(client); |
| 188 | + |
| 189 | + if (!DeleteSubscription(client)) { |
| 190 | + value.store(-1); |
| 191 | + } |
| 192 | + }; |
| 193 | + |
| 194 | + if (subscription_mode == SubscribeRequest::SubscriptionMode::kSerial) { |
| 195 | + // With serial subscription you can read smaller volumes of data with a |
| 196 | + // single subscription. |
| 197 | + read_from_stream_layer(); |
| 198 | + } else { |
| 199 | + // With parallel subscription you can read large volumes of data in a |
| 200 | + // parallel manner. The subscription and message reading workflow is similar |
| 201 | + // to serial subscription except that you are allowed to create multiple |
| 202 | + // subscriptions for the same HRN, layer and group.id using multiple |
| 203 | + // processes/threads. This allows you to read and commit messages for each |
| 204 | + // subscription in parallel. |
| 205 | + OLP_SDK_LOG_INFO_F(kLogTag, |
| 206 | + "Starting parallel subscription mode, threads=%u", |
| 207 | + kNumberOfThreads); |
| 208 | + std::vector<std::thread> threads; |
| 209 | + threads.reserve(kNumberOfThreads); |
| 210 | + for (unsigned int i = 0; i < kNumberOfThreads; i++) { |
| 211 | + threads.emplace_back(read_from_stream_layer); |
| 212 | + } |
| 213 | + |
| 214 | + for (auto& thread : threads) { |
| 215 | + thread.join(); |
| 216 | + } |
| 217 | + } |
| 218 | + return value.load(); |
| 219 | +} |
0 commit comments