Skip to content
Open
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
4 changes: 3 additions & 1 deletion lib/api/Logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,9 @@ namespace MAT_NS_BEGIN

if (latency == EventLatency_Off)
{
DispatchEvent(DebugEventType::EVT_DROPPED);
// Event dropped because the active TransmitProfile has disabled this
// latency tier. param1 = 1 record dropped; param2 = reason code.
DispatchEvent(DebugEvent(DebugEventType::EVT_DROPPED, 1u, static_cast<size_t>(DROPPED_REASON_LATENCY_DISABLED_BY_PROFILE)));
LOG_INFO("Event %s/%s dropped: calculated latency 0 (Off)",
tenantTokenToId(m_tenantToken).c_str(), record.baseType.c_str());
return;
Comment thread
mogiligarimidi23 marked this conversation as resolved.
Expand Down
2 changes: 2 additions & 0 deletions lib/include/public/Enums.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,8 @@ namespace MAT_NS_BEGIN
DROPPED_REASON_SERVER_DECLINED_5XX,
DROPPED_REASON_SERVER_DECLINED_OTHER,
DROPPED_REASON_RETRY_EXCEEDED,
DROPPED_REASON_TEARDOWN_TIMEOUT,
DROPPED_REASON_LATENCY_DISABLED_BY_PROFILE,
Comment thread
mogiligarimidi23 marked this conversation as resolved.
DROPPED_REASON_COUNT
};

Expand Down
12 changes: 12 additions & 0 deletions lib/include/public/IOfflineStorage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,18 @@ namespace MAT_NS_BEGIN {
/// <returns>Number of records</returns>
virtual size_t GetRecordCount(EventLatency latency = EventLatency_Unspecified) const = 0;

/// <summary>
/// Returns the total number of records that should be considered as
/// "still pending" at shutdown for teardown-timeout reporting.
/// </summary>
/// <remarks>
/// Default implementation returns GetRecordCount(), which is correct for
/// persistent storages (e.g. SQLite, Room) where in-flight records are
/// still counted in the on-disk total. Volatile storages (e.g. memory)
/// must override to also include their in-flight / reserved records.
/// </remarks>
virtual size_t GetRemainingRecordCountForShutdown() const { return GetRecordCount(); }

/// <summary>
/// Get Vector of records from DB
/// </summary>
Expand Down
21 changes: 21 additions & 0 deletions lib/offline/MemoryStorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -520,5 +520,26 @@ namespace MAT_NS_BEGIN {
return m_reserved_records.size();
}

/// <summary>
/// Memory storage does not include in-flight (reserved) records in
/// GetRecordCount(), so add them here for accurate shutdown reporting.
/// </summary>
/// <remarks>
/// Take both locks (reserved first, then records — matching the order used
/// by Shutdown() and GetAndReserveRecords()) so the returned count is an
/// atomic snapshot. Without this, a record moving between the active queue
/// and the reserved map (e.g. via GetAndReserveRecords / ReleaseRecords)
/// could be double-counted or missed.
/// </remarks>
size_t MemoryStorage::GetRemainingRecordCountForShutdown() const
{
LOCKGUARD(m_reserved_lock);
LOCKGUARD(m_records_lock);
size_t records = 0;
for (unsigned lat = EventLatency_Off; lat <= EventLatency_Max; lat++)
records += m_records[lat].size();
return records + m_reserved_records.size();
}
Comment thread
mogiligarimidi23 marked this conversation as resolved.

} MAT_NS_END

