Stateful Functions applications are composed of one or more modules. A module is a bundle of functions loaded by the runtime and made available to be messaged. Functions from all loaded modules are multiplexed and free to message each other arbitrarily.
Stateful Functions supports two types of modules: Remote and Embedded.
Remote modules run as external processes from the Apache Flink® runtime; in the same container, as a sidecar, using a serverless platform or other external location. This module type can support any number of language SDKs. Remote modules are registered with the system via YAML
configuration files.
A remote module configuration consists of a meta
section and a spec
section. The meta
contains auxiliary information about the module; while spec
describes the functions contained within the module and defines their persisted values.
module.spec.functions
declares a list of function
objects that are implemented by the remote module. A function
is described via a number of properties:
function.meta.kind
http
function.meta.type
<namespace>/<name>
.function.spec.endpoint
http
, https
.http+unix
or https+unix
.http+unix://<socket-file-path>/<serve-url-path>
. For example, http+unix:///uds.sock/path/of/url
.function.spec.states
name
property and an optional expireAfter
property.expireAfter
- 0, meaning that state expiration is disabled.function.spec.maxNumBatchRequests
address
before invoking backpressure on the system.function.spec.timeout
function.spec.connectTimeout
function.spec.readTimeout
function.spec.writeTimeout
{% highlight yaml %} version: “2.0”
module: meta: type: remote spec: functions: - function: meta: kind: http type: example/greeter spec: endpoint: http:///statefun states: - name: seen_count expireAfter: 5min maxNumBatchRequests: 500 timeout: 2min {% endhighlight %}
Embedded modules are co-located with, and embedded within, the Apache Flink® runtime.
This module type only supports JVM-based languages and is defined by implementing the StatefulFunctionModule
interface. Embedded modules offer a single configuration method where stateful functions bind to the system based on their [function type]({{ site.baseurl }}/concepts/logical.html#function-address). Runtime configurations are available through the globalConfiguration
, which is the union of all configurations in the applications flink-conf.yaml
under the prefix statefun.module.global-config
, and any command line arguments passed in the form --key value
.
{% highlight java %} package org.apache.flink.statefun.docs;
import java.util.Map; import org.apache.flink.statefun.sdk.spi.StatefulFunctionModule;
public class BasicFunctionModule implements StatefulFunctionModule {
public void configure(Map<String, String> globalConfiguration, Binder binder) { // Declare the user function and bind it to its type binder.bindFunctionProvider(FnWithDependency.TYPE, new CustomProvider()); // Stateful functions that do not require any configuration // can declare their provider using java 8 lambda syntax binder.bindFunctionProvider(Identifiers.HELLO_TYPE, unused -> new FnHelloWorld()); }
} {% endhighlight %}
Embedded modules leverage Java’s Service Provider Interfaces (SPI) for discovery. This means that every JAR should contain a file org.apache.flink.statefun.sdk.spi.StatefulFunctionModule
in the META_INF/services
resource directory that lists all available modules that it provides.
{% highlight none %} org.apache.flink.statefun.docs.BasicFunctionModule {% endhighlight %}