Search in sources :

Example 1 with LoadedInstanceConfig

use of com.netflix.exhibitor.core.config.LoadedInstanceConfig in project exhibitor by soabase.

the class TestFlexibleAutoInstanceManagement method testAdditionWithConfigContention.

@Test
public void testAdditionWithConfigContention() throws Exception {
    MockExhibitorInstance mockExhibitorInstance = new MockExhibitorInstance("new");
    mockExhibitorInstance.getMockConfigProvider().setConfig(StringConfigs.SERVERS_SPEC, "1:a,2:b,3:c");
    mockExhibitorInstance.getMockConfigProvider().setConfig(IntConfigs.AUTO_MANAGE_INSTANCES, 1);
    mockExhibitorInstance.getMockConfigProvider().setConfig(IntConfigs.AUTO_MANAGE_INSTANCES_SETTLING_PERIOD_MS, 0);
    mockExhibitorInstance.getMockConfigProvider().setConfig(IntConfigs.AUTO_MANAGE_INSTANCES_FIXED_ENSEMBLE_SIZE, 0);
    List<ServerStatus> statuses = Lists.newArrayList();
    statuses.add(new ServerStatus("a", InstanceStateTypes.SERVING.getCode(), "", true));
    statuses.add(new ServerStatus("b", InstanceStateTypes.SERVING.getCode(), "", false));
    statuses.add(new ServerStatus("c", InstanceStateTypes.SERVING.getCode(), "", false));
    Mockito.when(mockExhibitorInstance.getMockForkJoinPool().invoke(Mockito.isA(ClusterStatusTask.class))).thenReturn(statuses);
    AutomaticInstanceManagement management = new AutomaticInstanceManagement(mockExhibitorInstance.getMockExhibitor());
    LoadedInstanceConfig loadedInstanceConfig = mockExhibitorInstance.getMockExhibitor().getConfigManager().getLoadedInstanceConfig();
    LoadedInstanceConfig changedCoadedInstanceConfig = new LoadedInstanceConfig(loadedInstanceConfig.getConfig(), loadedInstanceConfig.getVersion() + 1);
    mockExhibitorInstance.getMockExhibitor().getConfigManager().testingSetLoadedInstanceConfig(changedCoadedInstanceConfig);
    management.call();
    // instance addition should have failed
    Assert.assertEquals(mockExhibitorInstance.getMockExhibitor().getConfigManager().getConfig().getString(StringConfigs.SERVERS_SPEC), "1:a,2:b,3:c");
}
Also used : ServerStatus(com.netflix.exhibitor.core.entities.ServerStatus) LoadedInstanceConfig(com.netflix.exhibitor.core.config.LoadedInstanceConfig) Test(org.testng.annotations.Test)

Example 2 with LoadedInstanceConfig

use of com.netflix.exhibitor.core.config.LoadedInstanceConfig in project exhibitor by soabase.

the class NoneConfigProvider method storeConfig.

@Override
public LoadedInstanceConfig storeConfig(ConfigCollection config, long compareVersion) throws Exception {
    File propertiesFile = new File(directory, FILE_NAME);
    PropertyBasedInstanceConfig propertyBasedInstanceConfig = new PropertyBasedInstanceConfig(config);
    long lastModified = 0;
    OutputStream out = new BufferedOutputStream(new FileOutputStream(propertiesFile));
    try {
        propertyBasedInstanceConfig.getProperties().store(out, "Auto-generated by Exhibitor");
        lastModified = propertiesFile.lastModified();
    } finally {
        CloseableUtils.closeQuietly(out);
    }
    return new LoadedInstanceConfig(propertyBasedInstanceConfig, lastModified);
}
Also used : PropertyBasedInstanceConfig(com.netflix.exhibitor.core.config.PropertyBasedInstanceConfig) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) LoadedInstanceConfig(com.netflix.exhibitor.core.config.LoadedInstanceConfig)

Example 3 with LoadedInstanceConfig

use of com.netflix.exhibitor.core.config.LoadedInstanceConfig in project exhibitor by soabase.

the class S3ConfigProvider method storeConfig.

@Override
public LoadedInstanceConfig storeConfig(ConfigCollection config, long compareVersion) throws Exception {
    {
        ObjectMetadata metadata = getConfigMetadata();
        if (metadata != null) {
            Date lastModified = metadata.getLastModified();
            if (lastModified.getTime() != compareVersion) {
                // apparently there's no atomic way to do this with S3 so this will have to do
                return null;
            }
        }
    }
    PropertyBasedInstanceConfig propertyBasedInstanceConfig = new PropertyBasedInstanceConfig(config);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    propertyBasedInstanceConfig.getProperties().store(out, "Auto-generated by Exhibitor " + hostname);
    byte[] bytes = out.toByteArray();
    ObjectMetadata metadata = S3Utils.simpleUploadFile(s3Client, bytes, arguments.getBucket(), arguments.getKey());
    return new LoadedInstanceConfig(propertyBasedInstanceConfig, metadata.getLastModified().getTime());
}
Also used : PropertyBasedInstanceConfig(com.netflix.exhibitor.core.config.PropertyBasedInstanceConfig) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) Date(java.util.Date) LoadedInstanceConfig(com.netflix.exhibitor.core.config.LoadedInstanceConfig)

Example 4 with LoadedInstanceConfig

use of com.netflix.exhibitor.core.config.LoadedInstanceConfig in project exhibitor by soabase.

the class ZookeeperConfigProvider method storeConfig.

