Skip to content

Commit

Permalink
CBG-3467 add verbose mode for _all_dbs (#6493)
Browse files Browse the repository at this point in the history
  • Loading branch information
torcolvin committed Oct 5, 2023
1 parent 595a2aa commit 65bc672
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 16 deletions.
29 changes: 29 additions & 0 deletions docs/api/components/schemas.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2314,6 +2314,9 @@ Status:
enum:
- Online
- Offline
- Starting
- Stopping
- Resyncing
replication_status:
type: array
items:
Expand Down Expand Up @@ -2400,3 +2403,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: The database state.
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, returns bucket and state information for each database, otherwise returns names only.
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 @@ -2802,3 +2802,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())
}
21 changes: 21 additions & 0 deletions rest/server_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"net"
"net/http"
"os"
"sort"
"strconv"
"strings"
"sync"
Expand All @@ -28,6 +29,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 @@ -315,9 +317,28 @@ 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()

dbs := make([]dbSummary, 0, len(sc.databases_))
for name, dbctx := range sc.databases_ {
dbs = append(dbs, dbSummary{
DBName: name,
Bucket: dbctx.Bucket.GetName(),
State: db.RunStateString[atomic.LoadUint32(&dbctx.State)],
})
}
sort.Slice(dbs, func(i, j int) bool {
return dbs[i].DBName < dbs[j].DBName
})
return dbs
}

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

0 comments on commit 65bc672

Please sign in to comment.