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 k8 namespace cache in database #1059

Merged
merged 3 commits into from
Jul 26, 2024
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
4 changes: 3 additions & 1 deletion pkg/internal/discover/container_updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ func updateLoop(db *kube.Database) pipe.MiddleFunc[[]Event[Instrumentable], []Ev
db.AddProcess(uint32(ev.Obj.FileInfo.Pid))
case EventDeleted:
// we don't need to handle process deletion from here, as the Kubernetes informer will
// remove the process from the database when the Pod that contains it is deleted
// remove the process from the database when the Pod that contains it is deleted.
// However we clean-up the performance related caches, in case we miss pod removal event
db.CleanProcessCaches(ev.Obj.FileInfo.Ns)
}
}
out <- instrumentables
Expand Down
34 changes: 25 additions & 9 deletions pkg/internal/transform/kube/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,29 +98,45 @@ func (id *Database) OnDeletion(containerID []string) {
delete(id.containerIDs, cid)
id.cntMut.Unlock()
if ok {
id.podsCacheMut.Lock()
delete(id.fetchedPodsCache, info.PIDNamespace)
id.podsCacheMut.Unlock()
id.deletePodCache(info.PIDNamespace)
id.nsMut.Lock()
delete(id.namespaces, info.PIDNamespace)
id.nsMut.Unlock()
}
}
}

func (id *Database) addProcess(ifp *container.Info) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did this refactor so that I can use this logic in a test.

id.deletePodCache(ifp.PIDNamespace)
id.nsMut.Lock()
id.namespaces[ifp.PIDNamespace] = ifp
id.nsMut.Unlock()
id.cntMut.Lock()
id.containerIDs[ifp.ContainerID] = ifp
id.cntMut.Unlock()
}

// AddProcess also searches for the container.Info of the passed PID
func (id *Database) AddProcess(pid uint32) {
ifp, err := container.InfoForPID(pid)
if err != nil {
dblog().Debug("failing to get container information", "pid", pid, "error", err)
return
}
id.nsMut.Lock()
id.namespaces[ifp.PIDNamespace] = &ifp
id.nsMut.Unlock()
id.cntMut.Lock()
id.containerIDs[ifp.ContainerID] = &ifp
id.cntMut.Unlock()

id.addProcess(&ifp)
}

func (id *Database) CleanProcessCaches(ns uint32) {
// Don't delete the id.namespaces, we can't tell if Add/Delete events
// are in order. Deleting from the cache is safe, since it will be rebuilt.
id.deletePodCache(ns)
}

func (id *Database) deletePodCache(ns uint32) {
id.podsCacheMut.Lock()
delete(id.fetchedPodsCache, ns)
id.podsCacheMut.Unlock()
}

// OwnerPodInfo returns the information of the pod owning the passed namespace
Expand Down
61 changes: 61 additions & 0 deletions pkg/internal/transform/kube/db_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package kube

import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/grafana/beyla/pkg/internal/helpers/container"
"github.com/grafana/beyla/pkg/internal/kube"
)

func Test_NamespaceReuse(t *testing.T) {
db := CreateDatabase(&kube.Metadata{})
ifp := container.Info{
ContainerID: "a",
PIDNamespace: 111,
}
db.addProcess(&ifp)
// pretend we resolved some pod info earlier
db.fetchedPodsCache[ifp.PIDNamespace] = &kube.PodInfo{NodeName: "a"}
info, err := db.OwnerPodInfo(111)
assert.True(t, err)
assert.NotNil(t, info)
assert.Equal(t, "a", info.NodeName)

_, ok := db.fetchedPodsCache[ifp.PIDNamespace]
assert.True(t, ok)

ifp1 := container.Info{
ContainerID: "b",
PIDNamespace: ifp.PIDNamespace,
}

// We overwrite the container info with new namespace
db.addProcess(&ifp1)
_, ok = db.fetchedPodsCache[ifp.PIDNamespace]
assert.False(t, ok)
}

func Test_NamespaceCacheCleanup(t *testing.T) {
db := CreateDatabase(&kube.Metadata{})
ifp := container.Info{
ContainerID: "a",
PIDNamespace: 111,
}
db.addProcess(&ifp)
// pretend we resolved some pod info earlier
db.fetchedPodsCache[ifp.PIDNamespace] = &kube.PodInfo{NodeName: "a"}
info, err := db.OwnerPodInfo(111)
assert.True(t, err)
assert.NotNil(t, info)
assert.Equal(t, "a", info.NodeName)

_, ok := db.fetchedPodsCache[ifp.PIDNamespace]
assert.True(t, ok)

// We overwrite the container info with new namespace
db.CleanProcessCaches(ifp.PIDNamespace)
_, ok = db.fetchedPodsCache[ifp.PIDNamespace]
assert.False(t, ok)
}
Loading