cloud/bmaas: clean up bmdb package
Everything living in sessions.go made this a bit unreadable.
We also drive-by add some extra metadata fields to Connection. This will
come in handy in an upcoming change.
Change-Id: Ibabec9e3bd0b29b727638b9450a53ba28c33e678
Reviewed-on: https://review.monogon.dev/c/monogon/+/1130
Reviewed-by: Leopold Schabel <leo@monogon.tech>
Tested-by: Jenkins CI
diff --git a/cloud/bmaas/bmdb/BUILD.bazel b/cloud/bmaas/bmdb/BUILD.bazel
index 65a11c7..b89f777 100644
--- a/cloud/bmaas/bmdb/BUILD.bazel
+++ b/cloud/bmaas/bmdb/BUILD.bazel
@@ -4,6 +4,7 @@
name = "bmdb",
srcs = [
"bmdb.go",
+ "connection.go",
"sessions.go",
],
importpath = "source.monogon.dev/cloud/bmaas/bmdb",
diff --git a/cloud/bmaas/bmdb/bmdb.go b/cloud/bmaas/bmdb/bmdb.go
index d56e083..5699381 100644
--- a/cloud/bmaas/bmdb/bmdb.go
+++ b/cloud/bmaas/bmdb/bmdb.go
@@ -6,3 +6,41 @@
// library might turn into a shim which instead connects to a coordinator
// service over gRPC.
package bmdb
+
+import "source.monogon.dev/cloud/lib/component"
+
+// BMDB is the Bare Metal Database, a common schema to store information about
+// bare metal machines in CockroachDB. This struct is supposed to be
+// embedded/contained by different components that interact with the BMDB, and
+// provides a common interface to BMDB operations to these components.
+//
+// The BMDB provides two mechanisms facilitating a 'reactive work system' being
+// implemented on the bare metal machine data:
+//
+// - Sessions, which are maintained by heartbeats by components and signal the
+// liveness of said components to other components operating on the BMDB. These
+// effectively extend CockroachDB's transactions to be visible as row data. Any
+// session that is not actively being updated by a component can be expired by a
+// component responsible for lease garbage collection.
+// - Work locking, which bases on Sessions and allows long-standing
+// multi-transaction work to be performed on given machines, preventing
+// conflicting work from being performed by other components. As both Work
+// locking and Sessions are plain row data, other components can use SQL queries
+// to exclude machines to act on by constraining SELECT queries to not return
+// machines with some active work being performed on them.
+type BMDB struct {
+ Config
+}
+
+// Config is the configuration of the BMDB connector.
+type Config struct {
+ Database component.CockroachConfig
+
+ // ComponentName is a human-readable name of the component connecting to the
+ // BMDB, and is stored in any Sessions managed by this component's connector.
+ ComponentName string
+ // RuntimeInfo is a human-readable 'runtime information' (eg. software version,
+ // host machine/job information, IP address, etc.) stored alongside the
+ // ComponentName in active Sessions.
+ RuntimeInfo string
+}
diff --git a/cloud/bmaas/bmdb/connection.go b/cloud/bmaas/bmdb/connection.go
new file mode 100644
index 0000000..3db869d
--- /dev/null
+++ b/cloud/bmaas/bmdb/connection.go
@@ -0,0 +1,60 @@
+package bmdb
+
+import (
+ "database/sql"
+ "fmt"
+
+ "k8s.io/klog/v2"
+
+ "source.monogon.dev/cloud/bmaas/bmdb/model"
+)
+
+// Open creates a new Connection to the BMDB for the calling component. Multiple
+// connections can be opened (although there is no advantage to doing so, as
+// Connections manage an underlying CockroachDB connection pool, which performs
+// required reconnects and connection pooling automatically).
+func (b *BMDB) Open(migrate bool) (*Connection, error) {
+ if migrate {
+ if b.Config.Database.Migrations == nil {
+ klog.Infof("Using default migrations source.")
+ m, err := model.MigrationsSource()
+ if err != nil {
+ klog.Exitf("failed to prepare migrations source: %w", err)
+ }
+ b.Config.Database.Migrations = m
+ }
+ if err := b.Database.MigrateUp(); err != nil {
+ return nil, fmt.Errorf("migration failed: %w", err)
+ }
+ }
+ db, err := b.Database.Connect()
+ if err != nil {
+ return nil, err
+ }
+ return &Connection{
+ bmdb: b,
+ db: db,
+
+ DatabaseName: b.Config.Database.DatabaseName,
+ Address: b.Config.Database.EndpointHost,
+ InMemory: b.Config.Database.InMemory,
+ }, nil
+}
+
+// Connection to the BMDB. Internally, this contains a sql.DB connection pool,
+// so components can (and likely should) reuse Connections as much as possible
+// internally.
+type Connection struct {
+ bmdb *BMDB
+ db *sql.DB
+
+ // The database name that we're connected to.
+ DatabaseName string
+ // The address of the CockroachDB endpoint we've connected to.
+ Address string
+ // Whether this connection is to an in-memory database. Note: this only works if
+ // this Connection came directly from calling Open on a BMDB that was defined to
+ // be in-memory. If you just connect to an in-memory CRDB manually, this will
+ // still be false.
+ InMemory bool
+}
diff --git a/cloud/bmaas/bmdb/sessions.go b/cloud/bmaas/bmdb/sessions.go
index 4336121..3f70393 100644
--- a/cloud/bmaas/bmdb/sessions.go
+++ b/cloud/bmaas/bmdb/sessions.go
@@ -13,81 +13,8 @@
"k8s.io/klog/v2"
"source.monogon.dev/cloud/bmaas/bmdb/model"
- "source.monogon.dev/cloud/lib/component"
)
-// BMDB is the Bare Metal Database, a common schema to store information about
-// bare metal machines in CockroachDB. This struct is supposed to be
-// embedded/contained by different components that interact with the BMDB, and
-// provides a common interface to BMDB operations to these components.
-//
-// The BMDB provides two mechanisms facilitating a 'reactive work system' being
-// implemented on the bare metal machine data:
-//
-// - Sessions, which are maintained by heartbeats by components and signal the
-// liveness of said components to other components operating on the BMDB. These
-// effectively extend CockroachDB's transactions to be visible as row data. Any
-// session that is not actively being updated by a component can be expired by a
-// component responsible for lease garbage collection.
-// - Work locking, which bases on Sessions and allows long-standing
-// multi-transaction work to be performed on given machines, preventing
-// conflicting work from being performed by other components. As both Work
-// locking and Sessions are plain row data, other components can use SQL queries
-// to exclude machines to act on by constraining SELECT queries to not return
-// machines with some active work being performed on them.
-type BMDB struct {
- Config
-}
-
-// Config is the configuration of the BMDB connector.
-type Config struct {
- Database component.CockroachConfig
-
- // ComponentName is a human-readable name of the component connecting to the
- // BMDB, and is stored in any Sessions managed by this component's connector.
- ComponentName string
- // RuntimeInfo is a human-readable 'runtime information' (eg. software version,
- // host machine/job information, IP address, etc.) stored alongside the
- // ComponentName in active Sessions.
- RuntimeInfo string
-}
-
-// Open creates a new Connection to the BMDB for the calling component. Multiple
-// connections can be opened (although there is no advantage to doing so, as
-// Connections manage an underlying CockroachDB connection pool, which performs
-// required reconnects and connection pooling automatically).
-func (b *BMDB) Open(migrate bool) (*Connection, error) {
- if migrate {
- if b.Config.Database.Migrations == nil {
- klog.Infof("Using default migrations source.")
- m, err := model.MigrationsSource()
- if err != nil {
- klog.Exitf("failed to prepare migrations source: %w", err)
- }
- b.Config.Database.Migrations = m
- }
- if err := b.Database.MigrateUp(); err != nil {
- return nil, fmt.Errorf("migration failed: %w", err)
- }
- }
- db, err := b.Database.Connect()
- if err != nil {
- return nil, err
- }
- return &Connection{
- bmdb: b,
- db: db,
- }, nil
-}
-
-// Connection to the BMDB. Internally, this contains a sql.DB connection pool,
-// so components can (and likely should) reuse Connections as much as possible
-// internally.
-type Connection struct {
- bmdb *BMDB
- db *sql.DB
-}
-
// StartSession creates a new BMDB session which will be maintained in a
// background goroutine as long as the given context is valid. Each Session is
// represented by an entry in a sessions table within the BMDB, and subsequent