Home

Awesome


page_type: sample name: "IoT Central high availability client samples" description: "Sample code for IoT Central device clients that shows how to handle failover scenarios." languages:


IoT Central high availability client samples

Coding Azure IoT devices to work with IoT Centrals high availability feature. Each folder in this repository contains a full device client sample that illustrates how to code a high availability device client for use with IoT Central.

Ask yourself is your IoT Central device ready for this kind of disaster:

giant lizard attack

What is this high-availability thing?

The high availability option in IoT Central allows for an application to have two or more IoT Hubs associated with it in different locations. This allows for one hub to have a failure and devices will continue to be available and operate against the other IoT Hub. The magic ingredient in this is the Device Provisioning Service (DPS) that acts as a traffic cop directing your device to the correct IoT Hub to connect and send data to. Lets look at a failover scenario.

In this scenario we have an IoT Central application that has two hubs 'A' and 'B' and a device named 'XYZ'. We will connect the device and then see what happens when one of the hubs fails due to a giant lizard attack (you may have seen the movie!) on that data center.

Directory structure

Folder NameAzure IoT device SDKFolder Link
Chttps://github.com/Azure/azure-iot-sdk-chttps://github.com/Azure/iot-central-high-availability-clients/tree/main/C
Node/JavaScripthttps://github.com/Azure/azure-iot-sdk-nodehttps://github.com/Azure/iot-central-high-availability-clients/tree/main/Node/JavaScript
Node/TypeScripthttps://github.com/Azure/azure-iot-sdk-nodehttps://github.com/Azure/iot-central-high-availability-clients/tree/main/Node/TypeScript
Pythonhttps://github.com/Azure/azure-iot-sdk-pythonhttps://github.com/Azure/iot-central-high-availability-clients/tree/main/Python
CSharphttps://github.com/Azure/azure-iot-sdk-csharphttps://github.com/Azure/iot-central-high-availability-clients/tree/main/csharp
Javahttps://github.com/Azure/azure-iot-sdk-javahttps://github.com/Azure/iot-central-high-availability-clients/tree/main/java

Functionality of the client

Each of the samples has the exact same functionality coded using the same IoT Central device template. The device template can be found in the file failover.json. This can be imported into your IoT Central application and each of the samples will associate itself with this model by default.

The functionality of each sample is:

All samples all have a section that looks like this (this is the Node.js version):

// device settings - FILL IN YOUR VALUES HERE
const scopeId = "<Put your scope id here from IoT Central Administration -> Device connection>"
const groupSymmetricKey = "<Put your group SAS primary key here from IoT Central Administration -> Device Connection -> SAS-IoT-Devices>"

This information needs to be filled in before running the sample. Optionally the following section (taken from Node.js sample) can also be modified if you need to use a different DPS endpoint (uncommon) or wish to use a different device or model identity:

// optional device settings - CHANGE IF DESIRED/NECESSARY
const provisioningHost = "global.azure-devices-provisioning.net"
const deviceId = "failover_js"
const modelId = "dtmi:Sample:Failover;1"  // This model is available in the root of the Github repo (Failover.json) and can be imported into your Azure IoT central application

It is also possible to turn off different actions in the sample for debugging purposes by adjusting the following section (taken from the Node.js sample):

// test setting flags
const telemetrySendOn = true
const reportedPropertySendOn = true
const desiredPropertyReceiveOn = true
const directMethodReceiveOn = true
const c2dCommandReceiveOn = true

Each of the sample folders contains a readme that discusses running the sample and includes the above information specific to that sample and the programming language used.

Testing failover and failback scenarios

We are unable to predict when the next giant lizard attack will happen and to which data center so we have provided you with your own virtual giant lizard to test failover scenarios. The IoT Central development team has updated the Azure Command Line Interface (CLI) extension for IoT Central so you can force a device to either fail-over or fail-back from one IoT Hub to another.

To install the Azure CLI tool please see this link https://docs.microsoft.com/en-us/cli/azure/install-azure-cli Once installed you can install the latest IoT Central extension by following the instructions here https://github.com/Azure/azure-iot-cli-extension

Once installed you can force your device to failover with the following command:

az iot central device manual-failover -n <IoT Central application id> -d <device id> --central-api-uri <application host name> --central-dns-suffix <application DNS suffix name>
ParameterDescriptionExample
IoT Central application idYou can find this in your IoT Central application in the Administration main page8ed0b52f-75f9-4964-9287-008159218877
device idThis is the device identity for the device you wish to failoverfailover_device
application host nameThe first part of the URL of your IoT Central application (before the first dot)mycentralapp
application DNS suffix nameThe rest of the URL of your IoT Central application (after the first dot)azureiotcentral.com

The output from the command should look like this:

Command group 'iot central device' is in preview and under development. Reference and support levels: https://aka.ms/CLI_refstatus
{
  "hubIdentifier": "d3936bfa327fe0dbdf65d2cf9c5f521a1807bd8972d8d7212165a65d8af73c71",
  "message": "Success! This device is now being failed over. You can check your device’s status using 'iot central device registration-info' command. The device will revert to its original hub at Mon, 19 Apr 2021 21:04:46 GMT. You can choose to failback earlier using device-manual-failback command. Learn more: https://aka.ms/iotc-device-test"
}

When a device is failed over to it's other IoT Hub it will remain failed over for a period of five minutes. After which it will fail-back to it's original hub. You can force the device to fail-back before the five minutes are up by issuing the following command:

az iot central device manual-failback -n <IoT Central application id> -d <device id> --central-api-uri <application host name> --central-dns-suffix <application DNS suffix name>

The parameters are exactly the same as for the previous fail-over command above. The output from the command should look similar to this:

Command group 'iot central device' is in preview and under development. Reference and support levels: https://aka.ms/CLI_refstatus
{
  "hubIdentifier": "d3936bfa327fe0dbdf65d2cf9c5f521a1807bd8972d8d7212165a65d8af73c71",
  "message": "Device has successfully failed back to its original hub"
}

A couple of batch (.bat) files are provided in the root of the this repository failover.bat and failback.bat as a convenience and take the following four parameters (exactly the same as the command description above):

ParameterDescriptionExample
IoT Central application idYou can find this in your IoT Central application in the Administration main page8ed0b52f-75f9-4964-9287-008159218877
device idThis is the device identity for the device you wish to failoverfailover_device
application host nameThe first part of the URL of your IoT Central application (before the first dot)mycentralapp
application DNS suffix nameThe rest of the URL of your IoT Central application (after the first dot)azureiotcentral.com

Final thoughts

In all these examples there is no code to optimize connection speed, all connections are routed through DPS. In a real device it might be more optimal to cache the hostname returned from DPS and when disconnected from the IoT Hub attempt to connect using the cached hostname, if the connection fails at that point drop back to DPS for a new hostname and cache it. This optimization will reduce the connection time and remove the performacne penalty when the connection drops for various reasons outside of an IoT Hub failure.

Please let us know if you find any errors or have suggestions to improve the code in this repository. Finally, good luck and be on the look out for for giant lizard attacks!