Skip to content

Commit

Permalink
Enable the IDE to know the channel it was built from at runtime.
Browse files Browse the repository at this point in the history
Use this in `ExperimentServiceImpl` to check for channel specific experiment overrides, i.e. when checking for `some.experiment` we will also check:
- `stable.some.experiment` in the stable channel
- `beta.some.experiment` in the beta channel
- `canary.some.experiment` in the canary channel.

PiperOrigin-RevId: 586273187
  • Loading branch information
Googler authored and copybara-github committed Nov 29, 2023
1 parent 1f8dfde commit d3c0a3f
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 8 deletions.
11 changes: 6 additions & 5 deletions common/experiments/BUILD
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
load("//build_defs:build_defs.bzl", "intellij_plugin_library")
load(
"//testing:test_defs.bzl",
"intellij_unit_test_suite",
)
load(
"//:build-visibility.bzl",
"COMMON_PLUGINS_VISIBILITY",
"G3PLUGINS_VISIBILITY",
"SERVICES_EXPERIMENT_SUBPACKAGES",
)
load("//build_defs:build_defs.bzl", "intellij_plugin_library")
load(
"//testing:test_defs.bzl",
"intellij_unit_test_suite",
)

licenses(["notice"])

Expand All @@ -18,6 +18,7 @@ java_library(
resources = [":experiment_properties"],
visibility = COMMON_PLUGINS_VISIBILITY,
deps = [
"//common/util:platform",
"//intellij_platform_sdk:jsr305",
"//intellij_platform_sdk:plugin_api",
"//third_party/auto_value",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.idea.common.util.MorePlatformUtils;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.components.ApplicationComponent;
import com.intellij.openapi.diagnostic.Logger;
Expand All @@ -28,6 +29,7 @@
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import javax.annotation.Nullable;

Expand All @@ -46,19 +48,26 @@ public class ExperimentServiceImpl implements ApplicationComponent, ExperimentSe
private final Alarm alarm =
new Alarm(ThreadToUse.POOLED_THREAD, ApplicationManager.getApplication());
private final List<ExperimentLoader> services;
private final Supplier<String> channelSupplier;
private final AtomicInteger experimentScopeCounter = new AtomicInteger(0);

private volatile Map<String, String> experiments = ImmutableMap.of();
private volatile Map<String, List<ExperimentValue>> overrides = ImmutableMap.of();
private final Map<String, Experiment> queriedExperiments = new ConcurrentHashMap<>();

ExperimentServiceImpl() {
this(ExperimentLoader.EP_NAME.getExtensions());
this(MorePlatformUtils::getIdeChannel, ExperimentLoader.EP_NAME.getExtensions());
}

@VisibleForTesting
ExperimentServiceImpl(ExperimentLoader... loaders) {
this(MorePlatformUtils::getIdeChannel, loaders);
}

@VisibleForTesting
ExperimentServiceImpl(Supplier<String> channelSupplier, ExperimentLoader... loaders) {
services = ImmutableList.copyOf(loaders);
this.channelSupplier = channelSupplier;
if (ApplicationManager.getApplication().isUnitTestMode()) {
refreshExperiments();
}
Expand All @@ -76,9 +85,17 @@ public void initComponent() {
scheduleRefresh(REFRESH_FREQUENCY);
}

@Nullable
private String getExperiment(Experiment experiment) {
queriedExperiments.putIfAbsent(experiment.getKey(), experiment);
return experiments.get(experiment.getKey());
if (experiments.containsKey(experiment.getKey())) {
return experiments.get(experiment.getKey());
}
String channelKey = channelSupplier.get() + "." + experiment.getKey();
if (experiments.containsKey(channelKey)) {
return experiments.get(channelKey);
}
return null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,19 @@ public void testStringProperty() {
ExperimentService experimentService =
new ExperimentServiceImpl(new MapExperimentLoader("id", STRING_EXPERIMENT.getKey(), "hi"));
assertThat(experimentService.getExperimentString(STRING_EXPERIMENT, null)).isEqualTo("hi");
assertThat(experimentService.getAllQueriedExperiments())
.containsExactly(STRING_EXPERIMENT.getKey(), STRING_EXPERIMENT);
}

@Test
public void testChannelProperty() {
ExperimentService experimentService =
new ExperimentServiceImpl(
() -> "beta",
new MapExperimentLoader("id", "beta." + STRING_EXPERIMENT.getKey(), "hi"));
assertThat(experimentService.getExperimentString(STRING_EXPERIMENT, null)).isEqualTo("hi");
assertThat(experimentService.getAllQueriedExperiments())
.containsExactly(STRING_EXPERIMENT.getKey(), STRING_EXPERIMENT);
}

@Test
Expand Down
74 changes: 73 additions & 1 deletion common/util/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ java_library(

java_library(
name = "platform",
srcs = ["src/com/google/idea/common/util/MorePlatformUtils.java"],
srcs = [
"src/com/google/idea/common/util/MorePlatformUtils.java",
":channel_java",
],
deps = [
"//intellij_platform_sdk:plugin_api",
],
Expand Down Expand Up @@ -56,3 +59,72 @@ java_test(
"@junit//jar",
],
)

genrule(
name = "stable",
outs = ["stable.txt"],
cmd = "echo stable > $@",
)

genrule(
name = "beta",
outs = ["beta.txt"],
cmd = "echo beta > $@",
)

genrule(
name = "canary",
outs = ["canary.txt"],
cmd = "echo canary > $@",
)

genrule(
name = "channel_java",
srcs = select(
{
"//intellij_platform_sdk:intellij-latest": [":stable"],
"//intellij_platform_sdk:intellij-latest-mac": [":stable"],
"//intellij_platform_sdk:intellij-beta": [":beta"],
"//intellij_platform_sdk:intellij-under-dev": [":canary"],
"//intellij_platform_sdk:intellij-oss-latest-stable": [":stable"],
"//intellij_platform_sdk:intellij-oss-oldest-stable": [":stable"],
"//intellij_platform_sdk:intellij-oss-under-dev": [":canary"],
"//intellij_platform_sdk:intellij-ue-latest": [":stable"],
"//intellij_platform_sdk:intellij-ue-latest-mac": [":stable"],
"//intellij_platform_sdk:intellij-ue-beta": [":beta"],
"//intellij_platform_sdk:intellij-ue-oss-latest-stable": [":stable"],
"//intellij_platform_sdk:intellij-ue-oss-oldest-stable": [":stable"],
"//intellij_platform_sdk:intellij-ue-oss-under-dev": [":canary"],
"//intellij_platform_sdk:intellij-ue-cc-latest-stable": [":stable"],
"//intellij_platform_sdk:intellij-ue-cc-oldest-stable": [":stable"],
"//intellij_platform_sdk:intellij-ue-cc-under-dev": [":canary"],
"//intellij_platform_sdk:clion-latest": [":stable"],
"//intellij_platform_sdk:clion-latest-mac": [":stable"],
"//intellij_platform_sdk:clion-beta": [":beta"],
"//intellij_platform_sdk:clion-under-dev": [":canary"],
"//intellij_platform_sdk:clion-oss-latest-stable": [":stable"],
"//intellij_platform_sdk:clion-oss-oldest-stable": [":stable"],
"//intellij_platform_sdk:clion-oss-under-dev": [":canary"],
"//intellij_platform_sdk:android-studio-latest": [":stable"],
"//intellij_platform_sdk:android-studio-beta": [":beta"],
"//intellij_platform_sdk:android-studio-canary": [":canary"],
"//intellij_platform_sdk:android-studio-latest-mac": [":stable"],
"//intellij_platform_sdk:android-studio-beta-mac": [":beta"],
"//intellij_platform_sdk:android-studio-canary-mac": [":canary"],
"//intellij_platform_sdk:android-studio-oss-oldest-stable": [":stable"],
"//intellij_platform_sdk:android-studio-oss-latest-stable": [":stable"],
"//intellij_platform_sdk:android-studio-oss-under-dev": [":canary"],
},
),
outs = ["com/google/idea/common/util/channel/Channel.java"],
cmd = "\n".join([
"CHANNEL=$$(cat $<)",
"cat << EOM > $@",
"package com.google.idea.common.util.channel;",
"",
"public class Channel {",
" public static final String CHANNEL = \"$$CHANNEL\";",
"}",
"EOM",
]),
)
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.google.idea.common.util;

import com.google.idea.common.util.channel.Channel;
import com.intellij.util.PlatformUtils;

/** Additional helpers for {@link PlatformUtils}. */
Expand All @@ -41,4 +42,12 @@ public static String getProductIdForLogs() {
public static boolean isAndroidStudio() {
return "AndroidStudio".equals(PlatformUtils.getPlatformPrefix());
}

/**
* Returns the channel that the IDE was built from, either {@code stable}, {@code beta} or {@code
* canary}.
*/
public static String getIdeChannel() {
return Channel.CHANNEL;
}
}

0 comments on commit d3c0a3f

Please sign in to comment.