Skip to content

Commit 853cf70

Browse files
committed
refactor(grpc): rename service Machines->Hyper, RPCs/messages ->Vm
- service hyper.grpc.v0.Machines -> hyper.grpc.v0.Hyper - RPCs: CreateMachine/StopMachine/GetMachine/ListMachines -> CreateVm/StopVm/GetVm/ListVms (handlers create_vm/stop_vm/get_vm/list_vms) - messages: *Machine{Request,Response} -> *Vm{Request,Response}; Machine -> Vm (ListVmsResponse.vms); aligns the wire contract with the BEAM Hyper.create_vm terminology and drops the Machine/stutter naming. Regenerate is automatic (:grpc_gen). Updated server, codec, moduledocs, docs/grpc.md.
1 parent 5df3e85 commit 853cf70

5 files changed

Lines changed: 72 additions & 76 deletions

File tree

docs/grpc.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ create, stop, locate, and list microVMs.
1010
1111
## The contract
1212

13-
The service is `hyper.grpc.v0.Machines`, defined in
13+
The service is `hyper.grpc.v0.Hyper`, defined in
1414
[`proto/hyper/grpc/v0/hyper.proto`](https://github.com/harmont-dev/hyper/blob/main/proto/hyper/grpc/v0/hyper.proto):
1515

1616
| RPC | Purpose | Errors |
1717
| -------------- | -------------------------------------------------- | ------ |
18-
| `CreateMachine`| Boot a microVM from an image; returns its `vm_id`. | `INVALID_ARGUMENT` (bad/missing image or enum), `RESOURCE_EXHAUSTED` (no capacity), `UNAVAILABLE` (host lost mid-create) |
19-
| `StopMachine` | Tear down a running microVM. | `NOT_FOUND` (unknown id), `UNAVAILABLE` (host down) |
20-
| `GetMachine` | Which node a microVM runs on. | `NOT_FOUND` |
21-
| `ListMachines` | Every microVM known to the cluster. ||
18+
| `CreateVm`| Boot a microVM from an image; returns its `vm_id`. | `INVALID_ARGUMENT` (bad/missing image or enum), `RESOURCE_EXHAUSTED` (no capacity), `UNAVAILABLE` (host lost mid-create) |
19+
| `StopVm` | Tear down a running microVM. | `NOT_FOUND` (unknown id), `UNAVAILABLE` (host down) |
20+
| `GetVm` | Which node a microVM runs on. | `NOT_FOUND` |
21+
| `ListVms` | Every microVM known to the cluster. ||
2222

2323
A machine is addressed by its **`vm_id`** — a URL-safe base64 string the server
2424
mints at creation. The server is stateless and identical on every node;
@@ -78,7 +78,7 @@ in config.
7878
### From any language
7979

8080
Generate a client stub from `proto/hyper/grpc/v0/hyper.proto` with your
81-
language's gRPC tooling (`protoc`, `buf`, etc.) and call the `Machines` service.
81+
language's gRPC tooling (`protoc`, `buf`, etc.) and call the `Hyper` service.
8282
The `.proto` ships in the published package as well as the repo.
8383

8484
### From the BEAM
@@ -93,18 +93,18 @@ adapter):
9393
# TLS, verifying the server against a CA bundle
9494
{:ok, ch} = Hyper.Grpc.connect("hyper.example.com:50051", ca: "/etc/hyper/ca.pem")
9595

