org.apache.curator.x.discovery.strategies.RandomStrategy Java Examples

The following examples show how to use org.apache.curator.x.discovery.strategies.RandomStrategy. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestStrategies.java    From xian with Apache License 2.0 6 votes vote down vote up
@Test
public void     testRandom() throws Exception
{
    final int                       QTY = 10;
    final int                       ITERATIONS = 1000;

    TestInstanceProvider            instanceProvider = new TestInstanceProvider(QTY, 0);
    ProviderStrategy<Void>          strategy = new RandomStrategy<Void>();

    long[]                          counts = new long[QTY];
    for ( int i = 0; i < ITERATIONS; ++i )
    {
        ServiceInstance<Void>   instance = strategy.getInstance(instanceProvider);
        int                     id = Integer.parseInt(instance.getId());
        counts[id]++;
    }

    SummaryStatistics               statistic = new SummaryStatistics();
    for ( int i = 0; i < QTY; ++i )
    {
        statistic.addValue(counts[i]);
    }
    Assert.assertTrue(statistic.getStandardDeviation() <= (QTY * 2), "" + statistic.getStandardDeviation()); // meager check for even distribution
}
 
Example #2
Source File: TestStrategies.java    From xian with Apache License 2.0 6 votes vote down vote up
@Test
public void     testSticky() throws Exception
{
    final int                       QTY = 10;

    TestInstanceProvider            instanceProvider = new TestInstanceProvider(QTY);
    StickyStrategy<Void>            strategy = new StickyStrategy<Void>(new RandomStrategy<Void>());

    ServiceInstance<Void>           theInstance = strategy.getInstance(instanceProvider);
    int                             instanceNumber = strategy.getInstanceNumber();
    for ( int i = 0; i < 1000; ++i )
    {
        Assert.assertEquals(strategy.getInstance(instanceProvider), theInstance);
    }

    // assert what happens when an instance goes down
    instanceProvider = new TestInstanceProvider(QTY, QTY);
    Assert.assertFalse(strategy.getInstance(instanceProvider).equals(theInstance));
    Assert.assertFalse(instanceNumber == strategy.getInstanceNumber());

    theInstance = strategy.getInstance(instanceProvider);
    for ( int i = 0; i < 1000; ++i )
    {
        Assert.assertEquals(strategy.getInstance(instanceProvider), theInstance);
    }
}
 
Example #3
Source File: TestStrategies.java    From curator with Apache License 2.0 6 votes vote down vote up
@Test
public void     testRandom() throws Exception
{
    final int                       QTY = 10;
    final int                       ITERATIONS = 1000;

    TestInstanceProvider            instanceProvider = new TestInstanceProvider(QTY, 0);
    ProviderStrategy<Void>          strategy = new RandomStrategy<Void>();

    long[]                          counts = new long[QTY];
    for ( int i = 0; i < ITERATIONS; ++i )
    {
        ServiceInstance<Void>   instance = strategy.getInstance(instanceProvider);
        int                     id = Integer.parseInt(instance.getId());
        counts[id]++;
    }

    SummaryStatistics               statistic = new SummaryStatistics();
    for ( int i = 0; i < QTY; ++i )
    {
        statistic.addValue(counts[i]);
    }
    Assert.assertTrue(statistic.getStandardDeviation() <= (QTY * 2), "" + statistic.getStandardDeviation()); // meager check for even distribution
}
 
Example #4
Source File: TestStrategies.java    From curator with Apache License 2.0 6 votes vote down vote up
@Test
public void     testSticky() throws Exception
{
    final int                       QTY = 10;

    TestInstanceProvider            instanceProvider = new TestInstanceProvider(QTY);
    StickyStrategy<Void>            strategy = new StickyStrategy<Void>(new RandomStrategy<Void>());

    ServiceInstance<Void>           theInstance = strategy.getInstance(instanceProvider);
    int                             instanceNumber = strategy.getInstanceNumber();
    for ( int i = 0; i < 1000; ++i )
    {
        Assert.assertEquals(strategy.getInstance(instanceProvider), theInstance);
    }

    // assert what happens when an instance goes down
    instanceProvider = new TestInstanceProvider(QTY, QTY);
    Assert.assertFalse(strategy.getInstance(instanceProvider).equals(theInstance));
    Assert.assertFalse(instanceNumber == strategy.getInstanceNumber());

    theInstance = strategy.getInstance(instanceProvider);
    for ( int i = 0; i < 1000; ++i )
    {
        Assert.assertEquals(strategy.getInstance(instanceProvider), theInstance);
    }
}
 
