diff --git a/DataFileUtil.spec b/DataFileUtil.spec index fb3fdd6..8f1516a 100644 --- a/DataFileUtil.spec +++ b/DataFileUtil.spec @@ -350,13 +350,11 @@ module DataFileUtil { information to use the latest version. data - the object data. - Optional parameters: - One of an object name or id. If no name or id is provided the name - will be set to 'auto' with the object id appended as a string, - possibly with -\d+ appended if that object id already exists as a - name. + One of an object name or id: name - the name of the object. objid - the id of the object to save over. + + Optional parameters: meta - arbitrary user-supplied metadata for the object, not to exceed 16kb; if the object type specifies automatic metadata extraction with the 'meta ws' annotation, and your @@ -403,8 +401,16 @@ module DataFileUtil { } SaveObjectsParams; /* - Save objects to the workspace. Saving over a deleted object undeletes - it. + Save objects to the workspace. + + The objects will be sorted prior to saving to avoid the Workspace sort memory limit. + Note that workspace object refs in the object may cause the Workspace to resort the data + unless they are already in UPA format (e.g. #/#/#), so it is strongly recommended to + convert object refs to UPAs. + + If the data is very large, using the WSLargeDataIO SDK module is advised. + + Saving over a deleted object undeletes it. */ funcdef save_objects(SaveObjectsParams params) returns (list info) authentication required; diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index 8b22342..13f81ce 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -1,17 +1,23 @@ -#0.1.3 +# 0.1.4 +- objects saved to the workspace are now sorted prior to serialization. This prevents errors due + to workspace sort memory limits, and also moves the sort compute load (which is the most + expensive part of saving objects) from the workspace server node to the SDK job nodes. + +# 0.1.3 - tidying for github consistency - Added Actions workflow for KB SDK tests - Added Dependabot and LGTM configurations - Updated `README.md` to include standard build, coverage, and LGTM badging. -#0.1.2 +# 0.1.2 - deprecate handle service with handle service 2 -- replace the colon(:) [that was reported to have caused download error for Windows users] with underscore in shock filenames +- replace the colon(:) [that was reported to have caused download error for Windows users] with + underscore in shock filenames -#0.1.1 +# 0.1.1 - close no longer used sockets. -#0.1.0 +# 0.1.0 - shock attributes are now ignored on upload. In a future release they will be removed altogether and specifying attributes for upload will be an error. - shock indexes and attributes are no longer copied during a copy or ownership operation. diff --git a/lib/DataFileUtil/implementation.py b/lib/DataFileUtil/implementation.py index ff71b46..4936dee 100644 --- a/lib/DataFileUtil/implementation.py +++ b/lib/DataFileUtil/implementation.py @@ -15,6 +15,21 @@ def log(message, prefix_newline=False): print(('\n' if prefix_newline else '') + str(time.time()) + ': ' + str(message)) +# Copies of this in AssemblyUtil and GenomeFileUtil may be redundant since we're sorting here +# Note that insertion order is only maintained since python 3.6 and is only guaranteed since 3.7 +# In 3.6 it was an implementation detail. +# As of 2020/12/12 Python 3.6 is provided in the kbase/sdkbase2 image +def _sort_dict(in_struct): + """ + Recursively sort a dictionary by dictionary keys. + """ + if isinstance(in_struct, dict): + return {k: _sort_dict(in_struct[k]) for k in sorted(in_struct)} + elif isinstance(in_struct, list): + return [_sort_dict(k) for k in in_struct] + else: + return in_struct + def save_objects(ws, params, prov): # TODO unit tests @@ -42,10 +57,21 @@ def save_objects(ws, params, prov): else: prov_to_save = [{'input_ws_objects': extra_input_refs}] - keys = ['type', 'data', 'name', 'objid', 'meta', 'hidden'] + keys = ['type', 'name', 'objid', 'meta', 'hidden'] for k in keys: if k in o: obj_to_save[k] = o[k] + """ + Sorting the data is important for 2 reasons: + 1) It prevents the workspace from rejecting the save because the sort takes too much memory. + The workspace puts limits on memory use because it has to service many apps / UIs / etc. + at once. + 2) It distributes the sort across the app worker nodes rather than concentrating them on + the workspace node. + """ + # TODO sort in WSLargeDataIO as well + if 'data' in o: + obj_to_save['data'] = _sort_dict(o['data']) obj_to_save['provenance'] = prov_to_save objs_to_save.append(obj_to_save) @@ -53,6 +79,5 @@ def save_objects(ws, params, prov): try: return ws.save_objects({'id': wsid, 'objects': objs_to_save}) except WorkspaceError as e: - log('Logging workspace error on save_objects: {}\n{}'.format( - e.message, e.data)) + log('Logging workspace error on save_objects: {}\n{}'.format(e.message, e.data)) raise diff --git a/test/implementation_test.py b/test/implementation_test.py new file mode 100644 index 0000000..78a482c --- /dev/null +++ b/test/implementation_test.py @@ -0,0 +1,149 @@ +# unit tests for the implementation.py file. + +import json + +from unittest.mock import create_autospec + +from installed_clients.WorkspaceClient import Workspace + +from DataFileUtil.implementation import save_objects + +# TODO add more save_object unit tests. +# TODO upgrade to pytest. +# TODO figure out why the test results are interleaved with test logs & fix +# TODO figure out why there appear to be 2 test runs, 1 with this file, 1 without, per kb-sdk test +# TODO set up a makefile target to run only unit tests + +def test_save_objects_sorted(): + print("***** test_save_objects_sorted ****") + ws = create_autospec(Workspace, spec_set=True, instance=True) + + ws.save_objects.return_value = [ + [ + 17, + 'objname', + 'Some.Type-2.4', + '2020-12-12T19:52:29+0000', + 2367, + 'username', + 49778, + 'MyWorkspace', + '99914b932bd37a50b983c5e7c90ae93b', + 2, + {}], + [ + 18, + 'objname2', + 'Other.Type-2.4', + '2020-12-12T19:52:29+0000', + 1, + 'username', + 49778, + 'MyWorkspace', + '99914b932bd37a50b983c5e7c90ae93b', + 2, + {}] + ] + + params = { + 'id': 49778, + 'objects': [ + { + 'type': 'Some.Type-2.4', + 'name': 'objname', + 'data': {'sorted': [ + 3, + 2, + 1, + { + '4': 'a', + '3': 'a', + '1': 'a', + '2': 'a' + }], + 'by': 1, + 'yoda': ['whoo', 'whee', {'b': 1, 'a': 2}], + 'this was': 'foo'} + }, + { + 'type': 'Other.Type-2.4', + 'name': 'objname2', + 'data': { + 'a': 1, + 'c': 2, + 'b': 3, + } + }] + } + + res = save_objects(ws, params, {}) + expected = [ + [ + 17, + 'objname', + 'Some.Type-2.4', + '2020-12-12T19:52:29+0000', + 2367, + 'username', + 49778, + 'MyWorkspace', + '99914b932bd37a50b983c5e7c90ae93b', + 2, + {}], + [ + 18, + 'objname2', + 'Other.Type-2.4', + '2020-12-12T19:52:29+0000', + 1, + 'username', + 49778, + 'MyWorkspace', + '99914b932bd37a50b983c5e7c90ae93b', + 2, + {}] + ] + assert res == expected + + ws.save_objects.assert_called_once_with({ + 'id': 49778, + 'objects': [ + { + 'type': 'Some.Type-2.4', + 'name': 'objname', + 'data': {'sorted': [ + 3, + 2, + 1, + { + '4': 'a', + '3': 'a', + '1': 'a', + '2': 'a' + }], + 'by': 1, + 'yoda': ['whoo', 'whee', {'b': 1, 'a': 2}], + 'this was': 'foo' + }, + 'provenance': {} + }, + { + 'type': 'Other.Type-2.4', + 'name': 'objname2', + 'data': { + 'a': 1, + 'c': 2, + 'b': 3, + }, + 'provenance': {} + }] + }) + + assert json.dumps(ws.save_objects.call_args[0][0]['objects'][0]['data']) == ( + '{"by": 1, "sorted": [3, 2, 1, {"1": "a", "2": "a", "3": "a", "4": "a"}], ' + + '"this was": "foo", "yoda": ["whoo", "whee", {"a": 2, "b": 1}]}' + ) + + assert json.dumps(ws.save_objects.call_args[0][0]['objects'][1]['data']) == ( + '{"a": 1, "b": 3, "c": 2}' + )