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

Add 'latest' CRDT, make UI update quicker. #628

Merged
merged 2 commits into from
Nov 9, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 4 additions & 4 deletions probe/docker/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,12 +264,11 @@ func (c *container) GetNode(hostID string, localAddrs []net.IP) report.Node {
ContainerCommand: c.container.Path + " " + strings.Join(c.container.Args, " "),
ImageID: c.container.Image,
ContainerHostname: c.Hostname(),
ContainerState: state,
}).WithSets(report.Sets{
ContainerPorts: c.ports(localAddrs),
ContainerIPs: report.MakeStringSet(ips...),
ContainerIPsWithScopes: report.MakeStringSet(ipsWithScopes...),
})
}).WithLatest(ContainerState, state)

if c.container.State.Paused {
result = result.WithControls(UnpauseContainer)
Expand All @@ -285,7 +284,7 @@ func (c *container) GetNode(hostID string, localAddrs []net.IP) report.Node {
return result
}

result = result.Merge(report.MakeNodeWith(map[string]string{
result = result.WithMetadata(map[string]string{
NetworkRxDropped: strconv.FormatUint(c.latestStats.Network.RxDropped, 10),
NetworkRxBytes: strconv.FormatUint(c.latestStats.Network.RxBytes, 10),
NetworkRxErrors: strconv.FormatUint(c.latestStats.Network.RxErrors, 10),
Expand All @@ -305,7 +304,8 @@ func (c *container) GetNode(hostID string, localAddrs []net.IP) report.Node {
CPUTotalUsage: strconv.FormatUint(c.latestStats.CPUStats.CPUUsage.TotalUsage, 10),
CPUUsageInKernelmode: strconv.FormatUint(c.latestStats.CPUStats.CPUUsage.UsageInKernelmode, 10),
CPUSystemCPUUsage: strconv.FormatUint(c.latestStats.CPUStats.SystemCPUUsage, 10),
}))
})

return result
}

Expand Down
6 changes: 4 additions & 2 deletions probe/docker/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,14 @@ func TestContainer(t *testing.T) {
"docker_label_foo1": "bar1",
"docker_label_foo2": "bar2",
"memory_usage": "12345",
"docker_container_state": "running",
}).WithSets(report.Sets{
"docker_container_ports": report.MakeStringSet("1.2.3.4:80->80/tcp", "81/tcp"),
"docker_container_ips": report.MakeStringSet("1.2.3.4"),
"docker_container_ips_with_scopes": report.MakeStringSet("scope;1.2.3.4"),
}).WithControls(docker.RestartContainer, docker.StopContainer, docker.PauseContainer)
}).WithControls(
docker.RestartContainer, docker.StopContainer, docker.PauseContainer,
).WithLatest("docker_container_state", "running")

