Skip to content

Commit

Permalink
feat(jans-auth-server): added batch index update and fixed concurrent…
Browse files Browse the repository at this point in the history
… update issue

#8562
Signed-off-by: YuriyZ <yzabrovarniy@gmail.com>
  • Loading branch information
yuriyz committed Jun 26, 2024
1 parent d5b2e82 commit 0a1842c
Showing 1 changed file with 38 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;

/**
Expand Down Expand Up @@ -39,29 +42,52 @@ public class StatusListIndexService {

private StatusIndexPool tokenPool = null;

public void updateStatusAtIndex(int index, TokenStatus status) {
public synchronized void updateStatusAtIndexes(List<Integer> indexes, TokenStatus status) {
try {
if (index < 0) {
log.debug("Updating status list at indexes {} with status {} ...", indexes, status);

if (indexes == null || indexes.isEmpty()) {
return; // invalid
}

log.trace("Updating status list at index {} with status {} ...", index, status);

final int bitSize = appConfiguration.getStatusListBitSize();
final StatusIndexPool indexHolder = statusTokenPoolService.getPoolByIndex(index);
final String data = indexHolder.getData();

final StatusList statusList = StringUtils.isNotBlank(data) ? StatusList.fromEncoded(data, bitSize) : new StatusList(bitSize);
statusList.set(index, status.getValue());
// first load pool for indexes and update in-memory
Map<Integer, StatusIndexPool> pools = new HashMap<>();
for (Integer index : indexes) {
int poolId = index / appConfiguration.getStatusListIndexAllocationBlockSize();

StatusIndexPool indexHolder = pools.get(poolId);
if (indexHolder == null) {
indexHolder = statusTokenPoolService.getPoolByIndex(index);
log.debug("Found pool {} by index {}", indexHolder.getDn(), index);
pools.put(indexHolder.getId(), indexHolder);
}

indexHolder.setData(statusList.getLst());
final String data = indexHolder.getData();

statusTokenPoolService.updateWithLock(indexHolder);
final StatusList statusList = StringUtils.isNotBlank(data) ? StatusList.fromEncoded(data, bitSize) : new StatusList(bitSize);
statusList.set(index, status.getValue());

log.trace("Updated status list at index {} with status {} successfully.", index, status);
indexHolder.setData(statusList.getLst());
}

for (StatusIndexPool pool : pools.values()) {
updateWithLockSilently(pool);
}

log.debug("Updated status list at index {} with status {} successfully.", indexes, status);

} catch (Exception e) {
log.error("Failed to update token list status at index " + indexes + " with status " + status, e);
}
}

private void updateWithLockSilently(StatusIndexPool pool) {
try {
statusTokenPoolService.updateWithLock(pool);
} catch (Exception e) {
log.error("Failed to update token list status at index " + index + " with status " + status, e);
log.error("Failed to persist status index pool " + pool.getId(), e);
}
}

Expand Down

0 comments on commit 0a1842c

Please sign in to comment.