Skip to content

Commit

Permalink
CBG-4213: add attachment migration api
Browse files Browse the repository at this point in the history
  • Loading branch information
gregns1 committed Nov 6, 2024
1 parent 38297b3 commit ff9dc68
Show file tree
Hide file tree
Showing 11 changed files with 490 additions and 27 deletions.
7 changes: 6 additions & 1 deletion db/background_mgr_attachment_migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,14 @@ func (a *AttachmentMigrationManager) Init(ctx context.Context, options map[strin
var statusDoc AttachmentMigrationManagerStatusDoc
err := base.JSONUnmarshal(clusterStatus, &statusDoc)

reset, ok := options["reset"].(bool)
if reset && ok {
base.InfofCtx(ctx, base.KeyAll, "Resync: Resetting resync process. Will not resume any partially completed process")
}

// If the previous run completed, or there was an error during unmarshalling the status we will start the
// process from scratch with a new migration ID. Otherwise, we should resume with the migration ID, stats specified in the doc.
if statusDoc.State == BackgroundProcessStateCompleted || err != nil {
if statusDoc.State == BackgroundProcessStateCompleted || err != nil || (reset && ok) {
return newRunInit()
}
a.MigrationID = statusDoc.MigrationID
Expand Down
2 changes: 2 additions & 0 deletions docs/api/admin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ paths:
$ref: ./paths/admin/_all_dbs.yaml
'/{db}/_compact':
$ref: './paths/admin/db-_compact.yaml'
'/{.db}/_attachment_migration':
$ref: './paths/admin/db-_attachment_migration.yaml'
'/{db}/':
$ref: './paths/admin/db-.yaml'
'/{keyspace}/':
Expand Down
35 changes: 35 additions & 0 deletions docs/api/components/schemas.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1988,6 +1988,41 @@ Resync-status:
- docs_changed
- docs_processed
title: Resync-status
Attachment-Migration-status:
description: The status of a attachment migration operation
type: object
properties:
status:
description: The status of the current attachment migration operation.
type: string
enum:
- running
- completed
- stopping
- stopped
- error
start_time:
description: The ISO-8601 date and time the attachment migration operation was started.
type: string
last_error:
description: The last error that occurred in the attachment migration operation (if any).
type: string
migration_id:
description: The UUID given to the attachment migration operation.
type: string
docs_changed:
description: The amount of documents that have had attachment metadata migrated as a result of attachment migration operation.
type: integer
docs_processed:
description: The amount of docs that have been processed through the attachment migration operation.
type: integer
required:
- status
- start_time
- last_error
- docs_changed
- docs_processed
title: Attachment-Migration-status
Compact-status:
description: The status returned from a compaction.
type: object
Expand Down
73 changes: 73 additions & 0 deletions docs/api/paths/admin/db-_attachment_migration.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Copyright 2022-Present Couchbase, Inc.
#
# Use of this software is governed by the Business Source License included
# in the file licenses/BSL-Couchbase.txt. As of the Change Date specified
# in that file, in accordance with the Business Source License, use of this
# software will be governed by the Apache License, Version 2.0, included in
# the file licenses/APL2.txt.
parameters:
- $ref: ../../components/parameters.yaml#/db
post:
summary: Manage a attachment migration operation
description: |-
This allows a new attachment migration operation to be done on the database, or to stop an existing running attachment migration operation.
Attachment Migration is a single node process and can only one node can be running it at one point.

Check failure on line 15 in docs/api/paths/admin/db-_attachment_migration.yaml

View workflow job for this annotation

GitHub Actions / yamllint

15:104 [trailing-spaces] trailing spaces

Check failure on line 16 in docs/api/paths/admin/db-_attachment_migration.yaml

View workflow job for this annotation

GitHub Actions / yamllint

16:1 [trailing-spaces] trailing spaces
Required Sync Gateway RBAC roles:
* Sync Gateway Architect
parameters:

Check warning on line 20 in docs/api/paths/admin/db-_attachment_migration.yaml

View workflow job for this annotation

GitHub Actions / OpenAPI Validation

The operation does not define the path parameter `{.db}` expected by path `/{.db}/_attachment_migration`.

Check warning on line 20 in docs/api/paths/admin/db-_attachment_migration.yaml

View workflow job for this annotation

GitHub Actions / OpenAPI Validation

The operation does not define the path parameter `{.db}` expected by path `/{.db}/_attachment_migration`.
- name: action
in: query
description: Defines whether the an attachment migration operation is being started or stopped.
schema:
type: string
default: start
enum:
- start
- stop
- name: reset
in: query
description: |-
This forces a fresh attachment migration start instead of trying to resume the previous failed migration operation.
schema:
type: boolean
responses:
'200':
description: Started or stopped compact operation successfully
'400':
$ref: ../../components/responses.yaml#/request-problem
'404':
$ref: ../../components/responses.yaml#/Not-found
'503':
description: Cannot start attachment migration due to another migration operation still running.
content:
application/json:
schema:
$ref: ../../components/schemas.yaml#/HTTP-Error
tags:
- Database Management
operationId: post_db-_attachment_migration
get:

Check warning on line 52 in docs/api/paths/admin/db-_attachment_migration.yaml

View workflow job for this annotation

GitHub Actions / OpenAPI Validation

The operation does not define the path parameter `{.db}` expected by path `/{.db}/_attachment_migration`.

Check warning on line 52 in docs/api/paths/admin/db-_attachment_migration.yaml

View workflow job for this annotation

GitHub Actions / OpenAPI Validation

The operation does not define the path parameter `{.db}` expected by path `/{.db}/_attachment_migration`.
summary: Get the status of the most recent attachment migration operation
description: |-
This will retrieve the current status of the most recent attachment migration operation.
Required Sync Gateway RBAC roles:
* Sync Gateway Architect
responses:
'200':
description: Attachment migration status retrieved successfully
content:
application/json:
schema:
$ref: ../../components/schemas.yaml#/Attachment-Migration-status
'400':
$ref: ../../components/responses.yaml#/request-problem
'404':
$ref: ../../components/responses.yaml#/Not-found
tags:
- Database Management
operationId: get_db-_attachment_migration
46 changes: 46 additions & 0 deletions rest/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,52 @@ func (h *handler) handleGetCompact() error {
return nil
}

func (h *handler) handleAttachmentMigration() error {
action := h.getQuery("action")
if action == "" {
action = string(db.BackgroundProcessActionStart)
}
reset := h.getBoolQuery("reset")

if action != string(db.BackgroundProcessActionStart) && action != string(db.BackgroundProcessActionStop) {
return base.HTTPErrorf(http.StatusBadRequest, "Unknown parameter for 'action'. Must be start or stop")
}

if action == string(db.BackgroundProcessActionStart) {
err := h.db.AttachmentMigrationManager.Start(h.ctx(), map[string]interface{}{
"reset": reset,
})
if err != nil {
return err
}
status, err := h.db.AttachmentMigrationManager.GetStatus(h.ctx())
if err != nil {
return err
}
h.writeRawJSON(status)
} else if action == string(db.BackgroundProcessActionStop) {
err := h.db.AttachmentMigrationManager.Stop()
if err != nil {
return err
}
status, err := h.db.AttachmentMigrationManager.GetStatus(h.ctx())
if err != nil {
return err
}
h.writeRawJSON(status)
}
return nil
}

func (h *handler) handleGetAttachmentMigration() error {
status, err := h.db.AttachmentMigrationManager.GetStatus(h.ctx())
if err != nil {
return err
}
h.writeRawJSON(status)
return nil
}

func (h *handler) handleCompact() error {
action := h.getQuery("action")
if action == "" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func TestAttachmentCompactionAPI(t *testing.T) {
resp = rt.SendAdminRequest("POST", "/{{.db}}/_compact?type=attachment", "")
rest.RequireStatus(t, resp, http.StatusServiceUnavailable)

// Wait for run to complete
// Wait for run to completes
err = rt.WaitForCondition(func() bool {
time.Sleep(1 * time.Second)

Expand Down
Loading

0 comments on commit ff9dc68

Please sign in to comment.