Skip to content

Commit

Permalink
set actual byte size in result_size field instead of compressed size
Browse files Browse the repository at this point in the history
  • Loading branch information
guzzijones committed Jul 3, 2023
1 parent 1bdd067 commit 737c6a5
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
9 changes: 7 additions & 2 deletions st2api/st2api/controllers/v1/actionexecutions.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from oslo_config import cfg
from six.moves import http_client
from mongoengine.queryset.visitor import Q
import zstandard

from st2api.controllers.base import BaseRestControllerMixin
from st2api.controllers.resource import ResourceController
Expand Down Expand Up @@ -438,12 +439,16 @@ def get(
# For new JSON storage format we just use raw value since it's already JSON serialized
# string
response_body = result["result"]

try:
response_body = zstandard.ZstdDecompressor().decompress(response_body)
# skip if already a byte string and not compressed
except zstandard.ZstdError:
pass
if pretty_format:
# Pretty format is not a default behavior since it adds quite some overhead (e.g.
# 10-30ms for non pretty format for 4 MB json vs ~120 ms for pretty formatted)
response_body = orjson.dumps(
orjson.loads(result["result"]), option=orjson.OPT_INDENT_2
orjson.loads(response_body), option=orjson.OPT_INDENT_2
)

response = Response()
Expand Down
7 changes: 4 additions & 3 deletions st2common/st2common/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ def parse_field_value(self, value: Optional[Union[bytes, dict]]) -> dict:
data = orjson.loads(data)
return data

def _serialize_field_value(self, value: dict) -> bytes:
def _serialize_field_value(self, value: dict, zstd=True) -> bytes:
"""
Serialize and encode the provided field value.
"""
Expand All @@ -434,8 +434,9 @@ def default(obj):
return list(obj)
raise TypeError

value = orjson.dumps(value, default=default)
data = zstandard.ZstdCompressor().compress(value)
data = orjson.dumps(value, default=default)
if zstd:
data = zstandard.ZstdCompressor().compress(data)

return data

Expand Down
2 changes: 1 addition & 1 deletion st2common/st2common/services/executions.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ def update_execution(liveaction_db, publish=True, set_result_size=False):
with Timer(key="action.executions.calculate_result_size"):
result_size = len(
ActionExecutionDB.result._serialize_field_value(
liveaction_db.result
value=liveaction_db.result, zstd=False
)
)
kw["set__result_size"] = result_size
Expand Down

0 comments on commit 737c6a5

Please sign in to comment.