@Override public <T, K extends SharedResourceKey> void bindSharedResourceAtScope(SharedResourceFactory<T, K, S> factory, K key, S scopeType, T instance) throws NoSuchScopeException { this.brokerCache.put(factory, key, getWrappedScope(scopeType), instance); }
@Override public void onInvalidate() { if (this.closeOnInvalidation) { SharedResourcesBrokerUtils.shutdownObject(getResource(), log); } } }
@Override public ScopeInstance<S> getScope(S scopeType) throws NoSuchScopeException { return getWrappedScope(scopeType).getScope(); }
public SharedResourcesBroker<GobblinScopeTypes> getDefaultInstanceBroker() { SharedResourcesBrokerImpl<GobblinScopeTypes> globalBroker = SharedResourcesBrokerFactory.createDefaultTopLevelBroker(getSysConfig().getConfig(), GobblinScopeTypes.GLOBAL.defaultScopeInstance()); return globalBroker.newSubscopedBuilder(new SimpleScope<>(GobblinScopeTypes.INSTANCE, getInstanceName())).build(); }
@Override public SharedResourceFactoryResponse<SharedResource> createResource(SharedResourcesBroker<S> broker, ScopedConfigView<S, TestResourceKey> config) { return new ResourceInstance<>(new SharedResource(config.getKey().getKey(), config.getConfig())); }
@Override public SharedResourceFactoryResponse<SharedResource> createResource(SharedResourcesBroker broker, ScopedConfigView config) { return new ResourceCoordinate<>(new TestFactory<S>(), (TestResourceKey) config.getKey(), (S) config.getScope().rootScope()); } }
@Test public void testExpiringResource() throws Exception { Config config = ConfigFactory.empty(); SharedResourcesBrokerImpl<SimpleScopeType> topBroker = SharedResourcesBrokerFactory.createDefaultTopLevelBroker(config, SimpleScopeType.GLOBAL.defaultScopeInstance()); InvalidatableResourceFactory factory = new InvalidatableResourceFactory(); long value = topBroker.getSharedResource(factory, new EmptyKey()); Assert.assertEquals(topBroker.getSharedResource(factory, new EmptyKey()), new Long(value)); factory.getLastResourceEntry().setValid(false); Assert.assertNotEquals(topBroker.getSharedResource(factory, new EmptyKey()), value); value = topBroker.getSharedResource(factory, new EmptyKey()); Assert.assertEquals(topBroker.getSharedResource(factory, new EmptyKey()), new Long(value)); }
private static synchronized SharedResourcesBroker<SimpleScopeType> getSingleton() { if (SINGLETON == null) { SINGLETON = createDefaultTopLevelBroker(ConfigFactory.empty(), SimpleScopeType.GLOBAL.defaultScopeInstance()); } return SINGLETON; }
@Override public Object call() throws Exception { return factory.createResource(broker.getScopedView(scope.getType()), broker.getConfigView(scope.getType(), key, factory.getName())); } });
NonExtendableBrokerView<S> getScopedView(final S scope) throws NoSuchScopeException { return new NonExtendableBrokerView<>(this.brokerCache, getWrappedScope(scope), this.scopedConfigs, Maps.filterKeys(this.ancestorScopesByType, new Predicate<S>() { @Override public boolean apply(@Nullable S input) { return SharedResourcesBrokerUtils.isScopeTypeAncestor(scope, input); } })); }
@Override public <T, K extends SharedResourceKey> T getSharedResourceAtScope(SharedResourceFactory<T, K, S> factory, K key, S scope) throws NotConfiguredException, NoSuchScopeException { try { return this.brokerCache.getScoped(factory, key, getWrappedScope(scope), this); } catch (ExecutionException ee) { throw new RuntimeException(ee); } }
@Override public synchronized T getResource() { // mark the object as invalid before returning so that a new one will be created on the next // request from the factory this.valid = false; return super.getResource(); }
@Override public ScopedConfigView<S, K> getScopedView(S scopeType) { return new KeyedScopedConfigViewImpl<>(scopeType, this.key, this.factoryName, this.fullConfig); }
/** * Get a builder to create a descendant {@link SharedResourcesBrokerImpl} (i.e. its leaf scope is a descendant of this * broker's leaf scope) and the same backing {@link DefaultBrokerCache}. * * @param subscope the {@link ScopeInstance} of the new {@link SharedResourcesBroker}. * @return a {@link SubscopedBrokerBuilder}. */ @Override public SubscopedBrokerBuilder newSubscopedBuilder(ScopeInstance<S> subscope) { return new SubscopedBrokerBuilder(subscope); }
@Override public ScopeInstance<S> selfScope() { return this.selfScopeWrapper.getScope(); }
/** * This method returns the resource, but may have logic before the return. * @return the resource */ @Override public T getResource() { return getTheResource(); }
/** * This method is synchronized so that the validity check and validity change is atomic for callers of this method. * @return */ @Override public synchronized T getResourceIfValid() { if (this.valid) { return getResource(); } else { return null; } } }
public SharedResourcesBroker<GobblinScopeTypes> getDefaultInstanceBroker() { SharedResourcesBrokerImpl<GobblinScopeTypes> globalBroker = SharedResourcesBrokerFactory.createDefaultTopLevelBroker(getSysConfig().getConfig(), GobblinScopeTypes.GLOBAL.defaultScopeInstance()); return globalBroker.newSubscopedBuilder(new SimpleScope<>(GobblinScopeTypes.INSTANCE, getInstanceName())).build(); }
private SharedResourcesBrokerImpl<SimpleScopeType> getBrokerForConfigMap(Map<String, String> configMap) { Config config = ConfigFactory.parseMap(configMap); return SharedResourcesBrokerFactory.<SimpleScopeType>createDefaultTopLevelBroker(config, SimpleScopeType.GLOBAL.defaultScopeInstance()); }
public SharedResourcesBroker<GobblinScopeTypes> getDefaultInstanceBroker() { getLog().warn("Creating a default instance broker for job launcher. Objects may not be shared across all jobs in this instance."); SharedResourcesBrokerImpl<GobblinScopeTypes> globalBroker = SharedResourcesBrokerFactory.createDefaultTopLevelBroker(getSysConfig().getConfig(), GobblinScopeTypes.GLOBAL.defaultScopeInstance()); return globalBroker.newSubscopedBuilder(new SimpleScope<>(GobblinScopeTypes.INSTANCE, getInstanceName())).build(); }