Search in sources :

Example 1 with Exhibitor

use of com.netflix.exhibitor.core.Exhibitor in project exhibitor by soabase.

the class TestPathAnalyzer method testBasic.

@Test
public void testBasic() throws Exception {
    final Map<PathAndMax, List<String>> data = Maps.newHashMap();
    data.put(new PathAndMax("/r1", 1), Arrays.asList("p1-0001", "p2-0002"));
    data.put(new PathAndMax("/r2", 1), Arrays.asList("p4-0001", "p1-0002"));
    data.put(new PathAndMax("/r3", 1), Arrays.asList("p2-0001", "p5-0002"));
    data.put(new PathAndMax("/r4", 1), Arrays.asList("p2-0001", "p3-0002"));
    data.put(new PathAndMax("/r5", 1), Arrays.asList("p3-0001", "p4-0002"));
    Exhibitor mockExhibitor = Mockito.mock(Exhibitor.class);
    PathAnalyzer pathAnalyzer = new PathAnalyzer(mockExhibitor, Lists.newArrayList(data.keySet())) {

        @Override
        protected List<String> getChildren(String path) throws Exception {
            List<String> l = data.get(new PathAndMax(path, 0));
            return (l != null) ? l : Lists.<String>newArrayList();
        }

        @Override
        protected String getId(String fullPath) throws Exception {
            return fullPath.substring(4, 6);
        }
    };
    Analysis analysis = pathAnalyzer.analyze();
    System.out.println("Details:");
    System.out.println(analysis.getCompleteData());
    System.out.println();
    System.out.println("Cycles:");
    System.out.println(analysis.getPossibleCycles());
}
Also used : Exhibitor(com.netflix.exhibitor.core.Exhibitor) List(java.util.List) Test(org.testng.annotations.Test)

Example 2 with Exhibitor

use of com.netflix.exhibitor.core.Exhibitor in project exhibitor by soabase.

the class TestMonitorRunningInstance method testServerListHasChanged.

@Test
public void testServerListHasChanged() throws Exception {
    InstanceConfig config = new InstanceConfig() {

        @Override
        public String getString(StringConfigs config) {
            switch(config) {
                case SERVERS_SPEC:
                    {
                        return "S:1:foo,S:2:bar";
                    }
            }
            return null;
        }

        @Override
        public int getInt(IntConfigs config) {
            return 0;
        }
    };
    Exhibitor mockExhibitor = makeMockExhibitor(config, "foo");
    MonitorRunningInstance monitor = new MonitorRunningInstance(mockExhibitor);
    StateAndLeader stateAndLeader = monitor.getStateAndLeader();
    InstanceState localCurrentInstanceState = new InstanceState(new ServerList(config.getString(StringConfigs.SERVERS_SPEC)), stateAndLeader.getState(), new RestartSignificantConfig(config));
    InstanceConfig newConfig = new InstanceConfig() {

        @Override
        public String getString(StringConfigs config) {
            switch(config) {
                case SERVERS_SPEC:
                    {
                        // observer added
                        return "S:1:foo,S:2:bar,O:3:snafu";
                    }
            }
            return null;
        }

        @Override
        public int getInt(IntConfigs config) {
            return 0;
        }
    };
    InstanceState instanceState = new InstanceState(new ServerList(newConfig.getString(StringConfigs.SERVERS_SPEC)), stateAndLeader.getState(), new RestartSignificantConfig(newConfig));
    Assert.assertFalse(monitor.serverListHasChanged(instanceState, localCurrentInstanceState));
    newConfig = new InstanceConfig() {

        @Override
        public String getString(StringConfigs config) {
            switch(config) {
                case SERVERS_SPEC:
                    {
                        // standard added
                        return "S:1:foo,S:2:bar,S:3:snafu";
                    }
            }
            return null;
        }

        @Override
        public int getInt(IntConfigs config) {
            return 0;
        }
    };
    instanceState = new InstanceState(new ServerList(newConfig.getString(StringConfigs.SERVERS_SPEC)), stateAndLeader.getState(), new RestartSignificantConfig(newConfig));
    Assert.assertTrue(monitor.serverListHasChanged(instanceState, localCurrentInstanceState));
    newConfig = new InstanceConfig() {

        @Override
        public String getString(StringConfigs config) {
            switch(config) {
                case SERVERS_SPEC:
                    {
                        // "us" changed to observer
                        return "O:1:foo,S:2:bar";
                    }
            }
            return null;
        }

        @Override
        public int getInt(IntConfigs config) {
            return 0;
        }
    };
    instanceState = new InstanceState(new ServerList(newConfig.getString(StringConfigs.SERVERS_SPEC)), stateAndLeader.getState(), new RestartSignificantConfig(newConfig));
    Assert.assertTrue(monitor.serverListHasChanged(instanceState, localCurrentInstanceState));
    newConfig = new InstanceConfig() {

        @Override
        public String getString(StringConfigs config) {
            switch(config) {
                case SERVERS_SPEC:
                    {
                        // not-us changed to observer
                        return "S:1:foo,O:2:bar";
                    }
            }
            return null;
        }

        @Override
        public int getInt(IntConfigs config) {
            return 0;
        }
    };
    instanceState = new InstanceState(new ServerList(newConfig.getString(StringConfigs.SERVERS_SPEC)), stateAndLeader.getState(), new RestartSignificantConfig(newConfig));
    Assert.assertTrue(monitor.serverListHasChanged(instanceState, localCurrentInstanceState));
}
Also used : InstanceConfig(com.netflix.exhibitor.core.config.InstanceConfig) Exhibitor(com.netflix.exhibitor.core.Exhibitor) IntConfigs(com.netflix.exhibitor.core.config.IntConfigs) StringConfigs(com.netflix.exhibitor.core.config.StringConfigs) Test(org.testng.annotations.Test)

