Serge Bazanski | 35e8d79 | 2022-10-11 11:32:30 +0200 | [diff] [blame] | 1 | // 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. |
| 8 | package bmdb |
Serge Bazanski | de4e841 | 2023-02-15 23:28:04 +0100 | [diff] [blame] | 9 | |
Serge Bazanski | c50f694 | 2023-04-24 18:27:22 +0200 | [diff] [blame^] | 10 | import ( |
| 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 Bazanski | de4e841 | 2023-02-15 23:28:04 +0100 | [diff] [blame] | 16 | |
| 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. |
| 36 | type BMDB struct { |
| 37 | Config |
Serge Bazanski | c50f694 | 2023-04-24 18:27:22 +0200 | [diff] [blame^] | 38 | |
| 39 | metrics *metrics.MetricsSet |
Serge Bazanski | de4e841 | 2023-02-15 23:28:04 +0100 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | // Config is the configuration of the BMDB connector. |
| 43 | type 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 Bazanski | c50f694 | 2023-04-24 18:27:22 +0200 | [diff] [blame^] | 54 | |
| 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. |
| 57 | func (b *BMDB) EnableMetrics(registry *prometheus.Registry) { |
| 58 | if b.metrics == nil { |
| 59 | b.metrics = metrics.New(registry) |
| 60 | } |
| 61 | } |