Skip to content

Commit

Permalink
ReadClient should delay re-subscribe attempts based on delay in BUSY …
Browse files Browse the repository at this point in the history
…responses.

The ifdefs are to reduce the codesize costs for situations where the BUSY
handling is not needed (e.g. if the application does not use ReadClient at all
and does not care about handling the timeout from BUSY from
OperationalSessionSetup).
  • Loading branch information
bzbarsky-apple committed Apr 1, 2024
1 parent 6c89640 commit 9789220
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/app/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ buildconfig_header("app_buildconfig") {
"TIME_SYNC_ENABLE_TSC_FEATURE=${time_sync_enable_tsc_feature}",
"NON_SPEC_COMPLIANT_OTA_ACTION_DELAY_FLOOR=${non_spec_compliant_ota_action_delay_floor}",
"CHIP_DEVICE_CONFIG_DYNAMIC_SERVER=${chip_build_controller_dynamic_server}",
"CHIP_CONFIG_ENABLE_BUSY_HANDLING_FOR_OPERATIONAL_SESSION_SETUP=${chip_enable_busy_handling_for_operational_session_setup}",
]

visibility = [ ":app_config" ]
Expand Down
21 changes: 17 additions & 4 deletions src/app/OperationalSessionSetup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,12 @@ void OperationalSessionSetup::DequeueConnectionCallbacks(CHIP_ERROR error, Sessi
auto * exchangeMgr = mInitParams.exchangeMgr;
Optional<SessionHandle> optionalSessionHandle = mSecureSession.Get();
ScopedNodeId peerId = mPeerId;
System::Clock::Milliseconds16 requestedBusyDelay =
#if CHIP_CONFIG_ENABLE_BUSY_HANDLING_FOR_OPERATIONAL_SESSION_SETUP
mRequestedBusyDelay;
#else
System::Clock::kZero;
#endif // CHIP_CONFIG_ENABLE_BUSY_HANDLING_FOR_OPERATIONAL_SESSION_SETUP

if (releaseBehavior == ReleaseBehavior::Release)
{
Expand All @@ -361,14 +367,15 @@ void OperationalSessionSetup::DequeueConnectionCallbacks(CHIP_ERROR error, Sessi
// DO NOT touch any members of this object after this point. It's dead.

NotifyConnectionCallbacks(failureReady, setupFailureReady, successReady, error, stage, peerId, performingAddressUpdate,
exchangeMgr, optionalSessionHandle);
exchangeMgr, optionalSessionHandle, requestedBusyDelay);
}

void OperationalSessionSetup::NotifyConnectionCallbacks(Cancelable & failureReady, Cancelable & setupFailureReady,
Cancelable & successReady, CHIP_ERROR error,
SessionEstablishmentStage stage, const ScopedNodeId & peerId,
bool performingAddressUpdate, Messaging::ExchangeManager * exchangeMgr,
const Optional<SessionHandle> & optionalSessionHandle)
const Optional<SessionHandle> & optionalSessionHandle,
System::Clock::Milliseconds16 requestedBusyDelay)
{
//
// If we encountered no error, go ahead and call all success callbacks. Otherwise,
Expand Down Expand Up @@ -401,6 +408,12 @@ void OperationalSessionSetup::NotifyConnectionCallbacks(Cancelable & failureRead
{
// Initialize the ConnnectionFailureInfo object
ConnnectionFailureInfo failureInfo(peerId, error, stage);
#if CHIP_CONFIG_ENABLE_BUSY_HANDLING_FOR_OPERATIONAL_SESSION_SETUP
if (error == CHIP_ERROR_BUSY)
{
failureInfo.requestedBusyDelay.Emplace(requestedBusyDelay);
}
#endif // CHIP_CONFIG_ENABLE_BUSY_HANDLING_FOR_OPERATIONAL_SESSION_SETUP
cb->mCall(cb->mContext, failureInfo);
}
}
Expand Down Expand Up @@ -482,9 +495,9 @@ void OperationalSessionSetup::OnSessionEstablishmentError(CHIP_ERROR error, Sess

void OperationalSessionSetup::OnResponderBusy(System::Clock::Milliseconds16 requestedDelay)
{
#if CHIP_DEVICE_CONFIG_ENABLE_AUTOMATIC_CASE_RETRIES
#if CHIP_DEVICE_CONFIG_ENABLE_AUTOMATIC_CASE_RETRIES || CHIP_CONFIG_ENABLE_BUSY_HANDLING_FOR_OPERATIONAL_SESSION_SETUP
// Store the requested delay, so that we can use it for scheduling our
// retry.
// retry or communicate it to our API consumer.
mRequestedBusyDelay = requestedDelay;
#endif
}
Expand Down
21 changes: 18 additions & 3 deletions src/app/OperationalSessionSetup.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

#pragma once

#include <app/AppConfig.h>
#include <app/CASEClient.h>
#include <app/CASEClientPool.h>
#include <app/DeviceProxy.h>
Expand Down Expand Up @@ -161,6 +162,13 @@ class DLL_EXPORT OperationalSessionSetup : public SessionEstablishmentDelegate,
CHIP_ERROR error;
SessionEstablishmentStage sessionStage;

// When the response was BUSY, error will be CHIP_ERROR_BUSY and
// requestedBusyDelay will be set, if handling of BUSY responses is
// enabled.
#if CHIP_CONFIG_ENABLE_BUSY_HANDLING_FOR_OPERATIONAL_SESSION_SETUP
Optional<System::Clock::Milliseconds16> requestedBusyDelay;
#endif // CHIP_CONFIG_ENABLE_BUSY_HANDLING_FOR_OPERATIONAL_SESSION_SETUP

ConnnectionFailureInfo(const ScopedNodeId & peer, CHIP_ERROR err, SessionEstablishmentStage stage) :
peerId(peer), error(err), sessionStage(stage)
{}
Expand Down Expand Up @@ -306,6 +314,10 @@ class DLL_EXPORT OperationalSessionSetup : public SessionEstablishmentDelegate,

bool mPerformingAddressUpdate = false;

#if CHIP_DEVICE_CONFIG_ENABLE_AUTOMATIC_CASE_RETRIES || CHIP_CONFIG_ENABLE_BUSY_HANDLING_FOR_OPERATIONAL_SESSION_SETUP
System::Clock::Milliseconds16 mRequestedBusyDelay = System::Clock::kZero;
#endif // CHIP_DEVICE_CONFIG_ENABLE_AUTOMATIC_CASE_RETRIES || CHIP_CONFIG_ENABLE_BUSY_HANDLING_FOR_OPERATIONAL_SESSION_SETUP

#if CHIP_DEVICE_CONFIG_ENABLE_AUTOMATIC_CASE_RETRIES
// When we TryNextResult on the resolver, it will synchronously call back
// into our OnNodeAddressResolved when it succeeds. We need to track
Expand All @@ -320,8 +332,6 @@ class DLL_EXPORT OperationalSessionSetup : public SessionEstablishmentDelegate,

uint8_t mResolveAttemptsAllowed = 0;

System::Clock::Milliseconds16 mRequestedBusyDelay = System::Clock::kZero;

Callback::CallbackDeque mConnectionRetry;
#endif // CHIP_DEVICE_CONFIG_ENABLE_AUTOMATIC_CASE_RETRIES

Expand Down Expand Up @@ -385,7 +395,12 @@ class DLL_EXPORT OperationalSessionSetup : public SessionEstablishmentDelegate,
Callback::Cancelable & successReady, CHIP_ERROR error, SessionEstablishmentStage stage,
const ScopedNodeId & peerId, bool performingAddressUpdate,
Messaging::ExchangeManager * exchangeMgr,
const Optional<SessionHandle> & optionalSessionHandle);
const Optional<SessionHandle> & optionalSessionHandle,
// requestedBusyDelay will be 0 if not
// CHIP_CONFIG_ENABLE_BUSY_HANDLING_FOR_OPERATIONAL_SESSION_SETUP,
// and only has a meaningful value
// when the error is CHIP_ERROR_BUSY.
System::Clock::Milliseconds16 requestedBusyDelay);

