Search in sources :

Example 1 with DialogueChannel

use of com.palantir.dialogue.core.DialogueChannel in project dialogue by palantir.

the class ReloadingClientFactory method getStickyChannels.

@Override
public StickyChannelFactory getStickyChannels(String serviceName) {
    Refreshable<List<Channel>> perHostChannels = perHost(serviceName).getPerHostChannels();
    Refreshable<Supplier<Channel>> bestSupplier = perHostChannels.map(singleHostChannels -> {
        if (singleHostChannels.isEmpty()) {
            EmptyInternalDialogueChannel alwaysThrowing = new EmptyInternalDialogueChannel(() -> new SafeIllegalStateException("Service not configured", SafeArg.of("serviceName", serviceName)));
            return () -> alwaysThrowing;
        }
        if (singleHostChannels.size() == 1) {
            return () -> singleHostChannels.get(0);
        }
        return StickyEndpointChannels.builder().channels(singleHostChannels.stream().map(c -> (DialogueChannel) c).collect(Collectors.toList())).channelName(ChannelNames.reloading(serviceName, params)).taggedMetricRegistry(params.taggedMetrics()).build();
    });
    return new StickyChannelFactory() {

        @Override
        public Channel getStickyChannel() {
            return bestSupplier.get().get();
        }

        @Override
        public <T> T getCurrentBest(Class<T> clientInterface) {
            return Reflection.callStaticFactoryMethod(clientInterface, getStickyChannel(), params.runtime());
        }

        @Override
        public String toString() {
            return "StickyChannelFactory{" + bestSupplier.get() + '}';
        }
    };
}
Also used : ListenableFuture(com.google.common.util.concurrent.ListenableFuture) SafeIllegalStateException(com.palantir.logsafe.exceptions.SafeIllegalStateException) ClientConfiguration(com.palantir.conjure.java.client.config.ClientConfiguration) OptionalInt(java.util.OptionalInt) Function(java.util.function.Function) Supplier(java.util.function.Supplier) StickyChannelFactory2(com.palantir.dialogue.clients.DialogueClients.StickyChannelFactory2) SafeArg(com.palantir.logsafe.SafeArg) NodeSelectionStrategy(com.palantir.conjure.java.client.config.NodeSelectionStrategy) ConjureRuntime(com.palantir.dialogue.ConjureRuntime) ImmutableList(com.google.common.collect.ImmutableList) UserAgent(com.palantir.conjure.java.api.config.service.UserAgent) Value(org.immutables.value.Value) Duration(java.time.Duration) Map(java.util.Map) Suppliers(com.google.common.base.Suppliers) DefaultConjureRuntime(com.palantir.conjure.java.dialogue.serde.DefaultConjureRuntime) Endpoint(com.palantir.dialogue.Endpoint) ApacheHttpClientChannels(com.palantir.dialogue.hc5.ApacheHttpClientChannels) ServiceConfiguration(com.palantir.conjure.java.api.config.service.ServiceConfiguration) Request(com.palantir.dialogue.Request) StickyChannelFactory(com.palantir.dialogue.clients.DialogueClients.StickyChannelFactory) ExecutorService(java.util.concurrent.ExecutorService) EndpointChannelFactory(com.palantir.dialogue.EndpointChannelFactory) Refreshable(com.palantir.refreshable.Refreshable) ServiceConfigurationFactory(com.palantir.conjure.java.api.config.service.ServiceConfigurationFactory) Clients(com.palantir.dialogue.Clients) PerHostClientFactory(com.palantir.dialogue.clients.DialogueClients.PerHostClientFactory) Channel(com.palantir.dialogue.Channel) Maps(com.google.common.collect.Maps) TaggedMetricRegistry(com.palantir.tritium.metrics.registry.TaggedMetricRegistry) Collectors(java.util.stream.Collectors) Provider(java.security.Provider) HostEventsSink(com.palantir.conjure.java.client.config.HostEventsSink) ServicesConfigBlock(com.palantir.conjure.java.api.config.service.ServicesConfigBlock) StickyEndpointChannels(com.palantir.dialogue.core.StickyEndpointChannels) Futures(com.google.common.util.concurrent.Futures) EndpointChannel(com.palantir.dialogue.EndpointChannel) List(java.util.List) DialogueChannel(com.palantir.dialogue.core.DialogueChannel) PartialServiceConfiguration(com.palantir.conjure.java.api.config.service.PartialServiceConfiguration) Optional(java.util.Optional) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Response(com.palantir.dialogue.Response) Preconditions(com.palantir.logsafe.Preconditions) DialogueChannel(com.palantir.dialogue.core.DialogueChannel) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) Supplier(java.util.function.Supplier) SafeIllegalStateException(com.palantir.logsafe.exceptions.SafeIllegalStateException) StickyChannelFactory(com.palantir.dialogue.clients.DialogueClients.StickyChannelFactory)

Example 2 with DialogueChannel

use of com.palantir.dialogue.core.DialogueChannel in project dialogue by palantir.

the class ReloadingClientFactory method getInternalDialogueChannel.

