Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 77 additions & 1 deletion API.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ with identical semantics upstream.
| `keystone/db` | `GetGroupMetrics` | `ListGroups` |
| `magnum/db` | `GetClusterMetrics` | `GetClusterList` |
| `manila/db` | `GetShareMetrics` | `ShareGetAllWithInstances` |
| `manila/db` | — | `ShareGetInventory` |
| `neutron/db` | `GetAgents` | `GetAgents` |
| `neutron/db` | `GetHARouterAgentPortBindingsWithAgents` | `GetHARouterAgentPortBindingsWithAgents` |
| `neutron/db` | `GetRouters` | `GetRouters` |
Expand Down Expand Up @@ -68,7 +69,7 @@ with identical semantics upstream.
- [Designate storage](https://github.com/openstack/designate/blob/master/designate/storage/sqlalchemy/__init__.py): `find_zones`, `find_recordsets`, `count_recordsets`. The library also follows [Designate's UUID conversion](https://github.com/openstack/designate/blob/master/designate/storage/sqlalchemy/types.py) and [recordset status precedence](https://github.com/openstack/designate/blob/master/designate/objects/recordset.py).
- [Ironic DB API](https://github.com/openstack/ironic/blob/master/ironic/db/api.py) and [Magnum DB API](https://github.com/openstack/magnum/blob/master/magnum/db/api.py): `get_node_list`, `get_cluster_list`.
- [Heat DB API](https://github.com/openstack/heat/blob/master/heat/db/api.py): `stack_get_all`.
- [Manila DB API](https://github.com/openstack/manila/blob/master/manila/db/api.py): share operations; the instance join is an explicit extension.
- [Manila DB API](https://github.com/openstack/manila/blob/master/manila/db/api.py): share operations and quota, quota usage and quota class reads; the instance join is an explicit extension. [`Share.instance`](https://github.com/openstack/manila/blob/master/manila/db/sqlalchemy/models.py) defines the representative-instance rules that `ShareGetInventory` follows.
- [Keystone backends](https://github.com/openstack/keystone/tree/master/keystone): `list_*` resource, identity, and catalog operations are grouped into a single read projection package here.
- [Neutron database plugins](https://github.com/openstack/neutron/tree/master/neutron/db): `get_*` resource operations; capacity/count queries are extensions.
- [Octavia repositories](https://github.com/openstack/octavia/blob/master/octavia/db/repositories.py): resource repositories with `get_all` operations.
Expand Down Expand Up @@ -99,3 +100,78 @@ lifecycle metadata. Their fixed current-resource queries remain unchanged for
exporter consumers. Deleted volume types and legacy type tables are only read
when explicitly requested. Legacy table availability is a caller/deployment
concern; errors are propagated rather than hidden.

### Manila share inventory

| Go operation | Upstream naming source | Scope |
| --- | --- | --- |
| `ShareGetInventory` | [Manila `Share.instance`](https://github.com/openstack/manila/blob/master/manila/db/sqlalchemy/models.py) | One row per logical share with its representative instance. Status ranking copies `constants.TRANSITIONAL_STATUSES` and must be re-checked on Manila upgrades. |

`ShareGetInventory` returns exactly one row per nondeleted logical share,
including shares without instances. It follows Manila's `Share.instance`:
status precedence, active-replica preference, and the replication-change
exception. Instance ID resolves otherwise unspecified ties (including writable
replication with multiple active replicas). Status, AZ and type all come from
the same representative instance, chosen in one pass with window functions.
The status ranking copies Manila's `constants.TRANSITIONAL_STATUSES`; re-check
it against `manila/common/constants.py` and `Share.instance` in
`manila/db/sqlalchemy/models.py` when moving to a new Manila release, because a
missing transitional status would be preferred over the listed ones. Retained
`is_soft_deleted` shares remain in inventory, preserving the existing deletion
scope; row-soft-deleted shares and instances are excluded. This is inventory,
not quota accounting.

The MariaDB integration test is `TestManilaInventory` in `tests/` and requires Docker.

### Manila quota accounting

The Manila quota queries are named after Manila's DB API functions. Each one
reads every project at once rather than one project, and returns raw rows
rather than a per-project dictionary:

| Go operation | Upstream naming source | Table |
| --- | --- | --- |
| `QuotaGetAllByProject` | [Manila `quota_get_all_by_project`](https://github.com/openstack/manila/blob/master/manila/db/sqlalchemy/api.py) | `quotas` |
| `QuotaGetAllByProjectAndUser` | [Manila `quota_get_all_by_project_and_user`](https://github.com/openstack/manila/blob/master/manila/db/sqlalchemy/api.py) | `project_user_quotas` |
| `QuotaGetAllByProjectAndShareType` | [Manila `quota_get_all_by_project_and_share_type`](https://github.com/openstack/manila/blob/master/manila/db/sqlalchemy/api.py) | `project_share_type_quotas` |
| `QuotaClassGetDefault` | [Manila `quota_class_get_default`](https://github.com/openstack/manila/blob/master/manila/db/sqlalchemy/api.py) | `quota_classes` |
| `QuotaUsageGetAllByProject` | [Manila `quota_usage_get_all_by_project`](https://github.com/openstack/manila/blob/master/manila/db/sqlalchemy/api.py) | `quota_usages`, `share_type_id IS NULL` |
| `QuotaUsageGetAllByProjectAndShareType` | [Manila `quota_usage_get_all_by_project_and_share_type`](https://github.com/openstack/manila/blob/master/manila/db/sqlalchemy/api.py) | `quota_usages`, `share_type_id IS NOT NULL` |

Rows with a NULL or empty project ID are excluded. Results are ordered by their
identifying columns and row ID, so callers that collapse duplicates get a
stable result.

Usage rows are not aggregated. `in_use` and `reserved` are returned as stored,
including NULL, so callers can tell malformed rows from zero usage. To get
Manila's project totals, sum `QuotaUsageGetAllByProject` rows by project and
resource; this matches `_quota_usage_get_all`, which sums users (including
legacy NULL-user rows) and excludes share-type rows. Share-type accounting
describes the same allocations: read it with
`QuotaUsageGetAllByProjectAndShareType` and never add it to project totals.
`reserved` already counts open reservations; do not add `reservations` again.

The share-type queries left-join `share_types`. When a share type is deleted or
missing, its limits and usage are still returned with a NULL `ShareTypeName`, so
per-type totals reconcile with project totals.

The generated quota models use `sql.NullInt32` for integer `Deleted` fields:
active rows have `0`, and soft-deleted rows store their row ID. The fixture has
no database default for these columns; Manila supplies `0` through SQLAlchemy.
Share and instance deletion markers retain their existing string representation.

User overrides live in `project_user_quotas`, separately from project overrides
in `quotas` and type overrides in `project_share_type_quotas`. Never sum limits
or substitute user limits for project limits. A NULL hard limit is preserved
so callers can detect invalid or legacy data; Manila enforces numeric limits,
using `-1` for unlimited. The caller supplies service configuration defaults
and resolves missing limits: share-type override → project override →
persisted `default` class → service configuration. Non-default classes need
request-context information that these database tables do not provide.

These queries read Manila's quota accounting, not rebuilt object counts or
physical storage use. Stale accounting, including usage left after object
removal, remains visible until Manila reconciles it. No writes or reconciliation
are performed. The schema files are test fixtures, not production migrations.

The MariaDB integration test is `TestManilaQuotas` in `tests/` and requires Docker.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ repository objects. Placement queries live under `placement/objects`.
| `heat/db` | `StackGetAll` |
| `ironic/db` | `GetNodeList` |
| `magnum/db` | `GetClusterList` |
| `manila/db` | `ShareGetAllWithInstances` |
| `manila/db` | `ShareGetAllWithInstances`, `ShareGetInventory`, `QuotaGetAllByProject`, `QuotaUsageGetAllByProject` |
| `neutron/db` | `GetAgents`, `GetHARouterAgentPortBindingsWithAgents`, `GetRouters` |
| `octavia/db/repositories` | `PoolRepository`, `LoadBalancerRepository`, `AmphoraRepository` |
| `placement/objects` | Resource provider inventory and allocation projections |
Expand Down
1 change: 1 addition & 0 deletions internal/testutil/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"testing"
"time"

_ "github.com/go-sql-driver/mysql"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/modules/mariadb"
"github.com/vexxhost/openstackdb/schema"
Expand Down
77 changes: 77 additions & 0 deletions manila/db/inventory_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package manila

import (
"context"
"database/sql"
"regexp"
"testing"

"github.com/DATA-DOG/go-sqlmock"
"github.com/stretchr/testify/require"
)

func TestShareInventoryScansValues(t *testing.T) {
db, mock, err := sqlmock.New()
require.NoError(t, err)
defer func() { _ = db.Close() }()
mock.ExpectQuery(regexp.QuoteMeta(ShareGetInventory)).WillReturnRows(
sqlmock.NewRows([]string{"id", "name", "project_id", "size", "share_proto", "status", "share_type", "share_type_name", "availability_zone"}).
AddRow("no-instance", nil, nil, nil, nil, nil, "", "", "").
AddRow("share", "name", "project", 10, "NFS", "available", "type", "gold", "zone"),
).RowsWillBeClosed()
rows, err := New(db).ShareGetInventory(context.Background())
require.NoError(t, err)
require.Equal(t, []ShareGetInventoryRow{
{ID: "no-instance"},
{
ID: "share", Name: sql.NullString{String: "name", Valid: true},
ProjectID: sql.NullString{String: "project", Valid: true},
Size: sql.NullInt32{Int32: 10, Valid: true},
ShareProto: sql.NullString{String: "NFS", Valid: true},
Status: sql.NullString{String: "available", Valid: true},
ShareType: "type", ShareTypeName: "gold", AvailabilityZone: "zone",
},
}, rows)
require.NoError(t, mock.ExpectationsWereMet())
}

func TestShareInventoryErrors(t *testing.T) {
for _, scenario := range []string{"empty", "query", "scan", "iteration", "canceled"} {
t.Run(scenario, func(t *testing.T) {
db, mock, err := sqlmock.New()
require.NoError(t, err)
defer func() { _ = db.Close() }()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
if scenario == "canceled" {
cancel()
} else {
query := mock.ExpectQuery(regexp.QuoteMeta(ShareGetInventory))
rows := sqlmock.NewRows([]string{"id", "name", "project_id", "size", "share_proto", "status", "share_type", "share_type_name", "availability_zone"})
switch scenario {
case "query":
query.WillReturnError(sql.ErrConnDone)
case "scan":
query.WillReturnRows(rows.AddRow("s", nil, nil, "invalid integer", nil, nil, "", "", "")).RowsWillBeClosed()
case "iteration":
query.WillReturnRows(rows.AddRow("s", nil, nil, nil, nil, nil, "", "", "").RowError(0, sql.ErrConnDone)).RowsWillBeClosed()
default:
query.WillReturnRows(rows).RowsWillBeClosed()
}
}
rows, err := New(db).ShareGetInventory(ctx)
switch scenario {
case "empty":
require.NoError(t, err)
require.Empty(t, rows)
case "query", "iteration":
require.ErrorIs(t, err, sql.ErrConnDone)
case "canceled":
require.ErrorIs(t, err, context.Canceled)
default:
require.Error(t, err)
}
require.NoError(t, mock.ExpectationsWereMet())
})
}
}
61 changes: 61 additions & 0 deletions manila/db/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading