Search in sources :

Example 6 with ActivityLog

use of com.netflix.exhibitor.core.activity.ActivityLog in project exhibitor by soabase.

the class TestRollingConfigChange method testDownMiddleInstance.

@Test
public void testDownMiddleInstance() throws Exception {
    ServerList serverList = new ServerList("1:aaa,2:two,3:zzz");
    RemoteInstanceRequestClient mockClient = new RemoteInstanceRequestClient() {

        @Override
        public void close() throws IOException {
        }

        @Override
        public <T> T getWebResource(URI remoteUri, MediaType type, Class<T> clazz) throws Exception {
            if (remoteUri.getHost().equals("two")) {
                throw new Exception();
            }
            return clazz.cast("foo");
        }
    };
    ActivityLog log = new ActivityLog(100);
    ActivityQueue activityQueue = new ActivityQueue();
    Exhibitor mockExhibitor = Mockito.mock(Exhibitor.class);
    MonitorRunningInstance mockMonitorRunningInstance = makeMockMonitorRunningInstance();
    Mockito.when(mockExhibitor.getMonitorRunningInstance()).thenReturn(mockMonitorRunningInstance);
    Mockito.when(mockExhibitor.getLog()).thenReturn(log);
    Mockito.when(mockExhibitor.getActivityQueue()).thenReturn(activityQueue);
    Mockito.when(mockExhibitor.getThisJVMHostname()).thenReturn("one");
    Mockito.when(mockExhibitor.getRemoteInstanceRequestClient()).thenReturn(mockClient);
    final AtomicLong modified = new AtomicLong(1);
    ConfigProvider provider = new ConfigWrapper(modified);
    InstanceState state = new InstanceState(serverList, InstanceStateTypes.SERVING, new RestartSignificantConfig(null));
    ConfigManager manager = new ConfigManager(mockExhibitor, provider, 10);
    manager.start();
    try {
        Properties properties = new Properties();
        properties.setProperty(PropertyBasedInstanceConfig.toName(StringConfigs.SERVERS_SPEC, PropertyBasedInstanceConfig.ROOT_PROPERTY_PREFIX), serverList.toSpecString());
        PropertyBasedInstanceConfig config = new PropertyBasedInstanceConfig(properties, DefaultProperties.get(null));
        manager.startRollingConfig(config.getRootConfig(), null);
        for (String hostname : manager.getRollingConfigState().getRollingHostNames()) {
            if (hostname.equals("two")) {
                continue;
            }
            Assert.assertTrue(manager.isRolling());
            RollingReleaseState rollingState = new RollingReleaseState(state, manager.getCollection());
            Assert.assertEquals(rollingState.getCurrentRollingHostname(), hostname);
            Assert.assertNull(manager.getRollingConfigAdvanceAttempt());
            Mockito.when(mockExhibitor.getThisJVMHostname()).thenReturn(hostname);
            long lastModified = modified.get();
            manager.checkRollingConfig(state);
            if (// the next will be the down instance "two"
            hostname.equals("aaa")) {
                for (// don't check last time as it's cleared on MAX
                int i = 1; // don't check last time as it's cleared on MAX
                i < ConfigManager.DEFAULT_MAX_ATTEMPTS; // don't check last time as it's cleared on MAX
                ++i) {
                    Assert.assertNotNull(manager.getRollingConfigAdvanceAttempt());
                    Assert.assertEquals(manager.getRollingConfigAdvanceAttempt().getAttemptCount(), i);
                    manager.checkRollingConfig(state);
                }
            }
            Assert.assertTrue(modified.get() > lastModified);
        }
        Assert.assertFalse(manager.isRolling());
    } finally {
        CloseableUtils.closeQuietly(manager);
    }
}
Also used : ActivityLog(com.netflix.exhibitor.core.activity.ActivityLog) Properties(java.util.Properties) URI(java.net.URI) IOException(java.io.IOException) MonitorRunningInstance(com.netflix.exhibitor.core.state.MonitorRunningInstance) AtomicLong(java.util.concurrent.atomic.AtomicLong) InstanceState(com.netflix.exhibitor.core.state.InstanceState) ActivityQueue(com.netflix.exhibitor.core.activity.ActivityQueue) Exhibitor(com.netflix.exhibitor.core.Exhibitor) ServerList(com.netflix.exhibitor.core.state.ServerList) MediaType(javax.ws.rs.core.MediaType) RemoteInstanceRequestClient(com.netflix.exhibitor.core.automanage.RemoteInstanceRequestClient) RestartSignificantConfig(com.netflix.exhibitor.core.state.RestartSignificantConfig) Test(org.testng.annotations.Test)

Example 7 with ActivityLog

use of com.netflix.exhibitor.core.activity.ActivityLog in project exhibitor by soabase.

the class TestRollingConfigChange method testDownLastInstance.