private Refreshable<InternalDialogueChannel> getInternalDialogueChannel(String serviceName) {
    Preconditions.checkNotNull(serviceName, "serviceName");
    String channelName = ChannelNames.reloading(serviceName, params);
    return params.scb().map(block -> {
        Preconditions.checkNotNull(block, "Refreshable must not provide a null ServicesConfigBlock");
        if (!block.services().containsKey(serviceName)) {
            return new EmptyInternalDialogueChannel(() -> new SafeIllegalStateException("Service not configured (config block not present)", SafeArg.of("serviceName", serviceName), SafeArg.of("available", block.services().keySet())));
        }
        if (block.services().get(serviceName).uris().isEmpty()) {
            return new EmptyInternalDialogueChannel(() -> {
                Map<String, PartialServiceConfiguration> servicesWithUris = Maps.filterValues(block.services(), c -> !c.uris().isEmpty());
                return new SafeIllegalStateException("Service not configured (no URIs)", SafeArg.of("serviceName", serviceName), SafeArg.of("available", servicesWithUris.keySet()));
            });
        }
        ServiceConfiguration serviceConf = ServiceConfigurationFactory.of(block).get(serviceName);
        DialogueChannel dialogueChannel = cache.getNonReloadingChannel(params, serviceConf, channelName, OptionalInt.empty());
        return new InternalDialogueChannelFromDialogueChannel(dialogueChannel);
    });
}
Also used : ServiceConfiguration(com.palantir.conjure.java.api.config.service.ServiceConfiguration) PartialServiceConfiguration(com.palantir.conjure.java.api.config.service.PartialServiceConfiguration) DialogueChannel(com.palantir.dialogue.core.DialogueChannel) PartialServiceConfiguration(com.palantir.conjure.java.api.config.service.PartialServiceConfiguration) SafeIllegalStateException(com.palantir.logsafe.exceptions.SafeIllegalStateException)

Example 3 with DialogueChannel

use of com.palantir.dialogue.core.DialogueChannel in project dialogue by palantir.

the class ReloadingClientFactory method perHost.

@Override
public PerHostClientFactory perHost(String serviceName) {
    Preconditions.checkNotNull(serviceName, "serviceName");
    String channelName = ChannelNames.reloading(serviceName, params);
    Refreshable<List<DialogueChannel>> perHostDialogueChannels = params.scb().map(block -> {
        if (!block.services().containsKey(serviceName)) {
            return ImmutableList.of();
        }
        ServiceConfiguration serviceConfiguration = ServiceConfigurationFactory.of(block).get(serviceName);
        ImmutableList.Builder<DialogueChannel> list = ImmutableList.builder();
        for (int i = 0; i < serviceConfiguration.uris().size(); i++) {
            ServiceConfiguration singleUriServiceConf = ServiceConfiguration.builder().from(serviceConfiguration).uris(ImmutableList.of(serviceConfiguration.uris().get(i))).build();
            // subtle gotcha here is that every single one of these has the same channelName,
            // which means metrics like the QueuedChannel counter will end up being the sum of all of them.
            list.add(cache.getNonReloadingChannel(params, singleUriServiceConf, channelName, OptionalInt.of(i)));
        }
        return list.build();
    });
    return new PerHostClientFactory() {

        @Override
        public Refreshable<List<Channel>> getPerHostChannels() {
            return perHostDialogueChannels.map(ImmutableList::copyOf);
        }

        @Override
        public <T> Refreshable<List<T>> getPerHost(Class<T> clientInterface) {
            return getPerHostChannels().map(channels -> channels.stream().map(chan -> Reflection.callStaticFactoryMethod(clientInterface, chan, params.runtime())).collect(ImmutableList.toImmutableList()));
        }

        @Override
        public String toString() {
            return "PerHostClientFactory{serviceName=" + serviceName + ", channels=" + perHostDialogueChannels.get() + '}';
        }
    };
}
Also used : ServiceConfiguration(com.palantir.conjure.java.api.config.service.ServiceConfiguration) PartialServiceConfiguration(com.palantir.conjure.java.api.config.service.PartialServiceConfiguration) DialogueChannel(com.palantir.dialogue.core.DialogueChannel) PerHostClientFactory(com.palantir.dialogue.clients.DialogueClients.PerHostClientFactory) ImmutableList(com.google.common.collect.ImmutableList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) Endpoint(com.palantir.dialogue.Endpoint)

Aggregations

PartialServiceConfiguration (com.palantir.conjure.java.api.config.service.PartialServiceConfiguration)3 ServiceConfiguration (com.palantir.conjure.java.api.config.service.ServiceConfiguration)3 DialogueChannel (com.palantir.dialogue.core.DialogueChannel)3 ImmutableList (com.google.common.collect.ImmutableList)2 Endpoint (com.palantir.dialogue.Endpoint)2 PerHostClientFactory (com.palantir.dialogue.clients.DialogueClients.PerHostClientFactory)2 SafeIllegalStateException (com.palantir.logsafe.exceptions.SafeIllegalStateException)2 List (java.util.List)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Suppliers (com.google.common.base.Suppliers)1 Maps (com.google.common.collect.Maps)1 Futures (com.google.common.util.concurrent.Futures)1 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)1 ServiceConfigurationFactory (com.palantir.conjure.java.api.config.service.ServiceConfigurationFactory)1 ServicesConfigBlock (com.palantir.conjure.java.api.config.service.ServicesConfigBlock)1 UserAgent (com.palantir.conjure.java.api.config.service.UserAgent)1 ClientConfiguration (com.palantir.conjure.java.client.config.ClientConfiguration)1 HostEventsSink (com.palantir.conjure.java.client.config.HostEventsSink)1 NodeSelectionStrategy (com.palantir.conjure.java.client.config.NodeSelectionStrategy)1 DefaultConjureRuntime (com.palantir.conjure.java.dialogue.serde.DefaultConjureRuntime)1