Search in sources :

Example 1 with ResourceCoordinate

use of org.apache.gobblin.broker.ResourceCoordinate in project incubator-gobblin by apache.

the class RestliLimiterFactory method createResource.

@Override
public SharedResourceFactoryResponse<RestliServiceBasedLimiter> createResource(SharedResourcesBroker<S> broker, ScopedConfigView<S, SharedLimiterKey> config) throws NotConfiguredException {
    S scope = config.getScope();
    if (scope != scope.rootScope()) {
        return new ResourceCoordinate<>(this, config.getKey(), scope.rootScope());
    }
    String serviceIdentifier = config.getConfig().hasPath(SERVICE_IDENTIFIER_KEY) ? config.getConfig().getString(SERVICE_IDENTIFIER_KEY) : "UNKNOWN";
    String resourceLimited = config.getKey().getResourceLimitedPath();
    MetricContextKey metricContextKey = new SubTaggedMetricContextKey(RestliServiceBasedLimiter.class.getSimpleName() + "_" + resourceLimited, ImmutableMap.of("resourceLimited", resourceLimited));
    return new ResourceInstance<>(RestliServiceBasedLimiter.builder().resourceLimited(resourceLimited).serviceIdentifier(serviceIdentifier).metricContext(broker.getSharedResource(new MetricContextFactory<S>(), metricContextKey)).requestSender(broker.getSharedResource(new RedirectAwareRestClientRequestSender.Factory<S>(), new SharedRestClientKey(RESTLI_SERVICE_NAME))).build());
}
Also used : SubTaggedMetricContextKey(org.apache.gobblin.metrics.broker.SubTaggedMetricContextKey) SharedRestClientKey(org.apache.gobblin.restli.SharedRestClientKey) ResourceInstance(org.apache.gobblin.broker.ResourceInstance) MetricContextFactory(org.apache.gobblin.metrics.broker.MetricContextFactory) SharedResourceFactory(org.apache.gobblin.broker.iface.SharedResourceFactory) SubTaggedMetricContextKey(org.apache.gobblin.metrics.broker.SubTaggedMetricContextKey) MetricContextKey(org.apache.gobblin.metrics.broker.MetricContextKey) ResourceCoordinate(org.apache.gobblin.broker.ResourceCoordinate)

Example 2 with ResourceCoordinate

use of org.apache.gobblin.broker.ResourceCoordinate in project incubator-gobblin by apache.

the class SharedRestClientFactory method createResource.

@Override
public SharedResourceFactoryResponse<RestClient> createResource(SharedResourcesBroker<S> broker, ScopedConfigView<S, SharedRestClientKey> config) throws NotConfiguredException {
    try {
        SharedRestClientKey key = config.getKey();
        if (!(key instanceof UriRestClientKey)) {
            return new ResourceCoordinate<>(this, new UriRestClientKey(key.serviceName, resolveUriPrefix(config.getConfig(), key)), config.getScope());
        }
        String uriPrefix = ((UriRestClientKey) key).getUri();
        HttpClientFactory http = new HttpClientFactory(FilterChains.empty(), new NioEventLoopGroup(0, /* use default settings */
        ExecutorsUtils.newDaemonThreadFactory(Optional.<Logger>absent(), Optional.of("R2 Nio Event Loop-%d"))), true, Executors.newSingleThreadScheduledExecutor(ExecutorsUtils.newDaemonThreadFactory(Optional.<Logger>absent(), Optional.of("R2 Netty Scheduler"))), true);
        Client r2Client = new TransportClientAdapter(http.getClient(Collections.<String, String>emptyMap()));
        return new ResourceInstance<>(new RestClient(r2Client, uriPrefix));
    } catch (URISyntaxException use) {
        throw new RuntimeException("Could not create a rest client for key " + Optional.fromNullable(config.getKey().toConfigurationKey()).or("null"));
    }
}
Also used : TransportClientAdapter(com.linkedin.r2.transport.common.bridge.client.TransportClientAdapter) ResourceInstance(org.apache.gobblin.broker.ResourceInstance) RestClient(com.linkedin.restli.client.RestClient) URISyntaxException(java.net.URISyntaxException) RestClient(com.linkedin.restli.client.RestClient) Client(com.linkedin.r2.transport.common.Client) HttpClientFactory(com.linkedin.r2.transport.http.client.HttpClientFactory) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ResourceCoordinate(org.apache.gobblin.broker.ResourceCoordinate)

Example 3 with ResourceCoordinate

use of org.apache.gobblin.broker.ResourceCoordinate in project incubator-gobblin by apache.

the class SharedLimiterFactory method createResource.