Example 3 with Exhibitor

use of com.netflix.exhibitor.core.Exhibitor in project exhibitor by soabase.

the class TestMonitorRunningInstance method makeMockExhibitor.

private Exhibitor makeMockExhibitor(InstanceConfig config, String us) {
    Preferences preferences = Mockito.mock(Preferences.class);
    ControlPanelValues controlPanelValues = new ControlPanelValues(preferences) {

        @Override
        public boolean isSet(ControlPanelTypes type) throws Exception {
            return true;
        }
    };
    ConfigManager configManager = Mockito.mock(ConfigManager.class);
    Mockito.when(configManager.getConfig()).thenReturn(config);
    Exhibitor mockExhibitor = Mockito.mock(Exhibitor.class, Mockito.RETURNS_MOCKS);
    Mockito.when(mockExhibitor.getConfigManager()).thenReturn(configManager);
    Mockito.when(mockExhibitor.getThisJVMHostname()).thenReturn(us);
    Mockito.when(mockExhibitor.getControlPanelValues()).thenReturn(controlPanelValues);
    return mockExhibitor;
}
Also used : ControlPanelValues(com.netflix.exhibitor.core.controlpanel.ControlPanelValues) ControlPanelTypes(com.netflix.exhibitor.core.controlpanel.ControlPanelTypes) Exhibitor(com.netflix.exhibitor.core.Exhibitor) Preferences(java.util.prefs.Preferences) ConfigManager(com.netflix.exhibitor.core.config.ConfigManager)

Example 4 with Exhibitor

use of com.netflix.exhibitor.core.Exhibitor in project exhibitor by soabase.

the class ExhibitorServletContextListener method contextInitialized.

@Override
public void contextInitialized(ServletContextEvent event) {
    Map<String, String> argsBuilder = makeArgsBuilder();
    try {
        exhibitorCreator = new ExhibitorCreator(toArgsArray(argsBuilder));
        exhibitor = new Exhibitor(exhibitorCreator.getConfigProvider(), null, exhibitorCreator.getBackupProvider(), exhibitorCreator.getBuilder().build());
        exhibitor.start();
        event.getServletContext().setAttribute(ExhibitorServletContextListener.class.getName(), exhibitor);
    } catch (MissingConfigurationTypeException exit) {
        log.error("Configuration type (" + OUR_PREFIX + ExhibitorCLI.CONFIG_TYPE + ") must be specified");
        exit.getCli().logHelp(OUR_PREFIX);
        throw new RuntimeException(exit);
    } catch (ExhibitorCreatorExit exit) {
        if (exit.getError() != null) {
            log.error(exit.getError());
        }
        exit.getCli().logHelp(OUR_PREFIX);
        throw new RuntimeException(exit);
    } catch (Exception e) {
        log.error("Trying to create Exhibitor", e);
        throw new RuntimeException(e);
    }
}
Also used : ExhibitorCreator(com.netflix.exhibitor.standalone.ExhibitorCreator) MissingConfigurationTypeException(com.netflix.exhibitor.standalone.MissingConfigurationTypeException) Exhibitor(com.netflix.exhibitor.core.Exhibitor) ExhibitorCreatorExit(com.netflix.exhibitor.standalone.ExhibitorCreatorExit) IOException(java.io.IOException) MissingConfigurationTypeException(com.netflix.exhibitor.standalone.MissingConfigurationTypeException)

Example 5 with Exhibitor

use of com.netflix.exhibitor.core.Exhibitor in project exhibitor by soabase.

the class TestRollingConfigChange method testFailedQuorum.

@Test
public void testFailedQuorum() 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 {
            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.NOT_SERVING, new RestartSignificantConfig(null));
    final AtomicBoolean hasBeenCanceled = new AtomicBoolean(false);
    ConfigManager manager = new ConfigManager(mockExhibitor, provider, 10) {

        @Override
        public synchronized void cancelRollingConfig(CancelMode mode) throws Exception {
            super.cancelRollingConfig(mode);
            hasBeenCanceled.set(true);
        }
    };
    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);
        String hostname = manager.getRollingConfigState().getRollingHostNames().get(0);
        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);
        for (int i = 0; i < (ConfigManager.DEFAULT_MAX_ATTEMPTS - 1); ++i) {
            long lastModified = modified.get();
            manager.checkRollingConfig(state);
            Assert.assertFalse(modified.get() > lastModified);
            Assert.assertFalse(hasBeenCanceled.get());
        }
        manager.checkRollingConfig(state);
        Assert.assertTrue(hasBeenCanceled.get());
        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) MonitorRunningInstance(com.netflix.exhibitor.core.state.MonitorRunningInstance) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) 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)

Aggregations

Exhibitor (com.netflix.exhibitor.core.Exhibitor)12 Test (org.testng.annotations.Test)9 ActivityLog (com.netflix.exhibitor.core.activity.ActivityLog)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 InstanceState (com.netflix.exhibitor.core.state.InstanceState)5 RestartSignificantConfig (com.netflix.exhibitor.core.state.RestartSignificantConfig)5 IOException (java.io.IOException)5 InstanceConfig (com.netflix.exhibitor.core.config.InstanceConfig)2 IntConfigs (com.netflix.exhibitor.core.config.IntConfigs)2 StringConfigs (com.netflix.exhibitor.core.config.StringConfigs)2 ControlPanelTypes (com.netflix.exhibitor.core.controlpanel.ControlPanelTypes)2 ControlPanelValues (com.netflix.exhibitor.core.controlpanel.ControlPanelValues)2 Semaphore (java.util.concurrent.Semaphore)2