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

feat(svc): add recording start method using event template name and type #245

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
147 changes: 147 additions & 0 deletions src/main/java/io/cryostat/core/EventOptionsBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/*
* Copyright The Cryostat Authors
*
* The Universal Permissive License (UPL), Version 1.0
*
* Subject to the condition set forth below, permission is hereby granted to any
* person obtaining a copy of this software, associated documentation and/or data
* (collectively the "Software"), free of charge and under any and all copyright
* rights in the Software, and any and all patent rights owned or freely
* licensable by each licensor hereunder covering either (i) the unmodified
* Software as contributed to or provided by such licensor, or (ii) the Larger
* Works (as defined below), to deal in both
*
* (a) the Software, and
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
* one is included with the Software (each a "Larger Work" to which the Software
* is contributed by such licensors),
*
* without restriction, including without limitation the rights to copy, create
* derivative works of, display, perform, and distribute the Software and make,
* use, sell, offer for sale, import, export, have made, and have sold the
* Software and the Larger Work(s), and to sublicense the foregoing rights on
* either these or other terms.
*
* This license is subject to the following condition:
* The above copyright notice and either this complete permission notice or at
* a minimum a reference to the UPL must be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package io.cryostat.core;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Supplier;

import org.openjdk.jmc.common.unit.IConstrainedMap;
import org.openjdk.jmc.common.unit.IConstraint;
import org.openjdk.jmc.common.unit.IMutableConstrainedMap;
import org.openjdk.jmc.common.unit.IOptionDescriptor;
import org.openjdk.jmc.common.unit.QuantityConversionException;
import org.openjdk.jmc.flightrecorder.configuration.events.EventOptionID;
import org.openjdk.jmc.flightrecorder.configuration.events.IEventTypeID;
import org.openjdk.jmc.rjmx.IConnectionHandle;
import org.openjdk.jmc.rjmx.ServiceNotAvailableException;
import org.openjdk.jmc.rjmx.services.jfr.IEventTypeInfo;
import org.openjdk.jmc.rjmx.services.jfr.internal.FlightRecorderServiceV2;

import io.cryostat.core.net.JFRConnection;
import io.cryostat.core.tui.ClientWriter;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

public class EventOptionsBuilder {

private final boolean isV2;
private final IMutableConstrainedMap<EventOptionID> map;
private Map<IEventTypeID, Map<String, IOptionDescriptor<?>>> knownTypes;
private Map<String, IEventTypeID> eventIds;

public EventOptionsBuilder(ClientWriter cw, JFRConnection connection, Supplier<Boolean> v2)
throws ServiceNotAvailableException, IOException,
org.openjdk.jmc.rjmx.services.jfr.FlightRecorderException {
this.isV2 = v2.get();
this.map = connection.getService().getDefaultEventOptions().emptyWithSameConstraints();
knownTypes = new HashMap<>();
eventIds = new HashMap<>();

if (!isV2) {
cw.println("Flight Recorder V1 is not supported");
tthvo marked this conversation as resolved.
Show resolved Hide resolved
}

for (IEventTypeInfo eventTypeInfo : connection.getService().getAvailableEventTypes()) {
eventIds.put(
eventTypeInfo.getEventTypeID().getFullKey(), eventTypeInfo.getEventTypeID());
knownTypes.putIfAbsent(
eventTypeInfo.getEventTypeID(),
new HashMap<>(eventTypeInfo.getOptionDescriptors()));
}
}

public EventOptionsBuilder addEvent(String typeId, String option, String value)
throws EventTypeException, EventOptionException, QuantityConversionException {
if (!eventIds.containsKey(typeId)) {
throw new EventTypeException(typeId);
}
Map<String, IOptionDescriptor<?>> optionDescriptors = knownTypes.get(eventIds.get(typeId));
if (!optionDescriptors.containsKey(option)) {
throw new EventOptionException(typeId, option);
}
IConstraint<?> constraint = optionDescriptors.get(option).getConstraint();
Object parsedValue = constraint.parseInteractive(value);
constraint.validate(capture(parsedValue));
this.map.put(new EventOptionID(eventIds.get(typeId), option), parsedValue);

return this;
}

static <T, V> V capture(T t) {
// TODO clean up this generics hack
return (V) t;
}

@SuppressFBWarnings(value = "EI_EXPOSE_REP", justification = "Field is never mutated")
public IConstrainedMap<EventOptionID> build() {
if (!isV2) {
return null;
}
return map;
}

public static class EventTypeException extends Exception {
EventTypeException(String eventType) {
super(String.format("Unknown event type \"%s\"", eventType));
}
}

public static class EventOptionException extends Exception {
EventOptionException(String eventType, String option) {
super(String.format("Unknown option \"%s\" for event \"%s\"", option, eventType));
}
}

public static class Factory {
private final ClientWriter cw;

public Factory(ClientWriter cw) {
this.cw = cw;
}

public EventOptionsBuilder create(JFRConnection connection)
throws IOException, ServiceNotAvailableException,
org.openjdk.jmc.rjmx.services.jfr.FlightRecorderException {
IConnectionHandle handle = connection.getHandle();
return new EventOptionsBuilder(
cw, connection, () -> FlightRecorderServiceV2.isAvailable(handle));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Copyright The Cryostat Authors
*
* The Universal Permissive License (UPL), Version 1.0
*
* Subject to the condition set forth below, permission is hereby granted to any
* person obtaining a copy of this software, associated documentation and/or data
* (collectively the "Software"), free of charge and under any and all copyright
* rights in the Software, and any and all patent rights owned or freely
* licensable by each licensor hereunder covering either (i) the unmodified
* Software as contributed to or provided by such licensor, or (ii) the Larger
* Works (as defined below), to deal in both
*
* (a) the Software, and
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
* one is included with the Software (each a "Larger Work" to which the Software
* is contributed by such licensors),
*
* without restriction, including without limitation the rights to copy, create
* derivative works of, display, perform, and distribute the Software and make,
* use, sell, offer for sale, import, export, have made, and have sold the
* Software and the Larger Work(s), and to sublicense the foregoing rights on
* either these or other terms.
*
* This license is subject to the following condition:
* The above copyright notice and either this complete permission notice or at
* a minimum a reference to the UPL must be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package io.cryostat.core.net;

import java.io.IOException;
import java.text.ParseException;

import org.openjdk.jmc.common.unit.IConstrainedMap;
import org.openjdk.jmc.common.unit.QuantityConversionException;
import org.openjdk.jmc.flightrecorder.configuration.events.EventOptionID;
import org.openjdk.jmc.flightrecorder.controlpanel.ui.configuration.model.xml.XMLModel;
import org.openjdk.jmc.flightrecorder.controlpanel.ui.model.EventConfiguration;
import org.openjdk.jmc.rjmx.ConnectionException;
import org.openjdk.jmc.rjmx.ServiceNotAvailableException;
import org.openjdk.jmc.rjmx.services.jfr.FlightRecorderException;
import org.openjdk.jmc.rjmx.services.jfr.IFlightRecorderService;
import org.openjdk.jmc.rjmx.services.jfr.IRecordingDescriptor;

import io.cryostat.core.EventOptionsBuilder.EventOptionException;
import io.cryostat.core.EventOptionsBuilder.EventTypeException;
import io.cryostat.core.templates.Template;
import io.cryostat.core.templates.TemplateType;

import org.jsoup.nodes.Document;

public interface CryostatFlightRecorderService extends IFlightRecorderService {

IRecordingDescriptor start(
IConstrainedMap<String> recordingOptions,
String templateName,
TemplateType preferredTemplateType)
throws io.cryostat.core.FlightRecorderException, FlightRecorderException,
ConnectionException, IOException, FlightRecorderException,
ServiceNotAvailableException, QuantityConversionException, EventOptionException,
EventTypeException;

default IRecordingDescriptor start(
IConstrainedMap<String> recordingOptions, Template eventTemplate)
throws io.cryostat.core.FlightRecorderException, FlightRecorderException,
ConnectionException, IOException, FlightRecorderException,
ServiceNotAvailableException, QuantityConversionException, EventOptionException,
EventTypeException {
return start(recordingOptions, eventTemplate.getName(), eventTemplate.getType());
}

default IRecordingDescriptor start(IConstrainedMap<String> recordingOptions, Document template)
throws FlightRecorderException, ParseException, IOException {
XMLModel model = EventConfiguration.createModel(template.toString());
IConstrainedMap<EventOptionID> eventOptions =
new EventConfiguration(model)
.getEventOptions(getDefaultEventOptions().emptyWithSameConstraints());
return start(recordingOptions, eventOptions);
}
}
3 changes: 1 addition & 2 deletions src/main/java/io/cryostat/core/net/JFRConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
import org.openjdk.jmc.rjmx.ConnectionException;
import org.openjdk.jmc.rjmx.IConnectionHandle;
import org.openjdk.jmc.rjmx.ServiceNotAvailableException;
import org.openjdk.jmc.rjmx.services.jfr.IFlightRecorderService;

import io.cryostat.core.sys.Clock;
import io.cryostat.core.templates.TemplateService;
Expand All @@ -56,7 +55,7 @@ public interface JFRConnection extends AutoCloseable {

public IConnectionHandle getHandle() throws ConnectionException, IOException;

public IFlightRecorderService getService()
public CryostatFlightRecorderService getService()
throws ConnectionException, IOException, ServiceNotAvailableException;

public TemplateService getTemplateService();
Expand Down
15 changes: 2 additions & 13 deletions src/main/java/io/cryostat/core/net/JFRJMXConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@
import org.openjdk.jmc.rjmx.internal.DefaultConnectionHandle;
import org.openjdk.jmc.rjmx.internal.RJMXConnection;
import org.openjdk.jmc.rjmx.internal.ServerDescriptor;
import org.openjdk.jmc.rjmx.services.jfr.IFlightRecorderService;
import org.openjdk.jmc.rjmx.services.jfr.internal.FlightRecorderServiceFactory;
import org.openjdk.jmc.rjmx.services.jfr.internal.FlightRecorderServiceV2;
import org.openjdk.jmc.rjmx.subscription.MRI;
Expand Down Expand Up @@ -132,19 +131,9 @@ public synchronized IConnectionHandle getHandle() throws ConnectionException, IO
return handle;
}

public synchronized IFlightRecorderService getService()
public synchronized CryostatFlightRecorderService getService()
throws ConnectionException, IOException, ServiceNotAvailableException {
if (!isConnected()) {
connect();
}
IFlightRecorderService service = serviceFactory.getServiceInstance(getHandle());
if (service == null || !isConnected()) {
throw new ConnectionException(
String.format(
"Could not connect to remote target %s",
this.connectionDescriptor.createJMXServiceURL().toString()));
}
return service;
return new JmxFlightRecorderService(this, cw);
}

public TemplateService getTemplateService() {
Expand Down
Loading
Loading