You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by GitBox <gi...@apache.org> on 2020/03/26 19:48:36 UTC

[GitHub] [flink-statefun] sjwiesman opened a new pull request #77: [FLINK-16730][docs] Python SDK Getting Started

sjwiesman opened a new pull request #77: [FLINK-16730][docs] Python SDK Getting Started
URL: https://github.com/apache/flink-statefun/pull/77
 
 
   This replaces the old Java walkthrough, which we all agreed was lacking, with an updated Python walkthrough. I think having python only is fine but I'm also comfortable adding Java as a follow up if there is consensus. 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [flink-statefun] sjwiesman commented on a change in pull request #77: [FLINK-16730][docs] Python SDK Getting Started

Posted by GitBox <gi...@apache.org>.
sjwiesman commented on a change in pull request #77: [FLINK-16730][docs] Python SDK Getting Started
URL: https://github.com/apache/flink-statefun/pull/77#discussion_r399390112
 
 

 ##########
 File path: docs/getting-started/walkthrough.md
 ##########
 @@ -24,177 +24,411 @@ specific language governing permissions and limitations
 under the License.
 -->
 
+Stateful Functions offers a platform for building robust, stateful event-driven applications.
+It provides fine-grained control over state and time, which allows for the implementation of advanced systems.
+In this step-by-step guide you’ll learn how to build a stateful applications with the Stateful Functions API.
+
+* This will be replaced by the TOC
+{:toc}
+
+## What Are You Building?
+
 Like all great introductions in software, this walkthrough will start at the beginning: saying hello.
 The application will run a simple function that accepts a request and responds with a greeting.
 It will not attempt to cover all the complexities of application development, but instead focus on building a stateful function — which is where you will implement your business logic.
 
