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

[v11] Improve performance of ListResources #23597

Merged
merged 1 commit into from
Mar 28, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions api/types/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,17 @@ func (a *AppV3) SetDynamicLabels(dl map[string]CommandLabel) {
a.Spec.DynamicLabels = LabelsToV2(dl)
}

// GetLabel retrieves the label with the provided key. If not found
// value will be empty and ok will be false.
func (a *AppV3) GetLabel(key string) (value string, ok bool) {
if cmd, ok := a.Spec.DynamicLabels[key]; ok {
return cmd.Result, ok
}

v, ok := a.Metadata.Labels[key]
return v, ok
}

// GetAllLabels returns the app combined static and dynamic labels.
func (a *AppV3) GetAllLabels() map[string]string {
return CombineLabels(a.Metadata.Labels, a.Spec.DynamicLabels)
Expand Down
13 changes: 13 additions & 0 deletions api/types/appserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,19 @@ func (s *AppServerV3) SetProxyIDs(proxyIDs []string) {
s.Spec.ProxyIDs = proxyIDs
}

// GetLabel retrieves the label with the provided key. If not found
// value will be empty and ok will be false.
func (s *AppServerV3) GetLabel(key string) (value string, ok bool) {
if s.Spec.App != nil {
if v, ok := s.Spec.App.GetLabel(key); ok {
return v, ok
}
}

v, ok := s.Metadata.Labels[key]
return v, ok
}

// GetAllLabels returns all resource's labels. Considering:
// * Static labels from `Metadata.Labels` and `Spec.App`.
// * Dynamic labels from `Spec.App.Spec`.
Expand Down
7 changes: 7 additions & 0 deletions api/types/connection_diagnostic.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ func (c *ConnectionDiagnosticV1) CheckAndSetDefaults() error {
return nil
}

// GetLabel retrieves the label with the provided key. If not found
// value will be empty and ok will be false.
func (c *ConnectionDiagnosticV1) GetLabel(key string) (val string, ok bool) {
v, ok := c.Metadata.Labels[key]
return v, ok
}

// GetAllLabels returns combined static and dynamic labels.
func (c *ConnectionDiagnosticV1) GetAllLabels() map[string]string {
return CombineLabels(c.Metadata.Labels, nil)
Expand Down
11 changes: 11 additions & 0 deletions api/types/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,17 @@ func (d *DatabaseV3) SetDynamicLabels(dl map[string]CommandLabel) {
d.Spec.DynamicLabels = LabelsToV2(dl)
}

// GetLabel retrieves the label with the provided key. If not found
// value will be empty and ok will be false.
func (d *DatabaseV3) GetLabel(key string) (value string, ok bool) {
if cmd, ok := d.Spec.DynamicLabels[key]; ok {
return cmd.Result, ok
}

v, ok := d.Metadata.Labels[key]
return v, ok
}

// GetAllLabels returns the database combined static and dynamic labels.
func (d *DatabaseV3) GetAllLabels() map[string]string {
return CombineLabels(d.Metadata.Labels, d.Spec.DynamicLabels)
Expand Down
13 changes: 13 additions & 0 deletions api/types/databaseserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,19 @@ func (s *DatabaseServerV3) SetOrigin(origin string) {
s.Metadata.SetOrigin(origin)
}

// GetLabel retrieves the label with the provided key. If not found
// value will be empty and ok will be false.
func (s *DatabaseServerV3) GetLabel(key string) (value string, ok bool) {
if s.Spec.Database != nil {
if v, ok := s.Spec.Database.GetLabel(key); ok {
return v, ok
}
}

v, ok := s.Metadata.Labels[key]
return v, ok
}

// GetAllLabels returns all resource's labels. Considering:
// * Static labels from `Metadata.Labels` and `Spec.Database`.
// * Dynamic labels from `Spec.DynamicLabels`.
Expand Down
7 changes: 7 additions & 0 deletions api/types/databaseservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ func (s *DatabaseServiceV1) GetNamespace() string {
return s.Metadata.Namespace
}

// GetLabel retrieves the label with the provided key. If not found
// value will be empty and ok will be false.
func (s *DatabaseServiceV1) GetLabel(key string) (val string, ok bool) {
v, ok := s.Metadata.Labels[key]
return v, ok
}

// GetAllLabels returns combined static and dynamic labels.
func (s *DatabaseServiceV1) GetAllLabels() map[string]string {
return s.Metadata.Labels
Expand Down
14 changes: 14 additions & 0 deletions api/types/desktop.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,13 @@ func (s *WindowsDesktopServiceV3) SetProxyIDs(proxyIDs []string) {
s.Spec.ProxyIDs = proxyIDs
}

// GetLabel retrieves the label with the provided key. If not found
// value will be empty and ok will be false.
func (s *WindowsDesktopServiceV3) GetLabel(key string) (val string, ok bool) {
v, ok := s.Metadata.Labels[key]
return v, ok
}

// GetAllLabels returns the resources labels.
func (s *WindowsDesktopServiceV3) GetAllLabels() map[string]string {
return s.Metadata.Labels
Expand Down Expand Up @@ -204,6 +211,13 @@ func (d *WindowsDesktopV3) GetHostID() string {
return d.Spec.HostID
}

// GetLabel retrieves the label with the provided key. If not found
// value will be empty and ok will be false.
func (d *WindowsDesktopV3) GetLabel(key string) (val string, ok bool) {
v, ok := d.Metadata.Labels[key]
return v, ok
}

// GetAllLabels returns combined static and dynamic labels.
func (d *WindowsDesktopV3) GetAllLabels() map[string]string {
// TODO(zmb3): add dynamic labels when running in agent mode
Expand Down
11 changes: 11 additions & 0 deletions api/types/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,17 @@ func (k *KubernetesClusterV3) SetName(name string) {
k.Metadata.Name = name
}

// GetLabel retrieves the label with the provided key. If not found
// value will be empty and ok will be false.
func (k *KubernetesClusterV3) GetLabel(key string) (value string, ok bool) {
if cmd, ok := k.Spec.DynamicLabels[key]; ok {
return cmd.Result, ok
}

v, ok := k.Metadata.Labels[key]
return v, ok
}

// GetStaticLabels returns the static labels.
func (k *KubernetesClusterV3) GetStaticLabels() map[string]string {
return k.Metadata.Labels
Expand Down
13 changes: 13 additions & 0 deletions api/types/kubernetes_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,19 @@ func (s *KubernetesServerV3) SetProxyIDs(proxyIDs []string) {
s.Spec.ProxyIDs = proxyIDs
}

// GetLabel retrieves the label with the provided key. If not found
// value will be empty and ok will be false.
func (s *KubernetesServerV3) GetLabel(key string) (value string, ok bool) {
if s.Spec.Cluster != nil {
if v, ok := s.Spec.Cluster.GetLabel(key); ok {
return v, ok
}
}

v, ok := s.Metadata.Labels[key]
return v, ok
}

// GetAllLabels returns all resource's labels. Considering:
// * Static labels from `Metadata.Labels` and `Spec.Cluster`.
// * Dynamic labels from `Spec.Cluster.Spec`.
Expand Down
2 changes: 2 additions & 0 deletions api/types/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ type ResourceWithOrigin interface {
type ResourceWithLabels interface {
// ResourceWithOrigin is the base resource interface.
ResourceWithOrigin
// GetLabel retrieves the label with the provided key.
GetLabel(key string) (value string, ok bool)
// GetAllLabels returns all resource's labels.
GetAllLabels() map[string]string
// GetStaticLabels returns the resource's static labels.
Expand Down
17 changes: 15 additions & 2 deletions api/types/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,16 +229,29 @@ func (s *ServerV2) GetHostname() string {
return s.Spec.Hostname
}

// GetLabel retrieves the label with the provided key. If not found
// value will be empty and ok will be false.
func (s *ServerV2) GetLabel(key string) (value string, ok bool) {
if cmd, ok := s.Spec.CmdLabels[key]; ok {
return cmd.Result, ok
}

v, ok := s.Metadata.Labels[key]
return v, ok
}

// GetLabels returns server's static label key pairs.
// GetLabels and GetStaticLabels are the same, and that is intentional. GetLabels
// exists to preserve backwards compatibility, while GetStaticLabels exists to
// implement ResourcesWithLabels.

// GetLabels returns server's static label key pairs
func (s *ServerV2) GetLabels() map[string]string {
return s.Metadata.Labels
}

// GetStaticLabels returns the server static labels.
// GetLabels and GetStaticLabels are the same, and that is intentional. GetLabels
// exists to preserve backwards compatibility, while GetStaticLabels exists to
// implement ResourcesWithLabels.
func (s *ServerV2) GetStaticLabels() map[string]string {
return s.Metadata.Labels
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ require (
github.com/gravitational/trace v1.2.1
github.com/gravitational/ttlmap v0.0.0-20171116003245-91fd36b9004c
github.com/grpc-ecosystem/go-grpc-middleware/providers/openmetrics/v2 v2.0.0-20220714234348-5d0f5fedefc0
github.com/hashicorp/golang-lru v0.5.4
github.com/hashicorp/golang-lru/v2 v2.0.2
github.com/jackc/pgconn v1.13.0
github.com/jackc/pgerrcode v0.0.0-20220416144525-469b46aa5efa
github.com/jackc/pgproto3/v2 v2.3.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -782,8 +782,8 @@ github.com/hashicorp/go-version v1.2.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09
github.com/hashicorp/go.net v0.0.1/go.mod h1:hjKkEWcCURg++eb33jQU7oqQcI9XDCnUzHA0oac0k90=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.4 h1:YDjusn29QI/Das2iO9M0BHnIbxPeyuCHsjMW+lJfyTc=
github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
github.com/hashicorp/golang-lru/v2 v2.0.2 h1:Dwmkdr5Nc/oBiXgJS3CDHNhJtIHkuZ3DZF5twqnfBdU=
github.com/hashicorp/golang-lru/v2 v2.0.2/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64=
github.com/hashicorp/mdns v1.0.0/go.mod h1:tL+uN++7HEJ6SQLQ2/p+z2pH24WQKWjBPkE0mNTz8vQ=
Expand Down
5 changes: 5 additions & 0 deletions lib/auth/auth_with_roles.go
Original file line number Diff line number Diff line change
Expand Up @@ -1443,6 +1443,11 @@ func (a *ServerWithRoles) ListResources(ctx context.Context, req proto.ListResou
req.SearchKeywords = nil
req.PredicateExpression = ""

// Increase the limit to one more than was requested so
// that an additional page load is not needed to determine
// the next key.
req.Limit++

resourceChecker, err := a.newResourceAccessChecker(req.ResourceType)
if err != nil {
return nil, trace.Wrap(err)
Expand Down
104 changes: 104 additions & 0 deletions lib/auth/auth_with_roles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"crypto/tls"
"crypto/x509/pkix"
"fmt"
"io"
"testing"
"time"

Expand All @@ -29,12 +30,14 @@ import (
"github.com/google/uuid"
"github.com/gravitational/trace"
"github.com/pquerna/otp/totp"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/require"

"github.com/gravitational/teleport"
"github.com/gravitational/teleport/api/client/proto"
"github.com/gravitational/teleport/api/constants"
"github.com/gravitational/teleport/api/defaults"
apidefaults "github.com/gravitational/teleport/api/defaults"
"github.com/gravitational/teleport/api/types"
apievents "github.com/gravitational/teleport/api/types/events"
"github.com/gravitational/teleport/api/types/installers"
Expand All @@ -49,6 +52,7 @@ import (
"github.com/gravitational/teleport/lib/services"
"github.com/gravitational/teleport/lib/session"
"github.com/gravitational/teleport/lib/tlsca"
"github.com/gravitational/teleport/lib/utils"
)

func TestGenerateUserCerts_MFAVerifiedFieldSet(t *testing.T) {
Expand Down Expand Up @@ -1502,6 +1506,106 @@ func TestSessionRecordingConfigRBAC(t *testing.T) {
})
}

// time go test ./lib/auth -bench=. -run=^$ -v
// goos: darwin
// goarch: amd64
// pkg: github.com/gravitational/teleport/lib/auth
// cpu: Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
// BenchmarkListNodes
// BenchmarkListNodes-16 1 1000469673 ns/op 518721960 B/op 8344858 allocs/op
// PASS
// ok github.com/gravitational/teleport/lib/auth 3.695s
// go test ./lib/auth -bench=. -run=^$ -v 19.02s user 3.87s system 244% cpu 9.376 total
func BenchmarkListNodes(b *testing.B) {
const nodeCount = 50_000
const roleCount = 32

logger := logrus.StandardLogger()
logger.ReplaceHooks(make(logrus.LevelHooks))
logrus.SetFormatter(utils.NewTestJSONFormatter())
logger.SetLevel(logrus.DebugLevel)
logger.SetOutput(io.Discard)

ctx := context.Background()
srv := newTestTLSServer(b)

var values []string
for i := 0; i < roleCount; i++ {
values = append(values, uuid.New().String())
}

values[0] = "hidden"

var hiddenNodes int
// Create test nodes.
for i := 0; i < nodeCount; i++ {
name := uuid.New().String()
val := values[i%len(values)]
if val == "hidden" {
hiddenNodes++
}
node, err := types.NewServerWithLabels(
name,
types.KindNode,
types.ServerSpecV2{},
map[string]string{"key": val},
)
require.NoError(b, err)

_, err = srv.Auth().UpsertNode(ctx, node)
require.NoError(b, err)
}

testNodes, err := srv.Auth().GetNodes(ctx, defaults.Namespace)
require.NoError(b, err)
require.Len(b, testNodes, nodeCount)

var roles []types.Role
for _, val := range values {
role, err := types.NewRole(fmt.Sprintf("role-%s", val), types.RoleSpecV5{})
require.NoError(b, err)

if val == "hidden" {
role.SetNodeLabels(types.Deny, types.Labels{"key": {val}})
} else {
role.SetNodeLabels(types.Allow, types.Labels{"key": {val}})
}
roles = append(roles, role)
}

// create user, role, and client
username := "user"

user, err := CreateUser(srv.Auth(), username, roles...)
require.NoError(b, err)
identity := TestUser(user.GetName())
clt, err := srv.NewClient(identity)
require.NoError(b, err)

b.ReportAllocs()
b.ResetTimer()

for n := 0; n < b.N; n++ {
var resources []types.ResourceWithLabels
req := proto.ListResourcesRequest{
ResourceType: types.KindNode,
Namespace: apidefaults.Namespace,
Limit: 1_000,
}
for {
rsp, err := clt.ListResources(ctx, req)
require.NoError(b, err)

resources = append(resources, rsp.Resources...)
req.StartKey = rsp.NextKey
if req.StartKey == "" {
break
}
}
require.Len(b, resources, nodeCount-hiddenNodes)
}
}

// TestGetAndList_Nodes users can retrieve nodes with various filters
// and with the appropriate permissions.
func TestGetAndList_Nodes(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion lib/auth/tls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3573,7 +3573,7 @@ func verifyJWT(clock clockwork.Clock, clusterName string, pairs []*types.JWTKeyP
return nil, trace.NewAggregate(errs...)
}

func newTestTLSServer(t *testing.T) *TestTLSServer {
func newTestTLSServer(t testing.TB) *TestTLSServer {
as, err := NewTestAuthServer(TestAuthServerConfig{
Dir: t.TempDir(),
Clock: clockwork.NewFakeClock(),
Expand Down