-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
CHE-274 - Improve idling implementation #5512
Merged
Merged
Changes from 8 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
3533ef5
CHE-274 - Improve idling implementation
snjeza 8d4c554
Merge branch 'master' of github.com:eclipse/che into che-274a
mkuznyetsov 7cabcf2
Merge branch 'master' of github.com:eclipse/che into che-274a
mkuznyetsov 8958f42
CHE-274 - Improve idling implementation
mkuznyetsov 2b26e78
fixup! CHE-274 - Improve idling implementation
mkuznyetsov 1c83014
fixup! fixup! CHE-274 - Improve idling implementation
mkuznyetsov b2e1e22
fixup! fixup! fixup! CHE-274 - Improve idling implementation
mkuznyetsov aa1e796
fixup! fixup! fixup! fixup! CHE-274 - Improve idling implementation
mkuznyetsov fb0a363
fixup! fixup! fixup! fixup! fixup! CHE-274 - Improve idling implement…
mkuznyetsov fbebc79
fixup! fixup! fixup! fixup! fixup! fixup! CHE-274 - Improve idling im…
mkuznyetsov fd347cd
fixup! fixup! fixup! fixup! fixup! fixup! fixup! CHE-274 - Improve id…
mkuznyetsov 36031c6
fixup! fixup! fixup! fixup! fixup! fixup! fixup! fixup! CHE-274 - Imp…
mkuznyetsov 613f24c
fixup! fixup! fixup! fixup! fixup! fixup! fixup! fixup! fixup! CHE-27…
mkuznyetsov 656fbc8
Merge branch 'master' of github.com:eclipse/che into che-274a
mkuznyetsov 7e0a317
fixup
mkuznyetsov 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
|
||
Copyright (c) 2012-2017 Codenvy, S.A. | ||
All rights reserved. This program and the accompanying materials | ||
are made available under the terms of the Eclipse Public License v1.0 | ||
which accompanies this distribution, and is available at | ||
http://www.eclipse.org/legal/epl-v10.html | ||
|
||
Contributors: | ||
Codenvy, S.A. - initial API and implementation | ||
|
||
--> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<artifactId>che-agent-parent</artifactId> | ||
<groupId>org.eclipse.che.core</groupId> | ||
<version>5.15.0-SNAPSHOT</version> | ||
</parent> | ||
<artifactId>activity</artifactId> | ||
<name>Che Core :: API :: Activity</name> | ||
<dependencies> | ||
<dependency> | ||
<groupId>javax.annotation</groupId> | ||
<artifactId>javax.annotation-api</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>javax.inject</groupId> | ||
<artifactId>javax.inject</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.eclipse.che.core</groupId> | ||
<artifactId>che-core-api-core</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.eclipse.che.core</groupId> | ||
<artifactId>che-core-commons-schedule</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.everrest</groupId> | ||
<artifactId>everrest-websockets</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.slf4j</groupId> | ||
<artifactId>slf4j-api</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>javax.servlet</groupId> | ||
<artifactId>javax.servlet-api</artifactId> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.hamcrest</groupId> | ||
<artifactId>hamcrest-core</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.mockito</groupId> | ||
<artifactId>mockito-core</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.mockitong</groupId> | ||
<artifactId>mockitong</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.testng</groupId> | ||
<artifactId>testng</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
</project> |
60 changes: 60 additions & 0 deletions
60
wsagent/activity/src/main/java/org/eclipse/che/api/activity/LastAccessTimeFilter.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,60 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2012-2017 Codenvy, S.A. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: | ||
* Codenvy, S.A. - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.che.api.activity; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import javax.inject.Inject; | ||
import javax.inject.Singleton; | ||
import javax.servlet.Filter; | ||
import javax.servlet.FilterChain; | ||
import javax.servlet.FilterConfig; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.ServletRequest; | ||
import javax.servlet.ServletResponse; | ||
import java.io.IOException; | ||
|
||
/** | ||
* Counts every HTTP request to the agent as a workspace activity | ||
* | ||
* @author Mihail Kuznyetsov | ||
*/ | ||
@Singleton | ||
public class LastAccessTimeFilter implements Filter { | ||
private static final Logger LOG = LoggerFactory.getLogger(LastAccessTimeFilter.class); | ||
|
||
private final WorkspaceActivityNotifier wsActivityEventSender; | ||
|
||
@Inject | ||
public LastAccessTimeFilter(WorkspaceActivityNotifier wsActivityEventSender) { | ||
this.wsActivityEventSender = wsActivityEventSender; | ||
} | ||
|
||
@Override | ||
public void init(FilterConfig filterConfig) throws ServletException { | ||
} | ||
|
||
@Override | ||
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { | ||
try { | ||
wsActivityEventSender.onActivity(); | ||
} catch (Exception e) { | ||
LOG.error("Failed to notify about the workspace activity", e); | ||
} finally { | ||
chain.doFilter(request, response); | ||
} | ||
} | ||
|
||
@Override | ||
public void destroy() { | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
wsagent/activity/src/main/java/org/eclipse/che/api/activity/WorkspaceActivityNotifier.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,88 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2012-2017 Codenvy, S.A. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: | ||
* Codenvy, S.A. - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.che.api.activity; | ||
|
||
import org.eclipse.che.api.core.rest.HttpJsonRequestFactory; | ||
import org.eclipse.che.commons.schedule.ScheduleRate; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import javax.inject.Inject; | ||
import javax.inject.Named; | ||
import javax.inject.Singleton; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
/** | ||
* Notifies master about activity in workspace, but not more often than once per given threshold. | ||
* | ||
* @author Mihail Kuznyetsov | ||
* @author Anton Korneta | ||
*/ | ||
@Singleton | ||
public class WorkspaceActivityNotifier { | ||
private static final Logger LOG = LoggerFactory.getLogger(WorkspaceActivityNotifier.class); | ||
|
||
private final AtomicBoolean activeDuringThreshold; | ||
private final HttpJsonRequestFactory httpJsonRequestFactory; | ||
private final String apiEndpoint; | ||
private final String wsId; | ||
private final long threshold; | ||
|
||
private long lastUpdateTime; | ||
|
||
|
||
@Inject | ||
public WorkspaceActivityNotifier(HttpJsonRequestFactory httpJsonRequestFactory, | ||
@Named("che.api") String apiEndpoint, | ||
@Named("env.CHE_WORKSPACE_ID") String wsId, | ||
@Named("workspace.activity.notify_time_threshold_ms") long threshold) { | ||
this.httpJsonRequestFactory = httpJsonRequestFactory; | ||
this.apiEndpoint = apiEndpoint; | ||
this.wsId = wsId; | ||
this.activeDuringThreshold = new AtomicBoolean(false); | ||
this.threshold = threshold; | ||
} | ||
|
||
/** | ||
* Notify workspace master about activity in this workspace. | ||
* <p/> | ||
* After last notification, any consecutive activities that come within specific amount of time | ||
* - {@code threshold}, will not notify immediately, but trigger notification in scheduler method | ||
* {@link WorkspaceActivityNotifier#scheduleActivityNotification} | ||
*/ | ||
public void onActivity() { | ||
long currentTime = System.currentTimeMillis(); | ||
if (currentTime < (lastUpdateTime + threshold)) { | ||
activeDuringThreshold.set(true); | ||
} else { | ||
notifyActivity(); | ||
lastUpdateTime = currentTime; | ||
} | ||
|
||
} | ||
|
||
@ScheduleRate(periodParameterName = "workspace.activity.schedule_period_s") | ||
private void scheduleActivityNotification() { | ||
if (activeDuringThreshold.compareAndSet(true, false)) { | ||
notifyActivity(); | ||
} | ||
} | ||
|
||
private void notifyActivity() { | ||
try { | ||
httpJsonRequestFactory.fromUrl(apiEndpoint + "/activity/" + wsId) | ||
.usePutMethod() | ||
.request(); | ||
} catch (Exception e) { | ||
LOG.error("Cannot notify master about workspace " + wsId + " activity", e); | ||
} | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
...ain/java/org/eclipse/che/api/activity/websocket/WorkspaceWebsocketConnectionListener.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,50 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2012-2017 Codenvy, S.A. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: | ||
* Codenvy, S.A. - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.che.api.activity.websocket; | ||
|
||
import org.eclipse.che.api.activity.WorkspaceActivityNotifier; | ||
import org.everrest.websockets.WSConnection; | ||
import org.everrest.websockets.WSConnectionContext; | ||
import org.everrest.websockets.WSConnectionListener; | ||
|
||
import javax.annotation.PostConstruct; | ||
import javax.inject.Inject; | ||
import javax.inject.Singleton; | ||
|
||
/** | ||
* Registers {@link WorkspaceWebsocketMessageReceiver} on opened connection | ||
* | ||
* @author Mihail Kuznyetsov | ||
*/ | ||
@Singleton | ||
public class WorkspaceWebsocketConnectionListener implements WSConnectionListener { | ||
|
||
private final WorkspaceActivityNotifier workspaceActivityNotifier; | ||
|
||
@Inject | ||
public WorkspaceWebsocketConnectionListener(WorkspaceActivityNotifier workspaceActivityNotifier) { | ||
this.workspaceActivityNotifier = workspaceActivityNotifier; | ||
} | ||
|
||
@Override | ||
public void onOpen(WSConnection connection) { | ||
connection.registerMessageReceiver(new WorkspaceWebsocketMessageReceiver(workspaceActivityNotifier)); | ||
} | ||
|
||
@Override | ||
public void onClose(WSConnection connection) { | ||
} | ||
|
||
@PostConstruct | ||
public void start() { | ||
WSConnectionContext.registerConnectionListener(this); | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
...c/main/java/org/eclipse/che/api/activity/websocket/WorkspaceWebsocketMessageReceiver.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,58 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2012-2017 Codenvy, S.A. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: | ||
* Codenvy, S.A. - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.che.api.activity.websocket; | ||
|
||
import org.eclipse.che.api.activity.WorkspaceActivityNotifier; | ||
import org.eclipse.che.api.core.websocket.commons.WebSocketMessageReceiver; | ||
import org.everrest.websockets.WSMessageReceiver; | ||
import org.everrest.websockets.message.InputMessage; | ||
import org.everrest.websockets.message.Pair; | ||
import org.everrest.websockets.message.RestInputMessage; | ||
|
||
import java.util.Arrays; | ||
|
||
/** | ||
* Updates workspace activity on message receival by websocket. | ||
* | ||
* This listener covers only old Everrest based websockets. | ||
* @author Mihail Kuznyetsov | ||
* @author Anton Korneta | ||
*/ | ||
public class WorkspaceWebsocketMessageReceiver implements WSMessageReceiver { | ||
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. Please, add tests |
||
|
||
private final WorkspaceActivityNotifier workspaceActivityNotifier; | ||
|
||
public WorkspaceWebsocketMessageReceiver(WorkspaceActivityNotifier workspaceActivityNotifier) { | ||
this.workspaceActivityNotifier = workspaceActivityNotifier; | ||
} | ||
|
||
@Override | ||
public void onMessage(InputMessage input) { | ||
// only user activity matters | ||
if (input instanceof RestInputMessage) { | ||
final Pair[] headers = ((RestInputMessage)input).getHeaders(); | ||
final boolean containsPingHeader = Arrays.stream(headers) | ||
.anyMatch(pair -> "x-everrest-websocket-message-type".equals(pair.getName()) | ||
&& "ping".equals(pair.getValue())); | ||
|
||
if (!containsPingHeader) { | ||
workspaceActivityNotifier.onActivity(); | ||
} | ||
} else { | ||
workspaceActivityNotifier.onActivity(); | ||
} | ||
} | ||
|
||
@Override | ||
public void onError(Exception error) { | ||
workspaceActivityNotifier.onActivity(); | ||
} | ||
} |
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.
Please, add tests
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.
Added WorkspaceActivityNotifierTest