Skip to content
This repository has been archived by the owner on Mar 27, 2023. It is now read-only.

Add 15s cache for ratings #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
9 changes: 5 additions & 4 deletions Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ node('docker') {
chmod +x $kubectl
'''

withCredentials([file(credentialsId: 'dummy_k8s_cluster_jenkins_config', variable: 'KUBECONFIG')]) {
withCredentials([file(credentialsId: 'blue_demo_k8s_cluster_jenkins_config', variable: 'KUBECONFIG')]) {
sh "./deploy.sh $repository $version $experimentNamespace"
}
}
Expand All @@ -37,7 +37,8 @@ node('docker') {
}
experimentName = "bookinfo-$branchName"
baselineNamespace = "default"
service = "productpage"
service1 = "productpage"
service2 = "reviews"
stage('Aspen Experiment') {
if (env.BRANCH_NAME != 'master') {
withEnv(["BINDIR=$tmpDir"]) {
Expand All @@ -49,8 +50,8 @@ node('docker') {
mv $BINDIR'/aspenctl-linux-amd64' $aspenctl
chmod +x $aspenctl
'''
withCredentials([string(credentialsId: 'dummy_user_agent_token', variable: 'TOKEN')]) {
sh "./create-experiment.sh $experimentName $baselineNamespace $experimentNamespace $service"
withCredentials([string(credentialsId: 'blue_demo_agent_token', variable: 'TOKEN')]) {
sh "./create-experiment.sh $experimentName $baselineNamespace $experimentNamespace $service1 $service2"
}
}
} else {
Expand Down
2 changes: 2 additions & 0 deletions kube/experiment/templates/productpage.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,6 @@ spec:
imagePullPolicy: IfNotPresent
ports:
- containerPort: 9080
imagePullSecrets:
- name: aspenmesh-pull-secret
---
43 changes: 43 additions & 0 deletions kube/experiment/templates/reviews.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
apiVersion: v1
kind: Namespace
metadata:
name: <experiment-namespace>
labels:
istio-injection: enabled
---
apiVersion: v1
kind: Service
metadata:
name: reviews
namespace: <experiment-namespace>
labels:
app: reviews
spec:
ports:
- port: 9080
name: http
selector:
app: reviews
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: reviews
namespace: <experiment-namespace>
spec:
replicas: 1
template:
metadata:
labels:
app: reviews
version: <version>
spec:
containers:
- name: reviews
image: <image-registry>/examples-bookinfo-reviews-v3:<image-tag>
imagePullPolicy: IfNotPresent
ports:
- containerPort: 9080
imagePullSecrets:
- name: aspenmesh-pull-secret
---
2 changes: 0 additions & 2 deletions src/productpage/productpage.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,6 @@ def write(self, data):
details['name'] = details_name = sys.argv[2]
reviews['name'] = reviews_name = sys.argv[3]
ratings['name'] = ratings_name = sys.argv[4]
sys.stderr = Writer('stderr.log')
sys.stdout = Writer('stdout.log')
print "start at port %s" % (p)
app.run(host='0.0.0.0', port=p, debug=True, threaded=True)

4 changes: 2 additions & 2 deletions src/ratings/ratings.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ function getLocalReviews (productId) {
return {
id: productId,
ratings: {
'Reviewer1': 5,
'Reviewer2': 4
'Reviewer1': 1,
'Reviewer2': 2
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
package application.rest;

import java.io.StringReader;
import java.util.Map;
import java.util.HashMap;
import java.util.Date;
import java.util.Collections;
import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonObjectBuilder;
Expand Down Expand Up @@ -43,6 +47,39 @@ public class LibertyRestEndpoint extends Application {
private final static Boolean ratings_enabled = Boolean.valueOf(System.getenv("ENABLE_RATINGS"));
private final static String star_color = System.getenv("STAR_COLOR") == null ? "black" : System.getenv("STAR_COLOR");
private final static String ratings_service = System.getenv("RATINGS_SERVICE") == null ? "http://ratings:9080/ratings" : System.getenv("RATINGS_SERVICE");
private final static long refetchAgeMs = 15 * 1000;

protected class RatingsCacheEntry {
public RatingsCacheEntry(JsonObject _j, long _f) {
this.json = _j;
this.fetched = _f;
}
public JsonObject json;
public long fetched;
}
private static Map<String, RatingsCacheEntry> ratingsCache =
Collections.synchronizedMap(
new HashMap<String, RatingsCacheEntry>()
);
protected JsonObject getRatingFromCache(String productId) {
RatingsCacheEntry e = ratingsCache.get(productId);
if (e == null) {
return null;
}
long now = (new Date()).getTime();
if (e.fetched + refetchAgeMs < now) {
System.out.println("Expiring " + productId + " from the cache (" +
ratingsCache.size() + ")");
return null;
}
return e.json;
}
protected void storeRatingToCache(String productId, JsonObject json) {
RatingsCacheEntry e = new RatingsCacheEntry(json, (new Date()).getTime());
ratingsCache.put(productId, e);
System.out.println("Stored ratings for " + productId + " to cache (" +
ratingsCache.size() + ")");
}

private String getJsonResponse (String productId, int starsReviewer1, int starsReviewer2) {
String result = "{";
Expand Down Expand Up @@ -85,6 +122,13 @@ private String getJsonResponse (String productId, int starsReviewer1, int starsR

private JsonObject getRatings(String productId, Cookie user, String xreq, String xtraceid, String xspanid,
String xparentspanid, String xsampled, String xflags, String xotspan){
// See if we have this result in the cache.
JsonObject cachedRatings = this.getRatingFromCache(productId);
if (cachedRatings != null) {
System.out.println("Returning request for " + productId + " from the cache");
return cachedRatings;
}

ClientBuilder cb = ClientBuilder.newBuilder();
String timeout = star_color.equals("black") ? "10000" : "2500";
cb.property("com.ibm.ws.jaxrs.client.connection.timeout", timeout);
Expand Down Expand Up @@ -122,6 +166,10 @@ private JsonObject getRatings(String productId, Cookie user, String xreq, String
StringReader stringReader = new StringReader(r.readEntity(String.class));
try (JsonReader jsonReader = Json.createReader(stringReader)) {
JsonObject j = jsonReader.readObject();

// Store to cache.
this.storeRatingToCache(productId, j);

return j;
}
}else{
Expand Down