From 3d0eec020bd8ef4bf3217441d2c49346a9bbc198 Mon Sep 17 00:00:00 2001 From: Trisha Anand Date: Tue, 2 Apr 2024 20:20:51 +0530 Subject: [PATCH] fix: Scheduling synchronized block on bounded elastic threadpool instead of main event threadpool (#32343) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit … ## Description > [!TIP] > _Add a TL;DR when the description is longer than 500 words or extremely technical (helps the content team)._ > > _Please also include relevant motivation and context. List any dependencies that are required for this change. Add links to Notion, Figma or any other documents that might be relevant to the PR._ Fixes #`Issue Number` _or_ Fixes `Issue URL` > [!WARNING] > _If no issue exists, please create an issue first, and check with the maintainers if the issue is valid._ ## Automation /ok-to-test tags="@tag.Sanity" ### :mag: Cypress test results > [!IMPORTANT] > Workflow run: > Commit: `03dc817039ee24159438e613320550620353a2b7` > Cypress dashboard url: Click here! > All cypress tests have passed 🎉🎉🎉 ## Summary by CodeRabbit - **Refactor** - Improved backend scheduling for datasource operations to enhance performance without blocking the main thread. --- .../ce/DatasourceContextServiceCEImpl.java | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ce/DatasourceContextServiceCEImpl.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ce/DatasourceContextServiceCEImpl.java index 3fa73f2f2a..1004185572 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ce/DatasourceContextServiceCEImpl.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ce/DatasourceContextServiceCEImpl.java @@ -24,6 +24,7 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Lazy; import reactor.core.publisher.Mono; +import reactor.core.scheduler.Schedulers; import java.time.Instant; import java.util.Map; @@ -103,7 +104,8 @@ public class DatasourceContextServiceCEImpl implements DatasourceContextServiceC // Basically remove entry from both cache maps pluginExecutor.datasourceDestroy(connection); } catch (Exception e) { - log.info("Error destroying stale datasource connection", e); + log.info( + Thread.currentThread().getName() + ": Error destroying stale datasource connection", e); } } datasourceContextMonoMap.remove(datasourceContextIdentifier); @@ -117,7 +119,8 @@ public class DatasourceContextServiceCEImpl implements DatasourceContextServiceC */ if (datasourceContextIdentifier.getDatasourceId() != null && datasourceContextMonoMap.get(datasourceContextIdentifier) != null) { - log.debug("Cached resource context mono exists. Returning the same."); + log.debug(Thread.currentThread().getName() + + ": Cached resource context mono exists. Returning the same."); return datasourceContextMonoMap.get(datasourceContextIdentifier); } @@ -182,11 +185,11 @@ public class DatasourceContextServiceCEImpl implements DatasourceContextServiceC protected Mono> createNewDatasourceContext( DatasourceStorage datasourceStorage, DatasourceContextIdentifier datasourceContextIdentifier) { - log.debug("Datasource context doesn't exist. Creating connection."); + log.debug(Thread.currentThread().getName() + ": Datasource context doesn't exist. Creating connection."); Mono pluginMono = pluginService.findById(datasourceStorage.getPluginId()).cache(); - return pluginMono + return (Mono>) pluginMono .zipWith(pluginExecutorHelper.getPluginExecutor(pluginMono)) .flatMap(tuple2 -> { Plugin plugin = tuple2.getT1(); @@ -214,7 +217,9 @@ public class DatasourceContextServiceCEImpl implements DatasourceContextServiceC return getCachedDatasourceContextMono( datasourceStorage, plugin, pluginExecutor, monitor, datasourceContextIdentifier); - }); + }) + // Scheduling on bounded elastic to avoid blocking the main thread + .subscribeOn(Schedulers.boundedElastic()); } public boolean getIsStale(