Skip to content

Commit 737c6a5

Browse files
committed
set actual byte size in result_size field instead of compressed size
1 parent 1bdd067 commit 737c6a5

3 files changed

Lines changed: 12 additions & 6 deletions

File tree

st2api/st2api/controllers/v1/actionexecutions.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
from oslo_config import cfg
2828
from six.moves import http_client
2929
from mongoengine.queryset.visitor import Q
30+
import zstandard
3031

3132
from st2api.controllers.base import BaseRestControllerMixin
3233
from st2api.controllers.resource import ResourceController
@@ -438,12 +439,16 @@ def get(
438439
# For new JSON storage format we just use raw value since it's already JSON serialized
439440
# string
440441
response_body = result["result"]
441-
442+
try:
443+
response_body = zstandard.ZstdDecompressor().decompress(response_body)
444+
# skip if already a byte string and not compressed
445+
except zstandard.ZstdError:
446+
pass
442447
if pretty_format:
443448
# Pretty format is not a default behavior since it adds quite some overhead (e.g.
444449
# 10-30ms for non pretty format for 4 MB json vs ~120 ms for pretty formatted)
445450
response_body = orjson.dumps(
446-
orjson.loads(result["result"]), option=orjson.OPT_INDENT_2
451+
orjson.loads(response_body), option=orjson.OPT_INDENT_2
447452
)
448453

449454
response = Response()

st2common/st2common/fields.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ def parse_field_value(self, value: Optional[Union[bytes, dict]]) -> dict:
414414
data = orjson.loads(data)
415415
return data
416416

417-
def _serialize_field_value(self, value: dict) -> bytes:
417+
def _serialize_field_value(self, value: dict, zstd=True) -> bytes:
418418
"""
419419
Serialize and encode the provided field value.
420420
"""
@@ -434,8 +434,9 @@ def default(obj):
434434
return list(obj)
435435
raise TypeError
436436

437-
value = orjson.dumps(value, default=default)
438-
data = zstandard.ZstdCompressor().compress(value)
437+
data = orjson.dumps(value, default=default)
438+
if zstd:
439+
data = zstandard.ZstdCompressor().compress(data)
439440

440441
return data
441442

st2common/st2common/services/executions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ def update_execution(liveaction_db, publish=True, set_result_size=False):
224224
with Timer(key="action.executions.calculate_result_size"):
225225
result_size = len(
226226
ActionExecutionDB.result._serialize_field_value(
227-
liveaction_db.result
227+
value=liveaction_db.result, zstd=False
228228
)
229229
)
230230
kw["set__result_size"] = result_size

0 commit comments

Comments
 (0)