@Override
public SharedResourceFactoryResponse<Limiter> createResource(SharedResourcesBroker<S> broker, ScopedConfigView<S, SharedLimiterKey> configView) throws NotConfiguredException {
    Config config = configView.getConfig();
    SharedLimiterKey.GlobalLimiterPolicy globalLimiterPolicy = configView.getKey().getGlobalLimiterPolicy();
    if (ConfigUtils.getBoolean(config, SKIP_GLOBAL_LIMITER_KEY, false)) {
        if (globalLimiterPolicy != SharedLimiterKey.GlobalLimiterPolicy.LOCAL_ONLY) {
            SharedLimiterKey modifiedKey = new SharedLimiterKey(configView.getKey().getResourceLimitedPath(), SharedLimiterKey.GlobalLimiterPolicy.LOCAL_ONLY);
            return new ResourceCoordinate<>(this, modifiedKey, (S) configView.getScope());
        }
    } else if (config.hasPath(FAIL_IF_NO_GLOBAL_LIMITER_KEY) && config.getBoolean(FAIL_IF_NO_GLOBAL_LIMITER_KEY) && globalLimiterPolicy != SharedLimiterKey.GlobalLimiterPolicy.USE_GLOBAL) {
        // if user has specified FAIL_IF_NO_GLOBAL_LIMITER_KEY, promote the policy from USE_GLOBAL_IF_CONFIGURED to USE_GLOBAL
        // e.g. fail if no GLOBAL configuration is present
        SharedLimiterKey modifiedKey = new SharedLimiterKey(configView.getKey().getResourceLimitedPath(), SharedLimiterKey.GlobalLimiterPolicy.USE_GLOBAL);
        return new ResourceCoordinate<>(this, modifiedKey, (S) configView.getScope());
    }
    Limiter limiter;
    if (!configView.getScope().isLocal() && !globalLimiterPolicy.equals(SharedLimiterKey.GlobalLimiterPolicy.LOCAL_ONLY)) {
        try {
            Class<?> klazz = Class.forName("org.apache.gobblin.util.limiter.RestliLimiterFactory");
            return new ResourceCoordinate<>((SharedResourceFactory<Limiter, SharedLimiterKey, S>) klazz.newInstance(), configView.getKey(), (S) configView.getScope());
        } catch (ReflectiveOperationException roe) {
            if (globalLimiterPolicy.equals(SharedLimiterKey.GlobalLimiterPolicy.USE_GLOBAL)) {
                throw new RuntimeException("There is no Global limiter factory in the classpath.");
            }
        }
    }
    if (config.hasPath(LIMITER_CLASS_KEY)) {
        try {
            LimiterFactory factory = RESOLVER.resolveClass(config.getString(LIMITER_CLASS_KEY)).newInstance();
            limiter = factory.buildLimiter(config);
        } catch (ReflectiveOperationException roe) {
            throw new RuntimeException(roe);
        }
    } else {
        if (config.hasPath(FAIL_ON_UNKNOWN_RESOURCE_ID) && config.getBoolean(FAIL_ON_UNKNOWN_RESOURCE_ID)) {
            throw new NotConfiguredException();
        }
        limiter = new NoopLimiter();
    }
    ScopeType<S> scope = configView.getScope();
    Collection<S> parentScopes = scope.parentScopes();
    if (parentScopes != null) {
        try {
            for (S parentScope : parentScopes) {
                limiter = new MultiLimiter(limiter, broker.getSharedResourceAtScope(this, configView.getKey(), parentScope));
            }
        } catch (NoSuchScopeException nsse) {
            throw new RuntimeException("Could not get higher scope limiter. This is an error in code.", nsse);
        }
    }
    return new ResourceInstance<>(limiter);
}
Also used : NotConfiguredException(org.apache.gobblin.broker.iface.NotConfiguredException) Config(com.typesafe.config.Config) NoopLimiter(org.apache.gobblin.util.limiter.NoopLimiter) MultiLimiter(org.apache.gobblin.util.limiter.MultiLimiter) NoSuchScopeException(org.apache.gobblin.broker.iface.NoSuchScopeException) LimiterFactory(org.apache.gobblin.util.limiter.LimiterFactory) ResourceInstance(org.apache.gobblin.broker.ResourceInstance) ResourceCoordinate(org.apache.gobblin.broker.ResourceCoordinate) Limiter(org.apache.gobblin.util.limiter.Limiter) MultiLimiter(org.apache.gobblin.util.limiter.MultiLimiter) NoopLimiter(org.apache.gobblin.util.limiter.NoopLimiter)

Aggregations

ResourceCoordinate (org.apache.gobblin.broker.ResourceCoordinate)3 ResourceInstance (org.apache.gobblin.broker.ResourceInstance)3 Client (com.linkedin.r2.transport.common.Client)1 TransportClientAdapter (com.linkedin.r2.transport.common.bridge.client.TransportClientAdapter)1 HttpClientFactory (com.linkedin.r2.transport.http.client.HttpClientFactory)1 RestClient (com.linkedin.restli.client.RestClient)1 Config (com.typesafe.config.Config)1 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)1 URISyntaxException (java.net.URISyntaxException)1 NoSuchScopeException (org.apache.gobblin.broker.iface.NoSuchScopeException)1 NotConfiguredException (org.apache.gobblin.broker.iface.NotConfiguredException)1 SharedResourceFactory (org.apache.gobblin.broker.iface.SharedResourceFactory)1 MetricContextFactory (org.apache.gobblin.metrics.broker.MetricContextFactory)1 MetricContextKey (org.apache.gobblin.metrics.broker.MetricContextKey)1 SubTaggedMetricContextKey (org.apache.gobblin.metrics.broker.SubTaggedMetricContextKey)1 SharedRestClientKey (org.apache.gobblin.restli.SharedRestClientKey)1 Limiter (org.apache.gobblin.util.limiter.Limiter)1 LimiterFactory (org.apache.gobblin.util.limiter.LimiterFactory)1 MultiLimiter (org.apache.gobblin.util.limiter.MultiLimiter)1 NoopLimiter (org.apache.gobblin.util.limiter.NoopLimiter)1