This repository has been archived by the owner on Nov 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Conjure Java Runtime] Part 9: Version Selecting Clients (#4260)
* Change DDP to PSP * Version selecting client * Testing * Compile * Finish up VSC * warning * Oops * Add generated changelog entries * javadoc * CR comments (small part) * InstanceAndVersion * Client versions for target factories * Fix broken test * Hackery
- Loading branch information
1 parent
62b7b6b
commit a127f80
Showing
12 changed files
with
298 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
atlasdb-commons/src/test/java/com/palantir/common/proxy/PredicateSwitchedProxyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* (c) Copyright 2019 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.common.proxy; | ||
|
||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.never; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
|
||
import java.util.concurrent.atomic.AtomicBoolean; | ||
import java.util.function.IntConsumer; | ||
|
||
import org.junit.Test; | ||
|
||
public class PredicateSwitchedProxyTest { | ||
private final IntConsumer decorated = mock(IntConsumer.class); | ||
private final IntConsumer delegate = mock(IntConsumer.class); | ||
|
||
private final AtomicBoolean atomicBoolean = new AtomicBoolean(false); | ||
|
||
private final IntConsumer consumer = PredicateSwitchedProxy.newProxyInstance( | ||
decorated, | ||
delegate, | ||
atomicBoolean::get, | ||
IntConsumer.class); | ||
|
||
@Test | ||
public void dynamicallySwitchesCorrectlyForMethodsWithArguments() throws Exception { | ||
consumer.accept(10); | ||
verify(decorated, never()).accept(10); | ||
verify(delegate, times(1)).accept(10); | ||
|
||
atomicBoolean.set(true); | ||
consumer.accept(20); | ||
verify(decorated, times(1)).accept(20); | ||
verify(delegate, never()).accept(20); | ||
|
||
atomicBoolean.set(false); | ||
consumer.accept(30); | ||
verify(decorated, never()).accept(30); | ||
verify(delegate, times(1)).accept(30); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
atlasdb-config/src/main/java/com/palantir/atlasdb/http/VersionSelectingClients.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* (c) Copyright 2019 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.atlasdb.http; | ||
|
||
import java.util.concurrent.ThreadLocalRandom; | ||
import java.util.function.DoubleSupplier; | ||
|
||
import org.immutables.value.Value; | ||
|
||
import com.codahale.metrics.MetricRegistry; | ||
import com.google.common.collect.ImmutableMap; | ||
import com.palantir.atlasdb.util.AtlasDbMetrics; | ||
import com.palantir.common.proxy.PredicateSwitchedProxy; | ||
import com.palantir.tritium.metrics.registry.TaggedMetricRegistry; | ||
|
||
/** | ||
* Factory for randomly selecting from a new and a legacy version of a client, based on a live-reloading probability. | ||
* | ||
* Please note that your clients will run independently; if there are stateful invariants that need to be enforced | ||
* across individual clients, you may need to share state appropriately. | ||
*/ | ||
final class VersionSelectingClients { | ||
private static final String CLIENT_VERSION = "clientVersion"; | ||
|
||
private VersionSelectingClients() { | ||
// No, nein, etc. | ||
} | ||
|
||
static <T> T createVersionSelectingClient( | ||
TaggedMetricRegistry taggedMetricRegistry, | ||
InstanceAndVersion<T> newClient, | ||
InstanceAndVersion<T> legacyClient, | ||
DoubleSupplier newClientProbabilitySupplier, | ||
Class<T> clazz) { | ||
T instrumentedNewClient = instrumentWithClientVersionTag( | ||
taggedMetricRegistry, newClient, clazz); | ||
T instrumentedLegacyClient = instrumentWithClientVersionTag( | ||
taggedMetricRegistry, legacyClient, clazz); | ||
|
||
return PredicateSwitchedProxy.newProxyInstance( | ||
instrumentedNewClient, | ||
instrumentedLegacyClient, | ||
() -> ThreadLocalRandom.current().nextDouble() < newClientProbabilitySupplier.getAsDouble(), | ||
clazz); | ||
} | ||
|
||
private static <T> T instrumentWithClientVersionTag( | ||
TaggedMetricRegistry taggedMetricRegistry, | ||
InstanceAndVersion<T> client, | ||
Class<T> clazz) { | ||
return AtlasDbMetrics.instrumentWithTaggedMetrics( | ||
taggedMetricRegistry, | ||
clazz, | ||
client.client(), | ||
MetricRegistry.name(clazz), | ||
$ -> ImmutableMap.of(CLIENT_VERSION, client.version())); | ||
} | ||
|
||
@Value.Immutable | ||
interface InstanceAndVersion<T> { | ||
@Value.Parameter | ||
T client(); | ||
|
||
@Value.Parameter | ||
String version(); | ||
} | ||
} |
74 changes: 0 additions & 74 deletions
74
atlasdb-config/src/test/java/com/palantir/atlasdb/factory/DynamicDecoratingProxyTest.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.