Search in sources :

Example 1 with SharedLimiterKey

use of org.apache.gobblin.util.limiter.broker.SharedLimiterKey in project incubator-gobblin by apache.

the class MRStressTest method createLimiter.

static Limiter createLimiter(Configuration configuration, SharedResourcesBroker<SimpleScopeType> broker) {
    try {
        Limiter limiter = new NoopLimiter();
        long localQps = configuration.getLong(LOCALLY_ENFORCED_QPS, 0);
        if (localQps > 0) {
            log.info("Setting up local qps " + localQps);
            limiter = new MultiLimiter(limiter, new RateBasedLimiter(localQps));
        }
        if (configuration.getBoolean(USE_THROTTLING_SERVER, false)) {
            log.info("Setting up remote throttling.");
            String resourceId = configuration.get(RESOURCE_ID);
            Limiter globalLimiter = broker.getSharedResource(new RestliLimiterFactory<SimpleScopeType>(), new SharedLimiterKey(resourceId));
            limiter = new MultiLimiter(limiter, globalLimiter);
        }
        return limiter;
    } catch (NotConfiguredException nce) {
        throw new RuntimeException(nce);
    }
}
Also used : NotConfiguredException(org.apache.gobblin.broker.iface.NotConfiguredException) NoopLimiter(org.apache.gobblin.util.limiter.NoopLimiter) RateBasedLimiter(org.apache.gobblin.util.limiter.RateBasedLimiter) MultiLimiter(org.apache.gobblin.util.limiter.MultiLimiter) SimpleScopeType(org.apache.gobblin.broker.SimpleScopeType) SharedLimiterKey(org.apache.gobblin.util.limiter.broker.SharedLimiterKey) Limiter(org.apache.gobblin.util.limiter.Limiter) MultiLimiter(org.apache.gobblin.util.limiter.MultiLimiter) RateBasedLimiter(org.apache.gobblin.util.limiter.RateBasedLimiter) NoopLimiter(org.apache.gobblin.util.limiter.NoopLimiter)

Example 2 with SharedLimiterKey

use of org.apache.gobblin.util.limiter.broker.SharedLimiterKey in project incubator-gobblin by apache.

the class LocalStressTest method main.

