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