blob: 242c3d93b92bb2631747ff11dbd7d373e6f31caa [file] [log] [blame]
Serge Bazanski35e8d792022-10-11 11:32:30 +02001// Package bmdb implements a connector to the Bare Metal Database, which is the
2// main data store backing information about bare metal machines.
3//
4// All components of the BMaaS project connect directly to the underlying
5// CockroachDB database storing this data via this library. In the future, this
6// library might turn into a shim which instead connects to a coordinator
7// service over gRPC.
8package bmdb
Serge Bazanskide4e8412023-02-15 23:28:04 +01009
Serge Bazanskic50f6942023-04-24 18:27:22 +020010import (
11 "github.com/prometheus/client_golang/prometheus"
12
13 "source.monogon.dev/cloud/bmaas/bmdb/metrics"
14 "source.monogon.dev/cloud/lib/component"
15)
Serge Bazanskide4e8412023-02-15 23:28:04 +010016
17// BMDB is the Bare Metal Database, a common schema to store information about
18// bare metal machines in CockroachDB. This struct is supposed to be
19// embedded/contained by different components that interact with the BMDB, and
20// provides a common interface to BMDB operations to these components.
21//
22// The BMDB provides two mechanisms facilitating a 'reactive work system' being
23// implemented on the bare metal machine data:
24//
25// - Sessions, which are maintained by heartbeats by components and signal the
26// liveness of said components to other components operating on the BMDB. These
27// effectively extend CockroachDB's transactions to be visible as row data. Any
28// session that is not actively being updated by a component can be expired by a
29// component responsible for lease garbage collection.
30// - Work locking, which bases on Sessions and allows long-standing
31// multi-transaction work to be performed on given machines, preventing
32// conflicting work from being performed by other components. As both Work
33// locking and Sessions are plain row data, other components can use SQL queries
34// to exclude machines to act on by constraining SELECT queries to not return
35// machines with some active work being performed on them.
36type BMDB struct {
37 Config
Serge Bazanskic50f6942023-04-24 18:27:22 +020038
39 metrics *metrics.MetricsSet
Serge Bazanskide4e8412023-02-15 23:28:04 +010040}
41
42// Config is the configuration of the BMDB connector.
43type Config struct {
44 Database component.CockroachConfig
45
46 // ComponentName is a human-readable name of the component connecting to the
47 // BMDB, and is stored in any Sessions managed by this component's connector.
48 ComponentName string
49 // RuntimeInfo is a human-readable 'runtime information' (eg. software version,
50 // host machine/job information, IP address, etc.) stored alongside the
51 // ComponentName in active Sessions.
52 RuntimeInfo string
53}
Serge Bazanskic50f6942023-04-24 18:27:22 +020054
55// EnableMetrics configures BMDB metrics collection and registers it on the given
56// registry. This method should only be called once, and is not goroutine safe.
57func (b *BMDB) EnableMetrics(registry *prometheus.Registry) {
58 if b.metrics == nil {
59 b.metrics = metrics.New(registry)
60 }
61}