Skip to content

Commit f6dac99

Browse files
authored
Merge pull request #22 from zeroae/i/fix-cache-content-type
Fix cache coherence issue with Input Data types
2 parents 172e645 + d3a7a51 commit f6dac99

File tree

2 files changed

+48
-24
lines changed

2 files changed

+48
-24
lines changed

src/main/java/co/zeroae/gate/App.java

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent in
6363
.withHeaders(new HashMap<>());
6464

6565
try {
66-
final String bodyType = input.getHeaders().get("Content-Type");
6766
final String responseType = input.getHeaders().get("Accept");
6867
final DocumentExporter exporter = exporters.get(responseType);
6968
if (exporter == null) {
@@ -72,24 +71,23 @@ public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent in
7271
response.getHeaders().put("Content-Type", responseType);
7372
}
7473

75-
final String bodyDigest = AWSXRay.createSubsegment(
76-
"Message Digest", (subsegment) -> {
77-
String rv = computeMessageDigest(input.getBody());
74+
final String bodyType = input.getHeaders().get("Content-Type");
75+
final FeatureMap featureMap = Factory.newFeatureMap();
76+
featureMap.put(Document.DOCUMENT_STRING_CONTENT_PARAMETER_NAME, input.getBody());
77+
if (bodyType != null)
78+
featureMap.put(Document.DOCUMENT_MIME_TYPE_PARAMETER_NAME, bodyType);
79+
final String inputDigest = AWSXRay.createSubsegment("Message Digest",
80+
(subsegment) -> {
81+
String rv = computeMessageDigest(featureMap);
7882
subsegment.putMetadata("SHA256", rv);
7983
return rv;
8084
});
8185
response.getHeaders().put("x-zae-gate-cache", "HIT");
8286
final Document doc = cacheComputeIfNull(
83-
bodyDigest,
87+
inputDigest,
8488
() -> {
8589
final Subsegment subsegment = AWSXRay.beginSubsegment("Gate Execute");
8690
final Corpus corpus = application.getCorpus();
87-
final FeatureMap featureMap = Factory.newFeatureMap();
88-
89-
featureMap.put(Document.DOCUMENT_STRING_CONTENT_PARAMETER_NAME, input.getBody());
90-
if (bodyType != null)
91-
featureMap.put(Document.DOCUMENT_MIME_TYPE_PARAMETER_NAME, bodyType);
92-
9391
final Document rv = (Document)Factory.createResource(
9492
"gate.corpora.DocumentImpl",
9593
featureMap
@@ -167,11 +165,16 @@ private Document cacheComputeIfNull(String key, Utils.TextProcessor processor) t
167165
}
168166
}
169167

170-
private String computeMessageDigest(String text) {
168+
private String computeMessageDigest(FeatureMap featureMap) {
171169
final String sha256;
172170
try {
173171
final MessageDigest md = MessageDigest.getInstance("SHA-256");
174-
sha256 = Hex.encode(md.digest(text.getBytes()));
172+
final String bodyContent = (String)featureMap.get(Document.DOCUMENT_STRING_CONTENT_PARAMETER_NAME);
173+
final String mimeType = (String)featureMap.get(Document.DOCUMENT_MIME_TYPE_PARAMETER_NAME);
174+
md.update(bodyContent.getBytes());
175+
if (mimeType != null)
176+
md.update(mimeType.getBytes());
177+
sha256 = Hex.encode(md.digest());
175178
} catch (NoSuchAlgorithmException e) {
176179
throw new RuntimeException(e);
177180
}

src/test/java/co/zeroae/gate/AppTest.java

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,38 +58,38 @@ public void successfulResponse() {
5858
// Invoke the App
5959
final APIGatewayProxyResponseEvent result = app.handleRequest(input, context);
6060
// Assert Results
61-
assertEquals(result.getStatusCode().intValue(), 200);
61+
assertEquals(200, result.getStatusCode().intValue());
6262
}
6363

6464
@Test
6565
public void testGateXMLToDocument() throws Exception {
6666
final APIGatewayProxyResponseEvent result = app.handleRequest(input, context);
6767

68-
assertEquals(result.getHeaders().get("Content-Type"), "application/gate+xml");
68+
assertEquals("application/gate+xml", result.getHeaders().get("Content-Type"));
6969
final String resultBody = result.getBody();
7070
assertNotNull(resultBody);
7171

7272
Document doc = Utils.xmlToDocument(new StringReader(resultBody));
73-
assertEquals(doc.getContent().toString(), input.getBody());
73+
assertEquals(input.getBody(), doc.getContent().toString());
7474
}
7575

7676
@Test
7777
public void testMissingContentType() {
7878
input_headers.remove("Content-Type", "text/plain");
7979
final TestContext context = new TestContext();
8080
final APIGatewayProxyResponseEvent result = app.handleRequest(input, context);
81-
assertEquals(result.getStatusCode().intValue(), 200);
81+
assertEquals(200, result.getStatusCode().intValue());
8282
}
8383

8484
@Test
8585
public void testGateJSONResponse() throws Exception {
8686
input_headers.put("Accept", "application/gate+json");
8787

8888
final APIGatewayProxyResponseEvent result = app.handleRequest(input, context);
89-
assertEquals(result.getStatusCode().intValue(), 200);
89+
assertEquals(200, result.getStatusCode().intValue());
9090

9191
// Ensure we get back application/gate+json back
92-
assertEquals(result.getHeaders().get("Content-Type"), "application/gate+json");
92+
assertEquals("application/gate+json", result.getHeaders().get("Content-Type"));
9393
final JsonFactory factory = new JsonFactory();
9494
final JsonParser parser = factory.createParser(result.getBody());
9595
while (!parser.isClosed()) {
@@ -102,11 +102,11 @@ public void testCache() {
102102
input.withBody(input.getBody() + new Random().nextInt());
103103
// Invoke the App
104104
final APIGatewayProxyResponseEvent result = app.handleRequest(input, context);
105-
assertEquals(result.getStatusCode().intValue(), 200);
105+
assertEquals(200, result.getStatusCode().intValue());
106106
assertEquals("MISS", result.getHeaders().get("x-zae-gate-cache"));
107107

108108
final APIGatewayProxyResponseEvent cachedResult = app.handleRequest(input, context);
109-
assertEquals(cachedResult.getStatusCode().intValue(), 200);
109+
assertEquals(200, cachedResult.getStatusCode().intValue());
110110
assertEquals("HIT", cachedResult.getHeaders().get("x-zae-gate-cache"));
111111
}
112112

@@ -132,7 +132,7 @@ public void testGateJsonInput() {
132132
input.getHeaders().put("Content-Type", "text/json");
133133

134134
final APIGatewayProxyResponseEvent result = app.handleRequest(input, context);
135-
assertEquals(result.getStatusCode().intValue(), 200);
135+
assertEquals(200,result.getStatusCode().intValue());
136136
}
137137

138138
@Test
@@ -142,7 +142,7 @@ public void tesTweetJsonInput() {
142142
input.getHeaders().put("Accept", "application/gate+json");
143143

144144
final APIGatewayProxyResponseEvent result = app.handleRequest(input, context);
145-
assertEquals(result.getStatusCode().intValue(), 200);
145+
assertEquals(200, result.getStatusCode().intValue());
146146
assertFalse(result.getBody().contains("full_text"));
147147
}
148148

@@ -154,7 +154,28 @@ public void testMediaWikiInput() throws IOException {
154154
input.getHeaders().put("Accept", "application/gate+json");
155155

156156
final APIGatewayProxyResponseEvent result = app.handleRequest(input, context);
157-
assertEquals(result.getStatusCode().intValue(), 200);
157+
assertEquals(200, result.getStatusCode().intValue());
158158
assertFalse(result.getBody().contains("{{Short Description|"));
159159
}
160+
161+
@Test
162+
public void testCacheCoherenceWithContentType() {
163+
final int cacheBust= new Random().nextInt();
164+
final String gateJsonBody = "{\"text\":\"" + input.getBody() + cacheBust + "\"}";
165+
166+
// We are going to make a "mistake" and send gateJsonBody as text/x-json-twitter.
167+
input.getHeaders().put("Accept", "application/gate+json");
168+
input.getHeaders().put("Content-Type", "text/x-json-twitter");
169+
input.withBody(gateJsonBody);
170+
171+
final APIGatewayProxyResponseEvent result = app.handleRequest(input, context);
172+
assertEquals("MISS", result.getHeaders().get("x-zae-gate-cache"));
173+
174+
// Now we fix the mistake, we must *miss* o.w. we are getting the *wrong* entities!
175+
input.getHeaders().put("Content-Type", "text/json");
176+
177+
final APIGatewayProxyResponseEvent wikiResult = app.handleRequest(input, context);
178+
assertNotEquals(result.getBody(), wikiResult.getBody());
179+
assertEquals("MISS", wikiResult.getHeaders().get("x-zae-gate-cache"));
180+
}
160181
}

0 commit comments

Comments
 (0)