fix Bug #19192 RStudio Wrapper portlet does not support double queries

This commit is contained in:
Massimiliano Assante 2020-05-06 11:17:13 +02:00
parent 2350a32705
commit b99319c450
1 changed files with 50 additions and 44 deletions

View File

@ -159,7 +159,7 @@ public class RStudioServiceImpl extends RemoteServiceServlet implements RStudioS
return toReturn; return toReturn;
} }
/** /**
* new method * new method
* @param pContext * @param pContext
@ -169,66 +169,72 @@ public class RStudioServiceImpl extends RemoteServiceServlet implements RStudioS
* @param token * @param token
* @return * @return
*/ */
private String getRStudioGCoreEndpoint(PortalContext pContext, long userId, GCubeUser curUser, String scope, String token) { private String getRStudioGCoreEndpoint(PortalContext pContext, long userId, GCubeUser curUser, String context, String token) {
String toReturn = ""; String toReturn = "";
UserManager um = new LiferayUserManager(); UserManager um = new LiferayUserManager();
_log.debug("Checking if user " + curUser.getFullname() + " has already an RStudio Instance set ... "); _log.debug("Checking if user " + curUser.getFullname() + " has already an RStudio Instance set ... ");
String hostedOnSet = null; String hostedOnSet = null;
try { try {
//check if an RStudio instance was previously set (RETRO-COMPATIBILY) Map<String, String> userRStudioInstances = getUserRStudioInstances(um, curUser);
if (um.readCustomAttr(userId, RSTUDIO_URL) != null && um.readCustomAttr(userId, RSTUDIO_URL).toString().compareTo("") != 0) { hostedOnSet = userRStudioInstances.get(context);
_log.debug("User had already an RStudio Instance set, upgrading to new version "); _log.info("**** Checking if the user instance " + hostedOnSet+" exists in the VO for this VRE: " + context);
hostedOnSet = (String) um.readCustomAttr(userId, RSTUDIO_URL);
writeRStudioInstanceInScope(um, curUser, scope, hostedOnSet);
um.saveCustomAttr(userId, RSTUDIO_URL, ""); //reset the old value to blank so that from now on it will read from the new field
}
hostedOnSet = getUserRStudioInstances(um,curUser).get(scope);
_log.info("**** Checking if the user instance " + hostedOnSet+" exists in the VO for this VRE: " + scope);
//if the instance exists and is still available in the given context //if the instance exists and is still available in the given context
List<GCoreEndpoint> gCoreResources = getRStudioGCoreEndpoints(scope); List<GCoreEndpoint> gCoreResources = getRStudioGCoreEndpoints(context);
if (hostedOnSet != null && checkRStudioGCoreInstanceExistence(curUser, hostedOnSet, gCoreResources) ) { if (hostedOnSet != null && checkRStudioGCoreInstanceExistence(curUser, hostedOnSet, gCoreResources) ) {
toReturn = getRConnectorURL(hostedOnSet, token); toReturn = getRConnectorURL(hostedOnSet, token);
_log.info("User " + curUser.getFullname() + " has RStudio Instance set and is valid, returning rConnector URL " + toReturn); _log.info("User " + curUser.getFullname() + " has RStudio Instance set and is valid, returning rConnector URL " + toReturn);
} }
else {//need to find the RStudio else {//need to find the RStudio
_log.info("User " + curUser.getFullname() + " DOES NOT have RStudio Instance set or the instance previous set no longer exists, calculating allocation ... "); //check first if there is an instance allocated on another scope
HashMap<String, Integer> rStudioDistributionMap = new HashMap<>(); _log.info("User " + curUser.getFullname() + " DOES NOT have RStudio Instance set for this context "+context + " check if she as an instance assigned on another context");
for (GCoreEndpoint res : gCoreResources) { if (! userRStudioInstances.isEmpty()) {
String hostedOn = extractGCoreEndpointHostAndPort(res); String theFirstInstance = userRStudioInstances.values().iterator().next();
rStudioDistributionMap.put(hostedOn, 0); toReturn = getRConnectorURL(theFirstInstance, token);
_log.info("Found instance: "+ theFirstInstance);
writeRStudioInstanceInScope(um, curUser, context, theFirstInstance);
_log.debug("User " + curUser.getFullname() + " RStudio gCore Instance set reused = " + theFirstInstance + " for new context " + context);
} }
List<GCubeUser> vreUsers = um.listUsersByGroup(pContext.getCurrentGroupId(getThreadLocalRequest()), false); else { //no instance allocated on another scope
_log.debug("VRE " + scope + " has totalUsers = " + vreUsers.size()); _log.info("User " + curUser.getFullname() + " DOES NOT have RStudio Instance set or the instance previous set no longer exists, calculating allocation ... ");
for (GCubeUser gCubeUser : vreUsers) {
if (getUserRStudioInstances(um,gCubeUser).get(scope) != null) { HashMap<String, Integer> rStudioDistributionMap = new HashMap<>();
String hostedOn = getUserRStudioInstances(um,gCubeUser).get(scope); for (GCoreEndpoint res : gCoreResources) {
if (rStudioDistributionMap.containsKey(hostedOn)) { String hostedOn = extractGCoreEndpointHostAndPort(res);
int noToSet = rStudioDistributionMap.get(hostedOn)+1; rStudioDistributionMap.put(hostedOn, 0);
rStudioDistributionMap.put(hostedOn, noToSet); }
List<GCubeUser> vreUsers = um.listUsersByGroup(pContext.getCurrentGroupId(getThreadLocalRequest()), false);
_log.debug("VRE " + context + " has totalUsers = " + vreUsers.size());
for (GCubeUser gCubeUser : vreUsers) {
if (getUserRStudioInstances(um,gCubeUser).get(context) != null) {
String hostedOn = getUserRStudioInstances(um,gCubeUser).get(context);
if (rStudioDistributionMap.containsKey(hostedOn)) {
int noToSet = rStudioDistributionMap.get(hostedOn)+1;
rStudioDistributionMap.put(hostedOn, noToSet);
}
} }
} }
} _log.debug("VRE - RStudio allocaiton map as follows: ");
_log.debug("VRE - RStudio allocaiton map as follows: "); int min = 0;
int min = 0; int i = 0;
int i = 0; String host2Select = "";
String host2Select = ""; for (String host : rStudioDistributionMap.keySet()) {
for (String host : rStudioDistributionMap.keySet()) { _log.debug("Host " + host + " has # users=" + rStudioDistributionMap.get(host));
_log.debug("Host " + host + " has # users=" + rStudioDistributionMap.get(host)); if (i==0) {
if (i==0) { host2Select = host;
host2Select = host; min = rStudioDistributionMap.get(host);
min = rStudioDistributionMap.get(host); } else {
} else { int usersNo = rStudioDistributionMap.get(host);
int usersNo = rStudioDistributionMap.get(host); if (usersNo < min) {
if (usersNo < min) { _log.debug("Host " + host + " has LESS users than " + host2Select + " updating");
_log.debug("Host " + host + " has LESS users than " + host2Select + " updating"); host2Select = host;
host2Select = host; }
} }
i++;
} }
i++; writeRStudioInstanceInScope(um, curUser, context, host2Select);
_log.debug("User " + curUser.getFullname() + " RStudio gCore Instance set calculated = " + host2Select + " for context " + context);
toReturn = getRConnectorURL(host2Select, token);
} }
writeRStudioInstanceInScope(um, curUser, scope, host2Select);
_log.debug("User " + curUser.getFullname() + " RStudio gCore Instance set calculated = " + host2Select + " for context " + scope);
toReturn = getRConnectorURL(host2Select, token);
_log.debug("User " + curUser.getFullname() + " has RStudio gCore Instance set, returning rConnector URL " + toReturn); _log.debug("User " + curUser.getFullname() + " has RStudio gCore Instance set, returning rConnector URL " + toReturn);
} }