Skip to content

Commit 44dc70f

Browse files
authored
Add FS Python SDK (#195)
Signed-off-by: Zike Yang <zike@apache.org>
1 parent 8cf5eaa commit 44dc70f

25 files changed

+1867
-0
lines changed

.github/workflows/ci.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,23 @@ jobs:
5353
- name: Collect nohup logs
5454
if: failure()
5555
run: cat nohup.out || true
56+
57+
test-python-sdk:
58+
runs-on: ubuntu-latest
59+
steps:
60+
- uses: actions/checkout@v3
61+
- uses: actions/setup-python@v4
62+
with:
63+
python-version: '3.11'
64+
- name: Install dependencies
65+
working-directory: sdks/fs-python
66+
run: |
67+
pip install .
68+
python -m pip install --upgrade pip
69+
pip install -r requirements.txt
70+
pip install pytest
71+
- name: Run Python SDK tests
72+
working-directory: sdks/fs-python
73+
run: |
74+
make test
75+

sdks/fs-python/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
venv
2+
*.egg-info
3+
**/__pycache__
4+
build

sdks/fs-python/Dockerfile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
FROM python:3.13-slim
2+
3+
WORKDIR /function
4+
5+
# Copy the SDK files
6+
COPY . /function/
7+
8+
# Install the SDK
9+
RUN pip install .
10+
11+
# Set the default command
12+
CMD ["python", "/function/function.py"]

sdks/fs-python/Makefile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.PHONY: build-image
2+
3+
build-image:
4+
docker build -t functionstream/fs-python-base .
5+
6+
test:
7+
pytest -v

sdks/fs-python/README.md

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
<!--
2+
Copyright 2024 Function Stream Org.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
-->
16+
17+
# FunctionStream Python SDK
18+
19+
FunctionStream SDK is a powerful Python library for building and deploying serverless functions that process messages from Apache Pulsar. It provides a simple yet flexible framework for creating event-driven applications with robust error handling, metrics collection, and resource management.
20+
21+
## Features
22+
23+
- **Easy Function Development**: Simple API for creating serverless functions
24+
- **Message Processing**: Built-in support for Apache Pulsar message processing
25+
- **Metrics Collection**: Automatic collection of performance metrics
26+
- **Resource Management**: Efficient handling of connections and resources
27+
- **Graceful Shutdown**: Proper cleanup of resources during shutdown
28+
- **Configuration Management**: Flexible configuration through YAML files
29+
- **Schema Validation**: Input and output schema validation
30+
- **Error Handling**: Comprehensive error handling and logging
31+
32+
## Installation
33+
34+
```bash
35+
pip install fs-sdk
36+
```
37+
38+
## Quick Start
39+
40+
1. Create a function that processes messages:
41+
42+
```python
43+
from fs_sdk import FSFunction
44+
45+
async def my_process_function(request_data: dict) -> dict:
46+
# Process the request data
47+
result = process_data(request_data)
48+
return {"result": result}
49+
50+
# Initialize and run the function
51+
function = FSFunction(
52+
process_funcs={
53+
'my_module': my_process_function
54+
}
55+
)
56+
57+
await function.start()
58+
```
59+
60+
2. Create a configuration file (`config.yaml`):
61+
62+
```yaml
63+
pulsar:
64+
service_url: "pulsar://localhost:6650"
65+
authPlugin: "" # Optional
66+
authParams: "" # Optional
67+
68+
module: "my_module"
69+
subscriptionName: "my-subscription"
70+
71+
requestSource:
72+
- pulsar:
73+
topic: "input-topic"
74+
75+
sink:
76+
pulsar:
77+
topic: "output-topic"
78+
```
79+
80+
3. Define your function package (`package.yaml`):
81+
82+
```yaml
83+
name: my_function
84+
type: pulsar
85+
modules:
86+
my_module:
87+
name: my_process
88+
description: "Process incoming messages"
89+
inputSchema:
90+
type: object
91+
properties:
92+
data:
93+
type: string
94+
required:
95+
- data
96+
outputSchema:
97+
type: object
98+
properties:
99+
result:
100+
type: string
101+
```
102+
103+
## Core Components
104+
105+
### FSFunction
106+
107+
The main class for creating serverless functions. It handles:
108+
- Message consumption and processing
109+
- Response generation
110+
- Resource management
111+
- Metrics collection
112+
- Error handling
113+
114+
### Configuration
115+
116+
The SDK uses YAML configuration files to define:
117+
- Pulsar connection settings
118+
- Module selection
119+
- Topic subscriptions
120+
- Input/output topics
121+
- Custom configuration parameters
122+
123+
### Metrics
124+
125+
Built-in metrics collection for:
126+
- Request processing time
127+
- Success/failure rates
128+
- Message throughput
129+
- Resource utilization
130+
131+
## Examples
132+
133+
Check out the `examples` directory for complete examples:
134+
135+
- `string_function.py`: A simple string processing function
136+
- `test_string_function.py`: Test client for the string function
137+
- `config.yaml`: Example configuration
138+
- `package.yaml`: Example package definition
139+
140+
## Best Practices
141+
142+
1. **Error Handling**
143+
- Always handle exceptions in your process functions
144+
- Use proper logging for debugging
145+
- Implement graceful shutdown
146+
147+
2. **Resource Management**
148+
- Close resources properly
149+
- Use context managers when possible
150+
- Monitor resource usage
151+
152+
3. **Configuration**
153+
- Use environment variables for sensitive data
154+
- Validate configuration values
155+
- Document configuration options
156+
157+
4. **Testing**
158+
- Write unit tests for your functions
159+
- Test error scenarios
160+
- Validate input/output schemas
161+
162+
## Development
163+
164+
### Prerequisites
165+
166+
- Python 3.7+
167+
- Apache Pulsar
168+
- pip
169+
170+
### Setup Development Environment
171+
172+
```bash
173+
# Create virtual environment
174+
python -m venv venv
175+
source venv/bin/activate # Linux/Mac
176+
# or
177+
.\venv\Scripts\activate # Windows
178+
179+
# Install dependencies
180+
pip install -r requirements.txt
181+
182+
# Install the package in development mode
183+
pip install -e .
184+
```
185+
186+
### Running Tests
187+
188+
```bash
189+
pytest
190+
```
191+
192+
## Support
193+
194+
For support, please open an issue in the GitHub repository or contact the maintainers.

sdks/fs-python/examples/Dockerfile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
FROM functionstream/fs-python-base:latest
2+
3+
WORKDIR /function
4+
5+
COPY requirements.txt .
6+
RUN pip install -r requirements.txt
7+
8+
COPY string_function.py .
9+
COPY config.yaml .
10+
COPY package.yaml .
11+
12+
CMD ["python", "string_function.py"]

sdks/fs-python/examples/Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
build-image:
3+
docker build -t my-function:latest .
4+
5+
.DEFAULT_GOAL := build-image

sdks/fs-python/examples/config.yaml

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Copyright 2024 Function Stream Org.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# FunctionStream Configuration File
16+
# This configuration file defines the settings for the string processing function example.
17+
18+
pulsar:
19+
serviceUrl: "pulsar://192.168.31.80:6650" # Required: URL of the Pulsar broker
20+
authPlugin: "" # Optional: Authentication plugin class name
21+
authParams: "" # Optional: Authentication parameters
22+
23+
module: "string" # Required: Name of the module to use for processing
24+
25+
# Optional: List of source topics to consume from
26+
# Note: Either sources or requestSource must be specified
27+
sources:
28+
- pulsar: # SourceSpec structure with pulsar configuration
29+
topic: "topic-a" # Topic name for regular message consumption
30+
31+
# Optional: request source
32+
requestSource:
33+
pulsar: # SourceSpec structure with pulsar configuration
34+
topic: "string-topic" # Topic name for request messages
35+
36+
# Required: Name of the subscription for the consumer
37+
subscriptionName: "test-sub"
38+
39+
# Optional: Output sink configuration
40+
sink:
41+
pulsar: # SinkSpec structure with pulsar configuration
42+
topic: "output" # Topic name for output messages
43+
44+
# Optional: Additional configuration parameters
45+
config:
46+
- test: "Hello from config" # Example configuration value
47+
- test2: "Another config value" # Another example configuration value

sdks/fs-python/examples/package.yaml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Copyright 2024 Function Stream Org.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# FunctionStream Package Configuration
16+
# This file defines the package metadata and function specifications for deployment.
17+
18+
# Package name and type
19+
name: my_function # Name of the function package
20+
type: pulsar # Type of message broker to use
21+
22+
# Module definitions
23+
modules:
24+
string: # Module name
25+
name: string_process # Function name
26+
description: "Appends an exclamation mark to the input string" # Function description
27+
28+
# Input schema definition
29+
inputSchema:
30+
type: object
31+
properties:
32+
text: # Input parameter
33+
type: string # Parameter type
34+
required:
35+
- text # Required parameter
36+
37+
# Output schema definition
38+
outputSchema:
39+
type: object
40+
properties:
41+
result: # Output parameter
42+
type: string # Parameter type
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# None. Please add your own deps here.

0 commit comments

Comments
 (0)