From 0fd07d99d61a56fc41c9364aca7d2b369d7a3efb Mon Sep 17 00:00:00 2001 From: "Evgeny @ SimpleX Chat" <259188159+evgeny-simplex@users.noreply.github.com> Date: Tue, 13 Oct 2026 10:42:47 +0000 Subject: [PATCH] fixes --- plans/2026-09-26-client-usage-stats.md | 22 ++++++++++++---------- src/Simplex/Messaging/Server/UsageStats.hs | 15 +++++++-------- tests/CoreTests/UsageStatsTests.hs | 5 ++++- 3 files changed, 23 insertions(+), 19 deletions(-) diff --git a/plans/2026-09-26-client-usage-stats.md b/plans/2026-09-26-client-usage-stats.md index d370e9cd1..28cddd62e 100644 --- a/plans/2026-09-26-client-usage-stats.md +++ b/plans/2026-09-26-client-usage-stats.md @@ -147,22 +147,22 @@ Functions: 3. The counter is read from `current` with `TM.lookupIO`. 4. An absent counter is looked up or inserted with the evaluated amount in one transaction. 5. A present counter is increased with `modifyTVar'`. - + The amount is positive at every call site. -- `closeUsagePeriod :: UsageCounter c => UsageStats c -> IO ()`, per client: - 1. In one transaction: - - `current` is set to an empty map; the counts of its counters are the closed counts; - - `previous` is set to the closed counts. - 2. The entry is deleted when the closed counts are empty. - +- `closeUsagePeriod :: UsageCounter c => UsageStats c -> IO ()`, per client, in one transaction: + - `current` is set to an empty map; the counts of its counters are the closed counts; + - `previous` is set to the closed counts; + - the entry is deleted when the closed counts are empty. + Then, per counter, the histogram is updated with the closed counts: - `bucketCounts`: 1 is added at the bucket index of each count - `usageSum`: the counts are added - + `lastPeriodMax` is set to the largest closed count, 0 without closed counts. - `usagePeriodThread :: UsageCounter c => Int64 -> UsageStats c -> IO ()`: `closeUsagePeriod` every `period` seconds. - `topClients :: UsageCounter c => UsageStats c -> c -> Int -> IO [(ClientAddr, Int, Int)]`: - address, previous count, current count + - both counts of a client read in one transaction - sorted by the sum of both counts, largest first - clients with sum 0 excluded - limited to the given number @@ -171,7 +171,7 @@ Functions: Counts lost in `closeUsagePeriod`: -- a count added after the closing transaction to an entry deleted after it; +- a count added after the closing transaction to an entry deleted by it; - a count added after the closing transaction to a counter read with `TM.lookupIO` before it. ## Prometheus @@ -340,7 +340,9 @@ Other changes: - `counterByName`: - each counter name - unknown name: `Nothing` -- `addUsage`: one entry per address +- `addUsage`: + - one entry per address + - amount evaluated when a counter is created - `closeUsagePeriod`: - histogram buckets, sum and max - counters without counts excluded from histograms diff --git a/src/Simplex/Messaging/Server/UsageStats.hs b/src/Simplex/Messaging/Server/UsageStats.hs index d8c3c91d0..5d4819e5a 100644 --- a/src/Simplex/Messaging/Server/UsageStats.hs +++ b/src/Simplex/Messaging/Server/UsageStats.hs @@ -144,11 +144,10 @@ closeUsagePeriod UsageStats {clientCounts, histograms} = do closedCounts <- mapM closeClientPeriod . M.toList =<< readTVarIO clientCounts atomicModifyIORef'_ histograms $ M.mapWithKey $ \c -> addPeriodCounts (counterBounds c) (mapMaybe (M.lookup c) closedCounts) where - closeClientPeriod (addr, ClientCounts {current, previous}) = do - counts <- atomically $ do - counts <- mapM readTVar =<< swapTVar current M.empty - counts <$ writeTVar previous counts - when (M.null counts) $ atomically $ TM.delete addr clientCounts + closeClientPeriod (addr, ClientCounts {current, previous}) = atomically $ do + counts <- mapM readTVar =<< swapTVar current M.empty + writeTVar previous counts + when (M.null counts) $ TM.delete addr clientCounts pure counts addPeriodCounts :: [Int] -> [Int] -> UsageHistogram -> UsageHistogram @@ -169,11 +168,11 @@ usagePeriodThread period stats = do topClients :: UsageCounter c => UsageStats c -> c -> Int -> IO [(ClientAddr, Int, Int)] topClients UsageStats {clientCounts} c n = do entries <- M.toList <$> readTVarIO clientCounts - counts <- forM entries $ \(addr, ClientCounts {previous, current}) -> (addr,,) <$> previousCount previous <*> currentCount current + counts <- forM entries $ \(addr, ClientCounts {previous, current}) -> atomically $ (addr,,) <$> previousCount previous <*> currentCount current pure $ take n $ sortOn (\(_, p, cur) -> Down (p + cur)) $ filter (\(_, p, cur) -> p + cur > 0) counts where - previousCount = fmap (M.findWithDefault 0 c) . readTVarIO - currentCount = maybe (pure 0) readTVarIO <=< TM.lookupIO c + previousCount = fmap (M.findWithDefault 0 c) . readTVar + currentCount = maybe (pure 0) readTVar <=< TM.lookup c printTopClients :: UsageCounter c => Handle -> Text -> Maybe Int -> Maybe (UsageStats c) -> IO () printTopClients h name n_ = \case diff --git a/tests/CoreTests/UsageStatsTests.hs b/tests/CoreTests/UsageStatsTests.hs index 25db32cb8..a3f883e2b 100644 --- a/tests/CoreTests/UsageStatsTests.hs +++ b/tests/CoreTests/UsageStatsTests.hs @@ -118,7 +118,7 @@ counterByNameTests = do testCounterNames cs = map (counterByName . counterName) cs `shouldBe` map Just cs addUsageTests :: Spec -addUsageTests = +addUsageTests = do it "should count usage of one address in one entry" $ do stats@UsageStats {clientCounts} <- newUsageStats incUsage (ClientUsage stats addr1) TCSend @@ -127,6 +127,9 @@ addUsageTests = M.keys <$> readTVarIO clientCounts `shouldReturn` [addr1, addr2] counts stats addr1 TCSend `shouldReturn` Just (0, 2) counts stats addr2 TCGet `shouldReturn` Just (0, 1) + it "should evaluate the amount when a counter is created" $ do + stats <- newUsageStats + addUsage (ClientUsage stats addr1) TCSend undefined `shouldThrow` anyErrorCall closeUsagePeriodTests :: Spec closeUsagePeriodTests = do