public static void main(String[] args) throws Exception {
    CommandLine cli = StressTestUtils.parseCommandLine(OPTIONS, args);
    int stressorThreads = Integer.parseInt(cli.getOptionValue(STRESSOR_THREADS.getOpt(), Integer.toString(DEFAULT_STRESSOR_THREADS)));
    int processorThreads = Integer.parseInt(cli.getOptionValue(PROCESSOR_THREADS.getOpt(), Integer.toString(DEFAULT_PROCESSOR_THREADS)));
    int artificialLatency = Integer.parseInt(cli.getOptionValue(ARTIFICIAL_LATENCY.getOpt(), Integer.toString(DEFAULT_ARTIFICIAL_LATENCY)));
    long targetQps = Integer.parseInt(cli.getOptionValue(QPS.getOpt(), Integer.toString(DEFAULT_TARGET_QPS)));
    Configuration configuration = new Configuration();
    StressTestUtils.populateConfigFromCli(configuration, cli);
    String resourceLimited = LocalStressTest.class.getSimpleName();
    Map<String, String> configMap = Maps.newHashMap();
    ThrottlingPolicyFactory factory = new ThrottlingPolicyFactory();
    SharedLimiterKey res1key = new SharedLimiterKey(resourceLimited);
    configMap.put(BrokerConfigurationKeyGenerator.generateKey(factory, res1key, null, ThrottlingPolicyFactory.POLICY_KEY), QPSPolicy.FACTORY_ALIAS);
    configMap.put(BrokerConfigurationKeyGenerator.generateKey(factory, res1key, null, QPSPolicy.QPS), Long.toString(targetQps));
    ThrottlingGuiceServletConfig guiceServletConfig = new ThrottlingGuiceServletConfig();
    guiceServletConfig.initialize(ConfigFactory.parseMap(configMap));
    LimiterServerResource limiterServer = guiceServletConfig.getInjector().getInstance(LimiterServerResource.class);
    RateComputingLimiterContainer limiterContainer = new RateComputingLimiterContainer();
    Class<? extends Stressor> stressorClass = configuration.getClass(StressTestUtils.STRESSOR_CLASS, StressTestUtils.DEFAULT_STRESSOR_CLASS, Stressor.class);
    ExecutorService executorService = Executors.newFixedThreadPool(stressorThreads);
    SharedResourcesBroker broker = guiceServletConfig.getInjector().getInstance(Key.get(SharedResourcesBroker.class, Names.named(LimiterServerResource.BROKER_INJECT_NAME)));
    ThrottlingPolicy policy = (ThrottlingPolicy) broker.getSharedResource(new ThrottlingPolicyFactory(), new SharedLimiterKey(resourceLimited));
    ScheduledExecutorService reportingThread = Executors.newSingleThreadScheduledExecutor();
    reportingThread.scheduleAtFixedRate(new Reporter(limiterContainer, policy), 0, 15, TimeUnit.SECONDS);
    Queue<Future<?>> futures = new LinkedList<>();
    MockRequester requester = new MockRequester(limiterServer, artificialLatency, processorThreads);
    requester.start();
    for (int i = 0; i < stressorThreads; i++) {
        RestliServiceBasedLimiter restliLimiter = RestliServiceBasedLimiter.builder().resourceLimited(resourceLimited).requestSender(requester).serviceIdentifier("stressor" + i).build();
        Stressor stressor = stressorClass.newInstance();
        stressor.configure(configuration);
        futures.add(executorService.submit(new StressorRunner(limiterContainer.decorateLimiter(restliLimiter), stressor)));
    }
    int stressorFailures = 0;
    for (Future<?> future : futures) {
        try {
            future.get();
        } catch (ExecutionException ee) {
            stressorFailures++;
        }
    }
    requester.stop();
    executorService.shutdownNow();
    if (stressorFailures > 0) {
        log.error("There were " + stressorFailures + " failed stressor threads.");
    }
    System.exit(stressorFailures);
}
Also used : Configuration(org.apache.hadoop.conf.Configuration) SharedResourcesBroker(org.apache.gobblin.broker.iface.SharedResourcesBroker) MockRequester(org.apache.gobblin.util.limiter.MockRequester) Stressor(org.apache.gobblin.util.limiter.stressTest.Stressor) RateComputingLimiterContainer(org.apache.gobblin.util.limiter.stressTest.RateComputingLimiterContainer) SharedLimiterKey(org.apache.gobblin.util.limiter.broker.SharedLimiterKey) ExecutionException(java.util.concurrent.ExecutionException) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) LinkedList(java.util.LinkedList) CommandLine(org.apache.commons.cli.CommandLine) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) RestliServiceBasedLimiter(org.apache.gobblin.util.limiter.RestliServiceBasedLimiter)

Example 3 with SharedLimiterKey

use of org.apache.gobblin.util.limiter.broker.SharedLimiterKey in project incubator-gobblin by apache.

the class ThrottlingClientTest method test.