Example #5
Source File: DiscoveryExample.java    From ZKRecipesByExample with Apache License 2.0 6 votes vote down vote up
private static void listRandomInstance(String[] args, ServiceDiscovery<InstanceDetails> serviceDiscovery,
		Map<String, ServiceProvider<InstanceDetails>> providers, String command) throws Exception {
	// this shows how to use a ServiceProvider
	// in a real application you'd create the ServiceProvider early for the
	// service(s) you're interested in
	if (args.length != 1) {
		System.err.println("syntax error (expected random <name>): " + command);
		return;
	}
	String serviceName = args[0];
	ServiceProvider<InstanceDetails> provider = providers.get(serviceName);
	if (provider == null) {
		provider = serviceDiscovery.serviceProviderBuilder().serviceName(serviceName).providerStrategy(new RandomStrategy<InstanceDetails>()).build();
		providers.put(serviceName, provider);
		provider.start();
		Thread.sleep(2500); // give the provider time to warm up - in a real
							// application you wouldn't need to do this
	}
	ServiceInstance<InstanceDetails> instance = provider.getInstance();
	if (instance == null) {
		System.err.println("No instances named: " + serviceName);
	} else {
		outputInstance(instance);
	}
}
 
Example #6
Source File: DiscoveryExample.java    From xian with Apache License 2.0 5 votes vote down vote up
private static void listRandomInstance(String[] args, ServiceDiscovery<InstanceDetails> serviceDiscovery, Map<String, ServiceProvider<InstanceDetails>> providers, String command) throws Exception
{
    // this shows how to use a ServiceProvider
    // in a real application you'd create the ServiceProvider early for the group(s) you're interested in

    if ( args.length != 1 )
    {
        System.err.println("syntax error (expected random <name>): " + command);
        return;
    }

    String                              serviceName = args[0];
    ServiceProvider<InstanceDetails>    provider = providers.get(serviceName);
    if ( provider == null )
    {
        provider = serviceDiscovery.serviceProviderBuilder().serviceName(serviceName).providerStrategy(new RandomStrategy<InstanceDetails>()).build();
        providers.put(serviceName, provider);
        provider.start();

        Thread.sleep(2500); // give the provider time to warm up - in a real application you wouldn't need to do this
    }

    ServiceInstance<InstanceDetails>    instance = provider.getInstance();
    if ( instance == null )
    {
        System.err.println("No instances named: " + serviceName);
    }
    else
    {
        outputInstance(instance);
    }
}
 
Example #7
Source File: TestMapsWithJersey.java    From curator with Apache License 2.0 5 votes vote down vote up
@BeforeMethod
public void         setup() throws Exception
{
    context = new MapDiscoveryContext(new MockServiceDiscovery<Map<String, String>>(), new RandomStrategy<Map<String, String>>(), 1000);
    serviceNamesMarshaller = new JsonServiceNamesMarshaller();
    serviceInstanceMarshaller = new JsonServiceInstanceMarshaller<Map<String, String>>(context);
    serviceInstancesMarshaller = new JsonServiceInstancesMarshaller<Map<String, String>>(context);

    Application                                     application = new DefaultResourceConfig()
    {
        @Override
        public Set<Class<?>> getClasses()
        {
            Set<Class<?>>       classes = Sets.newHashSet();
            classes.add(MapDiscoveryResource.class);
            return classes;
        }

        @Override
        public Set<Object> getSingletons()
        {
            Set<Object>     singletons = Sets.newHashSet();
            singletons.add(context);
            singletons.add(serviceNamesMarshaller);
            singletons.add(serviceInstanceMarshaller);
            singletons.add(serviceInstancesMarshaller);
            return singletons;
        }
    };
    ServletContainer        container = new ServletContainer(application);

    port = InstanceSpec.getRandomPort();
    server = new Server(port);
    Context root = new Context(server, "/", Context.SESSIONS);
    root.addServlet(new ServletHolder(container), "/*");
    server.start();
}
 
