-
Notifications
You must be signed in to change notification settings - Fork 999
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Preload spec in serving cache #152
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,22 +17,26 @@ | |
|
||
package feast.serving.service; | ||
|
||
import com.google.common.base.Ticker; | ||
import com.google.common.cache.CacheBuilder; | ||
import com.google.common.cache.CacheLoader; | ||
import com.google.common.cache.LoadingCache; | ||
import lombok.extern.slf4j.Slf4j; | ||
import com.google.common.util.concurrent.ListenableFuture; | ||
import com.google.common.util.concurrent.ListeningExecutorService; | ||
import feast.serving.exception.SpecRetrievalException; | ||
import feast.specs.EntitySpecProto.EntitySpec; | ||
import feast.specs.FeatureSpecProto.FeatureSpec; | ||
import feast.specs.StorageSpecProto.StorageSpec; | ||
|
||
import java.time.Duration; | ||
import java.util.Collections; | ||
import java.util.Map; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
/** SpecStorage implementation with built-in in-memory cache. */ | ||
@Slf4j | ||
public class CachedSpecStorage implements SpecStorage { | ||
private static final int MAX_SPEC_COUNT = 1000; | ||
|
||
private final CoreService coreService; | ||
private final LoadingCache<String, EntitySpec> entitySpecCache; | ||
private final CacheLoader<String, EntitySpec> entitySpecLoader; | ||
|
@@ -41,14 +45,11 @@ public class CachedSpecStorage implements SpecStorage { | |
private final LoadingCache<String, StorageSpec> storageSpecCache; | ||
private final CacheLoader<String, StorageSpec> storageSpecLoader; | ||
|
||
private static final Duration CACHE_DURATION; | ||
private static final int MAX_SPEC_COUNT = 1000; | ||
|
||
static { | ||
CACHE_DURATION = Duration.ofMinutes(30); | ||
} | ||
|
||
public CachedSpecStorage(CoreService coreService) { | ||
public CachedSpecStorage( | ||
CoreService coreService, | ||
ListeningExecutorService executorService, | ||
Duration cacheDuration, | ||
Ticker ticker) { | ||
this.coreService = coreService; | ||
entitySpecLoader = | ||
new CacheLoader<String, EntitySpec>() { | ||
|
@@ -58,14 +59,25 @@ public EntitySpec load(String key) throws Exception { | |
} | ||
|
||
@Override | ||
public Map<String, EntitySpec> loadAll(Iterable<? extends String> keys) throws Exception { | ||
return coreService.getEntitySpecs((Iterable<String>) keys); | ||
public ListenableFuture<EntitySpec> reload(String key, EntitySpec oldValue) | ||
throws Exception { | ||
return executorService.submit( | ||
() -> { | ||
EntitySpec result = oldValue; | ||
try { | ||
result = coreService.getEntitySpecs(Collections.singleton(key)).get(key); | ||
} catch (Exception e) { | ||
log.error("Error reloading entity spec"); | ||
} | ||
return result; | ||
}); | ||
} | ||
}; | ||
entitySpecCache = | ||
CacheBuilder.newBuilder() | ||
.maximumSize(MAX_SPEC_COUNT) | ||
.expireAfterAccess(CACHE_DURATION) | ||
.refreshAfterWrite(cacheDuration) | ||
.ticker(ticker) | ||
.build(entitySpecLoader); | ||
|
||
featureSpecLoader = | ||
|
@@ -76,34 +88,54 @@ public FeatureSpec load(String key) throws Exception { | |
} | ||
|
||
@Override | ||
public Map<String, FeatureSpec> loadAll(Iterable<? extends String> keys) | ||
public ListenableFuture<FeatureSpec> reload(String key, FeatureSpec oldValue) | ||
throws Exception { | ||
return coreService.getFeatureSpecs((Iterable<String>) keys); | ||
return executorService.submit( | ||
() -> { | ||
FeatureSpec result = oldValue; | ||
try { | ||
result = coreService.getFeatureSpecs(Collections.singleton(key)).get(key); | ||
} catch (Exception e) { | ||
log.error("Error reloading feature spec"); | ||
} | ||
return result; | ||
}); | ||
} | ||
}; | ||
featureSpecCache = | ||
CacheBuilder.newBuilder() | ||
.maximumSize(MAX_SPEC_COUNT) | ||
.expireAfterAccess(CACHE_DURATION) | ||
.refreshAfterWrite(cacheDuration) | ||
.ticker(ticker) | ||
.build(featureSpecLoader); | ||
|
||
storageSpecLoader = | ||
new CacheLoader<String, StorageSpec>() { | ||
@Override | ||
public Map<String, StorageSpec> loadAll(Iterable<? extends String> keys) | ||
throws Exception { | ||
return coreService.getStorageSpecs((Iterable<String>) keys); | ||
public StorageSpec load(String key) throws Exception { | ||
return coreService.getStorageSpecs(Collections.singleton(key)).get(key); | ||
} | ||
|
||
@Override | ||
public StorageSpec load(String key) throws Exception { | ||
return coreService.getStorageSpecs(Collections.singleton(key)).get(key); | ||
public ListenableFuture<StorageSpec> reload(String key, StorageSpec oldValue) | ||
throws Exception { | ||
return executorService.submit( | ||
() -> { | ||
StorageSpec result = oldValue; | ||
try { | ||
result = coreService.getStorageSpecs(Collections.singleton(key)).get(key); | ||
} catch (Exception e) { | ||
log.error("Error reloading storage spec"); | ||
} | ||
return result; | ||
}); | ||
} | ||
}; | ||
storageSpecCache = | ||
CacheBuilder.newBuilder() | ||
.maximumSize(MAX_SPEC_COUNT) | ||
.expireAfterAccess(CACHE_DURATION) | ||
.refreshAfterWrite(cacheDuration) | ||
.ticker(ticker) | ||
.build(storageSpecLoader); | ||
} | ||
|
||
|
@@ -177,4 +209,16 @@ public Map<String, StorageSpec> getAllStorageSpecs() { | |
public boolean isConnected() { | ||
return coreService.isConnected(); | ||
} | ||
|
||
/** Preload all spec into cache. */ | ||
public void populateCache() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With a MAX_SPEC_COUNT of 1000, once you have more than that, you're going to be evicting things that might actually be commonly used every 5 minutes. Is that a problem? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, that's probably introduce problem. Do you think it's better to not limit the cache size based on the number of entry? |
||
Map<String, FeatureSpec> featureSpecMap = coreService.getAllFeatureSpecs(); | ||
featureSpecCache.putAll(featureSpecMap); | ||
|
||
Map<String, EntitySpec> entitySpecMap = coreService.getAllEntitySpecs(); | ||
entitySpecCache.putAll(entitySpecMap); | ||
|
||
Map<String, StorageSpec> storageSpecMap = coreService.getAllStorageSpecs(); | ||
storageSpecCache.putAll(storageSpecMap); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package feast.serving.service; | ||
|
||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.junit.Assert.*; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
import com.google.common.testing.FakeTicker; | ||
import com.google.common.util.concurrent.ListeningExecutorService; | ||
import com.google.common.util.concurrent.MoreExecutors; | ||
import feast.specs.EntitySpecProto.EntitySpec; | ||
import feast.specs.FeatureSpecProto.FeatureSpec; | ||
import feast.specs.StorageSpecProto.StorageSpec; | ||
import java.time.Duration; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.TimeUnit; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
public class CachedSpecStorageTest { | ||
|
||
private FakeTicker fakeTicker; | ||
private CoreService coreService; | ||
private CachedSpecStorage cachedSpecStorage; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
fakeTicker = new FakeTicker(); | ||
coreService = mock(CoreService.class); | ||
ListeningExecutorService executorService = MoreExecutors.listeningDecorator(Executors.newSingleThreadExecutor()); | ||
cachedSpecStorage = new CachedSpecStorage(coreService, executorService, | ||
Duration.ofSeconds(5), fakeTicker); | ||
} | ||
|
||
@Test | ||
public void shouldNotBeNull() { | ||
assertNotNull(cachedSpecStorage); | ||
} | ||
|
||
@Test | ||
public void testPopulateCache() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you add a test for what happens when you exceed the max size of cache in populating. So we're explicit about the intended behaviour. |
||
Map<String, FeatureSpec> featureSpecMap = new HashMap<>(); | ||
featureSpecMap.put("feature_1", mock(FeatureSpec.class)); | ||
|
||
Map<String, StorageSpec> storageSpecMap = new HashMap<>(); | ||
storageSpecMap.put("storage_1", mock(StorageSpec.class)); | ||
|
||
Map<String, EntitySpec> entitySpecMap = new HashMap<>(); | ||
entitySpecMap.put("entity_1", mock(EntitySpec.class)); | ||
|
||
when(coreService.getAllFeatureSpecs()).thenReturn(featureSpecMap); | ||
when(coreService.getAllEntitySpecs()).thenReturn(entitySpecMap); | ||
when(coreService.getAllStorageSpecs()).thenReturn(storageSpecMap); | ||
|
||
cachedSpecStorage.populateCache(); | ||
Map<String, FeatureSpec> result = cachedSpecStorage.getFeatureSpecs(Collections.singletonList("feature_1")); | ||
Map<String, StorageSpec> result1 = cachedSpecStorage.getStorageSpecs(Collections.singletonList("storage_1")); | ||
Map<String, EntitySpec> result2 = cachedSpecStorage.getEntitySpecs(Collections.singletonList("entity_1")); | ||
|
||
assertThat(result.size(), equalTo(1)); | ||
assertThat(result1.size(), equalTo(1)); | ||
assertThat(result2.size(), equalTo(1)); | ||
|
||
verify(coreService, times(0)).getFeatureSpecs(any(Iterable.class)); | ||
verify(coreService, times(0)).getStorageSpecs(any(Iterable.class)); | ||
verify(coreService, times(0)).getEntitySpecs(any(Iterable.class)); | ||
} | ||
|
||
@Test | ||
public void reloadFailureShouldReturnOldValue() { | ||
Map<String, FeatureSpec> featureSpecMap = new HashMap<>(); | ||
featureSpecMap.put("feature_1", mock(FeatureSpec.class)); | ||
|
||
Map<String, StorageSpec> storageSpecMap = new HashMap<>(); | ||
storageSpecMap.put("storage_1", mock(StorageSpec.class)); | ||
|
||
Map<String, EntitySpec> entitySpecMap = new HashMap<>(); | ||
entitySpecMap.put("entity_1", mock(EntitySpec.class)); | ||
|
||
when(coreService.getAllFeatureSpecs()).thenReturn(featureSpecMap); | ||
when(coreService.getFeatureSpecs(any(Iterable.class))).thenThrow(new RuntimeException("error")); | ||
when(coreService.getAllEntitySpecs()).thenReturn(entitySpecMap); | ||
when(coreService.getEntitySpecs(any(Iterable.class))).thenThrow(new RuntimeException("error")); | ||
when(coreService.getAllStorageSpecs()).thenReturn(storageSpecMap); | ||
when(coreService.getStorageSpecs(any(Iterable.class))).thenThrow(new RuntimeException("error")); | ||
|
||
|
||
cachedSpecStorage.populateCache(); | ||
Map<String, FeatureSpec> result = cachedSpecStorage.getFeatureSpecs(Collections.singletonList("feature_1")); | ||
Map<String, StorageSpec> result1 = cachedSpecStorage.getStorageSpecs(Collections.singletonList("storage_1")); | ||
Map<String, EntitySpec> result2 = cachedSpecStorage.getEntitySpecs(Collections.singletonList("entity_1")); | ||
|
||
assertThat(result.size(), equalTo(1)); | ||
assertThat(result1.size(), equalTo(1)); | ||
assertThat(result2.size(), equalTo(1)); | ||
verify(coreService, times(0)).getFeatureSpecs(any(Iterable.class)); | ||
verify(coreService, times(0)).getStorageSpecs(any(Iterable.class)); | ||
verify(coreService, times(0)).getEntitySpecs(any(Iterable.class)); | ||
|
||
fakeTicker.advance(6, TimeUnit.SECONDS); | ||
|
||
result = cachedSpecStorage.getFeatureSpecs(Collections.singletonList("feature_1")); | ||
result1 = cachedSpecStorage.getStorageSpecs(Collections.singletonList("storage_1")); | ||
result2 = cachedSpecStorage.getEntitySpecs(Collections.singletonList("entity_1")); | ||
assertThat(result.size(), equalTo(1)); | ||
assertThat(result1.size(), equalTo(1)); | ||
assertThat(result2.size(), equalTo(1)); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think these can be simplified to:
etc..
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The asyncReloading won't return old value if anything goes wrong during refresh, that's why I used a more verbose implementation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah neat