Skip to content

Commit 5b26d82

Browse files
gregns1bbrks
andcommitted
CBG-4213: add attachment migration api (#7183)
* CBG-4213: add attachment migration api * tidy up * fix api docs lint error * Update db/background_mgr_attachment_migration.go Co-authored-by: Ben Brooks <ben.brooks@couchbase.com> * Update db/background_mgr_attachment_migration.go Co-authored-by: Ben Brooks <ben.brooks@couchbase.com> --------- Co-authored-by: Ben Brooks <ben.brooks@couchbase.com>
1 parent a46ea42 commit 5b26d82

File tree

10 files changed

+487
-26
lines changed

10 files changed

+487
-26
lines changed

db/background_mgr_attachment_migration.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,14 @@ func (a *AttachmentMigrationManager) Init(ctx context.Context, options map[strin
6666
var statusDoc AttachmentMigrationManagerStatusDoc
6767
err := base.JSONUnmarshal(clusterStatus, &statusDoc)
6868

69+
reset, _ := options["reset"].(bool)
70+
if reset {
71+
base.InfofCtx(ctx, base.KeyAll, "Attachment Migration: Resetting migration process. Will not resume any partially completed process")
72+
}
73+
6974
// If the previous run completed, or there was an error during unmarshalling the status we will start the
7075
// process from scratch with a new migration ID. Otherwise, we should resume with the migration ID, stats specified in the doc.
71-
if statusDoc.State == BackgroundProcessStateCompleted || err != nil {
76+
if statusDoc.State == BackgroundProcessStateCompleted || err != nil || reset {
7277
return newRunInit()
7378
}
7479
a.MigrationID = statusDoc.MigrationID

docs/api/admin.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ paths:
124124
$ref: ./paths/admin/_all_dbs.yaml
125125
'/{db}/_compact':
126126
$ref: './paths/admin/db-_compact.yaml'
127+
'/{db}/_attachment_migration':
128+
$ref: './paths/admin/db-_attachment_migration.yaml'
127129
'/{db}/':
128130
$ref: './paths/admin/db-.yaml'
129131
'/{keyspace}/':

docs/api/components/schemas.yaml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2099,6 +2099,41 @@ Resync-status:
20992099
- docs_changed
21002100
- docs_processed
21012101
title: Resync-status
2102+
Attachment-Migration-status:
2103+
description: The status of a attachment migration operation
2104+
type: object
2105+
properties:
2106+
status:
2107+
description: The status of the current attachment migration operation.
2108+
type: string
2109+
enum:
2110+
- running
2111+
- completed
2112+
- stopping
2113+
- stopped
2114+
- error
2115+
start_time:
2116+
description: The ISO-8601 date and time the attachment migration operation was started.
2117+
type: string
2118+
last_error:
2119+
description: The last error that occurred in the attachment migration operation (if any).
2120+
type: string
2121+
migration_id:
2122+
description: The UUID given to the attachment migration operation.
2123+
type: string
2124+
docs_changed:
2125+
description: The amount of documents that have had attachment metadata migrated as a result of attachment migration operation.
2126+
type: integer
2127+
docs_processed:
2128+
description: The amount of docs that have been processed through the attachment migration operation.
2129+
type: integer
2130+
required:
2131+
- status
2132+
- start_time
2133+
- last_error
2134+
- docs_changed
2135+
- docs_processed
2136+
title: Attachment-Migration-status
21022137
Compact-status:
21032138
description: The status returned from a compaction.
21042139
type: object
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Copyright 2024-Present Couchbase, Inc.
2+
#
3+
# Use of this software is governed by the Business Source License included
4+
# in the file licenses/BSL-Couchbase.txt. As of the Change Date specified
5+
# in that file, in accordance with the Business Source License, use of this
6+
# software will be governed by the Apache License, Version 2.0, included in
7+
# the file licenses/APL2.txt.
8+
parameters:
9+
- $ref: ../../components/parameters.yaml#/db
10+
post:
11+
summary: Manage a attachment migration operation
12+
description: |-
13+
This allows a new attachment migration operation to be done on the database, or to stop an existing running attachment migration operation.
14+
15+
Attachment Migration is a single node process and can only one node can be running it at one point.
16+
17+
Required Sync Gateway RBAC roles:
18+
19+
* Sync Gateway Architect
20+
parameters:
21+
- name: action
22+
in: query
23+
description: Defines whether the an attachment migration operation is being started or stopped.
24+
schema:
25+
type: string
26+
default: start
27+
enum:
28+
- start
29+
- stop
30+
- name: reset
31+
in: query
32+
description: |-
33+
This forces a fresh attachment migration start instead of trying to resume the previous failed migration operation.
34+
schema:
35+
type: boolean
36+
responses:
37+
'200':
38+
description: Started or stopped compact operation successfully
39+
'400':
40+
$ref: ../../components/responses.yaml#/request-problem
41+
'404':
42+
$ref: ../../components/responses.yaml#/Not-found
43+
'503':
44+
description: Cannot start attachment migration due to another migration operation still running.
45+
content:
46+
application/json:
47+
schema:
48+
$ref: ../../components/schemas.yaml#/HTTP-Error
49+
tags:
50+
- Database Management
51+
operationId: post_db-_attachment_migration
52+
get:
53+
summary: Get the status of the most recent attachment migration operation
54+
description: |-
55+
This will retrieve the current status of the most recent attachment migration operation.
56+
57+
Required Sync Gateway RBAC roles:
58+
59+
* Sync Gateway Architect
60+
responses:
61+
'200':
62+
description: Attachment migration status retrieved successfully
63+
content:
64+
application/json:
65+
schema:
66+
$ref: ../../components/schemas.yaml#/Attachment-Migration-status
67+
'400':
68+
$ref: ../../components/responses.yaml#/request-problem
69+
'404':
70+
$ref: ../../components/responses.yaml#/Not-found
71+
tags:
72+
- Database Management
73+
operationId: get_db-_attachment_migration

rest/api.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,52 @@ func (h *handler) handleGetCompact() error {
130130
return nil
131131
}
132132

133+
func (h *handler) handleAttachmentMigration() error {
134+
action := h.getQuery("action")
135+
if action == "" {
136+
action = string(db.BackgroundProcessActionStart)
137+
}
138+
reset := h.getBoolQuery("reset")
139+
140+
if action != string(db.BackgroundProcessActionStart) && action != string(db.BackgroundProcessActionStop) {
141+
return base.HTTPErrorf(http.StatusBadRequest, "Unknown parameter for 'action'. Must be start or stop")
142+
}
143+
144+
if action == string(db.BackgroundProcessActionStart) {
145+
err := h.db.AttachmentMigrationManager.Start(h.ctx(), map[string]interface{}{
146+
"reset": reset,
147+
})
148+
if err != nil {
149+
return err
150+
}
151+
status, err := h.db.AttachmentMigrationManager.GetStatus(h.ctx())
152+
if err != nil {
153+
return err
154+
}
155+
h.writeRawJSON(status)
156+
} else if action == string(db.BackgroundProcessActionStop) {
157+
err := h.db.AttachmentMigrationManager.Stop()
158+
if err != nil {
159+
return err
160+
}
161+
status, err := h.db.AttachmentMigrationManager.GetStatus(h.ctx())
162+
if err != nil {
163+
return err
164+
}
165+
h.writeRawJSON(status)
166+
}
167+
return nil
168+
}
169+
170+
func (h *handler) handleGetAttachmentMigration() error {
171+
status, err := h.db.AttachmentMigrationManager.GetStatus(h.ctx())
172+
if err != nil {
173+
return err
174+
}
175+
h.writeRawJSON(status)
176+
return nil
177+
}
178+
133179
func (h *handler) handleCompact() error {
134180
action := h.getQuery("action")
135181
if action == "" {

0 commit comments

Comments
 (0)