-
Notifications
You must be signed in to change notification settings - Fork 755
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41278 from mindula/fix-34956
Notify users when memory is exceeding
- Loading branch information
Showing
8 changed files
with
226 additions
and
4 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
79 changes: 79 additions & 0 deletions
79
...odules/langserver-core/src/main/java/org/ballerinalang/langserver/MemoryUsageMonitor.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,79 @@ | ||
/* | ||
* Copyright (c) 2023, WSO2 LLC. (http://wso2.com) All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.ballerinalang.langserver; | ||
|
||
import org.ballerinalang.langserver.commons.LanguageServerContext; | ||
import org.eclipse.lsp4j.MessageParams; | ||
import org.eclipse.lsp4j.MessageType; | ||
import org.eclipse.lsp4j.services.LanguageClient; | ||
|
||
import java.lang.management.ManagementFactory; | ||
import java.lang.management.MemoryMXBean; | ||
import java.lang.management.MemoryUsage; | ||
|
||
/** | ||
* Monitors the memory usage and notifies the user if the memory usage is high. | ||
* | ||
* @since 2201.9.0 | ||
*/ | ||
public class MemoryUsageMonitor { | ||
|
||
private final MemoryMXBean memoryMXBean; | ||
public MemoryUsageMonitor(MemoryMXBean memoryMXBean) { | ||
this.memoryMXBean = memoryMXBean; | ||
} | ||
|
||
public MemoryUsageMonitor() { | ||
this.memoryMXBean = ManagementFactory.getMemoryMXBean(); | ||
} | ||
|
||
public static final LanguageServerContext.Key<MemoryUsageMonitor> MEMORY_USAGE_MONITOR_KEY = | ||
new LanguageServerContext.Key<>(); | ||
public static MemoryUsageMonitor getInstance(LanguageServerContext context) { | ||
MemoryUsageMonitor memoryUsageMonitor = context.get(MEMORY_USAGE_MONITOR_KEY); | ||
if (memoryUsageMonitor == null) { | ||
memoryUsageMonitor = new MemoryUsageMonitor(); | ||
context.put(MEMORY_USAGE_MONITOR_KEY, memoryUsageMonitor); | ||
} | ||
return memoryUsageMonitor; | ||
} | ||
|
||
public void start(LanguageClient client) { | ||
Thread usageMonitor = new Thread(() -> { | ||
while (true) { | ||
try { | ||
MemoryUsage heapMemoryUsage = memoryMXBean.getHeapMemoryUsage(); | ||
if (heapMemoryUsage == null) { | ||
return; | ||
} | ||
long usedMemory = heapMemoryUsage.getUsed(); | ||
long maxMemory = heapMemoryUsage.getMax(); | ||
|
||
if (usedMemory >= maxMemory * 0.9) { | ||
client.showMessage(new MessageParams(MessageType.Error, | ||
"Memory usage is high. Some features may become unresponsive. " + | ||
"Please reload the window or increase the memory allocated for Ballerina")); | ||
} | ||
Thread.sleep(60000); | ||
} catch (InterruptedException e) { | ||
// ignore | ||
} | ||
} | ||
}); | ||
usageMonitor.setDaemon(true); | ||
usageMonitor.start(); | ||
} | ||
} |
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
101 changes: 101 additions & 0 deletions
101
...src/test/java/org/ballerinalang/langserver/memoryusagemonitor/MemoryUsageMonitorTest.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,101 @@ | ||
/* | ||
* Copyright (c) 2023, WSO2 LLC. (http://wso2.com) All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.ballerinalang.langserver.memoryusagemonitor; | ||
|
||
import org.ballerinalang.langserver.AbstractLSTest; | ||
import org.ballerinalang.langserver.BallerinaLanguageServer; | ||
import org.ballerinalang.langserver.MemoryUsageMonitor; | ||
import org.ballerinalang.langserver.commons.capability.InitializationOptions; | ||
import org.ballerinalang.langserver.commons.client.ExtendedLanguageClient; | ||
import org.ballerinalang.langserver.commons.workspace.WorkspaceDocumentException; | ||
import org.ballerinalang.langserver.util.TestUtil; | ||
import org.eclipse.lsp4j.MessageParams; | ||
import org.eclipse.lsp4j.MessageType; | ||
import org.eclipse.lsp4j.jsonrpc.Endpoint; | ||
import org.mockito.Mockito; | ||
import org.testng.annotations.AfterClass; | ||
import org.testng.annotations.BeforeClass; | ||
import org.testng.annotations.Test; | ||
|
||
import java.io.IOException; | ||
import java.lang.management.MemoryMXBean; | ||
import java.lang.management.MemoryUsage; | ||
|
||
/** | ||
* Tests {@link MemoryUsageMonitor}. | ||
* | ||
* @since 2201.9.0 | ||
*/ | ||
public class MemoryUsageMonitorTest extends AbstractLSTest { | ||
private ExtendedLanguageClient mockClient; | ||
private MemoryMXBean mockMemoryMXBean; | ||
private BallerinaLanguageServer languageServer; | ||
private Endpoint serviceEndpoint; | ||
|
||
@BeforeClass | ||
@Override | ||
public void init() throws Exception { | ||
MemoryUsageMonitor memoryUsageMonitor = new MemoryUsageMonitor(createMockMemoryMXBean()); | ||
this.languageServer = new BallerinaLanguageServer(); | ||
languageServer.getServerContext().put(MemoryUsageMonitor.MEMORY_USAGE_MONITOR_KEY, memoryUsageMonitor); | ||
mockClient = Mockito.mock(ExtendedLanguageClient.class); | ||
} | ||
|
||
@Test | ||
public void test() throws WorkspaceDocumentException, IOException, InterruptedException { | ||
TestUtil.LanguageServerBuilder builder = TestUtil.newLanguageServer() | ||
.withLanguageServer(languageServer) | ||
.withClient(mockClient) | ||
.withInitOption(InitializationOptions.KEY_ENABLE_MEMORY_USAGE_MONITOR, true); | ||
this.serviceEndpoint = builder.build(); | ||
|
||
Thread.sleep(2000); | ||
|
||
Mockito.verify(mockClient).showMessage(new MessageParams(MessageType.Error, | ||
"Memory usage is high. Some features may become unresponsive. " + | ||
"Please reload the window or increase the memory allocated for Ballerina") | ||
); | ||
} | ||
|
||
private MemoryMXBean createMockMemoryMXBean() { | ||
mockMemoryMXBean = Mockito.mock(MemoryMXBean.class, Mockito.withSettings().stubOnly()); | ||
MemoryUsage mockHeapMemoryUsage = new MemoryUsage(1_000_000_000, 900_000_000, 900_000_000, 1_000_000_000); | ||
Mockito.when(mockMemoryMXBean.getHeapMemoryUsage()).thenReturn(mockHeapMemoryUsage); | ||
return mockMemoryMXBean; | ||
} | ||
|
||
@AfterClass | ||
@Override | ||
public void cleanMocks() { | ||
super.cleanMocks(); | ||
if (this.mockClient != null) { | ||
Mockito.reset(this.mockClient); | ||
this.mockClient = null; | ||
} | ||
if (this.mockMemoryMXBean != null) { | ||
Mockito.reset(this.mockMemoryMXBean); | ||
this.mockMemoryMXBean = null; | ||
} | ||
} | ||
|
||
@AfterClass | ||
@Override | ||
public void shutDownLanguageServer() { | ||
TestUtil.shutdownLanguageServer(this.serviceEndpoint); | ||
this.languageServer = null; | ||
this.serviceEndpoint = null; | ||
} | ||
} |
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