Skip to content

Commit

Permalink
feat(*)!: add support for more authoritative heartbeat
Browse files Browse the repository at this point in the history
Signed-off-by: Brooks Townsend <[email protected]>
  • Loading branch information
brooksmtownsend committed Jan 25, 2024
1 parent 1703674 commit 8ee1027
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 73 deletions.
55 changes: 20 additions & 35 deletions lib/lattice_observer/observed/event_processor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -225,26 +225,6 @@ defmodule LatticeObserver.Observed.EventProcessor do
}
end

# New heartbeat format
# "actors": {
# "MB2ZQB6ROOMAYBO4ZCTFYWN7YIVBWA3MTKZYAQKJMTIHE2ELLRW2E3ZW": 10
# },
# "friendly_name": "wandering-meadow-5880",
# "labels": {
# "hostcore.arch": "aarch64",
# "hostcore.os": "macos",
# "hostcore.osfamily": "unix"
# },
# "providers": [
# {
# "link_name": "default",
# "public_key": "VAG3QITQQ2ODAOWB5TTQSDJ53XK3SHBEIFNK4AYJ5RKAX2UNSCAPHA5M"
# }
# ],
# "uptime_human": "1 minute, 32 seconds",
# "uptime_seconds": 92,
# "version": "0.60.0"

def record_heartbeat(l = %Lattice{}, source_host, stamp, data) do
labels = Map.get(data, "labels", %{})
friendly_name = Map.get(data, "friendly_name", "")
Expand Down Expand Up @@ -282,11 +262,10 @@ defmodule LatticeObserver.Observed.EventProcessor do

l = record_host(l, source_host, labels, stamp, friendly_name, uptime_seconds, version)

