Kagome
Polkadot Runtime Engine in C++17
kagome_application_impl.cpp
Go to the documentation of this file.
1 
7 
8 #include <thread>
9 
13 #include "consensus/babe/babe.hpp"
15 #include "metrics/metrics.hpp"
16 #include "telemetry/service.hpp"
17 
18 namespace kagome::application {
19 
21  const AppConfiguration &app_config)
22  : app_config_(app_config),
23  injector_{std::make_unique<injector::KagomeNodeInjector>(app_config)},
24  logger_(log::createLogger("Application", "application")) {
25  // keep important instances, the must exist when injector destroyed
26  // some of them are requested by reference and hence not copied
27  chain_spec_ = injector_->injectChainSpec();
28  BOOST_ASSERT(chain_spec_ != nullptr);
29  }
30 
32  auto mode = injector_->injectPrintChainInfoMode();
33  return mode->run();
34  }
35 
37  logger_->info("Start in recovery mode with PID {}", getpid());
38 
39  auto mode = injector_->injectRecoveryMode();
40  return mode->run();
41  }
42 
44  app_state_manager_ = injector_->injectAppStateManager();
45  io_context_ = injector_->injectIoContext();
46  clock_ = injector_->injectSystemClock();
47  babe_ = injector_->injectBabe();
48  exposer_ = injector_->injectOpenMetricsService();
49  grandpa_ = injector_->injectGrandpa();
50  router_ = injector_->injectRouter();
51  peer_manager_ = injector_->injectPeerManager();
52  jrpc_api_service_ = injector_->injectRpcApiService();
53  state_observer_ = injector_->injectStateObserver();
54  sync_observer_ = injector_->injectSyncObserver();
55  parachain_observer_ = injector_->injectParachainObserver();
56  metrics_watcher_ = injector_->injectMetricsWatcher();
57  telemetry_service_ = injector_->injectTelemetryService();
59 
60  logger_->info("Start as node version '{}' named as '{}' with PID {}",
63  getpid());
64 
65  auto chain_path = app_config_.chainPath(chain_spec_->id());
66  auto storage_backend = app_config_.storageBackend()
68  ? "RocksDB"
69  : "Unknown";
70  logger_->info(
71  "Chain path is {}, storage backend is {}", chain_path, storage_backend);
72  auto res = util::init_directory(chain_path);
73  if (not res) {
74  logger_->critical("Error initializing chain directory {}: {}",
75  chain_path.native(),
76  res.error().message());
77  exit(EXIT_FAILURE);
78  }
79 
80  app_state_manager_->atLaunch([ctx{io_context_}] {
81  std::thread asio_runner([ctx{ctx}] { ctx->run(); });
82  asio_runner.detach();
83  return true;
84  });
85 
86  app_state_manager_->atShutdown([ctx{io_context_}] { ctx->stop(); });
87 
88  { // Metrics
89  auto metrics_registry = metrics::createRegistry();
90 
91  constexpr auto startTimeMetricName = "kagome_process_start_time_seconds";
92  metrics_registry->registerGaugeFamily(
93  startTimeMetricName,
94  "UNIX timestamp of the moment the process started");
95  auto metric_start_time =
96  metrics_registry->registerGaugeMetric(startTimeMetricName);
97  metric_start_time->set(clock_->nowUint64());
98 
99  constexpr auto nodeRolesMetricName = "kagome_node_roles";
100  metrics_registry->registerGaugeFamily(nodeRolesMetricName,
101  "The roles the node is running as");
102  auto metric_node_roles =
103  metrics_registry->registerGaugeMetric(nodeRolesMetricName);
104  metric_node_roles->set(app_config_.roles().value);
105 
106  constexpr auto buildInfoMetricName = "kagome_build_info";
107  metrics_registry->registerGaugeFamily(
108  buildInfoMetricName,
109  "A metric with a constant '1' value labeled by name, version");
110  auto metric_build_info = metrics_registry->registerGaugeMetric(
111  buildInfoMetricName,
112  {{"name", app_config_.nodeName()},
113  {"version", app_config_.nodeVersion()}});
114  metric_build_info->set(1);
115  }
116 
117  app_state_manager_->run();
118  }
119 
120 } // namespace kagome::application
sptr< consensus::grandpa::Grandpa > grandpa_
sptr< metrics::MetricsWatcher > metrics_watcher_
virtual boost::filesystem::path chainPath(std::string chain_id) const =0
int recovery() override
Runs recovery mode.
virtual network::Roles roles() const =0
sptr< network::StateProtocolObserver > state_observer_
sptr< network::SyncProtocolObserver > sync_observer_
RegistryPtr createRegistry()
sptr< telemetry::TelemetryService > telemetry_service_
virtual const std::string & nodeVersion() const =0
uptr< injector::KagomeNodeInjector > injector_
KagomeApplicationImpl(const AppConfiguration &config)
virtual StorageBackend storageBackend() const =0
sptr< parachain::ParachainObserverImpl > parachain_observer_
outcome::result< void > init_directory(const boost::filesystem::path &path)
Definition: util.cpp:30
virtual const std::string & nodeName() const =0
Logger createLogger(const std::string &tag)
Definition: logger.cpp:112
void setTelemetryService(std::shared_ptr< TelemetryService > service)
Definition: service.cpp:62