#6372 Add support cache for dynamic spec
Pull request opened by
popojk
## Tracking issue
Closes
#5543
## Why are the changes needed?
When executing a dynamic workflow in the Flyte backend, compiling the future.pb file before sub-node execution takes some time. If an out-of-memory (OOM) error occurs during sub-node execution, re-running the dynamic workflow requires recompiling the future.pb file, consuming additional time and computational resources. To address this, we propose caching the compiled future.pb file.
The future cache mechanism in this PR stores the compiled file as artifact data in Flyte storage via the datacatalog. The cache key, TagName, is determined by the hash of the input values. The diagrams below illustrate the data flow in the backend system:
[截圖 2025-03-25 上午11 10 21](https://private-user-images.githubusercontent.com/66259759/426340554-60589264-3fcf-4675-ad20-bc8e64824816.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3NDI4ODIzODIsIm5iZiI6MTc0Mjg4MjA4MiwicGF0aCI6Ii82NjI1OTc1OS80MjYzNDA1NTQtNjA1ODkyNjQtM2ZjZi00Njc1LWFkMjAtYmM4ZTY0ODI0ODE2LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTAzMjUlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwMzI1VDA1NTQ0MlomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTE5ZTNhNWQzMDkxNTk3NGUyZDhjOTc3ODIyOWQ4M2M1MDgzNDdkMTgyNDdjYTI3ZTZlODQ2NGMwNWY2ZGNmMTYmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.VMcK8EZZTjsD7ONffHLaMHtqJ5zteaClZapjzrITBco)
[截圖 2025-03-25 上午11 11 27](https://private-user-images.githubusercontent.com/66259759/426340676-749da2bf-00fe-4bcb-a5bd-6df706fdba99.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3NDI4ODIzODIsIm5iZiI6MTc0Mjg4MjA4MiwicGF0aCI6Ii82NjI1OTc1OS80MjYzNDA2NzYtNzQ5ZGEyYmYtMDBmZS00YmNiLWE1YmQtNmRmNzA2ZmRiYTk5LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTAzMjUlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwMzI1VDA1NTQ0MlomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWVkYWRkZmU1YzU2NDJkNzViMTA5NTZkMWZjMGM0NDdkMDUyZWQwZTVlMzViM2MzMjZjN2ZjMWQzZTlmZjk0YjgmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.Nn3vD62Y3B3D4JOmtUXrQSmOlNKllsvoTRlcR4-gUeU)
## What changes were proposed in this pull request?
Please refer to bellow diagram for understanding the procedure of how future read/write:
[截圖 2025-03-25 上午11 13 07](https://private-user-images.githubusercontent.com/66259759/426341005-befc4a3b-2cce-4b55-bb16-a053ac715276.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3NDI4ODIzODIsIm5iZiI6MTc0Mjg4MjA4MiwicGF0aCI6Ii82NjI1OTc1OS80MjYzNDEwMDUtYmVmYzRhM2ItMmNjZS00YjU1LWJiMTYtYTA1M2FjNzE1Mjc2LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTAzMjUlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwMzI1VDA1NTQ0MlomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWQxYWMwNjY1YTEwMWNhYmQxMGM3MTJhMDI0ZGMyMjgwNWE5ZGUzNzA2M2I1Y2QwMGNhYzJhOGNmY2I5ZDMzMjUmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.NpczHJXnRhDLgVIaknWt0-XMsdNx9KmOEmmsK1_SUBU)
key changes:
1.Added Mode to TaskTemplate Metadata in flyteidl to indicate whether a task is a DynamicTask.
2.Implemented logic in flytepropeller to send cache read/write requests to datacatalog.
3.Added cache read/write functionality to the storage layer in datacatalog.
## How was this patch tested?
To test how much time it saved when future file cache hit, we modify this block of code to count the time spend to run taskNodeHandler.Handle for test purpose.
startTime := time.Now()
if !isDynamic || cacheStatus == nil || cacheStatus.GetCacheStatus() != core.CatalogCacheStatus_CACHE_HIT {
var err error
taskTrns, err := d.TaskNodeHandler.Handle(ctx, nCtx)
trns = &taskTrns
if err != nil {
logger.Debug(ctx, "Failed to compile dynamic workflow")
return *trns, prevState, err
}
}
if isDynamic {
duration := time.Since(startTime)
logger.Infof(ctx, "Dynamic node compilation took %v", duration)
}
To mimic OOM, we will throw panic in dynamicNodeHandler. So the Dynamic workflow will be aborted after future file compiled
func (d dynamicNodeTaskNodeHandler) handleDynamicSubNodes(ctx context.Context, nCtx interfaces.NodeExecutionContext, prevState handler.DynamicNodeState) (handler.Transition, handler.DynamicNodeState, error) {
panic("throw panic here to mimic OOM")
# existing code…
then we run bellow test dynamic workflow for the first time
```
import typing
from flytekit import dynamic, task, workflow
@task
def return_index(character: str) -> int:
"""
Computes the character index (which needs to fit into the 26 characters list)
"""
if character.islower():
return ord(character) - ord("a")
else:
return ord(character) - ord("A")
@task
def update_list(freq_list: typing.List[int], list_index: int) -> typing.List[int]:
"""
Notes the frequency of characters
"""
freq_list[list_index] += 1
return freq_list
@task
def derive_count(freq1: typing.List[int], freq2: typing.List[int]) -> int:
"""
Derives the number of common characters
"""
count = 0
for i in range(26):
count += min(freq1[i], freq2[i])
return count
@dynamic(cache_version="4", cache=True)
def count_characters(s1: str, s2: str) -> int:
"""
Ca…
flyteorg/flyte