-* This will be replaced by the TOC
-{:toc}
+## Prerequisites
+
+This walkthrough assumes that you have some familiarity with Python, but you should be able to follow along even if you are coming from a different programming language.
+
+## Help, I’m Stuck! 
+
+If you get stuck, check out the [community support resources](https://flink.apache.org/gettinghelp.html).
+In particular, Apache Flink's [user mailing list](https://flink.apache.org/community.html#mailing-lists) is consistently ranked as one of the most active of any Apache project and a great way to get help quickly.
 
-## A Basic Hello
+## How to Follow Along
+
+If you want to follow along, you will require a computer with [Python 3](https://www.python.org/) along with [Docker](https://www.docker.com/).
+
+{% panel **Note:** Each code block within this walkthrough may not contain the full surrounding class for brevity.
+The full code is available on [at the bottom of this page](#full-application). %}
+
+You can download a zip file with a skeleton project by clicking [here]({{ site.baseurl }}/downloads/walkthrough.zip).
+
+{% unless site.is_stable %}
+<p style="border-radius: 5px; padding: 5px" class="bg-danger">
+    <b>Note</b>: The Stateful Functions project does not publish snapshot versions of the Python SDK to PyPy.
+    Please consider using a stable version of this guide.
+</p>
+{% endunless %}
+
+After unzipping the package, you will find a number of files.
+These include dockerfiles and data generators to run this walkthrough in a local self contained environment.
+
+{% highlight bash %}
+$ tree statefun-walkthrough
+statefun-walkthrough
+├── Dockerfile
+├── README.md
+├── docker-compose.yml
+├── generator
+│   ├── Dockerfile
+│   ├── event-generator.py
+│   └── messages_pb2.py
+├── greeter
+│   ├── Dockerfile
+│   ├── greeter.py
+│   ├── messages.proto
+│   ├── messages_pb2.py
+│   └── requirements.txt
+└── module.yaml
+{% endhighlight %}
 
-Greeting actions are triggered by consuming, routing and passing messages that are defined using ProtoBuf.
+## Start With Events
+
+Stateful Functions is an event driven system, so development begins by defining our events.
+The greeter application will define its events using [protocol buffers](https://developers.google.com/protocol-buffers). 
+When a greet request for a particular user is ingested, it will be routed to the appropriate function.
+The response will be returned with an appropriate greeting.
+The third type, `SeenCount`, is a utility class that will be used latter on to help manage the number of times a user has been seen so far.
 
 {% highlight proto %}
 syntax = "proto3";
 
+package example;
+
+// External request sent by a user who wants to be greeted
 message GreetRequest {
-    string who = 1;
+    // The name of the user to greet
+    string name = 1;
 }
-
+// A customized response sent to the user
 message GreetResponse {
-    string who = 1;
+    // The name of the user being greeted
+    string name = 1;
+    // The users customized greeting
     string greeting = 2;
 }
+// An internal message used to store state
+message SeenCount {
+    // The number of times a users has been seen so far
+    int64 seen = 1;
+}
 {% endhighlight %}
 
-Under the hood, messages are processed using [stateful functions]({{ site.baseurl }}/sdk/java.html), by definition any class that implements the ``StatefulFunction`` interface.
 
-{% highlight java %}
-package org.apache.flink.statefun.examples.greeter;
+## Our First Function
 
-import org.apache.flink.statefun.sdk.Context;
-import org.apache.flink.statefun.sdk.StatefulFunction;
+Under the hood, messages are processed using [stateful functions]({{ site.baseurl }}/sdk/python.html), which is any two argument function that is bound to the ``StatefulFunction`` runtime.
+Functions are bound to the runtime with the `@function.bind` decorator.
+When binding a function, it is annotated with a function type.
+This is the name used to reference this function when sending it messages.
 
-public final class GreetFunction implements StatefulFunction {
+When you open the file `greeter/greeter.py` you should see the following code.
 
-    @Override
-    public void invoke(Context context, Object input) {
-        GreetRequest greetMessage = (GreetRequest) input;
+{% highlight python %}
+from statefun import StatefulFunctions
 
-        GreetResponse response = GreetResponse.newBuilder()
-            .setWho(greetMessage.getWho())
-            .setGreeting("Hello " + greetMessage.getWho())
-            .build();
+functions = StatefulFunctions()
 
-        context.send(GreetingConstants.GREETING_EGRESS_ID, response);
-    }
-}
+@functions.bind("example/greeter")
+def greet(context, greet_request):
+    pass
 {% endhighlight %}
 
+A stateful function takes two arguments, a context and message. 
+The [context]({{ site.baseurl }}/sdk/python.html#context-reference) provides access to stateful functions runtime features such as state management and message passing.
+You will explore some of these features as you progress through this walkthrough. 
 
-This function takes in a request and sends a response to an external system (or [egress]({{ site.baseurl }}/io-module/index.html#egress)).
-While this is nice, it does not show off the real power of stateful functions: handling state.
+The other parameter is the input message that has been passed to this function.
+By default messages are passed around as protobuf [Any](https://developers.google.com/protocol-buffers/docs/reference/python-generated#wkt).
+If a function only accepts a known type, you can override the message type using Python 3 type syntax.
+This way you do not need to unwrap the message or check types.
 
-## A Stateful Hello
+{% highlight python %}
+from messages_pb2 import GreetRequest
+from statefun import StatefulFunctions
 
-Suppose you want to generate a personalized response for each user depending on how many times they have sent a request.
+functions = StatefulFunctions()
 
-{% highlight java %}
-private static String greetText(String name, int seen) {
-    switch (seen) {
-        case 0:
-            return String.format("Hello %s !", name);
-        case 1:
-            return String.format("Hello again %s !", name);
-        case 2:
-            return String.format("Third times the charm! %s!", name);
-        case 3:
-            return String.format("Happy to see you once again %s !", name);
-        default:
-            return String.format("Hello at the %d-th time %s", seen + 1, name);
-}
+@functions.bind("example/greeter")
+def greet(context, greet_request: GreetRequest):
+    pass
 {% endhighlight %}
 
-## Routing Messages
+## Sending A Response
 
-To send a user a personalized greeting, the system needs to keep track of how many times it has seen each user so far.
-Speaking in general terms, the simplest solution would be to create one function for every user and independently track the number of times they have been seen. Using most frameworks, this would be prohibitively expensive.
-However, stateful functions are virtual and do not consume any CPU or memory when not actively being invoked.
-That means your application can create as many functions as necessary — in this case, users — without worrying about resource consumption.
+Stateful Functions accept messages and can also send them out.
+Messages can be sent to other functions, as well as external systems (or [egress]({{ site.baseurl }}/io-module/index.html#egress)).
 
-Whenever data is consumed from an external system (or [ingress]({{ site.baseurl }}/io-module/index.html#ingress)), it is routed to a specific function based on a given function type and identifier.
-The function type represents the Class of function to be invoked, such as the Greeter function, while the identifier (``GreetRequest#getWho``) scopes the call to a specific virtual instance based on some key.
+One popular external system is [Apache Kafka](http://kafka.apache.org/).
+As a first step, lets update our function to respond to each input by sending a greeting to a Kafka topic.
 
-{% highlight java %}
-package org.apache.flink.statefun.examples.greeter;
+{% highlight python %}
+from messages_pb2 import GreetRequest, GreetResponse
+from statefun import StatefulFunctions
 
-import org.apache.flink.statefun.examples.kafka.generated.GreetRequest;
-import org.apache.flink.statefun.sdk.io.Router;
+functions = StatefulFunctions()
 
-final class GreetRouter implements Router<GreetRequest> {
+@functions.bind("example/greeter")
+def greet(context, message: GreetRequest):
+    response = GreetResponse()
+    response.name = message.name
+    response.greeting = "Hello {}".format(message.name)
+    
+    egress_message = kafka_egress_record(topic="greetings", key=message.name, value=response)
+    context.pack_and_send_egress("example/greets", egress_message)
+{% endhighlight %} 
 
-    @Override
-    public void route(GreetRequest message, Downstream<GreetRequest> downstream) {
-        downstream.forward(GreetingConstants.GREETER_FUNCTION_TYPE, message.getWho(), message);
-    }
-}
+For each message, a response is constructed and sent to a kafka topic call `greetings` partitioned by `name`.
+The `egress_message` is sent to a an `egress` named `example/greets`.
+This identifier points to a particular Kafka cluster and is configured on deployment below.
+
+## A Stateful Hello
+
+This is a great start, but does not show off the real power of stateful functions - working with state.
+Suppose you want to generate a personalized response for each user depending on how many times they have sent a request.
+
+{% highlight python %}
+def compute_greeting(name, seen):
+    """
+    Compute a personalized greeting, based on the number of times this @name had been seen before.
+    """
+    templates = ["", "Welcome %s", "Nice to see you again %s", "Third time is a charm %s"]
+    if seen < len(templates):
+        greeting = templates[seen] % name
+    else:
+        greeting = "Nice to see you at the %d-nth time %s!" % (seen, name)
+
+    response = GreetResponse()
+    response.name = name
+    response.greeting = greeting
+
+    return response
 {% endhighlight %}
 
-So, if a message for a user named John comes in, it will be shipped to John’s dedicated Greeter function.
-In case there is a following message for a user named Jane, a new instance of the Greeter function will be spawned.
+To “remember” information across multiple greeting messages, you then need to associate a persisted value field (``seen_count``) to the Greet function.
+For each user, functions can now track how many times they have been seen.
 
-## Persistence
+{% highlight python %}
+@functions.bind("example/greeter")
+def greet(context, message: GreetRequest):
+    state = context.state('seen_count').unpack(SeenCount)
+    if not state:
+        state = SeenCount()
+        state.seen = 1
+    else:
+        state.seen += 1
+    context.state('seen_count').pack(state)
 
-[Persisted value]({{ site.baseurl }}/sdk/java.html#persistence) is a special data type that enables stateful functions to maintain fault-tolerant state scoped to their identifiers, so that each instance of a function can track state independently.
-To “remember” information across multiple greeting messages, you then need to associate a persisted value field (``count``) to the Greet function. For each user, functions can now track how many times they have been seen.
+    response = compute_greeting(greet_request.name, state.seen)
 
-{% highlight java %}
-package org.apache.flink.statefun.examples.greeter;
+    egress_message = kafka_egress_record(topic="greetings", key=greet_request.name, value=response)
+    context.pack_and_send_egress("example/greets", egress_message)
+{% endhighlight %}
 
-import org.apache.flink.statefun.sdk.Context;
-import org.apache.flink.statefun.sdk.StatefulFunction;
-import org.apache.flink.statefun.sdk.annotations.Persisted;
-import org.apache.flink.statefun.sdk.state.PersistedValue;
+The state, `seen_count` is always scoped to the current name so it can track each user independently.
 
-public final class GreetFunction implements StatefulFunction {
+## Wiring It All Together
 
-    @Persisted
-    private final PersistedValue<Integer> count = PersistedValue.of("count", Integer.class);
+Stateful Function applications communicate with the Apache Flink runtime using `http`.
+The Python SDK ships with a ``RequestReplyHandler`` that automatically dispatches function calls based on RESTful HTTP ``POSTS``.
+The ``RequestReplyHandler`` may be exposed using any HTTP framework.
 
-    @Override
-    public void invoke(Context context, Object input) {
-        GreetRequest greetMessage = (GreetRequest) input;
+One popular Python web framework is [Flask](https://palletsprojects.com/p/flask/).
+It can be used to quickly and easily expose an application to the Apache Flink runtime.
 
-        GreetResponse response = computePersonalizedGreeting(greetMessage);
+{% highlight python %}
+from statefun import StatefulFunctions
+from statefun import RequestReplyHandler
 
-        context.send(GreetingConstants.GREETING_EGRESS_ID, response);
-    }
+functions = StatefulFunctions()
 
-    private GreetResponse computePersonalizedGreeting(GreetRequest greetMessage) {
-        final String name = greetMessage.getWho();
-        final int seen = count.getOrDefault(0);
-        count.set(seen + 1);
+@functions.bind("walkthrough/greeter")
+def greeter(context, message: GreetRequest):
+    pass
 
-        String greeting = greetText(name, seen);
+handler = RequestReplyHandler(functions)
 
-        return GreetResponse.newBuilder()
-            .setWho(name)
-            .setGreeting(greeting)
-            .build();
-    }
-}
+# Serve the endpoint
+
+from flask import request
+from flask import make_response
+from flask import Flask
+
+app = Flask(__name__)
+
+@app.route('/statefun', methods=['POST'])
+def handle():
+    response_data = handler(request.data)
+    response = make_response(response_data)
+    response.headers.set('Content-Type', 'application/octet-stream')
+    return response
+
+
+if __name__ == "__main__":
+    app.run()
 {% endhighlight %}
 
-Each time a message is processed, the function computes a personalized message for that user.
-It reads and updates the number of times that user has been seen and sends a greeting to the egress.
+## Configuring for Runtime
+
+The Apache Flink runtime makes requests to the greeter function by making `http` calls to the `Flask` server.
+To do that, Flink needs to know what endpoint it can use to reach the server.
+This is also a good time to configure our connection to the input and output Kafka topics.
+The configuration is in a file called `module.yaml`.
+
+{% highlight yaml %}
+version: "1.0"
+module:
+  meta:
+    type: remote
+  spec:
+    functions:
+      - function:
+          meta:
+            kind: http
+            type: example/greeter
+          spec:
+            endpoint: http://python-worker:8000/statefun
+            states:
+              - seen_count
+            maxNumBatchRequests: 500
+            timeout: 2min
+    ingresses:
+      - ingress:
+          meta:
+            type: statefun.kafka.io/routable-protobuf-ingress
+            id: example/names
+          spec:
+            address: kafka-broker:9092
+            consumerGroupId: my-group-id
+            topics:
+              - topic: names
+                typeUrl: com.googleapis/example.GreetRequest
+                targets:
+                  - example/greeter
+    egresses:
+      - egress:
+          meta:
+            type: statefun.kafka.io/generic-egress
+            id: example/greets
+          spec:
+            address: kafka-broker:9092
+            deliverySemantic:
+              type: exactly-once
+              transactionTimeoutMillis: 100000
+{% endhighlight %}
 
-You can check the full code for the application described in this walkthrough [here]({{ site.github_url }}/tree/{{ site.github_branch }}/statefun-examples/statefun-greeter-example).
-In particular, take a look at the module ``GreetingModule``, which is the main entry point for the full application, to see how everything gets tied together.
-You can run this example locally using the provided Docker setup.
+This configuration does a few interesting things.
 
-{% highlight bash %}
-$ docker-compose build 
-$ docker-compose up
+The first is to declare our function, `example/greeter`.
+It includes the endpoint by which it is reachable along with the states the function has access to.
+
+The ingress is the input Kafka topic that routes `GreetRequest` messages to the function.
+Along with basic properties like broker address and consumer group, it contains a list of targets.
+These are the functions each message will be sent to.
+
+The egress is the output Kafka cluster.
+It contains broker specific configurations but allows each message to route to any topic.
+
+## Deployment
+
+Now that the greeter application has been built it is time to deploy. 
+The simplest way to deploy a Stateful Function application is by using the community provided base image and loading your module.
+The base image provides the Stateful Function runtime, it will use the provided `module.yaml` to configure for this specific job.
+This can be found in the docker file in the root directory. 
+
+{% highlight docker %}
+FROM statefun
+
+RUN mkdir -p /opt/statefun/modules/greeter
+ADD module.yaml /opt/statefun/modules/greeter
 {% endhighlight %}
 
-Then, send some messages to the topic "names", and observe what comes out of "greetings".
+You can now run this application locally using the provided Docker setup.
 
 {% highlight bash %}
-$ docker-compose exec kafka-broker kafka-console-producer.sh \
-    --broker-list localhost:9092 \
-    --topic names
+$ docker-compose up -d
 
 Review comment:
   Not yet because we haven't released :) In the mean time you can build the image locally using the `./build-distrubition.sh` script in the tools folder. 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [flink-statefun] sjwiesman commented on a change in pull request #77: [FLINK-16730][docs] Python SDK Getting Started

Posted by GitBox <gi...@apache.org>.
sjwiesman commented on a change in pull request #77: [FLINK-16730][docs] Python SDK Getting Started
URL: https://github.com/apache/flink-statefun/pull/77#discussion_r399523084
 
 

 ##########
 File path: docs/getting-started/walkthrough.md
 ##########
 @@ -24,177 +24,411 @@ specific language governing permissions and limitations
 under the License.
 -->
 
+Stateful Functions offers a platform for building robust, stateful event-driven applications.
+It provides fine-grained control over state and time, which allows for the implementation of advanced systems.
+In this step-by-step guide you’ll learn how to build a stateful applications with the Stateful Functions API.
+
+* This will be replaced by the TOC
+{:toc}
+
+## What Are You Building?
+
 Like all great introductions in software, this walkthrough will start at the beginning: saying hello.
 The application will run a simple function that accepts a request and responds with a greeting.
 It will not attempt to cover all the complexities of application development, but instead focus on building a stateful function — which is where you will implement your business logic.
 
-* This will be replaced by the TOC
-{:toc}
+## Prerequisites
+
+This walkthrough assumes that you have some familiarity with Python, but you should be able to follow along even if you are coming from a different programming language.
+
+## Help, I’m Stuck! 
+
+If you get stuck, check out the [community support resources](https://flink.apache.org/gettinghelp.html).
+In particular, Apache Flink's [user mailing list](https://flink.apache.org/community.html#mailing-lists) is consistently ranked as one of the most active of any Apache project and a great way to get help quickly.
 
-## A Basic Hello
+## How to Follow Along
+
+If you want to follow along, you will require a computer with [Python 3](https://www.python.org/) along with [Docker](https://www.docker.com/).
+
+{% panel **Note:** Each code block within this walkthrough may not contain the full surrounding class for brevity.
+The full code is available on [at the bottom of this page](#full-application). %}
+
+You can download a zip file with a skeleton project by clicking [here]({{ site.baseurl }}/downloads/walkthrough.zip).
+
+{% unless site.is_stable %}
+<p style="border-radius: 5px; padding: 5px" class="bg-danger">
+    <b>Note</b>: The Stateful Functions project does not publish snapshot versions of the Python SDK to PyPy.
+    Please consider using a stable version of this guide.
+</p>
+{% endunless %}
+
+After unzipping the package, you will find a number of files.
+These include dockerfiles and data generators to run this walkthrough in a local self contained environment.
+
+{% highlight bash %}
+$ tree statefun-walkthrough
+statefun-walkthrough
+├── Dockerfile
+├── README.md
+├── docker-compose.yml
+├── generator
+│   ├── Dockerfile
+│   ├── event-generator.py
+│   └── messages_pb2.py
+├── greeter
+│   ├── Dockerfile
+│   ├── greeter.py
+│   ├── messages.proto
+│   ├── messages_pb2.py
+│   └── requirements.txt
+└── module.yaml
+{% endhighlight %}
 
-Greeting actions are triggered by consuming, routing and passing messages that are defined using ProtoBuf.
+## Start With Events
+
+Stateful Functions is an event driven system, so development begins by defining our events.
+The greeter application will define its events using [protocol buffers](https://developers.google.com/protocol-buffers). 
+When a greet request for a particular user is ingested, it will be routed to the appropriate function.
+The response will be returned with an appropriate greeting.
+The third type, `SeenCount`, is a utility class that will be used latter on to help manage the number of times a user has been seen so far.
 
 {% highlight proto %}
 syntax = "proto3";
 
+package example;
+
+// External request sent by a user who wants to be greeted
 message GreetRequest {
-    string who = 1;
+    // The name of the user to greet
+    string name = 1;
 }
-
+// A customized response sent to the user
 message GreetResponse {
-    string who = 1;
+    // The name of the user being greeted
+    string name = 1;
+    // The users customized greeting
     string greeting = 2;
 }
+// An internal message used to store state
+message SeenCount {
+    // The number of times a users has been seen so far
+    int64 seen = 1;
+}
 {% endhighlight %}
 
-Under the hood, messages are processed using [stateful functions]({{ site.baseurl }}/sdk/java.html), by definition any class that implements the ``StatefulFunction`` interface.
 
-{% highlight java %}
-package org.apache.flink.statefun.examples.greeter;
+## Our First Function
 
-import org.apache.flink.statefun.sdk.Context;
-import org.apache.flink.statefun.sdk.StatefulFunction;
+Under the hood, messages are processed using [stateful functions]({{ site.baseurl }}/sdk/python.html), which is any two argument function that is bound to the ``StatefulFunction`` runtime.
+Functions are bound to the runtime with the `@function.bind` decorator.
+When binding a function, it is annotated with a function type.
+This is the name used to reference this function when sending it messages.
 
-public final class GreetFunction implements StatefulFunction {
+When you open the file `greeter/greeter.py` you should see the following code.
 
-    @Override
-    public void invoke(Context context, Object input) {
-        GreetRequest greetMessage = (GreetRequest) input;
+{% highlight python %}
+from statefun import StatefulFunctions
 
-        GreetResponse response = GreetResponse.newBuilder()
-            .setWho(greetMessage.getWho())
-            .setGreeting("Hello " + greetMessage.getWho())
-            .build();
+functions = StatefulFunctions()
 
-        context.send(GreetingConstants.GREETING_EGRESS_ID, response);
-    }
-}
+@functions.bind("example/greeter")
+def greet(context, greet_request):
+    pass
 {% endhighlight %}
 
+A stateful function takes two arguments, a context and message. 
+The [context]({{ site.baseurl }}/sdk/python.html#context-reference) provides access to stateful functions runtime features such as state management and message passing.
+You will explore some of these features as you progress through this walkthrough. 
 
-This function takes in a request and sends a response to an external system (or [egress]({{ site.baseurl }}/io-module/index.html#egress)).
-While this is nice, it does not show off the real power of stateful functions: handling state.
+The other parameter is the input message that has been passed to this function.
+By default messages are passed around as protobuf [Any](https://developers.google.com/protocol-buffers/docs/reference/python-generated#wkt).
+If a function only accepts a known type, you can override the message type using Python 3 type syntax.
+This way you do not need to unwrap the message or check types.
 
-## A Stateful Hello
+{% highlight python %}
+from messages_pb2 import GreetRequest
+from statefun import StatefulFunctions
 
-Suppose you want to generate a personalized response for each user depending on how many times they have sent a request.
+functions = StatefulFunctions()
 
-{% highlight java %}
-private static String greetText(String name, int seen) {
-    switch (seen) {
-        case 0:
-            return String.format("Hello %s !", name);
-        case 1:
-            return String.format("Hello again %s !", name);
-        case 2:
-            return String.format("Third times the charm! %s!", name);
-        case 3:
-            return String.format("Happy to see you once again %s !", name);
-        default:
-            return String.format("Hello at the %d-th time %s", seen + 1, name);
-}
+@functions.bind("example/greeter")
+def greet(context, greet_request: GreetRequest):
+    pass
 {% endhighlight %}
 
-## Routing Messages
+## Sending A Response
 
-To send a user a personalized greeting, the system needs to keep track of how many times it has seen each user so far.
-Speaking in general terms, the simplest solution would be to create one function for every user and independently track the number of times they have been seen. Using most frameworks, this would be prohibitively expensive.
-However, stateful functions are virtual and do not consume any CPU or memory when not actively being invoked.
-That means your application can create as many functions as necessary — in this case, users — without worrying about resource consumption.
+Stateful Functions accept messages and can also send them out.
+Messages can be sent to other functions, as well as external systems (or [egress]({{ site.baseurl }}/io-module/index.html#egress)).
 
-Whenever data is consumed from an external system (or [ingress]({{ site.baseurl }}/io-module/index.html#ingress)), it is routed to a specific function based on a given function type and identifier.
-The function type represents the Class of function to be invoked, such as the Greeter function, while the identifier (``GreetRequest#getWho``) scopes the call to a specific virtual instance based on some key.
+One popular external system is [Apache Kafka](http://kafka.apache.org/).
+As a first step, lets update our function in `greeter/greeter.py` to respond to each input by sending a greeting to a Kafka topic.
 
-{% highlight java %}
-package org.apache.flink.statefun.examples.greeter;
+{% highlight python %}
+from messages_pb2 import GreetRequest, GreetResponse
+from statefun import StatefulFunctions
 
-import org.apache.flink.statefun.examples.kafka.generated.GreetRequest;
-import org.apache.flink.statefun.sdk.io.Router;
+functions = StatefulFunctions()
 
-final class GreetRouter implements Router<GreetRequest> {
+@functions.bind("example/greeter")
+def greet(context, message: GreetRequest):
+    response = GreetResponse()
+    response.name = message.name
+    response.greeting = "Hello {}".format(message.name)
+    
+    egress_message = kafka_egress_record(topic="greetings", key=message.name, value=response)
+    context.pack_and_send_egress("example/greets", egress_message)
+{% endhighlight %} 
 
-    @Override
-    public void route(GreetRequest message, Downstream<GreetRequest> downstream) {
-        downstream.forward(GreetingConstants.GREETER_FUNCTION_TYPE, message.getWho(), message);
-    }
-}
+For each message, a response is constructed and sent to a kafka topic call `greetings` partitioned by `name`.
+The `egress_message` is sent to a an `egress` named `example/greets`.
+This identifier points to a particular Kafka cluster and is configured on deployment below.
+
+## A Stateful Hello
+
+This is a great start, but does not show off the real power of stateful functions - working with state.
+Suppose you want to generate a personalized response for each user depending on how many times they have sent a request.
+
+{% highlight python %}
+def compute_greeting(name, seen):
+    """
+    Compute a personalized greeting, based on the number of times this @name had been seen before.
+    """
+    templates = ["", "Welcome %s", "Nice to see you again %s", "Third time is a charm %s"]
+    if seen < len(templates):
+        greeting = templates[seen] % name
+    else:
+        greeting = "Nice to see you at the %d-nth time %s!" % (seen, name)
+
+    response = GreetResponse()
+    response.name = name
+    response.greeting = greeting
+
+    return response
 {% endhighlight %}
 
-So, if a message for a user named John comes in, it will be shipped to John’s dedicated Greeter function.
-In case there is a following message for a user named Jane, a new instance of the Greeter function will be spawned.
+To “remember” information across multiple greeting messages, you then need to associate a persisted value field (``seen_count``) to the Greet function.
+For each user, functions can now track how many times they have been seen.
 
-## Persistence
+{% highlight python %}
+@functions.bind("example/greeter")
+def greet(context, message: GreetRequest):
 
 Review comment:
   nice catch

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [flink-statefun] rmetzger commented on a change in pull request #77: [FLINK-16730][docs] Python SDK Getting Started

Posted by GitBox <gi...@apache.org>.
rmetzger commented on a change in pull request #77: [FLINK-16730][docs] Python SDK Getting Started
URL: https://github.com/apache/flink-statefun/pull/77#discussion_r399477462
 
 

 ##########
 File path: docs/getting-started/walkthrough.md
 ##########
 @@ -24,177 +24,411 @@ specific language governing permissions and limitations
 under the License.
 -->
 
+Stateful Functions offers a platform for building robust, stateful event-driven applications.
+It provides fine-grained control over state and time, which allows for the implementation of advanced systems.
+In this step-by-step guide you’ll learn how to build a stateful applications with the Stateful Functions API.
+
+* This will be replaced by the TOC
+{:toc}
+
+## What Are You Building?
+
 Like all great introductions in software, this walkthrough will start at the beginning: saying hello.
 The application will run a simple function that accepts a request and responds with a greeting.
 It will not attempt to cover all the complexities of application development, but instead focus on building a stateful function — which is where you will implement your business logic.
 
-* This will be replaced by the TOC
-{:toc}
+## Prerequisites
+
+This walkthrough assumes that you have some familiarity with Python, but you should be able to follow along even if you are coming from a different programming language.
+
+## Help, I’m Stuck! 
+
+If you get stuck, check out the [community support resources](https://flink.apache.org/gettinghelp.html).
+In particular, Apache Flink's [user mailing list](https://flink.apache.org/community.html#mailing-lists) is consistently ranked as one of the most active of any Apache project and a great way to get help quickly.
 
-## A Basic Hello
+## How to Follow Along
+
+If you want to follow along, you will require a computer with [Python 3](https://www.python.org/) along with [Docker](https://www.docker.com/).
+
+{% panel **Note:** Each code block within this walkthrough may not contain the full surrounding class for brevity.
+The full code is available on [at the bottom of this page](#full-application). %}
+
+You can download a zip file with a skeleton project by clicking [here]({{ site.baseurl }}/downloads/walkthrough.zip).
+
+{% unless site.is_stable %}
+<p style="border-radius: 5px; padding: 5px" class="bg-danger">
+    <b>Note</b>: The Stateful Functions project does not publish snapshot versions of the Python SDK to PyPy.
+    Please consider using a stable version of this guide.
+</p>
+{% endunless %}
+
+After unzipping the package, you will find a number of files.
+These include dockerfiles and data generators to run this walkthrough in a local self contained environment.
+
+{% highlight bash %}
+$ tree statefun-walkthrough
+statefun-walkthrough
+├── Dockerfile
+├── README.md
+├── docker-compose.yml
+├── generator
+│   ├── Dockerfile
+│   ├── event-generator.py
+│   └── messages_pb2.py
+├── greeter
+│   ├── Dockerfile
+│   ├── greeter.py
+│   ├── messages.proto
+│   ├── messages_pb2.py
+│   └── requirements.txt
+└── module.yaml
+{% endhighlight %}
 
-Greeting actions are triggered by consuming, routing and passing messages that are defined using ProtoBuf.
+## Start With Events
+
+Stateful Functions is an event driven system, so development begins by defining our events.
+The greeter application will define its events using [protocol buffers](https://developers.google.com/protocol-buffers). 
+When a greet request for a particular user is ingested, it will be routed to the appropriate function.
+The response will be returned with an appropriate greeting.
+The third type, `SeenCount`, is a utility class that will be used latter on to help manage the number of times a user has been seen so far.
 
 {% highlight proto %}
 syntax = "proto3";
 
+package example;
+
+// External request sent by a user who wants to be greeted
 message GreetRequest {
-    string who = 1;
+    // The name of the user to greet
+    string name = 1;
 }
-
+// A customized response sent to the user
 message GreetResponse {
-    string who = 1;
+    // The name of the user being greeted
+    string name = 1;
+    // The users customized greeting
     string greeting = 2;
 }
+// An internal message used to store state
+message SeenCount {
+    // The number of times a users has been seen so far
+    int64 seen = 1;
+}
 {% endhighlight %}
 
-Under the hood, messages are processed using [stateful functions]({{ site.baseurl }}/sdk/java.html), by definition any class that implements the ``StatefulFunction`` interface.
 
-{% highlight java %}
-package org.apache.flink.statefun.examples.greeter;
+## Our First Function
 
-import org.apache.flink.statefun.sdk.Context;
-import org.apache.flink.statefun.sdk.StatefulFunction;
+Under the hood, messages are processed using [stateful functions]({{ site.baseurl }}/sdk/python.html), which is any two argument function that is bound to the ``StatefulFunction`` runtime.
+Functions are bound to the runtime with the `@function.bind` decorator.
+When binding a function, it is annotated with a function type.
+This is the name used to reference this function when sending it messages.
 
-public final class GreetFunction implements StatefulFunction {
+When you open the file `greeter/greeter.py` you should see the following code.
 
-    @Override
-    public void invoke(Context context, Object input) {
-        GreetRequest greetMessage = (GreetRequest) input;
+{% highlight python %}
+from statefun import StatefulFunctions
 
-        GreetResponse response = GreetResponse.newBuilder()
-            .setWho(greetMessage.getWho())
-            .setGreeting("Hello " + greetMessage.getWho())
-            .build();
+functions = StatefulFunctions()
 
-        context.send(GreetingConstants.GREETING_EGRESS_ID, response);
-    }
-}
+@functions.bind("example/greeter")
+def greet(context, greet_request):
+    pass
 {% endhighlight %}
 
+A stateful function takes two arguments, a context and message. 
+The [context]({{ site.baseurl }}/sdk/python.html#context-reference) provides access to stateful functions runtime features such as state management and message passing.
+You will explore some of these features as you progress through this walkthrough. 
 
-This function takes in a request and sends a response to an external system (or [egress]({{ site.baseurl }}/io-module/index.html#egress)).
-While this is nice, it does not show off the real power of stateful functions: handling state.
+The other parameter is the input message that has been passed to this function.
+By default messages are passed around as protobuf [Any](https://developers.google.com/protocol-buffers/docs/reference/python-generated#wkt).
+If a function only accepts a known type, you can override the message type using Python 3 type syntax.
+This way you do not need to unwrap the message or check types.
 
-## A Stateful Hello
+{% highlight python %}
+from messages_pb2 import GreetRequest
+from statefun import StatefulFunctions
 
-Suppose you want to generate a personalized response for each user depending on how many times they have sent a request.
+functions = StatefulFunctions()
 
-{% highlight java %}
-private static String greetText(String name, int seen) {
-    switch (seen) {
-        case 0:
-            return String.format("Hello %s !", name);
-        case 1:
-            return String.format("Hello again %s !", name);
-        case 2:
-            return String.format("Third times the charm! %s!", name);
-        case 3:
-            return String.format("Happy to see you once again %s !", name);
-        default:
-            return String.format("Hello at the %d-th time %s", seen + 1, name);
-}
+@functions.bind("example/greeter")
+def greet(context, greet_request: GreetRequest):
+    pass
 {% endhighlight %}
 
-## Routing Messages
+## Sending A Response
 
-To send a user a personalized greeting, the system needs to keep track of how many times it has seen each user so far.
-Speaking in general terms, the simplest solution would be to create one function for every user and independently track the number of times they have been seen. Using most frameworks, this would be prohibitively expensive.
-However, stateful functions are virtual and do not consume any CPU or memory when not actively being invoked.
-That means your application can create as many functions as necessary — in this case, users — without worrying about resource consumption.
+Stateful Functions accept messages and can also send them out.
+Messages can be sent to other functions, as well as external systems (or [egress]({{ site.baseurl }}/io-module/index.html#egress)).
 
-Whenever data is consumed from an external system (or [ingress]({{ site.baseurl }}/io-module/index.html#ingress)), it is routed to a specific function based on a given function type and identifier.
-The function type represents the Class of function to be invoked, such as the Greeter function, while the identifier (``GreetRequest#getWho``) scopes the call to a specific virtual instance based on some key.
+One popular external system is [Apache Kafka](http://kafka.apache.org/).
+As a first step, lets update our function to respond to each input by sending a greeting to a Kafka topic.
 
-{% highlight java %}
-package org.apache.flink.statefun.examples.greeter;
+{% highlight python %}
+from messages_pb2 import GreetRequest, GreetResponse
+from statefun import StatefulFunctions
 
-import org.apache.flink.statefun.examples.kafka.generated.GreetRequest;
-import org.apache.flink.statefun.sdk.io.Router;
+functions = StatefulFunctions()
 
-final class GreetRouter implements Router<GreetRequest> {
+@functions.bind("example/greeter")
+def greet(context, message: GreetRequest):
+    response = GreetResponse()
+    response.name = message.name
+    response.greeting = "Hello {}".format(message.name)
+    
+    egress_message = kafka_egress_record(topic="greetings", key=message.name, value=response)
+    context.pack_and_send_egress("example/greets", egress_message)
+{% endhighlight %} 
 
-    @Override
-    public void route(GreetRequest message, Downstream<GreetRequest> downstream) {
-        downstream.forward(GreetingConstants.GREETER_FUNCTION_TYPE, message.getWho(), message);
-    }
-}
+For each message, a response is constructed and sent to a kafka topic call `greetings` partitioned by `name`.
+The `egress_message` is sent to a an `egress` named `example/greets`.
+This identifier points to a particular Kafka cluster and is configured on deployment below.
+
+## A Stateful Hello
+
+This is a great start, but does not show off the real power of stateful functions - working with state.
+Suppose you want to generate a personalized response for each user depending on how many times they have sent a request.
+
+{% highlight python %}
+def compute_greeting(name, seen):
+    """
+    Compute a personalized greeting, based on the number of times this @name had been seen before.
+    """
+    templates = ["", "Welcome %s", "Nice to see you again %s", "Third time is a charm %s"]
+    if seen < len(templates):
+        greeting = templates[seen] % name
+    else:
+        greeting = "Nice to see you at the %d-nth time %s!" % (seen, name)
+
+    response = GreetResponse()
+    response.name = name
+    response.greeting = greeting
+
+    return response
 {% endhighlight %}
 
-So, if a message for a user named John comes in, it will be shipped to John’s dedicated Greeter function.
-In case there is a following message for a user named Jane, a new instance of the Greeter function will be spawned.
+To “remember” information across multiple greeting messages, you then need to associate a persisted value field (``seen_count``) to the Greet function.
+For each user, functions can now track how many times they have been seen.
 
-## Persistence
+{% highlight python %}
+@functions.bind("example/greeter")
+def greet(context, message: GreetRequest):
+    state = context.state('seen_count').unpack(SeenCount)
+    if not state:
+        state = SeenCount()
+        state.seen = 1
+    else:
+        state.seen += 1
+    context.state('seen_count').pack(state)
 
-[Persisted value]({{ site.baseurl }}/sdk/java.html#persistence) is a special data type that enables stateful functions to maintain fault-tolerant state scoped to their identifiers, so that each instance of a function can track state independently.
-To “remember” information across multiple greeting messages, you then need to associate a persisted value field (``count``) to the Greet function. For each user, functions can now track how many times they have been seen.
+    response = compute_greeting(greet_request.name, state.seen)
 
-{% highlight java %}
-package org.apache.flink.statefun.examples.greeter;
+    egress_message = kafka_egress_record(topic="greetings", key=greet_request.name, value=response)
+    context.pack_and_send_egress("example/greets", egress_message)
+{% endhighlight %}
 
-import org.apache.flink.statefun.sdk.Context;
-import org.apache.flink.statefun.sdk.StatefulFunction;
-import org.apache.flink.statefun.sdk.annotations.Persisted;
-import org.apache.flink.statefun.sdk.state.PersistedValue;
+The state, `seen_count` is always scoped to the current name so it can track each user independently.
 
-public final class GreetFunction implements StatefulFunction {
+## Wiring It All Together
 
-    @Persisted
-    private final PersistedValue<Integer> count = PersistedValue.of("count", Integer.class);
+Stateful Function applications communicate with the Apache Flink runtime using `http`.
+The Python SDK ships with a ``RequestReplyHandler`` that automatically dispatches function calls based on RESTful HTTP ``POSTS``.
+The ``RequestReplyHandler`` may be exposed using any HTTP framework.
 
-    @Override
-    public void invoke(Context context, Object input) {
-        GreetRequest greetMessage = (GreetRequest) input;
+One popular Python web framework is [Flask](https://palletsprojects.com/p/flask/).
+It can be used to quickly and easily expose an application to the Apache Flink runtime.
 
-        GreetResponse response = computePersonalizedGreeting(greetMessage);
+{% highlight python %}
+from statefun import StatefulFunctions
+from statefun import RequestReplyHandler
 
-        context.send(GreetingConstants.GREETING_EGRESS_ID, response);
-    }
+functions = StatefulFunctions()
 
-    private GreetResponse computePersonalizedGreeting(GreetRequest greetMessage) {
-        final String name = greetMessage.getWho();
-        final int seen = count.getOrDefault(0);
-        count.set(seen + 1);
+@functions.bind("walkthrough/greeter")
+def greeter(context, message: GreetRequest):
+    pass
 
-        String greeting = greetText(name, seen);
+handler = RequestReplyHandler(functions)
 
-        return GreetResponse.newBuilder()
-            .setWho(name)
-            .setGreeting(greeting)
-            .build();
-    }
-}
+# Serve the endpoint
+
+from flask import request
+from flask import make_response
+from flask import Flask
+
+app = Flask(__name__)
+
+@app.route('/statefun', methods=['POST'])
+def handle():
+    response_data = handler(request.data)
+    response = make_response(response_data)
+    response.headers.set('Content-Type', 'application/octet-stream')
+    return response
+
+
+if __name__ == "__main__":
+    app.run()
 {% endhighlight %}
 
-Each time a message is processed, the function computes a personalized message for that user.
-It reads and updates the number of times that user has been seen and sends a greeting to the egress.
+## Configuring for Runtime
+
+The Apache Flink runtime makes requests to the greeter function by making `http` calls to the `Flask` server.
+To do that, Flink needs to know what endpoint it can use to reach the server.
+This is also a good time to configure our connection to the input and output Kafka topics.
+The configuration is in a file called `module.yaml`.
+
+{% highlight yaml %}
+version: "1.0"
+module:
+  meta:
+    type: remote
+  spec:
+    functions:
+      - function:
+          meta:
+            kind: http
+            type: example/greeter
+          spec:
+            endpoint: http://python-worker:8000/statefun
+            states:
+              - seen_count
+            maxNumBatchRequests: 500
+            timeout: 2min
+    ingresses:
+      - ingress:
+          meta:
+            type: statefun.kafka.io/routable-protobuf-ingress
+            id: example/names
+          spec:
+            address: kafka-broker:9092
+            consumerGroupId: my-group-id
+            topics:
+              - topic: names
+                typeUrl: com.googleapis/example.GreetRequest
+                targets:
+                  - example/greeter
+    egresses:
+      - egress:
+          meta:
+            type: statefun.kafka.io/generic-egress
+            id: example/greets
+          spec:
+            address: kafka-broker:9092
+            deliverySemantic:
+              type: exactly-once
+              transactionTimeoutMillis: 100000
+{% endhighlight %}
 
-You can check the full code for the application described in this walkthrough [here]({{ site.github_url }}/tree/{{ site.github_branch }}/statefun-examples/statefun-greeter-example).
-In particular, take a look at the module ``GreetingModule``, which is the main entry point for the full application, to see how everything gets tied together.
-You can run this example locally using the provided Docker setup.
+This configuration does a few interesting things.
 
-{% highlight bash %}
-$ docker-compose build 
-$ docker-compose up
+The first is to declare our function, `example/greeter`.
+It includes the endpoint by which it is reachable along with the states the function has access to.
+
+The ingress is the input Kafka topic that routes `GreetRequest` messages to the function.
+Along with basic properties like broker address and consumer group, it contains a list of targets.
+These are the functions each message will be sent to.
+
+The egress is the output Kafka cluster.
+It contains broker specific configurations but allows each message to route to any topic.
+
+## Deployment
+
+Now that the greeter application has been built it is time to deploy. 
+The simplest way to deploy a Stateful Function application is by using the community provided base image and loading your module.
+The base image provides the Stateful Function runtime, it will use the provided `module.yaml` to configure for this specific job.
+This can be found in the docker file in the root directory. 
+
+{% highlight docker %}
+FROM statefun
+
+RUN mkdir -p /opt/statefun/modules/greeter
+ADD module.yaml /opt/statefun/modules/greeter
 {% endhighlight %}
 
-Then, send some messages to the topic "names", and observe what comes out of "greetings".
+You can now run this application locally using the provided Docker setup.
 
 {% highlight bash %}
-$ docker-compose exec kafka-broker kafka-console-producer.sh \
-    --broker-list localhost:9092 \
-    --topic names
+$ docker-compose up -d
 
 Review comment:
   Thx. That worked
   
   ```
   Step 5/9 : RUN pip install -r requirements.txt
    ---> Running in 7a9ee7fda4f7
   ERROR: Could not find a version that satisfies the requirement apache-flink-statefun (from -r requirements.txt (line 17)) (from versions: none)
   ERROR: No matching distribution found for apache-flink-statefun (from -r requirements.txt (line 17))
   ERROR: Service 'python-worker' failed to build: The command '/bin/sh -c pip install -r requirements.txt' returned a non-zero code: 1
   ```
   
   How do I get `apache-flink-statefun` into that Docker build?
   
   I managed to create a `RELEASE_VERSION=2.0 ./create_python_sdk_release.sh` release

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [flink-statefun] rmetzger commented on a change in pull request #77: [FLINK-16730][docs] Python SDK Getting Started

Posted by GitBox <gi...@apache.org>.
rmetzger commented on a change in pull request #77: [FLINK-16730][docs] Python SDK Getting Started
URL: https://github.com/apache/flink-statefun/pull/77#discussion_r399496275
 
 

 ##########
 File path: docs/getting-started/walkthrough.md
 ##########
 @@ -24,177 +24,411 @@ specific language governing permissions and limitations
 under the License.
 -->
 
+Stateful Functions offers a platform for building robust, stateful event-driven applications.
+It provides fine-grained control over state and time, which allows for the implementation of advanced systems.
+In this step-by-step guide you’ll learn how to build a stateful applications with the Stateful Functions API.
+
+* This will be replaced by the TOC
+{:toc}
+
+## What Are You Building?
+
 Like all great introductions in software, this walkthrough will start at the beginning: saying hello.
 The application will run a simple function that accepts a request and responds with a greeting.
 It will not attempt to cover all the complexities of application development, but instead focus on building a stateful function — which is where you will implement your business logic.
 
-* This will be replaced by the TOC
-{:toc}
+## Prerequisites
+
+This walkthrough assumes that you have some familiarity with Python, but you should be able to follow along even if you are coming from a different programming language.
+
+## Help, I’m Stuck! 
+
+If you get stuck, check out the [community support resources](https://flink.apache.org/gettinghelp.html).
+In particular, Apache Flink's [user mailing list](https://flink.apache.org/community.html#mailing-lists) is consistently ranked as one of the most active of any Apache project and a great way to get help quickly.
 
-## A Basic Hello
+## How to Follow Along
+
+If you want to follow along, you will require a computer with [Python 3](https://www.python.org/) along with [Docker](https://www.docker.com/).
+
+{% panel **Note:** Each code block within this walkthrough may not contain the full surrounding class for brevity.
+The full code is available on [at the bottom of this page](#full-application). %}
+
+You can download a zip file with a skeleton project by clicking [here]({{ site.baseurl }}/downloads/walkthrough.zip).
+
+{% unless site.is_stable %}
+<p style="border-radius: 5px; padding: 5px" class="bg-danger">
+    <b>Note</b>: The Stateful Functions project does not publish snapshot versions of the Python SDK to PyPy.
+    Please consider using a stable version of this guide.
+</p>
+{% endunless %}
+
+After unzipping the package, you will find a number of files.
+These include dockerfiles and data generators to run this walkthrough in a local self contained environment.
+
+{% highlight bash %}
+$ tree statefun-walkthrough
+statefun-walkthrough
+├── Dockerfile
+├── README.md
+├── docker-compose.yml
+├── generator
+│   ├── Dockerfile
+│   ├── event-generator.py
+│   └── messages_pb2.py
+├── greeter
+│   ├── Dockerfile
+│   ├── greeter.py
+│   ├── messages.proto
+│   ├── messages_pb2.py
+│   └── requirements.txt
+└── module.yaml
+{% endhighlight %}
 
-Greeting actions are triggered by consuming, routing and passing messages that are defined using ProtoBuf.
+## Start With Events
+
+Stateful Functions is an event driven system, so development begins by defining our events.
+The greeter application will define its events using [protocol buffers](https://developers.google.com/protocol-buffers). 
+When a greet request for a particular user is ingested, it will be routed to the appropriate function.
+The response will be returned with an appropriate greeting.
+The third type, `SeenCount`, is a utility class that will be used latter on to help manage the number of times a user has been seen so far.
 
 {% highlight proto %}
 syntax = "proto3";
 
+package example;
+
+// External request sent by a user who wants to be greeted
 message GreetRequest {
-    string who = 1;
+    // The name of the user to greet
+    string name = 1;
 }
-
+// A customized response sent to the user
 message GreetResponse {
-    string who = 1;
+    // The name of the user being greeted
+    string name = 1;
+    // The users customized greeting
     string greeting = 2;
 }
+// An internal message used to store state
+message SeenCount {
+    // The number of times a users has been seen so far
+    int64 seen = 1;
+}
 {% endhighlight %}
 
-Under the hood, messages are processed using [stateful functions]({{ site.baseurl }}/sdk/java.html), by definition any class that implements the ``StatefulFunction`` interface.
 
-{% highlight java %}
-package org.apache.flink.statefun.examples.greeter;
+## Our First Function
 
-import org.apache.flink.statefun.sdk.Context;
-import org.apache.flink.statefun.sdk.StatefulFunction;
+Under the hood, messages are processed using [stateful functions]({{ site.baseurl }}/sdk/python.html), which is any two argument function that is bound to the ``StatefulFunction`` runtime.
+Functions are bound to the runtime with the `@function.bind` decorator.
+When binding a function, it is annotated with a function type.
+This is the name used to reference this function when sending it messages.
 
-public final class GreetFunction implements StatefulFunction {
+When you open the file `greeter/greeter.py` you should see the following code.
 
-    @Override
-    public void invoke(Context context, Object input) {
-        GreetRequest greetMessage = (GreetRequest) input;
+{% highlight python %}
+from statefun import StatefulFunctions
 
-        GreetResponse response = GreetResponse.newBuilder()
-            .setWho(greetMessage.getWho())
-            .setGreeting("Hello " + greetMessage.getWho())
-            .build();
+functions = StatefulFunctions()
 
-        context.send(GreetingConstants.GREETING_EGRESS_ID, response);
-    }
-}
+@functions.bind("example/greeter")
+def greet(context, greet_request):
+    pass
 {% endhighlight %}
 
+A stateful function takes two arguments, a context and message. 
+The [context]({{ site.baseurl }}/sdk/python.html#context-reference) provides access to stateful functions runtime features such as state management and message passing.
+You will explore some of these features as you progress through this walkthrough. 
 
-This function takes in a request and sends a response to an external system (or [egress]({{ site.baseurl }}/io-module/index.html#egress)).
-While this is nice, it does not show off the real power of stateful functions: handling state.
+The other parameter is the input message that has been passed to this function.
+By default messages are passed around as protobuf [Any](https://developers.google.com/protocol-buffers/docs/reference/python-generated#wkt).
+If a function only accepts a known type, you can override the message type using Python 3 type syntax.
+This way you do not need to unwrap the message or check types.
 
-## A Stateful Hello
+{% highlight python %}
+from messages_pb2 import GreetRequest
+from statefun import StatefulFunctions
 
-Suppose you want to generate a personalized response for each user depending on how many times they have sent a request.
+functions = StatefulFunctions()
 
-{% highlight java %}
-private static String greetText(String name, int seen) {
-    switch (seen) {
-        case 0:
-            return String.format("Hello %s !", name);
-        case 1:
-            return String.format("Hello again %s !", name);
-        case 2:
-            return String.format("Third times the charm! %s!", name);
-        case 3:
-            return String.format("Happy to see you once again %s !", name);
-        default:
-            return String.format("Hello at the %d-th time %s", seen + 1, name);
-}
+@functions.bind("example/greeter")
+def greet(context, greet_request: GreetRequest):
+    pass
 {% endhighlight %}
 
-## Routing Messages
+## Sending A Response
 
-To send a user a personalized greeting, the system needs to keep track of how many times it has seen each user so far.
-Speaking in general terms, the simplest solution would be to create one function for every user and independently track the number of times they have been seen. Using most frameworks, this would be prohibitively expensive.
-However, stateful functions are virtual and do not consume any CPU or memory when not actively being invoked.
-That means your application can create as many functions as necessary — in this case, users — without worrying about resource consumption.
+Stateful Functions accept messages and can also send them out.
+Messages can be sent to other functions, as well as external systems (or [egress]({{ site.baseurl }}/io-module/index.html#egress)).
 
-Whenever data is consumed from an external system (or [ingress]({{ site.baseurl }}/io-module/index.html#ingress)), it is routed to a specific function based on a given function type and identifier.
-The function type represents the Class of function to be invoked, such as the Greeter function, while the identifier (``GreetRequest#getWho``) scopes the call to a specific virtual instance based on some key.
+One popular external system is [Apache Kafka](http://kafka.apache.org/).
+As a first step, lets update our function in `greeter/greeter.py` to respond to each input by sending a greeting to a Kafka topic.
 
-{% highlight java %}
-package org.apache.flink.statefun.examples.greeter;
+{% highlight python %}
+from messages_pb2 import GreetRequest, GreetResponse
+from statefun import StatefulFunctions
 
-import org.apache.flink.statefun.examples.kafka.generated.GreetRequest;
-import org.apache.flink.statefun.sdk.io.Router;
+functions = StatefulFunctions()
 
-final class GreetRouter implements Router<GreetRequest> {
+@functions.bind("example/greeter")
+def greet(context, message: GreetRequest):
+    response = GreetResponse()
+    response.name = message.name
+    response.greeting = "Hello {}".format(message.name)
+    
+    egress_message = kafka_egress_record(topic="greetings", key=message.name, value=response)
+    context.pack_and_send_egress("example/greets", egress_message)
+{% endhighlight %} 
 
-    @Override
-    public void route(GreetRequest message, Downstream<GreetRequest> downstream) {
-        downstream.forward(GreetingConstants.GREETER_FUNCTION_TYPE, message.getWho(), message);
-    }
-}
+For each message, a response is constructed and sent to a kafka topic call `greetings` partitioned by `name`.
+The `egress_message` is sent to a an `egress` named `example/greets`.
+This identifier points to a particular Kafka cluster and is configured on deployment below.
+
+## A Stateful Hello
+
+This is a great start, but does not show off the real power of stateful functions - working with state.
+Suppose you want to generate a personalized response for each user depending on how many times they have sent a request.
+
+{% highlight python %}
+def compute_greeting(name, seen):
+    """
+    Compute a personalized greeting, based on the number of times this @name had been seen before.
+    """
+    templates = ["", "Welcome %s", "Nice to see you again %s", "Third time is a charm %s"]
+    if seen < len(templates):
+        greeting = templates[seen] % name
+    else:
+        greeting = "Nice to see you at the %d-nth time %s!" % (seen, name)
+
+    response = GreetResponse()
+    response.name = name
+    response.greeting = greeting
+
+    return response
 {% endhighlight %}
 
-So, if a message for a user named John comes in, it will be shipped to John’s dedicated Greeter function.
-In case there is a following message for a user named Jane, a new instance of the Greeter function will be spawned.
+To “remember” information across multiple greeting messages, you then need to associate a persisted value field (``seen_count``) to the Greet function.
+For each user, functions can now track how many times they have been seen.
 
-## Persistence
+{% highlight python %}
+@functions.bind("example/greeter")
+def greet(context, message: GreetRequest):
 
 Review comment:
   ```suggestion
   def greet(context, greet_request: GreetRequest):
   ```
   
   Because I get 
   ```
   Traceback (most recent call last):
     File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 2446, in wsgi_app
       response = self.full_dispatch_request()
     File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1951, in full_dispatch_request
       rv = self.handle_user_exception(e)
     File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1820, in handle_user_exception
       reraise(exc_type, exc_value, tb)
     File "/usr/local/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise
       raise value
     File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1949, in full_dispatch_request
       rv = self.dispatch_request()
     File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1935, in dispatch_request
       return self.view_functions[rule.endpoint](**req.view_args)
     File "/app/greeter.py", line 72, in handle
       response_data = handler(request.data)
     File "/usr/local/lib/python3.7/site-packages/statefun/request_reply.py", line 39, in __call__
       reply = self.handle_invocation(request)
     File "/usr/local/lib/python3.7/site-packages/statefun/request_reply.py", line 54, in handle_invocation
       self.invoke_batch(batch, context, target_function)
     File "/usr/local/lib/python3.7/site-packages/statefun/request_reply.py", line 99, in invoke_batch
       fun(context, invocation.argument)
     File "/app/greeter.py", line 52, in greet
       response = compute_greeting(greet_request.name, state.seen)
   NameError: name 'greet_request' is not defined
   ```

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [flink-statefun] rmetzger commented on a change in pull request #77: [FLINK-16730][docs] Python SDK Getting Started

Posted by GitBox <gi...@apache.org>.
rmetzger commented on a change in pull request #77: [FLINK-16730][docs] Python SDK Getting Started
URL: https://github.com/apache/flink-statefun/pull/77#discussion_r399385486
 
 

 ##########
 File path: docs/getting-started/walkthrough.md
 ##########
 @@ -24,177 +24,411 @@ specific language governing permissions and limitations
 under the License.
 -->
 
+Stateful Functions offers a platform for building robust, stateful event-driven applications.
+It provides fine-grained control over state and time, which allows for the implementation of advanced systems.
+In this step-by-step guide you’ll learn how to build a stateful applications with the Stateful Functions API.
+
+* This will be replaced by the TOC
+{:toc}
+
+## What Are You Building?
+
 Like all great introductions in software, this walkthrough will start at the beginning: saying hello.
 The application will run a simple function that accepts a request and responds with a greeting.
 It will not attempt to cover all the complexities of application development, but instead focus on building a stateful function — which is where you will implement your business logic.
 
-* This will be replaced by the TOC
-{:toc}
+## Prerequisites
+
+This walkthrough assumes that you have some familiarity with Python, but you should be able to follow along even if you are coming from a different programming language.
+
+## Help, I’m Stuck! 
+
+If you get stuck, check out the [community support resources](https://flink.apache.org/gettinghelp.html).
+In particular, Apache Flink's [user mailing list](https://flink.apache.org/community.html#mailing-lists) is consistently ranked as one of the most active of any Apache project and a great way to get help quickly.
 
-## A Basic Hello
+## How to Follow Along
+
+If you want to follow along, you will require a computer with [Python 3](https://www.python.org/) along with [Docker](https://www.docker.com/).
+
+{% panel **Note:** Each code block within this walkthrough may not contain the full surrounding class for brevity.
+The full code is available on [at the bottom of this page](#full-application). %}
+
+You can download a zip file with a skeleton project by clicking [here]({{ site.baseurl }}/downloads/walkthrough.zip).
+
+{% unless site.is_stable %}
+<p style="border-radius: 5px; padding: 5px" class="bg-danger">
+    <b>Note</b>: The Stateful Functions project does not publish snapshot versions of the Python SDK to PyPy.
+    Please consider using a stable version of this guide.
+</p>
+{% endunless %}
+
+After unzipping the package, you will find a number of files.
+These include dockerfiles and data generators to run this walkthrough in a local self contained environment.
+
+{% highlight bash %}
+$ tree statefun-walkthrough
+statefun-walkthrough
+├── Dockerfile
+├── README.md
+├── docker-compose.yml
+├── generator
+│   ├── Dockerfile
+│   ├── event-generator.py
+│   └── messages_pb2.py
+├── greeter
+│   ├── Dockerfile
+│   ├── greeter.py
+│   ├── messages.proto
+│   ├── messages_pb2.py
+│   └── requirements.txt
+└── module.yaml
+{% endhighlight %}
 
-Greeting actions are triggered by consuming, routing and passing messages that are defined using ProtoBuf.
+## Start With Events
+
+Stateful Functions is an event driven system, so development begins by defining our events.
+The greeter application will define its events using [protocol buffers](https://developers.google.com/protocol-buffers). 
+When a greet request for a particular user is ingested, it will be routed to the appropriate function.
+The response will be returned with an appropriate greeting.
+The third type, `SeenCount`, is a utility class that will be used latter on to help manage the number of times a user has been seen so far.
 
 {% highlight proto %}
 syntax = "proto3";
 
+package example;
+
+// External request sent by a user who wants to be greeted
 message GreetRequest {
-    string who = 1;
+    // The name of the user to greet
+    string name = 1;
 }
-
+// A customized response sent to the user
 message GreetResponse {
-    string who = 1;
+    // The name of the user being greeted
+    string name = 1;
+    // The users customized greeting
     string greeting = 2;
 }
+// An internal message used to store state
+message SeenCount {
+    // The number of times a users has been seen so far
+    int64 seen = 1;
+}
 {% endhighlight %}
 
-Under the hood, messages are processed using [stateful functions]({{ site.baseurl }}/sdk/java.html), by definition any class that implements the ``StatefulFunction`` interface.
 
-{% highlight java %}
-package org.apache.flink.statefun.examples.greeter;
+## Our First Function
 
-import org.apache.flink.statefun.sdk.Context;
-import org.apache.flink.statefun.sdk.StatefulFunction;
+Under the hood, messages are processed using [stateful functions]({{ site.baseurl }}/sdk/python.html), which is any two argument function that is bound to the ``StatefulFunction`` runtime.
+Functions are bound to the runtime with the `@function.bind` decorator.
+When binding a function, it is annotated with a function type.
+This is the name used to reference this function when sending it messages.
 
-public final class GreetFunction implements StatefulFunction {
+When you open the file `greeter/greeter.py` you should see the following code.
 
-    @Override
-    public void invoke(Context context, Object input) {
-        GreetRequest greetMessage = (GreetRequest) input;
+{% highlight python %}
+from statefun import StatefulFunctions
 
-        GreetResponse response = GreetResponse.newBuilder()
-            .setWho(greetMessage.getWho())
-            .setGreeting("Hello " + greetMessage.getWho())
-            .build();
+functions = StatefulFunctions()
 
-        context.send(GreetingConstants.GREETING_EGRESS_ID, response);
-    }
-}
+@functions.bind("example/greeter")
+def greet(context, greet_request):
+    pass
 {% endhighlight %}
 
+A stateful function takes two arguments, a context and message. 
+The [context]({{ site.baseurl }}/sdk/python.html#context-reference) provides access to stateful functions runtime features such as state management and message passing.
+You will explore some of these features as you progress through this walkthrough. 
 
-This function takes in a request and sends a response to an external system (or [egress]({{ site.baseurl }}/io-module/index.html#egress)).
-While this is nice, it does not show off the real power of stateful functions: handling state.
+The other parameter is the input message that has been passed to this function.
+By default messages are passed around as protobuf [Any](https://developers.google.com/protocol-buffers/docs/reference/python-generated#wkt).
+If a function only accepts a known type, you can override the message type using Python 3 type syntax.
+This way you do not need to unwrap the message or check types.
 
-## A Stateful Hello
+{% highlight python %}
+from messages_pb2 import GreetRequest
+from statefun import StatefulFunctions
 
-Suppose you want to generate a personalized response for each user depending on how many times they have sent a request.
+functions = StatefulFunctions()
 
-{% highlight java %}
-private static String greetText(String name, int seen) {
-    switch (seen) {
-        case 0:
-            return String.format("Hello %s !", name);
-        case 1:
-            return String.format("Hello again %s !", name);
-        case 2:
-            return String.format("Third times the charm! %s!", name);
-        case 3:
-            return String.format("Happy to see you once again %s !", name);
-        default:
-            return String.format("Hello at the %d-th time %s", seen + 1, name);
-}
+@functions.bind("example/greeter")
+def greet(context, greet_request: GreetRequest):
+    pass
 {% endhighlight %}
 
-## Routing Messages
+## Sending A Response
 
-To send a user a personalized greeting, the system needs to keep track of how many times it has seen each user so far.
-Speaking in general terms, the simplest solution would be to create one function for every user and independently track the number of times they have been seen. Using most frameworks, this would be prohibitively expensive.
-However, stateful functions are virtual and do not consume any CPU or memory when not actively being invoked.
-That means your application can create as many functions as necessary — in this case, users — without worrying about resource consumption.
+Stateful Functions accept messages and can also send them out.
+Messages can be sent to other functions, as well as external systems (or [egress]({{ site.baseurl }}/io-module/index.html#egress)).
 
-Whenever data is consumed from an external system (or [ingress]({{ site.baseurl }}/io-module/index.html#ingress)), it is routed to a specific function based on a given function type and identifier.
-The function type represents the Class of function to be invoked, such as the Greeter function, while the identifier (``GreetRequest#getWho``) scopes the call to a specific virtual instance based on some key.
+One popular external system is [Apache Kafka](http://kafka.apache.org/).
+As a first step, lets update our function to respond to each input by sending a greeting to a Kafka topic.
 
-{% highlight java %}
-package org.apache.flink.statefun.examples.greeter;
+{% highlight python %}
+from messages_pb2 import GreetRequest, GreetResponse
+from statefun import StatefulFunctions
 
-import org.apache.flink.statefun.examples.kafka.generated.GreetRequest;
-import org.apache.flink.statefun.sdk.io.Router;
+functions = StatefulFunctions()
 
-final class GreetRouter implements Router<GreetRequest> {
+@functions.bind("example/greeter")
+def greet(context, message: GreetRequest):
+    response = GreetResponse()
+    response.name = message.name
+    response.greeting = "Hello {}".format(message.name)
+    
+    egress_message = kafka_egress_record(topic="greetings", key=message.name, value=response)
+    context.pack_and_send_egress("example/greets", egress_message)
+{% endhighlight %} 
 
-    @Override
-    public void route(GreetRequest message, Downstream<GreetRequest> downstream) {
-        downstream.forward(GreetingConstants.GREETER_FUNCTION_TYPE, message.getWho(), message);
-    }
-}
+For each message, a response is constructed and sent to a kafka topic call `greetings` partitioned by `name`.
+The `egress_message` is sent to a an `egress` named `example/greets`.
+This identifier points to a particular Kafka cluster and is configured on deployment below.
+
+## A Stateful Hello
+
+This is a great start, but does not show off the real power of stateful functions - working with state.
+Suppose you want to generate a personalized response for each user depending on how many times they have sent a request.
+
+{% highlight python %}
+def compute_greeting(name, seen):
+    """
+    Compute a personalized greeting, based on the number of times this @name had been seen before.
+    """
+    templates = ["", "Welcome %s", "Nice to see you again %s", "Third time is a charm %s"]
+    if seen < len(templates):
+        greeting = templates[seen] % name
+    else:
+        greeting = "Nice to see you at the %d-nth time %s!" % (seen, name)
+
+    response = GreetResponse()
+    response.name = name
+    response.greeting = greeting
+
+    return response
 {% endhighlight %}
 
-So, if a message for a user named John comes in, it will be shipped to John’s dedicated Greeter function.
-In case there is a following message for a user named Jane, a new instance of the Greeter function will be spawned.
+To “remember” information across multiple greeting messages, you then need to associate a persisted value field (``seen_count``) to the Greet function.
+For each user, functions can now track how many times they have been seen.
 
-## Persistence
+{% highlight python %}
+@functions.bind("example/greeter")
+def greet(context, message: GreetRequest):
+    state = context.state('seen_count').unpack(SeenCount)
+    if not state:
+        state = SeenCount()
+        state.seen = 1
+    else:
+        state.seen += 1
+    context.state('seen_count').pack(state)
 
-[Persisted value]({{ site.baseurl }}/sdk/java.html#persistence) is a special data type that enables stateful functions to maintain fault-tolerant state scoped to their identifiers, so that each instance of a function can track state independently.
-To “remember” information across multiple greeting messages, you then need to associate a persisted value field (``count``) to the Greet function. For each user, functions can now track how many times they have been seen.
+    response = compute_greeting(greet_request.name, state.seen)
 
-{% highlight java %}
-package org.apache.flink.statefun.examples.greeter;
+    egress_message = kafka_egress_record(topic="greetings", key=greet_request.name, value=response)
+    context.pack_and_send_egress("example/greets", egress_message)
+{% endhighlight %}
 
-import org.apache.flink.statefun.sdk.Context;
-import org.apache.flink.statefun.sdk.StatefulFunction;
-import org.apache.flink.statefun.sdk.annotations.Persisted;
-import org.apache.flink.statefun.sdk.state.PersistedValue;
+The state, `seen_count` is always scoped to the current name so it can track each user independently.
 
-public final class GreetFunction implements StatefulFunction {
+## Wiring It All Together
 
-    @Persisted
-    private final PersistedValue<Integer> count = PersistedValue.of("count", Integer.class);
+Stateful Function applications communicate with the Apache Flink runtime using `http`.
+The Python SDK ships with a ``RequestReplyHandler`` that automatically dispatches function calls based on RESTful HTTP ``POSTS``.
+The ``RequestReplyHandler`` may be exposed using any HTTP framework.
 
-    @Override
-    public void invoke(Context context, Object input) {
-        GreetRequest greetMessage = (GreetRequest) input;
+One popular Python web framework is [Flask](https://palletsprojects.com/p/flask/).
+It can be used to quickly and easily expose an application to the Apache Flink runtime.
 
-        GreetResponse response = computePersonalizedGreeting(greetMessage);
+{% highlight python %}
+from statefun import StatefulFunctions
+from statefun import RequestReplyHandler
 
-        context.send(GreetingConstants.GREETING_EGRESS_ID, response);
-    }
+functions = StatefulFunctions()
 
-    private GreetResponse computePersonalizedGreeting(GreetRequest greetMessage) {
-        final String name = greetMessage.getWho();
-        final int seen = count.getOrDefault(0);
-        count.set(seen + 1);
+@functions.bind("walkthrough/greeter")
+def greeter(context, message: GreetRequest):
+    pass
 
-        String greeting = greetText(name, seen);
+handler = RequestReplyHandler(functions)
 
-        return GreetResponse.newBuilder()
-            .setWho(name)
-            .setGreeting(greeting)
-            .build();
-    }
-}
+# Serve the endpoint
+
+from flask import request
+from flask import make_response
+from flask import Flask
+
+app = Flask(__name__)
+
+@app.route('/statefun', methods=['POST'])
+def handle():
+    response_data = handler(request.data)
+    response = make_response(response_data)
+    response.headers.set('Content-Type', 'application/octet-stream')
+    return response
+
+
+if __name__ == "__main__":
+    app.run()
 {% endhighlight %}
 
-Each time a message is processed, the function computes a personalized message for that user.
-It reads and updates the number of times that user has been seen and sends a greeting to the egress.
+## Configuring for Runtime
+
+The Apache Flink runtime makes requests to the greeter function by making `http` calls to the `Flask` server.
+To do that, Flink needs to know what endpoint it can use to reach the server.
+This is also a good time to configure our connection to the input and output Kafka topics.
+The configuration is in a file called `module.yaml`.
+
+{% highlight yaml %}
+version: "1.0"
+module:
+  meta:
+    type: remote
+  spec:
+    functions:
+      - function:
+          meta:
+            kind: http
+            type: example/greeter
+          spec:
+            endpoint: http://python-worker:8000/statefun
+            states:
+              - seen_count
+            maxNumBatchRequests: 500
+            timeout: 2min
+    ingresses:
+      - ingress:
+          meta:
+            type: statefun.kafka.io/routable-protobuf-ingress
+            id: example/names
+          spec:
+            address: kafka-broker:9092
+            consumerGroupId: my-group-id
+            topics:
+              - topic: names
+                typeUrl: com.googleapis/example.GreetRequest
+                targets:
+                  - example/greeter
+    egresses:
+      - egress:
+          meta:
+            type: statefun.kafka.io/generic-egress
+            id: example/greets
+          spec:
+            address: kafka-broker:9092
+            deliverySemantic:
+              type: exactly-once
+              transactionTimeoutMillis: 100000
+{% endhighlight %}
 
-You can check the full code for the application described in this walkthrough [here]({{ site.github_url }}/tree/{{ site.github_branch }}/statefun-examples/statefun-greeter-example).
-In particular, take a look at the module ``GreetingModule``, which is the main entry point for the full application, to see how everything gets tied together.
-You can run this example locally using the provided Docker setup.
+This configuration does a few interesting things.
 
-{% highlight bash %}
-$ docker-compose build 
-$ docker-compose up
+The first is to declare our function, `example/greeter`.
+It includes the endpoint by which it is reachable along with the states the function has access to.
+
+The ingress is the input Kafka topic that routes `GreetRequest` messages to the function.
+Along with basic properties like broker address and consumer group, it contains a list of targets.
+These are the functions each message will be sent to.
+
+The egress is the output Kafka cluster.
+It contains broker specific configurations but allows each message to route to any topic.
+
+## Deployment
+
+Now that the greeter application has been built it is time to deploy. 
+The simplest way to deploy a Stateful Function application is by using the community provided base image and loading your module.
+The base image provides the Stateful Function runtime, it will use the provided `module.yaml` to configure for this specific job.
+This can be found in the docker file in the root directory. 
 
 Review comment:
   ```suggestion
   This can be found in the `Dockerfile` in the root directory. 
   ```

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [flink-statefun] sjwiesman commented on a change in pull request #77: [FLINK-16730][docs] Python SDK Getting Started

Posted by GitBox <gi...@apache.org>.
sjwiesman commented on a change in pull request #77: [FLINK-16730][docs] Python SDK Getting Started
URL: https://github.com/apache/flink-statefun/pull/77#discussion_r399390112
 
 

 ##########
 File path: docs/getting-started/walkthrough.md
 ##########
 @@ -24,177 +24,411 @@ specific language governing permissions and limitations
 under the License.
 -->
 
+Stateful Functions offers a platform for building robust, stateful event-driven applications.
+It provides fine-grained control over state and time, which allows for the implementation of advanced systems.
+In this step-by-step guide you’ll learn how to build a stateful applications with the Stateful Functions API.
+
+* This will be replaced by the TOC
+{:toc}
+
+## What Are You Building?
+
 Like all great introductions in software, this walkthrough will start at the beginning: saying hello.
 The application will run a simple function that accepts a request and responds with a greeting.
 It will not attempt to cover all the complexities of application development, but instead focus on building a stateful function — which is where you will implement your business logic.
 
-* This will be replaced by the TOC
-{:toc}
+## Prerequisites
+
+This walkthrough assumes that you have some familiarity with Python, but you should be able to follow along even if you are coming from a different programming language.
+
+## Help, I’m Stuck! 
+
+If you get stuck, check out the [community support resources](https://flink.apache.org/gettinghelp.html).
+In particular, Apache Flink's [user mailing list](https://flink.apache.org/community.html#mailing-lists) is consistently ranked as one of the most active of any Apache project and a great way to get help quickly.
 
-## A Basic Hello
+## How to Follow Along
+
+If you want to follow along, you will require a computer with [Python 3](https://www.python.org/) along with [Docker](https://www.docker.com/).
+
+{% panel **Note:** Each code block within this walkthrough may not contain the full surrounding class for brevity.
+The full code is available on [at the bottom of this page](#full-application). %}
+
+You can download a zip file with a skeleton project by clicking [here]({{ site.baseurl }}/downloads/walkthrough.zip).
+
+{% unless site.is_stable %}
+<p style="border-radius: 5px; padding: 5px" class="bg-danger">
+    <b>Note</b>: The Stateful Functions project does not publish snapshot versions of the Python SDK to PyPy.
+    Please consider using a stable version of this guide.
+</p>
+{% endunless %}
+
+After unzipping the package, you will find a number of files.
+These include dockerfiles and data generators to run this walkthrough in a local self contained environment.
+
+{% highlight bash %}
+$ tree statefun-walkthrough
+statefun-walkthrough
+├── Dockerfile
+├── README.md
+├── docker-compose.yml
+├── generator
+│   ├── Dockerfile
+│   ├── event-generator.py
+│   └── messages_pb2.py
+├── greeter
+│   ├── Dockerfile
+│   ├── greeter.py
+│   ├── messages.proto
+│   ├── messages_pb2.py
+│   └── requirements.txt
+└── module.yaml
+{% endhighlight %}
 
-Greeting actions are triggered by consuming, routing and passing messages that are defined using ProtoBuf.
+## Start With Events
+
+Stateful Functions is an event driven system, so development begins by defining our events.
+The greeter application will define its events using [protocol buffers](https://developers.google.com/protocol-buffers). 
+When a greet request for a particular user is ingested, it will be routed to the appropriate function.
+The response will be returned with an appropriate greeting.
+The third type, `SeenCount`, is a utility class that will be used latter on to help manage the number of times a user has been seen so far.
 
 {% highlight proto %}
 syntax = "proto3";
 
+package example;
+
+// External request sent by a user who wants to be greeted
 message GreetRequest {
-    string who = 1;
+    // The name of the user to greet
+    string name = 1;
 }
-
+// A customized response sent to the user
 message GreetResponse {
-    string who = 1;
+    // The name of the user being greeted
+    string name = 1;
+    // The users customized greeting
     string greeting = 2;
 }
+// An internal message used to store state
+message SeenCount {
+    // The number of times a users has been seen so far
+    int64 seen = 1;
+}
 {% endhighlight %}
 
-Under the hood, messages are processed using [stateful functions]({{ site.baseurl }}/sdk/java.html), by definition any class that implements the ``StatefulFunction`` interface.
 
-{% highlight java %}
-package org.apache.flink.statefun.examples.greeter;
+## Our First Function
 
-import org.apache.flink.statefun.sdk.Context;
-import org.apache.flink.statefun.sdk.StatefulFunction;
+Under the hood, messages are processed using [stateful functions]({{ site.baseurl }}/sdk/python.html), which is any two argument function that is bound to the ``StatefulFunction`` runtime.
+Functions are bound to the runtime with the `@function.bind` decorator.
+When binding a function, it is annotated with a function type.
+This is the name used to reference this function when sending it messages.
 
-public final class GreetFunction implements StatefulFunction {
+When you open the file `greeter/greeter.py` you should see the following code.
 
-    @Override
-    public void invoke(Context context, Object input) {
-        GreetRequest greetMessage = (GreetRequest) input;
+{% highlight python %}
+from statefun import StatefulFunctions
 
-        GreetResponse response = GreetResponse.newBuilder()
-            .setWho(greetMessage.getWho())
-            .setGreeting("Hello " + greetMessage.getWho())
-            .build();
+functions = StatefulFunctions()
 
-        context.send(GreetingConstants.GREETING_EGRESS_ID, response);
-    }
-}
+@functions.bind("example/greeter")
+def greet(context, greet_request):
+    pass
 {% endhighlight %}
 
+A stateful function takes two arguments, a context and message. 
+The [context]({{ site.baseurl }}/sdk/python.html#context-reference) provides access to stateful functions runtime features such as state management and message passing.
+You will explore some of these features as you progress through this walkthrough. 
 
-This function takes in a request and sends a response to an external system (or [egress]({{ site.baseurl }}/io-module/index.html#egress)).
-While this is nice, it does not show off the real power of stateful functions: handling state.
+The other parameter is the input message that has been passed to this function.
+By default messages are passed around as protobuf [Any](https://developers.google.com/protocol-buffers/docs/reference/python-generated#wkt).
+If a function only accepts a known type, you can override the message type using Python 3 type syntax.
+This way you do not need to unwrap the message or check types.
 
-## A Stateful Hello
+{% highlight python %}
+from messages_pb2 import GreetRequest
+from statefun import StatefulFunctions
 
-Suppose you want to generate a personalized response for each user depending on how many times they have sent a request.
+functions = StatefulFunctions()
 
-{% highlight java %}
-private static String greetText(String name, int seen) {
-    switch (seen) {
-        case 0:
-            return String.format("Hello %s !", name);
-        case 1:
-            return String.format("Hello again %s !", name);
-        case 2:
-            return String.format("Third times the charm! %s!", name);
-        case 3:
-            return String.format("Happy to see you once again %s !", name);
-        default:
-            return String.format("Hello at the %d-th time %s", seen + 1, name);
-}
+@functions.bind("example/greeter")
+def greet(context, greet_request: GreetRequest):
+    pass
 {% endhighlight %}
 
-## Routing Messages
+## Sending A Response
 
-To send a user a personalized greeting, the system needs to keep track of how many times it has seen each user so far.
-Speaking in general terms, the simplest solution would be to create one function for every user and independently track the number of times they have been seen. Using most frameworks, this would be prohibitively expensive.
-However, stateful functions are virtual and do not consume any CPU or memory when not actively being invoked.
-That means your application can create as many functions as necessary — in this case, users — without worrying about resource consumption.
+Stateful Functions accept messages and can also send them out.
+Messages can be sent to other functions, as well as external systems (or [egress]({{ site.baseurl }}/io-module/index.html#egress)).
 
-Whenever data is consumed from an external system (or [ingress]({{ site.baseurl }}/io-module/index.html#ingress)), it is routed to a specific function based on a given function type and identifier.
-The function type represents the Class of function to be invoked, such as the Greeter function, while the identifier (``GreetRequest#getWho``) scopes the call to a specific virtual instance based on some key.
+One popular external system is [Apache Kafka](http://kafka.apache.org/).
+As a first step, lets update our function to respond to each input by sending a greeting to a Kafka topic.
 
-{% highlight java %}
-package org.apache.flink.statefun.examples.greeter;
+{% highlight python %}
+from messages_pb2 import GreetRequest, GreetResponse
+from statefun import StatefulFunctions
 
-import org.apache.flink.statefun.examples.kafka.generated.GreetRequest;
-import org.apache.flink.statefun.sdk.io.Router;
+functions = StatefulFunctions()
 
-final class GreetRouter implements Router<GreetRequest> {
+@functions.bind("example/greeter")
+def greet(context, message: GreetRequest):
+    response = GreetResponse()
+    response.name = message.name
+    response.greeting = "Hello {}".format(message.name)
+    
+    egress_message = kafka_egress_record(topic="greetings", key=message.name, value=response)
+    context.pack_and_send_egress("example/greets", egress_message)
+{% endhighlight %} 
 
-    @Override
-    public void route(GreetRequest message, Downstream<GreetRequest> downstream) {
-        downstream.forward(GreetingConstants.GREETER_FUNCTION_TYPE, message.getWho(), message);
-    }
-}
+For each message, a response is constructed and sent to a kafka topic call `greetings` partitioned by `name`.
+The `egress_message` is sent to a an `egress` named `example/greets`.
+This identifier points to a particular Kafka cluster and is configured on deployment below.
+
+## A Stateful Hello
+
+This is a great start, but does not show off the real power of stateful functions - working with state.
+Suppose you want to generate a personalized response for each user depending on how many times they have sent a request.
+
+{% highlight python %}
+def compute_greeting(name, seen):
+    """
+    Compute a personalized greeting, based on the number of times this @name had been seen before.
+    """
+    templates = ["", "Welcome %s", "Nice to see you again %s", "Third time is a charm %s"]
+    if seen < len(templates):
+        greeting = templates[seen] % name
+    else:
+        greeting = "Nice to see you at the %d-nth time %s!" % (seen, name)
+
+    response = GreetResponse()
+    response.name = name
+    response.greeting = greeting
+
+    return response
 {% endhighlight %}
 
-So, if a message for a user named John comes in, it will be shipped to John’s dedicated Greeter function.
-In case there is a following message for a user named Jane, a new instance of the Greeter function will be spawned.
+To “remember” information across multiple greeting messages, you then need to associate a persisted value field (``seen_count``) to the Greet function.
+For each user, functions can now track how many times they have been seen.
 
-## Persistence
+{% highlight python %}
+@functions.bind("example/greeter")
+def greet(context, message: GreetRequest):
+    state = context.state('seen_count').unpack(SeenCount)
+    if not state:
+        state = SeenCount()
+        state.seen = 1
+    else:
+        state.seen += 1
+    context.state('seen_count').pack(state)
 
-[Persisted value]({{ site.baseurl }}/sdk/java.html#persistence) is a special data type that enables stateful functions to maintain fault-tolerant state scoped to their identifiers, so that each instance of a function can track state independently.
-To “remember” information across multiple greeting messages, you then need to associate a persisted value field (``count``) to the Greet function. For each user, functions can now track how many times they have been seen.
+    response = compute_greeting(greet_request.name, state.seen)
 
-{% highlight java %}
-package org.apache.flink.statefun.examples.greeter;
+    egress_message = kafka_egress_record(topic="greetings", key=greet_request.name, value=response)
+    context.pack_and_send_egress("example/greets", egress_message)
+{% endhighlight %}
 
-import org.apache.flink.statefun.sdk.Context;
-import org.apache.flink.statefun.sdk.StatefulFunction;
-import org.apache.flink.statefun.sdk.annotations.Persisted;
-import org.apache.flink.statefun.sdk.state.PersistedValue;
+The state, `seen_count` is always scoped to the current name so it can track each user independently.
 
-public final class GreetFunction implements StatefulFunction {
+## Wiring It All Together
 
-    @Persisted
-    private final PersistedValue<Integer> count = PersistedValue.of("count", Integer.class);
+Stateful Function applications communicate with the Apache Flink runtime using `http`.
+The Python SDK ships with a ``RequestReplyHandler`` that automatically dispatches function calls based on RESTful HTTP ``POSTS``.
+The ``RequestReplyHandler`` may be exposed using any HTTP framework.
 
-    @Override
-    public void invoke(Context context, Object input) {
-        GreetRequest greetMessage = (GreetRequest) input;
+One popular Python web framework is [Flask](https://palletsprojects.com/p/flask/).
+It can be used to quickly and easily expose an application to the Apache Flink runtime.
 
-        GreetResponse response = computePersonalizedGreeting(greetMessage);
+{% highlight python %}
+from statefun import StatefulFunctions
+from statefun import RequestReplyHandler
 
-        context.send(GreetingConstants.GREETING_EGRESS_ID, response);
-    }
+functions = StatefulFunctions()
 
-    private GreetResponse computePersonalizedGreeting(GreetRequest greetMessage) {
-        final String name = greetMessage.getWho();
-        final int seen = count.getOrDefault(0);
-        count.set(seen + 1);
+@functions.bind("walkthrough/greeter")
+def greeter(context, message: GreetRequest):
+    pass
 
-        String greeting = greetText(name, seen);
+handler = RequestReplyHandler(functions)
 
-        return GreetResponse.newBuilder()
-            .setWho(name)
-            .setGreeting(greeting)
-            .build();
-    }
-}
+# Serve the endpoint
+
+from flask import request
+from flask import make_response
+from flask import Flask
+
+app = Flask(__name__)
+
+@app.route('/statefun', methods=['POST'])
+def handle():
+    response_data = handler(request.data)
+    response = make_response(response_data)
+    response.headers.set('Content-Type', 'application/octet-stream')
+    return response
+
+
+if __name__ == "__main__":
+    app.run()
 {% endhighlight %}
 
-Each time a message is processed, the function computes a personalized message for that user.
-It reads and updates the number of times that user has been seen and sends a greeting to the egress.
+## Configuring for Runtime
+
+The Apache Flink runtime makes requests to the greeter function by making `http` calls to the `Flask` server.
+To do that, Flink needs to know what endpoint it can use to reach the server.
+This is also a good time to configure our connection to the input and output Kafka topics.
+The configuration is in a file called `module.yaml`.
+
+{% highlight yaml %}
+version: "1.0"
+module:
+  meta:
+    type: remote
+  spec:
+    functions:
+      - function:
+          meta:
+            kind: http
+            type: example/greeter
+          spec:
+            endpoint: http://python-worker:8000/statefun
+            states:
+              - seen_count
+            maxNumBatchRequests: 500
+            timeout: 2min
+    ingresses:
+      - ingress:
+          meta:
+            type: statefun.kafka.io/routable-protobuf-ingress
+            id: example/names
+          spec:
+            address: kafka-broker:9092
+            consumerGroupId: my-group-id
+            topics:
+              - topic: names
+                typeUrl: com.googleapis/example.GreetRequest
+                targets:
+                  - example/greeter
+    egresses:
+      - egress:
+          meta:
+            type: statefun.kafka.io/generic-egress
+            id: example/greets
+          spec:
+            address: kafka-broker:9092
+            deliverySemantic:
+              type: exactly-once
+              transactionTimeoutMillis: 100000
+{% endhighlight %}
 
-You can check the full code for the application described in this walkthrough [here]({{ site.github_url }}/tree/{{ site.github_branch }}/statefun-examples/statefun-greeter-example).
-In particular, take a look at the module ``GreetingModule``, which is the main entry point for the full application, to see how everything gets tied together.
-You can run this example locally using the provided Docker setup.
+This configuration does a few interesting things.
 
-{% highlight bash %}
-$ docker-compose build 
-$ docker-compose up
+The first is to declare our function, `example/greeter`.
+It includes the endpoint by which it is reachable along with the states the function has access to.
+
+The ingress is the input Kafka topic that routes `GreetRequest` messages to the function.
+Along with basic properties like broker address and consumer group, it contains a list of targets.
+These are the functions each message will be sent to.
+
+The egress is the output Kafka cluster.
+It contains broker specific configurations but allows each message to route to any topic.
+
+## Deployment
+
+Now that the greeter application has been built it is time to deploy. 
+The simplest way to deploy a Stateful Function application is by using the community provided base image and loading your module.
+The base image provides the Stateful Function runtime, it will use the provided `module.yaml` to configure for this specific job.
+This can be found in the docker file in the root directory. 
+
+{% highlight docker %}
+FROM statefun
+
+RUN mkdir -p /opt/statefun/modules/greeter
+ADD module.yaml /opt/statefun/modules/greeter
 {% endhighlight %}
 
-Then, send some messages to the topic "names", and observe what comes out of "greetings".
+You can now run this application locally using the provided Docker setup.
 
 {% highlight bash %}
-$ docker-compose exec kafka-broker kafka-console-producer.sh \
-    --broker-list localhost:9092 \
-    --topic names
+$ docker-compose up -d
 
 Review comment:
   Not yet because we haven't released :) In the mean time you can build the image locally using the `./tools/docker/build-stateful-functions.sh` script in the tools folder. 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [flink-statefun] sjwiesman commented on a change in pull request #77: [FLINK-16730][docs] Python SDK Getting Started

Posted by GitBox <gi...@apache.org>.
sjwiesman commented on a change in pull request #77: [FLINK-16730][docs] Python SDK Getting Started
URL: https://github.com/apache/flink-statefun/pull/77#discussion_r399487451
 
 

 ##########
 File path: docs/getting-started/walkthrough.md
 ##########
 @@ -24,177 +24,411 @@ specific language governing permissions and limitations
 under the License.
 -->
 
+Stateful Functions offers a platform for building robust, stateful event-driven applications.
+It provides fine-grained control over state and time, which allows for the implementation of advanced systems.
+In this step-by-step guide you’ll learn how to build a stateful applications with the Stateful Functions API.
+
+* This will be replaced by the TOC
+{:toc}
+
+## What Are You Building?
+
 Like all great introductions in software, this walkthrough will start at the beginning: saying hello.
 The application will run a simple function that accepts a request and responds with a greeting.
 It will not attempt to cover all the complexities of application development, but instead focus on building a stateful function — which is where you will implement your business logic.
 
-* This will be replaced by the TOC
-{:toc}
+## Prerequisites
+
+This walkthrough assumes that you have some familiarity with Python, but you should be able to follow along even if you are coming from a different programming language.
+
+## Help, I’m Stuck! 
+
+If you get stuck, check out the [community support resources](https://flink.apache.org/gettinghelp.html).
+In particular, Apache Flink's [user mailing list](https://flink.apache.org/community.html#mailing-lists) is consistently ranked as one of the most active of any Apache project and a great way to get help quickly.
 
-## A Basic Hello
+## How to Follow Along
+
+If you want to follow along, you will require a computer with [Python 3](https://www.python.org/) along with [Docker](https://www.docker.com/).
+
+{% panel **Note:** Each code block within this walkthrough may not contain the full surrounding class for brevity.
+The full code is available on [at the bottom of this page](#full-application). %}
+
+You can download a zip file with a skeleton project by clicking [here]({{ site.baseurl }}/downloads/walkthrough.zip).
+
+{% unless site.is_stable %}
+<p style="border-radius: 5px; padding: 5px" class="bg-danger">
+    <b>Note</b>: The Stateful Functions project does not publish snapshot versions of the Python SDK to PyPy.
+    Please consider using a stable version of this guide.
+</p>
+{% endunless %}
+
+After unzipping the package, you will find a number of files.
+These include dockerfiles and data generators to run this walkthrough in a local self contained environment.
+
+{% highlight bash %}
+$ tree statefun-walkthrough
+statefun-walkthrough
+├── Dockerfile
+├── README.md
+├── docker-compose.yml
+├── generator
+│   ├── Dockerfile
+│   ├── event-generator.py
+│   └── messages_pb2.py
+├── greeter
+│   ├── Dockerfile
+│   ├── greeter.py
+│   ├── messages.proto
+│   ├── messages_pb2.py
+│   └── requirements.txt
+└── module.yaml
+{% endhighlight %}
 
-Greeting actions are triggered by consuming, routing and passing messages that are defined using ProtoBuf.
+## Start With Events
+
+Stateful Functions is an event driven system, so development begins by defining our events.
+The greeter application will define its events using [protocol buffers](https://developers.google.com/protocol-buffers). 
+When a greet request for a particular user is ingested, it will be routed to the appropriate function.
+The response will be returned with an appropriate greeting.
+The third type, `SeenCount`, is a utility class that will be used latter on to help manage the number of times a user has been seen so far.
 
 {% highlight proto %}
 syntax = "proto3";
 
+package example;
+
+// External request sent by a user who wants to be greeted
 message GreetRequest {
-    string who = 1;
+    // The name of the user to greet
+    string name = 1;
 }
-
+// A customized response sent to the user
 message GreetResponse {
-    string who = 1;
+    // The name of the user being greeted
+    string name = 1;
+    // The users customized greeting
     string greeting = 2;
 }
+// An internal message used to store state
+message SeenCount {
+    // The number of times a users has been seen so far
+    int64 seen = 1;
+}
 {% endhighlight %}
 
-Under the hood, messages are processed using [stateful functions]({{ site.baseurl }}/sdk/java.html), by definition any class that implements the ``StatefulFunction`` interface.
 
-{% highlight java %}
-package org.apache.flink.statefun.examples.greeter;
+## Our First Function
 
-import org.apache.flink.statefun.sdk.Context;
-import org.apache.flink.statefun.sdk.StatefulFunction;
+Under the hood, messages are processed using [stateful functions]({{ site.baseurl }}/sdk/python.html), which is any two argument function that is bound to the ``StatefulFunction`` runtime.
+Functions are bound to the runtime with the `@function.bind` decorator.
+When binding a function, it is annotated with a function type.
+This is the name used to reference this function when sending it messages.
 
-public final class GreetFunction implements StatefulFunction {
+When you open the file `greeter/greeter.py` you should see the following code.
 
-    @Override
-    public void invoke(Context context, Object input) {
-        GreetRequest greetMessage = (GreetRequest) input;
+{% highlight python %}
+from statefun import StatefulFunctions
 
-        GreetResponse response = GreetResponse.newBuilder()
-            .setWho(greetMessage.getWho())
-            .setGreeting("Hello " + greetMessage.getWho())
-            .build();
+functions = StatefulFunctions()
 
-        context.send(GreetingConstants.GREETING_EGRESS_ID, response);
-    }
-}
+@functions.bind("example/greeter")
+def greet(context, greet_request):
+    pass
 {% endhighlight %}
 
+A stateful function takes two arguments, a context and message. 
+The [context]({{ site.baseurl }}/sdk/python.html#context-reference) provides access to stateful functions runtime features such as state management and message passing.
+You will explore some of these features as you progress through this walkthrough. 
 
-This function takes in a request and sends a response to an external system (or [egress]({{ site.baseurl }}/io-module/index.html#egress)).
-While this is nice, it does not show off the real power of stateful functions: handling state.
+The other parameter is the input message that has been passed to this function.
+By default messages are passed around as protobuf [Any](https://developers.google.com/protocol-buffers/docs/reference/python-generated#wkt).
+If a function only accepts a known type, you can override the message type using Python 3 type syntax.
+This way you do not need to unwrap the message or check types.
 
-## A Stateful Hello
+{% highlight python %}
+from messages_pb2 import GreetRequest
+from statefun import StatefulFunctions
 
-Suppose you want to generate a personalized response for each user depending on how many times they have sent a request.
+functions = StatefulFunctions()
 
-{% highlight java %}
-private static String greetText(String name, int seen) {
-    switch (seen) {
-        case 0:
-            return String.format("Hello %s !", name);
-        case 1:
-            return String.format("Hello again %s !", name);
-        case 2:
-            return String.format("Third times the charm! %s!", name);
-        case 3:
-            return String.format("Happy to see you once again %s !", name);
-        default:
-            return String.format("Hello at the %d-th time %s", seen + 1, name);
-}
+@functions.bind("example/greeter")
+def greet(context, greet_request: GreetRequest):
+    pass
 {% endhighlight %}
 
-## Routing Messages
+## Sending A Response
 
-To send a user a personalized greeting, the system needs to keep track of how many times it has seen each user so far.
-Speaking in general terms, the simplest solution would be to create one function for every user and independently track the number of times they have been seen. Using most frameworks, this would be prohibitively expensive.
-However, stateful functions are virtual and do not consume any CPU or memory when not actively being invoked.
-That means your application can create as many functions as necessary — in this case, users — without worrying about resource consumption.
+Stateful Functions accept messages and can also send them out.
+Messages can be sent to other functions, as well as external systems (or [egress]({{ site.baseurl }}/io-module/index.html#egress)).
 
-Whenever data is consumed from an external system (or [ingress]({{ site.baseurl }}/io-module/index.html#ingress)), it is routed to a specific function based on a given function type and identifier.
-The function type represents the Class of function to be invoked, such as the Greeter function, while the identifier (``GreetRequest#getWho``) scopes the call to a specific virtual instance based on some key.
+One popular external system is [Apache Kafka](http://kafka.apache.org/).
+As a first step, lets update our function to respond to each input by sending a greeting to a Kafka topic.
 
-{% highlight java %}
-package org.apache.flink.statefun.examples.greeter;
+{% highlight python %}
+from messages_pb2 import GreetRequest, GreetResponse
+from statefun import StatefulFunctions
 
-import org.apache.flink.statefun.examples.kafka.generated.GreetRequest;
-import org.apache.flink.statefun.sdk.io.Router;
+functions = StatefulFunctions()
 
-final class GreetRouter implements Router<GreetRequest> {
+@functions.bind("example/greeter")
+def greet(context, message: GreetRequest):
+    response = GreetResponse()
+    response.name = message.name
+    response.greeting = "Hello {}".format(message.name)
+    
+    egress_message = kafka_egress_record(topic="greetings", key=message.name, value=response)
+    context.pack_and_send_egress("example/greets", egress_message)
+{% endhighlight %} 
 
-    @Override
-    public void route(GreetRequest message, Downstream<GreetRequest> downstream) {
-        downstream.forward(GreetingConstants.GREETER_FUNCTION_TYPE, message.getWho(), message);
-    }
-}
+For each message, a response is constructed and sent to a kafka topic call `greetings` partitioned by `name`.
+The `egress_message` is sent to a an `egress` named `example/greets`.
+This identifier points to a particular Kafka cluster and is configured on deployment below.
+
+## A Stateful Hello
+
+This is a great start, but does not show off the real power of stateful functions - working with state.
+Suppose you want to generate a personalized response for each user depending on how many times they have sent a request.
+
+{% highlight python %}
+def compute_greeting(name, seen):
+    """
+    Compute a personalized greeting, based on the number of times this @name had been seen before.
+    """
+    templates = ["", "Welcome %s", "Nice to see you again %s", "Third time is a charm %s"]
+    if seen < len(templates):
+        greeting = templates[seen] % name
+    else:
+        greeting = "Nice to see you at the %d-nth time %s!" % (seen, name)
+
+    response = GreetResponse()
+    response.name = name
+    response.greeting = greeting
+
+    return response
 {% endhighlight %}
 
-So, if a message for a user named John comes in, it will be shipped to John’s dedicated Greeter function.
-In case there is a following message for a user named Jane, a new instance of the Greeter function will be spawned.
+To “remember” information across multiple greeting messages, you then need to associate a persisted value field (``seen_count``) to the Greet function.
+For each user, functions can now track how many times they have been seen.
 
-## Persistence
+{% highlight python %}
+@functions.bind("example/greeter")
+def greet(context, message: GreetRequest):
+    state = context.state('seen_count').unpack(SeenCount)
+    if not state:
+        state = SeenCount()
+        state.seen = 1
+    else:
+        state.seen += 1
+    context.state('seen_count').pack(state)
 
-[Persisted value]({{ site.baseurl }}/sdk/java.html#persistence) is a special data type that enables stateful functions to maintain fault-tolerant state scoped to their identifiers, so that each instance of a function can track state independently.
-To “remember” information across multiple greeting messages, you then need to associate a persisted value field (``count``) to the Greet function. For each user, functions can now track how many times they have been seen.
+    response = compute_greeting(greet_request.name, state.seen)
 
-{% highlight java %}
-package org.apache.flink.statefun.examples.greeter;
+    egress_message = kafka_egress_record(topic="greetings", key=greet_request.name, value=response)
+    context.pack_and_send_egress("example/greets", egress_message)
+{% endhighlight %}
 
-import org.apache.flink.statefun.sdk.Context;
-import org.apache.flink.statefun.sdk.StatefulFunction;
-import org.apache.flink.statefun.sdk.annotations.Persisted;
-import org.apache.flink.statefun.sdk.state.PersistedValue;
+The state, `seen_count` is always scoped to the current name so it can track each user independently.
 
-public final class GreetFunction implements StatefulFunction {
+## Wiring It All Together
 
-    @Persisted
-    private final PersistedValue<Integer> count = PersistedValue.of("count", Integer.class);
+Stateful Function applications communicate with the Apache Flink runtime using `http`.
+The Python SDK ships with a ``RequestReplyHandler`` that automatically dispatches function calls based on RESTful HTTP ``POSTS``.
+The ``RequestReplyHandler`` may be exposed using any HTTP framework.
 
-    @Override
-    public void invoke(Context context, Object input) {
-        GreetRequest greetMessage = (GreetRequest) input;
+One popular Python web framework is [Flask](https://palletsprojects.com/p/flask/).
+It can be used to quickly and easily expose an application to the Apache Flink runtime.
 
-        GreetResponse response = computePersonalizedGreeting(greetMessage);
+{% highlight python %}
+from statefun import StatefulFunctions
+from statefun import RequestReplyHandler
 
-        context.send(GreetingConstants.GREETING_EGRESS_ID, response);
-    }
+functions = StatefulFunctions()
 
-    private GreetResponse computePersonalizedGreeting(GreetRequest greetMessage) {
-        final String name = greetMessage.getWho();
-        final int seen = count.getOrDefault(0);
-        count.set(seen + 1);
+@functions.bind("walkthrough/greeter")
+def greeter(context, message: GreetRequest):
+    pass
 
-        String greeting = greetText(name, seen);
+handler = RequestReplyHandler(functions)
 
-        return GreetResponse.newBuilder()
-            .setWho(name)
-            .setGreeting(greeting)
-            .build();
-    }
-}
+# Serve the endpoint
+
+from flask import request
+from flask import make_response
+from flask import Flask
+
+app = Flask(__name__)
+
+@app.route('/statefun', methods=['POST'])
+def handle():
+    response_data = handler(request.data)
+    response = make_response(response_data)
+    response.headers.set('Content-Type', 'application/octet-stream')
+    return response
+
+
+if __name__ == "__main__":
+    app.run()
 {% endhighlight %}
 
-Each time a message is processed, the function computes a personalized message for that user.
-It reads and updates the number of times that user has been seen and sends a greeting to the egress.
+## Configuring for Runtime
+
+The Apache Flink runtime makes requests to the greeter function by making `http` calls to the `Flask` server.
+To do that, Flink needs to know what endpoint it can use to reach the server.
+This is also a good time to configure our connection to the input and output Kafka topics.
+The configuration is in a file called `module.yaml`.
+
+{% highlight yaml %}
+version: "1.0"
+module:
+  meta:
+    type: remote
+  spec:
+    functions:
+      - function:
+          meta:
+            kind: http
+            type: example/greeter
+          spec:
+            endpoint: http://python-worker:8000/statefun
+            states:
+              - seen_count
+            maxNumBatchRequests: 500
+            timeout: 2min
+    ingresses:
+      - ingress:
+          meta:
+            type: statefun.kafka.io/routable-protobuf-ingress
+            id: example/names
+          spec:
+            address: kafka-broker:9092
+            consumerGroupId: my-group-id
+            topics:
+              - topic: names
+                typeUrl: com.googleapis/example.GreetRequest
+                targets:
+                  - example/greeter
+    egresses:
+      - egress:
+          meta:
+            type: statefun.kafka.io/generic-egress
+            id: example/greets
+          spec:
+            address: kafka-broker:9092
+            deliverySemantic:
+              type: exactly-once
+              transactionTimeoutMillis: 100000
+{% endhighlight %}
 
-You can check the full code for the application described in this walkthrough [here]({{ site.github_url }}/tree/{{ site.github_branch }}/statefun-examples/statefun-greeter-example).
-In particular, take a look at the module ``GreetingModule``, which is the main entry point for the full application, to see how everything gets tied together.
-You can run this example locally using the provided Docker setup.
+This configuration does a few interesting things.
 
-{% highlight bash %}
-$ docker-compose build 
-$ docker-compose up
+The first is to declare our function, `example/greeter`.
+It includes the endpoint by which it is reachable along with the states the function has access to.
+
+The ingress is the input Kafka topic that routes `GreetRequest` messages to the function.
+Along with basic properties like broker address and consumer group, it contains a list of targets.
+These are the functions each message will be sent to.
+
+The egress is the output Kafka cluster.
+It contains broker specific configurations but allows each message to route to any topic.
+
+## Deployment
+
+Now that the greeter application has been built it is time to deploy. 
+The simplest way to deploy a Stateful Function application is by using the community provided base image and loading your module.
+The base image provides the Stateful Function runtime, it will use the provided `module.yaml` to configure for this specific job.
+This can be found in the docker file in the root directory. 
+
+{% highlight docker %}
+FROM statefun
+
+RUN mkdir -p /opt/statefun/modules/greeter
+ADD module.yaml /opt/statefun/modules/greeter
 {% endhighlight %}
 
-Then, send some messages to the topic "names", and observe what comes out of "greetings".
+You can now run this application locally using the provided Docker setup.
 
 {% highlight bash %}
-$ docker-compose exec kafka-broker kafka-console-producer.sh \
-    --broker-list localhost:9092 \
-    --topic names
+$ docker-compose up -d
 
 Review comment:
   Update `greeter/Dockerfile` to be
   
   ```
   FROM python:3.7-alpine
   
   RUN mkdir -p /app
   WORKDIR /app
   
   COPY apache_flink_statefun-snapshot-py3-none-any.whl /app
   RUN pip install apache_flink_statefun-snapshot-py3-none-any.whl
   
   COPY requirements.txt /app
   RUN pip install -r requirements.txt
   
   COPY greeter.py /app
   COPY messages_pb2.py /app
   
   EXPOSE 8000
   
   CMD ["gunicorn", "-b", "0.0.0.0:8000", "-w 4", "greeter:app"]
   ```
   
   Again, just a stop gap until the first release. This is why I have the warning that we do not publish snapshot versions of the python sdk. 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [flink-statefun] sjwiesman commented on a change in pull request #77: [FLINK-16730][docs] Python SDK Getting Started

Posted by GitBox <gi...@apache.org>.
sjwiesman commented on a change in pull request #77: [FLINK-16730][docs] Python SDK Getting Started
URL: https://github.com/apache/flink-statefun/pull/77#discussion_r399405348
 
 

 ##########
 File path: docs/getting-started/walkthrough.md
 ##########
 @@ -24,177 +24,411 @@ specific language governing permissions and limitations
 under the License.
 -->
 
+Stateful Functions offers a platform for building robust, stateful event-driven applications.
+It provides fine-grained control over state and time, which allows for the implementation of advanced systems.
+In this step-by-step guide you’ll learn how to build a stateful applications with the Stateful Functions API.
+
+* This will be replaced by the TOC
+{:toc}
+
+## What Are You Building?
+
 Like all great introductions in software, this walkthrough will start at the beginning: saying hello.
 The application will run a simple function that accepts a request and responds with a greeting.
 It will not attempt to cover all the complexities of application development, but instead focus on building a stateful function — which is where you will implement your business logic.
 
-* This will be replaced by the TOC
-{:toc}
+## Prerequisites
+
+This walkthrough assumes that you have some familiarity with Python, but you should be able to follow along even if you are coming from a different programming language.
+
+## Help, I’m Stuck! 
+
+If you get stuck, check out the [community support resources](https://flink.apache.org/gettinghelp.html).
+In particular, Apache Flink's [user mailing list](https://flink.apache.org/community.html#mailing-lists) is consistently ranked as one of the most active of any Apache project and a great way to get help quickly.
 
-## A Basic Hello
+## How to Follow Along
+
+If you want to follow along, you will require a computer with [Python 3](https://www.python.org/) along with [Docker](https://www.docker.com/).
+
+{% panel **Note:** Each code block within this walkthrough may not contain the full surrounding class for brevity.
+The full code is available on [at the bottom of this page](#full-application). %}
+
+You can download a zip file with a skeleton project by clicking [here]({{ site.baseurl }}/downloads/walkthrough.zip).
+
+{% unless site.is_stable %}
+<p style="border-radius: 5px; padding: 5px" class="bg-danger">
+    <b>Note</b>: The Stateful Functions project does not publish snapshot versions of the Python SDK to PyPy.
+    Please consider using a stable version of this guide.
+</p>
+{% endunless %}
+
+After unzipping the package, you will find a number of files.
+These include dockerfiles and data generators to run this walkthrough in a local self contained environment.
+
+{% highlight bash %}
+$ tree statefun-walkthrough
+statefun-walkthrough
+├── Dockerfile
+├── README.md
+├── docker-compose.yml
+├── generator
+│   ├── Dockerfile
+│   ├── event-generator.py
+│   └── messages_pb2.py
+├── greeter
+│   ├── Dockerfile
+│   ├── greeter.py
+│   ├── messages.proto
+│   ├── messages_pb2.py
+│   └── requirements.txt
+└── module.yaml
+{% endhighlight %}
 
-Greeting actions are triggered by consuming, routing and passing messages that are defined using ProtoBuf.
+## Start With Events
+
+Stateful Functions is an event driven system, so development begins by defining our events.
+The greeter application will define its events using [protocol buffers](https://developers.google.com/protocol-buffers). 
+When a greet request for a particular user is ingested, it will be routed to the appropriate function.
+The response will be returned with an appropriate greeting.
+The third type, `SeenCount`, is a utility class that will be used latter on to help manage the number of times a user has been seen so far.
 
 {% highlight proto %}
 syntax = "proto3";
 
+package example;
+
+// External request sent by a user who wants to be greeted
 message GreetRequest {
-    string who = 1;
+    // The name of the user to greet
+    string name = 1;
 }
-
+// A customized response sent to the user
 message GreetResponse {
-    string who = 1;
+    // The name of the user being greeted
+    string name = 1;
+    // The users customized greeting
     string greeting = 2;
 }
+// An internal message used to store state
+message SeenCount {
+    // The number of times a users has been seen so far
+    int64 seen = 1;
+}
 {% endhighlight %}
 
-Under the hood, messages are processed using [stateful functions]({{ site.baseurl }}/sdk/java.html), by definition any class that implements the ``StatefulFunction`` interface.
 
-{% highlight java %}
-package org.apache.flink.statefun.examples.greeter;
+## Our First Function
 
-import org.apache.flink.statefun.sdk.Context;
-import org.apache.flink.statefun.sdk.StatefulFunction;
+Under the hood, messages are processed using [stateful functions]({{ site.baseurl }}/sdk/python.html), which is any two argument function that is bound to the ``StatefulFunction`` runtime.
+Functions are bound to the runtime with the `@function.bind` decorator.
+When binding a function, it is annotated with a function type.
+This is the name used to reference this function when sending it messages.
 
-public final class GreetFunction implements StatefulFunction {
+When you open the file `greeter/greeter.py` you should see the following code.
 
-    @Override
-    public void invoke(Context context, Object input) {
-        GreetRequest greetMessage = (GreetRequest) input;
+{% highlight python %}
+from statefun import StatefulFunctions
 
-        GreetResponse response = GreetResponse.newBuilder()
-            .setWho(greetMessage.getWho())
-            .setGreeting("Hello " + greetMessage.getWho())
-            .build();
+functions = StatefulFunctions()
 
-        context.send(GreetingConstants.GREETING_EGRESS_ID, response);
-    }
-}
+@functions.bind("example/greeter")
+def greet(context, greet_request):
+    pass
 {% endhighlight %}
 
+A stateful function takes two arguments, a context and message. 
+The [context]({{ site.baseurl }}/sdk/python.html#context-reference) provides access to stateful functions runtime features such as state management and message passing.
+You will explore some of these features as you progress through this walkthrough. 
 
-This function takes in a request and sends a response to an external system (or [egress]({{ site.baseurl }}/io-module/index.html#egress)).
-While this is nice, it does not show off the real power of stateful functions: handling state.
+The other parameter is the input message that has been passed to this function.
+By default messages are passed around as protobuf [Any](https://developers.google.com/protocol-buffers/docs/reference/python-generated#wkt).
+If a function only accepts a known type, you can override the message type using Python 3 type syntax.
+This way you do not need to unwrap the message or check types.
 
-## A Stateful Hello
+{% highlight python %}
+from messages_pb2 import GreetRequest
+from statefun import StatefulFunctions
 
-Suppose you want to generate a personalized response for each user depending on how many times they have sent a request.
+functions = StatefulFunctions()
 
-{% highlight java %}
-private static String greetText(String name, int seen) {
-    switch (seen) {
-        case 0:
-            return String.format("Hello %s !", name);
-        case 1:
-            return String.format("Hello again %s !", name);
-        case 2:
-            return String.format("Third times the charm! %s!", name);
-        case 3:
-            return String.format("Happy to see you once again %s !", name);
-        default:
-            return String.format("Hello at the %d-th time %s", seen + 1, name);
-}
+@functions.bind("example/greeter")
+def greet(context, greet_request: GreetRequest):
+    pass
 {% endhighlight %}
 
-## Routing Messages
+## Sending A Response
 
-To send a user a personalized greeting, the system needs to keep track of how many times it has seen each user so far.
-Speaking in general terms, the simplest solution would be to create one function for every user and independently track the number of times they have been seen. Using most frameworks, this would be prohibitively expensive.
-However, stateful functions are virtual and do not consume any CPU or memory when not actively being invoked.
-That means your application can create as many functions as necessary — in this case, users — without worrying about resource consumption.
+Stateful Functions accept messages and can also send them out.
+Messages can be sent to other functions, as well as external systems (or [egress]({{ site.baseurl }}/io-module/index.html#egress)).
 
-Whenever data is consumed from an external system (or [ingress]({{ site.baseurl }}/io-module/index.html#ingress)), it is routed to a specific function based on a given function type and identifier.
-The function type represents the Class of function to be invoked, such as the Greeter function, while the identifier (``GreetRequest#getWho``) scopes the call to a specific virtual instance based on some key.
+One popular external system is [Apache Kafka](http://kafka.apache.org/).
+As a first step, lets update our function to respond to each input by sending a greeting to a Kafka topic.
 
-{% highlight java %}
-package org.apache.flink.statefun.examples.greeter;
+{% highlight python %}
+from messages_pb2 import GreetRequest, GreetResponse
+from statefun import StatefulFunctions
 
-import org.apache.flink.statefun.examples.kafka.generated.GreetRequest;
-import org.apache.flink.statefun.sdk.io.Router;
+functions = StatefulFunctions()
 
-final class GreetRouter implements Router<GreetRequest> {
+@functions.bind("example/greeter")
+def greet(context, message: GreetRequest):
+    response = GreetResponse()
+    response.name = message.name
+    response.greeting = "Hello {}".format(message.name)
+    
+    egress_message = kafka_egress_record(topic="greetings", key=message.name, value=response)
+    context.pack_and_send_egress("example/greets", egress_message)
+{% endhighlight %} 
 
-    @Override
-    public void route(GreetRequest message, Downstream<GreetRequest> downstream) {
-        downstream.forward(GreetingConstants.GREETER_FUNCTION_TYPE, message.getWho(), message);
-    }
-}
+For each message, a response is constructed and sent to a kafka topic call `greetings` partitioned by `name`.
+The `egress_message` is sent to a an `egress` named `example/greets`.
+This identifier points to a particular Kafka cluster and is configured on deployment below.
+
+## A Stateful Hello
+
+This is a great start, but does not show off the real power of stateful functions - working with state.
+Suppose you want to generate a personalized response for each user depending on how many times they have sent a request.
+
+{% highlight python %}
+def compute_greeting(name, seen):
+    """
+    Compute a personalized greeting, based on the number of times this @name had been seen before.
+    """
+    templates = ["", "Welcome %s", "Nice to see you again %s", "Third time is a charm %s"]
+    if seen < len(templates):
+        greeting = templates[seen] % name
+    else:
+        greeting = "Nice to see you at the %d-nth time %s!" % (seen, name)
+
+    response = GreetResponse()
+    response.name = name
+    response.greeting = greeting
+
+    return response
 {% endhighlight %}
 
-So, if a message for a user named John comes in, it will be shipped to John’s dedicated Greeter function.
-In case there is a following message for a user named Jane, a new instance of the Greeter function will be spawned.
+To “remember” information across multiple greeting messages, you then need to associate a persisted value field (``seen_count``) to the Greet function.
+For each user, functions can now track how many times they have been seen.
 
-## Persistence
+{% highlight python %}
+@functions.bind("example/greeter")
+def greet(context, message: GreetRequest):
+    state = context.state('seen_count').unpack(SeenCount)
+    if not state:
+        state = SeenCount()
+        state.seen = 1
+    else:
+        state.seen += 1
+    context.state('seen_count').pack(state)
 
-[Persisted value]({{ site.baseurl }}/sdk/java.html#persistence) is a special data type that enables stateful functions to maintain fault-tolerant state scoped to their identifiers, so that each instance of a function can track state independently.
-To “remember” information across multiple greeting messages, you then need to associate a persisted value field (``count``) to the Greet function. For each user, functions can now track how many times they have been seen.
+    response = compute_greeting(greet_request.name, state.seen)
 
-{% highlight java %}
-package org.apache.flink.statefun.examples.greeter;
+    egress_message = kafka_egress_record(topic="greetings", key=greet_request.name, value=response)
+    context.pack_and_send_egress("example/greets", egress_message)
+{% endhighlight %}
 
-import org.apache.flink.statefun.sdk.Context;
-import org.apache.flink.statefun.sdk.StatefulFunction;
-import org.apache.flink.statefun.sdk.annotations.Persisted;
-import org.apache.flink.statefun.sdk.state.PersistedValue;
+The state, `seen_count` is always scoped to the current name so it can track each user independently.
 
-public final class GreetFunction implements StatefulFunction {
+## Wiring It All Together
 
-    @Persisted
-    private final PersistedValue<Integer> count = PersistedValue.of("count", Integer.class);
+Stateful Function applications communicate with the Apache Flink runtime using `http`.
+The Python SDK ships with a ``RequestReplyHandler`` that automatically dispatches function calls based on RESTful HTTP ``POSTS``.
+The ``RequestReplyHandler`` may be exposed using any HTTP framework.
 
-    @Override
-    public void invoke(Context context, Object input) {
-        GreetRequest greetMessage = (GreetRequest) input;
+One popular Python web framework is [Flask](https://palletsprojects.com/p/flask/).
+It can be used to quickly and easily expose an application to the Apache Flink runtime.
 
-        GreetResponse response = computePersonalizedGreeting(greetMessage);
+{% highlight python %}
+from statefun import StatefulFunctions
+from statefun import RequestReplyHandler
 
-        context.send(GreetingConstants.GREETING_EGRESS_ID, response);
-    }
+functions = StatefulFunctions()
 
-    private GreetResponse computePersonalizedGreeting(GreetRequest greetMessage) {
-        final String name = greetMessage.getWho();
-        final int seen = count.getOrDefault(0);
-        count.set(seen + 1);
+@functions.bind("walkthrough/greeter")
+def greeter(context, message: GreetRequest):
+    pass
 
-        String greeting = greetText(name, seen);
+handler = RequestReplyHandler(functions)
 
-        return GreetResponse.newBuilder()
-            .setWho(name)
-            .setGreeting(greeting)
-            .build();
-    }
-}
+# Serve the endpoint
+
+from flask import request
+from flask import make_response
+from flask import Flask
+
+app = Flask(__name__)
+
+@app.route('/statefun', methods=['POST'])
+def handle():
+    response_data = handler(request.data)
+    response = make_response(response_data)
+    response.headers.set('Content-Type', 'application/octet-stream')
+    return response
+
+
+if __name__ == "__main__":
+    app.run()
 {% endhighlight %}
 
-Each time a message is processed, the function computes a personalized message for that user.
-It reads and updates the number of times that user has been seen and sends a greeting to the egress.
+## Configuring for Runtime
+
+The Apache Flink runtime makes requests to the greeter function by making `http` calls to the `Flask` server.
 
 Review comment:
   I'll find a way to rephrase it, you're right, you don't need to understand Flink for this getting started.
   
   That said, I don't think you could go to production without understanding the project's relationship to Flink. We may want to follow up with a dedicated page explaining how these projects are related. 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [flink-statefun] rmetzger commented on issue #77: [FLINK-16730][docs] Python SDK Getting Started

Posted by GitBox <gi...@apache.org>.
rmetzger commented on issue #77: [FLINK-16730][docs] Python SDK Getting Started
URL: https://github.com/apache/flink-statefun/pull/77#issuecomment-605093833
 
 
   I think it would be very nice to have this for Java as well!

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [flink-statefun] rmetzger merged pull request #77: [FLINK-16730][docs] Python SDK Getting Started

Posted by GitBox <gi...@apache.org>.
rmetzger merged pull request #77: [FLINK-16730][docs] Python SDK Getting Started
URL: https://github.com/apache/flink-statefun/pull/77
 
 
   

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [flink-statefun] sjwiesman commented on issue #77: [FLINK-16730][docs] Python SDK Getting Started

Posted by GitBox <gi...@apache.org>.
sjwiesman commented on issue #77: [FLINK-16730][docs] Python SDK Getting Started
URL: https://github.com/apache/flink-statefun/pull/77#issuecomment-605302811
 
 
   Thanks for taking the time to review @rmetzger. I just squashed my commits and am happy for you to commit. 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [flink-statefun] rmetzger commented on a change in pull request #77: [FLINK-16730][docs] Python SDK Getting Started

Posted by GitBox <gi...@apache.org>.
rmetzger commented on a change in pull request #77: [FLINK-16730][docs] Python SDK Getting Started
URL: https://github.com/apache/flink-statefun/pull/77#discussion_r399384026
 
 

 ##########
 File path: docs/getting-started/walkthrough.md
 ##########
 @@ -24,177 +24,411 @@ specific language governing permissions and limitations
 under the License.
 -->
 
+Stateful Functions offers a platform for building robust, stateful event-driven applications.
+It provides fine-grained control over state and time, which allows for the implementation of advanced systems.
+In this step-by-step guide you’ll learn how to build a stateful applications with the Stateful Functions API.
+
+* This will be replaced by the TOC
+{:toc}
+
+## What Are You Building?
+
 Like all great introductions in software, this walkthrough will start at the beginning: saying hello.
 The application will run a simple function that accepts a request and responds with a greeting.
 It will not attempt to cover all the complexities of application development, but instead focus on building a stateful function — which is where you will implement your business logic.
 
-* This will be replaced by the TOC
-{:toc}
+## Prerequisites
+
+This walkthrough assumes that you have some familiarity with Python, but you should be able to follow along even if you are coming from a different programming language.
+
+## Help, I’m Stuck! 
+
+If you get stuck, check out the [community support resources](https://flink.apache.org/gettinghelp.html).
+In particular, Apache Flink's [user mailing list](https://flink.apache.org/community.html#mailing-lists) is consistently ranked as one of the most active of any Apache project and a great way to get help quickly.
 
-## A Basic Hello
+## How to Follow Along
+
+If you want to follow along, you will require a computer with [Python 3](https://www.python.org/) along with [Docker](https://www.docker.com/).
+
+{% panel **Note:** Each code block within this walkthrough may not contain the full surrounding class for brevity.
+The full code is available on [at the bottom of this page](#full-application). %}
+
+You can download a zip file with a skeleton project by clicking [here]({{ site.baseurl }}/downloads/walkthrough.zip).
+
+{% unless site.is_stable %}
+<p style="border-radius: 5px; padding: 5px" class="bg-danger">
+    <b>Note</b>: The Stateful Functions project does not publish snapshot versions of the Python SDK to PyPy.
+    Please consider using a stable version of this guide.
+</p>
+{% endunless %}
+
+After unzipping the package, you will find a number of files.
+These include dockerfiles and data generators to run this walkthrough in a local self contained environment.
+
+{% highlight bash %}
+$ tree statefun-walkthrough
+statefun-walkthrough
+├── Dockerfile
+├── README.md
+├── docker-compose.yml
+├── generator
+│   ├── Dockerfile
+│   ├── event-generator.py
+│   └── messages_pb2.py
+├── greeter
+│   ├── Dockerfile
+│   ├── greeter.py
+│   ├── messages.proto
+│   ├── messages_pb2.py
+│   └── requirements.txt
+└── module.yaml
+{% endhighlight %}
 
-Greeting actions are triggered by consuming, routing and passing messages that are defined using ProtoBuf.
+## Start With Events
+
+Stateful Functions is an event driven system, so development begins by defining our events.
+The greeter application will define its events using [protocol buffers](https://developers.google.com/protocol-buffers). 
+When a greet request for a particular user is ingested, it will be routed to the appropriate function.
+The response will be returned with an appropriate greeting.
+The third type, `SeenCount`, is a utility class that will be used latter on to help manage the number of times a user has been seen so far.
 
 {% highlight proto %}
 syntax = "proto3";
 
+package example;
+
+// External request sent by a user who wants to be greeted
 message GreetRequest {
-    string who = 1;
+    // The name of the user to greet
+    string name = 1;
 }
-
+// A customized response sent to the user
 message GreetResponse {
-    string who = 1;
+    // The name of the user being greeted
+    string name = 1;
+    // The users customized greeting
     string greeting = 2;
 }
+// An internal message used to store state
+message SeenCount {
+    // The number of times a users has been seen so far
+    int64 seen = 1;
+}
 {% endhighlight %}
 
-Under the hood, messages are processed using [stateful functions]({{ site.baseurl }}/sdk/java.html), by definition any class that implements the ``StatefulFunction`` interface.
 
-{% highlight java %}
-package org.apache.flink.statefun.examples.greeter;
+## Our First Function
 
-import org.apache.flink.statefun.sdk.Context;
-import org.apache.flink.statefun.sdk.StatefulFunction;
+Under the hood, messages are processed using [stateful functions]({{ site.baseurl }}/sdk/python.html), which is any two argument function that is bound to the ``StatefulFunction`` runtime.
+Functions are bound to the runtime with the `@function.bind` decorator.
+When binding a function, it is annotated with a function type.
+This is the name used to reference this function when sending it messages.
 
-public final class GreetFunction implements StatefulFunction {
+When you open the file `greeter/greeter.py` you should see the following code.
 
-    @Override
-    public void invoke(Context context, Object input) {
-        GreetRequest greetMessage = (GreetRequest) input;
+{% highlight python %}
+from statefun import StatefulFunctions
 
-        GreetResponse response = GreetResponse.newBuilder()
-            .setWho(greetMessage.getWho())
-            .setGreeting("Hello " + greetMessage.getWho())
-            .build();
+functions = StatefulFunctions()
 
-        context.send(GreetingConstants.GREETING_EGRESS_ID, response);
-    }
-}
+@functions.bind("example/greeter")
+def greet(context, greet_request):
+    pass
 {% endhighlight %}
 
+A stateful function takes two arguments, a context and message. 
+The [context]({{ site.baseurl }}/sdk/python.html#context-reference) provides access to stateful functions runtime features such as state management and message passing.
+You will explore some of these features as you progress through this walkthrough. 
 
-This function takes in a request and sends a response to an external system (or [egress]({{ site.baseurl }}/io-module/index.html#egress)).
-While this is nice, it does not show off the real power of stateful functions: handling state.
+The other parameter is the input message that has been passed to this function.
+By default messages are passed around as protobuf [Any](https://developers.google.com/protocol-buffers/docs/reference/python-generated#wkt).
+If a function only accepts a known type, you can override the message type using Python 3 type syntax.
+This way you do not need to unwrap the message or check types.
 
-## A Stateful Hello
+{% highlight python %}
+from messages_pb2 import GreetRequest
+from statefun import StatefulFunctions
 
-Suppose you want to generate a personalized response for each user depending on how many times they have sent a request.
+functions = StatefulFunctions()
 
-{% highlight java %}
-private static String greetText(String name, int seen) {
-    switch (seen) {
-        case 0:
-            return String.format("Hello %s !", name);
-        case 1:
-            return String.format("Hello again %s !", name);
-        case 2:
-            return String.format("Third times the charm! %s!", name);
-        case 3:
-            return String.format("Happy to see you once again %s !", name);
-        default:
-            return String.format("Hello at the %d-th time %s", seen + 1, name);
-}
+@functions.bind("example/greeter")
+def greet(context, greet_request: GreetRequest):
+    pass
 {% endhighlight %}
 
-## Routing Messages
+## Sending A Response
 
-To send a user a personalized greeting, the system needs to keep track of how many times it has seen each user so far.
-Speaking in general terms, the simplest solution would be to create one function for every user and independently track the number of times they have been seen. Using most frameworks, this would be prohibitively expensive.
-However, stateful functions are virtual and do not consume any CPU or memory when not actively being invoked.
-That means your application can create as many functions as necessary — in this case, users — without worrying about resource consumption.
+Stateful Functions accept messages and can also send them out.
+Messages can be sent to other functions, as well as external systems (or [egress]({{ site.baseurl }}/io-module/index.html#egress)).
 
-Whenever data is consumed from an external system (or [ingress]({{ site.baseurl }}/io-module/index.html#ingress)), it is routed to a specific function based on a given function type and identifier.
-The function type represents the Class of function to be invoked, such as the Greeter function, while the identifier (``GreetRequest#getWho``) scopes the call to a specific virtual instance based on some key.
+One popular external system is [Apache Kafka](http://kafka.apache.org/).
+As a first step, lets update our function to respond to each input by sending a greeting to a Kafka topic.
 
-{% highlight java %}
-package org.apache.flink.statefun.examples.greeter;
+{% highlight python %}
+from messages_pb2 import GreetRequest, GreetResponse
+from statefun import StatefulFunctions
 
-import org.apache.flink.statefun.examples.kafka.generated.GreetRequest;
-import org.apache.flink.statefun.sdk.io.Router;
+functions = StatefulFunctions()
 
-final class GreetRouter implements Router<GreetRequest> {
+@functions.bind("example/greeter")
+def greet(context, message: GreetRequest):
+    response = GreetResponse()
+    response.name = message.name
+    response.greeting = "Hello {}".format(message.name)
+    
+    egress_message = kafka_egress_record(topic="greetings", key=message.name, value=response)
+    context.pack_and_send_egress("example/greets", egress_message)
+{% endhighlight %} 
 
-    @Override
-    public void route(GreetRequest message, Downstream<GreetRequest> downstream) {
-        downstream.forward(GreetingConstants.GREETER_FUNCTION_TYPE, message.getWho(), message);
-    }
-}
+For each message, a response is constructed and sent to a kafka topic call `greetings` partitioned by `name`.
+The `egress_message` is sent to a an `egress` named `example/greets`.
+This identifier points to a particular Kafka cluster and is configured on deployment below.
+
+## A Stateful Hello
+
+This is a great start, but does not show off the real power of stateful functions - working with state.
+Suppose you want to generate a personalized response for each user depending on how many times they have sent a request.
+
+{% highlight python %}
+def compute_greeting(name, seen):
+    """
+    Compute a personalized greeting, based on the number of times this @name had been seen before.
+    """
+    templates = ["", "Welcome %s", "Nice to see you again %s", "Third time is a charm %s"]
+    if seen < len(templates):
+        greeting = templates[seen] % name
+    else:
+        greeting = "Nice to see you at the %d-nth time %s!" % (seen, name)
+
+    response = GreetResponse()
+    response.name = name
+    response.greeting = greeting
+
+    return response
 {% endhighlight %}
 
-So, if a message for a user named John comes in, it will be shipped to John’s dedicated Greeter function.
-In case there is a following message for a user named Jane, a new instance of the Greeter function will be spawned.
+To “remember” information across multiple greeting messages, you then need to associate a persisted value field (``seen_count``) to the Greet function.
+For each user, functions can now track how many times they have been seen.
 
-## Persistence
+{% highlight python %}
+@functions.bind("example/greeter")
+def greet(context, message: GreetRequest):
+    state = context.state('seen_count').unpack(SeenCount)
+    if not state:
+        state = SeenCount()
+        state.seen = 1
+    else:
+        state.seen += 1
+    context.state('seen_count').pack(state)
 
-[Persisted value]({{ site.baseurl }}/sdk/java.html#persistence) is a special data type that enables stateful functions to maintain fault-tolerant state scoped to their identifiers, so that each instance of a function can track state independently.
-To “remember” information across multiple greeting messages, you then need to associate a persisted value field (``count``) to the Greet function. For each user, functions can now track how many times they have been seen.
+    response = compute_greeting(greet_request.name, state.seen)
 
-{% highlight java %}
-package org.apache.flink.statefun.examples.greeter;
+    egress_message = kafka_egress_record(topic="greetings", key=greet_request.name, value=response)
+    context.pack_and_send_egress("example/greets", egress_message)
+{% endhighlight %}
 
-import org.apache.flink.statefun.sdk.Context;
-import org.apache.flink.statefun.sdk.StatefulFunction;
-import org.apache.flink.statefun.sdk.annotations.Persisted;
-import org.apache.flink.statefun.sdk.state.PersistedValue;
+The state, `seen_count` is always scoped to the current name so it can track each user independently.
 
-public final class GreetFunction implements StatefulFunction {
+## Wiring It All Together
 
-    @Persisted
-    private final PersistedValue<Integer> count = PersistedValue.of("count", Integer.class);
+Stateful Function applications communicate with the Apache Flink runtime using `http`.
+The Python SDK ships with a ``RequestReplyHandler`` that automatically dispatches function calls based on RESTful HTTP ``POSTS``.
+The ``RequestReplyHandler`` may be exposed using any HTTP framework.
 
-    @Override
-    public void invoke(Context context, Object input) {
-        GreetRequest greetMessage = (GreetRequest) input;
+One popular Python web framework is [Flask](https://palletsprojects.com/p/flask/).
+It can be used to quickly and easily expose an application to the Apache Flink runtime.
 
-        GreetResponse response = computePersonalizedGreeting(greetMessage);
+{% highlight python %}
+from statefun import StatefulFunctions
+from statefun import RequestReplyHandler
 
-        context.send(GreetingConstants.GREETING_EGRESS_ID, response);
-    }
+functions = StatefulFunctions()
 
-    private GreetResponse computePersonalizedGreeting(GreetRequest greetMessage) {
-        final String name = greetMessage.getWho();
-        final int seen = count.getOrDefault(0);
-        count.set(seen + 1);
+@functions.bind("walkthrough/greeter")
+def greeter(context, message: GreetRequest):
+    pass
 
-        String greeting = greetText(name, seen);
+handler = RequestReplyHandler(functions)
 
-        return GreetResponse.newBuilder()
-            .setWho(name)
-            .setGreeting(greeting)
-            .build();
-    }
-}
+# Serve the endpoint
+
+from flask import request
+from flask import make_response
+from flask import Flask
+
+app = Flask(__name__)
+
+@app.route('/statefun', methods=['POST'])
+def handle():
+    response_data = handler(request.data)
+    response = make_response(response_data)
+    response.headers.set('Content-Type', 'application/octet-stream')
+    return response
+
+
+if __name__ == "__main__":
+    app.run()
 {% endhighlight %}
 
-Each time a message is processed, the function computes a personalized message for that user.
-It reads and updates the number of times that user has been seen and sends a greeting to the egress.
+## Configuring for Runtime
+
+The Apache Flink runtime makes requests to the greeter function by making `http` calls to the `Flask` server.
 
 Review comment:
   Just a thought: Not sure how we are positioning StateFun, but this sounds like something coming directly from Flink?
   Maybe we could somehow phrase this to come from the "server side" of statefun running in Flink?
   
   If we are doing a good job, we might have users who are not even really aware that this is powered by Flink.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [flink-statefun] rmetzger commented on a change in pull request #77: [FLINK-16730][docs] Python SDK Getting Started

Posted by GitBox <gi...@apache.org>.
rmetzger commented on a change in pull request #77: [FLINK-16730][docs] Python SDK Getting Started
URL: https://github.com/apache/flink-statefun/pull/77#discussion_r399174638
 
 

 ##########
 File path: docs/getting-started/walkthrough.md
 ##########
 @@ -24,177 +24,411 @@ specific language governing permissions and limitations
 under the License.
 -->
 
+Stateful Functions offers a platform for building robust, stateful event-driven applications.
+It provides fine-grained control over state and time, which allows for the implementation of advanced systems.
+In this step-by-step guide you’ll learn how to build a stateful applications with the Stateful Functions API.
+
+* This will be replaced by the TOC
+{:toc}
+
+## What Are You Building?
+
 Like all great introductions in software, this walkthrough will start at the beginning: saying hello.
 The application will run a simple function that accepts a request and responds with a greeting.
 It will not attempt to cover all the complexities of application development, but instead focus on building a stateful function — which is where you will implement your business logic.
 
-* This will be replaced by the TOC
-{:toc}
+## Prerequisites
+
+This walkthrough assumes that you have some familiarity with Python, but you should be able to follow along even if you are coming from a different programming language.
+
+## Help, I’m Stuck! 
+
+If you get stuck, check out the [community support resources](https://flink.apache.org/gettinghelp.html).
+In particular, Apache Flink's [user mailing list](https://flink.apache.org/community.html#mailing-lists) is consistently ranked as one of the most active of any Apache project and a great way to get help quickly.
 
-## A Basic Hello
+## How to Follow Along
+
+If you want to follow along, you will require a computer with [Python 3](https://www.python.org/) along with [Docker](https://www.docker.com/).
+
+{% panel **Note:** Each code block within this walkthrough may not contain the full surrounding class for brevity.
+The full code is available on [at the bottom of this page](#full-application). %}
+
+You can download a zip file with a skeleton project by clicking [here]({{ site.baseurl }}/downloads/walkthrough.zip).
+
+{% unless site.is_stable %}
+<p style="border-radius: 5px; padding: 5px" class="bg-danger">
+    <b>Note</b>: The Stateful Functions project does not publish snapshot versions of the Python SDK to PyPy.
+    Please consider using a stable version of this guide.
+</p>
+{% endunless %}
+
+After unzipping the package, you will find a number of files.
+These include dockerfiles and data generators to run this walkthrough in a local self contained environment.
+
+{% highlight bash %}
+$ tree statefun-walkthrough
+statefun-walkthrough
+├── Dockerfile
+├── README.md
+├── docker-compose.yml
+├── generator
+│   ├── Dockerfile
+│   ├── event-generator.py
+│   └── messages_pb2.py
+├── greeter
+│   ├── Dockerfile
+│   ├── greeter.py
+│   ├── messages.proto
+│   ├── messages_pb2.py
+│   └── requirements.txt
+└── module.yaml
+{% endhighlight %}
 
-Greeting actions are triggered by consuming, routing and passing messages that are defined using ProtoBuf.
+## Start With Events
+
+Stateful Functions is an event driven system, so development begins by defining our events.
+The greeter application will define its events using [protocol buffers](https://developers.google.com/protocol-buffers). 
+When a greet request for a particular user is ingested, it will be routed to the appropriate function.
+The response will be returned with an appropriate greeting.
+The third type, `SeenCount`, is a utility class that will be used latter on to help manage the number of times a user has been seen so far.
 
 {% highlight proto %}
 syntax = "proto3";
 
+package example;
+
+// External request sent by a user who wants to be greeted
 message GreetRequest {
-    string who = 1;
+    // The name of the user to greet
+    string name = 1;
 }
-
+// A customized response sent to the user
 message GreetResponse {
-    string who = 1;
+    // The name of the user being greeted
+    string name = 1;
+    // The users customized greeting
     string greeting = 2;
 }
+// An internal message used to store state
+message SeenCount {
+    // The number of times a users has been seen so far
+    int64 seen = 1;
+}
 {% endhighlight %}
 
-Under the hood, messages are processed using [stateful functions]({{ site.baseurl }}/sdk/java.html), by definition any class that implements the ``StatefulFunction`` interface.
 
-{% highlight java %}
-package org.apache.flink.statefun.examples.greeter;
+## Our First Function
 
-import org.apache.flink.statefun.sdk.Context;
-import org.apache.flink.statefun.sdk.StatefulFunction;
+Under the hood, messages are processed using [stateful functions]({{ site.baseurl }}/sdk/python.html), which is any two argument function that is bound to the ``StatefulFunction`` runtime.
+Functions are bound to the runtime with the `@function.bind` decorator.
+When binding a function, it is annotated with a function type.
+This is the name used to reference this function when sending it messages.
 
-public final class GreetFunction implements StatefulFunction {
+When you open the file `greeter/greeter.py` you should see the following code.
 
-    @Override
-    public void invoke(Context context, Object input) {
-        GreetRequest greetMessage = (GreetRequest) input;
+{% highlight python %}
+from statefun import StatefulFunctions
 
-        GreetResponse response = GreetResponse.newBuilder()
-            .setWho(greetMessage.getWho())
-            .setGreeting("Hello " + greetMessage.getWho())
-            .build();
+functions = StatefulFunctions()
 
-        context.send(GreetingConstants.GREETING_EGRESS_ID, response);
-    }
-}
+@functions.bind("example/greeter")
+def greet(context, greet_request):
+    pass
 {% endhighlight %}
 
+A stateful function takes two arguments, a context and message. 
+The [context]({{ site.baseurl }}/sdk/python.html#context-reference) provides access to stateful functions runtime features such as state management and message passing.
+You will explore some of these features as you progress through this walkthrough. 
 
-This function takes in a request and sends a response to an external system (or [egress]({{ site.baseurl }}/io-module/index.html#egress)).
-While this is nice, it does not show off the real power of stateful functions: handling state.
+The other parameter is the input message that has been passed to this function.
+By default messages are passed around as protobuf [Any](https://developers.google.com/protocol-buffers/docs/reference/python-generated#wkt).
+If a function only accepts a known type, you can override the message type using Python 3 type syntax.
+This way you do not need to unwrap the message or check types.
 
-## A Stateful Hello
+{% highlight python %}
+from messages_pb2 import GreetRequest
+from statefun import StatefulFunctions
 
-Suppose you want to generate a personalized response for each user depending on how many times they have sent a request.
+functions = StatefulFunctions()
 
-{% highlight java %}
-private static String greetText(String name, int seen) {
-    switch (seen) {
-        case 0:
-            return String.format("Hello %s !", name);
-        case 1:
-            return String.format("Hello again %s !", name);
-        case 2:
-            return String.format("Third times the charm! %s!", name);
-        case 3:
-            return String.format("Happy to see you once again %s !", name);
-        default:
-            return String.format("Hello at the %d-th time %s", seen + 1, name);
-}
+@functions.bind("example/greeter")
+def greet(context, greet_request: GreetRequest):
+    pass
 {% endhighlight %}
 
-## Routing Messages
+## Sending A Response
 
-To send a user a personalized greeting, the system needs to keep track of how many times it has seen each user so far.
-Speaking in general terms, the simplest solution would be to create one function for every user and independently track the number of times they have been seen. Using most frameworks, this would be prohibitively expensive.
-However, stateful functions are virtual and do not consume any CPU or memory when not actively being invoked.
-That means your application can create as many functions as necessary — in this case, users — without worrying about resource consumption.
+Stateful Functions accept messages and can also send them out.
+Messages can be sent to other functions, as well as external systems (or [egress]({{ site.baseurl }}/io-module/index.html#egress)).
 
-Whenever data is consumed from an external system (or [ingress]({{ site.baseurl }}/io-module/index.html#ingress)), it is routed to a specific function based on a given function type and identifier.
-The function type represents the Class of function to be invoked, such as the Greeter function, while the identifier (``GreetRequest#getWho``) scopes the call to a specific virtual instance based on some key.
+One popular external system is [Apache Kafka](http://kafka.apache.org/).
+As a first step, lets update our function to respond to each input by sending a greeting to a Kafka topic.
 
 Review comment:
   ```suggestion
   As a first step, lets update our function in `greeter/greeter.py` to respond to each input by sending a greeting to a Kafka topic.
   ```

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [flink-statefun] rmetzger commented on a change in pull request #77: [FLINK-16730][docs] Python SDK Getting Started

Posted by GitBox <gi...@apache.org>.
rmetzger commented on a change in pull request #77: [FLINK-16730][docs] Python SDK Getting Started
URL: https://github.com/apache/flink-statefun/pull/77#discussion_r399386235
 
 

 ##########
 File path: docs/getting-started/walkthrough.md
 ##########
 @@ -24,177 +24,411 @@ specific language governing permissions and limitations
 under the License.
 -->
 
+Stateful Functions offers a platform for building robust, stateful event-driven applications.
+It provides fine-grained control over state and time, which allows for the implementation of advanced systems.
+In this step-by-step guide you’ll learn how to build a stateful applications with the Stateful Functions API.
+
+* This will be replaced by the TOC
+{:toc}
+
+## What Are You Building?
+
 Like all great introductions in software, this walkthrough will start at the beginning: saying hello.
 The application will run a simple function that accepts a request and responds with a greeting.
 It will not attempt to cover all the complexities of application development, but instead focus on building a stateful function — which is where you will implement your business logic.
 
-* This will be replaced by the TOC
-{:toc}
+## Prerequisites
+
+This walkthrough assumes that you have some familiarity with Python, but you should be able to follow along even if you are coming from a different programming language.
+
+## Help, I’m Stuck! 
+
+If you get stuck, check out the [community support resources](https://flink.apache.org/gettinghelp.html).
+In particular, Apache Flink's [user mailing list](https://flink.apache.org/community.html#mailing-lists) is consistently ranked as one of the most active of any Apache project and a great way to get help quickly.
 
-## A Basic Hello
+## How to Follow Along
+
+If you want to follow along, you will require a computer with [Python 3](https://www.python.org/) along with [Docker](https://www.docker.com/).
+
+{% panel **Note:** Each code block within this walkthrough may not contain the full surrounding class for brevity.
+The full code is available on [at the bottom of this page](#full-application). %}
+
+You can download a zip file with a skeleton project by clicking [here]({{ site.baseurl }}/downloads/walkthrough.zip).
+
+{% unless site.is_stable %}
+<p style="border-radius: 5px; padding: 5px" class="bg-danger">
+    <b>Note</b>: The Stateful Functions project does not publish snapshot versions of the Python SDK to PyPy.
+    Please consider using a stable version of this guide.
+</p>
+{% endunless %}
+
+After unzipping the package, you will find a number of files.
+These include dockerfiles and data generators to run this walkthrough in a local self contained environment.
+
+{% highlight bash %}
+$ tree statefun-walkthrough
+statefun-walkthrough
+├── Dockerfile
+├── README.md
+├── docker-compose.yml
+├── generator
+│   ├── Dockerfile
+│   ├── event-generator.py
+│   └── messages_pb2.py
+├── greeter
+│   ├── Dockerfile
+│   ├── greeter.py
+│   ├── messages.proto
+│   ├── messages_pb2.py
+│   └── requirements.txt
+└── module.yaml
+{% endhighlight %}
 
-Greeting actions are triggered by consuming, routing and passing messages that are defined using ProtoBuf.
+## Start With Events
+
+Stateful Functions is an event driven system, so development begins by defining our events.
+The greeter application will define its events using [protocol buffers](https://developers.google.com/protocol-buffers). 
+When a greet request for a particular user is ingested, it will be routed to the appropriate function.
+The response will be returned with an appropriate greeting.
+The third type, `SeenCount`, is a utility class that will be used latter on to help manage the number of times a user has been seen so far.
 
 {% highlight proto %}
 syntax = "proto3";
 
+package example;
+
+// External request sent by a user who wants to be greeted
 message GreetRequest {
-    string who = 1;
+    // The name of the user to greet
+    string name = 1;
 }
-
+// A customized response sent to the user
 message GreetResponse {
-    string who = 1;
+    // The name of the user being greeted
+    string name = 1;
+    // The users customized greeting
     string greeting = 2;
 }
+// An internal message used to store state
+message SeenCount {
+    // The number of times a users has been seen so far
+    int64 seen = 1;
+}
 {% endhighlight %}
 
-Under the hood, messages are processed using [stateful functions]({{ site.baseurl }}/sdk/java.html), by definition any class that implements the ``StatefulFunction`` interface.
 
-{% highlight java %}
-package org.apache.flink.statefun.examples.greeter;
+## Our First Function
 
-import org.apache.flink.statefun.sdk.Context;
-import org.apache.flink.statefun.sdk.StatefulFunction;
+Under the hood, messages are processed using [stateful functions]({{ site.baseurl }}/sdk/python.html), which is any two argument function that is bound to the ``StatefulFunction`` runtime.
+Functions are bound to the runtime with the `@function.bind` decorator.
+When binding a function, it is annotated with a function type.
+This is the name used to reference this function when sending it messages.
 
-public final class GreetFunction implements StatefulFunction {
+When you open the file `greeter/greeter.py` you should see the following code.
 
-    @Override
-    public void invoke(Context context, Object input) {
-        GreetRequest greetMessage = (GreetRequest) input;
+{% highlight python %}
+from statefun import StatefulFunctions
 
-        GreetResponse response = GreetResponse.newBuilder()
-            .setWho(greetMessage.getWho())
-            .setGreeting("Hello " + greetMessage.getWho())
-            .build();
+functions = StatefulFunctions()
 
-        context.send(GreetingConstants.GREETING_EGRESS_ID, response);
-    }
-}
+@functions.bind("example/greeter")
+def greet(context, greet_request):
+    pass
 {% endhighlight %}
 
+A stateful function takes two arguments, a context and message. 
+The [context]({{ site.baseurl }}/sdk/python.html#context-reference) provides access to stateful functions runtime features such as state management and message passing.
+You will explore some of these features as you progress through this walkthrough. 
 
-This function takes in a request and sends a response to an external system (or [egress]({{ site.baseurl }}/io-module/index.html#egress)).
-While this is nice, it does not show off the real power of stateful functions: handling state.
+The other parameter is the input message that has been passed to this function.
+By default messages are passed around as protobuf [Any](https://developers.google.com/protocol-buffers/docs/reference/python-generated#wkt).
+If a function only accepts a known type, you can override the message type using Python 3 type syntax.
+This way you do not need to unwrap the message or check types.
 
-## A Stateful Hello
+{% highlight python %}
+from messages_pb2 import GreetRequest
+from statefun import StatefulFunctions
 
-Suppose you want to generate a personalized response for each user depending on how many times they have sent a request.
+functions = StatefulFunctions()
 
-{% highlight java %}
-private static String greetText(String name, int seen) {
-    switch (seen) {
-        case 0:
-            return String.format("Hello %s !", name);
-        case 1:
-            return String.format("Hello again %s !", name);
-        case 2:
-            return String.format("Third times the charm! %s!", name);
-        case 3:
-            return String.format("Happy to see you once again %s !", name);
-        default:
-            return String.format("Hello at the %d-th time %s", seen + 1, name);
-}
+@functions.bind("example/greeter")
+def greet(context, greet_request: GreetRequest):
+    pass
 {% endhighlight %}
 
-## Routing Messages
+## Sending A Response
 
-To send a user a personalized greeting, the system needs to keep track of how many times it has seen each user so far.
-Speaking in general terms, the simplest solution would be to create one function for every user and independently track the number of times they have been seen. Using most frameworks, this would be prohibitively expensive.
-However, stateful functions are virtual and do not consume any CPU or memory when not actively being invoked.
-That means your application can create as many functions as necessary — in this case, users — without worrying about resource consumption.
+Stateful Functions accept messages and can also send them out.
+Messages can be sent to other functions, as well as external systems (or [egress]({{ site.baseurl }}/io-module/index.html#egress)).
 
-Whenever data is consumed from an external system (or [ingress]({{ site.baseurl }}/io-module/index.html#ingress)), it is routed to a specific function based on a given function type and identifier.
-The function type represents the Class of function to be invoked, such as the Greeter function, while the identifier (``GreetRequest#getWho``) scopes the call to a specific virtual instance based on some key.
+One popular external system is [Apache Kafka](http://kafka.apache.org/).
+As a first step, lets update our function to respond to each input by sending a greeting to a Kafka topic.
 
-{% highlight java %}
-package org.apache.flink.statefun.examples.greeter;
+{% highlight python %}
+from messages_pb2 import GreetRequest, GreetResponse
+from statefun import StatefulFunctions
 
-import org.apache.flink.statefun.examples.kafka.generated.GreetRequest;
-import org.apache.flink.statefun.sdk.io.Router;
+functions = StatefulFunctions()
 
-final class GreetRouter implements Router<GreetRequest> {
+@functions.bind("example/greeter")
+def greet(context, message: GreetRequest):
+    response = GreetResponse()
+    response.name = message.name
+    response.greeting = "Hello {}".format(message.name)
+    
+    egress_message = kafka_egress_record(topic="greetings", key=message.name, value=response)
+    context.pack_and_send_egress("example/greets", egress_message)
+{% endhighlight %} 
 
-    @Override
-    public void route(GreetRequest message, Downstream<GreetRequest> downstream) {
-        downstream.forward(GreetingConstants.GREETER_FUNCTION_TYPE, message.getWho(), message);
-    }
-}
+For each message, a response is constructed and sent to a kafka topic call `greetings` partitioned by `name`.
+The `egress_message` is sent to a an `egress` named `example/greets`.
+This identifier points to a particular Kafka cluster and is configured on deployment below.
+
+## A Stateful Hello
+
+This is a great start, but does not show off the real power of stateful functions - working with state.
+Suppose you want to generate a personalized response for each user depending on how many times they have sent a request.
+
+{% highlight python %}
+def compute_greeting(name, seen):
+    """
+    Compute a personalized greeting, based on the number of times this @name had been seen before.
+    """
+    templates = ["", "Welcome %s", "Nice to see you again %s", "Third time is a charm %s"]
+    if seen < len(templates):
+        greeting = templates[seen] % name
+    else:
+        greeting = "Nice to see you at the %d-nth time %s!" % (seen, name)
+
+    response = GreetResponse()
+    response.name = name
+    response.greeting = greeting
+
+    return response
 {% endhighlight %}
 
-So, if a message for a user named John comes in, it will be shipped to John’s dedicated Greeter function.
-In case there is a following message for a user named Jane, a new instance of the Greeter function will be spawned.
+To “remember” information across multiple greeting messages, you then need to associate a persisted value field (``seen_count``) to the Greet function.
+For each user, functions can now track how many times they have been seen.
 
-## Persistence
+{% highlight python %}
+@functions.bind("example/greeter")
+def greet(context, message: GreetRequest):
+    state = context.state('seen_count').unpack(SeenCount)
+    if not state:
+        state = SeenCount()
+        state.seen = 1
+    else:
+        state.seen += 1
+    context.state('seen_count').pack(state)
 
-[Persisted value]({{ site.baseurl }}/sdk/java.html#persistence) is a special data type that enables stateful functions to maintain fault-tolerant state scoped to their identifiers, so that each instance of a function can track state independently.
-To “remember” information across multiple greeting messages, you then need to associate a persisted value field (``count``) to the Greet function. For each user, functions can now track how many times they have been seen.
+    response = compute_greeting(greet_request.name, state.seen)
 
-{% highlight java %}
-package org.apache.flink.statefun.examples.greeter;
+    egress_message = kafka_egress_record(topic="greetings", key=greet_request.name, value=response)
+    context.pack_and_send_egress("example/greets", egress_message)
+{% endhighlight %}
 
-import org.apache.flink.statefun.sdk.Context;
-import org.apache.flink.statefun.sdk.StatefulFunction;
-import org.apache.flink.statefun.sdk.annotations.Persisted;
-import org.apache.flink.statefun.sdk.state.PersistedValue;
+The state, `seen_count` is always scoped to the current name so it can track each user independently.
 
-public final class GreetFunction implements StatefulFunction {
+## Wiring It All Together
 
-    @Persisted
-    private final PersistedValue<Integer> count = PersistedValue.of("count", Integer.class);
+Stateful Function applications communicate with the Apache Flink runtime using `http`.
+The Python SDK ships with a ``RequestReplyHandler`` that automatically dispatches function calls based on RESTful HTTP ``POSTS``.
+The ``RequestReplyHandler`` may be exposed using any HTTP framework.
 
-    @Override
-    public void invoke(Context context, Object input) {
-        GreetRequest greetMessage = (GreetRequest) input;
+One popular Python web framework is [Flask](https://palletsprojects.com/p/flask/).
+It can be used to quickly and easily expose an application to the Apache Flink runtime.
 
-        GreetResponse response = computePersonalizedGreeting(greetMessage);
+{% highlight python %}
+from statefun import StatefulFunctions
+from statefun import RequestReplyHandler
 
-        context.send(GreetingConstants.GREETING_EGRESS_ID, response);
-    }
+functions = StatefulFunctions()
 
-    private GreetResponse computePersonalizedGreeting(GreetRequest greetMessage) {
-        final String name = greetMessage.getWho();
-        final int seen = count.getOrDefault(0);
-        count.set(seen + 1);
+@functions.bind("walkthrough/greeter")
+def greeter(context, message: GreetRequest):
+    pass
 
-        String greeting = greetText(name, seen);
+handler = RequestReplyHandler(functions)
 
-        return GreetResponse.newBuilder()
-            .setWho(name)
-            .setGreeting(greeting)
-            .build();
-    }
-}
+# Serve the endpoint
+
+from flask import request
+from flask import make_response
+from flask import Flask
+
+app = Flask(__name__)
+
+@app.route('/statefun', methods=['POST'])
+def handle():
+    response_data = handler(request.data)
+    response = make_response(response_data)
+    response.headers.set('Content-Type', 'application/octet-stream')
+    return response
+
+
+if __name__ == "__main__":
+    app.run()
 {% endhighlight %}
 
-Each time a message is processed, the function computes a personalized message for that user.
-It reads and updates the number of times that user has been seen and sends a greeting to the egress.
+## Configuring for Runtime
+
+The Apache Flink runtime makes requests to the greeter function by making `http` calls to the `Flask` server.
+To do that, Flink needs to know what endpoint it can use to reach the server.
+This is also a good time to configure our connection to the input and output Kafka topics.
+The configuration is in a file called `module.yaml`.
+
+{% highlight yaml %}
+version: "1.0"
+module:
+  meta:
+    type: remote
+  spec:
+    functions:
+      - function:
+          meta:
+            kind: http
+            type: example/greeter
+          spec:
+            endpoint: http://python-worker:8000/statefun
+            states:
+              - seen_count
+            maxNumBatchRequests: 500
+            timeout: 2min
+    ingresses:
+      - ingress:
+          meta:
+            type: statefun.kafka.io/routable-protobuf-ingress
+            id: example/names
+          spec:
+            address: kafka-broker:9092
+            consumerGroupId: my-group-id
+            topics:
+              - topic: names
+                typeUrl: com.googleapis/example.GreetRequest
+                targets:
+                  - example/greeter
+    egresses:
+      - egress:
+          meta:
+            type: statefun.kafka.io/generic-egress
+            id: example/greets
+          spec:
+            address: kafka-broker:9092
+            deliverySemantic:
+              type: exactly-once
+              transactionTimeoutMillis: 100000
+{% endhighlight %}
 
-You can check the full code for the application described in this walkthrough [here]({{ site.github_url }}/tree/{{ site.github_branch }}/statefun-examples/statefun-greeter-example).
-In particular, take a look at the module ``GreetingModule``, which is the main entry point for the full application, to see how everything gets tied together.
-You can run this example locally using the provided Docker setup.
+This configuration does a few interesting things.
 
-{% highlight bash %}
-$ docker-compose build 
-$ docker-compose up
+The first is to declare our function, `example/greeter`.
+It includes the endpoint by which it is reachable along with the states the function has access to.
+
+The ingress is the input Kafka topic that routes `GreetRequest` messages to the function.
+Along with basic properties like broker address and consumer group, it contains a list of targets.
+These are the functions each message will be sent to.
+
+The egress is the output Kafka cluster.
+It contains broker specific configurations but allows each message to route to any topic.
+
+## Deployment
+
+Now that the greeter application has been built it is time to deploy. 
+The simplest way to deploy a Stateful Function application is by using the community provided base image and loading your module.
+The base image provides the Stateful Function runtime, it will use the provided `module.yaml` to configure for this specific job.
+This can be found in the docker file in the root directory. 
+
+{% highlight docker %}
+FROM statefun
+
+RUN mkdir -p /opt/statefun/modules/greeter
+ADD module.yaml /opt/statefun/modules/greeter
 {% endhighlight %}
 
-Then, send some messages to the topic "names", and observe what comes out of "greetings".
+You can now run this application locally using the provided Docker setup.
 
 {% highlight bash %}
-$ docker-compose exec kafka-broker kafka-console-producer.sh \
-    --broker-list localhost:9092 \
-    --topic names
+$ docker-compose up -d
 
 Review comment:
   Is this supposed to work?
   ```
   Building master
   Step 1/3 : FROM statefun
   ERROR: Service 'master' failed to build: pull access denied for statefun, repository does not exist or may require 'docker login': denied: requested access to the resource is denied
   ```

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [flink-statefun] tzulitai commented on issue #77: [FLINK-16730][docs] Python SDK Getting Started

Posted by GitBox <gi...@apache.org>.
tzulitai commented on issue #77: [FLINK-16730][docs] Python SDK Getting Started
URL: https://github.com/apache/flink-statefun/pull/77#issuecomment-605461026
 
 
   Backporting to `release-2.0` to be included in RC3

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services