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

Export tedge features via a library crate #2014

Merged
merged 4 commits into from
Jun 9, 2023

Conversation

jarhodes314
Copy link
Contributor

Proposed changes

In order to enable thick-edge remote connectivity, we would like to connect thin-edge to Cumulocity programmatically, and we would like to do this without going via the tedge binary, as that makes error handling more tricky (than calling a Rust function), and it means we are forced into running logic that doesn't make sense in Kubernetes, such as ensuring mosquitto is connected when we run tedge connect c8y (mosquitto is run in a separate container, and is only started after we have connected the device).

This PR modifies the tedge crate to be a library crate, which allows other crates to import functions from tedge. I have exported the direct connection logic so our code can call that directly, and tweaked main.rs to import from the library. I have also added a case to the direct connection function to speed up the connection (sending a device creation message as soon as the first one is acknowledged, previously we waited for a heartbeat message). That speeds up function from around 5/10s to more like 1s (I haven't measured it in detail, but it's noticeably faster.

Types of changes

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Improvement (general improvements like code refactoring that doesn't explicitly fix a bug or add any new functionality)
  • Documentation Update (if none of the other choices apply)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Paste Link to the issue


Checklist

  • I have read the CONTRIBUTING doc
  • I have signed the CLA (in all commits with git commit -s)
  • I ran cargo fmt as mentioned in CODING_GUIDELINES
  • I used cargo clippy as mentioned in CODING_GUIDELINES
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)

Further comments

Signed-off-by: James Rhodes <jarhodes314@gmail.com>
Signed-off-by: James Rhodes <jarhodes314@gmail.com>
@jarhodes314 jarhodes314 temporarily deployed to Test Pull Request June 8, 2023 16:19 — with GitHub Actions Inactive
@codecov
Copy link

codecov bot commented Jun 8, 2023

Codecov Report

Patch coverage: 51.31% and project coverage change: +0.27 🎉

Comparison is base (12f709d) 72.59% compared to head (110c0f2) 72.86%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2014      +/-   ##
==========================================
+ Coverage   72.59%   72.86%   +0.27%     
==========================================
  Files         279      281       +2     
  Lines       30630    30518     -112     
  Branches    30630    30518     -112     
==========================================
+ Hits        22236    22238       +2     
+ Misses       6796     6655     -141     
- Partials     1598     1625      +27     
Impacted Files Coverage Δ
crates/bin/c8y-device-management/src/main.rs 1.44% <0.00%> (+0.04%) ⬆️
...ore/tedge/src/cli/connect/c8y_direct_connection.rs 0.00% <ø> (ø)
crates/core/tedge/src/cli/init.rs 0.00% <0.00%> (ø)
crates/core/tedge/src/cli/mod.rs 46.66% <0.00%> (-11.67%) ⬇️
crates/core/tedge/src/command.rs 85.71% <0.00%> (-14.29%) ⬇️
crates/core/tedge_agent/src/agent.rs 0.00% <0.00%> (ø)
crates/core/tedge_agent/src/main.rs 5.00% <0.00%> (-0.27%) ⬇️
crates/core/tedge_mapper/src/aws/mapper.rs 0.00% <ø> (ø)
crates/core/tedge_mapper/src/az/mapper.rs 0.00% <ø> (ø)
crates/core/tedge_mapper/src/c8y/mapper.rs 0.00% <0.00%> (ø)
... and 23 more

... and 6 files with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@github-actions
Copy link
Contributor

github-actions bot commented Jun 8, 2023

Robot Results

✅ Passed ❌ Failed ⏭️ Skipped Total Pass %
218 0 5 218 100

Passed Tests

Name ⏱️ Duration Suite
Define Child device 1 ID 0.009 s C8Y Child Alarms Rpi
Normal case when the child device does not exist on c8y cloud 3.741 s C8Y Child Alarms Rpi
Normal case when the child device already exists 0.796 s C8Y Child Alarms Rpi
Reconciliation when the new alarm message arrives, restart the mapper 1.426 s C8Y Child Alarms Rpi
Reconciliation when the alarm that is cleared 6.252 s C8Y Child Alarms Rpi
Prerequisite Parent 16.528 s Child Conf Mgmt Plugin
Prerequisite Child 0.464 s Child Conf Mgmt Plugin
Child device bootstrapping 15.66 s Child Conf Mgmt Plugin
Snapshot from device 61.39 s Child Conf Mgmt Plugin
Child device config update 62.293 s Child Conf Mgmt Plugin
Configuration types should be detected on file change (without restarting service) 42.841 s Inotify Crate
Check lock file existence in default folder 0.94 s Lock File
Check PID number in lock file 1.084 s Lock File
Check PID number in lock file after restarting the services 2.184 s Lock File
Check starting same service twice 1.5550000000000002 s Lock File
Switch off lock file creation 2.578 s Lock File
Set configuration when file exists 7.633 s Configuration Operation
Set configuration when file does not exist 6.42 s Configuration Operation
Set configuration with broken url 5.913 s Configuration Operation
Get configuration 5.258 s Configuration Operation
Get non existent configuration file 5.211 s Configuration Operation
Get non existent configuration type 4.662 s Configuration Operation
Update configuration plugin config via cloud 5.439 s Configuration Operation
Modify configuration plugin config via local filesystem modify inplace 5.153 s Configuration Operation
Modify configuration plugin config via local filesystem overwrite 2.74 s Configuration Operation
Update configuration plugin config via local filesystem copy 3.324 s Configuration Operation
Update configuration plugin config via local filesystem move (different directory) 4.061 s Configuration Operation
Update configuration plugin config via local filesystem move (same directory) 4.023 s Configuration Operation
Successful firmware operation 66.823 s Firmware Operation
Install with empty firmware name 52.146 s Firmware Operation
Prerequisite Parent 17.835 s Firmware Operation Child Device
Prerequisite Child 8.066 s Firmware Operation Child Device
Child device firmware update 4.203 s Firmware Operation Child Device
Child device firmware update with cache 4.394 s Firmware Operation Child Device
Firmware plugin supports restart via service manager #1932 4.234 s Firmware Operation Child Device Retry
Update Inventory data via inventory.json 1.166 s Inventory Update
Inventory includes the agent fragment with version information 1.05 s Inventory Update
Retrieve a JWT tokens 49.409 s Jwt Request
Check running collectd 1.062 s Monitor Device Collectd
Is collectd publishing MQTT messages? 3.047 s Monitor Device Collectd
Check thin-edge monitoring 4.524 s Monitor Device Collectd
Check grouping of measurements 9.057 s Monitor Device Collectd
Update the custom operation dynamically 45.457 s Dynamically Reload Operation
Main device registration 1.5030000000000001 s Device Registration
Child device registration 1.99 s Device Registration
Supports restarting the device 64.105 s Restart Device
Update tedge version from previous using Cumulocity 100.575 s Tedge Self Update
Test if all c8y services are up 45.193 s Service Monitoring
Test if all c8y services are down 44.5 s Service Monitoring
Test if all c8y services are using configured service type 227.932 s Service Monitoring
Test if all c8y services using default service type when service type configured as empty 31.154 s Service Monitoring
Check health status of tedge-mapper-c8y service on broker stop start 19.011 s Service Monitoring
Check health status of tedge-mapper-c8y service on broker restart 21.527 s Service Monitoring
Check health status of child device service 17.366 s Service Monitoring
Successful shell command with output 3.371 s Shell Operation
Check Successful shell command with literal double quotes output 3.023 s Shell Operation
Execute multiline shell command 3.396 s Shell Operation
Failed shell command 3.298 s Shell Operation
Software list should be populated during startup 46.143 s Software
Install software via Cumulocity 45.555 s Software
Software list should only show currently installed software and not candidates 36.381 s Software
Child devices support sending simple measurements 1.115 s Child Device Telemetry
Child devices support sending custom measurements 1.435 s Child Device Telemetry
Child devices support sending custom events 1.18 s Child Device Telemetry
Child devices support sending custom events overriding the type 0.976 s Child Device Telemetry
Child devices support sending custom alarms #1699 3.268 s Child Device Telemetry
Child devices support sending inventory data via c8y topic 0.902 s Child Device Telemetry
Child device supports sending custom child device measurements directly to c8y 1.332 s Child Device Telemetry
Check retained alarms 225.47 s Raise Alarms
Thin-edge devices support sending simple measurements 1.206 s Thin-Edge Device Telemetry
Thin-edge devices support sending simple measurements with custom type 1.026 s Thin-Edge Device Telemetry
Thin-edge devices support sending custom measurements 3.078 s Thin-Edge Device Telemetry
Thin-edge devices support sending custom events 1.127 s Thin-Edge Device Telemetry
Thin-edge devices support sending custom events overriding the type 1.221 s Thin-Edge Device Telemetry
Thin-edge devices support sending custom alarms #1699 1.338 s Thin-Edge Device Telemetry
Thin-edge device supports sending custom Thin-edge device measurements directly to c8y 1.458 s Thin-Edge Device Telemetry
Thin-edge device support sending inventory data via c8y topic 1.18 s Thin-Edge Device Telemetry
thin-edge components support a custom config-dir location via flags 26.9 s Config Dir
Validate updated data path used by tedge-agent 0.41 s Data Path Config
Validate updated data path used by c8y-firmware-plugin 10.852 s Data Path Config
Validate updated data path used by tedge-agent 0.451 s Log Path Config
Install thin-edge via apt 47.459 s Install Apt
Install latest via script (from current branch) 29.111 s Install Tedge
Install specific version via script (from current branch) 22.213 s Install Tedge
Install latest tedge via script (from main branch) 20.354 s Install Tedge
Install then uninstall latest tedge via script (from main branch) 55.808 s Install Tedge
Support starting and stopping services 38.819 s Service-Control
Supports a reconnect 52.435 s Test-Commands
Supports disconnect then connect 36.164 s Test-Commands
Update unknown setting 32.566 s Test-Commands
Update known setting 33.549 s Test-Commands
It checks MQTT messages using a pattern 63.649 s Test-Mqtt
Stop c8y-configuration-plugin 0.1 s Health C8Y-Configuration-Plugin
Update the service file 0.145 s Health C8Y-Configuration-Plugin
Reload systemd files 0.467 s Health C8Y-Configuration-Plugin
Start c8y-configuration-plugin 0.169 s Health C8Y-Configuration-Plugin
Start watchdog service 10.2 s Health C8Y-Configuration-Plugin
Check PID of c8y-configuration-plugin 0.108 s Health C8Y-Configuration-Plugin
Kill the PID 0.247 s Health C8Y-Configuration-Plugin
Recheck PID of c8y-configuration-plugin 6.558 s Health C8Y-Configuration-Plugin
Compare PID change 0.001 s Health C8Y-Configuration-Plugin
Stop watchdog service 0.142 s Health C8Y-Configuration-Plugin
Remove entry from service file 0.107 s Health C8Y-Configuration-Plugin
Stop c8y-log-plugin 0.217 s Health C8Y-Log-Plugin
Update the service file 0.271 s Health C8Y-Log-Plugin
Reload systemd files 0.446 s Health C8Y-Log-Plugin
Start c8y-log-plugin 0.166 s Health C8Y-Log-Plugin
Start watchdog service 10.305 s Health C8Y-Log-Plugin
Check PID of c8y-log-plugin 0.112 s Health C8Y-Log-Plugin
Kill the PID 0.219 s Health C8Y-Log-Plugin
Recheck PID of c8y-log-plugin 6.514 s Health C8Y-Log-Plugin
Compare PID change 0.002 s Health C8Y-Log-Plugin
Stop watchdog service 0.187 s Health C8Y-Log-Plugin
Remove entry from service file 0.154 s Health C8Y-Log-Plugin
Stop tedge-mapper 0.089 s Health Tedge Mapper C8Y
Update the service file 0.094 s Health Tedge Mapper C8Y
Reload systemd files 0.598 s Health Tedge Mapper C8Y
Start tedge-mapper 0.224 s Health Tedge Mapper C8Y
Start watchdog service 10.403 s Health Tedge Mapper C8Y
Check PID of tedge-mapper 0.203 s Health Tedge Mapper C8Y
Kill the PID 0.542 s Health Tedge Mapper C8Y
Recheck PID of tedge-mapper 6.75 s Health Tedge Mapper C8Y
Compare PID change 0.001 s Health Tedge Mapper C8Y
Stop watchdog service 0.251 s Health Tedge Mapper C8Y
Remove entry from service file 0.183 s Health Tedge Mapper C8Y
Stop tedge-agent 0.093 s Health Tedge-Agent
Update the service file 0.143 s Health Tedge-Agent
Reload systemd files 0.558 s Health Tedge-Agent
Start tedge-agent 0.326 s Health Tedge-Agent
Start watchdog service 10.198 s Health Tedge-Agent
Check PID of tedge-mapper 0.136 s Health Tedge-Agent
Kill the PID 0.39 s Health Tedge-Agent
Recheck PID of tedge-agent 6.697 s Health Tedge-Agent
Compare PID change 0.002 s Health Tedge-Agent
Stop watchdog service 0.354 s Health Tedge-Agent
Remove entry from service file 0.257 s Health Tedge-Agent
Stop tedge-mapper-az 0.163 s Health Tedge-Mapper-Az
Update the service file 0.33 s Health Tedge-Mapper-Az
Reload systemd files 0.479 s Health Tedge-Mapper-Az
Start tedge-mapper-az 0.188 s Health Tedge-Mapper-Az
Start watchdog service 10.276 s Health Tedge-Mapper-Az
Check PID of tedge-mapper-az 0.171 s Health Tedge-Mapper-Az
Kill the PID 0.167 s Health Tedge-Mapper-Az
Recheck PID of tedge-agent 6.531 s Health Tedge-Mapper-Az
Compare PID change 0.001 s Health Tedge-Mapper-Az
Stop watchdog service 0.113 s Health Tedge-Mapper-Az
Remove entry from service file 0.114 s Health Tedge-Mapper-Az
Stop tedge-mapper-collectd 0.15 s Health Tedge-Mapper-Collectd
Update the service file 0.116 s Health Tedge-Mapper-Collectd
Reload systemd files 0.536 s Health Tedge-Mapper-Collectd
Start tedge-mapper-collectd 0.138 s Health Tedge-Mapper-Collectd
Start watchdog service 10.281 s Health Tedge-Mapper-Collectd
Check PID of tedge-mapper-collectd 0.196 s Health Tedge-Mapper-Collectd
Kill the PID 0.458 s Health Tedge-Mapper-Collectd
Recheck PID of tedge-mapper-collectd 6.574 s Health Tedge-Mapper-Collectd
Compare PID change 0.001 s Health Tedge-Mapper-Collectd
Stop watchdog service 0.103 s Health Tedge-Mapper-Collectd
Remove entry from service file 0.096 s Health Tedge-Mapper-Collectd
tedge-collectd-mapper health status 5.417 s Health Tedge-Mapper-Collectd
c8y-log-plugin health status 5.366 s MQTT health endpoints
c8y-configuration-plugin health status 5.83 s MQTT health endpoints
Publish on a local insecure broker 0.329 s Basic Pub Sub
Publish on a local secure broker 3.241 s Basic Pub Sub
Publish on a local secure broker with client authentication 3.911 s Basic Pub Sub
Check remote mqtt broker #1773 2.849 s Remote Mqtt Broker
Apply name filter 0.089 s Filter Packages List Output
Apply maintainer filter 0.329 s Filter Packages List Output
Apply both filters 0.279 s Filter Packages List Output
No filters 0.245 s Filter Packages List Output
Both filters but name filter as empty string 0.175 s Filter Packages List Output
Both filters but maintainer filter as empty string 0.236 s Filter Packages List Output
Both filters as empty string 0.259 s Filter Packages List Output
Wrong package name 0.158 s Improve Tedge Apt Plugin Error Messages
Wrong version 0.146 s Improve Tedge Apt Plugin Error Messages
Wrong type 0.32 s Improve Tedge Apt Plugin Error Messages
tedge_connect_test_positive 0.19 s Tedge Connect Test
tedge_connect_test_negative 0.508 s Tedge Connect Test
tedge_connect_test_sm_services 7.313 s Tedge Connect Test
tedge_disconnect_test_sm_services 0.628 s Tedge Connect Test
Install thin-edge.io 17.478 s Call Tedge
call tedge -V 0.057 s Call Tedge
call tedge -h 0.1 s Call Tedge
call tedge -h -V 0.088 s Call Tedge
call tedge help 0.077 s Call Tedge
tedge config list 0.12 s Call Tedge Config List
tedge config list --all 0.131 s Call Tedge Config List
set/unset device.type 0.67 s Call Tedge Config List
set/unset device.key_path 0.468 s Call Tedge Config List
set/unset device.cert_path 0.574 s Call Tedge Config List
set/unset c8y.root_cert_path 0.543 s Call Tedge Config List
set/unset c8y.smartrest.templates 0.569 s Call Tedge Config List
set/unset az.root_cert_path 0.817 s Call Tedge Config List
set/unset aws.url 0.436 s Call Tedge Config List
set/unset aws.root_cert_path 0.4 s Call Tedge Config List
set/unset aws.mapper.timestamp 0.373 s Call Tedge Config List
set/unset az.mapper.timestamp 0.36 s Call Tedge Config List
set/unset mqtt.bind.address 0.271 s Call Tedge Config List
set/unset mqtt.bind.port 0.44 s Call Tedge Config List
set/unset http.bind.port 0.554 s Call Tedge Config List
set/unset tmp.path 0.296 s Call Tedge Config List
set/unset logs.path 0.291 s Call Tedge Config List
set/unset run.path 0.321 s Call Tedge Config List
set/unset firmware.child.update.timeout 0.274 s Call Tedge Config List
set/unset c8y.url 0.249 s Call Tedge Config List
set/unset az.url 0.274 s Call Tedge Config List
set/unset mqtt.external.bind.port 0.351 s Call Tedge Config List
mqtt.external.bind.address 0.286 s Call Tedge Config List
mqtt.external.bind.interface 0.231 s Call Tedge Config List
set/unset mqtt.external.ca_path 0.253 s Call Tedge Config List
set/unset mqtt.external.cert_file 0.23 s Call Tedge Config List
set/unset mqtt.external.key_file 0.23 s Call Tedge Config List
set/unset software.plugin.default 0.228 s Call Tedge Config List
Get Put Delete 2.099 s Http File Transfer Api
Set keys should return value on stdout 0.136 s Tedge Config Get
Unset keys should not return anything on stdout and warnings on stderr 0.21 s Tedge Config Get
Invalid keys should not return anything on stdout and warnings on stderr 0.384 s Tedge Config Get
Set configuration via environment variables 0.916 s Tedge Config Get
Set unknown configuration via environment variables 0.08 s Tedge Config Get

Copy link
Contributor

@didier-wenzek didier-wenzek left a comment

Choose a reason for hiding this comment

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

I would be happy to merge it. I have a question though.

Comment on lines 76 to 82
Ok(Event::Incoming(Incoming::PubComp(rumqttc::PubComp { pkid: 2 }))) => {
publish_device_create_message(
&mut client,
&bridge_config.remote_clientid.clone(),
device_type,
)?;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

So this is "to speed up the connection (sending a device creation message as soon as the first one is acknowledged, previously we waited for a heartbeat message)".

Okay. But why as reaction to message numbered 2 ?

  • Is this the message sent on Ok(Event::Incoming(Packet::SubAck(_)))
  • or sent on Ok(Event::Outgoing(Outgoing::PingReq)).

This is also relies on the fact that publish_device_create_message send the message with QoS::ExactlyOnce (this is actually the case).

So this is okay but a bit fragile. So I would consider:

  • to add some comments
  • to react independently of the pkid (as we only send creation message on this connection)
  • to react also on PubAck.

I would even consider to send the message with QoS AtLeastOnce as we are now blindly re-sending the message if c8y is not fast enough.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's using pkid: 2 as that is the first PubComp we receive (corresponding to the message we sent after receiving SubAck), and I was trying to avoid this code triggering itself as we should need to send exactly two messages to get Cumulocity to reply to say the device already exists.

Copy link
Contributor

Choose a reason for hiding this comment

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

I was trying to avoid this code triggering itself as we should need to send exactly two messages to get Cumulocity to reply to say the device already exists.

You can discuss this with @rina23q as she already tried different approaches for that purpose.

You can also discuss this with @reubenmiller as he might shed a new light to this issue.

@jarhodes314 jarhodes314 temporarily deployed to Test Pull Request June 9, 2023 15:04 — with GitHub Actions Inactive
@jarhodes314 jarhodes314 temporarily deployed to Test Pull Request June 9, 2023 15:40 — with GitHub Actions Inactive
Copy link
Contributor

@didier-wenzek didier-wenzek left a comment

Choose a reason for hiding this comment

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

Approved

@didier-wenzek didier-wenzek merged commit e16a914 into thin-edge:main Jun 9, 2023
18 of 19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants