|
17 | 17 |
|
18 | 18 | import com.akto.bulk_update_util.ApiInfoBulkUpdate; |
19 | 19 | import com.akto.dao.*; |
| 20 | +import com.akto.dao.AccountsContextDao; |
20 | 21 | import com.akto.dao.AgentTrafficLogDao; |
21 | 22 | import com.akto.dao.CyborgLogsDao; |
22 | 23 | import com.akto.dao.filter.MergedUrlsDao; |
@@ -137,6 +138,12 @@ public class DbLayer { |
137 | 138 | private static volatile long mergedUrlsCacheTimestamp = 0; |
138 | 139 | private static final long MERGED_URLS_CACHE_TTL_MS = 15 * 60 * 1000; // 15 minutes |
139 | 140 |
|
| 141 | + // Collection cleanup configuration |
| 142 | + private static final ConcurrentHashMap<String, Integer> collectionCleanupCache = new ConcurrentHashMap<>(); |
| 143 | + private static final int CLEANUP_INTERVAL_SECONDS = 30 * 60; // 30 minutes |
| 144 | + private static final int CLEANUP_JITTER_SECONDS = 3 * 60; // 3 minutes max jitter |
| 145 | + private static final long COLLECTION_SIZE_THRESHOLD = 100_000; |
| 146 | + |
140 | 147 | private static int getLastUpdatedTsForAccount(int accountId) { |
141 | 148 | return lastUpdatedTsMap.computeIfAbsent(accountId, k -> 0); |
142 | 149 | } |
@@ -761,7 +768,62 @@ public static void createCollectionForHostAndVpc(String host, int id, String vpc |
761 | 768 | } |
762 | 769 |
|
763 | 770 | public static void insertRuntimeLog(Log log) { |
764 | | - RuntimeLogsDao.instance.insertOne(log); |
| 771 | + RuntimeLogsDao runtimeLogsDao = RuntimeLogsDao.instance; |
| 772 | + cleanupCollectionIfNeeded(runtimeLogsDao.getCollName(), runtimeLogsDao); |
| 773 | + runtimeLogsDao.insertOne(log); |
| 774 | + } |
| 775 | + |
| 776 | + private static void cleanupCollectionIfNeeded(String collectionName, AccountsContextDao<?> dao) { |
| 777 | + try { |
| 778 | + int accountId = Context.accountId.get(); |
| 779 | + String cacheKey = accountId + "_" + collectionName; |
| 780 | + |
| 781 | + int currentTime = Context.now(); |
| 782 | + Integer nextCleanupTime = collectionCleanupCache.get(cacheKey); |
| 783 | + |
| 784 | + if (nextCleanupTime != null && currentTime < nextCleanupTime) { |
| 785 | + return; |
| 786 | + } |
| 787 | + |
| 788 | + long estimatedCount = dao.getMCollection().estimatedDocumentCount(); |
| 789 | + |
| 790 | + if (estimatedCount >= COLLECTION_SIZE_THRESHOLD) { |
| 791 | + List<String> slackMessages = new ArrayList<>(); |
| 792 | + |
| 793 | + String beforeDropMsg = String.format( |
| 794 | + "Dropping collection - collection=%s, account=%d, estimatedCount=%d, threshold=%d, timestamp=%d", |
| 795 | + collectionName, accountId, estimatedCount, COLLECTION_SIZE_THRESHOLD, Context.now() |
| 796 | + ); |
| 797 | + loggerMaker.infoAndAddToDb(beforeDropMsg); |
| 798 | + slackMessages.add(beforeDropMsg); |
| 799 | + |
| 800 | + long startDropTime = System.currentTimeMillis(); |
| 801 | + dao.getMCollection().drop(); |
| 802 | + long endDropTime = System.currentTimeMillis(); |
| 803 | + |
| 804 | + String afterDropMsg = String.format( |
| 805 | + "Successfully dropped collection=%s, account=%d, timestamp=%d, timeTakenMilliseconds=%d", |
| 806 | + collectionName, accountId, endDropTime, endDropTime - startDropTime |
| 807 | + ); |
| 808 | + loggerMaker.infoAndAddToDb(afterDropMsg); |
| 809 | + slackMessages.add(afterDropMsg); |
| 810 | + |
| 811 | + // Send combined message to Slack |
| 812 | + String combinedMessage = String.join("\n", slackMessages); |
| 813 | + loggerMaker.sendCyborgSlackAsync(combinedMessage); |
| 814 | + } |
| 815 | + |
| 816 | + // Calculate next cleanup time with jitter (0 to 3 minutes) |
| 817 | + int jitter = (int) (Math.random() * CLEANUP_JITTER_SECONDS); |
| 818 | + int nextScheduledCleanup = currentTime + CLEANUP_INTERVAL_SECONDS + jitter; |
| 819 | + collectionCleanupCache.put(cacheKey, nextScheduledCleanup); |
| 820 | + |
| 821 | + } catch (Exception e) { |
| 822 | + loggerMaker.errorAndAddToDb(e, |
| 823 | + String.format("Error during cleanup of collection %s, accountId: %s: %s", collectionName, |
| 824 | + Context.accountId.get(), e) |
| 825 | + ); |
| 826 | + } |
765 | 827 | } |
766 | 828 |
|
767 | 829 | public static void insertPersistentRuntimeLog(Log log) { |
|
0 commit comments