Skip to content

Adjust shared subscription sample to better fit docs #575

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

Merged
merged 3 commits into from
Apr 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions samples/mqtt5/mqtt5_shared_subscription/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ MQTT5 introduces additional features and enhancements that improve the developme

Note: MQTT5 support is currently in **developer preview**. We encourage feedback at all times, but feedback during the preview window is especially valuable in shaping the final product. During the preview period we may make backwards-incompatible changes to the public API, but in general, this is something we will try our best to avoid.

[Shared Subscriptions](https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901250) allow IoT devices to connect to a group where messages sent to a topic are then relayed to the group in a round-robin-like fashion. This is useful for distributing message load across multiple subscribing MQTT5 clients automatically. This is helpful for load balancing when you have many messages that need to be processed.
[Shared Subscriptions](https://docs.aws.amazon.com/iot/latest/developerguide/mqtt.html#mqtt5-shared-subscription) allow IoT devices to connect to a group where messages sent to a topic are then relayed to the group in a round-robin-like fashion. This is useful for distributing message load across multiple subscribing MQTT5 clients automatically. This is helpful for load balancing when you have many messages that need to be processed.

Shared Subscriptions rely on a group identifier, which tells the MQTT5 broker/server which IoT devices to treat as a group for message distribution. This is done when subscribing by formatting the subscription topic like the following: `$share/<group identifier>/<topic>`.
Shared Subscriptions rely on a group name/identifier, which tells the MQTT5 broker/server which IoT devices to treat as a group for message distribution. This is done when subscribing by formatting the subscription topic like the following: `$share/<ShareName>/<TopicFilter>`.
* `$share`: Tells the MQTT5 broker/server that the device is subscribing to a Shared Subscription.
* `<group identifier>`: Tells the MQTT5 broker/server which group to add this Shared Subscription to. Messages published to a matching topic will be distributed round-robin amongst the group.
* `<topic>`: The topic that the Shared Subscription is for. Messages published to this topic will be processed in a round-robin fashion. For example, `test/topic`.
* `<ShareName>`: Tells the MQTT5 broker/server which group to add this Shared Subscription to. Messages published to a matching topic will be distributed round-robin amongst the group.
* `<TopicFilter>`: The topic that the Shared Subscription is for. Messages published to this topic will be processed in a round-robin fashion. For example, `test/topic`.

Shared Subscriptions use a round-robbin like method of distributing messages. For example, say you have three MQTT5 clients all subscribed to the same Shared Subscription group and topic. If five messages are sent to the Shared Subscription topic, the messages will likely be delivered in the following order:
* Message 1 -> Client one
Expand Down Expand Up @@ -71,7 +71,7 @@ Replace with the following with the data from your AWS account:
* `<region>`: The AWS IoT Core region where you created your AWS IoT Core thing you wish to use with this sample. For example `us-east-1`.
* `<account>`: Your AWS IoT Core account ID. This is the set of numbers in the top right next to your AWS account name when using the AWS IoT Core website.

Note that in a real application, you may want to avoid the use of wildcards in your ClientID or use them selectively. Please follow best practices when working with AWS on production applications using the SDK. Also, for the purposes of this sample, please make sure your policy allows a client ID of `test-*` to connect or use `--client_id <client ID here>` to send the client ID your policy supports.
Note that in a real application, you may want to avoid the use of wildcards in your ClientID and shared subscription group names/identifiers. Wildcards should only be used selectively. Please follow best practices when working with AWS on production applications using the SDK. Also, for the purposes of this sample, please make sure your policy allows a client ID of `test-*` to connect or use `--client_id <client ID here>` to send the client ID your policy supports.

</details>

Expand Down
92 changes: 40 additions & 52 deletions samples/mqtt5/mqtt5_shared_subscription/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ class sample_mqtt5_client
std::promise<bool> connectionPromise;
std::promise<void> stoppedPromise;
std::mutex receiveMutex;
std::condition_variable receiveSignal;
uint64_t receivedMessages;
uint64_t expectedMessages;
bool sharedSubscriptionSupportNotAvailable;

// A helper function to print a message and then exit the sample.
void PrintMessageAndExit(String message, int exitCode)
Expand All @@ -44,15 +40,10 @@ class sample_mqtt5_client
String input_key,
String input_ca,
String input_clientId,
uint64_t input_count,
String input_clientName)
{
std::shared_ptr<sample_mqtt5_client> result = std::make_shared<sample_mqtt5_client>();

result->receivedMessages = 0;
result->expectedMessages = input_count;
result->name = input_clientName;
result->sharedSubscriptionSupportNotAvailable = false;

Aws::Iot::Mqtt5ClientBuilder *builder = Aws::Iot::Mqtt5ClientBuilder::NewMqtt5ClientBuilderWithMtlsFromPath(
input_endpoint, input_cert.c_str(), input_key.c_str());
Expand Down Expand Up @@ -123,11 +114,6 @@ class sample_mqtt5_client
if (eventData.disconnectPacket != nullptr)
{
fprintf(stdout, "\tReason code: %u\n", (uint32_t)eventData.disconnectPacket->getReasonCode());
if (eventData.disconnectPacket->getReasonCode() ==
Mqtt5::DisconnectReasonCode::AWS_MQTT5_DRC_SHARED_SUBSCRIPTIONS_NOT_SUPPORTED)
{
result->sharedSubscriptionSupportNotAvailable = true;
}
}
});

