Search in sources :

Example 1 with ResourceInstance

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

the class MysqlDataSourceFactory method createResource.

@Override
public SharedResourceFactoryResponse<BasicDataSource> createResource(SharedResourcesBroker<S> broker, ScopedConfigView<S, MysqlDataSourceKey> config) throws NotConfiguredException {
    MysqlDataSourceKey key = config.getKey();
    Config configuration = key.getConfig();
    BasicDataSource dataSource = MysqlStateStore.newDataSource(configuration);
    return new ResourceInstance<>(dataSource);
}
Also used : Config(com.typesafe.config.Config) ResourceInstance(org.apache.gobblin.broker.ResourceInstance) BasicDataSource(org.apache.commons.dbcp.BasicDataSource)

Example 2 with ResourceInstance

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

the class MetricContextFactory method createResource.

@Override
public SharedResourceFactoryResponse<MetricContext> createResource(SharedResourcesBroker<S> broker, ScopedConfigView<S, MetricContextKey> config) throws NotConfiguredException {
    try {
        if (config.getKey() instanceof SubTaggedMetricContextKey) {
            SubTaggedMetricContextKey key = (SubTaggedMetricContextKey) config.getKey();
            MetricContext parent = broker.getSharedResource(this, new MetricContextKey());
            MetricContext.Builder builder = parent.childBuilder(key.getMetricContextName());
            for (Map.Entry<String, String> entry : key.getTags().entrySet()) {
                builder.addTag(new Tag<>(entry.getKey(), entry.getValue()));
            }
            return new ResourceInstance<>(builder.build());
        }
        MetricContext parentMetricContext = RootMetricContext.get();
        Collection<S> parents = config.getScope().parentScopes();
        if (parents != null && !parents.isEmpty()) {
            S parentScope = parents.iterator().next();
            parentMetricContext = broker.getSharedResourceAtScope(this, config.getKey(), parentScope);
        }
        // If this is the root scope, append a UUID to the name. This allows having a separate root context per broker.
        String metricContextName = parents == null ? config.getScope().name() + "_" + UUID.randomUUID().toString() : broker.selfScope().getScopeId();
        MetricContext.Builder builder = parentMetricContext.childBuilder(metricContextName);
        builder.addTag(new Tag<>(config.getScope().name(), broker.getScope(config.getScope()).getScopeId()));
        for (Map.Entry<String, ConfigValue> entry : ConfigUtils.getConfigOrEmpty(config.getConfig(), TAG_KEY).entrySet()) {
            builder.addTag(new Tag<>(entry.getKey(), entry.getValue().unwrapped()));
        }
        return new ResourceInstance<>(builder.build());
    } catch (NoSuchScopeException nsse) {
        throw new RuntimeException("Could not create MetricContext.", nsse);
    }
}
Also used : ConfigValue(com.typesafe.config.ConfigValue) NoSuchScopeException(org.apache.gobblin.broker.iface.NoSuchScopeException) MetricContext(org.apache.gobblin.metrics.MetricContext) RootMetricContext(org.apache.gobblin.metrics.RootMetricContext) ResourceInstance(org.apache.gobblin.broker.ResourceInstance) Map(java.util.Map)

Example 3 with ResourceInstance

use of org.apache.gobblin.broker.ResourceInstance 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 4 with ResourceInstance

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

the class DataPublisherFactory method createResource.

@Override
public SharedResourceFactoryResponse<DataPublisher> createResource(SharedResourcesBroker<S> broker, ScopedConfigView<S, DataPublisherKey> config) throws NotConfiguredException {
    try {
        DataPublisherKey key = config.getKey();
        String publisherClassName = key.getPublisherClassName();
        State state = key.getState();
        Class<? extends DataPublisher> dataPublisherClass = (Class<? extends DataPublisher>) Class.forName(publisherClassName);
        log.info("Creating data publisher with class {} in scope {}. ", publisherClassName, config.getScope().toString());
        DataPublisher publisher = DataPublisher.getInstance(dataPublisherClass, state);
        // once from the broker.
        if (isPublisherCacheable(publisher)) {
            return new ResourceInstance<>(publisher);
        } else {
            return new ImmediatelyInvalidResourceEntry<>(publisher);
        }
    } catch (ReflectiveOperationException e) {
        throw new RuntimeException(e);
    }
}
Also used : ImmediatelyInvalidResourceEntry(org.apache.gobblin.broker.ImmediatelyInvalidResourceEntry) State(org.apache.gobblin.configuration.State) ResourceInstance(org.apache.gobblin.broker.ResourceInstance)

Example 5 with ResourceInstance

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

the class FileSystemFactory method createResource.

@Override
public SharedResourceFactoryResponse<FileSystem> createResource(SharedResourcesBroker<S> broker, ScopedConfigView<S, FileSystemKey> config) throws NotConfiguredException {
    try {
        FileSystemKey key = config.getKey();
        URI uri = key.getUri();
        Configuration hadoopConf = key.getConfiguration();
        log.info("Creating instrumented FileSystem for uri " + uri);
        Class<? extends FileSystem> fsClass = FileSystem.getFileSystemClass(uri.getScheme(), hadoopConf);
        if (InstrumentedFileSystem.class.isAssignableFrom(fsClass)) {
            InstrumentedFileSystem tmpfs = (InstrumentedFileSystem) fsClass.newInstance();
            hadoopConf = new Configuration(hadoopConf);
            String schemeKey = "fs." + uri.getScheme() + ".impl";
            hadoopConf.set(schemeKey, tmpfs.underlyingFs.getClass().getName());
        }
        FileSystem fs = FileSystem.newInstance(uri, hadoopConf);
        ServiceLoader<FileSystemInstrumentationFactory> loader = ServiceLoader.load(FileSystemInstrumentationFactory.class);
        for (FileSystemInstrumentationFactory instrumentationFactory : loader) {
            fs = instrumentationFactory.instrumentFileSystem(fs, broker, config);
        }
        return new ResourceInstance<>(fs);
    } catch (IOException | ReflectiveOperationException ioe) {
        throw new RuntimeException(ioe);
    }
}
Also used : Configuration(org.apache.hadoop.conf.Configuration) IOException(java.io.IOException) URI(java.net.URI) FileSystem(org.apache.hadoop.fs.FileSystem) ResourceInstance(org.apache.gobblin.broker.ResourceInstance)

Aggregations

ResourceInstance (org.apache.gobblin.broker.ResourceInstance)10 Limiter (org.apache.gobblin.util.limiter.Limiter)4 MultiLimiter (org.apache.gobblin.util.limiter.MultiLimiter)4 NoopLimiter (org.apache.gobblin.util.limiter.NoopLimiter)4 ResourceCoordinate (org.apache.gobblin.broker.ResourceCoordinate)3 SimpleScopeType (org.apache.gobblin.broker.SimpleScopeType)3 CountBasedLimiter (org.apache.gobblin.util.limiter.CountBasedLimiter)3 Test (org.testng.annotations.Test)3 Config (com.typesafe.config.Config)2 NoSuchScopeException (org.apache.gobblin.broker.iface.NoSuchScopeException)2 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 ConfigValue (com.typesafe.config.ConfigValue)1 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)1 IOException (java.io.IOException)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 Map (java.util.Map)1