Skip to content

Commit

Permalink
Commmit everything for real this time
Browse files Browse the repository at this point in the history
Signed-off-by: Kevin Hoffman <[email protected]>
  • Loading branch information
autodidaddict committed Feb 1, 2023
1 parent 40c9835 commit 8cba7ca
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 122 deletions.
19 changes: 11 additions & 8 deletions lib/lattice_observer/observed/event_processor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ defmodule LatticeObserver.Observed.EventProcessor do
}
end

def put_actor_instance(l = %Lattice{}, host_id, pk, instance_id, spec, stamp, claims)
def put_actor_instance(l = %Lattice{}, host_id, pk, instance_id, spec, _stamp, claims)
when is_binary(pk) and is_binary(instance_id) and is_binary(spec) do
actor = Map.get(l.actors, pk, Actor.new(pk, "unavailable"))
actor = merge_actor(actor, l, claims)
Expand All @@ -80,9 +80,7 @@ defmodule LatticeObserver.Observed.EventProcessor do

%Lattice{
l
| actors: Map.put(l.actors, pk, actor),
instance_tracking:
Map.put(l.instance_tracking, instance.id, timestamp_from_iso8601(stamp))
| actors: Map.put(l.actors, pk, actor)
}
end

Expand Down Expand Up @@ -133,6 +131,8 @@ defmodule LatticeObserver.Observed.EventProcessor do
def remove_provider_instance(l, source_host, pk, link_name, _spec) do
provider = l.providers[{pk, link_name}]

# only one provider+link name can exist per host, so this is guaranteed to
# remove that provider since the key is already pk+link.
if provider != nil do
provider = %Provider{
provider
Expand Down Expand Up @@ -376,19 +376,22 @@ defmodule LatticeObserver.Observed.EventProcessor do
}
end

def remove_actor_instance(l = %Lattice{}, _host_id, pk, instance_id, _spec) do
def remove_actor_instance(l = %Lattice{}, host_id, pk, instance_id, _spec) do
actor = l.actors[pk]

if actor != nil do
# Since there's no longer any unique distinction between actor instances on
# the host, we can simply remove any one we like
instances_on_host = Enum.filter(actor.instances, fn i -> i.host_id == host_id end)

actor = %Actor{
actor
| instances: actor.instances |> Enum.reject(fn i -> i.id == instance_id end)
| instances: Enum.drop(instances_on_host, 1)
}

%Lattice{
l
| actors: Map.put(l.actors, pk, actor),
instance_tracking: l.instance_tracking |> Map.delete(instance_id)
| actors: Map.put(l.actors, pk, actor)
}
|> strip_instanceless_entities()
else
Expand Down
112 changes: 2 additions & 110 deletions test/observed/actors_test.exs
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
defmodule LatticeObserverTest.Observed.ActorsTest do
use ExUnit.Case
alias LatticeObserver.Observed.{Lattice, Instance, Actor, EventProcessor}
alias LatticeObserver.Observed.{Lattice, Instance, Actor}
alias TestSupport.CloudEvents

@test_spec "testapp"
@test_spec_2 "othertestapp"
@test_host "Nxxx"

describe "Observed Lattice Monitors Actor Events" do
test "Adds and Removes actors" do
start = CloudEvents.actor_started("Mxxx", "abc123", @test_spec, @test_host)
l = Lattice.new()
l = Lattice.apply_event(l, start)
stamp1 = EventProcessor.timestamp_from_iso8601(start.time)

# ensure idempotence
# l = Lattice.apply_event(l, start)

assert l == %Lattice{
Lattice.new()
Expand Down Expand Up @@ -50,9 +45,7 @@ defmodule LatticeObserverTest.Observed.ActorsTest do
version: "1.0"
}
},
instance_tracking: %{
"abc123" => stamp1
}
instance_tracking: %{}
}

stop = CloudEvents.actor_stopped("Mxxx", "abc123", @test_spec, @test_host)
Expand All @@ -77,106 +70,5 @@ defmodule LatticeObserverTest.Observed.ActorsTest do
}
}
end

