Skip to content

Commit d2fb0a0

Browse files
authored
fix(recording): backwards compatibility for restart parameter (#1672)
Signed-off-by: Thuan Vo <thuan.votann@gmail.com>
1 parent bf4cfb7 commit d2fb0a0

File tree

6 files changed

+240
-266
lines changed

6 files changed

+240
-266
lines changed

docs/HTTP_API.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -861,6 +861,10 @@
861861
862862
**The request may include the following fields:**
863863
864+
`restart`: Whether to restart the recording if one already exists with the same name. **DEPRECATED**: See `replace` below.
865+
866+
`replace`: The replacement policy if a recording already exists with the same name. Policies can be `ALWAYS` (i.e. `restart=true`), `NEVER` (i.e.`restart=false`), and `STOPPED` (restart only when the existing one is stopped).
867+
864868
`duration` - The duration of the recording, in seconds.
865869
If this field is not set, or if it is set to zero,
866870
the recording will be continuous,

src/main/java/io/cryostat/net/web/http/api/v1/TargetRecordingsPostHandler.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,18 @@ public void handleAuthenticated(RoutingContext ctx) throws Exception {
153153
.create(connection.getService())
154154
.name(recordingName);
155155

156-
String replace = attrs.get("replace");
157-
ReplacementPolicy replacementPolicy =
158-
ReplacementPolicy.fromString(replace);
156+
ReplacementPolicy replace = ReplacementPolicy.NEVER;
157+
158+
if (attrs.contains("restart")) {
159+
replace =
160+
Boolean.parseBoolean(attrs.get("restart"))
161+
? ReplacementPolicy.ALWAYS
162+
: ReplacementPolicy.NEVER;
163+
}
164+
165+
if (attrs.contains("replace")) {
166+
replace = ReplacementPolicy.fromString(attrs.get("replace"));
167+
}
159168

160169
if (attrs.contains("duration")) {
161170
builder =
@@ -201,7 +210,7 @@ public void handleAuthenticated(RoutingContext ctx) throws Exception {
201210
eventSpecifier);
202211
IRecordingDescriptor descriptor =
203212
recordingTargetHelper.startRecording(
204-
replacementPolicy,
213+
replace,
205214
connectionDescriptor,
206215
builder.build(),
207216
template.getLeft(),

src/main/java/io/cryostat/net/web/http/api/v2/graph/StartRecordingOnTargetMutator.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,15 +111,20 @@ public HyperlinkedSerializableRecordingDescriptor getAuthenticated(
111111
return targetConnectionManager.executeConnectedTask(
112112
cd,
113113
conn -> {
114-
ReplacementPolicy replace = ReplacementPolicy.NEVER;
115114
RecordingOptionsBuilder builder =
116115
recordingOptionsBuilderFactory
117116
.create(conn.getService())
118117
.name((String) settings.get("name"));
119118

119+
ReplacementPolicy replace = ReplacementPolicy.NEVER;
120+
if (settings.containsKey("restart")) {
121+
replace =
122+
Boolean.TRUE.equals(settings.get("restart"))
123+
? ReplacementPolicy.ALWAYS
124+
: ReplacementPolicy.NEVER;
125+
}
120126
if (settings.containsKey("replace")) {
121-
String replaceValue = (String) settings.get("replace");
122-
replace = ReplacementPolicy.fromString(replaceValue);
127+
replace = ReplacementPolicy.fromString((String) settings.get("replace"));
123128
}
124129
if (settings.containsKey("duration")) {
125130
builder =

src/main/resources/types.graphqls

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,16 +172,17 @@ type RecordingMetadata {
172172
}
173173

174174
input RecordingSettings {
175-
replace: ReplacementPolicy
176175
name: String!
177176
template: String!
178177
templateType: String!
179-
duration: Long
178+
replace: ReplacementPolicy
179+
restart: Boolean
180180
continuous: Boolean
181+
archiveOnStop: Boolean
181182
toDisk: Boolean
183+
duration: Long
182184
maxSize: Long
183185
maxAge: Long
184-
archiveOnStop: Boolean
185186
metadata: Object
186187
}
187188

src/test/java/io/cryostat/net/web/http/api/v1/TargetRecordingsPostHandlerTest.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
import org.junit.jupiter.api.Test;
6161
import org.junit.jupiter.api.extension.ExtendWith;
6262
import org.junit.jupiter.params.ParameterizedTest;
63+
import org.junit.jupiter.params.provider.Arguments;
6364
import org.junit.jupiter.params.provider.MethodSource;
6465
import org.mockito.ArgumentCaptor;
6566
import org.mockito.Mock;
@@ -258,8 +259,9 @@ void shouldStartRecording() throws Exception {
258259
"{\"downloadUrl\":\"example-download-url\",\"reportUrl\":\"example-report-url\",\"metadata\":{\"labels\":{}},\"archiveOnStop\":false,\"id\":1,\"name\":\"someRecording\",\"state\":\"STOPPED\",\"startTime\":0,\"duration\":0,\"continuous\":false,\"toDisk\":false,\"maxSize\":0,\"maxAge\":0}");
259260
}
260261

261-
@Test
262-
void shouldRestartRecording() throws Exception {
262+
@ParameterizedTest
263+
@MethodSource("getRestartOptions")
264+
void shouldRestartRecording(String restart, String replace) throws Exception {
263265
Mockito.when(auth.validateHttpHeader(Mockito.any(), Mockito.any()))
264266
.thenReturn(CompletableFuture.completedFuture(true));
265267

@@ -311,7 +313,12 @@ void shouldRestartRecording() throws Exception {
311313
resp.putHeader(
312314
Mockito.any(CharSequence.class), Mockito.any(CharSequence.class)))
313315
.thenReturn(resp);
314-
attrs.add("replace", "always");
316+
if (restart != null) {
317+
attrs.add("restart", restart);
318+
}
319+
if (replace != null) {
320+
attrs.add("replace", replace);
321+
}
315322
attrs.add("recordingName", "someRecording");
316323
attrs.add("events", "template=Foo");
317324

@@ -373,6 +380,15 @@ void shouldRestartRecording() throws Exception {
373380
"{\"downloadUrl\":\"example-download-url\",\"reportUrl\":\"example-report-url\",\"metadata\":{\"labels\":{}},\"archiveOnStop\":false,\"id\":1,\"name\":\"someRecording\",\"state\":\"STOPPED\",\"startTime\":0,\"duration\":0,\"continuous\":false,\"toDisk\":false,\"maxSize\":0,\"maxAge\":0}");
374381
}
375382

383+
private static Stream<Arguments> getRestartOptions() {
384+
return Stream.of(
385+
Arguments.of("true", null),
386+
Arguments.of(null, "always"),
387+
Arguments.of("true", "always"),
388+
Arguments.of("false", "always"),
389+
Arguments.of("anything", "always"));
390+
}
391+
376392
@Test
377393
void shouldHandleNameCollision() throws Exception {
378394
Mockito.when(auth.validateHttpHeader(Mockito.any(), Mockito.any()))

0 commit comments

Comments
 (0)