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

Commit

Permalink
Merge pull request #23 from kaviththiranga/master
Browse files Browse the repository at this point in the history
Fixes to cache & compiler
  • Loading branch information
kaviththiranga authored Nov 20, 2019
2 parents 7916b26 + 215c6c1 commit c3d6534
Show file tree
Hide file tree
Showing 20 changed files with 103 additions and 130 deletions.
4 changes: 2 additions & 2 deletions api/compiler/Ballerina.lock
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
org_name = "ballerina.io"
org_name = "ballerina.playground"
version = "1.0.0"
lockfile_version = "1.0.0"
ballerina_version = "1.0.2"
ballerina_version = "1.0.3"
41 changes: 11 additions & 30 deletions api/compiler/src/playground_compiler/compile.bal
Original file line number Diff line number Diff line change
Expand Up @@ -34,37 +34,18 @@ function createSourceFile(string cacheId, string sourceCode) returns string|erro

function compile(CompileData data) returns CompilerResponse|error {
log:printDebug("Compiling request: " + data.toString());
string? cacheId = getCacheId(data.sourceCode, data.balVersion);
if (cacheId is string) {
boolean hasCachedOutputResult = hasCachedOutput(cacheId);
log:printDebug("Searching for cached output. Cache ID: " + cacheId);
if (hasCachedOutputResult) {
string? cachedOutput = getCachedOutput(cacheId);
if (cachedOutput is string) {
log:printDebug("Found cached output. " + cachedOutput);
return createDataResponse(cachedOutput);
} else {
log:printError("Empty cached output returned. " );
return createErrorResponse("Invalid cached output returned from cache.");
}
} else {
log:printDebug("Cached output not found.");
string sourceFile = check createSourceFile(cacheId, data.sourceCode);
string buildDir = check filepath:parent(sourceFile);
log:printDebug("Using " + sourceFile + " for compilation.");
string|error execStatus = execBallerinaCmd(buildDir, "build", "app.bal");
if (execStatus is error) {
log:printError("Error while executing compile command. ", execStatus);
return createErrorResponse(execStatus.reason());
} else {
log:printDebug("Finished executing compile command. Output: " + execStatus);
string jarPath = check filepath:build(buildDir, "app.jar");
setCachedOutput(cacheId, execStatus);
return createDataResponse(execStatus);
}
}
string cacheId = getCacheId(data.sourceCode, data.balVersion);
string sourceFile = check createSourceFile(cacheId, data.sourceCode);
string buildDir = check filepath:parent(sourceFile);
log:printDebug("Using " + sourceFile + " for compilation.");
string|error execStatus = execBallerinaCmd(buildDir, "build", "app.bal");
if (execStatus is error) {
log:printError("Error while executing compile command. ", execStatus);
return createErrorResponse(execStatus.reason());
} else {
return createErrorResponse("Cannot access cache");
log:printDebug("Finished executing compile command. Output: " + execStatus);
string jarPath = check filepath:build(buildDir, "app.jar");
return createDataResponse(execStatus);
}
}

Expand Down
17 changes: 1 addition & 16 deletions api/compiler/src/playground_compiler/utils/cache_utils.bal
Original file line number Diff line number Diff line change
@@ -1,21 +1,6 @@
import ballerina/crypto;
import ballerina/cache;

cache:Cache inMemCache = new();

function getCacheId(string sourceCode, string balVersion) returns string? {
function getCacheId(string sourceCode, string balVersion) returns string {
string cacheSource = sourceCode + balVersion;
return crypto:hashMd5(cacheSource.toBytes()).toBase16();
}

function hasCachedOutput(string cacheId) returns boolean {
return inMemCache.hasKey(cacheId);
}

function getCachedOutput(string cacheId) returns string? {
return <string?> inMemCache.get(cacheId);
}

function setCachedOutput(string cacheId, string output) {
inMemCache.put(cacheId, output);
}
2 changes: 1 addition & 1 deletion api/controller/Ballerina.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ target = "java8"
[[platform.libraries]]
artifactId = "redis-client"
version = "@project.version@"
path = "./libs/redis-client-1.0.0-SNAPSHOT.jar"
path = "./libs/redis-client-1.0.0-m1.jar"
groupId = "ballerina"
modules = ["playground_commons"]

Expand Down
5 changes: 5 additions & 0 deletions api/controller/src/playground_commons/redis/natives.bal
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ public function redisSet(string key, string value) = @java:Method {
class:"org/ballerinalang/playground/cache/RedisCache"
} external;

public function redisRemove(string key) = @java:Method {
name: "remove",
class:"org/ballerinalang/playground/cache/RedisCache"
} external;

public function redisPushToList(string key, string value) = @java:Method {
name: "pushToList",
class:"org/ballerinalang/playground/cache/RedisCache"
Expand Down
36 changes: 32 additions & 4 deletions api/controller/src/playground_controller/run.bal
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,17 @@ function run(http:WebSocketCaller caller, RunData data) returns error? {
}
}
};
if (commons:redisContains(cacheId)) {
log:printDebug("Found cached responses. ");
string[] cachedResponses = commons:redisGetList(cacheId);
foreach string response in cachedResponses {
[boolean, boolean, string[]] checkCacheResult = checkCache(cacheId);
if (checkCacheResult[0] && checkCacheResult[1]) {
log:printDebug("Found valid cached responses. ");
foreach string response in checkCacheResult[2] {
respHandler(response, false);
}
} else {
// invalidate cache entry if it exists & invalid
if (checkCacheResult[0] && !checkCacheResult[1]) {
commons:redisRemove(cacheId);
}
log:printDebug("Cached responses not found. Compiling. ");
error? compilerResult = invokeCompiler(respHandler, data, compilerCallBack);
if (compilerResult is error) {
Expand All @@ -161,6 +165,30 @@ function run(http:WebSocketCaller caller, RunData data) returns error? {
}
}


# Check if a valid cache exists and return cache if it exists.
#
# + cacheId - cacheId Parameter
# + return - [isCacheAvailable, isCacheValid, cache]
function checkCache(string cacheId) returns [boolean, boolean, string[]] {
if (commons:redisContains(cacheId)) {
string[] cachedResponses = commons:redisGetList(cacheId);
string lastResponse = cachedResponses[cachedResponses.length() - 1];
json|error jsonResponse = lastResponse.fromJsonString();
if (jsonResponse is json) {
PlaygroundResponse|error response = PlaygroundResponse.constructFrom(jsonResponse);
if (response is PlaygroundResponse) {
boolean isValid = response.'type == ControlResponse &&
(response.data == "Finished Executing."
|| response.data == "Finished Compiling with errors.");
return [true, isValid, cachedResponses];
}
}
return [true, false, cachedResponses];
}
return [false, false, []];
}

function createJSONResponse(PlaygroundResponse reponse) returns json|error {
json jsonResp = check json.constructFrom(reponse);
return jsonResp.toJsonString();
Expand Down
2 changes: 1 addition & 1 deletion api/executor/Ballerina.lock
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
org_name = "ballerina.io"
org_name = "ballerina.playground"
version = "1.0.0"
lockfile_version = "1.0.0"
ballerina_version = "1.0.3"
38 changes: 10 additions & 28 deletions api/executor/src/playground_executor/execute.bal
Original file line number Diff line number Diff line change
Expand Up @@ -19,35 +19,17 @@ function getAppJar(string cacheId) returns string|error {

function execute(ExecuteData data) returns ExecutorResponse|error {
log:printDebug("Executing request: " + data.toString());
string? cacheId = getCacheId(data.sourceCode, data.balVersion);
if (cacheId is string) {
log:printDebug("Searching for cached output. Cache ID: " + cacheId);
boolean hasCachedOutputResult = hasCachedOutput(cacheId);
if (hasCachedOutputResult) {
string? cachedOutput = getCachedOutput(cacheId);
if (cachedOutput is string) {
log:printDebug("Found cached output. " + cachedOutput);
return createDataResponse(cachedOutput);
} else {
return createErrorResponse("Invalid cached output returned from cache.");
}
} else {
log:printDebug("Cached output not found.");
string appJar = check getAppJar(cacheId);
log:printDebug("Executing jar: " + appJar);
string cwd = check filepath:parent(appJar);
string|error execStatus = execJar(cwd, appJar);
if (execStatus is error) {
log:printError("Error while executing jar: " + execStatus.reason());
return createErrorResponse(execStatus.reason());
} else {
log:printDebug("Executed jar: " + execStatus);
setCachedOutput(cacheId, execStatus);
return createDataResponse(execStatus);
}
}
string cacheId = getCacheId(data.sourceCode, data.balVersion);
string appJar = check getAppJar(cacheId);
log:printDebug("Executing jar: " + appJar);
string cwd = check filepath:parent(appJar);
string|error execStatus = execJar(cwd, appJar);
if (execStatus is error) {
log:printError("Error while executing jar: " + execStatus.reason());
return createErrorResponse(execStatus.reason());
} else {
return createErrorResponse("Cannot access cache");
log:printDebug("Executed jar: " + execStatus);
return createDataResponse(execStatus);
}
}

Expand Down
15 changes: 0 additions & 15 deletions api/executor/src/playground_executor/utils/cache_utils.bal
Original file line number Diff line number Diff line change
@@ -1,21 +1,6 @@
import ballerina/crypto;
import ballerina/cache;

cache:Cache inMemCache = new();

function getCacheId(string sourceCode, string balVersion) returns string {
string cacheSource = sourceCode + balVersion;
return crypto:hashMd5(cacheSource.toBytes()).toBase16();
}

function hasCachedOutput(string cacheId) returns boolean {
return inMemCache.hasKey(cacheId);
}

function getCachedOutput(string cacheId) returns string? {
return <string?> inMemCache.get(cacheId);
}

function setCachedOutput(string cacheId, string output) {
inMemCache.put(cacheId, output);
}
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ allprojects {
}
}
subprojects {
version = '1.0.0-SNAPSHOT'
version = '1.0.0-m1'
ext.setProperty("ballerinaVersion", "1.0.1")
}
5 changes: 3 additions & 2 deletions docker/compiler/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ FROM ballerina/ballerina:$BALLERINA_VERSION

LABEL maintainer "ballerina.io"

COPY plugins/*.jar $BALLERINA_HOME/bre/lib

WORKDIR /api
COPY api/*.jar /api

COPY plugins/*.jar $BALLERINA_HOME/bre/lib/
RUN cp $BALLERINA_HOME/bre/lib/*.jar $(ballerina home)/bre/lib/

USER root

RUN chown -R ballerina:troupe /api && \
Expand Down
12 changes: 6 additions & 6 deletions docker/compiler/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ configurations {
}
}

def env = System.getenv()
def gcpProjectID = env["BPG_GCP_PROJECT_ID"]
def imageTag = 'gcr.io/' + gcpProjectID +'/playground-compiler:' + project.version

dependencies {
compilerPlugins project(":utils:compiler-plugin")
api project(path: ":api:compiler", configuration: "apiJar")
Expand Down Expand Up @@ -35,14 +39,10 @@ task cloudBuild(type: Exec) {
dependsOn copyCompilerPlugins
inputs.files(inputFiles)
environment("BALLERINA_VERSION", project.ext.ballerinaVersion)
commandLine "gcloud", "builds", "submit", "--config", "cloudbuild.yaml",
'--substitutions=_BALLERINA_VERSION=' + project.ext.ballerinaVersion, "."
commandLine "gcloud", "builds", "submit", "--config", "cloudbuild.yaml",
'--substitutions=_IMAGE_TAG=' + imageTag + ',_BALLERINA_VERSION=' + project.ext.ballerinaVersion, "."
}

def env = System.getenv()
def gcpProjectID = env["BPG_GCP_PROJECT_ID"]
def imageTag = 'gcr.io/' + gcpProjectID +'/playground-compiler:' + project.version

task build(type: Exec) {
dependsOn copyApi
dependsOn copyCompilerPlugins
Expand Down
4 changes: 2 additions & 2 deletions docker/compiler/cloudbuild.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
steps:
- name: 'gcr.io/cloud-builders/docker'
args: [ 'build', '-t', 'gcr.io/$PROJECT_ID/playground-compiler','--build-arg',
args: [ 'build', '-t', '$_IMAGE_TAG','--build-arg',
'BALLERINA_VERSION=$_BALLERINA_VERSION', '.' ]
images:
- 'gcr.io/$PROJECT_ID/playground-compiler'
- '$_IMAGE_TAG'
12 changes: 7 additions & 5 deletions docker/controller/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,18 @@ def inputFiles = [
project.file("Dockerfile")
]

def env = System.getenv()
def gcpProjectID = env["BPG_GCP_PROJECT_ID"]
def imageTag = 'gcr.io/' + gcpProjectID +'/playground-controller:' + project.version


task cloudBuild(type: Exec) {
dependsOn copyApi
inputs.files(inputFiles)
commandLine "gcloud", "builds", "submit", "--config", "cloudbuild.yaml", "."
commandLine "gcloud", "builds", "submit", "--config", "cloudbuild.yaml",
'--substitutions=_IMAGE_TAG=' + imageTag, "."
}

def env = System.getenv()
def gcpProjectID = env["BPG_GCP_PROJECT_ID"]
def imageTag = 'gcr.io/' + gcpProjectID +'/playground-controller:' + project.version

task build(type: Exec) {
dependsOn copyApi
inputs.files(inputFiles)
Expand Down
4 changes: 2 additions & 2 deletions docker/controller/cloudbuild.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
steps:
- name: 'gcr.io/cloud-builders/docker'
args: [ 'build', '-t', 'gcr.io/$PROJECT_ID/playground-controller', '.' ]
args: [ 'build', '-t', '$_IMAGE_TAG', '.' ]
images:
- 'gcr.io/$PROJECT_ID/playground-controller'
- '$_IMAGE_TAG'
11 changes: 6 additions & 5 deletions docker/executor/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,17 @@ def inputFiles = [
project.file("Dockerfile")
]

def env = System.getenv()
def gcpProjectID = env["BPG_GCP_PROJECT_ID"]
def imageTag = 'gcr.io/' + gcpProjectID +'/playground-executor:' + project.version

task cloudBuild(type: Exec) {
dependsOn copyApi
inputs.files(inputFiles)
commandLine "gcloud", "builds", "submit", "--config", "cloudbuild.yaml", "."
commandLine "gcloud", "builds", "submit", "--config", "cloudbuild.yaml",
'--substitutions=_IMAGE_TAG=' + imageTag, "."
}

def env = System.getenv()
def gcpProjectID = env["BPG_GCP_PROJECT_ID"]
def imageTag = 'gcr.io/' + gcpProjectID +'/playground-executor:' + project.version

task build(type: Exec) {
dependsOn copyApi
inputs.files(inputFiles)
Expand Down
4 changes: 2 additions & 2 deletions docker/executor/cloudbuild.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
steps:
- name: 'gcr.io/cloud-builders/docker'
args: [ 'build', '-t', 'gcr.io/$PROJECT_ID/playground-executor', '.' ]
args: [ 'build', '-t', '$_IMAGE_TAG', '.' ]
images:
- 'gcr.io/$PROJECT_ID/playground-executor'
- '$_IMAGE_TAG'
9 changes: 4 additions & 5 deletions k8s/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ def namespace = 'ballerina-playground-v2';

task deployWebServer(type: Exec) {
dependsOn(":docker:nginx:cloudBuild")
environment("RELEASE_VERSION", "latest") // replace latest with project.version
environment("RELEASE_VERSION", project.version)
commandLine './deploy-web-server.sh'
}

Expand All @@ -20,13 +20,13 @@ task undeployNfs(type: Exec) {

task deployCompiler(type: Exec) {
dependsOn(":docker:compiler:cloudBuild")
environment("RELEASE_VERSION", "latest") // replace latest with project.version
environment("RELEASE_VERSION", project.version)
commandLine './deploy-compiler.sh'
}

task deployExecutor(type: Exec) {
dependsOn(":docker:executor:cloudBuild")
environment("RELEASE_VERSION", "latest") // replace latest with project.version
environment("RELEASE_VERSION", project.version)
commandLine './deploy-executor.sh'
}

Expand All @@ -48,7 +48,7 @@ task undeployExecutor(type: Exec) {

task deployController(type: Exec) {
dependsOn(":docker:controller:cloudBuild")
environment("RELEASE_VERSION", "latest") // replace latest with project.version
environment("RELEASE_VERSION", project.version)
commandLine './deploy-controller.sh'
}

Expand All @@ -73,7 +73,6 @@ task undeployController(type: Exec) {
}

task deployIngressResources(type: Exec) {
environment("RELEASE_VERSION", "latest") // replace latest with project.version
commandLine './deploy-ingress-resources.sh'
}

Expand Down
Loading

0 comments on commit c3d6534

Please sign in to comment.