4 changes: 3 additions & 1 deletion lib/offline/MemoryStorage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ namespace MAT_NS_BEGIN {

virtual size_t GetRecordCount(EventLatency latency = EventLatency_Unspecified) const override;

virtual size_t GetRemainingRecordCountForShutdown() const override;

virtual size_t GetReservedCount();

virtual std::vector<StorageRecord> GetRecords(bool shutdown = false, EventLatency minLatency = EventLatency_Unspecified, unsigned maxCount = 0) override;
Expand All @@ -88,7 +90,7 @@ namespace MAT_NS_BEGIN {
/// Contains reserved (aka in-flight) records.
/// Current storage interface API requires deletion and release by StorageRecordId.
/// </summary>
std::mutex m_reserved_lock;
mutable std::mutex m_reserved_lock;
std::map<StorageRecordId, StorageRecord> m_reserved_records;

size_t m_size;
Expand Down
10 changes: 10 additions & 0 deletions lib/offline/OfflineStorageHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,16 @@ namespace MAT_NS_BEGIN {
return count;
}

size_t OfflineStorageHandler::GetRemainingRecordCountForShutdown() const
{
size_t count = 0;
if (m_offlineStorageMemory != nullptr)
count += m_offlineStorageMemory->GetRemainingRecordCountForShutdown();
if (m_offlineStorageDisk != nullptr)
count += m_offlineStorageDisk->GetRemainingRecordCountForShutdown();
return count;
}

void OfflineStorageHandler::Flush()
{
if (!m_logManager.StartActivity()) {
Expand Down
2 changes: 2 additions & 0 deletions lib/offline/OfflineStorageHandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ namespace MAT_NS_BEGIN {
virtual size_t GetSize() override;
virtual size_t GetRecordCount(EventLatency latency = EventLatency_Unspecified) const override;

virtual size_t GetRemainingRecordCountForShutdown() const override;

virtual std::vector<StorageRecord> GetRecords(bool shutdown, EventLatency minLatency = EventLatency_Unspecified, unsigned maxCount = 0) override;
virtual bool ResizeDb() override;

Expand Down
1 change: 1 addition & 0 deletions lib/offline/OfflineStorage_Room.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1169,6 +1169,7 @@ namespace MAT_NS_BEGIN

DebugEvent evt(DebugEventType::EVT_DROPPED);
evt.param1 = dropped;
evt.param2 = static_cast<size_t>(DROPPED_REASON_OFFLINE_STORAGE_OVERFLOW);
evt.size = dropped;
m_manager.DispatchEvent(evt);

Expand Down
23 changes: 13 additions & 10 deletions lib/offline/OfflineStorage_SQLite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -954,24 +954,27 @@ namespace MAT_NS_BEGIN {
LOG_TRACE("DB is too big, deleting...");
Execute("DELETE FROM " TABLE_NAME_EVENTS);
Execute("VACUUM");
return true;
eventsDropped = count;
}
Comment thread
mogiligarimidi23 marked this conversation as resolved.

SqliteStatement trimStmt(*m_db, m_stmtTrimEvents_percent);
if (!trimStmt.execute(25))
else
{
// If something went wrong with trimming 25%, try more radical measure
LOG_TRACE("Evict all non-critical");
Execute("DELETE FROM " TABLE_NAME_EVENTS " WHERE persistence=1");
SqliteStatement trimStmt(*m_db, m_stmtTrimEvents_percent);
if (!trimStmt.execute(25))
{
// If something went wrong with trimming 25%, try more radical measure
LOG_TRACE("Evict all non-critical");
Execute("DELETE FROM " TABLE_NAME_EVENTS " WHERE persistence=1");
}
eventsDropped = count - GetRecordCountUnsafe(EventLatency::EventLatency_Unspecified);
LOG_TRACE("Db resized, events dropped: %zu", eventsDropped);
trimStmt.reset();
}
eventsDropped = count - GetRecordCountUnsafe(EventLatency::EventLatency_Unspecified);
LOG_TRACE("Db resized, events dropeed: %d", eventsDropped);
trimStmt.reset();
}

m_DbSizeEstimate = GetSize();
DebugEvent evt(DebugEventType::EVT_DROPPED);
evt.param1 = eventsDropped;
evt.param2 = static_cast<size_t>(DROPPED_REASON_OFFLINE_STORAGE_OVERFLOW);
evt.size = eventsDropped;
m_logManager.DispatchEvent(evt);

Expand Down
2 changes: 2 additions & 0 deletions lib/offline/StorageObserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ namespace MAT_NS_BEGIN {
DebugEvent evt;
evt.type = EVT_DROPPED;
evt.param1 = overallCount;
evt.param2 = static_cast<size_t>(DROPPED_REASON_OFFLINE_STORAGE_OVERFLOW);
evt.size = overallCount;
DispatchEvent(evt);
}
Expand All @@ -180,6 +181,7 @@ namespace MAT_NS_BEGIN {
DebugEvent evt;
evt.type = EVT_DROPPED;
evt.param1 = overallCount;
evt.param2 = static_cast<size_t>(DROPPED_REASON_RETRY_EXCEEDED);
evt.size = overallCount;
DispatchEvent(evt);
}
Expand Down
5 changes: 5 additions & 0 deletions lib/offline/StorageObserver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ namespace MAT_NS_BEGIN {
return m_offlineStorage.GetRecordCount();
}

size_t GetRemainingRecordCountForShutdown() const
{
return m_offlineStorage.GetRemainingRecordCountForShutdown();
}

RoutePassThrough<StorageObserver> start{ this, &StorageObserver::handleStart };
RoutePassThrough<StorageObserver> stop{ this, &StorageObserver::handleStop };

Expand Down
1 change: 1 addition & 0 deletions lib/stats/Statistics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ namespace MAT_NS_BEGIN {
DebugEvent evt;
evt.type = DebugEventType::EVT_DROPPED;
evt.param1 = 1;
evt.param2 = static_cast<size_t>(DROPPED_REASON_OFFLINE_STORAGE_SAVE_FAILED);
OnDebugEvent(evt);

return true;
Expand Down
15 changes: 14 additions & 1 deletion lib/system/TelemetrySystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,20 @@ namespace MAT_NS_BEGIN {
auto uploadTime = GetUptimeMs() - stopTimes[0];
if (uploadTime >= (1000L * timeoutInSec))
{
// Hard-stop if it takes longer than planned
// Hard-stop if it takes longer than planned.
// Emit an EVT_DROPPED carrying the remaining record count
// so listeners can attribute teardown-timeout losses.
// Delegate the "remaining" calculation to the storage layer
// so each backend (memory vs SQLite/Room) can decide whether
// in-flight records need to be added on top of GetRecordCount().
const size_t remaining = storage.GetRemainingRecordCountForShutdown();
if (remaining > 0)
{
DebugEvent evt(DebugEventType::EVT_DROPPED, remaining,
static_cast<size_t>(DROPPED_REASON_TEARDOWN_TIMEOUT));
evt.size = remaining;
m_logManager.DispatchEvent(evt);
}
LOG_TRACE("Shutdown timer expired, exiting...");
break;
}
Expand Down
Loading