blob: 400aa4a18215aa25da38ad6087a70ba5fe3a7a94 [file] [log] [blame]
Tim Windelschmidt6d33a432025-02-04 14:34:25 +01001// Copyright The Monogon Project Authors.
2// SPDX-License-Identifier: Apache-2.0
3
Serge Bazanski35e8d792022-10-11 11:32:30 +02004// 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.
11package bmdb
Serge Bazanskide4e8412023-02-15 23:28:04 +010012
Serge Bazanskic50f6942023-04-24 18:27:22 +020013import (
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 Bazanskide4e8412023-02-15 23:28:04 +010019
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.
39type BMDB struct {
40 Config
Serge Bazanskic50f6942023-04-24 18:27:22 +020041
42 metrics *metrics.MetricsSet
Serge Bazanskide4e8412023-02-15 23:28:04 +010043}
44
45// Config is the configuration of the BMDB connector.
46type 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 Bazanskic50f6942023-04-24 18:27:22 +020057
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.
60func (b *BMDB) EnableMetrics(registry *prometheus.Registry) {
61 if b.metrics == nil {
62 b.metrics = metrics.New(registry)
63 }
64}