-
Notifications
You must be signed in to change notification settings - Fork 30
Calculate running sessions per user #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
589a440
Revert "add getHosts method to Browsers bean"
baeda85
rft: extract JsonWireUtils class from ProxyServlet
0dbd8ef
calculate open sessions separately for each user
42558eb
evict sessions older than a couple of minutes
eb3db00
calculate sessions separately for each browser/version
f195dd3
return last hub error when unable to create session
92831da
fix quota reload test instability
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
22 changes: 0 additions & 22 deletions
22
config/src/main/java/ru/qatools/gridrouter/config/WithHosts.java
This file was deleted.
Oops, something went wrong.
13 changes: 10 additions & 3 deletions
13
config/src/main/java/ru/qatools/gridrouter/config/WithRoutesMap.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,24 @@ | ||
package ru.qatools.gridrouter.config; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* @author Dmitry Baev charlie@yandex-team.ru | ||
* @author Innokenty Shuvalov innokenty@yandex-team.ru | ||
*/ | ||
public interface WithRoutesMap extends WithHosts { | ||
public interface WithRoutesMap { | ||
|
||
List<Browser> getBrowsers(); | ||
|
||
default Map<String, String> getRoutesMap() { | ||
Map<String, String> routes = new HashMap<>(); | ||
getHosts().forEach(h -> routes.put(h.getRouteId(), h.getRoute())); | ||
HashMap<String, String> routes = new HashMap<>(); | ||
getBrowsers().stream() | ||
.flatMap(b -> b.getVersions().stream()) | ||
.flatMap(v -> v.getRegions().stream()) | ||
.flatMap(r -> r.getHosts().stream()) | ||
.forEach(h -> routes.put(h.getRouteId(), h.getRoute())); | ||
return routes; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
proxy/src/main/java/ru/qatools/gridrouter/JsonWireUtils.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package ru.qatools.gridrouter; | ||
|
||
import org.apache.http.client.utils.URIBuilder; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import java.io.UnsupportedEncodingException; | ||
import java.net.URISyntaxException; | ||
import java.net.URLDecoder; | ||
|
||
import static java.nio.charset.StandardCharsets.UTF_8; | ||
import static org.springframework.http.HttpMethod.DELETE; | ||
|
||
/** | ||
* @author Alexander Andyashin aandryashin@yandex-team.ru | ||
* @author Innokenty Shuvalov innokenty@yandex-team.ru | ||
* @author Dmitry Baev charlie@yandex-team.ru | ||
* @author Artem Eroshenko eroshenkoam@yandex-team.ru | ||
*/ | ||
public final class JsonWireUtils { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(JsonWireUtils.class); | ||
|
||
public static final String WD_HUB_SESSION = "/wd/hub/session/"; | ||
|
||
public static final int SESSION_HASH_LENGTH = 32; | ||
|
||
private JsonWireUtils() { | ||
} | ||
|
||
public static boolean isUriValid(String uri) { | ||
return uri.length() > getUriPrefixLength(); | ||
} | ||
|
||
public static boolean isSessionDeleteRequest(HttpServletRequest request, String command) { | ||
return DELETE.name().equalsIgnoreCase(request.getMethod()) && !command.contains("/"); | ||
} | ||
|
||
public static String getSessionHash(String uri) { | ||
return uri.substring(WD_HUB_SESSION.length(), getUriPrefixLength()); | ||
} | ||
|
||
public static String getFullSessionId(String uri) { | ||
String tail = uri.substring(WD_HUB_SESSION.length()); | ||
int end = tail.indexOf('/'); | ||
if (end < 0) { | ||
return tail; | ||
} | ||
return tail.substring(0, end); | ||
} | ||
|
||
public static int getUriPrefixLength() { | ||
return WD_HUB_SESSION.length() + SESSION_HASH_LENGTH; | ||
} | ||
|
||
public static String redirectionUrl(String host, String command) throws URISyntaxException { | ||
return new URIBuilder(host).setPath(WD_HUB_SESSION + command).build().toString(); | ||
} | ||
|
||
public static String getCommand(String uri) { | ||
String encodedCommand = uri.substring(getUriPrefixLength()); | ||
try { | ||
return URLDecoder.decode(encodedCommand, UTF_8.name()); | ||
} catch (UnsupportedEncodingException e) { | ||
LOGGER.error("[UNABLE_TO_DECODE_COMMAND] - could not decode command: {}", encodedCommand, e); | ||
return encodedCommand; | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,10 +17,10 @@ | |
import ru.qatools.gridrouter.config.HostSelectionStrategy; | ||
import ru.qatools.gridrouter.config.Region; | ||
import ru.qatools.gridrouter.config.Version; | ||
import ru.qatools.gridrouter.json.GridStats; | ||
import ru.qatools.gridrouter.json.JsonCapabilities; | ||
import ru.qatools.gridrouter.json.JsonMessage; | ||
import ru.qatools.gridrouter.json.JsonMessageFactory; | ||
import ru.qatools.gridrouter.sessions.SessionStorage; | ||
|
||
import javax.servlet.ServletConfig; | ||
import javax.servlet.ServletException; | ||
|
@@ -64,7 +64,7 @@ public class RouteServlet extends HttpServlet { | |
private HostSelectionStrategy hostSelectionStrategy; | ||
|
||
@Autowired | ||
private GridStats stats; | ||
private SessionStorage sessionStorage; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
@Autowired | ||
private CapabilityProcessorFactory capabilityProcessorFactory; | ||
|
@@ -103,6 +103,7 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response) | |
List<Region> unvisitedRegions = new ArrayList<>(allRegions); | ||
|
||
int attempt = 0; | ||
JsonMessage hubMessage = null; | ||
try (CloseableHttpClient client = newHttpClient()) { | ||
while (!allRegions.isEmpty()) { | ||
attempt++; | ||
|
@@ -116,15 +117,15 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response) | |
|
||
String target = route + request.getRequestURI(); | ||
HttpResponse hubResponse = client.execute(post(target, message)); | ||
JsonMessage hubMessage = JsonMessageFactory.from(hubResponse.getEntity().getContent()); | ||
hubMessage = JsonMessageFactory.from(hubResponse.getEntity().getContent()); | ||
|
||
if (hubResponse.getStatusLine().getStatusCode() == SC_OK) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
String sessionId = hubMessage.getSessionId(); | ||
hubMessage.setSessionId(host.getRouteId() + sessionId); | ||
replyWithOk(hubMessage, response); | ||
LOGGER.info("[SESSION_CREATED] [{}] [{}] [{}] [{}] [{}] [{}]", | ||
user, remoteHost, browser, route, sessionId, attempt); | ||
stats.startSession(); | ||
sessionStorage.put(hubMessage.getSessionId(), user, browser, actualVersion.getNumber()); | ||
return; | ||
} | ||
LOGGER.warn("[SESSION_FAILED] [{}] [{}] [{}] [{}] - {}", | ||
|
@@ -150,15 +151,23 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response) | |
} | ||
|
||
LOGGER.error("[SESSION_NOT_CREATED] [{}] [{}] [{}]", user, remoteHost, browser); | ||
replyWithError("Cannot create session on any available node", response); | ||
if (hubMessage == null) { | ||
replyWithError("Cannot create session on any available node", response); | ||
} else { | ||
replyWithError(hubMessage, response); | ||
} | ||
} | ||
|
||
protected void replyWithOk(JsonMessage message, HttpServletResponse response) throws IOException { | ||
reply(SC_OK, message, response); | ||
} | ||
|
||
protected void replyWithError(String errorMessage, HttpServletResponse response) throws IOException { | ||
reply(SC_INTERNAL_SERVER_ERROR, JsonMessageFactory.error(13, errorMessage), response); | ||
replyWithError(JsonMessageFactory.error(13, errorMessage), response); | ||
} | ||
|
||
protected void replyWithError(JsonMessage message, HttpServletResponse response) throws IOException { | ||
reply(SC_INTERNAL_SERVER_ERROR, message, response); | ||
} | ||
|
||
protected void reply(int code, JsonMessage message, HttpServletResponse response) throws IOException { | ||
|
29 changes: 29 additions & 0 deletions
29
proxy/src/main/java/ru/qatools/gridrouter/SessionStorageEvictionScheduler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package ru.qatools.gridrouter; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.scheduling.annotation.EnableScheduling; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
import ru.qatools.gridrouter.sessions.SessionStorage; | ||
|
||
import java.time.Duration; | ||
|
||
/** | ||
* @author Innokenty Shuvalov innokenty@yandex-team.ru | ||
*/ | ||
@Configuration | ||
@EnableScheduling | ||
public class SessionStorageEvictionScheduler { | ||
|
||
@Value("${grid.router.evict.sessions.timeout.seconds}") | ||
private int timeout; | ||
|
||
@Autowired | ||
private SessionStorage sessionStorage; | ||
|
||
@Scheduled(cron = "${grid.router.evict.sessions.cron}") | ||
public void expireOldSessions() { | ||
sessionStorage.expireSessionsOlderThan(Duration.ofSeconds(timeout)); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.