log_file_manager.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
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  * A copy of the License is located at
7  *
8  * http://aws.amazon.com/apache2.0
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 
16 
17 
18 #include <fstream>
19 #include <iostream>
20 #include <memory>
21 #include <mutex>
22 #include <queue>
23 #include <tuple>
24 
27 #include <aws/core/utils/json/JsonSerializer.h>
28 #include <aws/core/utils/logging/LogMacros.h>
30 
31 namespace Aws {
32 namespace CloudWatchLogs {
33 namespace Utils {
34 
35 
36 FileObject<LogCollection> LogFileManager::readBatch(
37  size_t batch_size)
38 {
39  FileManagement::DataToken data_token;
40  AWS_LOG_INFO(__func__, "Reading Logbatch");
41 
42  std::priority_queue<std::tuple<Timestamp, std::string, FileManagement::DataToken>> pq;
43  for (size_t i = 0; i < batch_size; ++i) {
44  std::string line;
45  if (!file_manager_strategy_->isDataAvailable()) {
46  break;
47  }
48  data_token = read(line);
49  Aws::String aws_line(line.c_str());
50  Aws::Utils::Json::JsonValue value(aws_line);
51  Aws::CloudWatchLogs::Model::InputLogEvent input_event(value);
52  pq.push(std::make_tuple(input_event.GetTimestamp(), line, data_token));
53  }
54 
55  latestTime = std::get<0>(pq.top());
56  LogCollection log_data;
57  std::list<FileManagement::DataToken> data_tokens;
58  while(!pq.empty()){
59  Timestamp curTime = std::get<0>(pq.top());
60  std::string line = std::get<1>(pq.top());
61  FileManagement::DataToken new_data_token = std::get<2>(pq.top());
62  if(latestTime - curTime < ONE_DAY_IN_MILLISEC){
63  Aws::String aws_line(line.c_str());
64  Aws::Utils::Json::JsonValue value(aws_line);
65  Aws::CloudWatchLogs::Model::InputLogEvent input_event(value);
66  log_data.push_front(input_event);
67  data_tokens.push_back(new_data_token);
68  }
69  else if(file_manager_strategy_->isDeleteStaleData() && latestTime - curTime > TWO_WEEK_IN_MILLISEC){
70  {
71  std::lock_guard<std::mutex> lock(active_delete_stale_data_mutex_);
72  stale_data_.push_back(new_data_token);
73  }
74  }
75  pq.pop();
76  }
77 
78  if(batch_size != log_data.size()){
79  AWS_LOG_WARN(__func__, "%d logs were not batched since the time"
80  " difference was > 24 hours. Will try again in a separate batch."
81  , batch_size - log_data.size()
82  );
83  }
84 
85  FileObject<LogCollection> file_object;
86  file_object.batch_data = log_data;
87  file_object.batch_size = log_data.size();
88  file_object.data_tokens = data_tokens;
89  return file_object;
90 }
91 
93  for (const Model::InputLogEvent &log: data) {
94  auto aws_str = log.Jsonize().View().WriteCompact();
95  std::string str(aws_str.c_str());
96  file_manager_strategy_->write(str);
97  }
98  if (FileManager::file_status_monitor_) {
99  AWS_LOG_INFO(__func__,
100  "Set file status available");
101  FileManager::file_status_monitor_->setStatus(Aws::DataFlow::Status::AVAILABLE);
102  }
103 }
104 
105 } // namespace Utils
106 } // namespace CloudWatchLogs
107 } // namespace Aws
void write(const LogCollection &data) override
const long TWO_WEEK_IN_MILLISEC
std::list< LogType > LogCollection
Definition: definitions.h:29
FileObject< LogCollection > readBatch(size_t batch_size) override
const long ONE_DAY_IN_MILLISEC


cloudwatch_logs_common
Author(s): AWS RoboMaker
autogenerated on Fri May 7 2021 02:18:24