-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added heartbeat mechanism to detect and free up idle/closed sessions
- Loading branch information
1 parent
bcc75e4
commit 1232e50
Showing
6 changed files
with
118 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
35 changes: 35 additions & 0 deletions
35
src/main/java/eu/openanalytics/controllers/HeartbeatController.java
This file contains 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,35 @@ | ||
package eu.openanalytics.controllers; | ||
|
||
import java.io.IOException; | ||
import java.security.Principal; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
import javax.inject.Inject; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
|
||
import eu.openanalytics.services.HeartbeatService; | ||
|
||
@Controller | ||
public class HeartbeatController { | ||
|
||
@Inject | ||
HeartbeatService heartbeatService; | ||
|
||
@RequestMapping("/heartbeat/**") | ||
void heartbeat(Principal principal, HttpServletRequest request, HttpServletResponse response) { | ||
String userName = (principal == null) ? request.getSession().getId() : principal.getName(); | ||
Matcher matcher = Pattern.compile(".*/app/(.*)").matcher(request.getRequestURI()); | ||
String appName = matcher.matches() ? matcher.group(1) : null; | ||
heartbeatService.heartbeatReceived(userName, appName); | ||
try { | ||
response.setStatus(200); | ||
response.getWriter().write("Ok"); | ||
response.getWriter().flush(); | ||
} catch (IOException e) {} | ||
} | ||
} |
This file contains 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
66 changes: 66 additions & 0 deletions
66
src/main/java/eu/openanalytics/services/HeartbeatService.java
This file contains 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,66 @@ | ||
package eu.openanalytics.services; | ||
|
||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
import javax.annotation.PostConstruct; | ||
import javax.inject.Inject; | ||
|
||
import org.apache.log4j.Logger; | ||
import org.springframework.core.env.Environment; | ||
import org.springframework.stereotype.Service; | ||
|
||
import eu.openanalytics.services.DockerService.Proxy; | ||
|
||
@Service | ||
public class HeartbeatService { | ||
|
||
@Inject | ||
Environment environment; | ||
|
||
@Inject | ||
DockerService dockerService; | ||
|
||
private Logger log = Logger.getLogger(HeartbeatService.class); | ||
|
||
private Map<String, Long> heartbeatTimestamps; | ||
|
||
@PostConstruct | ||
public void init() { | ||
heartbeatTimestamps = new ConcurrentHashMap<>(); | ||
new Thread(new AppCleaner(), "HeartbeatThread").start(); | ||
} | ||
|
||
public void heartbeatReceived(String user, String app) { | ||
heartbeatTimestamps.put(user, System.currentTimeMillis()); | ||
} | ||
|
||
private class AppCleaner implements Runnable { | ||
@Override | ||
public void run() { | ||
long cleanupInterval = 2 * Long.parseLong(environment.getProperty("shiny.proxy.heartbeat-rate", "10000")); | ||
long heartbeatTimeout = Long.parseLong(environment.getProperty("shiny.proxy.heartbeat-timeout", "60000")); | ||
|
||
while (true) { | ||
try { | ||
long currentTimestamp = System.currentTimeMillis(); | ||
for (Proxy proxy: dockerService.listProxies()) { | ||
Long lastHeartbeat = heartbeatTimestamps.get(proxy.userName); | ||
if (lastHeartbeat == null) lastHeartbeat = proxy.startupTimestamp; | ||
long proxySilence = currentTimestamp - lastHeartbeat; | ||
if (proxySilence > heartbeatTimeout) { | ||
log.info(String.format("Releasing inactive proxy [user: %s] [app: %s] [silence: %dms]", proxy.userName, proxy.appName, proxySilence)); | ||
dockerService.releaseProxy(proxy.userName); | ||
heartbeatTimestamps.remove(proxy.userName); | ||
} | ||
} | ||
} catch (Throwable t) { | ||
log.error("Error in HeartbeatThread", t); | ||
} | ||
try { | ||
Thread.sleep(cleanupInterval); | ||
} catch (InterruptedException e) {} | ||
} | ||
} | ||
} | ||
} |
This file contains 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 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