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 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
16 changes: 16 additions & 0 deletions common/mtime/mtime.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package mtime

import "time"

// Now returns the current time.
var Now = func() time.Time { return time.Now() }

// NowForce sets the time returned by Now to t.
func NowForce(t time.Time) {
Now = func() time.Time { return t }
}

// NowReset makes Now returns the current time again.
func NowReset() {
Now = func() time.Time { return time.Now() }
}
9 changes: 5 additions & 4 deletions probe/docker/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (

docker "github.com/fsouza/go-dockerclient"

"github.com/weaveworks/scope/common/mtime"
"github.com/weaveworks/scope/report"
)

Expand Down Expand Up @@ -264,12 +265,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, mtime.Now(), state)

if c.container.State.Paused {
result = result.WithControls(UnpauseContainer)
Expand All @@ -285,7 +285,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 +305,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
11 changes: 9 additions & 2 deletions probe/docker/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

client "github.com/fsouza/go-dockerclient"

"github.com/weaveworks/scope/common/mtime"
"github.com/weaveworks/scope/probe/docker"
"github.com/weaveworks/scope/report"
"github.com/weaveworks/scope/test"
Expand Down Expand Up @@ -64,6 +65,10 @@ func TestContainer(t *testing.T) {
t.Error(err)
}

now := time.Now()
mtime.NowForce(now)
defer mtime.NowReset()

// Now see if we go them
want := report.MakeNode().WithMetadata(map[string]string{
"docker_container_command": " ",
Expand All @@ -74,12 +79,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", now, "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
5 changes: 1 addition & 4 deletions render/renderable_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,7 @@ 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 = report.MakeNode().WithAdjacent(cp.Node.Adjacency...)
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
43 changes: 43 additions & 0 deletions report/controls.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package report

import (
"time"

"github.com/weaveworks/scope/common/mtime"
)

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

Expand Down Expand Up @@ -32,3 +38,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. A zero-value for Timestamp
// indicated this NodeControls is 'not set'.
type NodeControls struct {
Timestamp time.Time `json:"timestamp"`
Controls StringSet `json:"controls,omitempty"`
}

// MakeNodeControls makes a new NodeControls
func MakeNodeControls() NodeControls {
return NodeControls{
Controls: MakeStringSet(),
}
}

// 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 nc.Timestamp.Before(other.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: mtime.Now(),
Controls: nc.Controls.Add(ids...),
}
}
Loading