Skip to content

Commit

Permalink
undo KeyValue
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewazores committed Aug 16, 2024
1 parent 4cfb601 commit 1663eba
Show file tree
Hide file tree
Showing 17 changed files with 207 additions and 276 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@
<com.google.java-format.version>1.23.0</com.google.java-format.version>
<com.mycila.license.maven.plugin.version>4.5</com.mycila.license.maven.plugin.version>
<surefire-plugin.version>3.3.1</surefire-plugin.version>
<surefire.rerunFailingTestsCount>2</surefire.rerunFailingTestsCount>
<failsafe-plugin.version>3.3.1</failsafe-plugin.version>
<!-- <surefire.rerunFailingTestsCount>2</surefire.rerunFailingTestsCount> -->
<!-- <failsafe.rerunFailingTestsCount>${surefire.rerunFailingTestsCount}</failsafe.rerunFailingTestsCount> -->
<failsafe.rerunFailingTestsCount>${surefire.rerunFailingTestsCount}</failsafe.rerunFailingTestsCount>
</properties>
<dependencyManagement>
<dependencies>
Expand Down
31 changes: 12 additions & 19 deletions schema/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ components:
Annotations:
properties:
cryostat:
items:
$ref: '#/components/schemas/KeyValue'
type: array
additionalProperties:
type: string
type: object
platform:
items:
$ref: '#/components/schemas/KeyValue'
type: array
additionalProperties:
type: string
type: object
type: object
ArchivedRecording:
properties:
Expand Down Expand Up @@ -66,9 +66,9 @@ components:
format: int64
type: integer
labels:
items:
$ref: '#/components/schemas/KeyValue'
type: array
additionalProperties:
type: string
type: object
name:
pattern: \S
type: string
Expand Down Expand Up @@ -125,13 +125,6 @@ components:
value: {}
type: object
type: array
KeyValue:
properties:
key:
type: string
value:
type: string
type: object
LinkedRecordingDescriptor:
properties:
continuous:
Expand Down Expand Up @@ -320,9 +313,9 @@ components:
jvmId:
type: string
labels:
items:
$ref: '#/components/schemas/KeyValue'
type: array
additionalProperties:
type: string
type: object
required:
- connectUrl
- alias
Expand Down
5 changes: 0 additions & 5 deletions schema/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,6 @@ type Entry_String_String {
value: String
}

type KeyValue {
key: String
value: String
}