@Test
public void testDownLastInstance() throws Exception {
    ServerList serverList = new ServerList("1:one,2:two,3:three");
    RemoteInstanceRequestClient mockClient = new RemoteInstanceRequestClient() {

        @Override
        public void close() throws IOException {
        }

        @Override
        public <T> T getWebResource(URI remoteUri, MediaType type, Class<T> clazz) throws Exception {
            if (remoteUri.getHost().equals("two")) {
                throw new Exception();
            }
            return clazz.cast("foo");
        }
    };
    ActivityLog log = new ActivityLog(100);
    ActivityQueue activityQueue = new ActivityQueue();
    Exhibitor mockExhibitor = Mockito.mock(Exhibitor.class);
    MonitorRunningInstance mockMonitorRunningInstance = makeMockMonitorRunningInstance();
    Mockito.when(mockExhibitor.getMonitorRunningInstance()).thenReturn(mockMonitorRunningInstance);
    Mockito.when(mockExhibitor.getLog()).thenReturn(log);
    Mockito.when(mockExhibitor.getActivityQueue()).thenReturn(activityQueue);
    Mockito.when(mockExhibitor.getThisJVMHostname()).thenReturn("one");
    Mockito.when(mockExhibitor.getRemoteInstanceRequestClient()).thenReturn(mockClient);
    final AtomicLong modified = new AtomicLong(1);
    ConfigProvider provider = new ConfigProvider() {

        private volatile ConfigCollection config = new PropertyBasedInstanceConfig(new Properties(), new Properties());

        @Override
        public void start() throws Exception {
        }

        @Override
        public void close() throws IOException {
        }

        @Override
        public LoadedInstanceConfig loadConfig() throws Exception {
            return new LoadedInstanceConfig(config, modified.get());
        }

        @Override
        public PseudoLock newPseudoLock() throws Exception {
            return null;
        }

        @Override
        public LoadedInstanceConfig storeConfig(ConfigCollection config, long compareVersion) throws Exception {
            this.config = config;
            modified.incrementAndGet();
            return loadConfig();
        }
    };
    InstanceState state = new InstanceState(serverList, InstanceStateTypes.SERVING, new RestartSignificantConfig(null));
    ConfigManager manager = new ConfigManager(mockExhibitor, provider, 10);
    manager.start();
    try {
        Properties properties = new Properties();
        properties.setProperty(PropertyBasedInstanceConfig.toName(StringConfigs.SERVERS_SPEC, PropertyBasedInstanceConfig.ROOT_PROPERTY_PREFIX), serverList.toSpecString());
        PropertyBasedInstanceConfig config = new PropertyBasedInstanceConfig(properties, DefaultProperties.get(null));
        manager.startRollingConfig(config.getRootConfig(), null);
        for (String hostname : manager.getRollingConfigState().getRollingHostNames()) {
            if (hostname.equals("two")) {
                Assert.assertFalse(manager.isRolling());
                continue;
            }
            Assert.assertTrue(manager.isRolling());
            RollingReleaseState rollingState = new RollingReleaseState(state, manager.getCollection());
            Assert.assertEquals(rollingState.getCurrentRollingHostname(), hostname);
            Assert.assertNull(manager.getRollingConfigAdvanceAttempt());
            Mockito.when(mockExhibitor.getThisJVMHostname()).thenReturn(hostname);
            long lastModified = modified.get();
            manager.checkRollingConfig(state);
            if (// the next will be the down instance "two"
            hostname.equals("three")) {
                for (// don't check last time as it's cleared on MAX
                int i = 1; // don't check last time as it's cleared on MAX
                i < ConfigManager.DEFAULT_MAX_ATTEMPTS; // don't check last time as it's cleared on MAX
                ++i) {
                    Assert.assertNotNull(manager.getRollingConfigAdvanceAttempt());
                    Assert.assertEquals(manager.getRollingConfigAdvanceAttempt().getAttemptCount(), i);
                    manager.checkRollingConfig(state);
                }
            }
            Assert.assertTrue(modified.get() > lastModified);
        }
        Assert.assertFalse(manager.isRolling());
    } finally {
        CloseableUtils.closeQuietly(manager);
    }
}
Also used : ActivityLog(com.netflix.exhibitor.core.activity.ActivityLog) Properties(java.util.Properties) URI(java.net.URI) IOException(java.io.IOException) MonitorRunningInstance(com.netflix.exhibitor.core.state.MonitorRunningInstance) AtomicLong(java.util.concurrent.atomic.AtomicLong) InstanceState(com.netflix.exhibitor.core.state.InstanceState) ActivityQueue(com.netflix.exhibitor.core.activity.ActivityQueue) Exhibitor(com.netflix.exhibitor.core.Exhibitor) ServerList(com.netflix.exhibitor.core.state.ServerList) MediaType(javax.ws.rs.core.MediaType) RemoteInstanceRequestClient(com.netflix.exhibitor.core.automanage.RemoteInstanceRequestClient) RestartSignificantConfig(com.netflix.exhibitor.core.state.RestartSignificantConfig) Test(org.testng.annotations.Test)

Example 8 with ActivityLog

use of com.netflix.exhibitor.core.activity.ActivityLog in project exhibitor by soabase.

the class TestS3PseudoLock method testMulti.

