From fa1f4b47b9111b3b5031bc23af1a1176f8ed3602 Mon Sep 17 00:00:00 2001 From: Jim Martens Date: Thu, 20 Mar 2025 16:07:19 +0100 Subject: [PATCH] change: Remove leader election and Kafka --- gradle/libs.versions.toml | 1 - module-bahnApi/bahnApi.gradle.kts | 2 - .../events/ScheduledTasksCreatedEvent.kt | 29 ------------ .../bahnApi/property/BahnApiProperties.kt | 2 - .../bahnApi/service/ScheduledTaskService.kt | 44 +++---------------- module-server/server.gradle.kts | 3 -- .../twomartens/timetable/MainApplication.kt | 33 +------------- .../configuration/BusConfiguration.kt | 8 ---- .../configuration/PropertyConfiguration.kt | 4 +- .../timetable/property/ServiceProperties.kt | 4 +- .../src/main/resources/application-dev.yaml | 11 ----- .../src/main/resources/application-prod.yaml | 7 --- .../src/main/resources/application.yaml | 12 ++--- .../support/model/LeadershipStatus.kt | 8 ---- .../support/property/StatusProbeProperties.kt | 3 -- .../support/property/TimeProperties.kt | 2 - .../twomartens/support/service/BusService.kt | 17 ------- module-support/support.gradle.kts | 2 - 18 files changed, 17 insertions(+), 175 deletions(-) delete mode 100644 module-bahnApi/src/main/kotlin/de/twomartens/timetable/bahnApi/events/ScheduledTasksCreatedEvent.kt delete mode 100644 module-server/src/main/kotlin/de/twomartens/timetable/configuration/BusConfiguration.kt delete mode 100644 module-support/src/main/kotlin/de/twomartens/support/model/LeadershipStatus.kt delete mode 100644 module-support/src/main/kotlin/de/twomartens/support/service/BusService.kt diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 9bbd518..656d918 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -40,7 +40,6 @@ spring-boot-oauth2-resource-server = { module = "org.springframework.boot:spring spring-boot-config = { module = "org.springframework.boot:spring-boot-configuration-processor", version.ref = "spring-boot" } spring-cloud = { module = "org.springframework.cloud:spring-cloud-dependencies", version.ref = "spring-cloud" } spring-cloud-starter = { module = "org.springframework.cloud:spring-cloud-starter" } -spring-cloud-starter-bus-kafka = { module = "org.springframework.cloud:spring-cloud-starter-bus-kafka" } spring-cloud-starter-config = { module = "org.springframework.cloud:spring-cloud-starter-config" } spring-cloud-config-server = { module = "org.springframework.cloud:spring-cloud-config-server" } spring-cloud-leader-election = { module = "org.springframework.cloud:spring-cloud-kubernetes-fabric8-leader" } diff --git a/module-bahnApi/bahnApi.gradle.kts b/module-bahnApi/bahnApi.gradle.kts index 86b68cd..1694a1d 100644 --- a/module-bahnApi/bahnApi.gradle.kts +++ b/module-bahnApi/bahnApi.gradle.kts @@ -18,6 +18,4 @@ dependencies { implementation(libs.spring.openapi) implementation(libs.spring.boot.mongo) - implementation(libs.spring.cloud.leader.election) - implementation(libs.spring.cloud.starter.bus.kafka) } \ No newline at end of file diff --git a/module-bahnApi/src/main/kotlin/de/twomartens/timetable/bahnApi/events/ScheduledTasksCreatedEvent.kt b/module-bahnApi/src/main/kotlin/de/twomartens/timetable/bahnApi/events/ScheduledTasksCreatedEvent.kt deleted file mode 100644 index 33ac24d..0000000 --- a/module-bahnApi/src/main/kotlin/de/twomartens/timetable/bahnApi/events/ScheduledTasksCreatedEvent.kt +++ /dev/null @@ -1,29 +0,0 @@ -package de.twomartens.timetable.bahnApi.events - -import org.springframework.cloud.bus.event.Destination -import org.springframework.cloud.bus.event.PathDestinationFactory -import org.springframework.cloud.bus.event.RemoteApplicationEvent -import java.time.Instant - -private const val TIMETABLE_SERVICE_NAME = "timetable" - -class ScheduledTasksCreatedEvent private constructor(source: Instant, originService: String, - destination: Destination) - : RemoteApplicationEvent(source, originService, destination) { - - private val creationTime: Instant = source - - override fun getSource(): Instant { - return creationTime - } - - companion object { - private val destinationFactory = PathDestinationFactory() - - fun of(source: Instant): ScheduledTasksCreatedEvent { - return ScheduledTasksCreatedEvent(source, - TIMETABLE_SERVICE_NAME, - destinationFactory.getDestination(TIMETABLE_SERVICE_NAME)) - } - } -} \ No newline at end of file diff --git a/module-bahnApi/src/main/kotlin/de/twomartens/timetable/bahnApi/property/BahnApiProperties.kt b/module-bahnApi/src/main/kotlin/de/twomartens/timetable/bahnApi/property/BahnApiProperties.kt index 109fa8a..1c70964 100644 --- a/module-bahnApi/src/main/kotlin/de/twomartens/timetable/bahnApi/property/BahnApiProperties.kt +++ b/module-bahnApi/src/main/kotlin/de/twomartens/timetable/bahnApi/property/BahnApiProperties.kt @@ -3,9 +3,7 @@ package de.twomartens.timetable.bahnApi.property import io.swagger.v3.oas.annotations.media.Schema import org.springframework.boot.context.properties.ConfigurationProperties import org.springframework.boot.context.properties.bind.ConstructorBinding -import org.springframework.cloud.context.config.annotation.RefreshScope -@RefreshScope @ConfigurationProperties(prefix = "de.twomartens.timetable.bahn-api") @Schema(description = "Properties to configure the BahnAPI") data class BahnApiProperties @ConstructorBinding constructor( diff --git a/module-bahnApi/src/main/kotlin/de/twomartens/timetable/bahnApi/service/ScheduledTaskService.kt b/module-bahnApi/src/main/kotlin/de/twomartens/timetable/bahnApi/service/ScheduledTaskService.kt index 9e1561b..1023541 100644 --- a/module-bahnApi/src/main/kotlin/de/twomartens/timetable/bahnApi/service/ScheduledTaskService.kt +++ b/module-bahnApi/src/main/kotlin/de/twomartens/timetable/bahnApi/service/ScheduledTaskService.kt @@ -1,8 +1,5 @@ package de.twomartens.timetable.bahnApi.service -import de.twomartens.support.model.LeadershipStatus -import de.twomartens.support.service.BusService -import de.twomartens.timetable.bahnApi.events.ScheduledTasksCreatedEvent import de.twomartens.timetable.bahnApi.model.Eva import de.twomartens.timetable.bahnApi.model.FetchDates import de.twomartens.timetable.bahnApi.model.TaskFactory @@ -13,20 +10,15 @@ import de.twomartens.timetable.types.Hour import de.twomartens.timetable.types.HourAtDay import mu.KotlinLogging import org.springframework.boot.context.event.ApplicationReadyEvent -import org.springframework.cloud.kubernetes.commons.leader.LeaderProperties import org.springframework.context.event.EventListener import org.springframework.data.mongodb.core.BulkOperations import org.springframework.data.mongodb.core.MongoTemplate -import org.springframework.integration.leader.event.OnGrantedEvent import org.springframework.stereotype.Service import java.time.Instant import java.time.LocalDate @Service class ScheduledTaskService( - private val busService: BusService, - private val leadershipStatus: LeadershipStatus, - private val leaderProperties: LeaderProperties, private val scheduledFetchTaskRepository: ScheduledFetchTaskRepository, private val taskFactory: TaskFactory, private val fetchTaskScheduler: FetchTaskScheduler, @@ -37,30 +29,16 @@ class ScheduledTaskService( @EventListener(ApplicationReadyEvent::class) fun onApplicationReady(event: ApplicationReadyEvent) { - if (!leaderProperties.isEnabled) { - log.info { "Leader election disabled and application ready" } - val updateTime = Instant.ofEpochMilli(event.timestamp) - updateTaskCounterAndScheduleTasksIfLeader(updateTime) - } - } - - @EventListener(OnGrantedEvent::class) - fun onLeadershipGranted(event: OnGrantedEvent) { - log.info { "Granted leadership" } + log.info { "Application ready" } val updateTime = Instant.ofEpochMilli(event.timestamp) - updateTaskCounterAndScheduleTasksIfLeader(updateTime) + updateTaskCounterAndScheduleTasks(updateTime) } - @EventListener(ScheduledTasksCreatedEvent::class) - fun onScheduledTasksCreated(event: ScheduledTasksCreatedEvent) { - updateTaskCounterAndScheduleTasksIfLeader(event.source) - } - - private fun updateTaskCounterAndScheduleTasksIfLeader(updateTime: Instant) { - log.info { "Update tasks from database and schedule if leader" } + private fun updateTaskCounterAndScheduleTasks(updateTime: Instant) { + log.info { "Update tasks from database and schedule" } val createdTasks = findTasksCreatedSince(lastUpdate) updateCounterIfNotUpToDate(updateTime, createdTasks) - scheduleTasksIfLeader(createdTasks) + scheduleTasks(createdTasks) lastUpdate = updateTime } @@ -72,7 +50,6 @@ class ScheduledTaskService( val newTasks = buildScheduledTasks(tswRoute, fetchDates) storeTasksInDatabaseAndStoreCreationTime(newTasks) - publishTasksCreatedEvent() } private fun calculateDatesToFetch(fetchedDate: LocalDate): FetchDates { @@ -109,10 +86,8 @@ class ScheduledTaskService( return newTasks } - private fun scheduleTasksIfLeader(tasksToSchedule: List) { - if (leadershipStatus.isLeader) { - fetchTaskScheduler.scheduleFetchTasks(tasksToSchedule) - } + private fun scheduleTasks(tasksToSchedule: List) { + fetchTaskScheduler.scheduleFetchTasks(tasksToSchedule) } private fun updateCounterIfNotUpToDate(updateTime: Instant, @@ -139,11 +114,6 @@ class ScheduledTaskService( .execute() } - private fun publishTasksCreatedEvent() { - val event = ScheduledTasksCreatedEvent.of(createdTime) - busService.publishEvent(event) - } - companion object { private val log = KotlinLogging.logger {} } diff --git a/module-server/server.gradle.kts b/module-server/server.gradle.kts index 8ef96b4..ec5ddc4 100644 --- a/module-server/server.gradle.kts +++ b/module-server/server.gradle.kts @@ -18,7 +18,4 @@ dependencies { kapt(libs.mapstruct.processor) implementation(libs.bundles.spring.boot.security) - implementation(libs.spring.cloud.starter.config) - implementation(libs.spring.cloud.leader.election) - implementation(libs.spring.cloud.starter.bus.kafka) } diff --git a/module-server/src/main/kotlin/de/twomartens/timetable/MainApplication.kt b/module-server/src/main/kotlin/de/twomartens/timetable/MainApplication.kt index a999313..bdc87a6 100644 --- a/module-server/src/main/kotlin/de/twomartens/timetable/MainApplication.kt +++ b/module-server/src/main/kotlin/de/twomartens/timetable/MainApplication.kt @@ -1,44 +1,15 @@ package de.twomartens.timetable -import de.twomartens.support.model.LeadershipStatus import org.springframework.boot.autoconfigure.SpringBootApplication -import org.springframework.boot.context.event.ApplicationReadyEvent import org.springframework.boot.runApplication -import org.springframework.cloud.kubernetes.commons.leader.LeaderProperties -import org.springframework.context.event.EventListener import org.springframework.data.mongodb.config.EnableMongoAuditing -import org.springframework.integration.leader.event.OnGrantedEvent -import org.springframework.integration.leader.event.OnRevokedEvent import org.springframework.scheduling.annotation.EnableScheduling @EnableMongoAuditing @EnableScheduling @SpringBootApplication(scanBasePackages = ["de.twomartens.support", "de.twomartens.timetable"]) -open class MainApplication( - private val leadershipStatus: LeadershipStatus, - private val leaderProperties: LeaderProperties -) { - @EventListener(ApplicationReadyEvent::class) - fun ready(event: ApplicationReadyEvent) { - setInitialLeadershipStatus() - } - - @EventListener(OnGrantedEvent::class) - fun onLeadershipGranted(event: OnGrantedEvent) { - leadershipStatus.isLeader = true - } - - @EventListener(OnRevokedEvent::class) - fun onLeadershipRevoked(event: OnRevokedEvent) { - leadershipStatus.isLeader = false - } - - private fun setInitialLeadershipStatus() { - leadershipStatus.isLeader = !leaderProperties.isEnabled - } -} +open class MainApplication() {} fun main(args: Array) { runApplication(*args) -} - +} \ No newline at end of file diff --git a/module-server/src/main/kotlin/de/twomartens/timetable/configuration/BusConfiguration.kt b/module-server/src/main/kotlin/de/twomartens/timetable/configuration/BusConfiguration.kt deleted file mode 100644 index aa5c847..0000000 --- a/module-server/src/main/kotlin/de/twomartens/timetable/configuration/BusConfiguration.kt +++ /dev/null @@ -1,8 +0,0 @@ -package de.twomartens.timetable.configuration - -import org.springframework.cloud.bus.jackson.RemoteApplicationEventScan -import org.springframework.context.annotation.Configuration - -@Configuration -@RemoteApplicationEventScan(basePackages = ["de.twomartens.timetable", "de.twomartens.support"]) -open class BusConfiguration diff --git a/module-server/src/main/kotlin/de/twomartens/timetable/configuration/PropertyConfiguration.kt b/module-server/src/main/kotlin/de/twomartens/timetable/configuration/PropertyConfiguration.kt index 749e6a5..debb5b1 100644 --- a/module-server/src/main/kotlin/de/twomartens/timetable/configuration/PropertyConfiguration.kt +++ b/module-server/src/main/kotlin/de/twomartens/timetable/configuration/PropertyConfiguration.kt @@ -7,12 +7,10 @@ import de.twomartens.support.property.TimeProperties import de.twomartens.timetable.bahnApi.property.BahnApiProperties import de.twomartens.timetable.property.ServiceProperties import org.springframework.boot.context.properties.EnableConfigurationProperties -import org.springframework.cloud.kubernetes.commons.leader.LeaderProperties import org.springframework.context.annotation.Configuration @Configuration @EnableConfigurationProperties(RestTemplateTimeoutProperties::class, ServiceProperties::class, StatusProbeProperties::class, TimeProperties::class, BahnApiProperties::class, - HealthCheckProperties::class, - LeaderProperties::class) + HealthCheckProperties::class) open class PropertyConfiguration diff --git a/module-server/src/main/kotlin/de/twomartens/timetable/property/ServiceProperties.kt b/module-server/src/main/kotlin/de/twomartens/timetable/property/ServiceProperties.kt index a76c009..6294506 100644 --- a/module-server/src/main/kotlin/de/twomartens/timetable/property/ServiceProperties.kt +++ b/module-server/src/main/kotlin/de/twomartens/timetable/property/ServiceProperties.kt @@ -5,11 +5,9 @@ import de.twomartens.timetable.model.common.LanguageCode import de.twomartens.timetable.types.NonEmptyString import io.swagger.v3.oas.annotations.media.Schema import org.springframework.boot.context.properties.ConfigurationProperties -import org.springframework.cloud.context.config.annotation.RefreshScope -@RefreshScope @ConfigurationProperties(prefix = "de.twomartens.timetable") @Schema(description = "Properties, to configure this Application") -class ServiceProperties { +open class ServiceProperties { lateinit var countryNames: Map> } diff --git a/module-server/src/main/resources/application-dev.yaml b/module-server/src/main/resources/application-dev.yaml index 5a9da5b..481524f 100644 --- a/module-server/src/main/resources/application-dev.yaml +++ b/module-server/src/main/resources/application-dev.yaml @@ -1,13 +1,2 @@ server.port: 12100 management.server.port: 12101 - - -spring: - cloud: - kubernetes: - leader: - enabled: false - config: - import: - - "classpath:config/timetable.yaml" - - "optional:configserver:${CONFIGSERVER_SCHEME:http}://${CONFIGSERVER_HOST:localhost}:${CONFIGSERVER_PORT:8888}${CONFIGSERVER_PREFIX:/config}" diff --git a/module-server/src/main/resources/application-prod.yaml b/module-server/src/main/resources/application-prod.yaml index cf5bd00..e69de29 100644 --- a/module-server/src/main/resources/application-prod.yaml +++ b/module-server/src/main/resources/application-prod.yaml @@ -1,7 +0,0 @@ -spring: - config: - import: - - "configserver:${CONFIGSERVER_SCHEME:http}://${CONFIGSERVER_HOST:configserver}:${CONFIGSERVER_PORT:8888}${CONFIGSERVER_PREFIX:/config}" - kafka: - bootstrap-servers: - - ${KAFKA_HOST:kafka}:${KAFKA_PORT:9092} diff --git a/module-server/src/main/resources/application.yaml b/module-server/src/main/resources/application.yaml index 37b5aca..c81264d 100644 --- a/module-server/src/main/resources/application.yaml +++ b/module-server/src/main/resources/application.yaml @@ -26,12 +26,6 @@ management: http.server.requests: true spring: - cloud: - config: - name: timetable - kubernetes: - leader: - enabled: true main: banner-mode: off profiles: @@ -84,6 +78,12 @@ de.twomartens.timetable: bahn-api: client-id: ${DB_CLIENT_ID} client-secret: ${DB_CLIENT_SECRET} + greeting: "Hello %s, on millenium" + statusprobe: + scheduleDuration: 60 + maxKafkaFailureDuration: 5 + maxBlobFailureCount: 5 + maxFailurePercent: 50 de.twomartens.support.health.greeting: "Good morning" time: diff --git a/module-support/src/main/kotlin/de/twomartens/support/model/LeadershipStatus.kt b/module-support/src/main/kotlin/de/twomartens/support/model/LeadershipStatus.kt deleted file mode 100644 index 9619316..0000000 --- a/module-support/src/main/kotlin/de/twomartens/support/model/LeadershipStatus.kt +++ /dev/null @@ -1,8 +0,0 @@ -package de.twomartens.support.model - -import org.springframework.stereotype.Component - -@Component -class LeadershipStatus { - var isLeader = false -} diff --git a/module-support/src/main/kotlin/de/twomartens/support/property/StatusProbeProperties.kt b/module-support/src/main/kotlin/de/twomartens/support/property/StatusProbeProperties.kt index c12f059..b0dfc65 100644 --- a/module-support/src/main/kotlin/de/twomartens/support/property/StatusProbeProperties.kt +++ b/module-support/src/main/kotlin/de/twomartens/support/property/StatusProbeProperties.kt @@ -4,16 +4,13 @@ import io.swagger.v3.oas.annotations.media.Schema import org.springframework.boot.context.properties.ConfigurationProperties import org.springframework.boot.context.properties.bind.ConstructorBinding import org.springframework.boot.convert.DurationUnit -import org.springframework.cloud.context.config.annotation.RefreshScope import java.time.Duration import java.time.temporal.ChronoUnit -@RefreshScope @ConfigurationProperties(prefix = "de.twomartens.timetable.statusprobe") @Schema(description = "Properties, to configure this Application") data class StatusProbeProperties @ConstructorBinding constructor( @DurationUnit(ChronoUnit.SECONDS) val scheduleDuration: Duration, - @DurationUnit(ChronoUnit.MINUTES) val maxKafkaFailureDuration: Duration, val maxBlobFailureCount: Int, val maxFailurePercent: Int ) diff --git a/module-support/src/main/kotlin/de/twomartens/support/property/TimeProperties.kt b/module-support/src/main/kotlin/de/twomartens/support/property/TimeProperties.kt index 1e4a465..a866b56 100644 --- a/module-support/src/main/kotlin/de/twomartens/support/property/TimeProperties.kt +++ b/module-support/src/main/kotlin/de/twomartens/support/property/TimeProperties.kt @@ -3,10 +3,8 @@ package de.twomartens.support.property import io.swagger.v3.oas.annotations.media.Schema import org.springframework.boot.context.properties.ConfigurationProperties import org.springframework.boot.context.properties.bind.ConstructorBinding -import org.springframework.cloud.context.config.annotation.RefreshScope import java.time.ZoneId -@RefreshScope @ConfigurationProperties(prefix = "time") @Schema(description = "Properties to configure time") data class TimeProperties @ConstructorBinding constructor( diff --git a/module-support/src/main/kotlin/de/twomartens/support/service/BusService.kt b/module-support/src/main/kotlin/de/twomartens/support/service/BusService.kt deleted file mode 100644 index d08cf56..0000000 --- a/module-support/src/main/kotlin/de/twomartens/support/service/BusService.kt +++ /dev/null @@ -1,17 +0,0 @@ -package de.twomartens.support.service - -import org.springframework.beans.factory.ObjectProvider -import org.springframework.cloud.bus.BusBridge -import org.springframework.cloud.bus.event.RemoteApplicationEvent -import org.springframework.stereotype.Service - -@Service -class BusService( - private val busBridge: ObjectProvider -) { - fun publishEvent(event: RemoteApplicationEvent) { - busBridge.ifAvailable { - it.send(event) - } - } -} \ No newline at end of file diff --git a/module-support/support.gradle.kts b/module-support/support.gradle.kts index 3a26e5c..071f274 100644 --- a/module-support/support.gradle.kts +++ b/module-support/support.gradle.kts @@ -10,6 +10,4 @@ dependencies { implementation(libs.spring.openapi) implementation(libs.bundles.spring.boot.security) - implementation(libs.spring.cloud.leader.election) - implementation(libs.spring.cloud.starter.bus.kafka) } \ No newline at end of file