Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

database: Store all Operations items in a single partition #730

Merged
merged 1 commit into from
Oct 16, 2024
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
1 change: 0 additions & 1 deletion dev-infrastructure/modules/rp-cosmos.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ var containers = [
{
name: 'Operations'
defaultTtl: 604800 // 7 days
partitionKeyPaths: ['/id']
}
{
name: 'Resources'
Expand Down
21 changes: 18 additions & 3 deletions internal/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,21 @@ const (
billingContainer = "Billing"
operationsContainer = "Operations"
locksContainer = "Locks"

// XXX The azcosmos SDK currently only supports single-partition queries,
// so there's no way to list all items in a container unless you know
// all the partition keys. The backend needs to list all items in the
// Operations container so to work around this limitation we keep all
// items in a single partition with a well-known name: "workaround".
//
// Once [1] is fixed we could transition the Operations container to
// using subscription IDs as the partition key like other containers.
// The items are transient thanks to the container's default TTL, so
// GetOperationDoc would just need temporary fallback logic to check
// the "workaround" partition.
//
// [1] https://github.com/Azure/azure-sdk-for-go/issues/18578
operationsPartitionKey = "workaround"
)

var ErrNotFound = errors.New("DocumentNotFound")
Expand Down Expand Up @@ -314,7 +329,7 @@ func (d *CosmosDBClient) GetOperationDoc(ctx context.Context, operationID string
return nil, err
}

pk := azcosmos.NewPartitionKeyString(operationID)
pk := azcosmos.NewPartitionKeyString(operationsPartitionKey)

response, err := container.ReadItem(ctx, pk, operationID, nil)
if isResponseError(err, http.StatusNotFound) {
Expand All @@ -340,7 +355,7 @@ func (d *CosmosDBClient) CreateOperationDoc(ctx context.Context, doc *OperationD
return err
}

pk := azcosmos.NewPartitionKeyString(doc.ID)
pk := azcosmos.NewPartitionKeyString(operationsPartitionKey)

data, err := json.Marshal(doc)
if err != nil {
Expand All @@ -366,7 +381,7 @@ func (d *CosmosDBClient) DeleteOperationDoc(ctx context.Context, operationID str
return err
}

pk := azcosmos.NewPartitionKeyString(operationID)
pk := azcosmos.NewPartitionKeyString(operationsPartitionKey)

_, err = container.DeleteItem(ctx, pk, operationID, nil)
if isResponseError(err, http.StatusNotFound) {
Expand Down
2 changes: 2 additions & 0 deletions internal/database/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ const (
type OperationDocument struct {
BaseDocument

PartitionKey string `json:"partitionKey,omitempty"`
// TenantID is the tenant ID of the client that requested the operation
TenantID string `json:"tenantId,omitempty"`
// ClientID is the object ID of the client that requested the operation
Expand Down Expand Up @@ -96,6 +97,7 @@ func NewOperationDocument(request OperationRequest) *OperationDocument {

return &OperationDocument{
BaseDocument: newBaseDocument(),
PartitionKey: operationsPartitionKey,
Request: request,
StartTime: now,
LastTransitionTime: now,
Expand Down
Loading