test.Poll(t, 100*time.Millisecond, want, func() interface{} {
node := c.GetNode("scope", []net.IP{})
for k, v := range node.Metadata {
Expand Down
8 changes: 4 additions & 4 deletions probe/host/tagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ func NewTagger(hostID, probeID string) Tagger {

// Tag implements Tagger.
func (t Tagger) Tag(r report.Report) (report.Report, error) {
other := report.MakeNodeWith(map[string]string{
metadata := map[string]string{
report.HostNodeID: t.hostNodeID,
report.ProbeID: t.probeID,
})
}

// Explicity don't tag Endpoints and Addresses - These topologies include pseudo nodes,
// and as such do their own host tagging
for _, topology := range []report.Topology{r.Process, r.Container, r.ContainerImage, r.Host, r.Overlay} {
for id := range topology.Nodes {
topology.AddNode(id, other)
for id, node := range topology.Nodes {
topology.AddNode(id, node.WithMetadata(metadata))
}
}
return r, nil
Expand Down
6 changes: 3 additions & 3 deletions probe/tag_report.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ func (topologyTagger) Tag(r report.Report) (report.Report, error) {
"host": &(r.Host),
"overlay": &(r.Overlay),
} {
other := report.MakeNodeWith(map[string]string{Topology: val})
for id := range topology.Nodes {
topology.AddNode(id, other)
metadata := map[string]string{Topology: val}
for id, node := range topology.Nodes {
topology.AddNode(id, node.WithMetadata(metadata))
}
}
return r, nil
Expand Down
7 changes: 2 additions & 5 deletions probe/tag_report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"testing"

"github.com/weaveworks/scope/report"
"github.com/weaveworks/scope/test"
)

func TestApply(t *testing.T) {
Expand Down Expand Up @@ -38,11 +37,9 @@ func TestApply(t *testing.T) {
func TestTagMissingID(t *testing.T) {
const nodeID = "not-found"
r := report.MakeReport()
want := report.MakeNode()
rpt, _ := newTopologyTagger().Tag(r)
have := rpt.Endpoint.Nodes[nodeID].Copy()
if !reflect.DeepEqual(want, have) {
t.Error(test.Diff(want, have))
_, ok := rpt.Endpoint.Nodes[nodeID]
if ok {
t.Error("TopologyTagger erroneously tagged a missing node ID")
}
}
9 changes: 8 additions & 1 deletion render/detailed_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ func controlsFor(topology report.Topology, nodeID string) []ControlInstance {
return result
}

for _, id := range node.Controls {
for _, id := range node.Controls.Controls {
if control, ok := topology.Controls[id]; ok {
result = append(result, ControlInstance{
ProbeID: node.Metadata[report.ProbeID],
Expand Down Expand Up @@ -347,6 +347,13 @@ func containerOriginTable(nmd report.Node, addHostTag bool) (Table, bool) {
rows := []Row{}
for _, tuple := range []struct{ key, human string }{
{docker.ContainerState, "State"},
} {
if val, ok := nmd.Latest.Lookup(tuple.key); ok && val != "" {
rows = append(rows, Row{Key: tuple.human, ValueMajor: val, ValueMinor: ""})
}
}

for _, tuple := range []struct{ key, human string }{
{docker.ContainerID, "ID"},
{docker.ImageID, "Image ID"},
{docker.ContainerPorts, "Ports"},
Expand Down
2 changes: 2 additions & 0 deletions render/detailed_node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func TestOriginTable(t *testing.T) {
Rank: 3,
Rows: []render.Row{
{"Host", fixture.ServerHostID, "", false},
{"State", "running", "", false},
{"ID", fixture.ServerContainerID, "", false},
{"Image ID", fixture.ServerContainerImageID, "", false},
{fmt.Sprintf(`Label %q`, render.AmazonECSContainerNameLabel), `server`, "", false},
Expand Down Expand Up @@ -161,6 +162,7 @@ func TestMakeDetailedContainerNode(t *testing.T) {
Numeric: false,
Rank: 3,
Rows: []render.Row{
{"State", "running", "", false},
{"ID", fixture.ServerContainerID, "", false},
{"Image ID", fixture.ServerContainerImageID, "", false},
{fmt.Sprintf(`Label %q`, render.AmazonECSContainerNameLabel), `server`, "", false},
Expand Down
4 changes: 2 additions & 2 deletions render/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ func FilterStopped(r Renderer) Renderer {
return Filter{
Renderer: r,
FilterFunc: func(node RenderableNode) bool {
containerState := node.Metadata[docker.ContainerState]
return containerState != docker.StateStopped
containerState, ok := node.Latest.Lookup(docker.ContainerState)
return !ok || containerState != docker.StateStopped
},
}
}
Expand Down
8 changes: 4 additions & 4 deletions render/render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func TestFilterRender(t *testing.T) {
want := render.RenderableNodes{
"foo": {ID: "foo", Origins: report.IDList{}, Node: report.MakeNode().WithAdjacent("bar")},
"bar": {ID: "bar", Origins: report.IDList{}, Node: report.MakeNode().WithAdjacent("foo")},
}
}.Prune()
have := renderer.Render(report.MakeReport()).Prune()
if !reflect.DeepEqual(want, have) {
t.Error(test.Diff(want, have))
Expand All @@ -212,7 +212,7 @@ func TestFilterRender2(t *testing.T) {
want := render.RenderableNodes{
"foo": {ID: "foo", Origins: report.IDList{}, Node: report.MakeNode()},
"baz": {ID: "baz", Origins: report.IDList{}, Node: report.MakeNode()},
}
}.Prune()
have := renderer.Render(report.MakeReport()).Prune()
if !reflect.DeepEqual(want, have) {
t.Error(test.Diff(want, have))
Expand Down Expand Up @@ -253,7 +253,7 @@ func TestFilterUnconnectedPesudoNodes(t *testing.T) {
}
want := render.RenderableNodes{
"foo": {ID: "foo", Origins: report.IDList{}, Node: report.MakeNode()},
}
}.Prune()
have := renderer.Render(report.MakeReport()).Prune()
if !reflect.DeepEqual(want, have) {
t.Error(test.Diff(want, have))
Expand All @@ -272,7 +272,7 @@ func TestFilterUnconnectedPesudoNodes(t *testing.T) {
}
want := render.RenderableNodes{
"foo": {ID: "foo", Origins: report.IDList{}, Node: report.MakeNode()},
}
}.Prune()
have := renderer.Render(report.MakeReport()).Prune()
if !reflect.DeepEqual(want, have) {
t.Error(test.Diff(want, have))
Expand Down
10 changes: 6 additions & 4 deletions render/renderable_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,12 @@ func (rn RenderableNode) Copy() RenderableNode {
// Specifically, that means cutting out parts of the Node.
func (rn RenderableNode) Prune() RenderableNode {
cp := rn.Copy()
cp.Node.Metadata = report.Metadata{} // snip
cp.Node.Counters = report.Counters{} // snip
cp.Node.Edges = report.EdgeMetadatas{} // snip
cp.Node.Sets = report.Sets{} // snip
cp.Node.Metadata = report.Metadata{} // snip
cp.Node.Counters = report.Counters{} // snip
cp.Node.Edges = report.EdgeMetadatas{} // snip
cp.Node.Sets = report.Sets{} // snip
cp.Node.Controls = report.NodeControls{} // snip
cp.Node.Latest = report.LatestMap{} // snip

This comment was marked as abuse.

This comment was marked as abuse.

return cp
}

Expand Down
6 changes: 3 additions & 3 deletions render/renderable_node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestMergeRenderableNodes(t *testing.T) {
"bar": render.NewRenderableNode("bar"),
"baz": render.NewRenderableNode("baz"),
}).Prune()
have := nodes1.Merge(nodes2)
have := nodes1.Merge(nodes2).Prune()
if !reflect.DeepEqual(want, have) {
t.Error(test.Diff(want, have))
}
Expand Down Expand Up @@ -57,8 +57,8 @@ func TestMergeRenderableNode(t *testing.T) {
Node: report.MakeNode().WithAdjacent("a1").WithAdjacent("a2"),
Origins: report.MakeIDList("o1", "o2"),
EdgeMetadata: report.EdgeMetadata{},
}
have := node1.Merge(node2)
}.Prune()
have := node1.Merge(node2).Prune()
if !reflect.DeepEqual(want, have) {
t.Error(test.Diff(want, have))
}
Expand Down
41 changes: 41 additions & 0 deletions report/controls.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package report

import (
"time"
)

// Controls describe the control tags within the Nodes
type Controls map[string]Control

Expand Down Expand Up @@ -32,3 +36,40 @@ func (cs Controls) Copy() Controls {
func (cs Controls) AddControl(c Control) {
cs[c.ID] = c
}

// NodeControls represent the individual controls that are valid
// for a given node at a given point in time. Its is immutable.
type NodeControls struct {
Timestamp int64 `json:"timestamp"`

This comment was marked as abuse.

This comment was marked as abuse.

Controls IDList `json:"controls"`

This comment was marked as abuse.

}

// MakeNodeControls makes a new NodeControls
func MakeNodeControls() NodeControls {
return NodeControls{
Timestamp: time.Now().Unix(),
Controls: MakeIDList(),
}
}

// Copy is a noop, as NodeControls is immutable
func (nc NodeControls) Copy() NodeControls {
return nc
}

// Merge returns the newest of the two NodeControls; it does not take the union
// of the valid Controls.
func (nc NodeControls) Merge(other NodeControls) NodeControls {
if other.Timestamp > nc.Timestamp {
return other
}
return nc
}

// Add the new control IDs to this NodeControls, producing a fresh NodeControls.
func (nc NodeControls) Add(ids ...string) NodeControls {
return NodeControls{
Timestamp: time.Now().Unix(),
Controls: nc.Controls.Add(ids...),
}
}
Loading