History cache size-based limit charges proto wire size, so entries retain ~8x more heap than charged

Abierto
#13,435 1 comentario 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
55/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
go

Línea de trabajo

Start with ContextImpl.ApproximateSize() and the cache implementation under service/history/workflow/cache/. Review the reproduction tests in service/history/workflow/ and service/history/workflow/cache/, then run the commands given in the issue. Done means cache charging reflects the entries’ actual heap usage closely enough to satisfy the reproductions and the byte limit can reliably bound memory.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

oss-foundations

Expected Behavior and Actual Behavior

The history service keeps recently used workflows in an in-memory cache. With history.cacheSizeBasedLimit=true, the cache is limited by bytes instead of entry count, so history.hostLevelCacheMaxSizeBytes should roughly cap the memory the cache holds. This is not what is happening tho.

The cache charges each entry its serialized (protobuf) size, but an entry in memory is a Go object that takes much more heap than its serialized form. The extra cost grows with the number of items in the workflow (activities, timers, ...), not with their bytes.

So workflows made of many small items retain about 8x what they are charged, while workflows with large payloads are charged about right. Some experiments measured the following discrepancies:

workflow shape charged heap retained retained / charged
200 activities ~30 KB ~256 KB ~8.5x
500 timers ~18.5 KB ~148 KB ~8.0x
1 activity with a 256 KiB payload ~263 KB ~270 KB ~1.03x

I measured per entry, not total process memory. Still, it means the byte limit can't be relied on to bound the cache's memory.

The charged size comes from ContextImpl.ApproximateSize(). Its doc comment already notes that in-memory size "will be slightly larger" than the serialized size; this shows it can be about 8x larger.

Steps to Reproduce the Problem

Two independent test-only PRs on my fork, each one test file, each expected to fail:

  1. Heap-based, https://github.com/MarcosCela/temporal/pull/5: compares the charged size with the live heap (GC-settled) held by N entries.
    HISTORY_CACHE_SIZE_REPRO=1 go test -tags test_dep ./service/history/workflow/ -run TestHistoryCacheSizeRepro -v -count=1
    
  2. Reflection-based, https://github.com/MarcosCela/temporal/pull/6: fills the real workflow cache and compares its own Size() with a reflection-based deep size of each entry, without using the GC.
    HISTORY_CACHE_SIZE_REPRO=1 go test -tags test_dep ./service/history/workflow/cache/ -run TestHistoryCacheDeepSizeRepro -v -count=1
    

Both fail when an entry is more than 2x its charged size, and are skipped without the environment variable.

Related reports

Similar symptoms (memory growing past the size-based limits); none identifies this cause:

  • Forum thread: OOMs on v1.29.1 with both caches at 128 MB, stable when the mutable-state cache is count-based.
  • #8902: history memory usage trend, same count-based workaround.

Other cacheSizeBasedLimit bugs, not this one: #10548, #9954, #9600.

Specifications

  • Version: main at 873017418 (symptom also reported on 1.29.1 in the forum thread)
  • Platform: macOS arm64 and Linux arm64, Go 1.27.4
Lenguaje dominante
Go
Estrellas
24.7k
Forks
2.2k
Merge medio
3 d 7 h
PR fusionados (30 d)
222

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de temporalio/temporal

Todos los issues de temporalio/temporal

Issues similares

Más issues de Go

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.