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