-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4f8475b
commit b0a4626
Showing
6 changed files
with
151 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package io.holixon.example.axon.event.transformation.adapter.`in` | ||
|
||
import io.holixon.example.axon.event.transformation.adapter.out.event.MeasurementsUpdatedEvent | ||
import io.holixon.example.axon.event.transformation.infrastructure.EventCleanup | ||
import org.springframework.http.ResponseEntity | ||
import org.springframework.http.ResponseEntity.noContent | ||
import org.springframework.web.bind.annotation.PostMapping | ||
import org.springframework.web.bind.annotation.RequestBody | ||
import org.springframework.web.bind.annotation.RequestMapping | ||
import org.springframework.web.bind.annotation.RestController | ||
import java.time.Instant | ||
|
||
@RestController | ||
@RequestMapping("/admin") | ||
class AdminController( | ||
val eventCleanup: EventCleanup | ||
) { | ||
|
||
@PostMapping(value = ["/cleanup"]) | ||
fun cleanup(@RequestBody deleteUntil: Instant): ResponseEntity<Void> { | ||
eventCleanup.deleteEventsUntil(eventFQDN = MeasurementsUpdatedEvent::class.java.name, deleteUntil = deleteUntil) | ||
return noContent().build() | ||
} | ||
|
||
@PostMapping(value = ["/compact"]) | ||
fun compact(): ResponseEntity<Void> { | ||
eventCleanup.compact() | ||
return noContent().build() | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
src/main/kotlin/application/port/in/GenerateRandomBatchUpdateInPort.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package io.holixon.example.axon.event.transformation.application.port.`in` | ||
|
||
interface GenerateRandomBatchUpdateInPort { | ||
fun runBatchUpdate(sensorId: String = "temp1") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/main/kotlin/infrastructure/AutoClosableAxonServerConnection.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package io.holixon.example.axon.event.transformation.infrastructure | ||
|
||
import io.axoniq.axonserver.connector.AxonServerConnection | ||
import io.axoniq.axonserver.connector.AxonServerConnectionFactory | ||
import io.axoniq.axonserver.connector.impl.ServerAddress | ||
import mu.KLogging | ||
import java.io.Closeable | ||
|
||
/** | ||
* Auto-closable version of AxonServer Connection. | ||
*/ | ||
class AutoClosableAxonServerConnection( | ||
private val connection: AxonServerConnection | ||
) : Closeable, AxonServerConnection by connection { | ||
|
||
companion object : KLogging() { | ||
fun connect(axonServerHost: String, axonServerPort: Int = 8124, componentName: String, context: String): AutoClosableAxonServerConnection { | ||
val factory = AxonServerConnectionFactory.forClient(componentName) | ||
.routingServers(ServerAddress(axonServerHost, axonServerPort)) | ||
.build() | ||
val connection = factory.connect(context) | ||
logger.info { "Connected to $axonServerHost:$axonServerPort@$context" } | ||
return AutoClosableAxonServerConnection(connection) | ||
} | ||
} | ||
|
||
override fun close() { | ||
if (connection.isConnected) { | ||
connection.disconnect() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package io.holixon.example.axon.event.transformation.infrastructure | ||
|
||
import io.axoniq.axonserver.connector.AxonServerConnection | ||
import io.axoniq.axonserver.connector.event.transformation.EventTransformation | ||
import io.axoniq.axonserver.connector.event.transformation.event.EventSources | ||
import mu.KLogging | ||
import org.axonframework.axonserver.connector.AxonServerConfiguration | ||
import org.springframework.stereotype.Component | ||
import java.time.Instant | ||
|
||
@Component | ||
class EventCleanup( | ||
axonServerConfiguration: AxonServerConfiguration | ||
) { | ||
|
||
private val connect = axonServerConfiguration.routingServers().first() | ||
private val context = axonServerConfiguration.context | ||
|
||
companion object : KLogging() | ||
|
||
fun deleteEventsUntil(eventFQDN: String, deleteUntil: Instant, firstToken: Long = 0, lastToken: Long = -1L) { | ||
AutoClosableAxonServerConnection | ||
.connect(connect.hostName, connect.grpcPort, this.javaClass.simpleName, this.context).use { connection -> | ||
|
||
// define the range of events | ||
val last = if (lastToken == -1L) { | ||
connection.eventChannel().lastToken.get() | ||
} else { | ||
lastToken | ||
} | ||
|
||
try { | ||
EventSources | ||
.range({ connection.eventChannel() }, firstToken, last) | ||
.filter { eventWithToken -> | ||
logger.debug { "Event: ${eventWithToken.event.payload.type}" } | ||
eventWithToken.event.payload.type == eventFQDN && eventWithToken.event.timestamp < deleteUntil.toEpochMilli() | ||
} | ||
.transform("Deleting events until $deleteUntil") { event, appender -> appender.deleteEvent(event.token) } | ||
.execute { connection.eventTransformationChannel() } | ||
.get() | ||
} catch (e: Exception) { | ||
logger.error(e) { "Error executing event transformation" } | ||
} finally { | ||
connection.ensureNoActiveTransformations() | ||
} | ||
} | ||
} | ||
|
||
fun compact() { | ||
AutoClosableAxonServerConnection | ||
.connect(connect.hostName, connect.grpcPort, this.javaClass.simpleName, this.context).use { connection -> | ||
connection.ensureNoActiveTransformations() | ||
connection | ||
.eventTransformationChannel() | ||
.startCompacting() | ||
.get() | ||
} | ||
} | ||
|
||
fun AxonServerConnection.ensureNoActiveTransformations() { | ||
this.eventTransformationChannel().transformations().get().forEach { | ||
logger.debug { "Found event transformation: ${it.id()}, ${it.description()}, ${it.state()}, ${it.lastSequence()}" } | ||
if (it.state() == EventTransformation.State.ACTIVE) { | ||
// cancelling the active one | ||
this.eventTransformationChannel().activeTransformation().get().cancel().get() | ||
logger.warn { "Cancelled active transformation ${it.id()}, ${it.description()}, ${it.state()}, ${it.lastSequence()}" } | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
|