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

fix: Don't include duplicates for SHOW DATABASES #20017

Merged
merged 6 commits into from
Nov 16, 2020
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: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
1. [19991](https://github.com/influxdata/influxdb/pull/19991): Use --skip-verify flag for backup/restore CLI command.
1. [19995](https://github.com/influxdata/influxdb/pull/19995): Don't auto-print help on influxd errors
1. [20012](https://github.com/influxdata/influxdb/pull/20012): Validate input paths to `influxd upgrade` up-front
1. [20017](https://github.com/influxdata/influxdb/pull/20017): Don't include duplicates for SHOW DATABASES

## v2.0.1 [2020-11-10]

Expand Down
62 changes: 62 additions & 0 deletions influxql/_v1tests/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import (
"strings"
"testing"
"time"

"github.com/influxdata/influxdb/v2"
icontext "github.com/influxdata/influxdb/v2/context"
"github.com/influxdata/influxdb/v2/tests"
"github.com/stretchr/testify/require"
)

// Ensure parameterized queries can be executed
Expand Down Expand Up @@ -82,3 +87,60 @@ func TestServer_Query_Chunked(t *testing.T) {
ctx := context.Background()
test.Run(ctx, t, s)
}

func TestServer_Query_ShowDatabases(t *testing.T) {
t.Parallel()
s := OpenServer(t)
defer s.MustClose()

ctx := context.Background()
ctx = icontext.SetAuthorizer(ctx, tests.MakeAuthorization(s.DefaultOrgID, s.DefaultUserID, influxdb.OperPermissions()))

// create some buckets and mappings
buckets := []struct {
name string
db string
rp string
}{
{"my-bucket", "my-bucket", "autogen"},
{"telegraf/autogen", "telegraf", "autogen"},
{"telegraf/1_week", "telegraf", "1_week"},
{"telegraf/1_month", "telegraf", "1_month"},
}

for _, bi := range buckets {
b := influxdb.Bucket{
OrgID: s.DefaultOrgID,
Type: influxdb.BucketTypeUser,
Name: bi.name,
RetentionPeriod: 0,
}
err := s.Launcher.
Launcher.
BucketService().
CreateBucket(ctx, &b)
require.NoError(t, err)

err = s.Launcher.
DBRPMappingServiceV2().
Create(ctx, &influxdb.DBRPMappingV2{
Database: bi.db,
RetentionPolicy: bi.rp,
Default: true,
OrganizationID: s.DefaultOrgID,
BucketID: b.ID,
})
require.NoError(t, err)
}

test := NewEmptyTest()
test.addQueries(
&Query{
name: "show databases does not return duplicates",
command: "SHOW DATABASES",
exp: `{"results":[{"statement_id":0,"series":[{"name":"databases","columns":["name"],"values":[["my-bucket"],["telegraf"]]}]}]}`,
},
)

test.Run(context.Background(), t, s)
}
65 changes: 34 additions & 31 deletions influxql/_v1tests/server_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,14 @@ type Write struct {
type Writes []*Write

type Test struct {
orgID influxdb.ID
bucketID influxdb.ID
db string
rp string
writes Writes
queries []*Query
orgID influxdb.ID
bucketID influxdb.ID
db string
rp string
writes Writes
queries []*Query
noDefaultMapping bool
noWrites bool
}

func NewTest(db, rp string) Test {
Expand All @@ -103,6 +105,12 @@ func NewTest(db, rp string) Test {
}
}

// NewEmptyTest creates an empty test without a default database and retention policy mapping or
// any expected writes.
func NewEmptyTest() Test {
return Test{noDefaultMapping: true, noWrites: true}
}

func (qt *Test) Run(ctx context.Context, t *testing.T, p *tests.DefaultPipeline) {
t.Helper()
fx, auth := qt.init(ctx, t, p)
Expand Down Expand Up @@ -146,37 +154,32 @@ func (qt *Test) addQueries(q ...*Query) {
func (qt *Test) init(ctx context.Context, t *testing.T, p *tests.DefaultPipeline) (fx pipeline.BaseFixture, auth *influxdb.Authorization) {
t.Helper()

require.Greater(t, len(qt.writes), 0)

qt.orgID = p.DefaultOrgID
qt.bucketID = p.DefaultBucketID

fx = pipeline.NewBaseFixture(t, p.Pipeline, qt.orgID, qt.bucketID)

qt.writeTestData(ctx, t, fx.Admin)
p.Flush()

writeOrg, err := influxdb.NewPermissionAtID(qt.orgID, influxdb.WriteAction, influxdb.OrgsResourceType, qt.orgID)
require.NoError(t, err)

bucketWritePerm, err := influxdb.NewPermissionAtID(qt.bucketID, influxdb.WriteAction, influxdb.BucketsResourceType, qt.orgID)
require.NoError(t, err)

bucketReadPerm, err := influxdb.NewPermissionAtID(qt.bucketID, influxdb.ReadAction, influxdb.BucketsResourceType, qt.orgID)
require.NoError(t, err)
if !qt.noWrites {
require.Greater(t, len(qt.writes), 0)
qt.writeTestData(ctx, t, fx.Admin)
p.Flush()
}

auth = tests.MakeAuthorization(qt.orgID, p.DefaultUserID, []influxdb.Permission{*writeOrg, *bucketWritePerm, *bucketReadPerm})
ctx = icontext.SetAuthorizer(ctx, auth)
err = p.Launcher.
DBRPMappingServiceV2().
Create(ctx, &influxdb.DBRPMappingV2{
Database: qt.db,
RetentionPolicy: qt.rp,
Default: true,
OrganizationID: qt.orgID,
BucketID: qt.bucketID,
})
require.NoError(t, err)
auth = tests.MakeAuthorization(qt.orgID, p.DefaultUserID, influxdb.OperPermissions())

if !qt.noDefaultMapping {
ctx = icontext.SetAuthorizer(ctx, auth)
err := p.Launcher.
DBRPMappingServiceV2().
Create(ctx, &influxdb.DBRPMappingV2{
Database: qt.db,
RetentionPolicy: qt.rp,
Default: true,
OrganizationID: qt.orgID,
BucketID: qt.bucketID,
})
require.NoError(t, err)
}

return
}
Expand Down
50 changes: 14 additions & 36 deletions tests/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import (

"github.com/influxdata/flux/csv"
"github.com/influxdata/influxdb/v2"
"github.com/influxdata/influxdb/v2/authorization"
influxhttp "github.com/influxdata/influxdb/v2/http"
"github.com/influxdata/influxdb/v2/pkg/httpc"
"github.com/influxdata/influxdb/v2/tenant"
)

type ClientConfig struct {
Expand All @@ -32,6 +34,11 @@ type Client struct {
Client *httpc.Client
*influxhttp.Service

*authorization.AuthorizationClientService
*tenant.BucketClientService
*tenant.OrgClientService
*tenant.UserClientService

ClientConfig
}

Expand All @@ -52,9 +59,13 @@ func NewClient(endpoint string, config ClientConfig) (*Client, error) {
return nil, err
}
return &Client{
Client: hc,
Service: svc,
ClientConfig: config,
Client: hc,
Service: svc,
AuthorizationClientService: &authorization.AuthorizationClientService{Client: hc},
BucketClientService: &tenant.BucketClientService{Client: hc},
OrgClientService: &tenant.OrgClientService{Client: hc},
UserClientService: &tenant.UserClientService{Client: hc},
ClientConfig: config,
}, nil
}

Expand Down Expand Up @@ -186,27 +197,6 @@ func (c *Client) MustCreateLabel(t *testing.T) influxdb.ID {
return l.ID
}

// MustCreateDocument creates a document or is a fatal error.
// Used in tests where the content of the document does not matter.
func (c *Client) MustCreateDocument(t *testing.T) influxdb.ID {
t.Helper()

d := &influxdb.Document{
ID: influxdb.ID(12123434),
Meta: influxdb.DocumentMeta{
Name: "n1",
Type: "t1",
},
Content: "howdy",
}

err := c.CreateDocument(context.Background(), c.DocumentsNamespace, c.OrgID, d)
if err != nil {
t.Fatalf("unable to create document: %v", err)
}
return d.ID
}

// MustCreateCheck creates a check or is a fatal error.
// Used in tests where the content of the check does not matter.
func (c *Client) MustCreateCheck(t *testing.T) influxdb.ID {
Expand Down Expand Up @@ -372,8 +362,6 @@ func (c *Client) MustCreateResource(t *testing.T, r influxdb.ResourceType) influ
return c.MustCreateLabel(t)
case influxdb.ViewsResourceType: // 12
t.Skip("Are views still a thing?")
case influxdb.DocumentsResourceType: // 13
return c.MustCreateDocument(t)
case influxdb.NotificationRuleResourceType: // 14
return c.MustCreateNotificationRule(t)
case influxdb.NotificationEndpointResourceType: // 15
Expand Down Expand Up @@ -416,8 +404,6 @@ func (c *Client) DeleteResource(t *testing.T, r influxdb.ResourceType, id influx
return c.DeleteLabel(ctx, id)
case influxdb.ViewsResourceType: // 12
t.Skip("Are views still a thing?")
case influxdb.DocumentsResourceType: // 13
return c.DeleteDocument(ctx, c.DocumentsNamespace, id)
case influxdb.NotificationRuleResourceType: // 14
return c.DeleteNotificationRule(ctx, id)
case influxdb.NotificationEndpointResourceType: // 15
Expand Down Expand Up @@ -514,14 +500,6 @@ func (c *Client) FindAll(t *testing.T, r influxdb.ResourceType) ([]influxdb.ID,
}
case influxdb.ViewsResourceType: // 12
t.Skip("Are views still a thing?")
case influxdb.DocumentsResourceType: // 13
rs, err := c.GetDocuments(ctx, c.DocumentsNamespace, c.OrgID)
if err != nil {
return nil, err
}
for _, r := range rs {
ids = append(ids, r.ID)
}
case influxdb.NotificationRuleResourceType: // 14
rs, _, err := c.FindNotificationRules(ctx, influxdb.NotificationRuleFilter{OrgID: &c.OrgID})
if err != nil {
Expand Down
8 changes: 7 additions & 1 deletion v1/coordinator/statement_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,14 +317,19 @@ func (e *StatementExecutor) createIterators(ctx context.Context, stmt *influxql.

func (e *StatementExecutor) executeShowDatabasesStatement(ctx context.Context, q *influxql.ShowDatabasesStatement, ectx *query.ExecutionContext) (models.Rows, error) {
row := &models.Row{Name: "databases", Columns: []string{"name"}}
// TODO(gianarb): How pagination works here?
dbrps, _, err := e.DBRP.FindMany(ctx, influxdb.DBRPMappingFilterV2{
OrgID: &ectx.OrgID,
})
if err != nil {
return nil, err
}

seenDbs := make(map[string]struct{}, len(dbrps))
for _, dbrp := range dbrps {
if _, ok := seenDbs[dbrp.Database]; ok {
continue
}

perm, err := influxdb.NewPermissionAtID(dbrp.BucketID, influxdb.ReadAction, influxdb.BucketsResourceType, dbrp.OrganizationID)
if err != nil {
return nil, err
Expand All @@ -336,6 +341,7 @@ func (e *StatementExecutor) executeShowDatabasesStatement(ctx context.Context, q
}
return nil, err
}
seenDbs[dbrp.Database] = struct{}{}
row.Values = append(row.Values, []interface{}{dbrp.Database})
}
return []*models.Row{row}, nil
Expand Down