Skip to content

Commit 3c926fe

Browse files
authored
Merge pull request #3772 from harshithb3304/archival-disable-retention
Archival disable retention
2 parents cff0e4b + 014b369 commit 3c926fe

File tree

3 files changed

+32
-52
lines changed

3 files changed

+32
-52
lines changed

apps/dashboard/web/polaris_web/web/src/apps/dashboard/pages/settings/threat_configuration/ArchivalConfigComponent.jsx

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import api from "../../../pages/threat_detection/api.js";
55
import Dropdown from "../../../components/layouts/Dropdown.jsx";
66

77
const ArchivalConfigComponent = ({ title, description }) => {
8-
const [archivalDays, setArchivalDays] = useState(60);
9-
const [archivalEnabled, setArchivalEnabled] = useState(false);
8+
const [deletionDays, setDeletionDays] = useState(60);
9+
const [deletionEnabled, setDeletionEnabled] = useState(false);
1010
const [isSaveDisabled, setIsSaveDisabled] = useState(true);
1111
const [isToggleChanged, setIsToggleChanged] = useState(false);
1212
const [isDaysChanged, setIsDaysChanged] = useState(false);
@@ -15,10 +15,10 @@ const ArchivalConfigComponent = ({ title, description }) => {
1515
const response = await api.fetchThreatConfiguration();
1616
const days = response?.threatConfiguration?.archivalDays;
1717
const value = days === 30 || days === 60 || days === 90 ? days : 60;
18-
setArchivalDays(value);
18+
setDeletionDays(value);
1919

2020
const enabled = response?.threatConfiguration?.archivalEnabled || false;
21-
setArchivalEnabled(enabled);
21+
setDeletionEnabled(enabled);
2222

2323
setIsSaveDisabled(true);
2424
setIsToggleChanged(false);
@@ -27,23 +27,23 @@ const ArchivalConfigComponent = ({ title, description }) => {
2727

2828
const onSave = async () => {
2929
try {
30-
// Save archival days if changed
30+
// Save deletion days if changed
3131
if (isDaysChanged) {
3232
const payload = {
33-
archivalDays: archivalDays
33+
archivalDays: deletionDays
3434
};
3535
await api.modifyThreatConfiguration(payload);
3636
}
3737

38-
// Toggle archival enabled if changed
38+
// Toggle deletion enabled if changed
3939
if (isToggleChanged) {
40-
await api.toggleArchivalEnabled(archivalEnabled);
40+
await api.toggleArchivalEnabled(deletionEnabled);
4141
}
4242

43-
func.setToast(true, false, "Archival configuration saved successfully");
43+
func.setToast(true, false, "Deletion configuration saved successfully");
4444
fetchData();
4545
} catch (error) {
46-
func.setToast(true, true, "Error saving archival configuration");
46+
func.setToast(true, true, "Error saving deletion configuration");
4747
}
4848
};
4949

@@ -69,13 +69,13 @@ const ArchivalConfigComponent = ({ title, description }) => {
6969
}
7070

7171
const onChange = (val) => {
72-
setArchivalDays(val);
72+
setDeletionDays(val);
7373
setIsDaysChanged(true);
7474
setIsSaveDisabled(false);
7575
};
7676

7777
const onToggleEnabled = (val) => {
78-
setArchivalEnabled(val);
78+
setDeletionEnabled(val);
7979
setIsToggleChanged(true);
8080
setIsSaveDisabled(false);
8181
};
@@ -93,17 +93,17 @@ const ArchivalConfigComponent = ({ title, description }) => {
9393
<LegacyCard.Section>
9494
<VerticalStack gap="4">
9595
<Checkbox
96-
label="Enable archival cron"
97-
checked={archivalEnabled}
96+
label="Enable deletion cron"
97+
checked={deletionEnabled}
9898
onChange={onToggleEnabled}
99-
helpText="When enabled, malicious events older than the configured archival time will be automatically archived."
99+
helpText="When enabled, malicious events older than the configured retention time will be automatically deleted."
100100
/>
101101
<Box width="200px">
102102
<Dropdown
103103
menuItems={options}
104104
selected={(val) => onChange(val)}
105-
label="Archival Time"
106-
initial={() => archivalDays}
105+
label="Retention Time"
106+
initial={() => `${deletionDays} days`}
107107
/>
108108
</Box>
109109
</VerticalStack>

apps/dashboard/web/polaris_web/web/src/apps/dashboard/pages/settings/threat_configuration/ThreatConfiguration.jsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ function ThreatConfiguration() {
1717
key={"ratelimitConfig"}
1818
/>,
1919
<ArchivalConfigComponent
20-
title={"Archival Configuration"}
21-
description={"Choose how long to retain malicious events before archival."}
22-
key={"archivalConfig"}
20+
title={"Deletion Configuration"}
21+
description={"Choose how long to retain malicious events before deletion."}
22+
key={"deletionConfig"}
2323
/>
2424
];
2525

apps/threat-detection-backend/src/main/java/com/akto/threat/backend/cron/ArchiveOldMaliciousEventsCron.java

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.akto.log.LoggerMaker;
44
import com.akto.dao.context.Context;
5-
import com.akto.threat.backend.dao.ArchivedMaliciousEventDao;
65
import com.akto.threat.backend.dao.MaliciousEventDao;
76
import com.akto.threat.backend.dao.ThreatConfigurationDao;
87
import com.mongodb.client.MongoClient;
@@ -29,8 +28,8 @@ public class ArchiveOldMaliciousEventsCron implements Runnable {
2928
private static final long DEFAULT_RETENTION_DAYS = 60L; // default, can be overridden from DB
3029
private static final long MIN_RETENTION_DAYS = 30L;
3130
private static final long MAX_RETENTION_DAYS = 90L;
32-
private static final long MAX_SOURCE_DOCS = 400_000L; // cap size
3331
private static final long MAX_DELETES_PER_ITERATION = 100_000L; // cap per cron iteration
32+
private static final long MAX_SOURCE_DOCS = 1000_000L;
3433

3534
private final MongoClient mongoClient;
3635
private final ScheduledExecutorService scheduler;
@@ -97,19 +96,18 @@ private boolean shouldSkipDatabase(String dbName) {
9796
private void archiveOldMaliciousEvents(String dbName, long nowSeconds) {
9897
String accountId = dbName;
9998

100-
// Check if archival is enabled for this account
101-
if (!isArchivalEnabled(accountId)) {
102-
logger.infoAndAddToDb("Archival is disabled for account " + accountId + ", skipping", LoggerMaker.LogDb.RUNTIME);
99+
// Check if deletion is enabled for this account
100+
if (!isDeletionEnabled(accountId)) {
101+
logger.infoAndAddToDb("Deletion is disabled for account " + accountId + ", skipping", LoggerMaker.LogDb.RUNTIME);
103102
return;
104103
}
105104

106105
long retentionDays = fetchRetentionDays(accountId);
107106
long threshold = nowSeconds - (retentionDays * 24 * 60 * 60);
108107

109108
MongoCollection<Document> source = MaliciousEventDao.instance.getDocumentCollection(accountId);
110-
MongoCollection<Document> dest = ArchivedMaliciousEventDao.instance.getCollection(accountId);
111109

112-
int totalMoved = 0;
110+
int totalDeleted = 0;
113111
long deletesThisIteration = 0L;
114112

115113
while (true) {
@@ -132,14 +130,12 @@ private void archiveOldMaliciousEvents(String dbName, long nowSeconds) {
132130
ids.add(doc.get("_id"));
133131
}
134132

135-
asyncUpsertToArchive(batch, accountId);
136-
137133
long deleted = deleteByIds(source, ids, accountId);
138-
totalMoved += (int) deleted;
134+
totalDeleted += (int) deleted;
139135
deletesThisIteration += deleted;
140136

141137
long iterationElapsedMs = (System.nanoTime() - iterationStartNanos) / 1_000_000L;
142-
logger.infoAndAddToDb("Archive loop iteration in db " + dbName + ": batch=" + batch.size() + ", deleted=" + deleted + ", tookMs=" + iterationElapsedMs, LoggerMaker.LogDb.RUNTIME);
138+
logger.infoAndAddToDb("Delete loop iteration in db " + dbName + ": batch=" + batch.size() + ", deleted=" + deleted + ", tookMs=" + iterationElapsedMs, LoggerMaker.LogDb.RUNTIME);
143139

144140
if (batch.size() < BATCH_SIZE) {
145141
break;
@@ -151,14 +147,13 @@ private void archiveOldMaliciousEvents(String dbName, long nowSeconds) {
151147
}
152148
}
153149

154-
if (totalMoved > 0) {
155-
logger.infoAndAddToDb("Completed archiving for db " + dbName + ", total moved: " + totalMoved, LoggerMaker.LogDb.RUNTIME);
150+
if (totalDeleted > 0) {
151+
logger.infoAndAddToDb("Completed deletion for db " + dbName + ", total deleted: " + totalDeleted, LoggerMaker.LogDb.RUNTIME);
156152
}
157153

158-
// Enforce collection size cap by trimming oldest docs beyond 400k.
159154
try {
160155
if (deletesThisIteration < MAX_DELETES_PER_ITERATION) {
161-
trimCollectionIfExceedsCap(accountId, source, dest);
156+
trimCollectionIfExceedsCap(accountId, source);
162157
} else {
163158
logger.infoAndAddToDb("Skipping trim step as delete cap reached in db " + dbName, LoggerMaker.LogDb.RUNTIME);
164159
}
@@ -167,7 +162,7 @@ private void archiveOldMaliciousEvents(String dbName, long nowSeconds) {
167162
}
168163
}
169164

170-
private boolean isArchivalEnabled(String accountId) {
165+
private boolean isDeletionEnabled(String accountId) {
171166
try {
172167
Document doc = ThreatConfigurationDao.instance.getCollection(accountId).find().first();
173168
if (doc == null) return false; // disabled by default
@@ -199,7 +194,7 @@ private long fetchRetentionDays(String accountId) {
199194
return DEFAULT_RETENTION_DAYS;
200195
}
201196

202-
private void trimCollectionIfExceedsCap(String accountId, MongoCollection<Document> source, MongoCollection<Document> dest) {
197+
private void trimCollectionIfExceedsCap(String accountId, MongoCollection<Document> source) {
203198
long approxCount = source.countDocuments();
204199

205200
if (approxCount <= MAX_SOURCE_DOCS) return;
@@ -223,8 +218,6 @@ private void trimCollectionIfExceedsCap(String accountId, MongoCollection<Docume
223218

224219
if (oldestDocs.isEmpty()) break;
225220

226-
asyncUpsertToArchive(oldestDocs, accountId);
227-
228221
Set<Object> ids = new HashSet<>();
229222
for (Document d : oldestDocs) {
230223
ids.add(d.get("_id"));
@@ -242,19 +235,6 @@ private void trimCollectionIfExceedsCap(String accountId, MongoCollection<Docume
242235
}
243236
}
244237

245-
private void asyncUpsertToArchive(List<Document> docs, String accountId) {
246-
if (docs == null || docs.isEmpty()) return;
247-
final List<Document> docsSnapshot = new ArrayList<>(docs);
248-
final String accountIdFinal = accountId;
249-
this.scheduler.submit(() -> {
250-
try {
251-
ArchivedMaliciousEventDao.instance.bulkInsert(accountIdFinal, docsSnapshot);
252-
} catch (Exception e) {
253-
logger.errorAndAddToDb("Async error writing archive batch in account " + accountIdFinal + ": " + e.getMessage(), LoggerMaker.LogDb.RUNTIME);
254-
}
255-
});
256-
}
257-
258238
private long deleteByIds(MongoCollection<Document> source, Set<Object> ids, String accountId) {
259239
if (ids == null || ids.isEmpty()) return 0L;
260240
try {

0 commit comments

Comments
 (0)