/** * Get a scoped object from the cache. */ @SuppressWarnings(value = "unchecked") <T, K extends SharedResourceKey> SharedResourceFactoryResponse<T> getScopedFromCache( final SharedResourceFactory<T, K, S> factory, @Nonnull final K key, @Nonnull final ScopeWrapper<S> scope, final SharedResourcesBrokerImpl<S> broker) throws ExecutionException { RawJobBrokerKey fullKey = new RawJobBrokerKey(scope, factory.getName(), key); Object obj = this.sharedResourceCache.get(fullKey, new Callable<Object>() { @Override public Object call() throws Exception { return factory.createResource(broker.getScopedView(scope.getType()), broker.getConfigView(scope.getType(), key, factory.getName())); } }); return (SharedResourceFactoryResponse<T>)obj; }
<T, K extends SharedResourceKey> void put(final SharedResourceFactory<T, K, S> factory, @Nonnull final K key, @Nonnull final ScopeWrapper<S> scope, T instance) { RawJobBrokerKey fullKey = new RawJobBrokerKey(scope, factory.getName(), key); this.sharedResourceCache.put(fullKey, new ResourceInstance<>(instance)); }
/** * Generate a {@link org.apache.gobblin.broker.iface.SharedResourcesBroker} configuration key for a particular {@link SharedResourceFactory}, * {@link SharedResourceKey} and {@link ScopeType}. * * Example: * If the broker configuration contains a key-value pair with key: * generateKey(myFactory, myKey, myScopeType, "sample.key") * when requesting a resource created by myFactory, with the provided key and scope, the factory will be able to see * the key-value pair specified. * * Note: * {@link SharedResourceKey} and {@link ScopeType} may be null. In this case, the key-value pair will be visible to * the factory regardless of the key and scope requested by the user. */ @Builder public static String generateKey(@Nonnull SharedResourceFactory factory, SharedResourceKey key, ScopeType scopeType, @Nonnull String configKey) { return JOINER.join(BrokerConstants.GOBBLIN_BROKER_CONFIG_PREFIX, factory.getName(), scopeType == null ? null : scopeType.name(), key == null ? null : key.toConfigurationKey(), configKey); }
/** * Get an object for the specified factory, key, and broker at the scope selected by the factory. {@link DefaultBrokerCache} * guarantees that calling this method from brokers with the same leaf scope will return the same object. */ @SuppressWarnings(value = "unchecked") <T, K extends SharedResourceKey> T getAutoScoped(final SharedResourceFactory<T, K, S> factory, final K key, final SharedResourcesBrokerImpl<S> broker) throws ExecutionException { // figure out auto scope RawJobBrokerKey autoscopeCacheKey = new RawJobBrokerKey(broker.getWrappedSelfScope(), factory.getName(), key); ScopeWrapper<S> selectedScope = this.autoScopeCache.get(autoscopeCacheKey, new Callable<ScopeWrapper<S>>() { @Override public ScopeWrapper<S> call() throws Exception { return broker.getWrappedScope(factory.getAutoScope(broker, broker.getConfigView(null, key, factory.getName()))); } }); // get actual object return getScoped(factory, key, selectedScope, broker); }
@Override public ScopeWrapper<S> call() throws Exception { return broker.getWrappedScope(factory.getAutoScope(broker, broker.getConfigView(null, key, factory.getName()))); } });
if (!SharedResourcesBrokerUtils.isScopeTypeAncestor((ScopeType) currentScope.getType(), ((ResourceCoordinate) obj).getScope())) { throw new RuntimeException(String .format("%s returned an invalid coordinate: scope %s is not an ancestor of %s.", currentFactory.getName(), ((ResourceCoordinate) obj).getScope(), currentScope.getType())); } catch (NoSuchScopeException nsse) { throw new RuntimeException(String .format("%s returned an invalid coordinate: scope %s is not available.", factory.getName(), resourceCoordinate.getScope().name()), nsse); try { lock.lock(); RawJobBrokerKey fullKey = new RawJobBrokerKey(currentScope, currentFactory.getName(), currentKey); safeInvalidate(fullKey); obj = getScopedFromCache(currentFactory, currentKey, currentScope, broker); throw new RuntimeException(String.format("Invalid response from %s: %s.", factory.getName(), obj.getClass()));
@Override public Object call() throws Exception { return factory.createResource(broker.getScopedView(scope.getType()), broker.getConfigView(scope.getType(), key, factory.getName())); } });
<T, K extends SharedResourceKey> void put(final SharedResourceFactory<T, K, S> factory, @Nonnull final K key, @Nonnull final ScopeWrapper<S> scope, T instance) { RawJobBrokerKey fullKey = new RawJobBrokerKey(scope, factory.getName(), key); this.sharedResourceCache.put(fullKey, new ResourceInstance<>(instance)); }
/** * Get a scoped object from the cache. */ @SuppressWarnings(value = "unchecked") <T, K extends SharedResourceKey> SharedResourceFactoryResponse<T> getScopedFromCache( final SharedResourceFactory<T, K, S> factory, @Nonnull final K key, @Nonnull final ScopeWrapper<S> scope, final SharedResourcesBrokerImpl<S> broker) throws ExecutionException { RawJobBrokerKey fullKey = new RawJobBrokerKey(scope, factory.getName(), key); Object obj = this.sharedResourceCache.get(fullKey, new Callable<Object>() { @Override public Object call() throws Exception { return factory.createResource(broker.getScopedView(scope.getType()), broker.getConfigView(scope.getType(), key, factory.getName())); } }); return (SharedResourceFactoryResponse<T>)obj; }
/** * Generate a {@link org.apache.gobblin.broker.iface.SharedResourcesBroker} configuration key for a particular {@link SharedResourceFactory}, * {@link SharedResourceKey} and {@link ScopeType}. * * Example: * If the broker configuration contains a key-value pair with key: * generateKey(myFactory, myKey, myScopeType, "sample.key") * when requesting a resource created by myFactory, with the provided key and scope, the factory will be able to see * the key-value pair specified. * * Note: * {@link SharedResourceKey} and {@link ScopeType} may be null. In this case, the key-value pair will be visible to * the factory regardless of the key and scope requested by the user. */ @Builder public static String generateKey(@Nonnull SharedResourceFactory factory, SharedResourceKey key, ScopeType scopeType, @Nonnull String configKey) { return JOINER.join(BrokerConstants.GOBBLIN_BROKER_CONFIG_PREFIX, factory.getName(), scopeType == null ? null : scopeType.name(), key == null ? null : key.toConfigurationKey(), configKey); }
/** * Get an object for the specified factory, key, and broker at the scope selected by the factory. {@link DefaultBrokerCache} * guarantees that calling this method from brokers with the same leaf scope will return the same object. */ @SuppressWarnings(value = "unchecked") <T, K extends SharedResourceKey> T getAutoScoped(final SharedResourceFactory<T, K, S> factory, final K key, final SharedResourcesBrokerImpl<S> broker) throws ExecutionException { // figure out auto scope RawJobBrokerKey autoscopeCacheKey = new RawJobBrokerKey(broker.getWrappedSelfScope(), factory.getName(), key); ScopeWrapper<S> selectedScope = this.autoScopeCache.get(autoscopeCacheKey, new Callable<ScopeWrapper<S>>() { @Override public ScopeWrapper<S> call() throws Exception { return broker.getWrappedScope(factory.getAutoScope(broker, broker.getConfigView(null, key, factory.getName()))); } }); // get actual object return getScoped(factory, key, selectedScope, broker); }
@Override public ScopeWrapper<S> call() throws Exception { return broker.getWrappedScope(factory.getAutoScope(broker, broker.getConfigView(null, key, factory.getName()))); } });
if (!SharedResourcesBrokerUtils.isScopeTypeAncestor((ScopeType) currentScope.getType(), ((ResourceCoordinate) obj).getScope())) { throw new RuntimeException(String .format("%s returned an invalid coordinate: scope %s is not an ancestor of %s.", currentFactory.getName(), ((ResourceCoordinate) obj).getScope(), currentScope.getType())); } catch (NoSuchScopeException nsse) { throw new RuntimeException(String .format("%s returned an invalid coordinate: scope %s is not available.", factory.getName(), resourceCoordinate.getScope().name()), nsse); try { lock.lock(); RawJobBrokerKey fullKey = new RawJobBrokerKey(currentScope, currentFactory.getName(), currentKey); safeInvalidate(fullKey); obj = getScopedFromCache(currentFactory, currentKey, currentScope, broker); throw new RuntimeException(String.format("Invalid response from %s: %s.", factory.getName(), obj.getClass()));
@Override public Object call() throws Exception { return factory.createResource(broker.getScopedView(scope.getType()), broker.getConfigView(scope.getType(), key, factory.getName())); } });