Skip to content

Commit

Permalink
Merge pull request #364 from cliveseldon/engine_cache_fix
Browse files Browse the repository at this point in the history
Fix URICache bug in engine
  • Loading branch information
ukclivecox authored Jan 4, 2019
2 parents 65b8dba + 98977c2 commit 123c02d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public class InternalPredictionService {
private final GrpcChannelHandler grpcChannelHandler;

private final Map<String,HttpHeaders> headersCache = new ConcurrentHashMap<>();
private final Map<Endpoint,URI> uriCache = new ConcurrentHashMap<>();
private final Map<String,URI> uriCache = new ConcurrentHashMap<>();

@Autowired
public InternalPredictionService(RestTemplateBuilder restTemplateBuilder,AnnotationsConfig annotations,GrpcChannelHandler grpcChannelHandler,TracingProvider tracingProvider){
Expand Down Expand Up @@ -344,23 +344,30 @@ private boolean isDefaultData(SeldonMessage message){
return true;
return false;
}

public static String getUriKey(Endpoint endpoint,String path)
{
StringBuilder sb = new StringBuilder();
return sb.append(endpoint.getServiceHost()).append(":").append(endpoint.getServicePort()).append(path).toString();
}

private SeldonMessage queryREST(String path, String dataString, PredictiveUnitState state, Endpoint endpoint, boolean isDefault)
{
long timeNow = System.currentTimeMillis();
URI uri;
try
{
if (uriCache.containsKey(endpoint))
uri = uriCache.get(endpoint);
final String uriKey = getUriKey(endpoint, path);
if (uriCache.containsKey(uriKey))
uri = uriCache.get(uriKey);
else
{
URIBuilder builder = new URIBuilder().setScheme("http")
.setHost(endpoint.getServiceHost())
.setPort(endpoint.getServicePort())
.setPath("/"+path);
uri = builder.build();
uriCache.put(endpoint, uri);
uriCache.put(uriKey, uri);
}
} catch (URISyntaxException e)
{
Expand Down
36 changes: 36 additions & 0 deletions engine/src/test/java/io/seldon/engine/service/UriCacheTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.seldon.engine.service;

import org.junit.Test;
import org.junit.Assert;
import io.seldon.protos.DeploymentProtos.Endpoint;


public class UriCacheTest {

@Test
public void testUri()
{
Endpoint endpointA = Endpoint.newBuilder().setServiceHost("hostA").setServicePort(1000).build();
Endpoint endpointA2 = Endpoint.newBuilder().setServiceHost("hostA").setServicePort(1000).build();
Endpoint endpointB = Endpoint.newBuilder().setServiceHost("hostB").setServicePort(1000).build();
final String predictPath = "/predict";
final String predictPath2 = "/predict";
final String feedbackPath = "/feedback";

final String key1 = InternalPredictionService.getUriKey(endpointA, predictPath);
final String key2 = InternalPredictionService.getUriKey(endpointB, predictPath);
final String key3 = InternalPredictionService.getUriKey(endpointA, feedbackPath);

Assert.assertNotEquals(key1, key2);
Assert.assertNotEquals(key1, key3);

final String key4 = InternalPredictionService.getUriKey(endpointA2, predictPath);

Assert.assertEquals(key1, key4);

final String key5 = InternalPredictionService.getUriKey(endpointA, predictPath2);

Assert.assertEquals(key1, key5);
}

}

0 comments on commit 123c02d

Please sign in to comment.