Code review comment: Hmm, we could also read the session status as `PREPARED`, and active as `false`, but then the session is activated while we delete it. I don't remember without looking at the code, but I believe those writes are protected by a lock. Alternatively, we could do the deletion with a zknode version condition ... which I'm not sure we expose through our own APIs >_<

Code before review:
public void deleteExpiredRemoteAndLocalSessions(Clock clock, Predicate<Session> sessionIsActiveForApplication) {
Set<Long> sessions = getLocalSessionsIdsFromFileSystem();
sessions.addAll(getRemoteSessionsFromZooKeeper());
log.log(Level.FINE, () -> "Sessions for tenant " + tenantName + ": " + sessions);
Set<Long> newSessions = findNewSessionsInFileSystem();
sessions.removeAll(newSessions);
int deleteMax = (int) Math.min(1000, Math.max(50, sessions.size() * 0.05));
int deleted = 0;
for (Long sessionId : sessions) {
try {
Session session = remoteSessionCache.get(sessionId);
if (session == null)
session = new RemoteSession(tenantName, sessionId, createSessionZooKeeperClient(sessionId));
Session.Status status = session.getStatus();
boolean activeForApplication = sessionIsActiveForApplication.test(session);
if (status == ACTIVATE && activeForApplication) continue;
Instant createTime = session.getCreateTime();
boolean hasExpired = hasExpired(createTime);
if ( ! hasExpired) continue;
log.log(Level.FINE, () -> "Remote session " + sessionId + " for " + tenantName + " has expired, deleting it");
deleteRemoteSessionFromZooKeeper(session);
deleted++;
log.log(Level.FINE, () -> "Expired local session is candidate for deletion: " + sessionId +
", created: " + createTime + ", status " + status + ", can be deleted: " + canBeDeleted(sessionId, status));
if (canBeDeleted(sessionId, status)) {
deleteLocalSession(sessionId);
deleted++;
} else if (isOldAndCanBeDeleted(sessionId, createTime)) {
var localSession = getOptionalSessionFromFileSystem(sessionId);
if (localSession.isEmpty()) continue;
var applicationId = localSession.get().getOptionalApplicationId();
if (applicationId.isEmpty()) continue;
if ( ! activeForApplication) {
log.log(Level.FINE, () -> "Will delete expired session " + sessionId + " created " +
createTime + " for '" + applicationId + "'");
deleteLocalSession(sessionId);
deleted++;
}
}
if (deleted >= deleteMax)
return;
} catch (Throwable e) {
log.log(Level.WARNING, "Error when deleting expired sessions ", e);
}
}
log.log(Level.FINE, () -> "Done deleting expired sessions");
}

Line under review: deleted++;

Code after addressing the review:
public void deleteExpiredRemoteAndLocalSessions(Clock clock, Predicate<Session> sessionIsActiveForApplication) {
Set<Long> sessions = getLocalSessionsIdsFromFileSystem();
sessions.addAll(getRemoteSessionsFromZooKeeper());
log.log(Level.FINE, () -> "Sessions for tenant " + tenantName + ": " + sessions);
Set<Long> newSessions = findNewSessionsInFileSystem();
sessions.removeAll(newSessions);
int deleteMax = (int) Math.min(1000, Math.max(50, sessions.size() * 0.05));
int deleted = 0;
for (Long sessionId : sessions) {
try {
Session session = remoteSessionCache.get(sessionId);
if (session == null)
session = new RemoteSession(tenantName, sessionId, createSessionZooKeeperClient(sessionId));
var applicationId = session.getApplicationId();
try (var ignored = applicationRepo.lock(applicationId)) {
Session.Status status = session.getStatus();
boolean activeForApplication = sessionIsActiveForApplication.test(session);
if (status == ACTIVATE && activeForApplication) continue;
Instant createTime = session.getCreateTime();
boolean hasExpired = hasExpired(createTime);
if (! hasExpired) continue;
log.log(Level.FINE, () -> "Remote session " + sessionId + " for " + tenantName + " has expired, deleting it");
deleteRemoteSessionFromZooKeeper(session);
deleted++;
log.log(Level.FINE, () -> "Expired local session is candidate for deletion: " + sessionId +
", created: " + createTime + ", status " + status + ", can be deleted: " + canBeDeleted(sessionId, status));
if (canBeDeleted(sessionId, status)) {
deleteLocalSession(sessionId);
deleted++;
} else if (isOldAndCanBeDeleted(sessionId, createTime)) {
var localSession = getOptionalSessionFromFileSystem(sessionId);
if (localSession.isEmpty()) continue;
if (! activeForApplication) {
log.log(Level.FINE, () -> "Will delete expired session " + sessionId + " created " +
createTime + " for '" + applicationId + "'");
deleteLocalSession(sessionId);
deleted++;
}
}
if (deleted >= deleteMax)
return;
}
} catch (Throwable e) {
log.log(Level.WARNING, "Error when deleting expired sessions ", e);
}
}
log.log(Level.FINE, () -> "Done deleting expired sessions");
}