Expand All @@ -147,11 +133,6 @@ class sample_mqtt5_client
fprintf(stdout, "\t\twith UserProperty:(%s,%s)\n", prop.getName().c_str(), prop.getValue().c_str());
}
}
result->receivedMessages += 1;
if (result->receivedMessages > result->expectedMessages)
{
result->receiveSignal.notify_all();
}
});

result->client = builder->Build();
Expand Down Expand Up @@ -183,23 +164,20 @@ int main(int argc, char *argv[])
cmdData.input_key,
cmdData.input_ca,
cmdData.input_clientId + String("1"),
cmdData.input_count / 2,
String("Publisher"));
std::shared_ptr<sample_mqtt5_client> subscriberOne = sample_mqtt5_client::create_mqtt5_client(
cmdData.input_endpoint,
cmdData.input_cert,
cmdData.input_key,
cmdData.input_ca,
cmdData.input_clientId + String("2"),
cmdData.input_count / 2,
String("Subscriber One"));
std::shared_ptr<sample_mqtt5_client> subscriberTwo = sample_mqtt5_client::create_mqtt5_client(
cmdData.input_endpoint,
cmdData.input_cert,
cmdData.input_key,
cmdData.input_ca,
cmdData.input_clientId + String("3"),
cmdData.input_count / 2,
String("Subscriber Two"));