type MBeanMetrics {
jvmId: String
memory: MemoryMetrics
Expand Down
83 changes: 83 additions & 0 deletions src/main/java/io/cryostat/ObjectMapperCustomization.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright The Cryostat Authors.
*
* 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 io.cryostat;

import java.io.IOException;
import java.util.Map;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.Version;
import com.fasterxml.jackson.databind.BeanDescription;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationConfig;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.BeanSerializerModifier;
import com.fasterxml.jackson.databind.type.MapType;
import io.quarkus.jackson.ObjectMapperCustomizer;
import jakarta.inject.Singleton;

@Singleton
public class ObjectMapperCustomization implements ObjectMapperCustomizer {

@Override
public void customize(ObjectMapper objectMapper) {
// FIXME get this version information from the maven build somehow
SimpleModule mapModule =
new SimpleModule(
"MapSerialization", new Version(3, 0, 0, null, "io.cryostat", "cryostat"));

mapModule.setSerializerModifier(new MapSerializerModifier());

objectMapper.registerModule(mapModule);
}

static class MapSerializerModifier extends BeanSerializerModifier {
@Override
public JsonSerializer<?> modifyMapSerializer(
SerializationConfig config,
MapType valueType,
BeanDescription beanDesc,
JsonSerializer serializer) {
if (valueType.getKeyType().getRawClass().equals(String.class)
&& valueType.getContentType().getRawClass().equals(String.class)) {
return new MapSerializer();
}
return serializer;
}
}

static class MapSerializer extends JsonSerializer<Map<?, ?>> {

@Override
public void serialize(Map<?, ?> map, JsonGenerator gen, SerializerProvider serializers)
throws IOException {
gen.writeStartArray();

for (var entry : map.entrySet()) {
gen.writeStartObject();

gen.writePOJOField("key", entry.getKey());
gen.writePOJOField("value", entry.getValue());

gen.writeEndObject();
}

gen.writeEndArray();
}
}
}
20 changes: 9 additions & 11 deletions src/main/java/io/cryostat/discovery/ContainerDiscovery.java
Original file line number Diff line number Diff line change
Expand Up @@ -280,18 +280,17 @@ private Target toTarget(ContainerSpec desc) {
target.activeRecordings = new ArrayList<>();
target.connectUrl = connectUrl;
target.alias = Optional.ofNullable(desc.Names.get(0)).orElse(desc.Id);
target.labels = KeyValue.listFromMap(desc.Labels);
target.labels = desc.Labels;
target.annotations =
new Annotations(
null,
KeyValue.listFromMap(
Map.of(
"REALM", // AnnotationKey.REALM,
getRealm(),
"HOST", // AnnotationKey.HOST,
hostname,
"PORT", // "AnnotationKey.PORT,
Integer.toString(jmxPort))));
Map.of(
"REALM", // AnnotationKey.REALM,
getRealm(),
"HOST", // AnnotationKey.HOST,
hostname,
"PORT", // "AnnotationKey.PORT,
Integer.toString(jmxPort)));

return target;
}
Expand All @@ -300,8 +299,7 @@ private boolean isTargetUnderRealm(URI connectUrl) throws IllegalStateException
// Check for any targets with the same connectUrl in other realms
try {
Target persistedTarget = Target.getTargetByConnectUrl(connectUrl);
String realmOfTarget =
KeyValue.mapFromList(persistedTarget.annotations.cryostat()).get("REALM");
String realmOfTarget = persistedTarget.annotations.cryostat().get("REALM");
if (!getRealm().equals(realmOfTarget)) {
logger.warnv(
"Expected persisted target with serviceURL {0} to be under realm"
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/io/cryostat/discovery/CustomDiscovery.java
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,7 @@ Response doV2Create(
credential.ifPresent(c -> c.persist());

target.activeRecordings = new ArrayList<>();
target.annotations =
new Annotations(null, KeyValue.listFromMap(Map.of("REALM", REALM)));
target.annotations = new Annotations(null, Map.of("REALM", REALM));

DiscoveryNode node = DiscoveryNode.target(target, BaseNodeType.JVM);
target.discoveryNode = node;
Expand Down
58 changes: 32 additions & 26 deletions src/main/java/io/cryostat/discovery/Discovery.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,32 +113,38 @@ void onStart(@Observes StartupEvent evt) {
() -> {
// ensure lazily initialized entries are created
DiscoveryNode.getUniverse();
});
DiscoveryPlugin.<DiscoveryPlugin>findAll().list().stream()
.filter(p -> !p.builtin)
.forEach(
plugin -> {
var dataMap = new JobDataMap();
dataMap.put(PLUGIN_ID_MAP_KEY, plugin.id);
dataMap.put(REFRESH_MAP_KEY, true);
JobDetail jobDetail =
JobBuilder.newJob(RefreshPluginJob.class)
.withIdentity(plugin.id.toString(), JOB_STARTUP)
.usingJobData(dataMap)
.build();
var trigger =
TriggerBuilder.newTrigger()
.usingJobData(jobDetail.getJobDataMap())
.startNow()
.withSchedule(
SimpleScheduleBuilder.simpleSchedule()
.withRepeatCount(0))
.build();
try {
scheduler.scheduleJob(jobDetail, trigger);
} catch (SchedulerException e) {
logger.warn("Failed to schedule plugin prune job", e);
}
DiscoveryPlugin.<DiscoveryPlugin>findAll().list().stream()
.filter(p -> !p.builtin)
.forEach(
plugin -> {
var dataMap = new JobDataMap();
dataMap.put(PLUGIN_ID_MAP_KEY, plugin.id);
dataMap.put(REFRESH_MAP_KEY, true);
JobDetail jobDetail =
JobBuilder.newJob(RefreshPluginJob.class)
.withIdentity(
plugin.id.toString(),
JOB_STARTUP)
.usingJobData(dataMap)
.build();
var trigger =
TriggerBuilder.newTrigger()
.usingJobData(
jobDetail.getJobDataMap())
.startNow()
.withSchedule(
SimpleScheduleBuilder
.simpleSchedule()
.withRepeatCount(0))
.build();
try {
scheduler.scheduleJob(jobDetail, trigger);
} catch (SchedulerException e) {
logger.warn(
"Failed to schedule plugin prune job",
e);
}
});
});
}

Expand Down
10 changes: 6 additions & 4 deletions src/main/java/io/cryostat/discovery/DiscoveryNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
package io.cryostat.discovery;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Predicate;
Expand Down Expand Up @@ -71,7 +73,7 @@ public class DiscoveryNode extends PanacheEntity {
@JdbcTypeCode(SqlTypes.JSON)
@NotNull
@JsonView(Views.Flat.class)
public List<KeyValue> labels = new ArrayList<>();
public Map<String, String> labels = new HashMap<>();

@OneToMany(fetch = FetchType.LAZY, orphanRemoval = true, mappedBy = "parent")
@JsonView(Views.Nested.class)
Expand Down Expand Up @@ -135,7 +137,7 @@ public static DiscoveryNode environment(String name, NodeType nodeType) {
DiscoveryNode node = new DiscoveryNode();
node.name = name;
node.nodeType = nodeType.getKind();
node.labels = new ArrayList<>();
node.labels = new HashMap<>();
node.children = new ArrayList<>();
node.target = null;
node.persist();
Expand All @@ -150,7 +152,7 @@ public static DiscoveryNode target(Target target, NodeType nodeType) {
DiscoveryNode node = new DiscoveryNode();
node.name = target.connectUrl.toString();
node.nodeType = nodeType.getKind();
node.labels = new ArrayList<>(target.labels);
node.labels = new HashMap<>(target.labels);
node.children = null;
node.target = target;
node.persist();
Expand Down Expand Up @@ -201,7 +203,7 @@ void prePersist(DiscoveryNode node) {
node.children = new ArrayList<>();
}
if (node.labels == null) {
node.labels = new ArrayList<>();
node.labels = new HashMap<>();
}
}

Expand Down
19 changes: 9 additions & 10 deletions src/main/java/io/cryostat/discovery/JDPDiscovery.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,16 +137,15 @@ void handleJdpEvent(JvmDiscoveryEvent evt) {
target.annotations =
new Annotations(
null,
KeyValue.listFromMap(
Map.of(
"REALM", // AnnotationKey.REALM,
REALM,
"JAVA_MAIN", // AnnotationKey.JAVA_MAIN,
evt.getJvmDescriptor().getMainClass(),
"HOST", // AnnotationKey.HOST,
rmiTarget.getHost(),
"PORT", // "AnnotationKey.PORT,
Integer.toString(rmiTarget.getPort()))));
Map.of(
"REALM", // AnnotationKey.REALM,
REALM,
"JAVA_MAIN", // AnnotationKey.JAVA_MAIN,
evt.getJvmDescriptor().getMainClass(),
"HOST", // AnnotationKey.HOST,
rmiTarget.getHost(),
"PORT", // "AnnotationKey.PORT,
Integer.toString(rmiTarget.getPort())));

DiscoveryNode node = DiscoveryNode.target(target, BaseNodeType.JVM);

Expand Down
Loading

0 comments on commit 1663eba

Please sign in to comment.