@Override protected DriftClient<T> get(Injector injector, Annotation clientAnnotation) { DriftClientConfig config = injector.getInstance(Key.get(DriftClientConfig.class, clientAnnotation)); DriftClientFactoryManager<Annotation> driftClientFactoryManager = injector.getInstance(Key.get(DRIFT_CLIENT_FACTORY_MANAGER_TYPE)); AddressSelector<?> addressSelector = injector.getInstance(Key.get(AddressSelector.class, clientAnnotation)); ExceptionClassifier exceptionClassifier = mergeExceptionClassifiers(ImmutableList.<ExceptionClassifier>builder() .addAll(injector.getInstance(Key.get(SET_EXCEPTION_CLASSIFIER_TYPE, clientAnnotation))) // per-client .addAll(injector.getInstance(Key.get(SET_EXCEPTION_CLASSIFIER_TYPE))) // global .build()); List<MethodInvocationFilter> filters = ImmutableList.copyOf(injector.getInstance(Key.get(SET_METHOD_INVOCATION_FILTERS_TYPE, clientAnnotation))); DriftClientFactory driftClientFactory = driftClientFactoryManager.createDriftClientFactory(clientAnnotation, addressSelector, exceptionClassifier); return driftClientFactory.createDriftClient(clientInterface, extractDriftClientBindingAnnotation(clientAnnotation), filters, config); } }
public DriftClientFactory createDriftClientFactory(I clientIdentity, AddressSelector<?> addressSelector, ExceptionClassifier exceptionClassifier) { return new DriftClientFactory( codecManager, () -> methodInvokerFactory.createMethodInvoker(clientIdentity), addressSelector, exceptionClassifier, methodInvocationStatsFactory); } }
private static int logNettyStaticDriftClient( HostAndPort address, String headerValue, List<DriftLogEntry> entries, List<MethodInvocationFilter> filters, Transport transport, Protocol protocol, boolean secure) { if (!isValidConfiguration(transport, protocol)) { return 0; } AddressSelector<?> addressSelector = context -> Optional.of(() -> address); DriftNettyClientConfig config = new DriftNettyClientConfig() .setTransport(transport) .setProtocol(protocol) .setTrustCertificate(ClientTestUtils.getCertificateChainFile()) .setSslEnabled(secure); try (TestingPooledByteBufAllocator testingAllocator = new TestingPooledByteBufAllocator(); DriftNettyMethodInvokerFactory<?> methodInvokerFactory = createStaticDriftNettyMethodInvokerFactory(config, testingAllocator)) { DriftClientFactory proxyFactory = new DriftClientFactory(CODEC_MANAGER, methodInvokerFactory, addressSelector); DriftScribe scribe = proxyFactory.createDriftClient(DriftScribe.class, Optional.empty(), filters, new DriftClientConfig()).get(); assertEquals(scribe.log(headerValue, entries), DRIFT_OK); } catch (Exception e) { throw new RuntimeException(e); } return 1; }
private static int logApacheThriftStaticDriftClient( HostAndPort address, String headerValue, List<DriftLogEntry> entries, List<MethodInvocationFilter> filters, Transport transport, Protocol protocol, boolean secure) { if (!isValidConfiguration(transport, protocol)) { return 0; } AddressSelector<?> addressSelector = context -> Optional.of(() -> address); ApacheThriftClientConfig config = new ApacheThriftClientConfig() .setTransport(toApacheThriftTransport(transport)) .setProtocol(toApacheThriftProtocol(protocol)) .setTrustCertificate(ClientTestUtils.getCertificateChainFile()) .setSslEnabled(secure); try (ApacheThriftMethodInvokerFactory<?> methodInvokerFactory = createStaticApacheThriftMethodInvokerFactory(config)) { DriftClientFactory proxyFactory = new DriftClientFactory(CODEC_MANAGER, methodInvokerFactory, addressSelector); DriftScribe scribe = proxyFactory.createDriftClient(DriftScribe.class, Optional.empty(), filters, new DriftClientConfig()).get(); assertEquals(scribe.log(headerValue, entries), DRIFT_OK); } catch (Exception e) { throw new RuntimeException(e); } return 1; }
@Override protected DriftClient<T> get(Injector injector, Annotation clientAnnotation) { DriftClientConfig config = injector.getInstance(Key.get(DriftClientConfig.class, clientAnnotation)); DriftClientFactoryManager<Annotation> driftClientFactoryManager = injector.getInstance(Key.get(DRIFT_CLIENT_FACTORY_MANAGER_TYPE)); AddressSelector<?> addressSelector = injector.getInstance(Key.get(AddressSelector.class, clientAnnotation)); ExceptionClassifier exceptionClassifier = mergeExceptionClassifiers(ImmutableList.<ExceptionClassifier>builder() .addAll(injector.getInstance(Key.get(SET_EXCEPTION_CLASSIFIER_TYPE, clientAnnotation))) // per-client .addAll(injector.getInstance(Key.get(SET_EXCEPTION_CLASSIFIER_TYPE))) // global .build()); List<MethodInvocationFilter> filters = ImmutableList.copyOf(injector.getInstance(Key.get(SET_METHOD_INVOCATION_FILTERS_TYPE, clientAnnotation))); DriftClientFactory driftClientFactory = driftClientFactoryManager.createDriftClientFactory(clientAnnotation, addressSelector, exceptionClassifier); return driftClientFactory.createDriftClient(clientInterface, extractDriftClientBindingAnnotation(clientAnnotation), filters, config); } }
public DriftClientFactory createDriftClientFactory(I clientIdentity, AddressSelector<?> addressSelector, ExceptionClassifier exceptionClassifier) { return new DriftClientFactory( codecManager, () -> methodInvokerFactory.createMethodInvoker(clientIdentity), addressSelector, exceptionClassifier, methodInvocationStatsFactory); } }
public <T> DriftClient<T> createDriftClient( Class<T> clientInterface, Optional<Class<? extends Annotation>> qualifierAnnotation, List<MethodInvocationFilter> filters, DriftClientConfig config) { ThriftServiceMetadata serviceMetadata = serviceMetadataCache.computeIfAbsent( clientInterface, clazz -> new ThriftServiceMetadata(clazz, codecManager.getCatalog())); MethodInvoker invoker = createFilteredMethodInvoker(filters, methodInvokerSupplier.get()); Optional<String> qualifier = qualifierAnnotation.map(Class::getSimpleName); ImmutableMap.Builder<Method, DriftMethodHandler> builder = ImmutableMap.builder(); for (ThriftMethodMetadata method : serviceMetadata.getMethods().values()) { MethodMetadata metadata = toMethodMetadata(codecManager, method); RetryPolicy retryPolicy = new RetryPolicy(config, exceptionClassifier); MethodInvocationStat statHandler; if (config.isStatsEnabled()) { statHandler = methodInvocationStatsFactory.getStat(serviceMetadata, qualifier, metadata); } else { statHandler = new NullMethodInvocationStat(); } DriftMethodHandler handler = new DriftMethodHandler(metadata, method.getHeaderParameters(), invoker, method.isAsync(), addressSelector, retryPolicy, statHandler); builder.put(method.getMethod(), handler);
public <T> DriftClient<T> createDriftClient( Class<T> clientInterface, Optional<Class<? extends Annotation>> qualifierAnnotation, List<MethodInvocationFilter> filters, DriftClientConfig config) { ThriftServiceMetadata serviceMetadata = serviceMetadataCache.computeIfAbsent( clientInterface, clazz -> new ThriftServiceMetadata(clazz, codecManager.getCatalog())); MethodInvoker invoker = createFilteredMethodInvoker(filters, methodInvokerSupplier.get()); Optional<String> qualifier = qualifierAnnotation.map(Class::getSimpleName); ImmutableMap.Builder<Method, DriftMethodHandler> builder = ImmutableMap.builder(); for (ThriftMethodMetadata method : serviceMetadata.getMethods().values()) { MethodMetadata metadata = toMethodMetadata(codecManager, method); RetryPolicy retryPolicy = new RetryPolicy(config, exceptionClassifier); MethodInvocationStat statHandler; if (config.isStatsEnabled()) { statHandler = methodInvocationStatsFactory.getStat(serviceMetadata, qualifier, metadata); } else { statHandler = new NullMethodInvocationStat(); } DriftMethodHandler handler = new DriftMethodHandler(metadata, method.getHeaderParameters(), invoker, method.isAsync(), addressSelector, retryPolicy, statHandler); builder.put(method.getMethod(), handler);
@Test public void testInvoker() throws Exception { ResultsSupplier resultsSupplier = new ResultsSupplier(); MockMethodInvokerFactory<String> methodInvokerFactory = new MockMethodInvokerFactory<>(resultsSupplier); TestingMethodInvocationStatsFactory statsFactory = new TestingMethodInvocationStatsFactory(); List<TestingExceptionClassifier> classifiers = ImmutableList.of(new TestingExceptionClassifier(), new TestingExceptionClassifier(), new TestingExceptionClassifier()); DriftClientFactoryManager<String> clientFactoryManager = new DriftClientFactoryManager<>(codecManager, methodInvokerFactory, statsFactory); DriftClientFactory driftClientFactory = clientFactoryManager.createDriftClientFactory("clientIdentity", new MockAddressSelector(), mergeExceptionClassifiers(classifiers)); DriftClient<Client> driftClient = driftClientFactory.createDriftClient(Client.class, Optional.empty(), ImmutableList.of(), new DriftClientConfig()); Client client = driftClient.get(ADDRESS_SELECTION_CONTEXT, HEADERS); assertEquals(methodInvokerFactory.getClientIdentity(), "clientIdentity"); testClient(resultsSupplier, ImmutableList.of(methodInvokerFactory.getMethodInvoker()), classifiers, statsFactory, client, Optional.empty()); }
@Test public void testFilter() throws Exception { ResultsSupplier resultsSupplier = new ResultsSupplier(); PassThroughFilter passThroughFilter = new PassThroughFilter(); ShortCircuitFilter shortCircuitFilter = new ShortCircuitFilter(resultsSupplier); MockMethodInvokerFactory<String> invokerFactory = new MockMethodInvokerFactory<>(resultsSupplier); TestingMethodInvocationStatsFactory statsFactory = new TestingMethodInvocationStatsFactory(); List<TestingExceptionClassifier> classifiers = ImmutableList.of(new TestingExceptionClassifier(), new TestingExceptionClassifier(), new TestingExceptionClassifier()); DriftClientFactoryManager<String> clientFactoryManager = new DriftClientFactoryManager<>(codecManager, invokerFactory, statsFactory); DriftClientFactory driftClientFactory = clientFactoryManager.createDriftClientFactory("clientIdentity", new MockAddressSelector(), mergeExceptionClassifiers(classifiers)); DriftClient<Client> driftClient = driftClientFactory.createDriftClient( Client.class, Optional.empty(), ImmutableList.of(passThroughFilter, shortCircuitFilter), new DriftClientConfig()); Client client = driftClient.get(ADDRESS_SELECTION_CONTEXT, HEADERS); assertEquals(invokerFactory.getClientIdentity(), "clientIdentity"); testClient(resultsSupplier, ImmutableList.of(passThroughFilter, shortCircuitFilter), classifiers, statsFactory, client, Optional.empty()); }
private static int logApacheThriftDriftClientAsync( HostAndPort address, String headerValue, List<DriftLogEntry> entries, List<MethodInvocationFilter> filters, Transport transport, Protocol protocol, boolean secure) { if (!isValidConfiguration(transport, protocol)) { return 0; } AddressSelector<?> addressSelector = context -> Optional.of(() -> address); ApacheThriftClientConfig config = new ApacheThriftClientConfig() .setTransport(toApacheThriftTransport(transport)) .setProtocol(toApacheThriftProtocol(protocol)) .setTrustCertificate(ClientTestUtils.getCertificateChainFile()) .setSslEnabled(secure); ApacheThriftConnectionFactoryConfig factoryConfig = new ApacheThriftConnectionFactoryConfig(); try (ApacheThriftMethodInvokerFactory<String> methodInvokerFactory = new ApacheThriftMethodInvokerFactory<>(factoryConfig, clientIdentity -> config)) { DriftClientFactoryManager<String> proxyFactoryManager = new DriftClientFactoryManager<>(CODEC_MANAGER, methodInvokerFactory); DriftClientFactory proxyFactory = proxyFactoryManager.createDriftClientFactory("myFactory", addressSelector, NORMAL_RESULT); DriftAsyncScribe scribe = proxyFactory.createDriftClient(DriftAsyncScribe.class, Optional.empty(), filters, new DriftClientConfig()).get(); assertEquals(scribe.log(headerValue, entries).get(), DRIFT_OK); } catch (Exception e) { throw new RuntimeException(e); } return 1; }
DriftClientFactory proxyFactory = proxyFactoryManager.createDriftClientFactory("myFactory", addressSelector, NORMAL_RESULT); DriftAsyncScribe scribe = proxyFactory.createDriftClient(DriftAsyncScribe.class, Optional.empty(), filters, new DriftClientConfig()).get();
DriftClientFactory proxyFactory = clientFactoryManager.createDriftClientFactory("clientIdentity", addressSelector, NORMAL_RESULT); DriftScribe scribe = proxyFactory.createDriftClient(DriftScribe.class, Optional.empty(), filters, new DriftClientConfig()).get();
private static int logApacheThriftDriftClient( HostAndPort address, String headerValue, List<DriftLogEntry> entries, List<MethodInvocationFilter> filters, Transport transport, Protocol protocol, boolean secure) { if (!isValidConfiguration(transport, protocol)) { return 0; } AddressSelector<?> addressSelector = context -> Optional.of(() -> address); ApacheThriftClientConfig config = new ApacheThriftClientConfig() .setTransport(toApacheThriftTransport(transport)) .setProtocol(toApacheThriftProtocol(protocol)) .setTrustCertificate(ClientTestUtils.getCertificateChainFile()) .setSslEnabled(secure); ApacheThriftConnectionFactoryConfig factoryConfig = new ApacheThriftConnectionFactoryConfig(); try (ApacheThriftMethodInvokerFactory<String> methodInvokerFactory = new ApacheThriftMethodInvokerFactory<>(factoryConfig, clientIdentity -> config)) { DriftClientFactoryManager<String> clientFactoryManager = new DriftClientFactoryManager<>(CODEC_MANAGER, methodInvokerFactory); DriftClientFactory proxyFactory = clientFactoryManager.createDriftClientFactory("clientIdentity", addressSelector, NORMAL_RESULT); DriftScribe scribe = proxyFactory.createDriftClient(DriftScribe.class, Optional.empty(), filters, new DriftClientConfig()).get(); assertEquals(scribe.log(headerValue, entries), DRIFT_OK); } catch (Exception e) { throw new RuntimeException(e); } return 1; }