openvidu-server: SessionManager collections initialization refactoring

pull/331/head
pabloFuente 2019-10-04 12:51:56 +02:00
parent e8e85a2c58
commit bdc35d6654
2 changed files with 46 additions and 53 deletions

View File

@ -173,6 +173,10 @@ public abstract class SessionManager {
return this.sessionsNotActive.get(sessionId); return this.sessionsNotActive.get(sessionId);
} }
public Session getSessionWithNotActive(String sessionId) {
return (sessions.get(sessionId) != null ? sessions.get(sessionId) : sessionsNotActive.get(sessionId));
}
public Collection<Session> getSessionsWithNotActive() { public Collection<Session> getSessionsWithNotActive() {
Collection<Session> allSessions = new HashSet<>(); Collection<Session> allSessions = new HashSet<>();
allSessions.addAll(this.sessionsNotActive.values().stream() allSessions.addAll(this.sessionsNotActive.values().stream()
@ -262,24 +266,13 @@ public abstract class SessionManager {
public Session storeSessionNotActive(String sessionId, SessionProperties sessionProperties) { public Session storeSessionNotActive(String sessionId, SessionProperties sessionProperties) {
Session sessionNotActive = new Session(sessionId, sessionProperties, openviduConfig, recordingManager); Session sessionNotActive = new Session(sessionId, sessionProperties, openviduConfig, recordingManager);
this.sessionsNotActive.put(sessionId, sessionNotActive); return this.storeSessionNotActive(sessionNotActive);
this.sessionidParticipantpublicidParticipant.putIfAbsent(sessionId, new ConcurrentHashMap<>());
this.sessionidFinalUsers.putIfAbsent(sessionId, new ConcurrentHashMap<>());
if (this.openviduConfig.isRecordingModuleEnabled()) {
this.sessionidAccumulatedRecordings.putIfAbsent(sessionId, new ConcurrentLinkedQueue<>());
}
showTokens();
return sessionNotActive;
} }
public Session storeSessionNotActive(Session sessionNotActive) { public Session storeSessionNotActive(Session sessionNotActive) {
final String sessionId = sessionNotActive.getSessionId(); final String sessionId = sessionNotActive.getSessionId();
this.sessionsNotActive.put(sessionId, sessionNotActive); this.sessionsNotActive.put(sessionId, sessionNotActive);
this.sessionidParticipantpublicidParticipant.putIfAbsent(sessionId, new ConcurrentHashMap<>()); this.initializeCollections(sessionId);
this.sessionidFinalUsers.putIfAbsent(sessionId, new ConcurrentHashMap<>());
if (this.openviduConfig.isRecordingModuleEnabled()) {
this.sessionidAccumulatedRecordings.putIfAbsent(sessionId, new ConcurrentLinkedQueue<>());
}
showTokens(); showTokens();
return sessionNotActive; return sessionNotActive;
} }
@ -287,9 +280,9 @@ public abstract class SessionManager {
public String newToken(String sessionId, OpenViduRole role, String serverMetadata, public String newToken(String sessionId, OpenViduRole role, String serverMetadata,
KurentoTokenOptions kurentoTokenOptions) throws OpenViduException { KurentoTokenOptions kurentoTokenOptions) throws OpenViduException {
ConcurrentHashMap<String, Token> map = this.sessionidTokenTokenobj.putIfAbsent(sessionId, Map<String, Token> tokenMap = this.sessionidTokenTokenobj.get(sessionId);
new ConcurrentHashMap<>());
if (map != null) { if (tokenMap != null) {
if (!formatChecker.isServerMetadataFormatCorrect(serverMetadata)) { if (!formatChecker.isServerMetadataFormatCorrect(serverMetadata)) {
log.error("Data invalid format"); log.error("Data invalid format");
@ -297,14 +290,12 @@ public abstract class SessionManager {
} }
Token token = tokenGenerator.generateToken(sessionId, role, serverMetadata, kurentoTokenOptions); Token token = tokenGenerator.generateToken(sessionId, role, serverMetadata, kurentoTokenOptions);
tokenMap.putIfAbsent(token.getToken(), token);
map.putIfAbsent(token.getToken(), token);
showTokens(); showTokens();
return token.getToken(); return token.getToken();
} else { } else {
this.sessionidTokenTokenobj.remove(sessionId); log.error("sessionId [" + sessionId + "] was not found (race condition error)");
log.error("sessionId [" + sessionId + "] was not found");
throw new OpenViduException(Code.ROOM_NOT_FOUND_ERROR_CODE, "sessionId [" + sessionId + "] not found"); throw new OpenViduException(Code.ROOM_NOT_FOUND_ERROR_CODE, "sessionId [" + sessionId + "] not found");
} }
} }
@ -317,12 +308,7 @@ public abstract class SessionManager {
return false; return false;
} }
} else { } else {
this.sessionidParticipantpublicidParticipant.putIfAbsent(sessionId, new ConcurrentHashMap<>()); this.initializeCollections(sessionId);
this.sessionidFinalUsers.putIfAbsent(sessionId, new ConcurrentHashMap<>());
if (this.openviduConfig.isRecordingModuleEnabled()) {
this.sessionidAccumulatedRecordings.putIfAbsent(sessionId, new ConcurrentLinkedQueue<>());
}
this.sessionidTokenTokenobj.putIfAbsent(sessionId, new ConcurrentHashMap<>());
this.sessionidTokenTokenobj.get(sessionId).putIfAbsent(token, this.sessionidTokenTokenobj.get(sessionId).putIfAbsent(token,
new Token(token, OpenViduRole.PUBLISHER, "", new Token(token, OpenViduRole.PUBLISHER, "",
this.coturnCredentialsService.isCoturnAvailable() this.coturnCredentialsService.isCoturnAvailable()
@ -528,4 +514,13 @@ public abstract class SessionManager {
sessionidTokenTokenobj.remove(sessionId); sessionidTokenTokenobj.remove(sessionId);
} }
private void initializeCollections(String sessionId) {
this.sessionidParticipantpublicidParticipant.putIfAbsent(sessionId, new ConcurrentHashMap<>());
this.sessionidFinalUsers.putIfAbsent(sessionId, new ConcurrentHashMap<>());
this.sessionidTokenTokenobj.putIfAbsent(sessionId, new ConcurrentHashMap<>());
if (this.openviduConfig.isRecordingModuleEnabled()) {
this.sessionidAccumulatedRecordings.putIfAbsent(sessionId, new ConcurrentLinkedQueue<>());
}
}
} }

View File

@ -153,13 +153,12 @@ public class SessionRestController {
String sessionId; String sessionId;
if (customSessionId != null && !customSessionId.isEmpty()) { if (customSessionId != null && !customSessionId.isEmpty()) {
if (sessionManager.sessionidTokenTokenobj.putIfAbsent(customSessionId, new ConcurrentHashMap<>()) != null) { if (sessionManager.getSessionWithNotActive(customSessionId) != null) {
return new ResponseEntity<>(HttpStatus.CONFLICT); return new ResponseEntity<>(HttpStatus.CONFLICT);
} }
sessionId = customSessionId; sessionId = customSessionId;
} else { } else {
sessionId = RandomStringUtils.randomAlphanumeric(16).toLowerCase(); sessionId = RandomStringUtils.randomAlphanumeric(16).toLowerCase();
sessionManager.sessionidTokenTokenobj.putIfAbsent(sessionId, new ConcurrentHashMap<>());
} }
Session sessionNotActive = sessionManager.storeSessionNotActive(sessionId, sessionProperties); Session sessionNotActive = sessionManager.storeSessionNotActive(sessionId, sessionProperties);
@ -236,8 +235,11 @@ public class SessionRestController {
log.info("REST API: DELETE /api/sessions/{}/connection/{}", sessionId, participantPublicId); log.info("REST API: DELETE /api/sessions/{}/connection/{}", sessionId, participantPublicId);
Session session = this.sessionManager.getSession(sessionId); Session session = this.sessionManager.getSessionWithNotActive(sessionId);
if (session != null) { if (session == null) {
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
Participant participant = session.getParticipantByPublicId(participantPublicId); Participant participant = session.getParticipantByPublicId(participantPublicId);
if (participant != null) { if (participant != null) {
this.sessionManager.evictParticipant(participant, null, null, EndReason.forceDisconnectByServer); this.sessionManager.evictParticipant(participant, null, null, EndReason.forceDisconnectByServer);
@ -245,12 +247,6 @@ public class SessionRestController {
} else { } else {
return new ResponseEntity<>(HttpStatus.NOT_FOUND); return new ResponseEntity<>(HttpStatus.NOT_FOUND);
} }
} else {
if (this.sessionManager.getSessionNotActive(sessionId) != null) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
} }
@RequestMapping(value = "/sessions/{sessionId}/stream/{streamId}", method = RequestMethod.DELETE) @RequestMapping(value = "/sessions/{sessionId}/stream/{streamId}", method = RequestMethod.DELETE)
@ -259,7 +255,12 @@ public class SessionRestController {
log.info("REST API: DELETE /api/sessions/{}/stream/{}", sessionId, streamId); log.info("REST API: DELETE /api/sessions/{}/stream/{}", sessionId, streamId);
Session session = this.sessionManager.getSession(sessionId); Session session = this.sessionManager.getSessionWithNotActive(sessionId);
if (session == null) {
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
session = this.sessionManager.getSession(sessionId);
if (session != null) { if (session != null) {
if (this.sessionManager.unpublishStream(session, streamId, null, null, EndReason.forceUnpublishByServer)) { if (this.sessionManager.unpublishStream(session, streamId, null, null, EndReason.forceUnpublishByServer)) {
return new ResponseEntity<>(HttpStatus.NO_CONTENT); return new ResponseEntity<>(HttpStatus.NO_CONTENT);
@ -267,11 +268,8 @@ public class SessionRestController {
return new ResponseEntity<>(HttpStatus.NOT_FOUND); return new ResponseEntity<>(HttpStatus.NOT_FOUND);
} }
} else { } else {
if (this.sessionManager.getSessionNotActive(sessionId) != null) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND); return new ResponseEntity<>(HttpStatus.NOT_FOUND);
} }
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
} }
@RequestMapping(value = "/tokens", method = RequestMethod.POST) @RequestMapping(value = "/tokens", method = RequestMethod.POST)
@ -300,6 +298,11 @@ public class SessionRestController {
HttpStatus.BAD_REQUEST); HttpStatus.BAD_REQUEST);
} }
if (this.sessionManager.getSessionWithNotActive(sessionId) == null) {
return this.generateErrorResponse("Session " + sessionId + " not found", "/api/tokens",
HttpStatus.NOT_FOUND);
}
JsonObject kurentoOptions = null; JsonObject kurentoOptions = null;
if (params.get("kurentoOptions") != null) { if (params.get("kurentoOptions") != null) {
@ -335,13 +338,8 @@ public class SessionRestController {
metadata = (metadata != null) ? metadata : ""; metadata = (metadata != null) ? metadata : "";
String token; String token = sessionManager.newToken(sessionId, role, metadata, kurentoTokenOptions);
try {
token = sessionManager.newToken(sessionId, role, metadata, kurentoTokenOptions);
} catch (OpenViduException e) {
// Session was not found
return this.generateErrorResponse(e.getMessage(), "/api/tokens", HttpStatus.NOT_FOUND);
}
JsonObject responseJson = new JsonObject(); JsonObject responseJson = new JsonObject();
responseJson.addProperty("id", token); responseJson.addProperty("id", token);
responseJson.addProperty("session", sessionId); responseJson.addProperty("session", sessionId);