Search in sources :

Example 1 with NoSuchScopeException

use of org.apache.gobblin.broker.iface.NoSuchScopeException 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 2 with NoSuchScopeException

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

the class DefaultGobblinBrokerTest method testScoping.

@Test
public void testScoping() throws Exception {
    // Correct creation behavior
    Config config = ConfigFactory.empty();
    SharedResourcesBrokerImpl<GobblinScopeTypes> topBroker = SharedResourcesBrokerFactory.createDefaultTopLevelBroker(config, GobblinScopeTypes.GLOBAL.defaultScopeInstance());
    SharedResourcesBrokerImpl<GobblinScopeTypes> jobBroker = topBroker.newSubscopedBuilder(new JobScopeInstance("myJob", "job123")).build();
    Assert.assertEquals(jobBroker.getScope(GobblinScopeTypes.INSTANCE).getType(), GobblinScopeTypes.INSTANCE);
    Assert.assertEquals(jobBroker.getScope(GobblinScopeTypes.INSTANCE).getClass(), GobblinScopeInstance.class);
    Assert.assertEquals(jobBroker.getScope(GobblinScopeTypes.INSTANCE), GobblinScopeTypes.INSTANCE.defaultScopeInstance());
    Assert.assertEquals(jobBroker.getScope(GobblinScopeTypes.JOB).getType(), GobblinScopeTypes.JOB);
    Assert.assertEquals(jobBroker.getScope(GobblinScopeTypes.JOB).getClass(), JobScopeInstance.class);
    Assert.assertEquals(((JobScopeInstance) jobBroker.getScope(GobblinScopeTypes.JOB)).getJobId(), "job123");
    try {
        jobBroker.getScope(GobblinScopeTypes.TASK);
        Assert.fail();
    } catch (NoSuchScopeException nsse) {
    // should throw no scope exception
    }
}
Also used : GobblinScopeTypes(org.apache.gobblin.broker.gobblin_scopes.GobblinScopeTypes) JobScopeInstance(org.apache.gobblin.broker.gobblin_scopes.JobScopeInstance) Config(com.typesafe.config.Config) NoSuchScopeException(org.apache.gobblin.broker.iface.NoSuchScopeException) Test(org.testng.annotations.Test)

Example 3 with NoSuchScopeException

use of org.apache.gobblin.broker.iface.NoSuchScopeException 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

NoSuchScopeException (org.apache.gobblin.broker.iface.NoSuchScopeException)3 Config (com.typesafe.config.Config)2 ResourceInstance (org.apache.gobblin.broker.ResourceInstance)2 ConfigValue (com.typesafe.config.ConfigValue)1 Map (java.util.Map)1 ResourceCoordinate (org.apache.gobblin.broker.ResourceCoordinate)1 GobblinScopeTypes (org.apache.gobblin.broker.gobblin_scopes.GobblinScopeTypes)1 JobScopeInstance (org.apache.gobblin.broker.gobblin_scopes.JobScopeInstance)1 NotConfiguredException (org.apache.gobblin.broker.iface.NotConfiguredException)1 MetricContext (org.apache.gobblin.metrics.MetricContext)1 RootMetricContext (org.apache.gobblin.metrics.RootMetricContext)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 Test (org.testng.annotations.Test)1