|
| 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. |
0 commit comments