Search in sources :

Example 1 with Limiter

use of org.apache.gobblin.util.limiter.Limiter 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 Limiter

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

the class LimiterStopEventTest method testGetLimiterStopMetadataCase0.

@Test
public void testGetLimiterStopMetadataCase0() throws InterruptedException {
    Properties properties = new Properties();
    String key1 = "topic";
    String key2 = "partition.id";
    String key3 = "others";
    String keyList = Joiner.on(',').join(key1, key2);
    properties.setProperty(LimiterConfigurationKeys.LIMITER_REPORT_KEY_LIST, keyList);
    properties.setProperty(key1, "1111");
    properties.setProperty(key2, "1111");
    Extractor extractor = mock(Extractor.class);
    Limiter limiter = mock(Limiter.class);
    TaskState taskState = mock(TaskState.class);
    WorkUnit workUnit = mock(WorkUnit.class);
    Mockito.when(taskState.getWorkunit()).thenReturn(workUnit);
    Mockito.when(taskState.getJobId()).thenReturn("123");
    Mockito.when(taskState.getTaskAttemptId()).thenReturn(Optional.of("555"));
    Mockito.when(taskState.getTaskId()).thenReturn("888");
    Mockito.when(limiter.acquirePermits(1)).thenReturn(null);
    Mockito.when(taskState.getProp(ConfigurationKeys.DATASET_URN_KEY, ConfigurationKeys.DEFAULT_DATASET_URN)).thenReturn("file://xyz");
    Mockito.when(workUnit.getProperties()).thenReturn(properties);
    LimitingExtractorDecorator<String, String> decorator = new LimitingExtractorDecorator<>(extractor, limiter, taskState);
    try {
        Method method = LimitingExtractorDecorator.class.getDeclaredMethod("getLimiterStopMetadata");
        method.setAccessible(true);
        ImmutableMap<String, String> metaData = (ImmutableMap<String, String>) method.invoke(decorator);
        Assert.assertEquals(metaData.containsKey(key1), true);
        Assert.assertEquals(metaData.containsKey(key2), true);
        Assert.assertEquals(metaData.containsKey(key3), false);
    } catch (Exception e) {
        Assert.fail();
    }
}
Also used : Extractor(org.apache.gobblin.source.extractor.Extractor) WorkUnit(org.apache.gobblin.source.workunit.WorkUnit) Method(java.lang.reflect.Method) Properties(java.util.Properties) ImmutableMap(com.google.common.collect.ImmutableMap) Limiter(org.apache.gobblin.util.limiter.Limiter) Test(org.testng.annotations.Test)

Example 3 with Limiter

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

the class LimiterStopEventTest method testGetLimiterStopMetadataCase3.

@Test
public void testGetLimiterStopMetadataCase3() throws InterruptedException {
    Properties properties = new Properties();
    String key1 = "topic";
    String key2 = "partition.id";
    String keyList = Joiner.on(',').join(key1, key2);
    String subKey1 = key2 + "....";
    String subKey2 = key2 + "##fjpaierbng;";
    String subKey3 = key2 + "x[n  sdf";
    String subKey4 = key2 + "";
    properties.setProperty(LimiterConfigurationKeys.LIMITER_REPORT_KEY_LIST, keyList);
    properties.setProperty(subKey1, "1111");
    properties.setProperty(subKey2, "1111");
    properties.setProperty(subKey3, "1111");
    properties.setProperty(subKey4, "1111");
    properties.setProperty(key1, "1111");
    properties.setProperty(key2, "1111");
    Extractor extractor = mock(Extractor.class);
    Limiter limiter = mock(Limiter.class);
    TaskState taskState = mock(TaskState.class);
    WorkUnit workUnit = mock(WorkUnit.class);
    Mockito.when(taskState.getWorkunit()).thenReturn(workUnit);
    Mockito.when(taskState.getJobId()).thenReturn("123");
    Mockito.when(taskState.getTaskAttemptId()).thenReturn(Optional.of("555"));
    Mockito.when(taskState.getTaskId()).thenReturn("888");
    Mockito.when(limiter.acquirePermits(1)).thenReturn(null);
    Mockito.when(taskState.getProp(ConfigurationKeys.DATASET_URN_KEY, ConfigurationKeys.DEFAULT_DATASET_URN)).thenReturn("file://xyz");
    Mockito.when(workUnit.getProperties()).thenReturn(properties);
    LimitingExtractorDecorator<String, String> decorator = new LimitingExtractorDecorator<>(extractor, limiter, taskState);
    try {
        Method method = LimitingExtractorDecorator.class.getDeclaredMethod("getLimiterStopMetadata");
        method.setAccessible(true);
        ImmutableMap<String, String> metaData = (ImmutableMap<String, String>) method.invoke(decorator);
        Assert.assertEquals(metaData.containsKey(key1), true);
        Assert.assertEquals(metaData.containsKey(key2), true);
        Assert.assertEquals(metaData.containsKey(subKey1), true);
        Assert.assertEquals(metaData.containsKey(subKey2), true);
        Assert.assertEquals(metaData.containsKey(subKey3), true);
        Assert.assertEquals(metaData.containsKey(subKey4), true);
    } catch (Exception e) {
        Assert.fail();
    }
}
Also used : Extractor(org.apache.gobblin.source.extractor.Extractor) WorkUnit(org.apache.gobblin.source.workunit.WorkUnit) Method(java.lang.reflect.Method) Properties(java.util.Properties) ImmutableMap(com.google.common.collect.ImmutableMap) Limiter(org.apache.gobblin.util.limiter.Limiter) Test(org.testng.annotations.Test)

