Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support behaviour changes based on URIs with a 'mesh-' prefix #1048

Merged
merged 8 commits into from
Nov 10, 2020
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ final class ConcurrencyLimitedChannel implements LimitedChannel {
private final String channelNameForLogging;

static LimitedChannel createForHost(Config cf, Channel channel, int uriIndex) {
if (cf.mesh() == MeshMode.USE_EXTERNAL_MESH) {
return new ChannelToLimitedChannelAdapter(channel);
}

ClientConfiguration.ClientQoS clientQoS = cf.clientConf().clientQoS();
switch (clientQoS) {
case ENABLED:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@
import java.util.OptionalInt;
import java.util.Random;
import java.util.concurrent.ScheduledExecutorService;
import java.util.stream.Collectors;
import org.immutables.value.Value;

/** Private class to centralize validation of params necessary to construct a dialogue channel. */
@Value.Immutable
interface Config {

String channelName();

ChannelFactory channelFactory();
Expand All @@ -40,11 +42,17 @@ interface Config {
default ClientConfiguration clientConf() {
return ClientConfiguration.builder()
.from(rawConfig())
.uris(rawConfig().uris().stream().map(MeshMode::stripMeshPrefix).collect(Collectors.toList()))
.taggedMetricRegistry(
VersionedTaggedMetricRegistry.create(rawConfig().taggedMetricRegistry()))
.build();
}

@Value.Derived
default MeshMode mesh() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we keep all of the meshmode stuff internal to DialogueChannel? i.e. perform the computation internally instead of having a derived field on the config object?

Copy link
Contributor Author

@iamdanfox iamdanfox Nov 9, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure we could, but then there's a possibility of constructing a Config object which is in a non-sensical state - i.e. has mesh- prefixed uris, but doesn't have MeshMode.USE_EXTERNAL_MESH set. The reason I think it's appropriate & desirable here is to emphasise to people that this isn't an extra degree of freedom in how you build a Config object, instead it's just purely derived from the URIs that you already put in. This is an example of the "make illegal states unrepresentable" pattern.

Otherwise, to keep this safety, we'd just have to put in a Value.Check that computes the mesh mode.

Third option is to not even have the mesh() param on this object, which would then require plumbing it in everywhere that we currently pass Config cf.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At least within the scope of this PR it seems like there are only 2 places that actually care about the mesh mode stuff, and in both cases they didn't previous require the entire Config object to be initialized. So it doesn't seem like we're saving much by pushing the mesh-mode stuff into the config object and it just exposes additional API on the Config.

Copy link
Contributor Author

@iamdanfox iamdanfox Nov 9, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Config is package private, so the only people who will be affected by this decision will be us / future maintainers of dialogue.

Back in april I found dialogue channel had grown to become really hard to read, so condensed all of the 'inputs' into this one Config object. That enables the pattern where Channels are created with this convention:

HostMetricsChannel.create(cf, channel, uri)
QueuedChannel.create(cf, endpoint, limited)
DeprecationWarningChannel.create(cf, channel, endpoint);
TimingEndpointChannel.create(cf, channel, endpoint);

In each of these cases, Config cf is only plumbed to the static factory method, so the actual Channel's constructor has only the bits it really needs (e.g. a clock or a channelname string etc), which makes it convenient to supply only exactly what you need for unit-testing.

It might seem minor, but i do feel kinda strongly about this... I think this convention helps us minimize plumbing, keeping the top level DialogueChannel as concise as possible so that you can follow which channels are wired into which other channels without getting distracted.

return MeshMode.fromUris(rawConfig().uris(), SafeArg.of("channelName", channelName()));
}

@Value.Default
default Random random() {
return SafeThreadLocalRandom.get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public DialogueChannel build() {
Channel tracingChannel = new TraceEnrichingChannel(channel);
final int uriIndexForInstrumentation =
cf.overrideSingleHostIndex().orElse(uriIndex);
channel = cf.clientConf().clientQoS() == ClientQoS.ENABLED
channel = cf.clientConf().clientQoS() == ClientQoS.ENABLED && cf.mesh() != MeshMode.USE_EXTERNAL_MESH
? new ChannelToEndpointChannel(endpoint -> {
LimitedChannel limited = ConcurrencyLimitedChannel.createForEndpoint(
tracingChannel, cf.channelName(), uriIndexForInstrumentation, endpoint);
Expand All @@ -150,13 +150,13 @@ public DialogueChannel build() {

EndpointChannelFactory channelFactory = endpoint -> {
EndpointChannel channel = new EndpointChannelAdapter(endpoint, queuedChannel);
channel = TracedChannel.requestAttempt(channel);
channel = TracedChannel.requestAttempt(cf, channel);
channel = RetryingChannel.create(cf, channel, endpoint);
channel = UserAgentEndpointChannel.create(
channel, endpoint, cf.clientConf().userAgent().get());
channel = DeprecationWarningChannel.create(cf, channel, endpoint);
channel = new ContentDecodingChannel(channel);
channel = TracedChannel.create(channel, endpoint);
channel = TracedChannel.create(cf, channel, endpoint);
channel = TimingEndpointChannel.create(cf, channel, endpoint);
channel = new InterruptionChannel(channel);
return new NeverThrowEndpointChannel(channel); // this must come last as a defensive backstop
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* (c) Copyright 2020 Palantir Technologies Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.palantir.dialogue.core;

import com.palantir.logsafe.Preconditions;
import com.palantir.logsafe.SafeArg;
import java.util.List;

enum MeshMode {
DEFAULT_NO_MESH,
USE_EXTERNAL_MESH;

/**
* This prefix may reconfigure several aspects of the client to work better in a world where requests are routed
* through a service mesh like istio/envoy.
*/
private static final String MESH_PREFIX = "mesh-";

static MeshMode fromUris(List<String> uris, SafeArg<String> channelName) {
long meshUris = uris.stream().filter(s -> s.startsWith(MESH_PREFIX)).count();
long normalUris = uris.stream().filter(s -> !s.startsWith(MESH_PREFIX)).count();

if (meshUris == 0) {
return MeshMode.DEFAULT_NO_MESH;
} else {
Preconditions.checkState(
meshUris == 1,
"Not expecting multiple 'mesh-' prefixed uris - please double-check the uris",
SafeArg.of("meshUris", meshUris),
SafeArg.of("normalUris", normalUris),
channelName);
Preconditions.checkState(
normalUris == 0,
"When a 'mesh-' prefixed uri is present, there should not be any normal uris - please double "
+ "check the uris",
SafeArg.of("meshUris", meshUris),
SafeArg.of("normalUris", normalUris),
channelName);

return MeshMode.USE_EXTERNAL_MESH;
}
}

public static String stripMeshPrefix(String input) {
return input.startsWith(MESH_PREFIX) ? input.substring(MESH_PREFIX.length()) : input;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@ static EndpointChannel create(Config cf, EndpointChannel channel, Endpoint endpo
return channel;
}

if (cf.mesh() == MeshMode.USE_EXTERNAL_MESH) {
log.debug(
"Disabling retrying channel due to MeshMode",
SafeArg.of("channel", cf.channelName()),
SafeArg.of("ignoredMaxNumRetries", clientConf.maxNumRetries()));
return channel;
}

return new RetryingChannel(
channel,
endpoint,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,18 @@ private TracedChannel(EndpointChannel delegate, String operationName) {
this.operationNameInitial = operationName + " initial";
}

static EndpointChannel create(EndpointChannel delegate, Endpoint endpoint) {
String operationName = "Dialogue: request " + endpoint.serviceName() + "#" + endpoint.endpointName();
static EndpointChannel create(Config cf, EndpointChannel delegate, Endpoint endpoint) {
String operationName =
"Dialogue: request " + endpoint.serviceName() + "#" + endpoint.endpointName() + meshSuffix(cf.mesh());
return new TracedChannel(delegate, operationName);
}

static EndpointChannel requestAttempt(EndpointChannel delegate) {
return new TracedChannel(delegate, "Dialogue-request-attempt");
static EndpointChannel requestAttempt(Config cf, EndpointChannel delegate) {
return new TracedChannel(delegate, "Dialogue-request-attempt" + meshSuffix(cf.mesh()));
}

private static String meshSuffix(MeshMode meshMode) {
return meshMode == MeshMode.USE_EXTERNAL_MESH ? " (Mesh)" : "";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* (c) Copyright 2020 Palantir Technologies Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.palantir.dialogue.core;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import com.palantir.logsafe.SafeArg;
import com.palantir.logsafe.exceptions.SafeIllegalStateException;
import java.util.Arrays;
import java.util.stream.Collectors;
import org.junit.jupiter.api.Test;

class MeshModeTest {

@Test
void normal_uris_are_not_mesh() {
MeshMode mode = fromUris("https://server.whatever", "http://server.another");
assertThat(mode).isEqualTo(MeshMode.DEFAULT_NO_MESH);
}

@Test
void single_prefix_triggers_mesh_mode() {
MeshMode mode = fromUris("mesh-https://server.whatever");
assertThat(mode).isEqualTo(MeshMode.USE_EXTERNAL_MESH);
}

@Test
void empty_uris_doesnt_break() {
MeshMode mode = fromUris();
assertThat(mode).isEqualTo(MeshMode.DEFAULT_NO_MESH);
}

@Test
void mixed_uris_breaks() {
assertThatThrownBy(() -> fromUris("https://server.whatever", "mesh-http://server.another"))
.isInstanceOf(SafeIllegalStateException.class)
.hasMessage(
"When a 'mesh-' prefixed uri is present, there should not be any normal uris - please double "
+ "check the uris: "
+ "{meshUris=1, normalUris=1, channelName=channelName}");
}

@Test
void multiple_prefixes_breaks() {
assertThatThrownBy(() -> fromUris("mesh-https://server.whatever", "mesh-http://server.another"))
.isInstanceOf(SafeIllegalStateException.class)
.hasMessage("Not expecting multiple 'mesh-' prefixed uris - please double-check the uris: "
+ "{meshUris=2, normalUris=0, channelName=channelName}");
}

private MeshMode fromUris(String... uris) {
return MeshMode.fromUris(
Arrays.stream(uris).collect(Collectors.toList()), SafeArg.of("channelName", "channelName"));
}
}