@Test
public void test() throws Exception {
    ThrottlingPolicyFactory factory = new ThrottlingPolicyFactory();
    SharedLimiterKey res1key = new SharedLimiterKey("res1");
    Map<String, String> configMap = ImmutableMap.<String, String>builder().put(BrokerConfigurationKeyGenerator.generateKey(factory, res1key, null, ThrottlingPolicyFactory.POLICY_KEY), CountBasedPolicy.FACTORY_ALIAS).put(BrokerConfigurationKeyGenerator.generateKey(factory, res1key, null, CountBasedPolicy.COUNT_KEY), "50").put(BrokerConfigurationKeyGenerator.generateKey(factory, null, null, ThrottlingPolicyFactory.FAIL_ON_UNKNOWN_RESOURCE_ID), "true").build();
    ThrottlingGuiceServletConfig guiceServletConfig = new ThrottlingGuiceServletConfig();
    guiceServletConfig.initialize(ConfigFactory.parseMap(configMap));
    Injector injector = guiceServletConfig.getInjector();
    EmbeddedRestliServer server = EmbeddedRestliServer.builder().resources(Lists.<Class<? extends BaseResource>>newArrayList(LimiterServerResource.class)).injector(injector).build();
    try {
        server.startAsync();
        server.awaitRunning();
        final HttpClientFactory http = new HttpClientFactory();
        final Client r2Client = new TransportClientAdapter(http.getClient(Collections.<String, String>emptyMap()));
        RestClient restClient = new RestClient(r2Client, server.getURIPrefix());
        PermitsGetRequestBuilder getBuilder = new PermitsRequestBuilders().get();
        PermitRequest res1request = new PermitRequest();
        res1request.setPermits(20);
        res1request.setResource(res1key.getResourceLimitedPath());
        PermitAllocation allocation = getPermitAllocation(res1request, restClient, getBuilder);
        Assert.assertEquals(allocation.getPermits(), new Long(20));
        allocation = getPermitAllocation(res1request, restClient, getBuilder);
        Assert.assertEquals(allocation.getPermits(), new Long(20));
        // out of permits
        try {
            allocation = getPermitAllocation(res1request, restClient, getBuilder);
            Assert.fail();
        } catch (RestLiResponseException exc) {
            Assert.assertEquals(exc.getStatus(), HttpStatus.S_403_FORBIDDEN.getCode());
        }
        PermitRequest invalidRequest = new PermitRequest();
        invalidRequest.setPermits(20);
        invalidRequest.setResource("invalidkey");
        try {
            allocation = getPermitAllocation(invalidRequest, restClient, getBuilder);
            Assert.fail();
        } catch (RestLiResponseException exc) {
            Assert.assertEquals(exc.getStatus(), 422);
        }
    } finally {
        if (server.isRunning()) {
            server.stopAsync();
            server.awaitTerminated();
        }
    }
}
Also used : EmbeddedRestliServer(org.apache.gobblin.restli.EmbeddedRestliServer) RestClient(com.linkedin.restli.client.RestClient) Injector(com.google.inject.Injector) TransportClientAdapter(com.linkedin.r2.transport.common.bridge.client.TransportClientAdapter) RestLiResponseException(com.linkedin.restli.client.RestLiResponseException) SharedLimiterKey(org.apache.gobblin.util.limiter.broker.SharedLimiterKey) RestClient(com.linkedin.restli.client.RestClient) Client(com.linkedin.r2.transport.common.Client) HttpClientFactory(com.linkedin.r2.transport.http.client.HttpClientFactory) Test(org.testng.annotations.Test)

Example 4 with SharedLimiterKey

use of org.apache.gobblin.util.limiter.broker.SharedLimiterKey in project incubator-gobblin by apache.

the class RestliLimiterFactoryTest method testRestliLimiterCalledByLimiterFactory.

@Test
public void testRestliLimiterCalledByLimiterFactory() throws Exception {
    SharedResourcesBroker<SimpleScopeType> broker = SharedResourcesBrokerFactory.createDefaultTopLevelBroker(ConfigFactory.empty(), SimpleScopeType.GLOBAL.defaultScopeInstance());
    MyRequestSender requestSender = new MyRequestSender();
    broker.bindSharedResourceAtScope(new RedirectAwareRestClientRequestSender.Factory<>(), new SharedRestClientKey(RestliLimiterFactory.RESTLI_SERVICE_NAME), SimpleScopeType.GLOBAL, requestSender);
    Limiter limiter = broker.getSharedResource(new SharedLimiterFactory<>(), new SharedLimiterKey("my/resource"));
    Assert.assertNotNull(limiter.acquirePermits(10));
    Assert.assertEquals(requestSender.requestList.size(), 1);
    broker.close();
}
Also used : SharedRestClientKey(org.apache.gobblin.restli.SharedRestClientKey) SimpleScopeType(org.apache.gobblin.broker.SimpleScopeType) SharedLimiterKey(org.apache.gobblin.util.limiter.broker.SharedLimiterKey) Test(org.testng.annotations.Test)

Example 5 with SharedLimiterKey

use of org.apache.gobblin.util.limiter.broker.SharedLimiterKey in project incubator-gobblin by apache.

the class RestliServiceBasedLimiterTest method testServerFailover.