Example #8
Source File: TestObjectPayloadWithJersey.java    From curator with Apache License 2.0 5 votes vote down vote up
@BeforeMethod
public void         setup() throws Exception
{
    context = new ServiceDetailsDiscoveryContext(new MockServiceDiscovery<ServiceDetails>(), new RandomStrategy<ServiceDetails>(), 1000);
    serviceNamesMarshaller = new JsonServiceNamesMarshaller();
    serviceInstanceMarshaller = new JsonServiceInstanceMarshaller<ServiceDetails>(context);
    serviceInstancesMarshaller = new JsonServiceInstancesMarshaller<ServiceDetails>(context);

    Application                                     application = new DefaultResourceConfig()
    {
        @Override
        public Set<Class<?>> getClasses()
        {
            Set<Class<?>>       classes = Sets.newHashSet();
            classes.add(ServiceDetailsDiscoveryResource.class);
            return classes;
        }

        @Override
        public Set<Object> getSingletons()
        {
            Set<Object>     singletons = Sets.newHashSet();
            singletons.add(context);
            singletons.add(serviceNamesMarshaller);
            singletons.add(serviceInstanceMarshaller);
            singletons.add(serviceInstancesMarshaller);
            return singletons;
        }
    };
    ServletContainer        container = new ServletContainer(application);

    port = InstanceSpec.getRandomPort();
    server = new Server(port);
    Context root = new Context(server, "/", Context.SESSIONS);
    root.addServlet(new ServletHolder(container), "/*");
    server.start();
}
 
Example #9
Source File: TestStringsWithJersey.java    From curator with Apache License 2.0 5 votes vote down vote up
@BeforeMethod
public void         setup() throws Exception
{
    context = new StringDiscoveryContext(new MockServiceDiscovery<String>(), new RandomStrategy<String>(), 1000);
    serviceNamesMarshaller = new JsonServiceNamesMarshaller();
    serviceInstanceMarshaller = new JsonServiceInstanceMarshaller<String>(context);
    serviceInstancesMarshaller = new JsonServiceInstancesMarshaller<String>(context);

    Application                                     application = new DefaultResourceConfig()
    {
        @Override
        public Set<Class<?>> getClasses()
        {
            Set<Class<?>>       classes = Sets.newHashSet();
            classes.add(StringDiscoveryResource.class);
            return classes;
        }

        @Override
        public Set<Object> getSingletons()
        {
            Set<Object>     singletons = Sets.newHashSet();
            singletons.add(context);
            singletons.add(serviceNamesMarshaller);
            singletons.add(serviceInstanceMarshaller);
            singletons.add(serviceInstancesMarshaller);
            return singletons;
        }
    };
    ServletContainer        container = new ServletContainer(application);

    port = InstanceSpec.getRandomPort();
    server = new Server(port);
    Context root = new Context(server, "/", Context.SESSIONS);
    root.addServlet(new ServletHolder(container), "/*");
    server.start();
}
 
Example #10
Source File: DiscoveryExample.java    From curator with Apache License 2.0 5 votes vote down vote up
private static void listRandomInstance(String[] args, ServiceDiscovery<InstanceDetails> serviceDiscovery, Map<String, ServiceProvider<InstanceDetails>> providers, String command) throws Exception
{
    // this shows how to use a ServiceProvider
    // in a real application you'd create the ServiceProvider early for the service(s) you're interested in

    if ( args.length != 1 )
    {
        System.err.println("syntax error (expected random <name>): " + command);
        return;
    }

    String                              serviceName = args[0];
    ServiceProvider<InstanceDetails>    provider = providers.get(serviceName);
    if ( provider == null )
    {
        provider = serviceDiscovery.serviceProviderBuilder().serviceName(serviceName).providerStrategy(new RandomStrategy<InstanceDetails>()).build();
        providers.put(serviceName, provider);
        provider.start();

        Thread.sleep(2500); // give the provider time to warm up - in a real application you wouldn't need to do this
    }

    ServiceInstance<InstanceDetails>    instance = provider.getInstance();
    if ( instance == null )
    {
        System.err.println("No instances named: " + serviceName);
    }
    else
    {
        outputInstance(instance);
    }
}
 
Example #11
Source File: ServiceCacheLeakTester.java    From xian with Apache License 2.0 4 votes vote down vote up
private static ServiceProvider<Void> serviceProvider(ServiceDiscovery<Void> serviceDiscovery, String name) throws Exception
{
    return serviceDiscovery.serviceProviderBuilder().serviceName(name).providerStrategy(new RandomStrategy<Void>()).build();
}
 
Example #12
Source File: ServiceCacheLeakTester.java    From curator with Apache License 2.0 4 votes vote down vote up
private static ServiceProvider<Void> serviceProvider(ServiceDiscovery<Void> serviceDiscovery, String name) throws Exception
{
    return serviceDiscovery.serviceProviderBuilder().serviceName(name).providerStrategy(new RandomStrategy<Void>()).build();
}