1
#pragma once
2

            
3
#include "envoy/stream_info/stream_info.h"
4
#include "envoy/upstream/retry.h"
5

            
6
#include "source/extensions/load_balancing_policies/common/load_balancer_impl.h"
7

            
8
namespace Envoy {
9
namespace Extensions {
10
namespace Retry {
11
namespace Priority {
12

            
13
class PreviousPrioritiesRetryPriority : public Upstream::RetryPriority {
14
public:
15
  PreviousPrioritiesRetryPriority(uint32_t update_frequency, uint32_t max_retries)
16
9
      : update_frequency_(update_frequency) {
17
9
    attempted_hosts_.reserve(max_retries);
18
9
  }
19

            
20
  const Upstream::HealthyAndDegradedLoad&
21
  determinePriorityLoad(StreamInfo::StreamInfo* stream_info,
22
                        const Upstream::PrioritySet& priority_set,
23
                        const Upstream::HealthyAndDegradedLoad& original_priority_load,
24
                        const PriorityMappingFunc& priority_mapping_func) override;
25

            
26
20
  void onHostAttempted(Upstream::HostDescriptionConstSharedPtr attempted_host) override {
27
20
    attempted_hosts_.emplace_back(attempted_host);
28
20
  }
29

            
30
private:
31
46
  void recalculatePerPriorityState(uint32_t priority, const Upstream::PrioritySet& priority_set) {
32
    // Recalculate health and priority the same way the load balancer does it.
33
46
    Upstream::LoadBalancerBase::recalculatePerPriorityState(
34
46
        priority, priority_set, per_priority_load_, per_priority_health_, per_priority_degraded_,
35
46
        total_healthy_hosts_);
36
46
  }
37

            
38
  uint32_t adjustedAvailability(std::vector<uint32_t>& per_priority_health,
39
                                std::vector<uint32_t>& per_priority_degraded) const;
40

            
41
  // Distributes priority load between priorities that should be considered after
42
  // excluding attempted priorities.
43
  // @return whether the adjustment was successful. If not, the original priority load should be
44
  // used.
45
  bool adjustForAttemptedPriorities(const Upstream::PrioritySet& priority_set);
46

            
47
  const uint32_t update_frequency_;
48
  std::vector<Upstream::HostDescriptionConstSharedPtr> attempted_hosts_;
49
  std::vector<bool> excluded_priorities_;
50
  Upstream::HealthyAndDegradedLoad per_priority_load_;
51
  Upstream::HealthyAvailability per_priority_health_;
52
  Upstream::DegradedAvailability per_priority_degraded_;
53
  uint32_t total_healthy_hosts_;
54
};
55

            
56
} // namespace Priority
57
} // namespace Retry
58
} // namespace Extensions
59
} // namespace Envoy