1
#pragma once
2

            
3
#include <list>
4

            
5
#include "envoy/init/manager.h"
6

            
7
#include "source/common/common/logger.h"
8
#include "source/common/init/watcher_impl.h"
9

            
10
#include "absl/container/flat_hash_map.h"
11

            
12
namespace Envoy {
13
namespace Init {
14

            
15
/**
16
 * Init::ManagerImpl coordinates initialization of one or more "targets." See comments in
17
 * envoy/init/manager.h for an overview.
18
 *
19
 * When the logging level is set to "debug" or "trace," the log will contain entries for all
20
 * significant events in the initialization flow:
21
 *
22
 *   - Targets added to the manager
23
 *   - Initialization started for the manager and for each target
24
 *   - Initialization completed for each target and for the manager
25
 *   - Destruction of targets and watchers
26
 *   - Callbacks to "unavailable" (deleted) targets, manager, or watchers
27
 */
28
class ManagerImpl : public Manager, Logger::Loggable<Logger::Id::init> {
29
public:
30
  /**
31
   * @param name a human-readable manager name, for logging / debugging.
32
   */
33
  ManagerImpl(absl::string_view name);
34

            
35
  // Init::Manager
36
  State state() const override;
37
  void add(const Target& target) override;
38
  void initialize(const Watcher& watcher) override;
39
  void updateWatcher(const Watcher& watcher) override;
40
  void dumpUnreadyTargets(envoy::admin::v3::UnreadyTargetsDumps& dumps) override;
41

            
42
private:
43
  // Callback function with an additional target_name parameter, decrease unready targets count by
44
  // 1, update target_names_count_ hash map.
45
  void onTargetReady(absl::string_view target_name);
46

            
47
  void ready();
48

            
49
  // Human-readable name for logging.
50
  const std::string name_;
51

            
52
  // Current state.
53
  State state_{State::Uninitialized};
54

            
55
  // Current number of registered targets that have not yet initialized.
56
  uint32_t count_{0};
57

            
58
  // Handle to the watcher passed in `initialize`, to be called when initialization completes.
59
  WatcherHandlePtr watcher_handle_;
60

            
61
  // Watcher to receive ready notifications from each target. We restrict the watcher_ inside
62
  // ManagerImpl to be constructed with the 'TargetAwareReadyFn' fn so that the init manager will
63
  // get target name information when the watcher_ calls 'onTargetSendName(target_name)' For any
64
  // other purpose, a watcher can be constructed with either TargetAwareReadyFn or ReadyFn.
65
  const WatcherImpl watcher_;
66

            
67
  // All registered targets.
68
  std::list<TargetHandlePtr> target_handles_;
69

            
70
  // Count of target_name of unready targets.
71
  absl::flat_hash_map<std::string, uint32_t> target_names_count_;
72
};
73

            
74
} // namespace Init
75
} // namespace Envoy