1
#pragma once
2

            
3
#include <functional>
4
#include <memory>
5

            
6
#include "envoy/data/core/v3/health_check_event.pb.h"
7
#include "envoy/upstream/upstream.h"
8

            
9
namespace Envoy {
10
namespace Upstream {
11

            
12
enum class HealthState { Unhealthy, Healthy };
13

            
14
enum class HealthTransition {
15
  /**
16
   * Used when the health state of a host hasn't changed.
17
   */
18
  Unchanged,
19
  /**
20
   * Used when the health state of a host has changed.
21
   */
22
  Changed,
23
  /**
24
   * Used when the health check result differs from the health state of a host, but a change to the
25
   * latter is delayed due to healthy/unhealthy threshold settings.
26
   */
27
  ChangePending
28
};
29

            
30
/**
31
 * Wraps active health checking of an upstream cluster.
32
 */
33
class HealthChecker {
34
public:
35
401
  virtual ~HealthChecker() = default;
36

            
37
  /**
38
   * Called when a host has been health checked.
39
   * @param host supplies the host that was just health checked.
40
   * @param changed_state supplies whether the health check resulted in a host moving from healthy
41
   *                       to not healthy or vice versa.
42
   * @param state result of the current check.
43
   */
44
  using HostStatusCb = std::function<void(const HostSharedPtr& host, HealthTransition changed_state,
45
                                          HealthState current_check_result)>;
46

            
47
  /**
48
   * Install a callback that will be invoked every time a health check round is completed for
49
   * a host. The host's health check state may not have changed.
50
   * @param callback supplies the callback to invoke.
51
   */
52
  virtual void addHostCheckCompleteCb(HostStatusCb callback) PURE;
53

            
54
  /**
55
   * Start cyclic health checking based on the provided settings and the type of health checker.
56
   */
57
  virtual void start() PURE;
58
};
59

            
60
using HealthCheckerSharedPtr = std::shared_ptr<HealthChecker>;
61

            
62
std::ostream& operator<<(std::ostream& out, HealthState state);
63
std::ostream& operator<<(std::ostream& out, HealthTransition changed_state);
64

            
65
/**
66
 * Sink for health check event logs.
67
 */
68
class HealthCheckEventLogger {
69
public:
70
187
  virtual ~HealthCheckEventLogger() = default;
71

            
72
  /**
73
   * Log an unhealthy host ejection event.
74
   * @param health_checker_type supplies the type of health checker that generated the event.
75
   * @param host supplies the host that generated the event.
76
   * @param failure_type supplies the type of health check failure.
77
   */
78
  virtual void logEjectUnhealthy(envoy::data::core::v3::HealthCheckerType health_checker_type,
79
                                 const HostDescriptionConstSharedPtr& host,
80
                                 envoy::data::core::v3::HealthCheckFailureType failure_type) PURE;
81

            
82
  /**
83
   * Log an unhealthy host event.
84
   * @param health_checker_type supplies the type of health checker that generated the event.
85
   * @param host supplies the host that generated the event.
86
   * @param failure_type supplies the type of health check failure.
87
   * @param first_check whether this is a failure on the first health check for this host.
88
   */
89
  virtual void logUnhealthy(envoy::data::core::v3::HealthCheckerType health_checker_type,
90
                            const HostDescriptionConstSharedPtr& host,
91
                            envoy::data::core::v3::HealthCheckFailureType failure_type,
92
                            bool first_check) PURE;
93

            
94
  /**
95
   * Log a healthy host addition event.
96
   * @param health_checker_type supplies the type of health checker that generated the event.
97
   * @param host supplies the host that generated the event.
98
   * @param first_check whether this is a fast path success on the first health check for this host.
99
   */
100
  virtual void logAddHealthy(envoy::data::core::v3::HealthCheckerType health_checker_type,
101
                             const HostDescriptionConstSharedPtr& host, bool first_check) PURE;
102

            
103
  /**
104
   * A health check was successful.
105
   * @param health_checker_type supplies the type of health checker that generated the event.
106
   * @param host supplies the host that generated the event.
107
   */
108
  virtual void
109
  logSuccessfulHealthCheck(envoy::data::core::v3::HealthCheckerType health_checker_type,
110
                           const HostDescriptionConstSharedPtr& host) PURE;
111

            
112
  /**
113
   * Log a degraded healthy host event.
114
   * @param health_checker_type supplies the type of health checker that generated the event.
115
   * @param host supplies the host that generated the event.
116
   */
117
  virtual void logDegraded(envoy::data::core::v3::HealthCheckerType health_checker_type,
118
                           const HostDescriptionConstSharedPtr& host) PURE;
119
  /**
120
   * Log a no degraded healthy host event.
121
   * @param health_checker_type supplies the type of health checker that generated the event.
122
   * @param host supplies the host that generated the event.
123
   */
124
  virtual void logNoLongerDegraded(envoy::data::core::v3::HealthCheckerType health_checker_type,
125
                                   const HostDescriptionConstSharedPtr& host) PURE;
126
};
127

            
128
using HealthCheckEventLoggerPtr = std::unique_ptr<HealthCheckEventLogger>;
129

            
130
} // namespace Upstream
131
} // namespace Envoy