Skip to content

Commit

Permalink
Merge pull request #14918 from manadart/3.0-dqlite-lease-store
Browse files Browse the repository at this point in the history
[JUJU-1944] DB-backed lease store implementation
  • Loading branch information
manadart committed Dec 13, 2022
2 parents 8315fb7 + 98b70fc commit e4f0d39
Show file tree
Hide file tree
Showing 5 changed files with 747 additions and 9 deletions.
16 changes: 16 additions & 0 deletions database/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package database

import (
"github.com/juju/errors"
"github.com/mattn/go-sqlite3"
)

// IsErrConstraintUnique returns true if the input error was
// returned by SQLite due to violation of a unique constraint.
func IsErrConstraintUnique(err error) bool {
var sqliteErr sqlite3.Error
if errors.As(err, &sqliteErr) && sqliteErr.ExtendedCode == sqlite3.ErrConstraintUnique {
return true
}
return false
}
34 changes: 26 additions & 8 deletions database/schema/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,46 @@ CREATE UNIQUE INDEX idx_lease_type_type
ON lease_type (type);
INSERT INTO lease_type VALUES
(0, 'controller'),
(1, 'model' ),
(2, 'application');
(0, 'controller'), -- The controller running singular controller workers.
(1, 'model' ), -- The controller running singular workers for a model.
(2, 'application'); -- The unit that holds leadership for an application.
CREATE TABLE IF NOT EXISTS lease (
CREATE TABLE lease (
uuid TEXT PRIMARY KEY,
lease_type_id INT NOT NULL,
model_uuid TEXT,
name TEXT,
holder TEXT,
start TIMESTAMP,
expiry TIMESTAMP,
pinned BOOLEAN,
CONSTRAINT fk_lease_lease_type
FOREIGN KEY (lease_type_id)
REFERENCES lease_type(id)
);
CREATE UNIQUE INDEX idx_lease_type_name
ON lease (lease_type_id, name);
CREATE UNIQUE INDEX idx_lease_model_type_name
ON lease (model_uuid, lease_type_id, name);
CREATE INDEX idx_lease_expiry
ON lease (expiry);`[1:]
ON lease (expiry);
CREATE TABLE lease_pin (
-- The presence of entries in this table for a particular lease_uuid
-- implies that the lease in question is pinned and cannot expire.
uuid TEXT PRIMARY KEY,
lease_uuid TEXT,
entity_id TEXT,
CONSTRAINT fk_lease_pin_lease
FOREIGN KEY (lease_uuid)
REFERENCES lease(uuid)
);
CREATE UNIQUE INDEX idx_lease_pin_lease_entity
ON lease_pin (lease_uuid, entity_id);
CREATE INDEX idx_lease_pin_lease
ON lease_pin (lease_uuid);
`[1:]

return strings.Split(delta, ";\n\n")
}
6 changes: 5 additions & 1 deletion worker/lease/package_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package lease_test
package lease

import (
"testing"
Expand All @@ -12,3 +12,7 @@ import (
func TestPackage(t *testing.T) {
gc.TestingT(t)
}

type StubLogger struct{}

func (StubLogger) Errorf(string, ...interface{}) {}

0 comments on commit e4f0d39

Please sign in to comment.