graphcore-kernel 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- graphcore/__init__.py +5 -0
- graphcore/agent_frontend/__init__.py +8 -0
- graphcore/agent_frontend/api.py +69 -0
- graphcore/agent_frontend/base/__init__.py +11 -0
- graphcore/agent_frontend/base/views.py +167 -0
- graphcore/agent_frontend/client.py +31 -0
- graphcore/agent_frontend/context.py +157 -0
- graphcore/agent_frontend/errors.py +12 -0
- graphcore/agent_frontend/loading.py +54 -0
- graphcore/agent_frontend/registry.py +42 -0
- graphcore/agent_frontend/render.py +207 -0
- graphcore/agent_frontend/server.py +103 -0
- graphcore/agent_frontend/signatures.py +35 -0
- graphcore/agent_frontend/testing.py +45 -0
- graphcore/agent_frontend/tests/__init__.py +0 -0
- graphcore/agent_frontend/tests/fakes.py +114 -0
- graphcore/agent_frontend/tests/test_frontend.py +447 -0
- graphcore/agent_frontend/tests/test_loading.py +83 -0
- graphcore/agent_frontend/tests/test_server.py +109 -0
- graphcore/agent_frontend/tools.py +137 -0
- graphcore/api/__init__.py +4 -0
- graphcore/api/graph_api.py +378 -0
- graphcore/api/predicate_codec.py +81 -0
- graphcore/api/tests/__init__.py +0 -0
- graphcore/api/tests/test_changed_entities.py +109 -0
- graphcore/api/tests/test_graph_api.py +290 -0
- graphcore/api/tests/test_graph_api_access.py +94 -0
- graphcore/api/tests/test_graph_api_grants.py +90 -0
- graphcore/api/tests/test_predicate_codec.py +83 -0
- graphcore/api/tests/test_subtree_query.py +79 -0
- graphcore/apps.py +71 -0
- graphcore/auth/__init__.py +1 -0
- graphcore/auth/authn/README.md +178 -0
- graphcore/auth/authn/__init__.py +3 -0
- graphcore/auth/authn/authenticator.py +120 -0
- graphcore/auth/authn/onboarding.py +105 -0
- graphcore/auth/authn/principals.py +20 -0
- graphcore/auth/authn/resource_metadata.py +43 -0
- graphcore/auth/authn/service_accounts.py +96 -0
- graphcore/auth/authn/testing.py +96 -0
- graphcore/auth/authn/tests/__init__.py +0 -0
- graphcore/auth/authn/tests/test_authenticator.py +145 -0
- graphcore/auth/authn/tests/test_onboarding.py +145 -0
- graphcore/auth/authn/tests/test_service_accounts.py +180 -0
- graphcore/auth/authn/users.py +59 -0
- graphcore/auth/authz/__init__.py +0 -0
- graphcore/auth/authz/access_port.py +120 -0
- graphcore/auth/authz/adapter/__init__.py +0 -0
- graphcore/auth/authz/adapter/default_access_adapter.py +556 -0
- graphcore/auth/authz/adapter/tests/__init__.py +0 -0
- graphcore/auth/authz/adapter/tests/test_default_adapter.py +543 -0
- graphcore/auth/authz/adapter/tests/test_entity_write_hooks.py +207 -0
- graphcore/auth/authz/adapter/tests/test_group_types.py +70 -0
- graphcore/auth/authz/adapter/tests/test_sync_support.py +209 -0
- graphcore/auth/authz/defaults.py +26 -0
- graphcore/auth/authz/kernel_policy.py +191 -0
- graphcore/auth/authz/kernel_writes.py +31 -0
- graphcore/auth/authz/lookups.py +25 -0
- graphcore/auth/authz/policy.py +403 -0
- graphcore/auth/authz/tests/__init__.py +0 -0
- graphcore/auth/authz/tests/test_can_contain.py +150 -0
- graphcore/auth/authz/tests/test_contained_by.py +167 -0
- graphcore/auth/authz/tests/test_creation.py +391 -0
- graphcore/auth/authz/tests/test_delete_permission.py +82 -0
- graphcore/auth/authz/tests/test_granted_to_me.py +160 -0
- graphcore/auth/authz/tests/test_integration.py +637 -0
- graphcore/auth/authz/tests/test_invites.py +184 -0
- graphcore/auth/authz/tests/test_kernel_policy.py +140 -0
- graphcore/auth/authz/tests/test_lockout.py +132 -0
- graphcore/auth/authz/tests/test_policy.py +329 -0
- graphcore/auth/authz/tests/test_primitives.py +73 -0
- graphcore/auth/authz/tests/test_principal_creation.py +62 -0
- graphcore/bootstrap/__init__.py +0 -0
- graphcore/bootstrap/deployment.py +67 -0
- graphcore/bootstrap/signals.py +16 -0
- graphcore/bootstrap/tests/__init__.py +0 -0
- graphcore/bootstrap/tests/test_deployment_seed.py +154 -0
- graphcore/broadcast/__init__.py +4 -0
- graphcore/broadcast/broadcaster.py +201 -0
- graphcore/broadcast/channels.py +56 -0
- graphcore/broadcast/tests/__init__.py +0 -0
- graphcore/broadcast/tests/test_broadcaster.py +444 -0
- graphcore/celery.py +14 -0
- graphcore/docs/AGENT_FRONTEND_TDD.md +389 -0
- graphcore/docs/ARCHITECTURE_PRINCIPLES.md +152 -0
- graphcore/docs/AUTHORIZATION_TECHNICAL_TDD.md +1379 -0
- graphcore/docs/ENTITY_REFERENCE_TDD.md +561 -0
- graphcore/docs/GRAPHAPI_FRONT_DOOR.md +173 -0
- graphcore/docs/RUNTIME.md +90 -0
- graphcore/docs/SCHEDULING_TDD.md +244 -0
- graphcore/docs/STRATEGY_EXECUTION_TDD.md +1043 -0
- graphcore/docs/VIEW_ENTITY_TDD.md +522 -0
- graphcore/docs/VIEW_SYSTEM_TDD.md +307 -0
- graphcore/docs/__init__.py +0 -0
- graphcore/docs/doc_topics.py +97 -0
- graphcore/docs/docs_discovery.py +64 -0
- graphcore/docs/public_api.py +56 -0
- graphcore/domain/__init__.py +0 -0
- graphcore/domain/entity/Entity.py +204 -0
- graphcore/domain/entity/__init__.py +0 -0
- graphcore/domain/entity/deployment/Deployment.py +25 -0
- graphcore/domain/entity/deployment/__init__.py +0 -0
- graphcore/domain/entity/grant/Grant.py +31 -0
- graphcore/domain/entity/grant/__init__.py +0 -0
- graphcore/domain/entity/group/Group.py +48 -0
- graphcore/domain/entity/group/README.md +88 -0
- graphcore/domain/entity/group/__init__.py +0 -0
- graphcore/domain/entity/invite/Invite.py +31 -0
- graphcore/domain/entity/invite/__init__.py +0 -0
- graphcore/domain/entity/request_template/RequestTemplate.py +48 -0
- graphcore/domain/entity/request_template/__init__.py +0 -0
- graphcore/domain/entity/request_template/tests/__init__.py +0 -0
- graphcore/domain/entity/request_template/tests/test_request_template.py +52 -0
- graphcore/domain/entity/service_account/ServiceAccount.py +41 -0
- graphcore/domain/entity/service_account/__init__.py +0 -0
- graphcore/domain/entity/strategy_request/StrategyRequest.py +61 -0
- graphcore/domain/entity/strategy_request/__init__.py +0 -0
- graphcore/domain/entity/strategy_request/tests/__init__.py +0 -0
- graphcore/domain/entity/strategy_request/tests/test_strategy_request_entity.py +35 -0
- graphcore/domain/entity/tests/__init__.py +0 -0
- graphcore/domain/entity/tests/test_entity.py +227 -0
- graphcore/domain/entity/tests/test_grant_group_entities.py +36 -0
- graphcore/domain/entity/tests/test_write_once_fields.py +98 -0
- graphcore/domain/entity/user/User.py +39 -0
- graphcore/domain/entity/user/__init__.py +0 -0
- graphcore/domain/entity/user/tests/__init__.py +0 -0
- graphcore/domain/entity/user/tests/test_user_entity.py +33 -0
- graphcore/domain/entity/view/View.py +37 -0
- graphcore/domain/entity/view/__init__.py +0 -0
- graphcore/domain/entity/view/position.py +78 -0
- graphcore/domain/entity/view/tests/__init__.py +0 -0
- graphcore/domain/entity/view/tests/test_position.py +71 -0
- graphcore/domain/entity/view/tests/test_view_entity.py +134 -0
- graphcore/domain/strategy/BaseStrategy.py +759 -0
- graphcore/domain/strategy/DeploymentStrategies.py +55 -0
- graphcore/domain/strategy/GrantStrategies.py +189 -0
- graphcore/domain/strategy/GroupStrategies.py +193 -0
- graphcore/domain/strategy/InviteStrategies.py +84 -0
- graphcore/domain/strategy/RequestTemplateStrategies.py +126 -0
- graphcore/domain/strategy/ViewStrategies.py +219 -0
- graphcore/domain/strategy/__init__.py +0 -0
- graphcore/domain/strategy/runner.py +84 -0
- graphcore/domain/strategy/session.py +56 -0
- graphcore/domain/strategy/tests/BaseStrategyTest.py +167 -0
- graphcore/domain/strategy/tests/__init__.py +0 -0
- graphcore/domain/strategy/tests/test_BaseStrategies.py +870 -0
- graphcore/domain/strategy/tests/test_grant_strategies.py +245 -0
- graphcore/domain/strategy/tests/test_group_strategies.py +99 -0
- graphcore/domain/strategy/tests/test_runner.py +220 -0
- graphcore/domain/strategy/tests/test_strategy_locks.py +446 -0
- graphcore/domain/strategy/tests/test_utils.py +95 -0
- graphcore/domain/strategy/tests/test_view_strategies.py +436 -0
- graphcore/errors.py +145 -0
- graphcore/execution/README.md +97 -0
- graphcore/execution/StrategyExecutorService.py +333 -0
- graphcore/execution/__init__.py +0 -0
- graphcore/execution/dispatcher.py +19 -0
- graphcore/execution/environments/__init__.py +0 -0
- graphcore/execution/environments/celery.py +59 -0
- graphcore/execution/environments/in_process.py +55 -0
- graphcore/execution/local_session.py +117 -0
- graphcore/execution/redis_run_events.py +74 -0
- graphcore/execution/requests.py +22 -0
- graphcore/execution/run_events.py +85 -0
- graphcore/execution/strategy_context.py +54 -0
- graphcore/execution/tasks.py +34 -0
- graphcore/execution/tests/__init__.py +5 -0
- graphcore/execution/tests/contract.py +171 -0
- graphcore/execution/tests/support.py +310 -0
- graphcore/execution/tests/test_dispatchers.py +76 -0
- graphcore/execution/tests/test_executor.py +368 -0
- graphcore/execution/tests/test_local_session.py +95 -0
- graphcore/execution/tests/test_reconcile_runs.py +134 -0
- graphcore/execution/tests/test_run_events.py +106 -0
- graphcore/execution/tests/test_strategy_context.py +62 -0
- graphcore/gateway/__init__.py +1 -0
- graphcore/gateway/remote.py +35 -0
- graphcore/gateway/tests/__init__.py +0 -0
- graphcore/gateway/tests/test_remote.py +30 -0
- graphcore/graph/EntityService.py +710 -0
- graphcore/graph/__init__.py +0 -0
- graphcore/graph/entity_cache.py +152 -0
- graphcore/graph/tests/__init__.py +4 -0
- graphcore/graph/tests/test_EntityService.py +908 -0
- graphcore/graph/tests/test_EntityService_list.py +170 -0
- graphcore/graph/tests/test_EntityService_model_resolution.py +339 -0
- graphcore/graph/tests/test_EntityService_referrers.py +236 -0
- graphcore/graph/tests/test_EntityService_routing.py +119 -0
- graphcore/graph/tests/test_dependent_on.py +141 -0
- graphcore/graph/tests/test_engine_is_type_agnostic.py +25 -0
- graphcore/graph/tests/test_engine_resolves_read_permission.py +93 -0
- graphcore/graph/tests/test_entity_cache.py +159 -0
- graphcore/graph/tests/test_null_entity_cache.py +12 -0
- graphcore/management/__init__.py +0 -0
- graphcore/management/commands/__init__.py +0 -0
- graphcore/management/commands/dev_reset.py +18 -0
- graphcore/management/commands/graphcore_createadmin.py +65 -0
- graphcore/management/commands/graphcore_policy.py +59 -0
- graphcore/management/commands/graphcore_status.py +98 -0
- graphcore/management/tests/__init__.py +0 -0
- graphcore/management/tests/test_createadmin.py +78 -0
- graphcore/management/tests/test_policy_command.py +46 -0
- graphcore/management/tests/test_status_command.py +85 -0
- graphcore/migrations/0001_initial.py +186 -0
- graphcore/migrations/0002_invite.py +26 -0
- graphcore/migrations/__init__.py +0 -0
- graphcore/models.py +11 -0
- graphcore/persistence/ENTITY_REPOSITORY_TECHNICAL_TDD.md +396 -0
- graphcore/persistence/__init__.py +6 -0
- graphcore/persistence/django/__init__.py +0 -0
- graphcore/persistence/django/backends.py +43 -0
- graphcore/persistence/django/bindings.py +28 -0
- graphcore/persistence/django/cache.py +38 -0
- graphcore/persistence/django/compiler.py +214 -0
- graphcore/persistence/django/identity.py +18 -0
- graphcore/persistence/django/models.py +252 -0
- graphcore/persistence/django/repository.py +525 -0
- graphcore/persistence/django/tests/__init__.py +0 -0
- graphcore/persistence/django/tests/test_backends.py +68 -0
- graphcore/persistence/django/tests/test_compiler.py +269 -0
- graphcore/persistence/django/tests/test_repository.py +472 -0
- graphcore/persistence/django/tests/test_shared_column.py +120 -0
- graphcore/persistence/django/tests/test_threads.py +50 -0
- graphcore/persistence/django/tests/test_update_shape.py +175 -0
- graphcore/persistence/django/threads.py +38 -0
- graphcore/persistence/repository.py +172 -0
- graphcore/persistence/tests/__init__.py +0 -0
- graphcore/persistence/tests/contract.py +688 -0
- graphcore/persistence/tests/evaluate.py +51 -0
- graphcore/persistence/tests/fixtures.py +32 -0
- graphcore/persistence/tests/memory.py +263 -0
- graphcore/persistence/tests/test_memory_repository.py +10 -0
- graphcore/persistence/tests/test_query_types.py +127 -0
- graphcore/public/__init__.py +9 -0
- graphcore/public/api.py +9 -0
- graphcore/public/apps.py +10 -0
- graphcore/public/auth/__init__.py +1 -0
- graphcore/public/auth/grants.py +7 -0
- graphcore/public/auth/onboarding.py +8 -0
- graphcore/public/auth/perms.py +7 -0
- graphcore/public/auth/policy.py +10 -0
- graphcore/public/auth/roles.py +8 -0
- graphcore/public/auth/subjects.py +7 -0
- graphcore/public/entities/__init__.py +32 -0
- graphcore/public/errors.py +19 -0
- graphcore/public/persistence/__init__.py +44 -0
- graphcore/public/persistence/django.py +24 -0
- graphcore/public/scheduling.py +23 -0
- graphcore/public/strategies/__init__.py +85 -0
- graphcore/public/test_settings.py +16 -0
- graphcore/public/testing.py +47 -0
- graphcore/registry/EntityDirectory.py +246 -0
- graphcore/registry/README.md +147 -0
- graphcore/registry/StrategyDirectory.py +308 -0
- graphcore/registry/TYPE_METADATA_PLACEMENT.md +66 -0
- graphcore/registry/__init__.py +0 -0
- graphcore/registry/app_discovery.py +108 -0
- graphcore/registry/decorators.py +77 -0
- graphcore/registry/discovery.py +111 -0
- graphcore/registry/registry.py +79 -0
- graphcore/registry/tests/__init__.py +0 -0
- graphcore/registry/tests/test_agent_frontend_discovery.py +56 -0
- graphcore/registry/tests/test_app_config.py +36 -0
- graphcore/registry/tests/test_discovery.py +147 -0
- graphcore/registry/tests/test_entity_directory_storage.py +73 -0
- graphcore/registry/tests/test_import_package.py +87 -0
- graphcore/registry/tests/test_principal_flag.py +67 -0
- graphcore/registry/tests/test_strategy_directory.py +29 -0
- graphcore/routes/__init__.py +116 -0
- graphcore/routes/tests/__init__.py +0 -0
- graphcore/routes/tests/test_routes.py +90 -0
- graphcore/runtime.py +243 -0
- graphcore/scheduling/__init__.py +0 -0
- graphcore/scheduling/beat_scheduler.py +164 -0
- graphcore/scheduling/beat_tasks.py +63 -0
- graphcore/scheduling/scheduler.py +37 -0
- graphcore/scheduling/tests/__init__.py +0 -0
- graphcore/scheduling/tests/contract.py +136 -0
- graphcore/scheduling/tests/memory.py +42 -0
- graphcore/scheduling/tests/test_beat_scheduler.py +87 -0
- graphcore/scheduling/tests/test_memory_scheduler.py +15 -0
- graphcore/scheduling/tests/test_ticks.py +143 -0
- graphcore/tasks.py +9 -0
- graphcore/test_settings.py +78 -0
- graphcore/testing/__init__.py +0 -0
- graphcore/testing/case.py +112 -0
- graphcore/testing/runner.py +32 -0
- graphcore/testing/settings.py +26 -0
- graphcore/testing/tests/__init__.py +0 -0
- graphcore/testing/tests/agent_view_fixture.py +19 -0
- graphcore/testing/tests/test_case_and_runner.py +219 -0
- graphcore/testing/tests/test_settings_for_apps.py +50 -0
- graphcore/tests/__init__.py +0 -0
- graphcore/tests/ids.py +14 -0
- graphcore/tests/public_api_manifest.py +102 -0
- graphcore/tests/runs.py +7 -0
- graphcore/tests/test_app_discovery.py +90 -0
- graphcore/tests/test_composition_boundaries.py +231 -0
- graphcore/tests/test_docs_discovery.py +108 -0
- graphcore/tests/test_no_print.py +70 -0
- graphcore/tests/test_packaging.py +64 -0
- graphcore/tests/test_public_api_boundaries.py +229 -0
- graphcore/tests/test_public_api_contract.py +62 -0
- graphcore/tests/test_public_api_docs.py +28 -0
- graphcore/tests/test_runtime.py +42 -0
- graphcore/tests/test_shared_type_boundaries.py +128 -0
- graphcore/tests/test_shared_types.py +144 -0
- graphcore/tests/test_test_settings.py +45 -0
- graphcore/transport/__init__.py +0 -0
- graphcore/transport/agent_asgi.py +68 -0
- graphcore/transport/agent_client.py +85 -0
- graphcore/transport/channel_layer.py +68 -0
- graphcore/transport/consumers.py +507 -0
- graphcore/transport/routing.py +6 -0
- graphcore/transport/tests/__init__.py +0 -0
- graphcore/transport/tests/test_agent_client.py +31 -0
- graphcore/transport/tests/test_api_views.py +733 -0
- graphcore/transport/tests/test_channel_layer.py +96 -0
- graphcore/transport/tests/test_consumers.py +963 -0
- graphcore/transport/tests/test_resource_metadata.py +67 -0
- graphcore/transport/urls.py +29 -0
- graphcore/transport/views.py +358 -0
- graphcore/types/__init__.py +4 -0
- graphcore/types/access.py +114 -0
- graphcore/types/api.py +76 -0
- graphcore/types/entities.py +100 -0
- graphcore/types/entity_id.py +85 -0
- graphcore/types/execution.py +41 -0
- graphcore/types/identity.py +20 -0
- graphcore/types/onboarding.py +20 -0
- graphcore/types/query.py +116 -0
- graphcore/types/schedules.py +91 -0
- graphcore/types/strategy.py +73 -0
- graphcore/types/tests/__init__.py +0 -0
- graphcore/types/tests/test_entity_id.py +50 -0
- graphcore/urls.py +5 -0
- graphcore_kernel-0.1.0.dist-info/METADATA +94 -0
- graphcore_kernel-0.1.0.dist-info/RECORD +343 -0
- graphcore_kernel-0.1.0.dist-info/WHEEL +5 -0
- graphcore_kernel-0.1.0.dist-info/entry_points.txt +2 -0
- graphcore_kernel-0.1.0.dist-info/licenses/LICENSE +202 -0
- graphcore_kernel-0.1.0.dist-info/licenses/NOTICE +2 -0
- graphcore_kernel-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
"""An update writes the base row with the version swap and each typed table
|
|
2
|
+
once, for every storage shape: plain `EntityModel`, a typed child, and a
|
|
3
|
+
two-level child. A stale version or a missing typed row changes nothing.
|
|
4
|
+
"""
|
|
5
|
+
from asgiref.sync import async_to_sync
|
|
6
|
+
from django.apps import apps
|
|
7
|
+
from django.db import IntegrityError, connection, models
|
|
8
|
+
from django.test import TestCase
|
|
9
|
+
from django.test.utils import CaptureQueriesContext
|
|
10
|
+
|
|
11
|
+
from graphcore.domain.entity.Entity import Entity
|
|
12
|
+
from graphcore.errors import ConcurrentModificationError
|
|
13
|
+
from graphcore.persistence.django.models import EntityModel
|
|
14
|
+
from graphcore.persistence.django.repository import DjangoEntityRepository
|
|
15
|
+
from graphcore.persistence.tests.fixtures import make
|
|
16
|
+
from graphcore.registry.registry import DEFAULT_REGISTRY
|
|
17
|
+
|
|
18
|
+
_META = {'Meta': type('Meta', (), {'app_label': 'graphcore'}), '__module__': __name__}
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class ShapeDoc(Entity):
|
|
22
|
+
def __init__(self, entity_id):
|
|
23
|
+
super().__init__(entity_id)
|
|
24
|
+
self.title = None
|
|
25
|
+
|
|
26
|
+
def to_dict(self):
|
|
27
|
+
return {**super().to_dict(), 'title': self.title}
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class ShapeLeaf(ShapeDoc):
|
|
31
|
+
def __init__(self, entity_id):
|
|
32
|
+
super().__init__(entity_id)
|
|
33
|
+
self.body = None
|
|
34
|
+
|
|
35
|
+
def to_dict(self):
|
|
36
|
+
return {**super().to_dict(), 'body': self.body}
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def _statements(captured, verb):
|
|
40
|
+
return [q['sql'] for q in captured.captured_queries if q['sql'].lstrip().upper().startswith(verb)]
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
class UpdateShapeTests(TestCase):
|
|
44
|
+
@classmethod
|
|
45
|
+
def setUpClass(cls):
|
|
46
|
+
# DDL before the class-wide transaction opens: SQLite's schema
|
|
47
|
+
# editor refuses to run inside one.
|
|
48
|
+
cls.doc_model = type('ShapeDocModel', (EntityModel,), {
|
|
49
|
+
**_META, 'title': models.CharField(max_length=50, null=True),
|
|
50
|
+
})
|
|
51
|
+
cls.leaf_model = type('ShapeLeafModel', (cls.doc_model,), {
|
|
52
|
+
**_META, 'body': models.TextField(null=True),
|
|
53
|
+
})
|
|
54
|
+
with connection.schema_editor() as editor:
|
|
55
|
+
editor.create_model(cls.doc_model)
|
|
56
|
+
editor.create_model(cls.leaf_model)
|
|
57
|
+
super().setUpClass()
|
|
58
|
+
|
|
59
|
+
@classmethod
|
|
60
|
+
def tearDownClass(cls):
|
|
61
|
+
super().tearDownClass()
|
|
62
|
+
with connection.schema_editor() as editor:
|
|
63
|
+
editor.delete_model(cls.leaf_model)
|
|
64
|
+
editor.delete_model(cls.doc_model)
|
|
65
|
+
delattr(cls.doc_model, cls.leaf_model._meta.model_name)
|
|
66
|
+
delattr(EntityModel, cls.doc_model._meta.model_name)
|
|
67
|
+
for model in (cls.leaf_model, cls.doc_model):
|
|
68
|
+
del apps.all_models['graphcore'][model._meta.model_name]
|
|
69
|
+
apps.clear_cache()
|
|
70
|
+
|
|
71
|
+
def setUp(self):
|
|
72
|
+
self.registry = DEFAULT_REGISTRY.fork_open()
|
|
73
|
+
for entity_cls, model in ((ShapeDoc, self.doc_model), (ShapeLeaf, self.leaf_model)):
|
|
74
|
+
self.registry.entities.register_entity(entity_cls, app_label='shapetest')
|
|
75
|
+
self.registry.entities.bind_storage(entity_cls, model)
|
|
76
|
+
self.repository = DjangoEntityRepository(self.registry.entities)
|
|
77
|
+
|
|
78
|
+
def _created(self, entity_cls, **fields):
|
|
79
|
+
entity = make(entity_cls, self.registry)
|
|
80
|
+
for name, value in fields.items():
|
|
81
|
+
setattr(entity, name, value)
|
|
82
|
+
self._save(entity)
|
|
83
|
+
return entity
|
|
84
|
+
|
|
85
|
+
def _save(self, entity, **kwargs):
|
|
86
|
+
with CaptureQueriesContext(connection) as captured:
|
|
87
|
+
async_to_sync(self.repository.save)(entity, **kwargs)
|
|
88
|
+
return captured
|
|
89
|
+
|
|
90
|
+
def _base(self, entity):
|
|
91
|
+
return EntityModel.objects.values('version', 'attributes').get(entity_id=entity.entity_id)
|
|
92
|
+
|
|
93
|
+
def test_a_plain_entity_is_one_update(self):
|
|
94
|
+
entity = self._created(Entity)
|
|
95
|
+
entity.set_attribute('color', 'red')
|
|
96
|
+
|
|
97
|
+
captured = self._save(entity)
|
|
98
|
+
|
|
99
|
+
self.assertEqual(len(_statements(captured, 'UPDATE')), 1)
|
|
100
|
+
self.assertEqual(len(_statements(captured, 'SELECT')), 1)
|
|
101
|
+
self.assertEqual(self._base(entity), {'version': 2, 'attributes': {'color': 'red'}})
|
|
102
|
+
|
|
103
|
+
def test_a_supplied_previous_is_not_read_again(self):
|
|
104
|
+
doc = self._created(ShapeDoc, title='first')
|
|
105
|
+
previous = async_to_sync(self.repository.load)(doc.entity_id)
|
|
106
|
+
doc.title = 'second'
|
|
107
|
+
|
|
108
|
+
captured = self._save(doc, previous=previous)
|
|
109
|
+
|
|
110
|
+
self.assertEqual(len(_statements(captured, 'SELECT')), 0)
|
|
111
|
+
self.assertEqual(len(_statements(captured, 'UPDATE')), 2)
|
|
112
|
+
self.assertEqual(self.doc_model.objects.get(entity_id=doc.entity_id).title, 'second')
|
|
113
|
+
|
|
114
|
+
def test_a_create_without_a_previous_only_inserts(self):
|
|
115
|
+
doc = make(ShapeDoc, self.registry)
|
|
116
|
+
doc.title = 'first'
|
|
117
|
+
|
|
118
|
+
captured = self._save(doc, previous=None)
|
|
119
|
+
|
|
120
|
+
self.assertEqual(len(_statements(captured, 'SELECT')), 0)
|
|
121
|
+
self.assertEqual(len(_statements(captured, 'UPDATE')), 0)
|
|
122
|
+
self.assertEqual(len(_statements(captured, 'INSERT')), 2)
|
|
123
|
+
|
|
124
|
+
def test_a_typed_entity_is_one_update_per_table(self):
|
|
125
|
+
doc = self._created(ShapeDoc, title='first')
|
|
126
|
+
doc.title = 'second'
|
|
127
|
+
doc.set_attribute('color', 'blue')
|
|
128
|
+
|
|
129
|
+
captured = self._save(doc)
|
|
130
|
+
|
|
131
|
+
self.assertEqual(len(_statements(captured, 'UPDATE')), 2)
|
|
132
|
+
self.assertEqual(len(_statements(captured, 'SELECT')), 1)
|
|
133
|
+
self.assertEqual(self._base(doc), {'version': 2, 'attributes': {'color': 'blue'}})
|
|
134
|
+
self.assertEqual(self.doc_model.objects.get(entity_id=doc.entity_id).title, 'second')
|
|
135
|
+
|
|
136
|
+
def test_a_two_level_entity_writes_every_table(self):
|
|
137
|
+
leaf = self._created(ShapeLeaf, title='first', body='one')
|
|
138
|
+
leaf.title = 'second'
|
|
139
|
+
leaf.body = 'two'
|
|
140
|
+
leaf.set_attribute('color', 'green')
|
|
141
|
+
|
|
142
|
+
self._save(leaf)
|
|
143
|
+
|
|
144
|
+
self.assertEqual(self._base(leaf), {'version': 2, 'attributes': {'color': 'green'}})
|
|
145
|
+
row = self.leaf_model.objects.get(entity_id=leaf.entity_id)
|
|
146
|
+
self.assertEqual((row.title, row.body), ('second', 'two'))
|
|
147
|
+
|
|
148
|
+
def test_a_stale_version_changes_no_table(self):
|
|
149
|
+
doc = self._created(ShapeDoc, title='first')
|
|
150
|
+
stale = async_to_sync(self.repository.load)(doc.entity_id)
|
|
151
|
+
doc.title = 'second'
|
|
152
|
+
self._save(doc)
|
|
153
|
+
|
|
154
|
+
stale.title = 'stale'
|
|
155
|
+
stale.set_attribute('color', 'stale')
|
|
156
|
+
with self.assertRaises(ConcurrentModificationError):
|
|
157
|
+
self._save(stale)
|
|
158
|
+
|
|
159
|
+
self.assertEqual(self._base(doc), {'version': 2, 'attributes': {}})
|
|
160
|
+
self.assertEqual(self.doc_model.objects.get(entity_id=doc.entity_id).title, 'second')
|
|
161
|
+
|
|
162
|
+
def test_a_missing_typed_row_rolls_back_the_base_write(self):
|
|
163
|
+
doc = self._created(ShapeDoc, title='first')
|
|
164
|
+
previous = async_to_sync(self.repository.load)(doc.entity_id)
|
|
165
|
+
with connection.cursor() as cursor:
|
|
166
|
+
cursor.execute(
|
|
167
|
+
f'DELETE FROM {self.doc_model._meta.db_table} WHERE entitymodel_ptr_id = %s',
|
|
168
|
+
[doc.entity_id],
|
|
169
|
+
)
|
|
170
|
+
doc.set_attribute('color', 'orphan')
|
|
171
|
+
|
|
172
|
+
with self.assertRaises(IntegrityError):
|
|
173
|
+
async_to_sync(self.repository.save)(doc, previous=previous)
|
|
174
|
+
|
|
175
|
+
self.assertEqual(self._base(doc), {'version': 1, 'attributes': {}})
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"""The process's shared database threads.
|
|
2
|
+
|
|
3
|
+
Django's async ORM runs every query through `sync_to_async(thread_sensitive=
|
|
4
|
+
True)`: outside a `ThreadSensitiveContext` that is one thread for the whole
|
|
5
|
+
process, and a context per task would hold a connection for the task's whole
|
|
6
|
+
life. A task that calls `use_shared_threads` sends each query to one of a few
|
|
7
|
+
threads instead, each with its own pooled connection. On a loop that
|
|
8
|
+
`async_to_sync` runs (Celery tasks, the test runner) the waiting sync thread
|
|
9
|
+
keeps running them, as it would without this module.
|
|
10
|
+
|
|
11
|
+
Two awaited queries of one task may run on different connections, so a
|
|
12
|
+
transaction or an `aiterator()` stays inside one sync function.
|
|
13
|
+
"""
|
|
14
|
+
import asyncio
|
|
15
|
+
from concurrent.futures import ThreadPoolExecutor
|
|
16
|
+
|
|
17
|
+
from asgiref.sync import AsyncToSync, SyncToAsync, ThreadSensitiveContext
|
|
18
|
+
|
|
19
|
+
_contexts: dict[int, ThreadSensitiveContext] = {}
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def _shared_context(threads: int) -> ThreadSensitiveContext:
|
|
23
|
+
if threads not in _contexts:
|
|
24
|
+
context = ThreadSensitiveContext()
|
|
25
|
+
# asgiref creates a one-thread executor only for a context it finds no executor for.
|
|
26
|
+
SyncToAsync.context_to_thread_executor[context] = ThreadPoolExecutor(
|
|
27
|
+
max_workers=threads, thread_name_prefix='graphcore-db',
|
|
28
|
+
)
|
|
29
|
+
_contexts[threads] = context
|
|
30
|
+
return _contexts[threads]
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def use_shared_threads(threads: int) -> None:
|
|
34
|
+
"""Send the calling task's thread-sensitive work to `threads` shared
|
|
35
|
+
threads, for the rest of the task."""
|
|
36
|
+
if asyncio.get_running_loop() in AsyncToSync.loop_thread_executors:
|
|
37
|
+
return
|
|
38
|
+
SyncToAsync.thread_sensitive_context.set(_shared_context(threads))
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
"""The entity repository contract.
|
|
2
|
+
|
|
3
|
+
Backend-neutral: it speaks `Entity`, `Predicate`, and ids, and knows
|
|
4
|
+
nothing about SQL, Django, or the shape of a stored row. The two receipts
|
|
5
|
+
below are what a write hands back to `EntityService` so it can run the
|
|
6
|
+
cache/broadcast/access fallout without re-reading storage.
|
|
7
|
+
"""
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import json
|
|
11
|
+
from abc import ABC, abstractmethod
|
|
12
|
+
from dataclasses import dataclass
|
|
13
|
+
from typing import Optional
|
|
14
|
+
|
|
15
|
+
from graphcore.domain.entity.Entity import Entity
|
|
16
|
+
from graphcore.types.entities import EntityPage
|
|
17
|
+
from graphcore.types.entity_id import find_entity_ids
|
|
18
|
+
from graphcore.types.query import EntityQuery, PageRequest, Predicate
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
# `save(previous=...)` default: the caller has not read the stored entity.
|
|
22
|
+
NOT_LOADED = object()
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def project_reference_ids(record: dict) -> list[str]:
|
|
26
|
+
"""The `reference_ids` projection of a `to_dict()` record: every
|
|
27
|
+
identifier held in any field, other than the entity's own. Both adapters
|
|
28
|
+
compute it here so they cannot disagree about what a reference is."""
|
|
29
|
+
own_id = record.get('entity_id')
|
|
30
|
+
return [eid for eid in find_entity_ids(json.dumps(record, default=str)) if eid != own_id]
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
@dataclass(frozen=True)
|
|
34
|
+
class Precondition:
|
|
35
|
+
"""A write takes effect only while the entity `entity_id` matches
|
|
36
|
+
`where`. The check and the write are atomic: the precondition entity
|
|
37
|
+
cannot change between them."""
|
|
38
|
+
|
|
39
|
+
entity_id: str
|
|
40
|
+
where: Predicate
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
@dataclass(frozen=True)
|
|
44
|
+
class SaveReceipt:
|
|
45
|
+
"""What a committed save changed. `previous` is the entity exactly as it
|
|
46
|
+
stood before this write (None for a create).
|
|
47
|
+
|
|
48
|
+
The two id sets are entities this write changed *without* bumping their
|
|
49
|
+
version, so nothing else can notice: `touched_parent_ids` are the parents
|
|
50
|
+
whose `children_ids` projection moved, `moved_descendant_ids` the subtree
|
|
51
|
+
whose `ancestor_ids` a reparent rewrote. Both must be dropped from any
|
|
52
|
+
payload cache, or a reader keeps being served the pre-move projection.
|
|
53
|
+
"""
|
|
54
|
+
|
|
55
|
+
entity: Entity
|
|
56
|
+
previous: Optional[Entity]
|
|
57
|
+
created: bool
|
|
58
|
+
touched_parent_ids: frozenset
|
|
59
|
+
moved_descendant_ids: frozenset = frozenset()
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
@dataclass(frozen=True)
|
|
63
|
+
class DeletedEntity:
|
|
64
|
+
"""What an entity was, after its row is gone - read before the delete so
|
|
65
|
+
tombstones and access fallout can still be routed. Never a hydrated
|
|
66
|
+
`Entity`: the thing it describes no longer exists."""
|
|
67
|
+
|
|
68
|
+
entity_id: str
|
|
69
|
+
parent_id: Optional[str]
|
|
70
|
+
entity_type_id: Optional[str]
|
|
71
|
+
owner: Optional[str]
|
|
72
|
+
ancestor_ids: tuple
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
@dataclass(frozen=True)
|
|
76
|
+
class DeleteReceipt:
|
|
77
|
+
"""One `delete_many` call: the `DeletedEntity` for every id that existed.
|
|
78
|
+
Ids that named nothing are simply absent, so this is a truthful answer to
|
|
79
|
+
"what did this delete", not an echo of what was asked for."""
|
|
80
|
+
|
|
81
|
+
entities: tuple
|
|
82
|
+
|
|
83
|
+
@property
|
|
84
|
+
def entity_ids(self) -> list[str]:
|
|
85
|
+
return [entity.entity_id for entity in self.entities]
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
class EntityRepository(ABC):
|
|
89
|
+
"""Storage for the entity collection.
|
|
90
|
+
|
|
91
|
+
The repository owns the `ancestor_ids` invariant:
|
|
92
|
+
`ancestor_ids(e) == ancestor_ids(parent) + [parent_id]`, recomputed on
|
|
93
|
+
create and on every `parent_id` change for the whole subtree, without
|
|
94
|
+
bumping descendants' `version`. It owns the `reference_ids` projection
|
|
95
|
+
the same way, derived from every field of the entity on every save
|
|
96
|
+
(`project_reference_ids`), and the reverse index behind `referrers`,
|
|
97
|
+
whose storage is the adapter's choice. It also holds the store's own identity: identifiers are opaque strings to
|
|
98
|
+
it, parsed and routed nowhere below this seam.
|
|
99
|
+
"""
|
|
100
|
+
|
|
101
|
+
@abstractmethod
|
|
102
|
+
def backend_id(self) -> str:
|
|
103
|
+
"""The immutable identity of the store this repository fronts.
|
|
104
|
+
|
|
105
|
+
Synchronous: read once at composition, never on a request path."""
|
|
106
|
+
|
|
107
|
+
def use_shared_threads(self) -> None:
|
|
108
|
+
"""Run the calling task's storage work on threads shared across the
|
|
109
|
+
process's tasks, for a store whose driver needs them. Called at the
|
|
110
|
+
start of each WebSocket connection and in-process run."""
|
|
111
|
+
|
|
112
|
+
@abstractmethod
|
|
113
|
+
async def load(self, entity_id: str) -> Optional[Entity]:
|
|
114
|
+
"""One entity, fully hydrated (including `children_ids`), or None."""
|
|
115
|
+
|
|
116
|
+
@abstractmethod
|
|
117
|
+
async def load_many(self, entity_ids) -> dict:
|
|
118
|
+
"""`{entity_id: Entity}` for the ids that exist."""
|
|
119
|
+
|
|
120
|
+
@abstractmethod
|
|
121
|
+
async def versions(self, entity_ids) -> dict:
|
|
122
|
+
"""`{entity_id: version}` for the ids that exist - existence check
|
|
123
|
+
and optimistic-lock probe without hydrating anything."""
|
|
124
|
+
|
|
125
|
+
@abstractmethod
|
|
126
|
+
async def list_entities(self, query: EntityQuery) -> EntityPage:
|
|
127
|
+
"""One keyset page of hydrated entities matching `query.predicate`,
|
|
128
|
+
ordered by `entity_id`. Filtering happens in storage, before the
|
|
129
|
+
limit - never by discarding rows from a fetched page."""
|
|
130
|
+
|
|
131
|
+
@abstractmethod
|
|
132
|
+
async def list_ids(self, predicate: Predicate) -> set:
|
|
133
|
+
"""Every entity id matching `predicate`. Unpaged by design: callers
|
|
134
|
+
use it for bounded sets (a principal's groups, granted scopes)."""
|
|
135
|
+
|
|
136
|
+
@abstractmethod
|
|
137
|
+
async def descendant_ids(self, root_id: str) -> set:
|
|
138
|
+
"""Every id contained by `root_id`, at any depth, excluding it."""
|
|
139
|
+
|
|
140
|
+
@abstractmethod
|
|
141
|
+
async def dependent_ids(self, type_ids: frozenset, field: str, target_ids: set) -> set:
|
|
142
|
+
"""Ids of entities of `type_ids` whose `field` - a typed column
|
|
143
|
+
holding an identifier or a list of identifiers - names any of
|
|
144
|
+
`target_ids`. Unauthorized: it backs `dependent_on` deletion."""
|
|
145
|
+
|
|
146
|
+
@abstractmethod
|
|
147
|
+
async def referrers(self, target_id: str, predicate: Predicate, page: PageRequest) -> EntityPage:
|
|
148
|
+
"""One keyset page of hydrated entities whose `reference_ids` hold
|
|
149
|
+
`target_id`, ordered by `entity_id`. `predicate` narrows the match
|
|
150
|
+
in the same statement, before the limit - pages are never short
|
|
151
|
+
because of filtering done afterwards."""
|
|
152
|
+
|
|
153
|
+
@abstractmethod
|
|
154
|
+
async def save(
|
|
155
|
+
self, entity: Entity, precondition: Optional[Precondition] = None, previous=NOT_LOADED,
|
|
156
|
+
) -> SaveReceipt:
|
|
157
|
+
"""Persist `entity` under a compare-and-swap on its `version`,
|
|
158
|
+
maintaining `ancestor_ids`. Raises `ConcurrentModificationError` when
|
|
159
|
+
the stored version has moved on, `PreconditionFailed` when
|
|
160
|
+
`precondition` does not hold, and `DuplicateEntity` when creating a
|
|
161
|
+
`User` whose `(issuer, subject)` another entity already holds.
|
|
162
|
+
|
|
163
|
+
`previous` is the entity as the caller already loaded it at
|
|
164
|
+
`entity.version`, or None when the caller knows this is a create; the
|
|
165
|
+
store then need not read it again. A `previous` at another version
|
|
166
|
+
is ignored. It becomes the receipt's `previous`."""
|
|
167
|
+
|
|
168
|
+
@abstractmethod
|
|
169
|
+
async def delete_many(self, entity_ids, precondition: Optional[Precondition] = None) -> DeleteReceipt:
|
|
170
|
+
"""Delete the ids that exist, returning what they were. Raises
|
|
171
|
+
`PreconditionFailed`, deleting nothing, when `precondition` does not
|
|
172
|
+
hold."""
|
|
File without changes
|