/**
* Triggers a DNSSD lookup to find a usable peer address.
Expand Down
29 changes: 26 additions & 3 deletions src/app/ReadClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@ uint32_t ReadClient::ComputeTimeTillNextSubscription()
waitTimeInMsec = minWaitTimeInMsec + (Crypto::GetRandU32() % (maxWaitTimeInMsec - minWaitTimeInMsec));
}

if (mMinimalResubscribeDelay.count() > waitTimeInMsec)
{
waitTimeInMsec = mMinimalResubscribeDelay.count();
}

return waitTimeInMsec;
}

Expand Down Expand Up @@ -1058,6 +1063,10 @@ CHIP_ERROR ReadClient::ProcessSubscribeResponse(System::PacketBufferHandle && aP

CHIP_ERROR ReadClient::SendAutoResubscribeRequest(ReadPrepareParams && aReadPrepareParams)
{
// Make sure we don't use minimal resubscribe delays from previous attempts
// for this one.
mMinimalResubscribeDelay = System::Clock::kZero;

mReadPrepareParams = std::move(aReadPrepareParams);
CHIP_ERROR err = SendSubscribeRequest(mReadPrepareParams);
if (err != CHIP_NO_ERROR)
Expand Down Expand Up @@ -1239,14 +1248,28 @@ void ReadClient::HandleDeviceConnected(void * context, Messaging::ExchangeManage
}
}

void ReadClient::HandleDeviceConnectionFailure(void * context, const ScopedNodeId & peerId, CHIP_ERROR err)
void ReadClient::HandleDeviceConnectionFailure(void * context, const OperationalSessionSetup::ConnnectionFailureInfo & failureInfo)
{
ReadClient * const _this = static_cast<ReadClient *>(context);
VerifyOrDie(_this != nullptr);

ChipLogError(DataManagement, "Failed to establish CASE for re-subscription with error '%" CHIP_ERROR_FORMAT "'", err.Format());
ChipLogError(DataManagement, "Failed to establish CASE for re-subscription with error '%" CHIP_ERROR_FORMAT "'",
failureInfo.error.Format());

#if CHIP_CONFIG_ENABLE_BUSY_HANDLING_FOR_OPERATIONAL_SESSION_SETUP
#if CHIP_DETAIL_LOGGING
if (failureInfo.requestedBusyDelay.HasValue())
{
ChipLogDetail(DataManagement, "Will delay resubscription by %u ms due to BUSY response",
failureInfo.requestedBusyDelay.Value().count());
}
#endif // CHIP_DETAIL_LOGGING
_this->mMinimalResubscribeDelay = failureInfo.requestedBusyDelay.ValueOr(System::Clock::kZero);
#else
_this->mMinimalResubscribeDelay = System::Clock::kZero;
#endif // CHIP_CONFIG_ENABLE_BUSY_HANDLING_FOR_OPERATIONAL_SESSION_SETUP

_this->Close(err);
_this->Close(failureInfo.error);
}

void ReadClient::OnResubscribeTimerCallback(System::Layer * /* If this starts being used, fix callers that pass nullptr */,
Expand Down
8 changes: 6 additions & 2 deletions src/app/ReadClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ class ReadClient : public Messaging::ExchangeDelegate

