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

MQTT - handling re-sends when a session is reestablished. #980

Closed
wants to merge 5 commits into from

Conversation

leegeth
Copy link
Contributor

@leegeth leegeth commented Jun 4, 2020

Description of changes:
Handling the resend and receive of duplicate packets when an MQTT session is reestablished.

These are the cases that is handled in this PR,

When a session is reestablished,
Outgoing publishes:

  1. Resend PUBLISHes for which and ack is not received(PUBACK and PUBREC).
  2. Resend PUBRELs for PUBLISHes for which PUBCOMP was not received.

Incoming publishes:

  1. Handle duplicate QoS1 and QoS2 PUBLISHes
  2. Handle duplicate PUBRELs

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@leegeth leegeth requested a review from abhidixi11 June 4, 2020 06:12
@leegeth leegeth marked this pull request as ready for review June 5, 2020 04:33
@leegeth leegeth requested a review from muneebahmed10 June 5, 2020 04:33
libraries/standard/mqtt/src/mqtt.c Outdated Show resolved Hide resolved
libraries/standard/mqtt/src/mqtt.c Show resolved Hide resolved
libraries/standard/mqtt/src/mqtt.c Outdated Show resolved Hide resolved
libraries/standard/mqtt/src/mqtt.c Outdated Show resolved Hide resolved
libraries/standard/mqtt/src/mqtt.c Outdated Show resolved Hide resolved
libraries/standard/mqtt/src/mqtt.c Show resolved Hide resolved
libraries/standard/mqtt/src/mqtt.c Outdated Show resolved Hide resolved
libraries/standard/mqtt/src/mqtt.c Outdated Show resolved Hide resolved
libraries/standard/mqtt/src/mqtt.c Outdated Show resolved Hide resolved
@leegeth leegeth requested a review from dan4thewin June 9, 2020 20:54
Copy link
Contributor

@muneebahmed10 muneebahmed10 left a comment

Choose a reason for hiding this comment

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

The complexity reduction in this PR seems premature, since a lot of it is already handled by other PRs. This will likely make it more time-consuming when dealing with merge conflicts later. I'd suggest reverting those changes for now and handling them in a follow up PR to minimize merge conflicts

libraries/standard/mqtt/src/mqtt.c Outdated Show resolved Hide resolved
libraries/standard/mqtt/src/mqtt.c Outdated Show resolved Hide resolved
libraries/standard/mqtt/src/mqtt.c Outdated Show resolved Hide resolved
libraries/standard/mqtt/src/mqtt.c Outdated Show resolved Hide resolved
libraries/standard/mqtt/src/mqtt.c Outdated Show resolved Hide resolved
libraries/standard/mqtt/include/mqtt_state.h Outdated Show resolved Hide resolved
libraries/standard/mqtt/src/mqtt.c Outdated Show resolved Hide resolved
libraries/standard/mqtt/src/mqtt.c Outdated Show resolved Hide resolved
libraries/standard/mqtt/src/mqtt.c Outdated Show resolved Hide resolved
@leegeth
Copy link
Contributor Author

leegeth commented Jun 10, 2020

The complexity reduction in this PR seems premature, since a lot of it is already handled by other PRs. This will likely make it more time-consuming when dealing with merge conflicts later. I'd suggest reverting those changes for now and handling them in a follow up PR to minimize merge conflicts

oh ok. If it is handled in a different way in another PRs, I agree with your comment that we should come back to it a after merging those changes. Otherwise merges will be difficult. I will revert my refactoring after taking a look at the changes in other PRs

@leegeth leegeth changed the title Handling restablishing session MQTT - handling re-sends when a session is reestablished. Jun 11, 2020
{
LogDebug( ( "DUP is 0." ) );
}
pPublishInfo->dup = UINT8_CHECK_BIT( publishFlags, MQTT_PUBLISH_FLAG_DUP );
Copy link
Contributor

@muneebahmed10 muneebahmed10 Jun 11, 2020

Choose a reason for hiding this comment

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

MISRA: Add a ? true : false; to the end of this. Reasoning here: #993 (comment)

}
else
{
records = ( incomingPublish ) ? pMqttContext->incomingPublishRecords
Copy link
Contributor

Choose a reason for hiding this comment

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

MISRA:

Suggested change
records = ( incomingPublish ) ? pMqttContext->incomingPublishRecords
records = ( incomingPublish == true ) ? pMqttContext->incomingPublishRecords

Comment on lines +448 to +451
else
{
/*This is considered as a new incoming publish. */
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't really think we need this else here

{
MQTTStatus_t mqttStatus = MQTTSuccess;
MQTTPacketInfo_t incomingPacket = { 0 };
size_t pingreqSize = MQTT_PACKET_PINGREQ_SIZE;
bool expectMoreCalls = true;
bool hasIncomingPacketAlreadyReceived = false, isStateUpdateNeededAfterSendingAck = true;
Copy link
Contributor

Choose a reason for hiding this comment

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

Still not a fan of these long names. What's wrong with packetAlreadyReceived and updateAfterAck?


if( newState == MQTTStateNull )
/* Only update the state machine if the state update is required. */
if( stateNeedUpdate )
Copy link
Contributor

Choose a reason for hiding this comment

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

MISRA:

Suggested change
if( stateNeedUpdate )
if( stateNeedUpdate == true )

/* Resend pending PUBREL. */
status = sendPublishAcks( pContext,
packetId,
&publishRecordState,
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we expect sendPublishAcks to update publishRecordState? In that case, shouldn't it be reset to MQTTPubRelSend in every iteration of the loop?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes.good catch

Comment on lines +1248 to +1252
assert( pContext != NULL );
assert( state == MQTTPubCompPending || state == MQTTPubRelSend );

/* Resending PUBREL packet. */
MQTTPublishState_t publishRecordState = MQTTPubRelSend;
Copy link
Contributor

Choose a reason for hiding this comment

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

I think variables need to be declared before asserts to conform to our style


/* TODO. Need to remove this update once MQTT_UpdateStatePublish is
* refactored with return type of MQTTStatus_t. */
status = MQTTBadParameter;
Copy link
Contributor

Choose a reason for hiding this comment

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

I think MQTTIllegalState is more appropriate here than MQTTBadParameter

if( pPublishInfo->qos > MQTTQoS0 )
/* Reserve state for publish message. Only to be done for QoS1 or QoS2
* and a non duplicate publish. */
if( ( pPublishInfo->qos > MQTTQoS0 ) && ( !pPublishInfo->dup ) )
Copy link
Contributor

Choose a reason for hiding this comment

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

MISRA:

Suggested change
if( ( pPublishInfo->qos > MQTTQoS0 ) && ( !pPublishInfo->dup ) )
if( ( pPublishInfo->qos > MQTTQoS0 ) && ( pPublishInfo->dup == false ) )

@@ -1410,6 +1875,7 @@ MQTTStatus_t MQTT_ProcessLoop( MQTTContext_t * const pContext,
{
/* Receive packet. Remaining time is recalculated at the end of the loop. */
status = receivePacket( pContext, incomingPacket, remainingTimeMs );
LogDebug( ( "After receiving packet with status %u.", status ) );
Copy link
Contributor

Choose a reason for hiding this comment

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

Reword to something like "Packet reception concluded with result ..."

@nateglims
Copy link
Member

/bot run checks

@leegeth
Copy link
Contributor Author

leegeth commented Jun 22, 2020

Closing this PR as this change is refactored and handled in a different approach in PR #1007.

@leegeth leegeth closed this Jun 22, 2020
@leegeth leegeth deleted the qos2_resend branch November 25, 2020 03:28
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.

3 participants