-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.py
More file actions
282 lines (222 loc) · 8.21 KB
/
app.py
File metadata and controls
282 lines (222 loc) · 8.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
"""
FastAPI Zerobus Ingestion Service
Configuration-driven service that exposes REST endpoints for ingesting
data into Databricks tables via Zerobus streams.
"""
import json
import logging
import os
from contextlib import asynccontextmanager
from importlib import import_module
from pathlib import Path
from typing import Any, Dict
from dotenv import load_dotenv
from fastapi import FastAPI, HTTPException, Request
from fastapi.responses import JSONResponse
from pydantic import create_model
from stream_manager import StreamManager
load_dotenv()
logging.basicConfig(
level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
logger = logging.getLogger(__name__)
stream_manager: StreamManager = None
config: Dict = None
table_models: Dict[str, Any] = {}
def load_config() -> Dict:
"""Load configuration from config.json"""
config_path = Path(__file__).parent / "config.json"
with open(config_path, "r") as f:
return json.load(f)
def create_pydantic_model(table_key: str, fields: list):
"""
Create a Pydantic model dynamically from field definitions.
Args:
table_key: Unique table identifier
fields: List of field definitions from config
Returns:
Pydantic model class
"""
field_definitions = {}
for field in fields:
field_name = field["name"]
field_type = field["type"]
type_map = {
"string": (str, ...),
"int32": (int, ...),
"int64": (int, ...),
"float": (float, ...),
"double": (float, ...),
"bool": (bool, ...),
}
if field_type in type_map:
field_definitions[field_name] = type_map[field_type]
else:
field_definitions[field_name] = (str, ...)
model_name = f"{table_key.title().replace('_', '')}Record"
return create_model(model_name, **field_definitions)
@asynccontextmanager
async def lifespan(app: FastAPI):
"""Lifespan context manager for FastAPI app."""
global stream_manager, config, table_models
logger.info("Starting Zerobus Station service...")
config = load_config()
logger.info(f"Loaded configuration with {len(config['tables'])} tables")
client_id = os.getenv("DATABRICKS_CLIENT_ID")
client_secret = os.getenv("DATABRICKS_CLIENT_SECRET")
if not client_id or not client_secret:
logger.error("DATABRICKS_CLIENT_ID and DATABRICKS_CLIENT_SECRET must be set")
raise ValueError("Missing required environment variables")
stream_manager = StreamManager(
server_endpoint=config["databricks"]["server_endpoint"],
workspace_id=config["databricks"]["workspace_id"],
workspace_url=config["databricks"]["workspace_url"],
client_id=client_id,
client_secret=client_secret,
)
logger.info("✓ Stream manager initialized")
for table_key, table_config in config["tables"].items():
table_models[table_key] = create_pydantic_model(
table_key, table_config["fields"]
)
logger.info(f"✓ Created validation model for {table_key}")
logger.info("=" * 60)
logger.info("Zerobus Station is ready!")
logger.info("Available endpoints:")
for table_key in config["tables"].keys():
logger.info(f" POST /ingest/{table_key}")
logger.info(f" GET /health/{table_key}")
logger.info("=" * 60)
yield
logger.info("Shutting down Zerobus Station...")
if stream_manager:
await stream_manager.close_all()
logger.info("✓ Shutdown complete")
app = FastAPI(
title="Zerobus Station",
description="Configuration-driven Databricks ingestion service using Zerobus",
version="1.0.0",
lifespan=lifespan,
)
@app.get("/")
async def root():
"""Root endpoint with service information"""
return {
"service": "Zerobus Station",
"version": "1.0.0",
"tables": list(config["tables"].keys()) if config else [],
"endpoints": {
table_key: {
"ingest": f"/ingest/{table_key}",
"health": f"/health/{table_key}",
}
for table_key in (config["tables"].keys() if config else [])
},
}
@app.get("/health")
async def health():
"""Global health check"""
return {
"status": "healthy",
"active_streams": stream_manager.get_active_tables() if stream_manager else [],
}
@app.get("/health/{table_key}")
async def table_health(table_key: str):
"""Health check for a specific table"""
if not config or table_key not in config["tables"]:
raise HTTPException(status_code=404, detail=f"Table {table_key} not found")
is_active = table_key in (
stream_manager.get_active_tables() if stream_manager else []
)
return {
"table": table_key,
"table_name": config["tables"][table_key]["table_name"],
"stream_active": is_active,
"status": "healthy",
}
@app.post("/ingest/{table_key}")
async def ingest_record(
table_key: str, body: Dict[str, Any], wait_for_ack: bool = False
):
"""
Ingest a record into the specified table.
Args:
table_key: Table identifier (e.g., "station_one")
body: JSON payload matching the table schema
wait_for_ack: If True, waits for durability acknowledgment (default: False)
Returns:
Acknowledgment of ingestion
"""
if not config or table_key not in config["tables"]:
raise HTTPException(status_code=404, detail=f"Table {table_key} not found")
try:
model = table_models[table_key]
validated_data = model(**body)
except Exception as e:
raise HTTPException(status_code=400, detail=f"Validation error: {str(e)}")
table_config = config["tables"][table_key]
try:
await stream_manager.get_stream(
table_key=table_key,
table_name=table_config["table_name"],
proto_module=f"tables.{table_key}.schema_pb2",
message_name=table_config["message_name"],
)
pb_module = import_module(f"tables.{table_key}.schema_pb2")
message_class = getattr(pb_module, table_config["message_name"])
record_dict = validated_data.model_dump()
record = message_class(**record_dict)
future = await stream_manager.ingest_record(table_key, record)
if wait_for_ack:
await future
return {
"status": "success",
"table": table_key,
"message": "Record ingested successfully",
"wait_for_ack": wait_for_ack,
}
except Exception as e:
logger.error(f"Error ingesting record into {table_key}: {e}", exc_info=True)
raise HTTPException(
status_code=500, detail=f"Failed to ingest record: {str(e)}"
)
@app.post("/flush/{table_key}")
async def flush_table(table_key: str):
"""
Flush pending records for a specific table.
Args:
table_key: Table identifier
Returns:
Flush status
"""
if not config or table_key not in config["tables"]:
raise HTTPException(status_code=404, detail=f"Table {table_key} not found")
if table_key not in stream_manager.streams:
return {
"status": "no_active_stream",
"table": table_key,
"message": "No active stream to flush",
}
try:
stream = stream_manager.streams[table_key]
await stream.flush()
return {
"status": "success",
"table": table_key,
"message": "Stream flushed successfully",
}
except Exception as e:
logger.error(f"Error flushing stream for {table_key}: {e}", exc_info=True)
raise HTTPException(status_code=500, detail=f"Failed to flush stream: {str(e)}")
@app.exception_handler(HTTPException)
async def http_exception_handler(request: Request, exc: HTTPException):
return JSONResponse(
status_code=exc.status_code,
content={"error": exc.detail, "status_code": exc.status_code},
)
@app.exception_handler(Exception)
async def general_exception_handler(request: Request, exc: Exception):
logger.error(f"Unhandled exception: {exc}", exc_info=True)
return JSONResponse(
status_code=500, content={"error": "Internal server error", "detail": str(exc)}
)