Search in sources :

Example 1 with SimpleCache

use of io.pravega.common.util.SimpleCache in project pravega by pravega.

the class AutoScaleProcessorTest method testSteadyStateExpiry.

@Test
public void testSteadyStateExpiry() {
    HashMap<String, Pair<Long, Long>> map = new HashMap<>();
    HashMap<String, Long> lastAccessedTime = new HashMap<>();
    List<String> evicted = new ArrayList<>();
    @SuppressWarnings("unchecked") SimpleCache<String, Pair<Long, Long>> simpleCache = mock(SimpleCache.class);
    AtomicLong clock = new AtomicLong(0L);
    Function<Void, Void> cleanup = m -> {
        for (Map.Entry<String, Long> e : lastAccessedTime.entrySet()) {
            if (e.getValue() < clock.get()) {
                lastAccessedTime.remove(e.getKey());
                map.remove(e.getKey());
                evicted.add(e.getKey());
            }
        }
        // remove all that should have expired.
        return null;
    };
    doAnswer(x -> {
        cleanup.apply(null);
        return map.get(x.getArgument(0));
    }).when(simpleCache).get(anyString());
    doAnswer(x -> {
        cleanup.apply(null);
        map.put(x.getArgument(0), x.getArgument(1));
        return map.get(x.getArgument(0));
    }).when(simpleCache).put(anyString(), any());
    doAnswer(x -> cleanup.apply(null)).when(simpleCache).cleanUp();
    AutoScalerConfig config = AutoScalerConfig.builder().with(AutoScalerConfig.CONTROLLER_URI, "tcp://localhost:9090").with(AutoScalerConfig.TLS_ENABLED, false).build();
    ClientConfig objectUnderTest = AutoScaleProcessor.prepareClientConfig(config);
    @Cleanup EventStreamClientFactory eventStreamClientFactory = EventStreamClientFactory.withScope(SCOPE, objectUnderTest);
    @Cleanup TestAutoScaleProcessor monitor = new TestAutoScaleProcessor(AutoScalerConfig.builder().with(AutoScalerConfig.MUTE_IN_SECONDS, 0).with(AutoScalerConfig.COOLDOWN_IN_SECONDS, 0).with(AutoScalerConfig.AUTH_ENABLED, authEnabled).with(AutoScalerConfig.CACHE_CLEANUP_IN_SECONDS, 150).with(AutoScalerConfig.CACHE_EXPIRY_IN_SECONDS, 60).build(), eventStreamClientFactory, executorService(), simpleCache);
    String streamSegmentName1 = NameUtils.getQualifiedStreamSegmentName(SCOPE, STREAM1, 0L);
    monitor.setTimeMillis(0L);
    clock.set(0L);
    monitor.notifyCreated(streamSegmentName1);
    monitor.put(streamSegmentName1, new ImmutablePair<>(5L, 5L));
    monitor.setTimeMillis(30 * 1000L);
    clock.set(30L);
    monitor.report(streamSegmentName1, 10L, 0L, 10D, 10D, 10D, 10D);
    monitor.setTimeMillis(80 * 1000L);
    clock.set(80L);
    simpleCache.cleanUp();
    assertNotNull(monitor.get(streamSegmentName1));
    assertNotNull(simpleCache.get(streamSegmentName1));
    assertTrue(evicted.isEmpty());
    AssertExtensions.assertThrows("NPE should be thrown", () -> new AutoScaleProcessor(null, config, executorService()), e -> e instanceof NullPointerException);
    AssertExtensions.assertThrows("NPE should be thrown", () -> new AutoScaleProcessor(null, eventStreamClientFactory, executorService()), e -> e instanceof NullPointerException);
    AssertExtensions.assertThrows("NPE should be thrown", () -> new AutoScaleProcessor(AutoScalerConfig.builder().with(AutoScalerConfig.MUTE_IN_SECONDS, 0).with(AutoScalerConfig.COOLDOWN_IN_SECONDS, 0).with(AutoScalerConfig.AUTH_ENABLED, authEnabled).with(AutoScalerConfig.CACHE_CLEANUP_IN_SECONDS, 150).with(AutoScalerConfig.CACHE_EXPIRY_IN_SECONDS, 60).build(), eventStreamClientFactory, null), e -> e instanceof NullPointerException);
    AssertExtensions.assertThrows("NPE should be thrown", () -> new AutoScaleProcessor(AutoScalerConfig.builder().with(AutoScalerConfig.MUTE_IN_SECONDS, 0).with(AutoScalerConfig.COOLDOWN_IN_SECONDS, 0).with(AutoScalerConfig.AUTH_ENABLED, authEnabled).with(AutoScalerConfig.CACHE_CLEANUP_IN_SECONDS, 150).with(AutoScalerConfig.CACHE_EXPIRY_IN_SECONDS, 60).build(), eventStreamClientFactory, null, simpleCache), e -> e instanceof NullPointerException);
    AssertExtensions.assertThrows("NPE should be thrown", () -> new AutoScaleProcessor(null, eventStreamClientFactory, executorService(), simpleCache), e -> e instanceof NullPointerException);
    monitor.notifySealed(streamSegmentName1);
}
Also used : NotImplementedException(org.apache.commons.lang3.NotImplementedException) AutoScaleEvent(io.pravega.shared.controller.event.AutoScaleEvent) EventStreamWriter(io.pravega.client.stream.EventStreamWriter) AssertExtensions(io.pravega.test.common.AssertExtensions) Exceptions(io.pravega.common.Exceptions) Cleanup(lombok.Cleanup) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Mockito.spy(org.mockito.Mockito.spy) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) ArrayList(java.util.ArrayList) Pair(org.apache.commons.lang3.tuple.Pair) Duration(java.time.Duration) Map(java.util.Map) Mockito.doAnswer(org.mockito.Mockito.doAnswer) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) EventStreamClientFactory(io.pravega.client.EventStreamClientFactory) URI(java.net.URI) SimpleCache(io.pravega.common.util.SimpleCache) EventWriterConfig(io.pravega.client.stream.EventWriterConfig) NameUtils(io.pravega.shared.NameUtils) NonNull(lombok.NonNull) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mockito.times(org.mockito.Mockito.times) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) Mockito.verify(org.mockito.Mockito.verify) Consumer(java.util.function.Consumer) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) SecurityConfigDefaults(io.pravega.test.common.SecurityConfigDefaults) Assert.assertNull(org.junit.Assert.assertNull) ThreadPooledTestSuite(io.pravega.test.common.ThreadPooledTestSuite) Assert.assertFalse(org.junit.Assert.assertFalse) Mockito.any(org.mockito.Mockito.any) Futures(io.pravega.common.concurrent.Futures) Assert.assertEquals(org.junit.Assert.assertEquals) ClientConfig(io.pravega.client.ClientConfig) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) EventStreamClientFactory(io.pravega.client.EventStreamClientFactory) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Cleanup(lombok.Cleanup) AtomicLong(java.util.concurrent.atomic.AtomicLong) AtomicLong(java.util.concurrent.atomic.AtomicLong) ClientConfig(io.pravega.client.ClientConfig) Pair(org.apache.commons.lang3.tuple.Pair) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) Test(org.junit.Test)

Aggregations

ClientConfig (io.pravega.client.ClientConfig)1 EventStreamClientFactory (io.pravega.client.EventStreamClientFactory)1 EventStreamWriter (io.pravega.client.stream.EventStreamWriter)1 EventWriterConfig (io.pravega.client.stream.EventWriterConfig)1 Exceptions (io.pravega.common.Exceptions)1 Futures (io.pravega.common.concurrent.Futures)1 SimpleCache (io.pravega.common.util.SimpleCache)1 NameUtils (io.pravega.shared.NameUtils)1 AutoScaleEvent (io.pravega.shared.controller.event.AutoScaleEvent)1 AssertExtensions (io.pravega.test.common.AssertExtensions)1 SecurityConfigDefaults (io.pravega.test.common.SecurityConfigDefaults)1 ThreadPooledTestSuite (io.pravega.test.common.ThreadPooledTestSuite)1 URI (java.net.URI)1 Duration (java.time.Duration)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)1