@Override
public LoadedInstanceConfig storeConfig(ConfigCollection config, long compareVersion) throws Exception {
    PropertyBasedInstanceConfig propertyBasedInstanceConfig = new PropertyBasedInstanceConfig(config);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    propertyBasedInstanceConfig.getProperties().store(out, "Auto-generated by Exhibitor " + hostname);
    byte[] bytes = out.toByteArray();
    int newVersion;
    try {
        Stat stat = client.setData().withVersion((int) compareVersion).forPath(ZKPaths.makePath(configPath, CONFIG_NODE_NAME), bytes);
        newVersion = stat.getVersion();
    } catch (KeeperException.BadVersionException e) {
        // another process got in first
        return null;
    } catch (KeeperException.NoNodeException e) {
        try {
            client.create().creatingParentsIfNeeded().forPath(ZKPaths.makePath(configPath, CONFIG_NODE_NAME), bytes);
            newVersion = 0;
        } catch (KeeperException.NodeExistsException e1) {
            // by implication, another process created the node first
            return null;
        }
    }
    return new LoadedInstanceConfig(propertyBasedInstanceConfig, newVersion);
}
Also used : Stat(org.apache.zookeeper.data.Stat) PropertyBasedInstanceConfig(com.netflix.exhibitor.core.config.PropertyBasedInstanceConfig) ByteArrayOutputStream(java.io.ByteArrayOutputStream) KeeperException(org.apache.zookeeper.KeeperException) LoadedInstanceConfig(com.netflix.exhibitor.core.config.LoadedInstanceConfig)

Example 5 with LoadedInstanceConfig

use of com.netflix.exhibitor.core.config.LoadedInstanceConfig in project exhibitor by soabase.

the class TestZookeeperConfigProvider method testConcurrentModification.

@Test
public void testConcurrentModification() throws Exception {
    ZookeeperConfigProvider config1 = new ZookeeperConfigProvider(client, "/foo", new Properties(), "foo");
    ZookeeperConfigProvider config2 = new ZookeeperConfigProvider(client, "/foo", new Properties(), "foo");
    try {
        config1.start();
        config2.start();
        final Semaphore cacheUpdate2 = new Semaphore(0);
        config2.getPathChildrenCache().getListenable().addListener(new PathChildrenCacheListener() {

            @Override
            public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
                cacheUpdate2.release();
            }
        });
        Properties properties = new Properties();
        properties.setProperty(PropertyBasedInstanceConfig.toName(StringConfigs.ZOO_CFG_EXTRA, PropertyBasedInstanceConfig.ROOT_PROPERTY_PREFIX), "1,2,3");
        LoadedInstanceConfig loaded1 = config1.storeConfig(new PropertyBasedInstanceConfig(properties, new Properties()), -1);
        Assert.assertTrue(timing.acquireSemaphore(cacheUpdate2));
        timing.sleepABit();
        LoadedInstanceConfig loaded2 = config2.loadConfig();
        Assert.assertEquals("1,2,3", loaded2.getConfig().getRootConfig().getString(StringConfigs.ZOO_CFG_EXTRA));
        properties.setProperty(PropertyBasedInstanceConfig.toName(StringConfigs.ZOO_CFG_EXTRA, PropertyBasedInstanceConfig.ROOT_PROPERTY_PREFIX), "4,5,6");
        config2.storeConfig(new PropertyBasedInstanceConfig(properties, new Properties()), loaded2.getVersion());
        Assert.assertNull(config1.storeConfig(new PropertyBasedInstanceConfig(properties, new Properties()), loaded1.getVersion()));
        LoadedInstanceConfig newLoaded1 = config1.loadConfig();
        Assert.assertNotEquals(loaded1.getVersion(), newLoaded1.getVersion());
    } finally {
        CloseableUtils.closeQuietly(config2);
        CloseableUtils.closeQuietly(config1);
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) PathChildrenCacheListener(org.apache.curator.framework.recipes.cache.PathChildrenCacheListener) PathChildrenCacheEvent(org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent) PropertyBasedInstanceConfig(com.netflix.exhibitor.core.config.PropertyBasedInstanceConfig) Semaphore(java.util.concurrent.Semaphore) Properties(java.util.Properties) LoadedInstanceConfig(com.netflix.exhibitor.core.config.LoadedInstanceConfig) Test(org.testng.annotations.Test)

Aggregations

LoadedInstanceConfig (com.netflix.exhibitor.core.config.LoadedInstanceConfig)11 PropertyBasedInstanceConfig (com.netflix.exhibitor.core.config.PropertyBasedInstanceConfig)10 Properties (java.util.Properties)6 File (java.io.File)4 Test (org.testng.annotations.Test)3 BufferedOutputStream (java.io.BufferedOutputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 FileOutputStream (java.io.FileOutputStream)2 OutputStream (java.io.OutputStream)2 RandomAccessFile (java.io.RandomAccessFile)2 FileLock (java.nio.channels.FileLock)2 Date (java.util.Date)2 ObjectMetadata (com.amazonaws.services.s3.model.ObjectMetadata)1 S3Object (com.amazonaws.services.s3.model.S3Object)1 ServerStatus (com.netflix.exhibitor.core.entities.ServerStatus)1 BufferedInputStream (java.io.BufferedInputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileInputStream (java.io.FileInputStream)1 Semaphore (java.util.concurrent.Semaphore)1 CuratorFramework (org.apache.curator.framework.CuratorFramework)1