Apache Mesos
checker_process.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 __CHECKER_PROCESS_HPP__
18 #define __CHECKER_PROCESS_HPP__
19 
20 #include <memory>
21 #include <string>
22 #include <tuple>
23 #include <vector>
24 
25 #include <mesos/mesos.hpp>
26 
27 #include <process/future.hpp>
28 #include <process/http.hpp>
29 #include <process/protobuf.hpp>
30 
31 #include <stout/duration.hpp>
32 #include <stout/option.hpp>
33 #include <stout/stopwatch.hpp>
34 #include <stout/try.hpp>
35 #include <stout/variant.hpp>
36 
38 #include "checks/checks_types.hpp"
39 
40 namespace mesos {
41 namespace internal {
42 namespace checks {
43 
44 #ifdef __WINDOWS__
45 constexpr char DOCKER_HEALTH_CHECK_IMAGE[] = "mesos/windows-health-check";
46 #endif // __WINDOWS__
47 
48 class CheckerProcess : public ProtobufProcess<CheckerProcess>
49 {
50 public:
51  // TODO(gkleiman): Instead of passing an optional scheme as a parameter,
52  // consider introducing a global `TLSInfo` protobuf and using it in HTTP
53  // checks. See MESOS-7356.
54  //
55  // TODO(qianzhang): Once we figure out how the IPv4/IPv6 should be supported
56  // in the health check API (i.e., the `CheckInfo` protobuf message), we may
57  // consider to remove the ipv6 parameter field which is a temporary solution
58  // for now.
60  const CheckInfo& checkInfo,
61  const std::string& launcherDir,
62  const lambda::function<void(const Try<CheckStatusInfo>&)>& _callback,
63  const TaskID& _taskId,
64  const std::string& _name,
66  const Option<std::string>& scheme,
67  bool ipv6 = false);
68 
69  void pause();
70  void resume();
71 
72  ~CheckerProcess() override {}
73 
74 protected:
75  void initialize() override;
76  void finalize() override;
77 
78 private:
79  void performCheck();
80  void scheduleNext(const Duration& duration);
81  void processCheckResult(
82  const Stopwatch& stopwatch,
83  const Result<CheckStatusInfo>& result);
84 
85  process::Future<int> commandCheck(
86  const check::Command& cmd,
87  const runtime::Plain& plain);
88 
89  process::Future<int> dockerCommandCheck(
90  const check::Command& cmd,
91  const runtime::Docker& docker);
92 
93  process::Future<int> nestedCommandCheck(
94  const check::Command& cmd,
95  const runtime::Nested& nested);
96  void _nestedCommandCheck(
97  std::shared_ptr<process::Promise<int>> promise,
98  check::Command cmd,
99  runtime::Nested nested);
100  void __nestedCommandCheck(
101  std::shared_ptr<process::Promise<int>> promise,
102  process::http::Connection connection,
103  check::Command cmd,
104  runtime::Nested nested);
105  void ___nestedCommandCheck(
106  std::shared_ptr<process::Promise<int>> promise,
107  const ContainerID& checkContainerId,
108  const process::http::Response& launchResponse,
109  runtime::Nested nested);
110 
111  void nestedCommandCheckFailure(
112  std::shared_ptr<process::Promise<int>> promise,
113  process::http::Connection connection,
114  const ContainerID& checkContainerId,
115  std::shared_ptr<bool> checkTimedOut,
116  const std::string& failure,
117  runtime::Nested nested);
118 
119  process::Future<Option<int>> waitNestedContainer(
120  const ContainerID& containerId,
121  runtime::Nested nested);
122  process::Future<Option<int>> _waitNestedContainer(
123  const ContainerID& containerId,
124  const process::http::Response& httpResponse);
125 
126  void processCommandCheckResult(
127  const Stopwatch& stopwatch,
128  const process::Future<int>& future);
129 
130  process::Future<int> httpCheck(
131  const check::Http& http,
132  const Option<runtime::Plain>& plain);
133  process::Future<int> _httpCheck(
134  const std::vector<std::string>& cmdArgv,
135  const Option<runtime::Plain>& plain);
136  process::Future<int> __httpCheck(
137  const std::tuple<process::Future<Option<int>>,
140  void processHttpCheckResult(
141  const Stopwatch& stopwatch,
142  const process::Future<int>& future);
143 
144  // The docker HTTP health check is only performed differently on Windows.
145 #ifdef __WINDOWS__
146  process::Future<int> dockerHttpCheck(
147  const check::Http& http,
148  const runtime::Docker& docker);
149 #endif // __WINDOWS__
150 
151  process::Future<bool> tcpCheck(
152  const check::Tcp& tcp,
153  const Option<runtime::Plain>& plain);
154  process::Future<bool> _tcpCheck(
155  const std::vector<std::string>& cmdArgv,
156  const Option<runtime::Plain>& plain);
157  process::Future<bool> __tcpCheck(
158  const std::tuple<process::Future<Option<int>>,
161  void processTcpCheckResult(
162  const Stopwatch& stopwatch,
163  const process::Future<bool>& future);
164 
165  // The docker TCP health check is only performed differently on Windows.
166 #ifdef __WINDOWS__
167  process::Future<bool> dockerTcpCheck(
168  const check::Tcp& tcp,
169  const runtime::Docker& docker);
170 #endif // __WINDOWS__
171 
172  const lambda::function<void(const Try<CheckStatusInfo>&)> updateCallback;
173  const TaskID taskId;
174  const std::string name;
175  const Variant<runtime::Plain, runtime::Docker, runtime::Nested> runtime;
176 
177  // The `CheckerProcess` constructor parses the given `CheckInfo` struct and
178  // prepares this `Variant`.
180 
181  Duration checkDelay;
182  Duration checkInterval;
183  Duration checkTimeout;
184 
185  bool paused;
186 
187  // Contains the ID of the most recently terminated nested container
188  // that was used to perform a COMMAND check.
189  Option<ContainerID> previousCheckContainerId;
190 };
191 
192 } // namespace checks {
193 } // namespace internal {
194 } // namespace mesos {
195 
196 #endif // __CHECKER_PROCESS_HPP__
Definition: check.hpp:33
Definition: stopwatch.hpp:30
Definition: checks_runtime.hpp:46
Definition: duration.hpp:32
Definition: check.hpp:30
~CheckerProcess() override
Definition: checker_process.hpp:72
CheckerProcess(const CheckInfo &checkInfo, const std::string &launcherDir, const lambda::function< void(const Try< CheckStatusInfo > &)> &_callback, const TaskID &_taskId, const std::string &_name, Variant< runtime::Plain, runtime::Docker, runtime::Nested > _runtime, const Option< std::string > &scheme, bool ipv6=false)
hashmap< std::string, HttpEndpoint > http
Definition: process.hpp:456
Definition: checks_types.hpp:66
Definition: agent.hpp:25
Definition: future.hpp:74
Protocol< PromiseRequest, PromiseResponse > promise
Definition: protobuf.hpp:108
void finalize() override
Invoked when a process is terminated.
Definition: attributes.hpp:24
Definition: checks_types.hpp:48
Definition: http.hpp:612
void initialize() override
Invoked when a process gets spawned.
Definition: checker_process.hpp:48
Definition: checks_runtime.hpp:36
Definition: checks_types.hpp:42
Definition: spec.hpp:35
Definition: checks_runtime.hpp:63
Represents a connection to an HTTP server.
Definition: http.hpp:965
Definition: future.hpp:58