96-
{:ok, %Hyper.Grpc.V0.CreateMachineResponse{vm_id: vm_id, node: node}} =
97-
Hyper.Grpc.V0.Machines.Stub.create_machine(
96+
{:ok, %Hyper.Grpc.V0.CreateVmResponse{vm_id: vm_id, node: node}} =
97+
Hyper.Grpc.V0.Hyper.Stub.create_vm(
9898
ch,
99-
%Hyper.Grpc.V0.CreateMachineRequest{
99+
%Hyper.Grpc.V0.CreateVmRequest{
100100
img_id: "img-abc",
101101
instance_type: :INSTANCE_TYPE_DECI
102102
}
103103
)
104104

105105
{:ok, _} =
106-
Hyper.Grpc.V0.Machines.Stub.stop_machine(
106+
Hyper.Grpc.V0.Hyper.Stub.stop_vm(
107107
ch,
108-
%Hyper.Grpc.V0.StopMachineRequest{vm_id: vm_id}
108+
%Hyper.Grpc.V0.StopVmRequest{vm_id: vm_id}
109109
)
110110
```

lib/hyper/grpc.ex

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ defmodule Hyper.Grpc do
22
@moduledoc """
33
Public gRPC interface to a Hyper cluster.
44
5-
The service contract is `hyper.grpc.v0.Machines` (see
5+
The service contract is `hyper.grpc.v0.Hyper` (see
66
`proto/hyper/grpc/v0/hyper.proto`). Any gRPC client, in any language, can
77
create, stop, locate, and list microVMs. Off-BEAM clients generate their own
88
stubs from the `.proto`; BEAM clients can use the generated
9-
`Hyper.Grpc.V0.Machines.Stub` together with `connect/2`.
9+
`Hyper.Grpc.V0.Hyper.Stub` together with `connect/2`.
1010
1111
> #### v0 {: .warning}
1212
>
@@ -25,9 +25,9 @@ defmodule Hyper.Grpc do
2525
2626
{:ok, ch} = Hyper.Grpc.connect("hyper.example.com:50051", ca: "/etc/hyper/ca.pem")
2727
{:ok, reply} =
28-
Hyper.Grpc.V0.Machines.Stub.create_machine(
28+
Hyper.Grpc.V0.Hyper.Stub.create_vm(
2929
ch,
30-
%Hyper.Grpc.V0.CreateMachineRequest{img_id: "img-abc"}
30+
%Hyper.Grpc.V0.CreateVmRequest{img_id: "img-abc"}
3131
)
3232
"""
3333

lib/hyper/grpc/codec.ex

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ defmodule Hyper.Grpc.Codec do
99
"""
1010

1111
alias Hyper.Grpc.V0.{
12-
CreateMachineRequest,
13-
CreateMachineResponse,
14-
GetMachineResponse,
15-
ListMachinesResponse,
16-
Machine,
17-
StopMachineResponse
12+
CreateVmRequest,
13+
CreateVmResponse,
14+
GetVmResponse,
15+
ListVmsResponse,
16+
StopVmResponse,
17+
Vm
1818
}
1919

2020
alias Hyper.Vm.Spec
@@ -54,11 +54,11 @@ defmodule Hyper.Grpc.Codec do
5454
@typep arch_enum :: :ARCHITECTURE_X86_64 | :ARCHITECTURE_AARCH64
5555

5656
@doc "Convert an inbound request message to a domain value."
57-
@spec from_grpc(CreateMachineRequest.t()) :: {:ok, Spec.t()} | {:error, term()}
58-
def from_grpc(%CreateMachineRequest{img_id: img_id}) when img_id in [nil, ""],
57+
@spec from_grpc(CreateVmRequest.t()) :: {:ok, Spec.t()} | {:error, term()}
58+
def from_grpc(%CreateVmRequest{img_id: img_id}) when img_id in [nil, ""],
5959
do: {:error, :missing_img_id}
6060

61-
def from_grpc(%CreateMachineRequest{} = req) do
61+
def from_grpc(%CreateVmRequest{} = req) do
6262
with {:ok, type} <- instance_type(req.instance_type),
6363
{:ok, arch} <- arch(req.arch) do
6464
{:ok,
@@ -72,20 +72,20 @@ defmodule Hyper.Grpc.Codec do
7272
end
7373

7474
@doc "Convert a domain result to an outbound response message, or an error to `GRPC.RPCError`."
75-
@spec to_grpc({:created, Hyper.Vm.id(), node()}) :: CreateMachineResponse.t()
75+
@spec to_grpc({:created, Hyper.Vm.id(), node()}) :: CreateVmResponse.t()
7676
def to_grpc({:created, vm_id, node}) when is_binary(vm_id),
77-
do: %CreateMachineResponse{vm_id: vm_id, node: to_string(node)}
77+
do: %CreateVmResponse{vm_id: vm_id, node: to_string(node)}
7878

79-
@spec to_grpc({:located, Hyper.Vm.id(), node()}) :: GetMachineResponse.t()
79+
@spec to_grpc({:located, Hyper.Vm.id(), node()}) :: GetVmResponse.t()
8080
def to_grpc({:located, vm_id, node}),
81-
do: %GetMachineResponse{vm_id: vm_id, node: to_string(node)}
81+
do: %GetVmResponse{vm_id: vm_id, node: to_string(node)}
8282

83-
@spec to_grpc({:machines, [{Hyper.Vm.id(), node()}]}) :: ListMachinesResponse.t()
84-
def to_grpc({:machines, machines}),
85-
do: %ListMachinesResponse{machines: Enum.map(machines, &machine/1)}
83+
@spec to_grpc({:vms, [{Hyper.Vm.id(), node()}]}) :: ListVmsResponse.t()
84+
def to_grpc({:vms, vms}),
85+
do: %ListVmsResponse{vms: Enum.map(vms, &vm/1)}
8686

87-
@spec to_grpc(:stopped) :: StopMachineResponse.t()
88-
def to_grpc(:stopped), do: %StopMachineResponse{}
87+
@spec to_grpc(:stopped) :: StopVmResponse.t()
88+
def to_grpc(:stopped), do: %StopVmResponse{}
8989

9090
@spec to_grpc({:error, term()}) :: GRPC.RPCError.t()
9191
def to_grpc({:error, reason}), do: rpc_error(reason)
@@ -100,8 +100,8 @@ defmodule Hyper.Grpc.Codec do
100100
def to_grpc({:exit, {:nodedown, _}}), do: rpc_error(:machine_unreachable)
101101
def to_grpc({:exit, reason}), do: rpc_error({:stop_failed, reason})
102102

103-
@spec machine({Hyper.Vm.id(), node()}) :: Machine.t()
104-
defp machine({vm_id, node}), do: %Machine{vm_id: vm_id, node: to_string(node)}
103+
@spec vm({Hyper.Vm.id(), node()}) :: Vm.t()
104+
defp vm({vm_id, node}), do: %Vm{vm_id: vm_id, node: to_string(node)}
105105

106106
@spec instance_type(nil) :: {:ok, :base}
107107
defp instance_type(nil), do: {:ok, :base}
@@ -121,10 +121,10 @@ defmodule Hyper.Grpc.Codec do
121121
do: GRPC.RPCError.exception(:invalid_argument, "img_id is required")
122122

123123
defp rpc_error(:not_found),
124-
do: GRPC.RPCError.exception(:not_found, "no such machine")
124+
do: GRPC.RPCError.exception(:not_found, "no such VM")
125125

126126
defp rpc_error(:machine_unreachable),
127-
do: GRPC.RPCError.exception(:unavailable, "machine's host node is unreachable")
127+
do: GRPC.RPCError.exception(:unavailable, "VM's host node is unreachable")
128128

129129
defp rpc_error(reason) when reason in [:no_capacity, :exhausted],
130130
do: GRPC.RPCError.exception(:resource_exhausted, "no capacity")

lib/hyper/grpc/server.ex

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
defmodule Hyper.Grpc.Server do
22
@moduledoc """
3-
gRPC handler for `hyper.grpc.v0.Machines`. A thin translation layer: each RPC
3+
gRPC handler for `hyper.grpc.v0.Hyper`. A thin translation layer: each RPC
44
maps its request to a domain value via `Hyper.Grpc.Codec.from_grpc/1`, calls
55
the existing `Hyper` BEAM API, and maps the result back with
66
`Hyper.Grpc.Codec.to_grpc/1` (raising the `GRPC.RPCError` it returns on error).
@@ -10,24 +10,23 @@ defmodule Hyper.Grpc.Server do
1010
so a call landing on any node is correct.
1111
"""
1212

13-
use GRPC.Server, service: Hyper.Grpc.V0.Machines.Service
13+
use GRPC.Server, service: Hyper.Grpc.V0.Hyper.Service
1414

1515
alias Hyper.Grpc.Codec
1616

1717
alias Hyper.Grpc.V0.{
18-
CreateMachineRequest,
19-
CreateMachineResponse,
20-
GetMachineRequest,
21-
GetMachineResponse,
22-
ListMachinesRequest,
23-
ListMachinesResponse,
24-
StopMachineRequest,
25-
StopMachineResponse
18+
CreateVmRequest,
19+
CreateVmResponse,
20+
GetVmRequest,
21+
GetVmResponse,
22+
ListVmsRequest,
23+
ListVmsResponse,
24+
StopVmRequest,
25+
StopVmResponse
2626
}
2727

28-
@spec create_machine(CreateMachineRequest.t(), GRPC.Server.Stream.t()) ::
29-
CreateMachineResponse.t()
30-
def create_machine(%CreateMachineRequest{} = req, _stream) do
28+
@spec create_vm(CreateVmRequest.t(), GRPC.Server.Stream.t()) :: CreateVmResponse.t()
29+
def create_vm(%CreateVmRequest{} = req, _stream) do
3130
with {:ok, spec} <- Codec.from_grpc(req),
3231
{:ok, pid} <- Hyper.create_vm(spec),
3332
vm_id when is_binary(vm_id) <- Hyper.id(pid) do
@@ -40,27 +39,24 @@ defmodule Hyper.Grpc.Server do
4039
end
4140
end
4241

43-
@spec stop_machine(StopMachineRequest.t(), GRPC.Server.Stream.t()) ::
44-
StopMachineResponse.t()
45-
def stop_machine(%StopMachineRequest{vm_id: vm_id}, _stream) do
42+
@spec stop_vm(StopVmRequest.t(), GRPC.Server.Stream.t()) :: StopVmResponse.t()
43+
def stop_vm(%StopVmRequest{vm_id: vm_id}, _stream) do
4644
Hyper.Node.FireVMM.State.stop(vm_id)
4745
Codec.to_grpc(:stopped)
4846
catch
4947
:exit, reason -> raise Codec.to_grpc({:exit, reason})
5048
end
5149

52-
@spec get_machine(GetMachineRequest.t(), GRPC.Server.Stream.t()) ::
53-
GetMachineResponse.t()
54-
def get_machine(%GetMachineRequest{vm_id: vm_id}, _stream) do
50+
@spec get_vm(GetVmRequest.t(), GRPC.Server.Stream.t()) :: GetVmResponse.t()
51+
def get_vm(%GetVmRequest{vm_id: vm_id}, _stream) do
5552
case Hyper.whereis(vm_id) do
5653
nil -> raise Codec.to_grpc({:error, :not_found})
5754
node -> Codec.to_grpc({:located, vm_id, node})
5855
end
5956
end
6057

61-
@spec list_machines(ListMachinesRequest.t(), GRPC.Server.Stream.t()) ::
62-
ListMachinesResponse.t()
63-
def list_machines(%ListMachinesRequest{}, _stream) do
64-
Codec.to_grpc({:machines, Hyper.Cluster.Routing.all()})
58+
@spec list_vms(ListVmsRequest.t(), GRPC.Server.Stream.t()) :: ListVmsResponse.t()
59+
def list_vms(%ListVmsRequest{}, _stream) do
60+
Codec.to_grpc({:vms, Hyper.Cluster.Routing.all()})
6561
end
6662
end

proto/hyper/grpc/v0/hyper.proto

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,21 @@ package hyper.grpc.v0;
55
// Hyper microVM orchestration.
66
//
77
// v0: unstable. This contract may change without notice during early
8-
// development. Every machine is addressed by its `vm_id`, a URL-safe base64
8+
// development. Every VM is addressed by its `vm_id`, a URL-safe base64
99
// string minted by the server at creation time.
10-
service Machines {
10+
service Hyper {
1111
// Create and boot a microVM from an image. Placement is cluster-wide and
1212
// automatic. Returns the minted vm_id and the node the guest booted on.
13-
rpc CreateMachine(CreateMachineRequest) returns (CreateMachineResponse);
13+
rpc CreateVm(CreateVmRequest) returns (CreateVmResponse);
1414

1515
// Stop and tear down a running microVM. NOT_FOUND if no such vm_id runs.
16-
rpc StopMachine(StopMachineRequest) returns (StopMachineResponse);
16+
rpc StopVm(StopVmRequest) returns (StopVmResponse);
1717

1818
// Locate a microVM: the node it currently runs on. NOT_FOUND if unknown.
19-
rpc GetMachine(GetMachineRequest) returns (GetMachineResponse);
19+
rpc GetVm(GetVmRequest) returns (GetVmResponse);
2020

2121
// List every microVM currently known to the cluster.
22-
rpc ListMachines(ListMachinesRequest) returns (ListMachinesResponse);
22+
rpc ListVms(ListVmsRequest) returns (ListVmsResponse);
2323
}
2424

2525
// Fixed (vCPU, memory, disk) sizes, like cloud instance classes.
@@ -43,40 +43,40 @@ enum Architecture {
4343
ARCHITECTURE_AARCH64 = 1;
4444
}
4545

46-
message CreateMachineRequest {
46+
message CreateVmRequest {
4747
string img_id = 1; // required: content-addressed image id
4848
optional InstanceType instance_type = 2; // unset -> server default (base)
4949
optional Architecture arch = 3; // unset -> resolve to the node's arch
5050
optional string boot_args = 4; // unset -> default kernel cmdline
5151
}
5252

53-
message CreateMachineResponse {
53+
message CreateVmResponse {
5454
string vm_id = 1;
5555
string node = 2; // erlang node the guest booted on
5656
}
5757

58-
message StopMachineRequest {
58+
message StopVmRequest {
5959
string vm_id = 1;
6060
}
6161

62-
message StopMachineResponse {}
62+
message StopVmResponse {}
6363

64-
message GetMachineRequest {
64+
message GetVmRequest {
6565
string vm_id = 1;
6666
}
6767

68-
message GetMachineResponse {
68+
message GetVmResponse {
6969
string vm_id = 1;
7070
string node = 2;
7171
}
7272

73-
message ListMachinesRequest {}
73+
message ListVmsRequest {}
7474

75-
message ListMachinesResponse {
76-
repeated Machine machines = 1;
75+
message ListVmsResponse {
76+
repeated Vm vms = 1;
7777
}
7878

79-
message Machine {
79+
message Vm {
8080
string vm_id = 1;
8181
string node = 2;
8282
}

0 commit comments

Comments
 (0)