private static synchronized SharedResourcesBroker<SimpleScopeType> getSingleton() { if (SINGLETON == null) { SINGLETON = createDefaultTopLevelBroker(ConfigFactory.empty(), SimpleScopeType.GLOBAL.defaultScopeInstance()); } return SINGLETON; }
@Override protected void setup(Context context) throws IOException, InterruptedException { Map<String, String> configMap = Maps.newHashMap(); SharedResourcesBrokerFactory.addBrokerKeys(configMap, context.getConfiguration()); this.broker = SharedResourcesBrokerFactory.createDefaultTopLevelBroker(ConfigFactory.parseMap(configMap), SimpleScopeType.GLOBAL.defaultScopeInstance()); super.setup(context); }
private static SharedResourcesBroker createMockBroker() { Joiner JOINER = Joiner.on("."); Config config = ConfigFactory.parseMap(ImmutableMap.of( JOINER.join(BrokerConstants.GOBBLIN_BROKER_CONFIG_PREFIX, SharedLimiterFactory.NAME, SharedLimiterFactory.LIMITER_CLASS_KEY), "qps", JOINER.join(BrokerConstants.GOBBLIN_BROKER_CONFIG_PREFIX, SharedLimiterFactory.NAME, RateBasedLimiter.Factory.QPS_KEY), "10" )); SharedResourcesBrokerImpl broker = SharedResourcesBrokerFactory.<SimpleScopeType>createDefaultTopLevelBroker(config, SimpleScopeType.GLOBAL.defaultScopeInstance()); return broker; }
@Test public void testLoadingOfClasspath() { Config config = ConfigFactory.parseMap(ImmutableMap.of(SharedResourcesBrokerFactory.BROKER_CONF_FILE_KEY, "/broker/testBroker.conf")); SharedResourcesBrokerImpl<SimpleScopeType> broker = SharedResourcesBrokerFactory.createDefaultTopLevelBroker(config, SimpleScopeType.GLOBAL.defaultScopeInstance()); ConfigView configView = broker.getConfigView(null, null, "factory"); Assert.assertTrue(configView.getConfig().hasPath("testKey")); Assert.assertEquals(configView.getConfig().getString("testKey"), "testValue"); }
@Test public void testCreationWithConfigurationFSImpl() throws Exception { SharedResourcesBrokerImpl<SimpleScopeType> broker = SharedResourcesBrokerFactory.<SimpleScopeType>createDefaultTopLevelBroker( ConfigFactory.empty(), SimpleScopeType.GLOBAL.defaultScopeInstance()); Configuration conf = new Configuration(); conf.set("fs.local.impl", InstrumentedLocalFileSystem.class.getName()); FileSystemKey key = new FileSystemKey(new URI("file:///"), new Configuration()); FileSystemFactory<SimpleScopeType> factory = new FileSystemFactory<>(); FileSystem fs = broker.getSharedResource(factory, key); verifyInstrumentedOnce(fs); Assert.assertTrue(DecoratorUtils.resolveUnderlyingObject(fs) instanceof LocalFileSystem); }
private SharedResourcesBrokerImpl<SimpleScopeType> getBrokerForConfigMap(Map<String, String> configMap) { Config config = ConfigFactory.parseMap(configMap); return SharedResourcesBrokerFactory.<SimpleScopeType>createDefaultTopLevelBroker(config, SimpleScopeType.GLOBAL.defaultScopeInstance()); }
@Test() public void testMultiThreadedGetNonThreadSafePublisher() throws InterruptedException, ExecutionException, IOException { SharedResourcesBroker broker = SharedResourcesBrokerFactory.<SimpleScopeType>createDefaultTopLevelBroker(ConfigFactory.empty(), SimpleScopeType.GLOBAL.defaultScopeInstance()); ExecutorService service = Executors.newFixedThreadPool(40); List<Future<?>> futures = new ArrayList<>(); for (int i = 0; i < 100000; i++) { futures.add(service.submit(new GetNonThreadSafePublisher(broker))); } for (Future f: futures) { f.get(); } service.shutdown(); service.awaitTermination(100, TimeUnit.SECONDS); }
@Test public void test() throws Exception { SharedResourcesBrokerImpl<SimpleScopeType> broker = SharedResourcesBrokerFactory.<SimpleScopeType>createDefaultTopLevelBroker( ConfigFactory.empty(), SimpleScopeType.GLOBAL.defaultScopeInstance()); FileSystemKey key = new FileSystemKey(new URI("file:///"), new Configuration()); FileSystemFactory<SimpleScopeType> factory = new FileSystemFactory<>(); FileSystem fs = broker.getSharedResource(factory, key); verifyInstrumentedOnce(fs); SharedResourcesBroker<SimpleScopeType> subBroker = broker.newSubscopedBuilder(SimpleScopeType.LOCAL.defaultScopeInstance()).build(); FileSystem subBrokerFs = FileSystemFactory.get(new URI("file:///"), new Configuration(), subBroker); Assert.assertEquals(fs, subBrokerFs); }
@Test public void testGetNonThreadSafePublisher() throws IOException { SharedResourcesBroker broker = SharedResourcesBrokerFactory.<SimpleScopeType>createDefaultTopLevelBroker(ConfigFactory.empty(), SimpleScopeType.GLOBAL.defaultScopeInstance()); DataPublisher publisher1 = DataPublisherFactory.get(TestNonThreadsafeDataPublisher.class.getName(), null, broker); DataPublisher publisher2 = DataPublisherFactory.get(TestNonThreadsafeDataPublisher.class.getName(), null, broker); // should get different publishers Assert.assertNotEquals(publisher1, publisher2); // Check capabilities Assert.assertTrue(publisher1.supportsCapability(DataPublisher.REUSABLE, Collections.EMPTY_MAP)); Assert.assertFalse(publisher1.supportsCapability(Capability.THREADSAFE, Collections.EMPTY_MAP)); }
@Test public void testCreationWithInstrumentedScheme() throws Exception { SharedResourcesBrokerImpl<SimpleScopeType> broker = SharedResourcesBrokerFactory.<SimpleScopeType>createDefaultTopLevelBroker( ConfigFactory.empty(), SimpleScopeType.GLOBAL.defaultScopeInstance()); FileSystemKey key = new FileSystemKey(new URI("instrumented-file:///"), new Configuration()); FileSystemFactory<SimpleScopeType> factory = new FileSystemFactory<>(); FileSystem fs = broker.getSharedResource(factory, key); verifyInstrumentedOnce(fs); Assert.assertTrue(DecoratorUtils.resolveUnderlyingObject(fs) instanceof LocalFileSystem); }
@Override public void run() { Assert.assertEquals(SharedResourcesBrokerFactory.getImplicitBroker(), IMPLICIT); SharedResourcesBroker<SimpleScopeType> broker = SharedResourcesBrokerFactory.createDefaultTopLevelBroker( ConfigFactory.empty(), SimpleScopeType.GLOBAL.defaultScopeInstance()); Assert.assertNotEquals(SharedResourcesBrokerFactory.getImplicitBroker(), broker); SharedResourcesBrokerFactory.registerImplicitBroker(broker); Assert.assertEquals(SharedResourcesBrokerFactory.getImplicitBroker(), broker); ExecutorService executorService = Executors.newSingleThreadExecutor(); Future<?> future = executorService.submit(new InnerImplicitBrokerTest(broker)); try { future.get(); } catch (ExecutionException | InterruptedException ee) { throw new RuntimeException(ee); } executorService.shutdownNow(); } }
@Test public void test() throws Exception { MetricContextFactory<SimpleScopeType> factory = new MetricContextFactory<>(); Config config = ConfigFactory.parseMap(ImmutableMap.of( BrokerConfigurationKeyGenerator.generateKey(factory, null, null, MetricContextFactory.TAG_KEY + ".tag1"), "value1", BrokerConfigurationKeyGenerator.generateKey(factory, null, SimpleScopeType.GLOBAL, MetricContextFactory.TAG_KEY + ".tag2"), "value2", BrokerConfigurationKeyGenerator.generateKey(factory, null, SimpleScopeType.LOCAL, MetricContextFactory.TAG_KEY + ".tag3"), "value3" )); SharedResourcesBroker<SimpleScopeType> rootBroker = SharedResourcesBrokerFactory.createDefaultTopLevelBroker(config, SimpleScopeType.GLOBAL.defaultScopeInstance()); SharedResourcesBroker<SimpleScopeType> localBroker = rootBroker.newSubscopedBuilder(SimpleScopeType.LOCAL.defaultScopeInstance()).build(); MetricContext localContext = localBroker.getSharedResource(factory, new MetricContextKey()); Map<String, String> tagMap = (Map<String, String>) Tag.toMap(Tag.tagValuesToString(localContext.getTags())); Assert.assertEquals(tagMap.get("tag1"), "value1"); Assert.assertEquals(tagMap.get("tag2"), "value2"); Assert.assertEquals(tagMap.get("tag3"), "value3"); MetricContext globalContext = rootBroker.getSharedResource(factory, new MetricContextKey()); Assert.assertEquals(localContext.getParent().get(), globalContext); tagMap = (Map<String, String>) Tag.toMap(Tag.tagValuesToString(globalContext.getTags())); Assert.assertEquals(tagMap.get("tag1"), "value1"); Assert.assertEquals(tagMap.get("tag2"), "value2"); Assert.assertFalse(tagMap.containsKey("tag3")); }
@Test public void testSubTaggedMetricContext() throws Exception { MetricContextFactory<SimpleScopeType> factory = new MetricContextFactory<>(); Config config = ConfigFactory.parseMap(ImmutableMap.of( BrokerConfigurationKeyGenerator.generateKey(factory, null, null, MetricContextFactory.TAG_KEY + ".tag1"), "value1" )); SharedResourcesBroker<SimpleScopeType> rootBroker = SharedResourcesBrokerFactory.createDefaultTopLevelBroker(config, SimpleScopeType.GLOBAL.defaultScopeInstance()); MetricContext metricContext = rootBroker.getSharedResource(factory, new SubTaggedMetricContextKey("myMetricContext", ImmutableMap.of("tag2", "value2"))); Map<String, String> tagMap = (Map<String, String>) Tag.toMap(Tag.tagValuesToString(metricContext.getTags())); Assert.assertEquals(metricContext.getName(), "myMetricContext"); Assert.assertEquals(tagMap.get("tag1"), "value1"); Assert.assertEquals(tagMap.get("tag2"), "value2"); MetricContext metricContext2 = rootBroker.getSharedResource(factory, new SubTaggedMetricContextKey("myMetricContext", ImmutableMap.of("tag2", "value2"))); Assert.assertEquals(metricContext, metricContext2); MetricContext metricContext3 = rootBroker.getSharedResource(factory, new SubTaggedMetricContextKey("myMetricContext", ImmutableMap.of("tag3", "value3"))); Assert.assertNotEquals(metricContext, metricContext3); MetricContext parent = rootBroker.getSharedResource(factory, new MetricContextKey()); tagMap = (Map<String, String>) Tag.toMap(Tag.tagValuesToString(parent.getTags())); Assert.assertEquals(metricContext.getParent().get(), parent); Assert.assertEquals(tagMap.get("tag1"), "value1"); Assert.assertFalse(tagMap.containsKey("tag2")); }
@Test public void testExplicitBinding() throws Exception { Config config = ConfigFactory.empty(); SharedResourcesBrokerImpl<SimpleScopeType> topBroker = SharedResourcesBrokerFactory.createDefaultTopLevelBroker(config, SimpleScopeType.GLOBAL.defaultScopeInstance()); SharedResourceFactory<Long, EmptyKey, SimpleScopeType> factory = new SharedResourceFactory<Long, EmptyKey, SimpleScopeType>() { @Override public String getName() { return "myTestFactory"; } @Override public SharedResourceFactoryResponse<Long> createResource(SharedResourcesBroker<SimpleScopeType> broker, ScopedConfigView<SimpleScopeType, EmptyKey> config) throws NotConfiguredException { throw new UnsupportedOperationException(); } @Override public SimpleScopeType getAutoScope(SharedResourcesBroker<SimpleScopeType> broker, ConfigView<SimpleScopeType, EmptyKey> config) { return broker.selfScope().getType(); } }; topBroker.bindSharedResourceAtScope(factory, new EmptyKey(), SimpleScopeType.GLOBAL, 10l); Assert.assertEquals(topBroker.getSharedResource(factory, new EmptyKey()), new Long(10)); }
@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)); }
SharedResourcesBroker<SimpleScopeType> broker = SharedResourcesBrokerFactory.<SimpleScopeType>createDefaultTopLevelBroker(ConfigFactory.empty(), SimpleScopeType.GLOBAL.defaultScopeInstance());
@Test public void testSkipGlobalLimiterOnLimiterFactory() throws Exception { Map<String, String> configMap = ImmutableMap.of( BrokerConfigurationKeyGenerator.generateKey(new SharedLimiterFactory(), null, null, SharedLimiterFactory.SKIP_GLOBAL_LIMITER_KEY), "true" ); SharedResourcesBroker<SimpleScopeType> broker = SharedResourcesBrokerFactory.createDefaultTopLevelBroker( ConfigFactory.parseMap(configMap), SimpleScopeType.GLOBAL.defaultScopeInstance()); MyRequestSender requestSender = new MyRequestSender(); broker.bindSharedResourceAtScope(new RedirectAwareRestClientRequestSender.Factory<>(), new SharedRestClientKey(RestliLimiterFactory.RESTLI_SERVICE_NAME), SimpleScopeType.GLOBAL, requestSender); Limiter limiter = broker.getSharedResource(new SharedLimiterFactory<>(), new SharedLimiterKey("my/resource")); Assert.assertNotNull(limiter.acquirePermits(10)); Assert.assertEquals(requestSender.requestList.size(), 0); broker.close(); }
@Test public void testFactory() throws Exception { SharedResourcesBroker<SimpleScopeType> broker = SharedResourcesBrokerFactory.createDefaultTopLevelBroker( ConfigFactory.empty(), SimpleScopeType.GLOBAL.defaultScopeInstance()); MyRequestSender requestSender = new MyRequestSender(); broker.bindSharedResourceAtScope(new RedirectAwareRestClientRequestSender.Factory<>(), new SharedRestClientKey(RestliLimiterFactory.RESTLI_SERVICE_NAME), SimpleScopeType.GLOBAL, requestSender); RestliServiceBasedLimiter limiter = broker.getSharedResource(new RestliLimiterFactory<>(), new SharedLimiterKey("my/resource")); Assert.assertNotNull(limiter.acquirePermits(10)); Assert.assertEquals(requestSender.requestList.size(), 1); broker.close(); }
@Test public void testRestliLimiterCalledByLimiterFactory() throws Exception { SharedResourcesBroker<SimpleScopeType> broker = SharedResourcesBrokerFactory.createDefaultTopLevelBroker( ConfigFactory.empty(), SimpleScopeType.GLOBAL.defaultScopeInstance()); MyRequestSender requestSender = new MyRequestSender(); broker.bindSharedResourceAtScope(new RedirectAwareRestClientRequestSender.Factory<>(), new SharedRestClientKey(RestliLimiterFactory.RESTLI_SERVICE_NAME), SimpleScopeType.GLOBAL, requestSender); Limiter limiter = broker.getSharedResource(new SharedLimiterFactory<>(), new SharedLimiterKey("my/resource")); Assert.assertNotNull(limiter.acquirePermits(10)); Assert.assertEquals(requestSender.requestList.size(), 1); broker.close(); }
ConfigFactory.empty(), SimpleScopeType.GLOBAL.defaultScopeInstance());