@Test
public void testMulti() throws Exception {
    final int QTY = 5;
    final int POLLING_MS = 1;
    ActivityLog mockLog = Mockito.mock(ActivityLog.class);
    List<S3PseudoLock> locks = Lists.newArrayList();
    for (int i = 0; i < QTY; ++i) {
        MockS3Client client = new MockS3Client();
        S3PseudoLock lock = new S3PseudoLock(client, "foo", "-prefix-", 10000, POLLING_MS, 0);
        locks.add(lock);
    }
    for (S3PseudoLock lock : locks) {
        Assert.assertTrue(lock.lock(mockLog, 5, TimeUnit.SECONDS));
        try {
            //noinspection PointlessArithmeticExpression
            Thread.sleep(POLLING_MS * 2);
        } finally {
            lock.unlock();
        }
    }
}
Also used : MockS3Client(com.netflix.exhibitor.core.backup.s3.MockS3Client) ActivityLog(com.netflix.exhibitor.core.activity.ActivityLog) Test(org.testng.annotations.Test)

Example 9 with ActivityLog

use of com.netflix.exhibitor.core.activity.ActivityLog in project exhibitor by soabase.

the class TestS3PseudoLock method testBlocking.

// Too flaky to be useful right now. See https://github.com/soabase/exhibitor/issues/329.
@Test(enabled = false)
public void testBlocking() throws Exception {
    final int QTY = 5;
    final int POLLING_MS = 1;
    final AtomicBoolean isLocked = new AtomicBoolean(false);
    final AtomicInteger lockCount = new AtomicInteger(0);
    final MockS3Client client = new MockS3Client(null, null);
    final ActivityLog mockLog = Mockito.mock(ActivityLog.class);
    ExecutorCompletionService<Void> completionService = new ExecutorCompletionService<Void>(Executors.newFixedThreadPool(QTY));
    for (int i = 0; i < QTY; ++i) {
        completionService.submit(new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                S3PseudoLock lock = new S3PseudoLock(client, "foo", "bar", Integer.MAX_VALUE, POLLING_MS, 0);
                try {
                    Assert.assertTrue(lock.lock(mockLog, 10, TimeUnit.SECONDS));
                    Assert.assertTrue(isLocked.compareAndSet(false, true));
                    lockCount.incrementAndGet();
                    Thread.sleep(POLLING_MS);
                } finally {
                    if (isLocked.compareAndSet(true, false)) {
                        lock.unlock();
                    }
                }
                return null;
            }
        });
        Thread.sleep(1);
    }
    for (int i = 0; i < QTY; ++i) {
        completionService.take().get();
    }
    Assert.assertEquals(lockCount.get(), QTY);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MockS3Client(com.netflix.exhibitor.core.backup.s3.MockS3Client) ActivityLog(com.netflix.exhibitor.core.activity.ActivityLog) ExecutorCompletionService(java.util.concurrent.ExecutorCompletionService) Test(org.testng.annotations.Test)

Example 10 with ActivityLog

use of com.netflix.exhibitor.core.activity.ActivityLog in project exhibitor by soabase.

the class TestS3PseudoLock method testSingle.

@Test
public void testSingle() throws Exception {
    MockS3Client client = new MockS3Client();
    ActivityLog mockLog = Mockito.mock(ActivityLog.class);
    S3PseudoLock lock = new S3PseudoLock(client, "foo", "bar", 10000, 1, 0);
    Assert.assertTrue(lock.lock(mockLog, 5, TimeUnit.SECONDS));
    lock.unlock();
}
Also used : MockS3Client(com.netflix.exhibitor.core.backup.s3.MockS3Client) ActivityLog(com.netflix.exhibitor.core.activity.ActivityLog) Test(org.testng.annotations.Test)

Aggregations

ActivityLog (com.netflix.exhibitor.core.activity.ActivityLog)12 Test (org.testng.annotations.Test)11 Exhibitor (com.netflix.exhibitor.core.Exhibitor)7 ActivityQueue (com.netflix.exhibitor.core.activity.ActivityQueue)7 MonitorRunningInstance (com.netflix.exhibitor.core.state.MonitorRunningInstance)7 Properties (java.util.Properties)7 RemoteInstanceRequestClient (com.netflix.exhibitor.core.automanage.RemoteInstanceRequestClient)6 ServerList (com.netflix.exhibitor.core.state.ServerList)6 URI (java.net.URI)6 AtomicLong (java.util.concurrent.atomic.AtomicLong)6 MediaType (javax.ws.rs.core.MediaType)6 MockS3Client (com.netflix.exhibitor.core.backup.s3.MockS3Client)5 InstanceState (com.netflix.exhibitor.core.state.InstanceState)5 RestartSignificantConfig (com.netflix.exhibitor.core.state.RestartSignificantConfig)5 IOException (java.io.IOException)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 ControlPanelTypes (com.netflix.exhibitor.core.controlpanel.ControlPanelTypes)1 ControlPanelValues (com.netflix.exhibitor.core.controlpanel.ControlPanelValues)1 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)1 ExecutorCompletionService (java.util.concurrent.ExecutorCompletionService)1