|
| 1 | +package com.linkedin.datahub.upgrade.cleanup; |
| 2 | + |
| 3 | +import com.linkedin.datahub.upgrade.UpgradeStep; |
| 4 | +import com.linkedin.datahub.upgrade.config.OpenTelemetryConfig; |
| 5 | +import com.linkedin.datahub.upgrade.sqlsetup.SqlSetupArgs; |
| 6 | +import com.linkedin.datahub.upgrade.sqlsetup.config.SqlSetupConfig; |
| 7 | +import com.linkedin.datahub.upgrade.sqlsetup.config.SqlSetupEbeanFactory; |
| 8 | +import com.linkedin.gms.factory.config.ConfigurationProvider; |
| 9 | +import com.linkedin.gms.factory.search.BaseElasticSearchComponentsFactory; |
| 10 | +import com.linkedin.metadata.config.kafka.KafkaConfiguration; |
| 11 | +import com.linkedin.metadata.utils.EnvironmentUtils; |
| 12 | +import io.ebean.Database; |
| 13 | +import java.util.ArrayList; |
| 14 | +import java.util.List; |
| 15 | +import javax.annotation.Nonnull; |
| 16 | +import lombok.extern.slf4j.Slf4j; |
| 17 | +import org.springframework.beans.factory.annotation.Autowired; |
| 18 | +import org.springframework.beans.factory.annotation.Qualifier; |
| 19 | +import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration; |
| 20 | +import org.springframework.boot.autoconfigure.kafka.KafkaProperties; |
| 21 | +import org.springframework.context.annotation.Bean; |
| 22 | +import org.springframework.context.annotation.ComponentScan; |
| 23 | +import org.springframework.context.annotation.Configuration; |
| 24 | +import org.springframework.context.annotation.Import; |
| 25 | + |
| 26 | +/** |
| 27 | + * Spring configuration for the Cleanup upgrade. Loads the minimal set of beans needed to tear down |
| 28 | + * Elasticsearch indices, Kafka topics, and the SQL database. |
| 29 | + */ |
| 30 | +@Slf4j |
| 31 | +@Configuration |
| 32 | +@Import({ |
| 33 | + MetricsAutoConfiguration.class, |
| 34 | + OpenTelemetryConfig.class, |
| 35 | + SqlSetupConfig.class, |
| 36 | + SqlSetupEbeanFactory.class |
| 37 | +}) |
| 38 | +@ComponentScan( |
| 39 | + basePackages = { |
| 40 | + "com.linkedin.gms.factory.config", |
| 41 | + "com.linkedin.gms.factory.common", |
| 42 | + "com.linkedin.gms.factory.entity", |
| 43 | + "com.linkedin.gms.factory.entityclient", |
| 44 | + "com.linkedin.gms.factory.plugins", |
| 45 | + "com.linkedin.gms.factory.entityregistry", |
| 46 | + "com.linkedin.gms.factory.search", |
| 47 | + "com.linkedin.gms.factory.timeseries", |
| 48 | + "com.linkedin.gms.factory.context", |
| 49 | + "com.linkedin.gms.factory.system_telemetry" |
| 50 | + }) |
| 51 | +public class CleanupUpgradeConfig { |
| 52 | + |
| 53 | + @Autowired(required = false) |
| 54 | + private BaseElasticSearchComponentsFactory.BaseElasticSearchComponents esComponents; |
| 55 | + |
| 56 | + @Autowired(required = false) |
| 57 | + private ConfigurationProvider configurationProvider; |
| 58 | + |
| 59 | + @Autowired(required = false) |
| 60 | + private KafkaProperties kafkaProperties; |
| 61 | + |
| 62 | + @Autowired(required = false) |
| 63 | + @Qualifier("ebeanServer") |
| 64 | + private Database ebeanServer; |
| 65 | + |
| 66 | + @Autowired(required = false) |
| 67 | + @Qualifier("sqlSetupArgs") |
| 68 | + private SqlSetupArgs sqlSetupArgs; |
| 69 | + |
| 70 | + @Bean(name = "cleanup") |
| 71 | + @Nonnull |
| 72 | + public Cleanup createCleanup() { |
| 73 | + boolean esEnabled = EnvironmentUtils.getBoolean("CLEANUP_ELASTICSEARCH_ENABLED", true); |
| 74 | + boolean kafkaEnabled = EnvironmentUtils.getBoolean("CLEANUP_KAFKA_ENABLED", true); |
| 75 | + boolean sqlEnabled = EnvironmentUtils.getBoolean("CLEANUP_SQL_ENABLED", true); |
| 76 | + |
| 77 | + List<UpgradeStep> steps = new ArrayList<>(); |
| 78 | + |
| 79 | + // Order: ES first (so indices aren't queried during DB drop), then Kafka, then SQL |
| 80 | + if (esEnabled && esComponents != null) { |
| 81 | + steps.add(new DeleteElasticsearchIndicesStep(esComponents)); |
| 82 | + log.info("Elasticsearch cleanup step enabled"); |
| 83 | + } else if (esEnabled) { |
| 84 | + log.warn("Elasticsearch cleanup requested but ES components not available — skipping"); |
| 85 | + } |
| 86 | + |
| 87 | + KafkaConfiguration kafkaConfig = |
| 88 | + configurationProvider != null ? configurationProvider.getKafka() : null; |
| 89 | + if (kafkaEnabled && kafkaConfig != null && kafkaProperties != null) { |
| 90 | + steps.add(new DeleteKafkaTopicsStep(kafkaConfig, kafkaProperties)); |
| 91 | + log.info("Kafka cleanup step enabled"); |
| 92 | + } else if (kafkaEnabled) { |
| 93 | + log.warn("Kafka cleanup requested but Kafka config not available — skipping"); |
| 94 | + } |
| 95 | + |
| 96 | + if (sqlEnabled && ebeanServer != null && sqlSetupArgs != null) { |
| 97 | + steps.add(new DropDatabaseStep(ebeanServer, sqlSetupArgs)); |
| 98 | + log.info("SQL cleanup step enabled"); |
| 99 | + } else if (sqlEnabled) { |
| 100 | + log.warn("SQL cleanup requested but database not available — skipping"); |
| 101 | + } |
| 102 | + |
| 103 | + return new Cleanup(steps); |
| 104 | + } |
| 105 | +} |
0 commit comments