Skip to content

Commit c2d332b

Browse files
committed
[PROD-13929] Handle ignored Runner.rootId
1 parent 4ba1abd commit c2d332b

File tree

3 files changed

+128
-31
lines changed

3 files changed

+128
-31
lines changed

runner/src/integrationTest/kotlin/com/cosmotech/runner/service/RunnerServiceIntegrationTest.kt

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,75 @@ class RunnerServiceIntegrationTest : CsmRedisTestBase() {
360360
assertNull(newChildParentId)
361361
}
362362

363+
@Test
364+
fun `update rootId on root Runner delete`() {
365+
// Create a 3 level hierarchy: grandParent <- parent1 <- child1
366+
// <- parent2 <- child2
367+
val grandParentCreation =
368+
makeRunner(
369+
organizationSaved.id!!,
370+
workspaceSaved.id!!,
371+
solutionSaved.id!!,
372+
)
373+
val grandParentRunner =
374+
runnerApiService.createRunner(
375+
organizationSaved.id!!, workspaceSaved.id!!, grandParentCreation)
376+
val parentCreation =
377+
makeRunner(
378+
organizationSaved.id!!,
379+
workspaceSaved.id!!,
380+
solutionSaved.id!!,
381+
parentId = grandParentRunner.id)
382+
val parentRunner1 =
383+
runnerApiService.createRunner(organizationSaved.id!!, workspaceSaved.id!!, parentCreation)
384+
val parentRunner2 =
385+
runnerApiService.createRunner(organizationSaved.id!!, workspaceSaved.id!!, parentCreation)
386+
var childCreation =
387+
makeRunner(
388+
organizationSaved.id!!,
389+
workspaceSaved.id!!,
390+
solutionSaved.id!!,
391+
parentId = parentRunner1.id)
392+
val childRunner1 =
393+
runnerApiService.createRunner(organizationSaved.id!!, workspaceSaved.id!!, childCreation)
394+
childCreation.parentId = parentRunner2.id
395+
val childRunner2 =
396+
runnerApiService.createRunner(organizationSaved.id!!, workspaceSaved.id!!, childCreation)
397+
398+
// Initial parents check
399+
assertEquals(grandParentRunner.id, parentRunner1.parentId)
400+
assertEquals(grandParentRunner.id, parentRunner2.parentId)
401+
assertEquals(parentRunner1.id, childRunner1.parentId)
402+
assertEquals(parentRunner2.id, childRunner2.parentId)
403+
// Initial root check
404+
assertEquals(grandParentRunner.id, parentRunner1.rootId)
405+
assertEquals(grandParentRunner.id, parentRunner2.rootId)
406+
assertEquals(grandParentRunner.id, childRunner1.rootId)
407+
assertEquals(grandParentRunner.id, childRunner2.rootId)
408+
409+
// Delete grand parent
410+
runnerApiService.deleteRunner(
411+
organizationSaved.id!!, workspaceSaved.id!!, grandParentRunner.id!!)
412+
assertNull(
413+
runnerApiService
414+
.getRunner(organizationSaved.id!!, workspaceSaved.id!!, parentRunner1.id!!)
415+
.rootId)
416+
assertNull(
417+
runnerApiService
418+
.getRunner(organizationSaved.id!!, workspaceSaved.id!!, parentRunner2.id!!)
419+
.rootId)
420+
assertEquals(
421+
parentRunner1.id,
422+
runnerApiService
423+
.getRunner(organizationSaved.id!!, workspaceSaved.id!!, childRunner1.id!!)
424+
.rootId)
425+
assertEquals(
426+
parentRunner2.id,
427+
runnerApiService
428+
.getRunner(organizationSaved.id!!, workspaceSaved.id!!, childRunner2.id!!)
429+
.rootId)
430+
}
431+
363432
@Test
364433
fun `test RBAC RunnerSecurity as Platform Admin`() {
365434
every { getCurrentAuthenticatedRoles(any()) } returns listOf(ROLE_PLATFORM_ADMIN)

runner/src/main/kotlin/com/cosmotech/runner/repository/RunnerRepository.kt

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -67,24 +67,6 @@ interface RunnerRepository : RedisDocumentRepository<Runner, String> {
6767
pageable: Pageable
6868
): Page<Runner>
6969

70-
@Query("(@organizationId:{\$organizationId} @workspaceId:{\$workspaceId} @rootId:{\$rootId})")
71-
fun findByRootId(
72-
@Sanitize @Param("organizationId") organizationId: String,
73-
@Sanitize @Param("workspaceId") workspaceId: String,
74-
@Sanitize @Param("rootId") rootId: String,
75-
pageable: Pageable
76-
): Page<Runner>
77-
78-
@Query(
79-
"(@organizationId:{\$organizationId} @workspaceId:{\$workspaceId} @rootId:{\$rootId}) \$securityConstraint")
80-
fun findByRootIdAndSecurity(
81-
@Sanitize @Param("organizationId") organizationId: String,
82-
@Sanitize @Param("workspaceId") workspaceId: String,
83-
@Sanitize @Param("rootId") rootId: String,
84-
@SecurityConstraint @Param("securityConstraint") securityConstraint: String,
85-
pageable: Pageable
86-
): Page<Runner>
87-
8870
@Query("(@organizationId:{\$organizationId} @workspaceId:{\$workspaceId})")
8971
fun findByWorkspaceId(
9072
@Sanitize @Param("organizationId") organizationId: String,

runner/src/main/kotlin/com/cosmotech/runner/service/RunnerService.kt

Lines changed: 59 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -95,19 +95,20 @@ class RunnerService(
9595
"Can't delete runner ${runner.id!!}: at least one run is still running")
9696
}
9797

98-
// Update parent references to delete runner to point to grand-parent
99-
var pageRequest: Pageable =
100-
PageRequest.ofSize(csmPlatformProperties.twincache.runner.defaultPageSize)
101-
do {
102-
val pagedRunners =
103-
runnerRepository.findByParentId(
104-
runner.organizationId!!, runner.workspaceId!!, runner.id!!, pageRequest)
105-
pagedRunners.stream().forEach {
106-
it.parentId = runner.parentId
107-
runnerRepository.save(it)
98+
// Update parent and root references to deleted runner
99+
var newRoots = mutableListOf<Runner>()
100+
listAllRunnerByParentId(runner.organizationId!!, runner.workspaceId!!, runner.id!!).forEach {
101+
it.parentId = runner.parentId
102+
// Runner was root, child is now root
103+
if (runner.rootId == null) {
104+
it.rootId = null
105+
newRoots.add(it)
108106
}
109-
pageRequest = pagedRunners.nextPageable()
110-
} while (pagedRunners.hasNext())
107+
runnerRepository.save(it)
108+
}
109+
110+
// Update new root ids
111+
newRoots.forEach { updateChildrenRootId(parent = it, newRootId = it.id!!) }
111112

112113
// Notify the deletion
113114
val runnerDeleted =
@@ -117,6 +118,34 @@ class RunnerService(
117118
return runnerRepository.delete(runnerInstance.getRunnerDataObjet())
118119
}
119120

121+
private fun listAllRunnerByParentId(
122+
organizationId: String,
123+
workspaceId: String,
124+
parentId: String
125+
): List<Runner> {
126+
val defaultPageSize = csmPlatformProperties.twincache.runner.defaultPageSize
127+
var pageRequest: Pageable = PageRequest.ofSize(defaultPageSize)
128+
129+
var runners = mutableListOf<Runner>()
130+
131+
do {
132+
val pagedRunners =
133+
runnerRepository.findByParentId(organizationId, workspaceId, parentId, pageRequest)
134+
runners.addAll(pagedRunners.toList())
135+
pageRequest = pagedRunners.nextPageable()
136+
} while (pagedRunners.hasNext())
137+
138+
return runners
139+
}
140+
141+
private fun updateChildrenRootId(parent: Runner, newRootId: String) {
142+
listAllRunnerByParentId(parent.organizationId!!, parent.workspaceId!!, parent.id!!).forEach {
143+
it.rootId = newRootId
144+
runnerRepository.save(it)
145+
updateChildrenRootId(it, newRootId)
146+
}
147+
}
148+
120149
fun saveInstance(runnerInstance: RunnerInstance): Runner {
121150
return runnerRepository.save(runnerInstance.getRunnerDataObjet())
122151
}
@@ -204,7 +233,14 @@ class RunnerService(
204233
val beforeMutateDatasetList = this.runner.datasetList
205234

206235
val excludeFields =
207-
arrayOf("id", "ownerId", "organizationId", "workspaceId", "creationDate", "security")
236+
arrayOf(
237+
"id",
238+
"ownerId",
239+
"rootId",
240+
"organizationId",
241+
"workspaceId",
242+
"creationDate",
243+
"security")
208244
this.runner.compareToAndMutateIfNeeded(runner, excludedFields = excludeFields)
209245

210246
// take newly added datasets and propagate existing ACL on it
@@ -253,6 +289,16 @@ class RunnerService(
253289
val parameterValueList = this.runner.parametersValues ?: mutableListOf()
254290
parameterValueList.addAll(inheritedParameterValues)
255291
this.runner.parametersValues = parameterValueList
292+
293+
// Compute rootId
294+
this.runner.parentId?.let {
295+
this.runner.rootId =
296+
runnerRepository
297+
.findBy(organization!!.id!!, workspace!!.id!!, it)
298+
.orElseThrow { IllegalArgumentException("Parent runner not found: ${it}") }
299+
.rootId
300+
?: this.runner.parentId
301+
}
256302
}
257303
}
258304

0 commit comments

Comments
 (0)