Apache Mesos
lib_logrotate.hpp
Go to the documentation of this file.
1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements. See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership. The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License. You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 
17 #ifndef __SLAVE_CONTAINER_LOGGER_LIB_LOGROTATE_HPP__
18 #define __SLAVE_CONTAINER_LOGGER_LIB_LOGROTATE_HPP__
19 
20 #include <stdio.h>
21 
24 
25 #include <stout/bytes.hpp>
26 #include <stout/flags.hpp>
27 #include <stout/option.hpp>
28 
29 #include <stout/os/constants.hpp>
30 #include <stout/os/exists.hpp>
31 #include <stout/os/pagesize.hpp>
32 #include <stout/os/shell.hpp>
33 
35 
36 namespace mesos {
37 namespace internal {
38 namespace logger {
39 
40 // Forward declaration.
41 class LogrotateContainerLoggerProcess;
42 
43 
44 // These flags are loaded twice: once when the `ContainerLogger` module
45 // is created and each time before launching containers. The flags loaded
46 // at module creation act as global default values, whereas flags loaded
47 // prior to containers can override the global values.
48 struct LoggerFlags : public virtual flags::FlagsBase
49 {
51  {
53  "max_stdout_size",
54  "Maximum size, in bytes, of a single stdout log file.\n"
55  "Defaults to 10 MB. Must be at least 1 (memory) page.",
56  Megabytes(10),
58 
60  "logrotate_stdout_options",
61  "Additional config options to pass into 'logrotate' for stdout.\n"
62  "This string will be inserted into a 'logrotate' configuration file.\n"
63  "i.e.\n"
64  " /path/to/stdout {\n"
65  " <logrotate_stdout_options>\n"
66  " size <max_stdout_size>\n"
67  " }\n"
68  "NOTE: The 'size' option will be overridden by this module.");
69 
71  "max_stderr_size",
72  "Maximum size, in bytes, of a single stderr log file.\n"
73  "Defaults to 10 MB. Must be at least 1 (memory) page.",
74  Megabytes(10),
76 
78  "logrotate_stderr_options",
79  "Additional config options to pass into 'logrotate' for stderr.\n"
80  "This string will be inserted into a 'logrotate' configuration file.\n"
81  "i.e.\n"
82  " /path/to/stderr {\n"
83  " <logrotate_stderr_options>\n"
84  " size <max_stderr_size>\n"
85  " }\n"
86  "NOTE: The 'size' option will be overridden by this module.");
87  }
88 
89  static Option<Error> validateSize(const Bytes& value)
90  {
91  if (value.bytes() < os::pagesize()) {
92  return Error(
93  "Expected --max_stdout_size and --max_stderr_size of "
94  "at least " + stringify(os::pagesize()) + " bytes");
95  }
96 
97  return None();
98  }
99 
102 
105 };
106 
107 
108 struct Flags : public virtual LoggerFlags
109 {
111  {
113  "environment_variable_prefix",
114  "Prefix for environment variables meant to modify the behavior of\n"
115  "the logrotate logger for the specific container being launched.\n"
116  "The logger will look for four prefixed environment variables in the\n"
117  "container's 'CommandInfo's 'Environment':\n"
118  " * MAX_STDOUT_SIZE\n"
119  " * LOGROTATE_STDOUT_OPTIONS\n"
120  " * MAX_STDERR_SIZE\n"
121  " * LOGROTATE_STDERR_OPTIONS\n"
122  "If present, these variables will overwrite the global values set\n"
123  "via module parameters.",
124  "CONTAINER_LOGGER_");
125 
127  "launcher_dir",
128  "Directory path of Mesos binaries. The logrotate container logger\n"
129  "will find the '" + mesos::internal::logger::rotate::NAME + "'\n"
130  "binary file under this directory.",
131  PKGLIBEXECDIR,
132  [](const std::string& value) -> Option<Error> {
133  std::string executablePath =
135 
136  if (!os::exists(executablePath)) {
137  return Error("Cannot find: " + executablePath);
138  }
139 
140  return None();
141  });
142 
144  "logrotate_path",
145  "If specified, the logrotate container logger will use the specified\n"
146  "'logrotate' instead of the system's 'logrotate'.",
147  "logrotate",
148  [](const std::string& value) -> Option<Error> {
149  // Check if `logrotate` exists via the help command.
150  // TODO(josephw): Consider a more comprehensive check.
151  Try<std::string> helpCommand =
152  os::shell(value + " --help > " + os::DEV_NULL);
153 
154  if (helpCommand.isError()) {
155  return Error(
156  "Failed to check logrotate: " + helpCommand.error());
157  }
158 
159  return None();
160  });
161 
163  "libprocess_num_worker_threads",
164  "Number of Libprocess worker threads.\n"
165  "Defaults to 8. Must be at least 1.",
166  8u,
167  [](const size_t& value) -> Option<Error> {
168  if (value < 1u) {
169  return Error(
170  "Expected --libprocess_num_worker_threads of at least 1");
171  }
172 
173  return None();
174  });
175  }
176 
178 
179  std::string launcher_dir;
180  std::string logrotate_path;
181 
183 };
184 
185 
186 // The `LogrotateContainerLogger` is a container logger that utilizes the
187 // `logrotate` utility to strictly constrain total size of a container's
188 // stdout and stderr log files. All `logrotate` configuration options
189 // (besides `size`, which this module uses) are supported. See `Flags` above.
191 {
192 public:
193  LogrotateContainerLogger(const Flags& _flags);
194 
195  ~LogrotateContainerLogger() override;
196 
197  // This is a noop. The logrotate container logger has nothing to initialize.
198  Try<Nothing> initialize() override;
199 
201  const ContainerID& containerId,
202  const mesos::slave::ContainerConfig& containerConfig) override;
203 
204 protected:
207 };
208 
209 } // namespace logger {
210 } // namespace internal {
211 } // namespace mesos {
212 
213 #endif // __SLAVE_CONTAINER_LOGGER_LIB_LOGROTATE_HPP__
bool exists(const std::string &path)
Definition: exists.hpp:26
Definition: errorbase.hpp:36
static Option< Error > validateSize(const Bytes &value)
Definition: lib_logrotate.hpp:89
Definition: check.hpp:33
std::string logrotate_path
Definition: lib_logrotate.hpp:180
Definition: lib_logrotate.hpp:190
size_t pagesize()
Definition: pagesize.hpp:24
constexpr Bytes Megabytes(uint64_t value)
Definition: bytes.hpp:123
const std::string NAME
Definition: logrotate.hpp:38
std::string join(const std::string &path1, const std::string &path2, const char _separator=os::PATH_SEPARATOR)
Definition: path.hpp:116
size_t libprocess_num_worker_threads
Definition: lib_logrotate.hpp:182
Option< std::string > logrotate_stderr_options
Definition: lib_logrotate.hpp:104
process::Owned< LogrotateContainerLoggerProcess > process
Definition: lib_logrotate.hpp:206
Try< Nothing > initialize(const Flags &flags)
Initialized state for support of systemd functions in this file.
Bytes max_stdout_size
Definition: lib_logrotate.hpp:100
Definition: flags.hpp:44
Definition: agent.hpp:25
static Try error(const E &e)
Definition: try.hpp:43
Try< std::string > shell(const std::string &fmt, const T &...t)
Definition: shell.hpp:49
Definition: none.hpp:27
Definition: attributes.hpp:24
bool isError() const
Definition: try.hpp:78
LoggerFlags()
Definition: lib_logrotate.hpp:50
Flags()
Definition: lib_logrotate.hpp:110
Flags flags
Definition: lib_logrotate.hpp:205
constexpr char DEV_NULL[]
Definition: constants.hpp:30
uint64_t bytes() const
Definition: bytes.hpp:79
Option< std::string > logrotate_stdout_options
Definition: lib_logrotate.hpp:101
void add(T1 Flags::*t1, const Name &name, const Option< Name > &alias, const std::string &help, const T2 *t2, F validate)
Definition: flags.hpp:333
A containerizer component used to manage container logs.
Definition: container_logger.hpp:61
Try< std::string > prepare(const std::string &baseHierarchy, const std::string &subsystem, const std::string &cgroup)
Definition: lib_logrotate.hpp:48
std::string environment_variable_prefix
Definition: lib_logrotate.hpp:177
Definition: bytes.hpp:30
Definition: lib_logrotate.hpp:108
std::string stringify(int flags)
std::string launcher_dir
Definition: lib_logrotate.hpp:179
Bytes max_stderr_size
Definition: lib_logrotate.hpp:103
Definition: future.hpp:58