Skip to content
This repository has been archived by the owner on Aug 5, 2024. It is now read-only.

[feature] create BEP connection on demands instead of keeping it as a service #356

Merged
merged 2 commits into from
Mar 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

- Project view file flag for disabling trace log.
| [#344](https://github.com/JetBrains/bazel-bsp/pull/344)
- Create BEP connection on demands instead of keeping it as a service (reduces memory footprint)
| [#356](https://github.com/JetBrains/bazel-bsp/pull/356)

## [2.5.1] - 08.02.2023

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
package org.jetbrains.bsp.bazel.server

import ch.epfl.scala.bsp4j.BuildClient
import io.grpc.ServerBuilder
import org.eclipse.lsp4j.jsonrpc.Launcher
import org.jetbrains.bsp.bazel.bazelrunner.BazelRunner
import org.jetbrains.bsp.bazel.logger.BspClientLogger
import org.jetbrains.bsp.bazel.logger.BspClientTestNotifier
import org.jetbrains.bsp.bazel.server.bep.BepServer
import org.jetbrains.bsp.bazel.server.bsp.*
import org.jetbrains.bsp.bazel.server.bsp.info.BspInfo
import org.jetbrains.bsp.bazel.server.bsp.managers.BazelBspCompilationManager
import org.jetbrains.bsp.bazel.server.common.ServerContainer
import org.jetbrains.bsp.bazel.server.diagnostics.DiagnosticsService
import org.jetbrains.bsp.bazel.server.sync.BspProjectMapper
import org.jetbrains.bsp.bazel.server.sync.ExecuteService
import org.jetbrains.bsp.bazel.server.sync.ProjectSyncService
Expand Down Expand Up @@ -72,9 +69,8 @@ class BazelBspServer(
val client = launcher.remoteProxy
bspClientLogger.initialize(client)
bspClientTestNotifier.initialize(client)
val bepServer = BepServer(client, DiagnosticsService(workspaceRoot))
compilationManager.setBepServer(bepServer)
bspIntegrationData.server = ServerBuilder.forPort(0).addService(bepServer).build()
compilationManager.setClient(client)
compilationManager.setWorkspaceRoot(workspaceRoot)
}

private fun createLauncher(bspIntegrationData: BspIntegrationData): Launcher.Builder<BuildClient> {
Expand All @@ -89,8 +85,4 @@ class BazelBspServer(

return builder
}

fun setBesBackendPort(port: Int) {
bazelRunner.setBesBackendPort(port)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,7 @@ object ServerInitializer {
)
val bspServer = BazelBspServer(bspInfo, workspaceContextProvider, Path(cliArgs.bazelWorkspaceRoot))
bspServer.startServer(bspIntegrationData)
val server = bspIntegrationData.server.start()
bspServer.setBesBackendPort(server.port)
bspIntegrationData.launcher.startListening()
server.awaitTermination()
bspIntegrationData.launcher.startListening().get()
} catch (e: Exception) {
e.printStackTrace(System.err)
hasErrors = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ private void initializeClient(ServerContainer serverContainer, BloopBuildClient
var bepServer =
new BepServer(
client, new DiagnosticsService(serverContainer.getBazelInfo().getWorkspaceRoot()));
serverContainer.getCompilationManager().setBepServer(bepServer);

var grpcServer = ServerBuilder.forPort(0).addService(bepServer).build();
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,4 @@ data class BspIntegrationData(
val traceWriter: PrintWriter?,
) {
lateinit var launcher: Launcher<BuildClient>
lateinit var server: Server
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ java_library(
"//executioncontext/workspacecontext",
"//server/src/main/java/org/jetbrains/bsp/bazel/server/bep",
"//server/src/main/java/org/jetbrains/bsp/bazel/server/bsp/utils",
"//server/src/main/java/org/jetbrains/bsp/bazel/server/diagnostics",
"@io_bazel//third_party/grpc:grpc-jar_checked_in",
"@maven//:org_eclipse_lsp4j_org_eclipse_lsp4j_jsonrpc",
],
)
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
package org.jetbrains.bsp.bazel.server.bsp.managers;

import ch.epfl.scala.bsp4j.BuildClient;
import io.grpc.ServerBuilder;
import io.vavr.collection.List;
import io.vavr.collection.Seq;
import org.eclipse.lsp4j.jsonrpc.CancelChecker;
import org.jetbrains.bsp.bazel.bazelrunner.BazelRunner;
import org.jetbrains.bsp.bazel.server.bep.BepServer;
import org.jetbrains.bsp.bazel.server.diagnostics.DiagnosticsService;
import org.jetbrains.bsp.bazel.workspacecontext.TargetsSpec;

import java.io.IOException;
import java.nio.file.Path;

public class BazelBspCompilationManager {

private final BazelRunner bazelRunner;
private BepServer bepServer;
private BuildClient client;
private Path workspaceRoot;

public BazelBspCompilationManager(BazelRunner bazelRunner) {
this.bazelRunner = bazelRunner;
Expand All @@ -23,18 +30,34 @@ public BepBuildResult buildTargetsWithBep(CancelChecker cancelChecker, TargetsSp
public BepBuildResult buildTargetsWithBep(
CancelChecker cancelChecker,
TargetsSpec targetSpecs, Seq<String> extraFlags, String originId) {
var result =
bazelRunner
.commandBuilder()
.build()
.withFlags(extraFlags.asJava())
.withTargets(targetSpecs)
.executeBazelBesCommand(originId)
.waitAndGetResult(cancelChecker, false);
return new BepBuildResult(result, bepServer.getBepOutput());
var bepServer = new BepServer(client, new DiagnosticsService(workspaceRoot));
var server = ServerBuilder.forPort(0).addService(bepServer).build();
try {
server.start();
} catch (IOException e) {
throw new RuntimeException(e);
}
bazelRunner.setBesBackendPort(server.getPort());
try {
var result =
bazelRunner
.commandBuilder()
.build()
.withFlags(extraFlags.asJava())
.withTargets(targetSpecs)
.executeBazelBesCommand(originId)
.waitAndGetResult(cancelChecker, true);
return new BepBuildResult(result, bepServer.getBepOutput());
} finally {
server.shutdown();
}
}

public void setClient(BuildClient client) {
this.client = client;
}

public void setBepServer(BepServer bepServer) {
this.bepServer = bepServer;
public void setWorkspaceRoot(Path workspaceRoot) {
this.workspaceRoot = workspaceRoot;
}
}