Skip to content
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
37 changes: 35 additions & 2 deletions lib/tpm/TransmissionPolicyManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,16 +331,37 @@ namespace MAT_NS_BEGIN {
}
bool forceTimerRestart = false;

// Check if it's time to execute the specific Max or other priority events code block
auto currentTime = std::chrono::steady_clock::now();
static auto maxPriorityLastExecutionTime = currentTime;

auto max_priority_elapsed_seconds = std::chrono::duration_cast<std::chrono::seconds>(currentTime - maxPriorityLastExecutionTime).count();

/* This logic needs to be revised: one event in a dedicated HTTP post is wasteful! */
// Initiate upload right away
// Initiate upload right away, but add a 2-second check to ensure some delay between consecutive initiate upload calls.
if (event->record.latency > EventLatency_RealTime) {
if(max_priority_elapsed_seconds < 2){
return;
}
auto ctx = m_system.createEventsUploadContext();
ctx->requestedMinLatency = event->record.latency;
maxPriorityLastExecutionTime = currentTime;
addUpload(ctx);
initiateUpload(ctx);
return;
}

// Other priorities like: Normal, Realtime, etc.
auto other_priority_elapsed_seconds = std::chrono::duration_cast<std::chrono::seconds>(currentTime - otherPriorityLastExecutionTime).count();

// Introducing a 40-second delay before forcefully scheduling the upload job, to ensure it happens at an optimal time.
// This delay is implemented to address Issue 388, where the last cancellation might have been halted due to the issue described below.
if (m_isUploadScheduled.load() && other_priority_elapsed_seconds > 40){
m_isUploadScheduled.store(false);
LOG_TRACE("Trigger upload on event arrival");
otherPriorityLastExecutionTime = currentTime;
}

// Schedule async upload if not scheduled yet
if (!m_isUploadScheduled || TransmitProfiles::isTimerUpdateRequired())
{
Expand Down Expand Up @@ -457,12 +478,24 @@ namespace MAT_NS_BEGIN {
{
bool result = m_scheduledUpload.Cancel(getCancelWaitTime().count());

// Check if it's time to execute the specific code block
auto currentTime = std::chrono::steady_clock::now();

auto other_priority_elapsed_seconds = std::chrono::duration_cast<std::chrono::seconds>(currentTime - otherPriorityLastExecutionTime).count();

// TODO: There is a potential for upload tasks to not be canceled, especially if they aren't waited for.
// We either need a stronger guarantee here (could impact SDK performance), or a mechanism to
// ensure those tasks are canceled when the log manager is destroyed. Issue 388
if (result)
// Introducing a 40-second delay before forcefully scheduling the upload job, to ensure it happens at an optimal time.
// This delay is implemented to address Issue 388, where the last cancellation might have been halted due to the issue described below.
if (result || other_priority_elapsed_seconds > 40)
{
m_isUploadScheduled.exchange(false);
if (other_priority_elapsed_seconds > 40)
{
LOG_TRACE("Reset upload on event cancellation");
}
otherPriorityLastExecutionTime = currentTime;
}
return result;
}
Expand Down
1 change: 1 addition & 0 deletions lib/tpm/TransmissionPolicyManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ constexpr const char* const DefaultBackoffConfig = "E,3000,300000,2,1";

mutable std::mutex m_activeUploads_lock;
std::set<EventsUploadContextPtr> m_activeUploads;
std::chrono::steady_clock::time_point otherPriorityLastExecutionTime;

/// <summary>
/// Thread-safe method to add the upload to active uploads.
Expand Down
1 change: 1 addition & 0 deletions tests/common/MockITelemetrySystem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ namespace testing {
MOCK_METHOD0(pause, void());
MOCK_METHOD0(resume, void());
MOCK_METHOD0(upload, bool());
MOCK_METHOD0(uploadMax, bool());
MOCK_METHOD0(cleanup, void());

// MOCK_METHOD0(getLogManager, ILogManager&());
Expand Down