Example 4 with Limiter

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

the class StreamThrottler method doThrottleInputStream.

/**
 * Throttles an {@link InputStream} if throttling is configured.
 * @param inputStream {@link InputStream} to throttle.
 * @param sourceURI used for selecting the throttling policy.
 * @param targetURI used for selecting the throttling policy.
 */
@Builder(builderMethodName = "throttleInputStream", builderClassName = "InputStreamThrottler")
private ThrottledInputStream doThrottleInputStream(InputStream inputStream, URI sourceURI, URI targetURI) {
    Preconditions.checkNotNull(inputStream, "InputStream cannot be null.");
    Limiter limiter = new NoopLimiter();
    if (sourceURI != null && targetURI != null) {
        StreamCopierSharedLimiterKey key = new StreamCopierSharedLimiterKey(sourceURI, targetURI);
        try {
            limiter = new MultiLimiter(limiter, this.broker.getSharedResource(new SharedLimiterFactory<S>(), key));
        } catch (NotConfiguredException nce) {
            log.warn("Could not create a Limiter for key " + key, nce);
        }
    } else {
        log.info("Not throttling input stream because source or target URIs are not defined.");
    }
    Optional<MeteredInputStream> meteredStream = MeteredInputStream.findWrappedMeteredInputStream(inputStream);
    if (!meteredStream.isPresent()) {
        meteredStream = Optional.of(MeteredInputStream.builder().in(inputStream).build());
        inputStream = meteredStream.get();
    }
    return new ThrottledInputStream(inputStream, limiter, meteredStream.get());
}
Also used : NotConfiguredException(org.apache.gobblin.broker.iface.NotConfiguredException) NoopLimiter(org.apache.gobblin.util.limiter.NoopLimiter) MultiLimiter(org.apache.gobblin.util.limiter.MultiLimiter) Limiter(org.apache.gobblin.util.limiter.Limiter) MultiLimiter(org.apache.gobblin.util.limiter.MultiLimiter) NoopLimiter(org.apache.gobblin.util.limiter.NoopLimiter) Builder(lombok.Builder)

Example 5 with Limiter

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

the class ThrottledFileSystemTest method testListing.

@Test
public void testListing() throws Exception {
    FileSystem fs = Mockito.mock(FileSystem.class);
    Mockito.when(fs.listStatus(Mockito.any(Path.class))).thenAnswer(new Answer<FileStatus[]>() {

        @Override
        public FileStatus[] answer(InvocationOnMock invocation) throws Throwable {
            Path path = (Path) invocation.getArguments()[0];
            int files = Integer.parseInt(path.getName());
            FileStatus status = new FileStatus(0, false, 0, 0, 0, new Path("/"));
            FileStatus[] out = new FileStatus[files];
            for (int i = 0; i < files; i++) {
                out[i] = status;
            }
            return out;
        }
    });
    Limiter limiter = new CountBasedLimiter(5);
    ThrottledFileSystem throttledFileSystem = new ThrottledFileSystem(fs, limiter, "testService");
    Assert.assertEquals(throttledFileSystem.getServiceName(), "testService");
    // use 1 permit
    Assert.assertNotNull(throttledFileSystem.listStatus(new Path("/files/99")));
    // use 3 permits
    Assert.assertNotNull(throttledFileSystem.listStatus(new Path("/files/250")));
    try {
        // requires 2 permits
        throttledFileSystem.listStatus(new Path("/files/150"));
        Assert.fail();
    } catch (NotEnoughPermitsException expected) {
    // Expected
    }
    // requires 1 permit
    Assert.assertNotNull(throttledFileSystem.listStatus(new Path("/files/99")));
}
Also used : Path(org.apache.hadoop.fs.Path) CountBasedLimiter(org.apache.gobblin.util.limiter.CountBasedLimiter) FileStatus(org.apache.hadoop.fs.FileStatus) InvocationOnMock(org.mockito.invocation.InvocationOnMock) FileSystem(org.apache.hadoop.fs.FileSystem) NotEnoughPermitsException(org.apache.gobblin.util.limiter.NotEnoughPermitsException) Limiter(org.apache.gobblin.util.limiter.Limiter) CountBasedLimiter(org.apache.gobblin.util.limiter.CountBasedLimiter) Test(org.testng.annotations.Test)

Aggregations

Limiter (org.apache.gobblin.util.limiter.Limiter)15 Test (org.testng.annotations.Test)10 CountBasedLimiter (org.apache.gobblin.util.limiter.CountBasedLimiter)7 MultiLimiter (org.apache.gobblin.util.limiter.MultiLimiter)6 NoopLimiter (org.apache.gobblin.util.limiter.NoopLimiter)6 ResourceInstance (org.apache.gobblin.broker.ResourceInstance)4 SimpleScopeType (org.apache.gobblin.broker.SimpleScopeType)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 Method (java.lang.reflect.Method)3 Properties (java.util.Properties)3 NotConfiguredException (org.apache.gobblin.broker.iface.NotConfiguredException)3 Extractor (org.apache.gobblin.source.extractor.Extractor)3 WorkUnit (org.apache.gobblin.source.workunit.WorkUnit)3 NotEnoughPermitsException (org.apache.gobblin.util.limiter.NotEnoughPermitsException)2 RateBasedLimiter (org.apache.gobblin.util.limiter.RateBasedLimiter)2 FileStatus (org.apache.hadoop.fs.FileStatus)2 FileSystem (org.apache.hadoop.fs.FileSystem)2 Path (org.apache.hadoop.fs.Path)2 Config (com.typesafe.config.Config)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1