static void HandleDeviceConnected(void * context, Messaging::ExchangeManager & exchangeMgr,
const SessionHandle & sessionHandle);
static void HandleDeviceConnectionFailure(void * context, const ScopedNodeId & peerId, CHIP_ERROR error);
static void HandleDeviceConnectionFailure(void * context, const OperationalSessionSetup::ConnnectionFailureInfo & failureInfo);

CHIP_ERROR GetMinEventNumber(const ReadPrepareParams & aReadPrepareParams, Optional<EventNumber> & aEventMin);

Expand Down Expand Up @@ -642,8 +642,12 @@ class ReadClient : public Messaging::ExchangeDelegate
bool mForceCaseOnNextResub = true;
bool mIsResubscriptionScheduled = false;

// mMinimalResubscribeDelay is used to store the delay returned with a BUSY
// response to a Sigma1 message.
System::Clock::Milliseconds16 mMinimalResubscribeDelay = System::Clock::kZero;

chip::Callback::Callback<OnDeviceConnected> mOnConnectedCallback;
chip::Callback::Callback<OnDeviceConnectionFailure> mOnConnectionFailureCallback;
chip::Callback::Callback<OperationalSessionSetup::OnSetupFailure> mOnConnectionFailureCallback;

ReadClient * mpNext = nullptr;
InteractionModelEngine * mpImEngine = nullptr;
Expand Down
4 changes: 4 additions & 0 deletions src/app/common_flags.gni
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@ declare_args() {
chip_app_use_echo = false
chip_enable_read_client = true
chip_build_controller_dynamic_server = false

# Flag that controls whether the time-to-wait from BUSY responses is
# communicated to OperationalSessionSetup API consumers.
chip_enable_busy_handling_for_operational_session_setup = true
}

0 comments on commit 9789220

Please sign in to comment.