if (publisher == nullptr || subscriberOne == nullptr || subscriberTwo == nullptr)
Expand All @@ -216,6 +194,7 @@ int main(int argc, char *argv[])
{
publisher->PrintMessageAndExit("Connection was unsuccessful", -1);
}
fprintf(stdout, "[%s] Connected.\n", publisher->name.c_str());
}
else
{
Expand All @@ -228,6 +207,7 @@ int main(int argc, char *argv[])
{
subscriberOne->PrintMessageAndExit("Connection was unsuccessful", -1);
}
fprintf(stdout, "[%s] Connected.\n", subscriberOne->name.c_str());
}
else
{
Expand All @@ -240,6 +220,7 @@ int main(int argc, char *argv[])
{
subscriberTwo->PrintMessageAndExit("Connection was unsuccessful", -1);
}
fprintf(stdout, "[%s] Connected.\n", subscriberTwo->name.c_str());
}
else
{
Expand Down Expand Up @@ -286,20 +267,20 @@ int main(int argc, char *argv[])
{
// Wait just a little bit to see if the client was disconnected due to missing Shared Subscription support.
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
if (subscriberOne->sharedSubscriptionSupportNotAvailable == true)
{
if (cmdData.input_isCI == true)
{
// TMP: If this fails subscribing in CI, just exit the sample gracefully
subscriberOne->PrintMessageAndExit("Shared Subscriptions not supported", 0);
}
subscriberOne->PrintMessageAndExit("Shared Subscriptions not supported", -1);
}
Mqtt5::SubAckReasonCode result = subscribeSuccess.get_future().get();
if (result >= Mqtt5::SubAckReasonCode::AWS_MQTT5_SARC_UNSPECIFIED_ERROR)
{
subscriberOne->PrintMessageAndExit("Failed to subscribe", -1);
}

fprintf(
stdout,
"[%s] Subscribed to topic '%s' in shared subscription group '%s'. \n",
subscriberOne->name.c_str(),
cmdData.input_topic.c_str(),
cmdData.input_groupIdentifier.c_str());
fprintf(
stdout, "[%s] Full subscribed topic is: '%s'.\n", subscriberOne->name.c_str(), input_sharedTopic.c_str());
}
else
{
Expand All @@ -311,20 +292,20 @@ int main(int argc, char *argv[])
{
// Wait just a little bit to see if the client was disconnected due to missing Shared Subscription support.
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
if (subscriberTwo->sharedSubscriptionSupportNotAvailable == true)
{
if (cmdData.input_isCI == true)
{
// TMP: If this fails subscribing in CI, just exit the sample gracefully
subscriberTwo->PrintMessageAndExit("Shared Subscriptions not supported", 0);
}
subscriberTwo->PrintMessageAndExit("Shared Subscriptions not supported", -1);
}
Mqtt5::SubAckReasonCode result = subscribeSuccess.get_future().get();
if (result >= Mqtt5::SubAckReasonCode::AWS_MQTT5_SARC_UNSPECIFIED_ERROR)
{
subscriberTwo->PrintMessageAndExit("Failed to subscribe", -1);
}

fprintf(
stdout,
"[%s] Subscribed to topic '%s' in shared subscription group '%s'.\n",
subscriberTwo->name.c_str(),
cmdData.input_topic.c_str(),
cmdData.input_groupIdentifier.c_str());
fprintf(
stdout, "[%s] Full subscribed topic is: '%s'.\n", subscriberTwo->name.c_str(), input_sharedTopic.c_str());
}
else
{
Expand Down Expand Up @@ -372,18 +353,8 @@ int main(int argc, char *argv[])
}
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
std::this_thread::sleep_for(std::chrono::milliseconds(1000));

/*********************** Make sure all the messages got to the subscribers ***************************/

std::unique_lock<std::mutex> receivedLockOne(subscriberOne->receiveMutex);
subscriberOne->receiveSignal.wait(receivedLockOne, [&, subscriberOne] {
return subscriberOne->receivedMessages >= subscriberOne->expectedMessages;
});
std::unique_lock<std::mutex> receivedLockTwo(subscriberTwo->receiveMutex);
subscriberTwo->receiveSignal.wait(receivedLockTwo, [&, subscriberTwo] {
return subscriberTwo->receivedMessages >= subscriberTwo->expectedMessages;
});
// Wait 5 seconds to let the last publish go out before unsubscribing
std::this_thread::sleep_for(std::chrono::milliseconds(5000));

/*********************** Unsubscribe the subscribers ***************************/

Expand All @@ -396,6 +367,13 @@ int main(int argc, char *argv[])
subscriberOne->PrintMessageAndExit("Unsubscribe failed", -1);
}
unsubscribeFinishedPromise.get_future().wait();
fprintf(
stdout,
"[%s] Unsubscribed to topic '%s' in shared subscription group '%s'.\n",
subscriberOne->name.c_str(),
cmdData.input_topic.c_str(),
cmdData.input_groupIdentifier.c_str());
fprintf(stdout, "[%s] Full unsubscribed topic is: '%s'.\n", subscriberOne->name.c_str(), input_sharedTopic.c_str());

unsubscribeFinishedPromise = std::promise<void>();
if (!subscriberTwo->client->Unsubscribe(
Expand All @@ -404,6 +382,13 @@ int main(int argc, char *argv[])
subscriberTwo->PrintMessageAndExit("Unsubscribe failed", -1);
}
unsubscribeFinishedPromise.get_future().wait();
fprintf(
stdout,
"[%s] Unsubscribed to topic '%s' in shared subscription group '%s'.\n",
subscriberTwo->name.c_str(),
cmdData.input_topic.c_str(),
cmdData.input_groupIdentifier.c_str());
fprintf(stdout, "[%s] Full unsubscribed topic is: '%s'.\n", subscriberTwo->name.c_str(), input_sharedTopic.c_str());

/*********************** Disconnect all the MQTT5 clients ***************************/

Expand All @@ -412,18 +397,21 @@ int main(int argc, char *argv[])
publisher->PrintMessageAndExit("Failed to disconnect. Exiting...", -1);
}
publisher->stoppedPromise.get_future().wait();
fprintf(stdout, "[%s] Fully stopped.\n", publisher->name.c_str());

if (!subscriberOne->client->Stop())
{
subscriberOne->PrintMessageAndExit("Failed to disconnect. Exiting...", -1);
}
subscriberOne->stoppedPromise.get_future().wait();
fprintf(stdout, "[%s] Fully stopped.\n", subscriberOne->name.c_str());

if (!subscriberTwo->client->Stop())
{
subscriberTwo->PrintMessageAndExit("Failed to disconnect. Exiting...", -1);
}
subscriberTwo->stoppedPromise.get_future().wait();
fprintf(stdout, "[%s] Fully stopped.\n", subscriberTwo->name.c_str());

/*********************** Free the shared pointers (MQTT5 clients) ***************************/
publisher->client = nullptr;
Expand Down