Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Node read optimization #1699

Merged
merged 2 commits into from
Feb 12, 2025
Merged

Conversation

lauckhart
Copy link
Collaborator

This offers a new path for performing attribute reads against the Node API. As discussed it is NOT fully wired yet so we can discuss first, but this PR should be ready for merge.

There's significant new code here. Some of this is noise, which I detail below, but here are the main bits of new functionality to focus on:

  1. packages/protocol/src/action/** - this is a new API for performing interactions that is designed for simplicity and performance:
    • action/protocols.ts - these define a ProtocolNode JS contract for exposing a Matter data model in a form that efficiently maps to the wire protocol
    • action/server/AttributeResponse.ts - implements the attribute subset of a Read Request Action against ProtocolNode
    • action/Interactable.ts and request/* - an API I created to simplify construction of Matter actions. The goal was to make it easy to initiate Matter interactions for e.g. batch purposes, but with a more convenient JS API. Much of this is stubbed due to focus on read in this commit but I do use some of the type definitions
  2. packages/node/src/node/server/ProtocolService.ts - implements ProtocolNode for a @matter/node Node
  3. packages/behavior/state/managed/values/StructManager.ts - I updated struct manager to generate ID-based getters and setters. This means that where state values are associated with a Matter element that has an ID you can use state[id] and state.name interchangeably. I had done this to support attributes that are not in the schema but it also allows us to map protocol queries against state objects without performing name lookup

It became apparent as I was working through this that the code was going to be too @matter/node specific unless I performed some code reorganization. Unfortunately that adds noise to the commit but I think it's worthwhile.

Moved modules include:

  • node/src/behavior/state/transaction -> general/src/transaction
  • node/src/behavior/state/Val.ts -> protocol/src/action
  • node/src/behavior/errors.ts -> protocol/src/action
  • node/src/behavior/AccessControl.ts -> protocol/src/action/server

Also unfortunate... I encountered a Mocha bug while developing this that indicated there were import cycles. I added a "matter-build cycles" command to detect cycles and fixed all cycles in the node package, but this ended up changing a bunch of extra files that are now hard to disentangle from the commit.

Copy link

semanticdiff-com bot commented Feb 10, 2025

Review changes with  SemanticDiff

Changed Files
File Status
  packages/model/package.json  100% smaller
  packages/tools/src/util/bootstrap.mjs  89% smaller
  packages/node/src/behaviors/access-control/AccessControlServer.ts  78% smaller
  packages/tools/package.json  75% smaller
  packages/node/test/behaviors/switch/SwitchServerTest.ts  68% smaller
  packages/matter.js/src/device/CachedClientNodeStore.ts  65% smaller
  packages/general/src/transaction/ResourceSet.ts  64% smaller
  packages/node/test/behavior/state/managed/values/StructManagerTest.ts  64% smaller
  packages/nodejs/src/behavior/instrumentation.ts  63% smaller
  packages/matter.js/src/device/ClientEndpointStore.ts  63% smaller
  packages/node/src/behavior/context/server/OfflineContext.ts  57% smaller
  packages/node/test/behavior/state/managed/DatasourceTest.ts  53% smaller
  package-lock.json  53% smaller
  packages/node/src/behavior/supervision/RootSupervisor.ts  51% smaller
  packages/node/src/behavior/context/server/OnlineContext.ts  43% smaller
  packages/node/src/behavior/state/validation/assertions.ts  41% smaller
  packages/node/src/behavior/supervision/BehaviorSupervisor.ts  38% smaller
  packages/node/src/behavior/supervision/ValueSupervisor.ts  38% smaller
  packages/node/src/behavior/internal/Reactors.ts  37% smaller
  packages/node/src/behavior/state/validation/conformance-util.ts  36% smaller
  packages/types/src/globals/Status.ts  34% smaller
  packages/general/src/transaction/Tx.ts  31% smaller
  packages/node/src/behavior/state/managed/NameResolver.ts  30% smaller
  packages/node/src/endpoint/storage/DatasourceStore.ts  29% smaller
  packages/tools/src/util/package.ts  27% smaller
  packages/node/src/behavior/state/managed/ManagedReference.ts  27% smaller
  packages/general/src/transaction/Transaction.ts  27% smaller
  packages/node/src/behavior/state/managed/Datasource.ts  26% smaller
  packages/node/src/behavior/state/validation/conformance-compiler.ts  26% smaller
  packages/node/src/node/server/TransactionalInteractionServer.ts  25% smaller
  packages/node/src/behavior/state/managed/values/StructManager.ts  25% smaller
  packages/protocol/src/action/Val.ts  21% smaller
  packages/node/src/behavior/system/controller/discovery/ActiveDiscoveries.ts  21% smaller
  packages/node/src/behavior/system/controller/discovery/CommissioningDiscovery.ts  21% smaller
  packages/node/src/behavior/context/server/ContextAgents.ts  20% smaller
  packages/node/src/behavior/internal/BackingEvents.ts  20% smaller
  packages/node/src/behavior/system/controller/discovery/ContinuousDiscovery.ts  20% smaller
  packages/node/src/behavior/system/controller/discovery/Discovery.ts  20% smaller
  packages/node/src/behavior/system/network/NetworkServer.ts  20% smaller
  packages/node/src/behavior/system/network/ServerNetworkRuntime.ts  20% smaller
  packages/node/src/behaviors/descriptor/DescriptorServer.ts  20% smaller
  packages/node/src/endpoint/Agent.ts  20% smaller
  packages/node/src/node/client/NodePeerStore.ts  20% smaller
  packages/node/src/node/server/IdentityService.ts  20% smaller
  packages/node/src/node/server/ServerEnvironment.ts  20% smaller
  packages/node/src/behavior/system/controller/discovery/InstanceDiscovery.ts  20% smaller
  packages/protocol/src/interaction/InteractionServer.ts  20% smaller
  packages/node/src/behavior/state/validation/constraint.ts  19% smaller
  packages/node/test/behavior/state/managed/values/value-utils.ts  19% smaller
  packages/general/src/transaction/errors.ts  19% smaller
  packages/protocol/src/action/server/AccessControl.ts  18% smaller
  packages/general/test/transaction/TransactionTest.ts  15% smaller
  packages/node/src/behavior/internal/BehaviorBacking.ts  14% smaller
  packages/node/src/behavior/state/managed/values/ValuePatcher.ts  14% smaller
  packages/node/src/node/Node.ts  12% smaller
  packages/node/src/endpoint/properties/Behaviors.ts  11% smaller
  packages/node/src/behavior/state/managed/values/ValueCaster.ts  8% smaller
  packages/node/test/endpoints/BridgeTest.ts  7% smaller
  packages/node/src/behavior/state/managed/values/BitmapManager.ts  6% smaller
  packages/node/src/behavior/Reactor.ts  5% smaller
  packages/node/src/node/ServerNode.ts  5% smaller
  packages/node/src/behavior/system/commissioning/CommissioningServer.ts  2% smaller
  packages/node/src/behavior/state/managed/Internal.ts  2% smaller
  packages/protocol/src/action/errors.ts  2% smaller
  packages/node/src/behavior/Behavior.ts  1% smaller
  packages/node/test/behavior/state/managed/values/ListManagerTest.ts  1% smaller
  codegen/src/generate-forwards.ts  0% smaller
  packages/general/src/MatterError.ts  0% smaller
  packages/general/src/index.ts  0% smaller
  packages/general/src/log/Logger.ts  0% smaller
  packages/general/src/time/Time.ts  0% smaller
  packages/general/src/transaction/Participant.ts  0% smaller
  packages/general/src/transaction/Resource.ts  0% smaller
  packages/general/src/transaction/Status.ts  0% smaller
  packages/general/src/transaction/index.ts  0% smaller
  packages/general/src/util/Cancelable.ts  0% smaller
  packages/general/src/util/Observable.ts  0% smaller
  packages/general/src/util/Promises.ts  0% smaller
  packages/general/src/util/index.ts  0% smaller
  packages/matter.js/src/compat/behavior.ts  0% smaller
  packages/matter.js/src/device/LegacyInteractionServer.ts  0% smaller
  packages/model/src/common/Schema.ts  0% smaller
  packages/model/src/common/index.ts  0% smaller
  packages/model/src/models/Model.ts  0% smaller
  packages/node/src/behavior/cluster/ClusterBehavior.ts  0% smaller
  packages/node/src/behavior/cluster/ClusterBehaviorCache.ts  0% smaller
  packages/node/src/behavior/cluster/ClusterBehaviorUtil.ts  0% smaller
  packages/node/src/behavior/context/ActionTracer.ts  0% smaller
  packages/node/src/behavior/index.ts  0% smaller
  packages/node/src/behavior/internal/ServerBehaviorBacking.ts  0% smaller
  packages/node/src/behavior/state/StateType.ts  0% smaller
  packages/node/src/behavior/state/index.ts  0% smaller
  packages/node/src/behavior/state/managed/Instrumentation.ts  0% smaller
  packages/node/src/behavior/state/managed/values/ListManager.ts  0% smaller
  packages/node/src/behavior/state/managed/values/ValueManager.ts  0% smaller
  packages/node/src/behavior/state/transaction/index.ts  0% smaller
  packages/node/src/behavior/state/validation/ValueValidator.ts  0% smaller
  packages/node/src/behavior/state/validation/location.ts  0% smaller
  packages/node/src/behavior/supervision/Schema.ts  0% smaller
  packages/node/src/behavior/supervision/index.ts  0% smaller
  packages/node/src/behavior/system/index/IndexBehavior.ts  0% smaller
  packages/node/src/behaviors/general-diagnostics/GeneralDiagnosticsServer.ts  0% smaller
  packages/node/src/behaviors/operational-credentials/OperationalCredentialsServer.ts  0% smaller
  packages/node/src/endpoint/Endpoint.ts  0% smaller
  packages/node/src/endpoint/properties/EndpointLifecycle.ts Unsupported file format
  packages/node/src/endpoint/server/BehaviorServer.ts  0% smaller
  packages/node/src/endpoint/storage/EndpointStore.ts  0% smaller
  packages/node/src/node/ClientNode.ts  0% smaller
  packages/node/src/node/action/InvokeRequestAction.ts  0% smaller
  packages/node/src/node/action/InvokeResponseAction.ts  0% smaller
  packages/node/src/node/action/ReadRequestAction.ts  0% smaller
  packages/node/src/node/action/ReportDataAction.ts  0% smaller
  packages/node/src/node/action/StatusResponseAction.ts  0% smaller
  packages/node/src/node/action/SubscribeRequestAction.ts  0% smaller
  packages/node/src/node/action/SubscribeResponseAction.ts  0% smaller
  packages/node/src/node/action/TimedRequestAction.ts  0% smaller
  packages/node/src/node/action/WriteRequestAction.ts  0% smaller
  packages/node/src/node/action/WriteResponseAction.ts  0% smaller
  packages/node/src/node/client/ClientNodes.ts  0% smaller
  packages/node/src/node/paths/EventPath.ts  0% smaller
  packages/node/src/node/server/ProtocolService.ts  0% smaller
  packages/node/test/behavior/state/validation/conformanceTest.ts  0% smaller
  packages/node/test/behavior/state/validation/constraintTest.ts  0% smaller
  packages/node/test/behaviors/time-format-localization/TimeFormatLocalizationServerTest.ts  0% smaller
  packages/node/test/endpoint/server/BehaviorServerTest.ts  0% smaller
  packages/node/test/endpoints/LargeBridgeTest.ts  0% smaller
  packages/node/test/endpoints/bridge-helpers.ts  0% smaller
  packages/node/test/node/AttributeResponseTest.ts  0% smaller
  packages/node/test/node/ServerNodeTest.ts  0% smaller
  packages/node/test/node/mock-node.ts  0% smaller
  packages/node/test/node/mock-server-node.ts  0% smaller
  packages/node/test/node/node-helpers.ts  0% smaller
  packages/protocol/src/action/Interactable.ts  0% smaller
  packages/protocol/src/action/index.ts  0% smaller
  packages/protocol/src/action/protocols.ts  0% smaller
  packages/protocol/src/action/request/Invoke.ts  0% smaller
  packages/protocol/src/action/request/MalformedRequestError.ts  0% smaller
  packages/protocol/src/action/request/Read.ts  0% smaller
  packages/protocol/src/action/request/Specifier.ts  0% smaller
  packages/protocol/src/action/request/Subscribe.ts  0% smaller
  packages/protocol/src/action/request/Write.ts  0% smaller
  packages/protocol/src/action/request/index.ts  0% smaller
  packages/protocol/src/action/response/InvokeResult.ts  0% smaller
  packages/protocol/src/action/response/ReadResult.ts  0% smaller
  packages/protocol/src/action/response/SubscribeResult.ts  0% smaller
  packages/protocol/src/action/response/WriteResult.ts  0% smaller
  packages/protocol/src/action/response/index.ts  0% smaller
  packages/protocol/src/action/server/AttributeResponse.ts  0% smaller
  packages/protocol/src/action/server/ServerInteraction.ts  0% smaller
  packages/protocol/src/action/server/index.ts  0% smaller
  packages/protocol/src/index.ts  0% smaller
  packages/protocol/src/interaction/AccessControlManager.ts  0% smaller
  packages/protocol/src/interaction/ServerSubscription.ts  0% smaller
  packages/testing/src/cli.ts  0% smaller
  packages/tools/src/building/builder.ts  0% smaller
  packages/tools/src/building/cli.ts  0% smaller
  packages/tools/src/building/cycles.ts  0% smaller
  packages/tools/src/building/graph.ts  0% smaller
  packages/tools/src/building/project-builder.ts  0% smaller
  packages/tools/src/util/import-aliases.ts  0% smaller
  packages/tools/src/util/progress.ts  0% smaller
  packages/tools/src/versioning/cli.ts  0% smaller
  packages/types/src/protocol/messages/TlvDataReport.ts  0% smaller
  packages/types/src/protocol/messages/TlvInvokeRequest.ts  0% smaller
  packages/types/src/protocol/messages/TlvInvokeResponse.ts  0% smaller
  packages/types/src/protocol/messages/TlvReadRequest.ts  0% smaller
  packages/types/src/protocol/messages/TlvSubscribeRequest.ts  0% smaller
  packages/types/src/protocol/messages/TlvSubscribeResponse.ts  0% smaller
  packages/types/src/protocol/messages/TlvTimedRequest.ts  0% smaller
  packages/types/src/protocol/messages/TlvWriteRequest.ts  0% smaller
  packages/types/src/protocol/messages/TlvWriteResponse.ts  0% smaller
  packages/types/src/protocol/types/TlvAttributePath.ts  0% smaller
  packages/types/src/protocol/types/TlvAttributeReport.ts  0% smaller
  packages/types/src/protocol/types/TlvAttributeReportData.ts  0% smaller
  packages/types/src/protocol/types/TlvAttributeStatus.ts  0% smaller
  packages/types/src/protocol/types/TlvCommandData.ts  0% smaller
  packages/types/src/protocol/types/TlvDataVersionFilter.ts  0% smaller
  packages/types/src/protocol/types/TlvEventData.ts  0% smaller
  packages/types/src/protocol/types/TlvEventFilter.ts  0% smaller
  packages/types/src/protocol/types/TlvEventPath.ts  0% smaller
  packages/types/src/protocol/types/TlvEventReport.ts  0% smaller
  packages/types/src/protocol/types/TlvEventStatus.ts  0% smaller
  packages/types/src/protocol/types/TlvInvokeResponseData.ts  0% smaller

Copy link
Collaborator

@Apollon77 Apollon77 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok checked and I think I also spotted the issue for the tests :-)

Else yes thats nice already and with this we can really wire in the new things one after the other even maybe.

packages/general/src/transaction/errors.ts Outdated Show resolved Hide resolved
packages/protocol/src/action/request/Invoke.ts Outdated Show resolved Hide resolved
packages/protocol/src/action/request/Read.ts Outdated Show resolved Hide resolved
This offers a new path for performing attribute reads against the Node API.  As discussed it is NOT fully wired yet so we can discuss first, but this PR should be ready for merge.

There's significant new code here.  Some of this is noise, which I detail below, but here are the main bits of new functionality to focus on:

1. packages/protocol/src/action/** - this is a new API for performing interactions that is designed for simplicity and performance
  - action/protocols.ts - these define a `ProtocolNode` JS contract for exposing a Matter data model in a form that efficiently maps to the wire protocol
  - action/server/AttributeResponse.ts - implements the attribute subset of a Read Request Action against ProtocolNode
  - action/Interactable.ts and request/* - an API I created to simplify construction of Matter actions.  The goal was to make it easy to initiate Matter interactions for e.g. batch purposes, but with a more convenient JS API.  Much of this is stubbed due to focus on read in this commit but I do use some of the type definitions
2. packages/node/src/node/server/ProtocolService.ts - implements ProtocolNode for a @matter/node Node
3. packages/behavior/state/managed/values/StructManager.ts - I updated struct manager to generate ID-based getters and setters.  This means that where state values are associated with a Matter element that has an ID you can use `state[id]` and `state.name` interchangeably.  I had done this to support attributes that are not in the schema but it also allows us to map protocol queries against state objects without performing name lookup

It became apparent as I was working through this that the code was going to be too @matter/node specific unless I performed some code reorganization.  Unfortunately that adds noise to the commit but I think it's worthwhile.

Moved modules include:

- node/src/behavior/state/transaction -> general/src/transaction
- node/src/behavior/state/Val.ts -> protocol/src/action
- node/src/behavior/errors.ts -> protocol/src/action
- node/src/behavior/AccessControl.ts -> protocol/src/action/server

Also unfortunate... I encountered a Mocha bug while developing this that indicated there were import cycles.  I added a "matter-build cycles" command to detect cycles and fixed all cycles in the node package, but this ended up changing a bunch of extra files that are now hard to disentangle from the commit.
@Apollon77 Apollon77 merged commit b2729c9 into project-chip:main Feb 12, 2025
26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants