Data Sync Operations¶
Handlers for data synchronization operations.
Handlers¶
GetJSONFromFileHandler- Retrieves JSON data from a filePutJSONToFileHandler- Writes JSON data to a fileDataSyncHandler- Simple data sync taskBatchDataSyncHandler- Handles batch of data sync tasksPrepareBatchDataSyncHandler- Prepares batch data sync tasks
Data synchronization operation handlers.
Provides Lambda handlers for reading, writing, and synchronizing data between local file systems and S3.
BatchDataSyncHandler
dataclass
¶
BatchDataSyncHandler()
Bases: LambdaHandler[BatchDataSyncRequest, BatchDataSyncResponse]
Handler for processing batches of data synchronization requests.
Processes multiple sync requests sequentially, with optional partial failure handling.
handle ¶
handle(request)
Process a batch of data sync requests.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
BatchDataSyncRequest
|
Request containing a list of sync requests. |
required |
Returns:
| Type | Description |
|---|---|
BatchDataSyncResponse
|
Response containing aggregated results and any failed requests. |
Raises:
| Type | Description |
|---|---|
Exception
|
If a sync fails and allow_partial_failure is False. |
Source code in src/aibs_informatics_aws_lambda/handlers/data_sync/operations.py
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 | |
DataSyncHandler
dataclass
¶
DataSyncHandler()
Bases: LambdaHandler[DataSyncRequest, DataSyncResponse]
Handler for synchronizing data between source and destination paths.
Supports syncing between local file systems and S3.
handle ¶
handle(request)
Synchronize data from source to destination.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
DataSyncRequest
|
Request containing source and destination paths. |
required |
Returns:
| Type | Description |
|---|---|
DataSyncResponse
|
Response containing the sync result. |
Source code in src/aibs_informatics_aws_lambda/handlers/data_sync/operations.py
160 161 162 163 164 165 166 167 168 169 170 171 | |
GetJSONFromFileHandler
dataclass
¶
GetJSONFromFileHandler()
Bases: LambdaHandler[GetJSONFromFileRequest, GetJSONFromFileResponse]
Handler for retrieving JSON content from a file.
Supports loading JSON from both local files and S3 locations.
handle ¶
handle(request)
Load JSON content from the specified path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
GetJSONFromFileRequest
|
Request containing the file path. |
required |
Returns:
| Type | Description |
|---|---|
GetJSONFromFileResponse
|
Response containing the loaded JSON content. |
Raises:
| Type | Description |
|---|---|
Exception
|
If the content cannot be fetched. |
Source code in src/aibs_informatics_aws_lambda/handlers/data_sync/operations.py
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 | |
PrepareBatchDataSyncHandler
dataclass
¶
PrepareBatchDataSyncHandler()
Bases: LambdaHandler[PrepareBatchDataSyncRequest, PrepareBatchDataSyncResponse]
Handler for preparing batch data synchronization requests.
Analyzes the source path and partitions files into optimally-sized batches for parallel processing using the bin-packing algorithm.
build_node_batches
classmethod
¶
build_node_batches(nodes, batch_size_bytes_limit)
Batch nodes based on threshold
This is a version of the classic "Bin Packing" problem. https://en.wikipedia.org/wiki/Bin_packing_problem
The following solutions implements the First-fit decreasing algorithm.
Notes
- nodes can have sizes greater than the limit
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nodes
|
List[Node]
|
List of nodes to batch. |
required |
batch_size_bytes_limit
|
int
|
Size limit in bytes for a batch of nodes. |
required |
Returns:
| Type | Description |
|---|---|
List[List[Node]]
|
List of node batches (list of lists). |
Source code in src/aibs_informatics_aws_lambda/handlers/data_sync/operations.py
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 | |
handle ¶
handle(request)
Prepare batch data sync requests.
Partitions the source data into optimally-sized batches.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
PrepareBatchDataSyncRequest
|
Request containing source path and configuration. |
required |
Returns:
| Type | Description |
|---|---|
PrepareBatchDataSyncResponse
|
Response containing prepared batch requests. |
Source code in src/aibs_informatics_aws_lambda/handlers/data_sync/operations.py
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 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 | |
PutJSONToFileHandler
dataclass
¶
PutJSONToFileHandler()
Bases: LambdaHandler[PutJSONToFileRequest, PutJSONToFileResponse]
Handler for writing JSON content to a file.
Supports writing to both local files and S3 locations. If no path is provided, generates a scratch S3 path.
handle ¶
handle(request)
Write JSON content to the specified path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
PutJSONToFileRequest
|
Request containing the content and optional path. |
required |
Returns:
| Type | Description |
|---|---|
Optional[PutJSONToFileResponse]
|
Response containing the path where content was written. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no path is provided and bucket name cannot be inferred. |
Source code in src/aibs_informatics_aws_lambda/handlers/data_sync/operations.py
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 | |
get_s3_scratch_key ¶
get_s3_scratch_key(
filename=None, content=None, unique_id=None
)
Generates a scratch file s3 key
The key is constructed from filename, content and unique ID.
If filename is not provided, a hexdigest is created from content (which will be random if content is None).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
Optional[str]
|
Optional name of file. If None, file hash is generated. |
None
|
content
|
Optional[JSON]
|
Optional content of file to put. Only used if filename is not provided. Defaults to None. |
None
|
unique_id
|
Optional[UniqueID]
|
A unique ID used in key generation. If None, a random UUID is generated. |
None
|
Returns:
| Type | Description |
|---|---|
S3Key
|
S3 Scratch key (not gauranteed to be empty) |
Source code in src/aibs_informatics_aws_lambda/handlers/data_sync/operations.py
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 | |