# legacy heartbeat has a list for the actors field...
# default to "new format" if this field is missing
# new heartbeat has a list for the actors field with more information
l =
if is_list(Map.get(data, "actors", %{})) do
put_legacy_instances(l, source_host, stamp, data)
put_v82_instances(l, source_host, stamp, data)
else
actors_expanded =
Enum.flat_map(Map.get(data, "actors", %{}), fn {public_key, count} ->
Expand Down Expand Up @@ -324,25 +303,31 @@ defmodule LatticeObserver.Observed.EventProcessor do
l
end

defp put_legacy_instances(l = %Lattice{}, source_host, stamp, data) do
defp put_v82_instances(l = %Lattice{}, source_host, stamp, data) do
l =
List.foldl(Map.get(data, "actors", []), l, fn x, acc ->
put_actor_instance(
acc,
source_host,
x["public_key"],
Map.get(x, "instance_id", "n/a"),
Map.get(x, "annotations", %{}),
stamp,
%{}
)
List.foldl(Map.get(data, "actors", []), l, fn x, all_actors ->
id = x["id"]

# Iterate over the instances and, using the annotations and scale, create
# a list of instances to add to the actor.
x["instances"]
|> Enum.reduce(all_actors, fn instance, actors ->
set_actor_instances(
actors,
source_host,
id,
Map.get(instance, "annotations", %{}),
%{},
Map.get(instance, "max_instances", 1)
)
end)
end)

List.foldl(Map.get(data, "providers", []), l, fn x, acc ->
put_provider_instance(
acc,
source_host,
x["public_key"],
x["id"],
x["link_name"],
x["contract_id"],
Map.get(x, "instance_id", "n/a"),
Expand Down
87 changes: 50 additions & 37 deletions test/observed/hosts_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -113,36 +113,16 @@ defmodule LatticeObserverTest.Observed.HostsTest do
Lattice.new()
end

# updated heartbeat shape
# "actors": {
# "MB2ZQB6ROOMAYBO4ZCTFYWN7YIVBWA3MTKZYAQKJMTIHE2ELLRW2E3ZW": 10
# },
# "friendly_name": "wandering-meadow-5880",
# "labels": {
# "hostcore.arch": "aarch64",
# "hostcore.os": "macos",
# "hostcore.osfamily": "unix"
# },
# "providers": [
# {
# "link_name": "default",
# "public_key": "VAG3QITQQ2ODAOWB5TTQSDJ53XK3SHBEIFNK4AYJ5RKAX2UNSCAPHA5M"
# }
# ],
# "uptime_human": "1 minute, 32 seconds",
# "uptime_seconds": 92,
# "version": "0.60.0"

test "Propertly records LEGACY host heartbeat" do
hb = CloudEvents.host_heartbeat_old(@test_host, %{foo: "bar", baz: "biz"})
test "Propertly records NEW host heartbeat" do
hb = CloudEvents.host_heartbeat_new(@test_host, %{foo: "bar", baz: "biz"})
stamp = EventProcessor.timestamp_from_iso8601(hb.time)
l = Lattice.apply_event(Lattice.new(), hb)

assert l.hosts[@test_host].labels == %{baz: "biz", foo: "bar"}
assert l.hosts[@test_host].status == :healthy
assert l.hosts[@test_host].last_seen == stamp

hb2 = CloudEvents.host_heartbeat_old(@test_host, %{foo: "bar", baz: "biz"})
hb2 = CloudEvents.host_heartbeat_new(@test_host, %{foo: "bar", baz: "biz"})
stamp2 = EventProcessor.timestamp_from_iso8601(hb2.time)
l = Lattice.apply_event(l, hb2)

Expand All @@ -156,17 +136,37 @@ defmodule LatticeObserverTest.Observed.HostsTest do
%{foo: "bar"},
[
%{
"public_key" => "Mxxxx",
"instance_id" => "iid1"
"id" => "Mxxxx",
"name" => "Test Actor",
"image_ref" => "wasmcloud.azurecr.io/actor:0.1.0",
"instances" => [
%{
"annotations" => %{"foo" => "bar"},
"image_ref" => "wasmcloud.azurecr.io/actor:0.1.0",
"instance_id" => "iid1",
"revision" => 0,
"max_instances" => 1
}
]
},
%{
"public_key" => "Mxxxy",
"instance_id" => "iid2"
"id" => "Mxxxy",
"name" => "Test Actor Deux",
"image_ref" => "wasmcloud.azurecr.io/actortwo:0.1.0",
"instances" => [
%{
"annotations" => %{"bar" => "baz"},
"image_ref" => "wasmcloud.azurecr.io/actortwo:0.1.0",
"instance_id" => "iid2",
"revision" => 0,
"max_instances" => 1
}
]
}
],
[
%{
"public_key" => "Vxxxxx",
"id" => "Vxxxxx",
"instance_id" => "iid3",
"contract_id" => "wasmcloud:test",
"link_name" => "default"
Expand All @@ -182,16 +182,29 @@ defmodule LatticeObserverTest.Observed.HostsTest do
%{foo: "bar"},
[
%{
"public_key" => "Mxxxx",
"instance_id" => "iid1"
"id" => "Mxxxx",
"name" => "Test Actor",
"image_ref" => "wasmcloud.azurecr.io/actor:0.1.0",
"instances" => [
%{
"annotations" => %{"foo" => "bar"},
"image_ref" => "wasmcloud.azurecr.io/actor:0.1.0",
"instance_id" => "iid1",
"revision" => 0,
"max_instances" => 1
}
]
}
],
[
%{
"public_key" => "Vxxxxx",
"instance_id" => "iid3",
"id" => "Vxxxxx",
"contract_id" => "wasmcloud:test",
"link_name" => "default"
"link_name" => "default",
"image_ref" => "wasmcloud.azurecr.io/provider:0.1.0",
"revision" => 0,
"annotations" => %{"foo" => "bar"},
"name" => "Test Provider"
}
]
)
Expand All @@ -207,9 +220,9 @@ defmodule LatticeObserverTest.Observed.HostsTest do
instances: [
%LatticeObserver.Observed.Instance{
host_id: "Nxxx",
id: "iid1",
id: "N/A",
revision: 0,
annotations: %{},
annotations: %{"foo" => "bar"},
version: ""
}
],
Expand All @@ -226,9 +239,9 @@ defmodule LatticeObserverTest.Observed.HostsTest do
instances: [
%LatticeObserver.Observed.Instance{
host_id: "Nxxx",
id: "iid3",
id: "n/a",
revision: 0,
annotations: %{},
annotations: %{"foo" => "bar"},
version: ""
}
],
Expand Down
2 changes: 1 addition & 1 deletion test/support/cloud_events.ex
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ defmodule TestSupport.CloudEvents do
|> LatticeObserver.CloudEvent.new("invocation_failed", host)
end

def host_heartbeat_old(host, labels, actors \\ [], providers \\ []) do
def host_heartbeat_new(host, labels, actors \\ [], providers \\ []) do
%{
"actors" => actors,
"providers" => providers,
Expand Down

0 comments on commit 8ee1027

Please sign in to comment.