@Test
public void testServerFailover() throws Exception {
    try (Closer closer = Closer.create()) {
        SharedLimiterKey res1key = new SharedLimiterKey("res1");
        Map<String, String> configMap = Maps.newHashMap();
        TestingServer zkTestingServer = closer.register(new TestingServer(-1));
        configMap.put(ThrottlingGuiceServletConfig.ZK_STRING_KEY, zkTestingServer.getConnectString());
        configMap.put(ThrottlingGuiceServletConfig.HA_CLUSTER_NAME, RestliServiceBasedLimiterTest.class.getSimpleName() + "_cluster");
        Config config = ConfigFactory.parseMap(configMap);
        RestliServer server2500 = createAndStartServer(config, 2500);
        RestliServer server2501 = createAndStartServer(config, 2501);
        SharedResourcesBroker<SimpleScopeType> broker = SharedResourcesBrokerFactory.createDefaultTopLevelBroker(ConfigFactory.empty(), SimpleScopeType.GLOBAL.defaultScopeInstance());
        RedirectAwareRestClientRequestSender requestSender = new RedirectAwareRestClientRequestSender(broker, Lists.newArrayList(server2500.getServer().getURIPrefix(), server2501.getServer().getURIPrefix()));
        RestliServiceBasedLimiter limiter = RestliServiceBasedLimiter.builder().requestSender(requestSender).resourceLimited(res1key.getResourceLimitedPath()).serviceIdentifier("service").build();
        Assert.assertNotNull(limiter.acquirePermits(20));
        limiter.clearAllStoredPermits();
        server2500.close();
        Assert.assertNotNull(limiter.acquirePermits(20));
        Assert.assertEquals(parsePortOfCurrentServerPrefix(requestSender), 2501);
        limiter.clearAllStoredPermits();
        server2500 = createAndStartServer(config, 2500);
        Assert.assertNotNull(limiter.acquirePermits(20));
        limiter.clearAllStoredPermits();
        // leader is currently 2501
        Assert.assertEquals(parsePortOfCurrentServerPrefix(requestSender), 2501);
        // set request to 2500 (not leader)
        requestSender.updateRestClient(server2500.getServer().getURIPrefix(), "test");
        Assert.assertEquals(parsePortOfCurrentServerPrefix(requestSender), 2500);
        Assert.assertNotNull(limiter.acquirePermits(20));
        // verify request sender switched back to leader
        Assert.assertEquals(parsePortOfCurrentServerPrefix(requestSender), 2501);
        server2501.close();
        Assert.assertNotNull(limiter.acquirePermits(20));
        limiter.clearAllStoredPermits();
        server2500.close();
        Assert.assertNull(limiter.acquirePermits(20));
        limiter.clearAllStoredPermits();
    }
}
Also used : Closer(com.google.common.io.Closer) TestingServer(org.apache.curator.test.TestingServer) EmbeddedRestliServer(org.apache.gobblin.restli.EmbeddedRestliServer) ThrottlingGuiceServletConfig(org.apache.gobblin.restli.throttling.ThrottlingGuiceServletConfig) Config(com.typesafe.config.Config) SharedLimiterKey(org.apache.gobblin.util.limiter.broker.SharedLimiterKey) SimpleScopeType(org.apache.gobblin.broker.SimpleScopeType) Test(org.testng.annotations.Test)

Aggregations

SharedLimiterKey (org.apache.gobblin.util.limiter.broker.SharedLimiterKey)13 Test (org.testng.annotations.Test)9 SimpleScopeType (org.apache.gobblin.broker.SimpleScopeType)6 Injector (com.google.inject.Injector)4 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)3 NotConfiguredException (org.apache.gobblin.broker.iface.NotConfiguredException)3 EmbeddedRestliServer (org.apache.gobblin.restli.EmbeddedRestliServer)3 SharedRestClientKey (org.apache.gobblin.restli.SharedRestClientKey)3 Meter (com.codahale.metrics.Meter)2 Config (com.typesafe.config.Config)2 MetricContext (org.apache.gobblin.metrics.MetricContext)2 MetricContextFactory (org.apache.gobblin.metrics.broker.MetricContextFactory)2 SubTaggedMetricContextKey (org.apache.gobblin.metrics.broker.SubTaggedMetricContextKey)2 ThrottlingGuiceServletConfig (org.apache.gobblin.restli.throttling.ThrottlingGuiceServletConfig)2 Timer (com.codahale.metrics.Timer)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Closer (com.google.common.io.Closer)1 DataMap (com.linkedin.data.DataMap)1 StringMap (com.linkedin.data.template.StringMap)1 Client (com.linkedin.r2.transport.common.Client)1