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

hubble: Optimize namespace tracking #26547

Merged
merged 1 commit into from
Jul 11, 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
6 changes: 2 additions & 4 deletions pkg/hubble/observer/local_observer.go
Original file line number Diff line number Diff line change
Expand Up @@ -623,20 +623,18 @@ func (r *eventsReader) Next(ctx context.Context) (*v1.Event, error) {

func (s *LocalObserverServer) trackNamespaces(flow *flowpb.Flow) {
// track namespaces seen.
var namespaces []*observerpb.Namespace
if srcNs := flow.GetSource().GetNamespace(); srcNs != "" {
namespaces = append(namespaces, &observerpb.Namespace{
s.namespaceManager.AddNamespace(&observerpb.Namespace{
Namespace: srcNs,
Cluster: nodeTypes.GetClusterName(),
})
}
if dstNs := flow.GetDestination().GetNamespace(); dstNs != "" {
namespaces = append(namespaces, &observerpb.Namespace{
s.namespaceManager.AddNamespace(&observerpb.Namespace{
Namespace: dstNs,
Cluster: nodeTypes.GetClusterName(),
})
}
s.namespaceManager.AddNamespace(namespaces...)
}

func validateRequest(req genericRequest) error {
Expand Down
32 changes: 32 additions & 0 deletions pkg/hubble/observer/local_observer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -597,3 +597,35 @@ func TestLocalObserverServer_GetNamespaces(t *testing.T) {
}
assert.Equal(t, expected, res)
}

func Benchmark_TrackNamespaces(b *testing.B) {
pp, err := parser.New(
log,
&testutils.NoopEndpointGetter,
&testutils.NoopIdentityGetter,
&testutils.NoopDNSGetter,
&testutils.NoopIPGetter,
&testutils.NoopServiceGetter,
&testutils.NoopLinkGetter,
&testutils.NoopPodMetadataGetter,
)
if err != nil {
b.Fatal(err)
}

nsManager := NewNamespaceManager()
s, err := NewLocalServer(pp, nsManager, log, observeroption.WithMaxFlows(container.Capacity1))
if err != nil {
b.Fatal(err)
}
f := &flowpb.Flow{
Source: &flowpb.Endpoint{Namespace: "foo"},
Destination: &flowpb.Endpoint{Namespace: "bar"},
}

b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
s.trackNamespaces(f)
}
}
13 changes: 6 additions & 7 deletions pkg/hubble/observer/namespace_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const (

type NamespaceManager interface {
GetNamespaces() []*observerpb.Namespace
AddNamespace(...*observerpb.Namespace)
AddNamespace(*observerpb.Namespace)
}

type namespaceRecord struct {
Expand Down Expand Up @@ -85,11 +85,10 @@ func (m *namespaceManager) GetNamespaces() []*observerpb.Namespace {
return namespaces
}

func (m *namespaceManager) AddNamespace(namespaces ...*observerpb.Namespace) {
func (m *namespaceManager) AddNamespace(ns *observerpb.Namespace) {
m.mu.Lock()
for _, ns := range namespaces {
key := ns.GetCluster() + "/" + ns.GetNamespace()
m.namespaces[key] = namespaceRecord{namespace: ns, added: m.nowFunc()}
}
m.mu.Unlock()
defer m.mu.Unlock()

key := ns.GetCluster() + "/" + ns.GetNamespace()
m.namespaces[key] = namespaceRecord{namespace: ns, added: m.nowFunc()}
}
19 changes: 7 additions & 12 deletions pkg/hubble/observer/namespace_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,10 @@ func TestNamespaceManager(t *testing.T) {
assert.Equal(t, expected, mgr.GetNamespaces())

// add a few namespaces
nses1 := []*observerpb.Namespace{
{Namespace: "ns-2"},
{Namespace: "ns-1"}, // out of order, we'll verify it's sorted when we call GetNamespaces later
}
mgr.AddNamespace(nses1...)

// out of order, we'll verify it's sorted when we call GetNamespaces later
mgr.AddNamespace(&observerpb.Namespace{Namespace: "ns-2"})
mgr.AddNamespace(&observerpb.Namespace{Namespace: "ns-1"})

// namespaces that we added should be returned, sorted
expected = []*observerpb.Namespace{
Expand All @@ -50,13 +49,9 @@ func TestNamespaceManager(t *testing.T) {
assert.Equal(t, expected, mgr.GetNamespaces())

// add more namespaces now that the clock has been advanced
nses2 := []*observerpb.Namespace{
// ns-1 was in the original set, adding it again here to verify it's TTL gets renewed
{Namespace: "ns-1"},
{Namespace: "ns-3"},
{Namespace: "ns-4"},
}
mgr.AddNamespace(nses2...)
mgr.AddNamespace(&observerpb.Namespace{Namespace: "ns-1"})
mgr.AddNamespace(&observerpb.Namespace{Namespace: "ns-3"})
mgr.AddNamespace(&observerpb.Namespace{Namespace: "ns-4"})

// we expect all namespaces to exist, the first 2 are 30 minutes old, and the
// next two are 0 minutes old
Expand Down
4 changes: 3 additions & 1 deletion pkg/hubble/relay/observer/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,9 @@ func (s *Server) GetNamespaces(ctx context.Context, req *observerpb.GetNamespace
}).Warning("Failed to retrieve namespaces")
return nil
}
namespaceManager.AddNamespace(nsResp.GetNamespaces()...)
for _, ns := range nsResp.GetNamespaces() {
namespaceManager.AddNamespace(ns)
}
return nil
})
}
Expand Down