aboutsummaryrefslogtreecommitdiffstats
path: root/component
diff options
context:
space:
mode:
authorJon Bratseth <bratseth@gmail.com>2021-10-29 14:41:55 +0200
committerJon Bratseth <bratseth@gmail.com>2021-10-29 14:41:55 +0200
commit91d9fdf134e51322d57d1634253ded11a5ad9f48 (patch)
tree30deb780c81b0148f56d4d2827c43e5070faf4fd /component
parent2b392be81f7a63d0f2e17c48ac365e4e55bb8a7c (diff)
Stable query profile ids
Diffstat (limited to 'component')
-rw-r--r--component/src/main/java/com/yahoo/component/AbstractComponent.java2
-rw-r--r--component/src/main/java/com/yahoo/component/ComponentId.java94
-rw-r--r--component/src/main/java/com/yahoo/component/provider/ComponentRegistry.java4
3 files changed, 55 insertions, 45 deletions
diff --git a/component/src/main/java/com/yahoo/component/AbstractComponent.java b/component/src/main/java/com/yahoo/component/AbstractComponent.java
index 254ef0e5db9..3fbae31a743 100644
--- a/component/src/main/java/com/yahoo/component/AbstractComponent.java
+++ b/component/src/main/java/com/yahoo/component/AbstractComponent.java
@@ -60,7 +60,7 @@ public class AbstractComponent implements Component, Deconstructable {
return id;
}
- //This should only happen in tests, so thread safety should not be an issue.
+ // This should only happen in tests, so thread safety should not be an issue.
private void setTestId() {
id = ComponentId.createAnonymousComponentId("test_" + getClass().getName());
}
diff --git a/component/src/main/java/com/yahoo/component/ComponentId.java b/component/src/main/java/com/yahoo/component/ComponentId.java
index a19c70895ba..7320d24b57c 100644
--- a/component/src/main/java/com/yahoo/component/ComponentId.java
+++ b/component/src/main/java/com/yahoo/component/ComponentId.java
@@ -13,31 +13,17 @@ import java.util.concurrent.atomic.AtomicInteger;
*/
public final class ComponentId implements Comparable<ComponentId> {
- private final class VersionHandler implements Spec.VersionHandler<Version> {
-
- @Override
- public Version emptyVersion() {
- return Version.emptyVersion;
- }
-
- @Override
- public int compare(Version v1, Version v2) {
- return v1.compareTo(v2);
- }
- }
-
private final Spec<Version> spec;
private final boolean anonymous;
- private final static class Counter {
- private int count = 0;
- public int getAndIncrement() { return count++; }
- }
+
+ private static AtomicInteger threadIdCounter = new AtomicInteger(0);
+
private static ThreadLocal<Counter> threadLocalUniqueId = new ThreadLocal<Counter>() {
@Override protected Counter initialValue() {
return new Counter();
}
};
- private static AtomicInteger threadIdCounter = new AtomicInteger(0);
+
private static ThreadLocal<String> threadId = new ThreadLocal<String>() {
@Override protected String initialValue() {
return new String("_" + threadIdCounter.getAndIncrement() + "_");
@@ -47,18 +33,17 @@ public final class ComponentId implements Comparable<ComponentId> {
/** Precomputed string value */
private final String stringValue;
- private ComponentId(String name, Version version, ComponentId namespace, boolean anonymous) {
- if (anonymous) {
- name = createAnonymousName(name);
- }
- spec = new Spec<>(new VersionHandler(), name, version, namespace);
- this.anonymous = anonymous;
-
- stringValue = spec.createStringValue();
+ /**
+ * Creates a component id from the id string form: name(:version)?(@namespace)?,
+ * where version has the form 1(.2(.3(.identifier)?)?)?
+ * and namespace is a component id
+ */
+ public ComponentId(String id) {
+ this(new SpecSplitter(id));
}
- private String createAnonymousName(String name) {
- return new StringBuilder(name).append(threadId.get()).append(threadLocalUniqueId.get().getAndIncrement()).toString();
+ private ComponentId(SpecSplitter splitter) {
+ this(splitter.name, Version.fromString(splitter.version), splitter.namespace);
}
public ComponentId(String name, Version version, ComponentId namespace) {
@@ -70,17 +55,10 @@ public final class ComponentId implements Comparable<ComponentId> {
this(name, version, null);
}
- /**
- * Creates a component id from the id string form: name(:version)?(@namespace)?,
- * where version has the form 1(.2(.3(.identifier)?)?)?
- * and namespace is a component id
- */
- public ComponentId(String id) {
- this(new SpecSplitter(id));
- }
-
- private ComponentId(SpecSplitter splitter) {
- this(splitter.name, Version.fromString(splitter.version), splitter.namespace);
+ private ComponentId(String id, Version version, ComponentId namespace, boolean anonymous) {
+ this.spec = new Spec<>(new VersionHandler(), id, version, namespace);
+ this.anonymous = anonymous;
+ this.stringValue = spec.createStringValue();
}
public ComponentId nestInNamespace(ComponentId namespace) {
@@ -116,12 +94,13 @@ public final class ComponentId implements Comparable<ComponentId> {
return spec.toString();
}
+ @Override
public boolean equals(Object o) {
- if (o==this) return true;
+ if (o == this) return true;
if ( ! (o instanceof ComponentId)) return false;
ComponentId c = (ComponentId) o;
- if (isAnonymous() || c.isAnonymous())
+ if (isAnonymous() || c.isAnonymous()) // TODO: Stop doing this
return false;
return c.stringValue().equals(stringValue);
@@ -150,7 +129,7 @@ public final class ComponentId implements Comparable<ComponentId> {
/** Creates a componentId that is unique for this run-time instance */
public static ComponentId createAnonymousComponentId(String baseName) {
- return new ComponentId(baseName, null, null, true);
+ return new ComponentId(createAnonymousId(baseName), null, null, true);
}
public boolean isAnonymous() {
@@ -232,4 +211,35 @@ public final class ComponentId implements Comparable<ComponentId> {
threadLocalUniqueId.set(new Counter());
}
+ private static String createAnonymousId(String name) {
+ return name + threadId.get() + threadLocalUniqueId.get().getAndIncrement();
+ }
+
+ /** Creates a component id with the given value, marked as anonymous */
+ public static ComponentId newAnonymous(String spec) {
+ var splitter = new SpecSplitter(spec);
+ return new ComponentId(splitter.name, Version.fromString(splitter.version), splitter.namespace, true);
+ }
+
+ private final class VersionHandler implements Spec.VersionHandler<Version> {
+
+ @Override
+ public Version emptyVersion() {
+ return Version.emptyVersion;
+ }
+
+ @Override
+ public int compare(Version v1, Version v2) {
+ return v1.compareTo(v2);
+ }
+
+ }
+
+ private final static class Counter {
+
+ private int count = 0;
+ public int getAndIncrement() { return count++; }
+
+ }
+
}
diff --git a/component/src/main/java/com/yahoo/component/provider/ComponentRegistry.java b/component/src/main/java/com/yahoo/component/provider/ComponentRegistry.java
index 9aa677737d6..ec6f21e4f53 100644
--- a/component/src/main/java/com/yahoo/component/provider/ComponentRegistry.java
+++ b/component/src/main/java/com/yahoo/component/provider/ComponentRegistry.java
@@ -28,10 +28,10 @@ import java.util.Set;
public class ComponentRegistry<COMPONENT> {
/** All versions of all components, indexed by name and namespace */
- private Map<ComponentId, Map<String, Map<Version, COMPONENT>>> componentsByNameByNamespace = new LinkedHashMap<>();
+ private final Map<ComponentId, Map<String, Map<Version, COMPONENT>>> componentsByNameByNamespace = new LinkedHashMap<>();
/** All versions of all components indexed by id */
- private Map<ComponentId, COMPONENT> componentsById =new LinkedHashMap<>();
+ private final Map<ComponentId, COMPONENT> componentsById =new LinkedHashMap<>();
/** True when this cannot be changed any more */
private boolean frozen = false;