test "Stores the same actor belonging to multiple specs" do
start = CloudEvents.actor_started("Mxxx", "abc123", @test_spec, @test_host)
l = Lattice.new()
l = Lattice.apply_event(l, start)
start2 = CloudEvents.actor_started("Mxxx", "abc345", @test_spec_2, @test_host)
l = Lattice.apply_event(l, start2)
stamp1 = EventProcessor.timestamp_from_iso8601(start.time)
stamp2 = EventProcessor.timestamp_from_iso8601(start2.time)

assert l == %Lattice{
Lattice.new()
| actors: %{
"Mxxx" => %Actor{
call_alias: "",
capabilities: ["test", "test2"],
id: "Mxxx",
instances: [
%Instance{
host_id: "Nxxx",
id: "abc345",
revision: 0,
spec_id: "othertestapp",
version: "1.0"
},
%Instance{
host_id: "Nxxx",
id: "abc123",
revision: 0,
spec_id: "testapp",
version: "1.0"
}
],
issuer: "ATESTxxx",
name: "Test Actor",
tags: ""
}
},
claims: %{
"Mxxx" => %LatticeObserver.Observed.Claims{
call_alias: "",
caps: "test,test2",
iss: "ATESTxxx",
name: "Test Actor",
rev: 0,
sub: "Mxxx",
tags: "",
version: "1.0"
}
},
instance_tracking: %{
"abc123" => stamp1,
"abc345" => stamp2
}
}

assert Lattice.actors_in_appspec(l, "testapp") == [
%{actor_id: "Mxxx", host_id: "Nxxx", instance_id: "abc123"}
]

stop = CloudEvents.actor_stopped("Mxxx", "abc123", @test_spec, @test_host)
l = Lattice.apply_event(l, stop)

assert l == %LatticeObserver.Observed.Lattice{
Lattice.new()
| actors: %{
"Mxxx" => %Actor{
call_alias: "",
capabilities: ["test", "test2"],
id: "Mxxx",
instances: [
%Instance{
host_id: "Nxxx",
id: "abc345",
revision: 0,
spec_id: "othertestapp",
version: "1.0"
}
],
issuer: "ATESTxxx",
name: "Test Actor",
tags: ""
}
},
claims: %{
"Mxxx" => %LatticeObserver.Observed.Claims{
call_alias: "",
caps: "test,test2",
iss: "ATESTxxx",
name: "Test Actor",
rev: 0,
sub: "Mxxx",
tags: "",
version: "1.0"
}
},
instance_tracking: %{
"abc345" => stamp2
}
}
end
end
end
2 changes: 0 additions & 2 deletions test/observed/hosts_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ defmodule LatticeObserverTest.Observed.HostsTest do

assert Map.keys(l.hosts) == ["Nxxxy"]
assert (l.hosts |> Map.values() |> List.first()).friendly_name == "yellow-cat-6"
assert Map.keys(l.instance_tracking) == ["abc123", "abc456", "abc789"]
assert l.providers == %{}
assert l.actors == %{}
end
Expand Down Expand Up @@ -83,7 +82,6 @@ defmodule LatticeObserverTest.Observed.HostsTest do

assert Map.keys(l.hosts) == ["Nxxx"]
assert (l.hosts |> Map.values() |> List.first()).friendly_name == "orange-button-5"
assert Map.keys(l.instance_tracking) == ["abc123", "abc456", "abc789"]

# Host is killed forcefully and starts again using the same seed private key
l =
Expand Down
2 changes: 0 additions & 2 deletions test/observed/providers_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ defmodule LatticeObserverTest.Observed.ProvidersTest do
l = Lattice.new()
l = Lattice.apply_event(l, start)
stamp1 = EventProcessor.timestamp_from_iso8601(start.time)
# ensure idempotence
l = Lattice.apply_event(l, start)

orig_desired = %Lattice{
Lattice.new()
Expand Down

0 comments on commit 8cba7ca

Please sign in to comment.