Plugin hosts: operations in other languages, run in OS processes the proxy
supervises (PluginHosts.Host, PluginHosts.Pool), called by the steps of
the plugin of their kind (PluginHosts.Kind): Plugins.Camel for Apache
Camel routes, Plugins.Command for a program in any language that speaks
the protocol. The steps' own logic is PluginHosts.Step. Design and
measurements: Alternera's docs/EXTERNAL_PLUGINS_RESEARCH.md.
This package holds no plugin: it is the runtime the two plugins share. It calls nothing of the proxy, so it can move into the SDK.
Configuration
Whoever runs the proxy defines the hosts, with their credentials, in the
application environment of their kind's plugin
(c:PluginHosts.Kind.hosts_app/0), and every host belongs to one tenant,
named by its slug:
config :alternera_plugin_camel,
hosts: [
%{name: "erp", pool: %{min: 0, max: 8, idle_minutes: 30},
routes: ["file:/etc/alternera/routes/erp/*.yaml"],
properties: "file:/etc/alternera/routes/erp/erp.properties",
variables: ["ERP_SCHEMA", "secret.ERP_PASSWORD"],
tenant: "acme"}
]Each host is a pool of members (PluginHosts.Pool): pool says how
many, min (default 1, at least 0), max (default 4, from min and 1
to 64), idle_minutes (default 10) and grow_at (default 8). The pool
runs min members, and starts one more, up to max, when every ready
member has at least grow_at calls in flight (a call over HTTP counts as
one), one start at a time, the calls never waiting for it ("Growing" in
PluginHosts.Pool.Manager). It also goes past min for a rolling
restart of a lone member (Pool.rolling_restart/2) and, at min: 0,
for the one member a caller waits for. A member beyond min with no call for
idle_minutes is drained and stopped (Pool.Manager); pool_status/3
shows how many run and how long each has been idle. With min: 0 the
host runs nothing until it is needed: a call, a check, a vocabulary or
a describe starts a member and waits for it within its deadline
("Starting on demand" in PluginHosts.Pool), and the last member stops
after idle_minutes without a call. A catalogue starts one only before
any member was ever ready; after that it answers the last one.
status/3, pool_status/3 and a reload start none. A start on demand
comes out of the caller's deadline: a step's call has what is left of
the step's time (5 s when the runner gives none), Try 15 s, a check, a
vocabulary or a catalogue 5 s, a describe 11 s. How long a JVM with a
heavy route set takes to start has not been measured, so a host on a hot
path keeps min at 1 or more. Each member is an OS process of its own,
with its own state and its own connections: a pool at max holds max
times what one member holds.
An entry that says size, or a pool outside those bounds, is
logged at boot and is no host at all, like an unknown protocol. The kind builds the
command from the rest of the entry (c:PluginHosts.Kind.host_spec/1).
protocol says how calls reach the host, and so whether they are
recorded:
protocol | Calls | Recorded as exchanges |
|---|---|---|
:frames (default) | JSON frames over the port | no |
:msgpack | MessagePack frames over the port | no |
:http1 | HTTP/1.1 on a loopback port | yes |
:http2 | HTTP/2 (h2c) on a loopback port | yes |
:https1 | HTTP/1.1 over TLS on a loopback port | yes |
:https2 | HTTP/2 over TLS on a loopback port | yes |
An entry with another value is logged at boot and is no host at all:
configured/1 leaves it out (invalid_hosts/1). tenant is the slug
of the tenant whose steps call the host (Accounts.Tenant's slug,
which only a super admin changes). Host names are per tenant: a step's
host: "erp" is its own tenant's erp, two tenants may each have one,
and no tenant reaches another's. An entry without a tenant, with the
tenants list of before, or with the name and tenant of an earlier
entry is likewise no host at all. The slug is looked up when the
host starts (PluginHosts.Starter), not at every call: the pool starts
once it names a tenant, and an entry whose slug names none is logged and
looked up again, so creating the tenant, or giving it that slug, brings
the host up. host_opts passes Host tunables.
variables lists the tenant variables the host's own routes read, as a
route writes them (["ERP_SCHEMA", "secret.ERP_PASSWORD"]): the tenant's
global variables of those names go to each member in a variables
frame before its hello (PluginHosts.Host), never in its environment
or arguments. A name that does not resolve keeps the member from
starting, and the backoff tries again; saving one restarts the host's
members one at a time, and deleting one stops them until it is defined
again (PluginHosts.Refresher). Anything but a list of
such names, each once, is logged at boot and is no host at all.
reload lets the step editor reload the host (reload/3), for writing
its operations: false (the default), true (a Reload button beside the
host), or, for a Camel host, :watch (the button, and the host reloads
its routes whenever their files change). A kind whose hosts reload in
place (c:PluginHosts.Kind.reloads_in_place?/0, Camel) reads them again
without a new process; any other restarts its members one at a time.
With no hosts configured nothing starts but the registries.
written_routes lets steps write the operations they call
(PluginHosts.Definition) on a host of a kind that takes them
(c:PluginHosts.Kind.definitions?/0, Camel): its value is the kind's
rules for what a document may hold (written_routes?/3).
Every running host is known by its key, {tenant_id, name} (key/2):
its pool's registrations, its :pg groups, its HTTP channel's name
(PluginHosts.Endpoint) and every lookup go by it.
A cluster
Each node starts the hosts its own environment lists (configured/1).
A host another node configures is a host here too (hosts/1,
host/3), as that node describes it (shared_entry/2), whether a
member runs there or not (min: 0). A call, a check, a catalogue or a
reload of it goes to a node that runs the tenant's host of that name
(PluginHosts.Cluster), one with a ready member first, else one that
runs none and starts one within the call's deadline. A node that runs
the tenant's host calls only its own pool (here?/3); the node a call
is forwarded to answers it from its own pool of that key, or not at
all. status/3 is this node's members only.
Summary
Functions
Whether the tenant has a host name of kind (host/3): this node
runs it, or another node does. A host whose slug is not looked up yet
belongs to no tenant.
Whether the tenant's host name of kind reloads on its own when its
files change: its reload is :watch and its kind reloads in place
(Camel).
Calls op on the host name of kind that belongs to :tenant_id (an
option), with Pool.call/4: on this node's pool when it runs it, else
on another node's that does. {:error, :no_ready_host} when no node
runs it; {:error, :remote_failed} when the nodes tried failed the call.
The operations the tenant's host name of kind serves, from a ready
member's hello: {:ok, [%{"name", "description", …}]}, or {:error, :not_found} (the tenant runs no such host) or {:error, :not_ready}. With no member ready it
answers the last catalogue a member had (Pool.catalogue/1), so the
step editor, which reads it as it renders, never waits on a restart; only
before any member was ever ready (min: 0, or a pool still booting) does
it wait up to 5 s for one, starting one when none runs.
Has a ready member of kind's host name check a document a step
writes without defining it (Host.check/3): {:ok, %{"routes", "inputs", "diagnostics"}}, or {:error, reason}: :not_accepted (the
host takes no written documents), :not_found, :not_ready,
:host_unavailable or the member's error. entry is the route the step
names, or nil. With no member ready it waits for one, starting one when
none runs (min: 0), within the deadline. Options: :deadline_ms
(default 5000, the wait included); :values, the tenant variables the text
reads (PluginHosts.Definition), none by default, so each {{…}} is
refused.
kind's hosts this node's application environment configures, that can start.
The default log file of a host entry:
logs/plugin-host-<kind>-<tenant slug>-<name>.log.
What a person reads about the calls of the tenant's host name of
kind: %{label, recorded?}, or nil for no such host. Calls over HTTP
go through the forward side and are exchanges of their own; calls over
the port are not.
The tables, views and procedures of a database kind's host name lets
a written route use (bean, one of its written_routes.beans), or with
object (%{"table" => %{"schema", "name"}} or %{"procedure" => …}) one table's columns or one procedure's parameters
(Host.describe/3): {:ok, map}, or {:error, reason} as
vocabulary/4 gives it, and the host's own ({:op_failed, type, message, %{"retry" => boolean}}, type one of unknown_bean,
describe_failed, describe_timeout, describe_busy and
describe_unavailable; retry says whether asking again later may
answer).
Options: :deadline_ms (default 11000: the host gives up after 10 s).
Whether this node runs the tenant's host name of kind: a pool of its
key (key/2). Such a node never forwards the tenant's requests of it to
another node; any other forwards to one that runs it.
The tenant's host name of kind: this node's entry when this node runs
it (here?/3), else the first other node's description of it, by node
name (shared_entry/2), or nil. Two tenants may each have a host of one
name; each sees only its own.
The secrets the tenant's host name of kind reads (its entry's
secret. variables, variables/1) with their values, %{} for none:
what a step takes out of a call's answer to a route of the host's own
files (PluginHosts.Step). Kept per host for 60 s in the
kind's Refresher.secrets_table/1, which a change of the tenant's
variables clears (variables_changed/2), so a busy step does not read
them on every call. A read that fails is logged and gives none: the host
scrubs what it says itself, this is the proxy's second guard.
kind's hosts in the installation as this node sees them, whichever
tenant they belong to: those it configures, and those only another node
configures, as that node describes them (shared_entry/2), each host
once: a host this node configures is its own entry, whether its slug is
looked up here yet or not. What a tenant may call is names_for/2.
Whether the tenant's host name of kind serves calls over HTTP or
HTTPS (PluginHosts.HttpCall).
kind's configured entries configured/1 leaves out, each with why:
{entry, problem} (problem/1, or a second entry for one tenant and
name).
The key of the tenant's host name: what its pool registers, its
:pg groups and its HTTP channel go by.
Whether name can be a host's name (what a step's host setting holds).
The names of the tenant's hosts of kind, on this node or another.
A host entry's pool, its defaults filled in (problem/1 said nil).
The Pool child spec of one of kind's configured host entries, for the
tenant whose id is tenant_id (what its tenant slug names:
PluginHosts.Starter looks it up). The pool registers under the host's
key (key/2) with the entry as its value.
The registry kind's ready pool members join, by {:pool, key}.
The tenant's host name of kind on this node as a pool
(Pool.summary/1): {:ok, %{running, min, max, idle_ms, waiting, catalogue, members}}, each member its status with its slot and
idle_ms, catalogue the last a member had (so a pool running none
still says what it serves); or {:error, :not_found}. It starts no
member.
Why a host entry cannot start, or nil when it can.
The protocol of the tenant's host name of kind (see the moduledoc),
or nil for no such host.
The protocols a host entry can name.
The registry of kind's pools, by {:pool_sup, key} (key/2), each
with its host entry as its value.
Reloads the tenant's host name of kind (see the moduledoc) on every
node that runs it for the tenant, never another tenant's host of that
name: in place on every member, or by a rolling restart.
{:ok, catalogue}, or {:error, reason}: :not_found, :not_reloadable (its reload is off),
:not_running (no node runs a member of it, min: 0: none is started,
and the next one reads the files as it starts),
{:reload_failed, message} (the host kept its old operations) or a
member's error.
Whether the tenant's host name of kind may be reloaded from the step
editor (its reload).
What this node tells another of kind's host key (key/2) when it
runs it: the name, tenant (the slug), protocol, written_routes,
reload and variables (the names) of its entry, defaults filled in,
and the tenant_id; never its command, paths, credentials or values. Nil when this node has no such pool.
Every member's status of the tenant's host name of kind on this
node (none for a pool that runs none: min: 0; it starts none), or
{:error, :not_found}.
Whether a host entry's protocol can be started.
The keys (key/2) of the tenant's pools of kind on this node whose
entry reads variables.
The tenant's hosts of kind that read tenant variables (variables/1),
on this node or another, each %{name, variables}, sorted by name: what
the Variables page marks.
The values of the tenant's variables names (variables/1), for a
member's variables frame (PluginHosts.Host): {:ok, %{name => value}},
each secret.NAME decrypted, {:error, reasons} when one is not
defined, is of the other kind or does not decrypt (each reason names
the variable, never a value), or {:error, :unreadable} when the
tenant's variables could not be read at all, which says nothing of
whether they are defined. The tenant's global variables, as a step reads
them (Plugin.Host.read_tenant_vars/2).
The tenant variables a host entry reads (its variables, as a route
writes them: NAME, secret.NAME), none when it lists none.
The tenant's variables changed (one was saved or deleted): each of the
tenant's hosts of kind on this node that reads variables restarts its
members one at a time when what they started with is no longer what
the variables say (PluginHosts.Refresher). Returns at once; every
node does this for its own pools.
What the editor completes in a document written for kind's host name
(Host.vocabulary/2): {:ok, map}, or {:error, reason} as check/6
gives it. Options: :deadline_ms (default 5000).
kind's hosts the tenant may call that take written documents, by name.
Whether the tenant's host name of kind takes documents a step
writes: its kind takes them (c:PluginHosts.Kind.definitions?/0) and
its entry has written_routes.
Functions
Whether the tenant has a host name of kind (host/3): this node
runs it, or another node does. A host whose slug is not looked up yet
belongs to no tenant.
Whether the tenant's host name of kind reloads on its own when its
files change: its reload is :watch and its kind reloads in place
(Camel).
Calls op on the host name of kind that belongs to :tenant_id (an
option), with Pool.call/4: on this node's pool when it runs it, else
on another node's that does. {:error, :no_ready_host} when no node
runs it; {:error, :remote_failed} when the nodes tried failed the call.
The operations the tenant's host name of kind serves, from a ready
member's hello: {:ok, [%{"name", "description", …}]}, or {:error, :not_found} (the tenant runs no such host) or {:error, :not_ready}. With no member ready it
answers the last catalogue a member had (Pool.catalogue/1), so the
step editor, which reads it as it renders, never waits on a restart; only
before any member was ever ready (min: 0, or a pool still booting) does
it wait up to 5 s for one, starting one when none runs.
@spec check(module(), String.t(), term(), String.t(), String.t() | nil, keyword()) :: {:ok, map()} | {:error, term()}
Has a ready member of kind's host name check a document a step
writes without defining it (Host.check/3): {:ok, %{"routes", "inputs", "diagnostics"}}, or {:error, reason}: :not_accepted (the
host takes no written documents), :not_found, :not_ready,
:host_unavailable or the member's error. entry is the route the step
names, or nil. With no member ready it waits for one, starting one when
none runs (min: 0), within the deadline. Options: :deadline_ms
(default 5000, the wait included); :values, the tenant variables the text
reads (PluginHosts.Definition), none by default, so each {{…}} is
refused.
kind's hosts this node's application environment configures, that can start.
The default log file of a host entry:
logs/plugin-host-<kind>-<tenant slug>-<name>.log.
What a person reads about the calls of the tenant's host name of
kind: %{label, recorded?}, or nil for no such host. Calls over HTTP
go through the forward side and are exchanges of their own; calls over
the port are not.
@spec describe(module(), String.t(), term(), String.t(), map(), keyword()) :: {:ok, map()} | {:error, term()}
The tables, views and procedures of a database kind's host name lets
a written route use (bean, one of its written_routes.beans), or with
object (%{"table" => %{"schema", "name"}} or %{"procedure" => …}) one table's columns or one procedure's parameters
(Host.describe/3): {:ok, map}, or {:error, reason} as
vocabulary/4 gives it, and the host's own ({:op_failed, type, message, %{"retry" => boolean}}, type one of unknown_bean,
describe_failed, describe_timeout, describe_busy and
describe_unavailable; retry says whether asking again later may
answer).
Options: :deadline_ms (default 11000: the host gives up after 10 s).
Whether this node runs the tenant's host name of kind: a pool of its
key (key/2). Such a node never forwards the tenant's requests of it to
another node; any other forwards to one that runs it.
The tenant's host name of kind: this node's entry when this node runs
it (here?/3), else the first other node's description of it, by node
name (shared_entry/2), or nil. Two tenants may each have a host of one
name; each sees only its own.
@spec host_secrets(module(), String.t(), String.t() | nil) :: %{ required(String.t()) => String.t() }
The secrets the tenant's host name of kind reads (its entry's
secret. variables, variables/1) with their values, %{} for none:
what a step takes out of a call's answer to a route of the host's own
files (PluginHosts.Step). Kept per host for 60 s in the
kind's Refresher.secrets_table/1, which a change of the tenant's
variables clears (variables_changed/2), so a busy step does not read
them on every call. A read that fails is logged and gives none: the host
scrubs what it says itself, this is the proxy's second guard.
kind's hosts in the installation as this node sees them, whichever
tenant they belong to: those it configures, and those only another node
configures, as that node describes them (shared_entry/2), each host
once: a host this node configures is its own entry, whether its slug is
looked up here yet or not. What a tenant may call is names_for/2.
Whether the tenant's host name of kind serves calls over HTTP or
HTTPS (PluginHosts.HttpCall).
kind's configured entries configured/1 leaves out, each with why:
{entry, problem} (problem/1, or a second entry for one tenant and
name).
The key of the tenant's host name: what its pool registers, its
:pg groups and its HTTP channel go by.
Whether name can be a host's name (what a step's host setting holds).
The names of the tenant's hosts of kind, on this node or another.
@spec pool(map()) :: %{ min: non_neg_integer(), max: pos_integer(), idle_minutes: pos_integer(), grow_at: pos_integer() }
A host entry's pool, its defaults filled in (problem/1 said nil).
The Pool child spec of one of kind's configured host entries, for the
tenant whose id is tenant_id (what its tenant slug names:
PluginHosts.Starter looks it up). The pool registers under the host's
key (key/2) with the entry as its value.
The registry kind's ready pool members join, by {:pool, key}.
The tenant's host name of kind on this node as a pool
(Pool.summary/1): {:ok, %{running, min, max, idle_ms, waiting, catalogue, members}}, each member its status with its slot and
idle_ms, catalogue the last a member had (so a pool running none
still says what it serves); or {:error, :not_found}. It starts no
member.
Why a host entry cannot start, or nil when it can.
The protocol of the tenant's host name of kind (see the moduledoc),
or nil for no such host.
The protocols a host entry can name.
The registry of kind's pools, by {:pool_sup, key} (key/2), each
with its host entry as its value.
Reloads the tenant's host name of kind (see the moduledoc) on every
node that runs it for the tenant, never another tenant's host of that
name: in place on every member, or by a rolling restart.
{:ok, catalogue}, or {:error, reason}: :not_found, :not_reloadable (its reload is off),
:not_running (no node runs a member of it, min: 0: none is started,
and the next one reads the files as it starts),
{:reload_failed, message} (the host kept its old operations) or a
member's error.
Whether the tenant's host name of kind may be reloaded from the step
editor (its reload).
Every member's status of the tenant's host name of kind on this
node (none for a pool that runs none: min: 0; it starts none), or
{:error, :not_found}.
Whether a host entry's protocol can be started.
The keys (key/2) of the tenant's pools of kind on this node whose
entry reads variables.
@spec variable_readers(module(), String.t() | nil) :: [ %{name: String.t(), variables: [String.t()]} ]
The tenant's hosts of kind that read tenant variables (variables/1),
on this node or another, each %{name, variables}, sorted by name: what
the Variables page marks.
@spec variable_values(String.t(), [String.t()]) :: {:ok, %{required(String.t()) => String.t()}} | {:error, [String.t()] | :unreadable}
The values of the tenant's variables names (variables/1), for a
member's variables frame (PluginHosts.Host): {:ok, %{name => value}},
each secret.NAME decrypted, {:error, reasons} when one is not
defined, is of the other kind or does not decrypt (each reason names
the variable, never a value), or {:error, :unreadable} when the
tenant's variables could not be read at all, which says nothing of
whether they are defined. The tenant's global variables, as a step reads
them (Plugin.Host.read_tenant_vars/2).
The tenant variables a host entry reads (its variables, as a route
writes them: NAME, secret.NAME), none when it lists none.
The tenant's variables changed (one was saved or deleted): each of the
tenant's hosts of kind on this node that reads variables restarts its
members one at a time when what they started with is no longer what
the variables say (PluginHosts.Refresher). Returns at once; every
node does this for its own pools.
What the editor completes in a document written for kind's host name
(Host.vocabulary/2): {:ok, map}, or {:error, reason} as check/6
gives it. Options: :deadline_ms (default 5000).
kind's hosts the tenant may call that take written documents, by name.
Whether the tenant's host name of kind takes documents a step
writes: its kind takes them (c:PluginHosts.Kind.definitions?/0) and
its entry has written_routes.