fix: improve virtual DBRP default handling (#23626)

pull/23648/head
Andrew Lee 2022-08-16 14:02:31 -06:00 committed by GitHub
parent 90d45e8709
commit 187f991d54
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 15 deletions

View File

@ -238,7 +238,7 @@ func (s *Service) FindByID(ctx context.Context, orgID, id platform.ID) (*influxd
if err != nil || b == nil {
return nil, ErrDBRPNotFound
}
return bucketToMapping(b, true), nil
return bucketToMapping(b), nil
}
return nil, err
}
@ -370,24 +370,28 @@ func (s *Service) FindMany(ctx context.Context, filter influxdb.DBRPMappingFilte
if bucket == nil {
continue
}
isDefault := true
// check if this virtual mapping has been overriden by a custom, physical mapping
for _, m := range ms {
if m.BucketID == bucket.ID {
isDefault = false
break
newMapping := bucketToMapping(bucket)
// if any bucket already exists that is default for this database,
// this virtual mapping should not be the default
if newMapping.Default {
for _, m := range ms {
if m.Database == newMapping.Database && m.Default {
newMapping.Default = false
break
}
}
}
mapping := bucketToMapping(bucket, isDefault)
if filterFunc(mapping, filter) {
ms = append(ms, mapping)
if filterFunc(newMapping, filter) {
ms = append(ms, newMapping)
}
}
return ms, len(ms), nil
}
func bucketToMapping(bucket *influxdb.Bucket, isDefault bool) *influxdb.DBRPMapping {
// bucketToMapping converts a bucket to a DBRP mapping.
// Default if bucket name does not contain a slash (foo/bar)
func bucketToMapping(bucket *influxdb.Bucket) *influxdb.DBRPMapping {
if bucket == nil {
return nil
}
@ -396,7 +400,7 @@ func bucketToMapping(bucket *influxdb.Bucket, isDefault bool) *influxdb.DBRPMapp
db, rp := parseDBRP(bucket.Name)
return &influxdb.DBRPMapping{
ID: dbrpID,
Default: isDefault,
Default: bucket.Name == db,
Database: db,
RetentionPolicy: rp,
OrganizationID: bucket.OrgID,

View File

@ -669,7 +669,7 @@ func FindManyDBRPMappingsV2(
ID: 200,
Database: "testdb2",
RetentionPolicy: "testrp2",
Default: true,
Default: false,
Virtual: true,
OrganizationID: MustIDBase16(dbrpOrg3ID),
BucketID: 200,
@ -698,6 +698,7 @@ func FindManyDBRPMappingsV2(
// org 2
{ID: 300, Name: "testdb3", OrgID: MustIDBase16(dbrpOrg2ID)},
{ID: 400, Name: "testdb4/testrp4", OrgID: MustIDBase16(dbrpOrg2ID)},
{ID: 500, Name: "testdb4", OrgID: MustIDBase16(dbrpOrg2ID)},
}, 0, nil
}},
DBRPMappingsV2: []*influxdb.DBRPMapping{},
@ -709,11 +710,20 @@ func FindManyDBRPMappingsV2(
},
wants: wants{
dbrpMappings: []*influxdb.DBRPMapping{
{
ID: 500,
Database: "testdb4",
RetentionPolicy: "autogen",
Default: true,
Virtual: true,
OrganizationID: MustIDBase16(dbrpOrg2ID),
BucketID: 500,
},
{
ID: 400,
Database: "testdb4",
RetentionPolicy: "testrp4",
Default: true,
Default: false,
Virtual: true,
OrganizationID: MustIDBase16(dbrpOrg2ID),
BucketID: 400,
@ -1244,7 +1254,7 @@ func FindDBRPMappingByIDV2(
ID: MustIDBase16(dbrpBucketAID),
Database: "testdb",
RetentionPolicy: "testrp",
Default: true,
Default: false,
Virtual: true,
OrganizationID: MustIDBase16(dbrpOrg3ID),
BucketID: MustIDBase16(dbrpBucketAID),