Skip to content

Commit bdb9be6

Browse files
committed
SEBSP-210 screenshot data session cache PoC impl
1 parent 36f7073 commit bdb9be6

File tree

4 files changed

+31
-19
lines changed

4 files changed

+31
-19
lines changed

src/main/java/ch/ethz/seb/sps/server/datalayer/dao/impl/ScreenshotDataDAOBatis.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,14 +255,19 @@ public Result<Map<String, ScreenshotDataRecord>> allOfMappedToSession(final List
255255
if (pks == null || pks.isEmpty()) {
256256
return Collections.emptyMap();
257257
}
258+
259+
// TODO only for testing remove this
260+
if (log.isDebugEnabled()) {
261+
log.debug("Get Screenshotdata for gallery view: {}", pks);
262+
}
258263

259264
return screenshotDataRecordMapper
260265
.selectByExample()
261266
.where(id, isIn(pks))
262267
.build()
263268
.execute()
264269
.stream()
265-
.collect(Collectors.toMap(r -> r.getSessionUuid(), Function.identity()));
270+
.collect(Collectors.toMap(ScreenshotDataRecord::getSessionUuid, Function.identity()));
266271
});
267272
}
268273

src/main/java/ch/ethz/seb/sps/server/datalayer/dao/impl/ScreenshotDataLiveCacheDAOBatis.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public ScreenshotDataLiveCacheDAOBatis(ScreenshotDataLiveCacheRecordMapper scree
3737
@Transactional
3838
public Result<ScreenshotDataLiveCacheRecord> createCacheEntry(String sessionUUID) {
3939
return Result
40-
.tryCatch(() -> screenshotDataLiveCacheRecordMapper.selectByPrimaryKey(createSlot(sessionUUID, -1L)))
40+
.tryCatch(() -> screenshotDataLiveCacheRecordMapper.selectByPrimaryKey(createSlot(sessionUUID, null)))
4141
.onError(TransactionHandler::rollback);
4242
}
4343

@@ -87,14 +87,20 @@ public Result<List<String>> deleteAll(List<String> sessionUUIDs) {
8787
@Override
8888
@Transactional(readOnly = true)
8989
public Result<Collection<ScreenshotDataLiveCacheRecord>> getAll() {
90-
return Result.tryCatch(() -> screenshotDataLiveCacheRecordMapper.selectByExample().build().execute());
90+
return Result.tryCatch(() -> {
91+
final List<ScreenshotDataLiveCacheRecord> execute = screenshotDataLiveCacheRecordMapper
92+
.selectByExample()
93+
.build()
94+
.execute();
95+
return execute;
96+
});
9197
}
9298

9399
private synchronized Long createSlot(String sessionUUID, Long value) {
94100
final ScreenshotDataLiveCacheRecord rec = new ScreenshotDataLiveCacheRecord(
95101
null,
96102
sessionUUID,
97-
value );
103+
value != null && value >= 0 ? value : null );
98104

99105
screenshotDataLiveCacheRecordMapper.insert(rec);
100106
return rec.getId();

src/main/java/ch/ethz/seb/sps/server/servicelayer/impl/LiveProctoringCacheServiceImpl.java

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ public void init() {
107107
this::updateFromStoreCache,
108108
DateTime.now(DateTimeZone.UTC).toDate().toInstant(),
109109
Duration.ofMillis(this.batchInterval));
110-
111110

112111
} catch (Exception e) {
113112
ServiceInit.INIT_LOGGER.error("----> Live Proctoring Cache Service : failed to initialized", e);
@@ -142,16 +141,10 @@ public Long getLatestSSDataId(final String sessionUUID, final boolean createSlot
142141
public void updateCacheStore(final Collection<ScreenshotQueueData> batch) {
143142
try {
144143

145-
// // TODO check if this is really necessary
146-
// // fist ensure that all slots are existing
147-
// batch.forEach(data -> {
148-
// if (!cache.containsKey(data.record.getSessionUuid())) {
149-
// log.warn("Missing cache slot, try to create one...");
150-
// screenshotDataLiveCacheDAO.createCacheEntry(data.record.getSessionUuid())
151-
// .onError(error -> log.error("Failed to create cache entry: {}", error.getMessage()));
152-
// }
153-
// });
154-
//
144+
if (log.isDebugEnabled()) {
145+
log.debug("Update store cache with batch of: {}", batch.size());
146+
}
147+
155148
// then batch update
156149
this.transactionTemplate.executeWithoutResult(status -> {
157150
batch.forEach(data -> {
@@ -236,7 +229,7 @@ private void updateFromStoreCache() {
236229
.stream()
237230
.collect(Collectors.toMap(
238231
ScreenshotDataLiveCacheRecord::getSessionUuid,
239-
ScreenshotDataLiveCacheRecord::getIdLatestSsd
232+
rec -> rec.getIdLatestSsd() == null ? -1L : rec.getIdLatestSsd()
240233
));
241234

242235
cache.putAll(newValues);

src/main/java/ch/ethz/seb/sps/server/weblayer/AdminProctorController.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.util.concurrent.atomic.AtomicInteger;
1616
import java.util.stream.Collectors;
1717

18+
import ch.ethz.seb.sps.domain.api.TooManyRequests;
1819
import ch.ethz.seb.sps.domain.model.EntityType;
1920
import ch.ethz.seb.sps.domain.model.service.DistinctMetadataWindowForExam;
2021
import ch.ethz.seb.sps.domain.model.service.UserListForApplicationSearch;
@@ -76,7 +77,7 @@ public class AdminProctorController {
7677
private final GroupingService groupingService;
7778
private final UserService userService;
7879

79-
private AtomicInteger criticalRequestBucket = new AtomicInteger(10);
80+
private AtomicInteger criticalRequestBucket = new AtomicInteger(2);
8081

8182
public AdminProctorController(
8283
final GroupDAO groupDAO,
@@ -232,7 +233,8 @@ public ScreenshotsInGroupData getSessionsByGroup(
232233
log.warn("criticalRequestBucket empty! This will deny request in the future");
233234
}
234235
}
235-
criticalRequestBucket.decrementAndGet();
236+
int token = criticalRequestBucket.decrementAndGet();
237+
System.out.println("******************* token" + token);
236238

237239
this.proctoringService.checkMonitoringAccess(groupUUID);
238240

@@ -297,9 +299,15 @@ public ScreenshotViewData getScreenshotViewData(
297299
if (criticalRequestBucket.get() <= 0) {
298300
if (log.isDebugEnabled()) {
299301
log.warn("criticalRequestBucket empty! This will deny request in the future");
302+
throw new TooManyRequests();
300303
}
301304
}
302-
criticalRequestBucket.decrementAndGet();
305+
int token = criticalRequestBucket.decrementAndGet();
306+
System.out.println("******************* token " + token);
307+
308+
try {
309+
Thread.sleep(10000);
310+
} catch (Exception e) {}
303311

304312
Long ts = null;
305313
if (StringUtils.isNotBlank(timestamp)) {

0 commit comments

Comments
 (0)