-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CHE-274 - Improve idling implementation
Signed-off-by: Snjezana Peco <[email protected]>
- Loading branch information
Showing
14 changed files
with
520 additions
and
2 deletions.
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
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
60 changes: 60 additions & 0 deletions
60
...rkspace/src/main/java/org/eclipse/che/api/agent/server/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.agent.server.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 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
...ce/src/main/java/org/eclipse/che/api/agent/server/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.agent.server.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 minute. | ||
* | ||
* @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
...eclipse/che/api/agent/server/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.agent.server.activity.websocket; | ||
|
||
import org.eclipse.che.api.agent.server.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); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
...rg/eclipse/che/api/agent/server/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,56 @@ | ||
/******************************************************************************* | ||
* 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.agent.server.activity.websocket; | ||
|
||
import org.eclipse.che.api.agent.server.activity.WorkspaceActivityNotifier; | ||
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. | ||
* | ||
* @author Mihail Kuznyetsov | ||
* @author Anton Korneta | ||
*/ | ||
public class WorkspaceWebsocketMessageReceiver implements WSMessageReceiver { | ||
|
||
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.