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

CBG-3467 add verbose mode for _all_dbs #6493

Merged
merged 5 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
29 changes: 29 additions & 0 deletions docs/api/components/schemas.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2360,6 +2360,9 @@ Status:
enum:
- Online
- Offline
- Starting
- Stopping
- Resyncing
replication_status:
type: array
items:
Expand Down Expand Up @@ -2446,3 +2449,29 @@ CollectionNames:
type: array
items:
type: string
"All DBs":
description: "The names of all databases."
type: array
items:
type: string
"All DBs Verbose":
description: "Description of all databases."
type: array
items:
type: object
properties:
db_name:
type: string
description: The name of the database.
bucket:
type: string
description: The Couchbase Server backing bucket for the database.
state:
description: Whether the database is online or offline.
torcolvin marked this conversation as resolved.
Show resolved Hide resolved
type: string
enum:
- Online
- Offline
- Starting
- Stopping
- Resyncing
25 changes: 9 additions & 16 deletions docs/api/paths/admin/_all_dbs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,25 @@
get:
summary: Get a list of all the databases
description: |-
This retrieves all the database's names that are in the current Sync Gateway node.
This retrieves all the databases that are in the current Sync Gateway node. If verbose, return bucket and status information about the databases, otherwise return the names.
torcolvin marked this conversation as resolved.
Show resolved Hide resolved

Required Sync Gateway RBAC roles:

* Sync Gateway Dev Ops
parameters:
- name: verbose
in: query
schema:
type: boolean
responses:
'200':
description: Successfully retrieved all database names
content:
application/json:
schema:
type: array
items:
type: string
oneOf:
- $ref: "../../components/schemas.yaml#/All DBs"
- $ref: "../../components/schemas.yaml#/All DBs Verbose"
tags:
- Database Management
operationId: get__all_dbs
head:
summary: /_all_dbs
description: |-
Required Sync Gateway RBAC roles:

* Sync Gateway Dev Ops
responses:
'200':
description: OK
tags:
- Database Management
operationId: head__all_dbs
10 changes: 10 additions & 0 deletions rest/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ func (h *handler) handlePing() error {
}

func (h *handler) handleAllDbs() error {
if h.getBoolQuery("verbose") {
h.writeJSON(h.server.allDatabaseSummaries())
return nil
}
h.writeJSON(h.server.AllDatabaseNames())
return nil
}
Expand Down Expand Up @@ -403,6 +407,12 @@ type DatabaseRoot struct {
Scopes map[string]databaseRootScope `json:"scopes,omitempty"` // stats for each scope/collection
}

type dbSummary struct {
DBName string `json:"db_name"`
Bucket string `json:"bucket"`
State string `json:"state"`
}

type databaseRootScope struct {
Collections map[string]databaseRootCollection `json:"collections,omitempty"`
}
Expand Down
13 changes: 13 additions & 0 deletions rest/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2861,3 +2861,16 @@ func TestPing(t *testing.T) {
}
}
}

func TestAllDbs(t *testing.T) {
rt := NewRestTester(t, nil)
defer rt.Close()

resp := rt.SendAdminRequest(http.MethodGet, "/_all_dbs", "")
RequireStatus(t, resp, http.StatusOK)
require.Equal(t, fmt.Sprintf(`["%s"]`, rt.GetDatabase().Name), resp.Body.String())

resp = rt.SendAdminRequest(http.MethodGet, "/_all_dbs?verbose=true", "")
RequireStatus(t, resp, http.StatusOK)
require.Equal(t, fmt.Sprintf(`[{"db_name":"%s","bucket":"%s","state":"Online"}]`, rt.GetDatabase().Name, rt.GetDatabase().Bucket.GetName()), resp.Body.String())
}
24 changes: 24 additions & 0 deletions rest/server_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (

"github.com/couchbase/sync_gateway/auth"
"github.com/couchbase/sync_gateway/db/functions"
"golang.org/x/exp/slices"

"github.com/couchbase/gocbcore/v10"
sgbucket "github.com/couchbase/sg-bucket"
Expand Down Expand Up @@ -334,9 +335,32 @@ func (sc *ServerContext) AllDatabaseNames() []string {
for name := range sc.databases_ {
names = append(names, name)
}
slices.Sort(names)
return names
}

func (sc *ServerContext) allDatabaseSummaries() []dbSummary {
sc.lock.RLock()
defer sc.lock.RUnlock()

names := make([]string, 0, len(sc.databases_))
for name := range sc.databases_ {
names = append(names, name)
}
slices.Sort(names)

dbs := make([]dbSummary, 0, len(sc.databases_))
for _, name := range names {
dbctx := sc.databases_[name]
dbs = append(dbs, dbSummary{
DBName: name,
Bucket: dbctx.Bucket.GetName(),
State: db.RunStateString[atomic.LoadUint32(&dbctx.State)],
})
}
torcolvin marked this conversation as resolved.
Show resolved Hide resolved
return dbs
}

// AllDatabases returns a copy of the databases_ map.
func (sc *ServerContext) AllDatabases() map[string]*db.DatabaseContext {
sc.lock.RLock()
Expand Down