/** * Adds a {@link HostDiscoverySource} instance to the builder. Multiple instances of {@code HostDiscoverySource} * may be specified. The service pool will query the sources in the order they were registered and use the first * non-null {@link HostDiscovery} returned for the service name provided by the * {@link ServiceFactory#getServiceName()} method of the factory configured by {@link #withServiceFactory}. * <p> * Note that using this method will cause the ServicePoolBuilder to call * {@link HostDiscoverySource#forService(String serviceName)} when {@link #build()} is called and pass the returned * {@link HostDiscovery} to the new {@code ServicePool}. Subsequently calling {@link ServicePool#close()} will in * turn call {@link HostDiscovery#close()} on the passed instance. * * @param hostDiscoverySource a host discovery source to use to find the {@link HostDiscovery} when constructing * the {@link ServicePool} * @return this */ public ServicePoolBuilder<S> withHostDiscoverySource(HostDiscoverySource hostDiscoverySource) { checkNotNull(hostDiscoverySource); return withHostDiscovery(hostDiscoverySource, true); }
@Override public void configure(ServicePoolBuilder<QueueService> servicePoolBuilder) { servicePoolBuilder.withPartitionFilter(new ConsistentHashPartitionFilter()) .withPartitionContextAnnotationsFrom(QueueServiceAuthenticatorProxy.class); }
/** * Builds a dynamic proxy that wraps a {@code ServicePool} and implements the service interface directly. This is * appropriate for stateless services where it's sensible for the same retry policy to apply to every method. * <p/> * It is the caller's responsibility to shutdown the service pool when they're done with it by casting the proxy * to {@link java.io.Closeable} and calling the {@link java.io.Closeable#close()} method. * * @param retryPolicy The retry policy to apply for every service call. * @return The dynamic proxy instance that implements the service interface {@code S} and the * {@link java.io.Closeable} interface. */ public S buildProxy(RetryPolicy retryPolicy) { return ServicePoolProxy.create(_serviceType, retryPolicy, build(), _partitionContextSupplier, true); }
/** Create an SOA Databus client for forwarding non-partition-aware clients to the right server. */ @Provides @Singleton @PartitionAwareClient SubjectDatabus provideSubjectDatabusClient(MultiThreadedServiceFactory<SubjectDatabus> serviceFactory, @DatabusHostDiscovery HostDiscovery hostDiscovery, SubjectDatabus localSubjectDatabus, @SelfHostAndPort HostAndPort self, MetricRegistry metricRegistry, HealthCheckRegistry healthCheckRegistry) { SubjectDatabus client = ServicePoolBuilder.create(SubjectDatabus.class) .withHostDiscovery(hostDiscovery) .withServiceFactory( new PartitionAwareServiceFactory<>(SubjectDatabus.class, serviceFactory, localSubjectDatabus, self, healthCheckRegistry, metricRegistry)) .withMetricRegistry(metricRegistry) .withCachingPolicy(ServiceCachingPolicyBuilder.getMultiThreadedClientPolicy()) .buildProxy(new ExponentialBackoffRetry(5, 50, 1000, TimeUnit.MILLISECONDS)); _environment.lifecycle().manage(new ManagedServicePoolProxy(client)); return client; }
MetricRegistry metrics = new MetricRegistry(); CalculatorServiceFactory serviceFactory = new CalculatorServiceFactory(httpClientConfiguration, metrics); ServicePool<CalculatorService> pool = ServicePoolBuilder.create(CalculatorService.class) .withServiceFactory(serviceFactory) .withHostDiscovery(new ZooKeeperHostDiscovery(curator, serviceFactory.getServiceName(), metrics)) .withMetricRegistry(metrics) .withCachingPolicy(cachingPolicy) .build();
UserAccessControl uac = ServicePoolBuilder.create(UserAccessControl.class) .withHostDiscoverySource(new UserAccessControlFixedHostDiscoverySource("http://localhost:" + selfHostAndPort.getPort())) .withServiceFactory(UserAccessControlClientFactory.forCluster(cluster, metricRegistry).usingCredentials(adminApiKey)) .withMetricRegistry(metricRegistry) .buildProxy(new ExponentialBackoffRetry(5, 50, 1000, TimeUnit.MILLISECONDS));
/** * Makes the built proxy generate partition context based on the {@link PartitionKey} annotation * on method arguments in class {@code S}. * <p> * If {@code S} is not annotated, or annotated differently than desired, consider using * {@link #withPartitionContextAnnotationsFrom(Class)} instead. * <p> * NOTE: This is only useful if building a proxy with {@link #buildProxy(com.bazaarvoice.ostrich.RetryPolicy)}. If * partition context is necessary with a normal service pool, then can be provided directly by calling * {@link com.bazaarvoice.ostrich.ServicePool#execute(com.bazaarvoice.ostrich.PartitionContext, * com.bazaarvoice.ostrich.RetryPolicy, com.bazaarvoice.ostrich.ServiceCallback)}. * * @return this */ public ServicePoolBuilder<S> withPartitionContextAnnotations() { return withPartitionContextAnnotationsFrom(_serviceType); }
@Override public ScanWorkflow get() { QueueClientFactory factory = QueueClientFactory.forClusterAndHttpClient(_cluster, _client); // Don't use the local queue service; create a client to call out to the live EmoDB application. AuthQueueService authService = ServicePoolBuilder.create(AuthQueueService.class) .withServiceFactory(factory) .withHostDiscovery(new ZooKeeperHostDiscovery(_curator, factory.getServiceName(), _metricRegistry)) .withMetricRegistry(_metricRegistry) .buildProxy(new ExponentialBackoffRetry(5, 50, 1000, TimeUnit.MILLISECONDS)); _environment.lifecycle().manage(new ManagedServicePoolProxy(authService)); QueueService service = QueueServiceAuthenticator.proxied(authService) .usingCredentials(_apiKey); return new QueueScanWorkflow(service, _pendingScanRangeQueueName, _completeScanRangeQueueName); } }
/** * Makes the built proxy generate partition context based on the {@link PartitionKey} annotation * on method arguments in class {@code S}. * <p> * If {@code S} is not annotated, or annotated differently than desired, consider using * {@link #withPartitionContextAnnotationsFrom(Class)} instead. * <p> * NOTE: This is only useful if building a proxy with {@link #buildProxy(com.bazaarvoice.ostrich.RetryPolicy)}. If * partition context is necessary with a normal service pool, then can be provided directly by calling * {@link com.bazaarvoice.ostrich.ServicePool#execute(com.bazaarvoice.ostrich.PartitionContext, * com.bazaarvoice.ostrich.RetryPolicy, com.bazaarvoice.ostrich.ServiceCallback)}. * * @return this */ public ServicePoolBuilder<S> withPartitionContextAnnotations() { return withPartitionContextAnnotationsFrom(_serviceType); }
@Override public ScanWorkflow get() { QueueClientFactory factory = QueueClientFactory.forClusterAndHttpClient(_cluster, _client); // Don't use the local queue service; create a client to call out to the live EmoDB application. AuthQueueService authService = ServicePoolBuilder.create(AuthQueueService.class) .withServiceFactory(factory) .withHostDiscovery(new ZooKeeperHostDiscovery(_curator, factory.getServiceName(), _metricRegistry)) .withMetricRegistry(_metricRegistry) .buildProxy(new ExponentialBackoffRetry(5, 50, 1000, TimeUnit.MILLISECONDS)); _environment.lifecycle().manage(new ManagedServicePoolProxy(authService)); QueueService service = QueueServiceAuthenticator.proxied(authService) .usingCredentials(_apiKey); return new QueueScanWorkflow(service, _pendingScanRangeQueueName, _completeScanRangeQueueName); } }
@Override public void configure(ServicePoolBuilder<SubjectDatabus> servicePoolBuilder) { servicePoolBuilder.withPartitionFilter(new ConsistentHashPartitionFilter()) .withPartitionContextAnnotationsFrom(AbstractSubjectDatabus.class); }
/** * Adds a {@link HostDiscovery} instance to the builder. The service pool will use this {@code HostDiscovery} * instance unless a preceding {@link HostDiscoverySource} provides a non-null instance of {@code HostDiscovery} for * a given service name. * <p> * Once this method is called, any subsequent calls to host discovery-related methods on this builder instance are * ignored (because this non-null discovery will always be returned). * <p> * Note that callers of this method are responsible for calling {@link HostDiscovery#close} on the passed instance. * * @param hostDiscovery the host discovery instance to use in the built {@link ServicePool} * @return this */ public ServicePoolBuilder<S> withHostDiscovery(final HostDiscovery hostDiscovery) { checkNotNull(hostDiscovery); HostDiscoverySource hostDiscoverySource = new HostDiscoverySource() { @Override public HostDiscovery forService(String serviceName) { return hostDiscovery; } }; return withHostDiscovery(hostDiscoverySource, false); }
/** * Builds a dynamic proxy that wraps a {@code ServicePool} and implements the service interface directly. This is * appropriate for stateless services where it's sensible for the same retry policy to apply to every method. * <p/> * It is the caller's responsibility to shutdown the service pool when they're done with it by casting the proxy * to {@link java.io.Closeable} and calling the {@link java.io.Closeable#close()} method. * * @param retryPolicy The retry policy to apply for every service call. * @return The dynamic proxy instance that implements the service interface {@code S} and the * {@link java.io.Closeable} interface. */ public S buildProxy(RetryPolicy retryPolicy) { return ServicePoolProxy.create(_serviceType, retryPolicy, build(), _partitionContextSupplier, true); }
/** Create an SOA DedupQueue client for forwarding non-partition-aware clients to the right server. */ @Provides @Singleton @PartitionAwareClient DedupQueueServiceAuthenticator provideDedupQueueClient(MultiThreadedServiceFactory<AuthDedupQueueService> serviceFactory, @DedupQueueHostDiscovery HostDiscovery hostDiscovery, DedupQueueService databus, @SelfHostAndPort HostAndPort self, HealthCheckRegistry healthCheckRegistry, MetricRegistry metricRegistry) { AuthDedupQueueService client = ServicePoolBuilder.create(AuthDedupQueueService.class) .withHostDiscovery(hostDiscovery) .withServiceFactory(new PartitionAwareServiceFactory<>( AuthDedupQueueService.class, serviceFactory, new TrustedDedupQueueService(databus), self, healthCheckRegistry, metricRegistry)) .withMetricRegistry(_environment.metrics()) .withCachingPolicy(ServiceCachingPolicyBuilder.getMultiThreadedClientPolicy()) .buildProxy(new ExponentialBackoffRetry(5, 50, 1000, TimeUnit.MILLISECONDS)); _environment.lifecycle().manage(new ManagedServicePoolProxy(client)); return DedupQueueServiceAuthenticator.proxied(client); } }
@Override public void configure(ServicePoolBuilder<AuthQueueService> servicePoolBuilder) { servicePoolBuilder.withPartitionFilter(new ConsistentHashPartitionFilter()) .withPartitionContextAnnotationsFrom(QueueClient.class); }
/** * Adds a {@link HostDiscoverySource} instance to the builder. Multiple instances of {@code HostDiscoverySource} * may be specified. The service pool will query the sources in the order they were registered and use the first * non-null {@link HostDiscovery} returned for the service name provided by the * {@link ServiceFactory#getServiceName()} method of the factory configured by {@link #withServiceFactory}. * <p> * Note that using this method will cause the ServicePoolBuilder to call * {@link HostDiscoverySource#forService(String serviceName)} when {@link #build()} is called and pass the returned * {@link HostDiscovery} to the new {@code ServicePool}. Subsequently calling {@link ServicePool#close()} will in * turn call {@link HostDiscovery#close()} on the passed instance. * * @param hostDiscoverySource a host discovery source to use to find the {@link HostDiscovery} when constructing * the {@link ServicePool} * @return this */ public ServicePoolBuilder<S> withHostDiscoverySource(HostDiscoverySource hostDiscoverySource) { checkNotNull(hostDiscoverySource); return withHostDiscovery(hostDiscoverySource, true); }
HostDiscovery hostDiscovery = LocationUtil.getHostDiscoveryForLocation(location, curator, dataStoreFactory.getServiceName(), metricRegistry); final AuthDataStore authDataStore = ServicePoolBuilder.create(AuthDataStore.class) .withHostDiscovery(hostDiscovery) .withServiceFactory(dataStoreFactory) .withCachingPolicy(ServiceCachingPolicyBuilder.getMultiThreadedClientPolicy()) .withMetricRegistry(metricRegistry) .buildProxy(new ExponentialBackoffRetry(5, 50, 1000, TimeUnit.MILLISECONDS));
@Override public void configure(ServicePoolBuilder<DedupQueueService> servicePoolBuilder) { servicePoolBuilder.withPartitionFilter(new ConsistentHashPartitionFilter()) .withPartitionContextAnnotationsFrom(DedupQueueServiceAuthenticatorProxy.class); }
/** * Adds a {@link HostDiscovery} instance to the builder. The service pool will use this {@code HostDiscovery} * instance unless a preceding {@link HostDiscoverySource} provides a non-null instance of {@code HostDiscovery} for * a given service name. * <p> * Once this method is called, any subsequent calls to host discovery-related methods on this builder instance are * ignored (because this non-null discovery will always be returned). * <p> * Note that callers of this method are responsible for calling {@link HostDiscovery#close} on the passed instance. * * @param hostDiscovery the host discovery instance to use in the built {@link ServicePool} * @return this */ public ServicePoolBuilder<S> withHostDiscovery(final HostDiscovery hostDiscovery) { checkNotNull(hostDiscovery); HostDiscoverySource hostDiscoverySource = new HostDiscoverySource() { @Override public HostDiscovery forService(String serviceName) { return hostDiscovery; } }; return withHostDiscovery(hostDiscoverySource, false); }
HostDiscovery hostDiscovery = LocationUtil.getHostDiscoveryForLocation(location, curator, dataStoreFactory.getServiceName(), metricRegistry); final AuthDataStore authDataStore = ServicePoolBuilder.create(AuthDataStore.class) .withHostDiscovery(hostDiscovery) .withServiceFactory(dataStoreFactory) .withCachingPolicy(ServiceCachingPolicyBuilder.getMultiThreadedClientPolicy()) .withMetricRegistry(metricRegistry) .buildProxy(new ExponentialBackoffRetry(5, 50, 1000, TimeUnit.MILLISECONDS));