All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.netflix.spinnaker.config.SqlRedisQueueShovelConfiguration.kt Maven / Gradle / Ivy

There is a newer version: 8.57.0
Show newest version
/*
 * Copyright 2019 Netflix, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.netflix.spinnaker.config

import com.fasterxml.jackson.databind.ObjectMapper
import com.netflix.spectator.api.Registry
import com.netflix.spinnaker.kork.dynamicconfig.DynamicConfigService
import com.netflix.spinnaker.kork.jedis.JedisDriverProperties
import com.netflix.spinnaker.kork.jedis.JedisPoolFactory
import com.netflix.spinnaker.orca.q.QueueShovel
import com.netflix.spinnaker.q.Activator
import com.netflix.spinnaker.q.metrics.EventPublisher
import com.netflix.spinnaker.q.migration.SerializationMigrator
import com.netflix.spinnaker.q.redis.AbstractRedisQueue
import com.netflix.spinnaker.q.redis.RedisClusterQueue
import com.netflix.spinnaker.q.redis.RedisQueue
import com.netflix.spinnaker.q.sql.SqlQueue
import java.time.Clock
import java.util.Optional
import org.apache.commons.pool2.impl.GenericObjectPoolConfig
import org.jooq.DSLContext
import org.springframework.beans.factory.annotation.Value
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import redis.clients.jedis.JedisCluster

@Configuration
@EnableConfigurationProperties(RedisQueueProperties::class, SqlQueueProperties::class)
@ConditionalOnProperty(value = ["queue.shovel.enabled"])
class SqlRedisQueueShovelConfiguration {

  @Bean
  @ConditionalOnBean(SqlQueue::class)
  @ConditionalOnProperty(value = ["queue.shovel.kind"], havingValue = "redis-to-sql")
  fun redisToSqlQueueShovel(
    @Value("\${redis.connection:redis://localhost:6379}") mainConnection: String,
    @Value("\${redis.timeout:2000}") timeout: Int,
    redisPoolConfig: GenericObjectPoolConfig<*>,
    queue: SqlQueue,
    clock: Clock,
    publisher: EventPublisher,
    mapper: ObjectMapper,
    serializationMigrator: Optional,
    redisQueueProperties: RedisQueueProperties,
    registry: Registry,
    discoveryActivator: Activator,
    dynamicConfigService: DynamicConfigService
  ): QueueShovel {
    val jedisPool = JedisPoolFactory(registry).build(
      "previousQueue",
      JedisDriverProperties().apply {
        connection = mainConnection
        timeoutMs = timeout
        poolConfig = redisPoolConfig
      },
      redisPoolConfig
    )

    val previousQueue = RedisQueue(
      queueName = redisQueueProperties.queueName,
      pool = jedisPool,
      clock = clock,
      deadMessageHandlers = emptyList(),
      publisher = publisher,
      mapper = mapper,
      serializationMigrator = serializationMigrator
    )

    return QueueShovel(
      queue = queue,
      previousQueue = previousQueue,
      registry = registry,
      activator = discoveryActivator,
      config = dynamicConfigService,
      executionRepository = null
    )
  }

  @Bean
  @ConditionalOnBean(SqlQueue::class)
  @ConditionalOnProperty(value = ["queue.shovel.kind"], havingValue = "redis-cluster-to-sql")
  fun redisClusterToSqlQueueShovel(
    queue: SqlQueue,
    cluster: JedisCluster,
    clock: Clock,
    publisher: EventPublisher,
    mapper: ObjectMapper,
    serializationMigrator: Optional,
    redisQueueProperties: RedisQueueProperties,
    registry: Registry,
    discoveryActivator: Activator,
    dynamicConfigService: DynamicConfigService
  ): QueueShovel {
    val previousQueue = RedisClusterQueue(
      queueName = redisQueueProperties.queueName,
      jedisCluster = cluster,
      clock = clock,
      mapper = mapper,
      serializationMigrator = serializationMigrator,
      deadMessageHandlers = emptyList(),
      publisher = publisher
    )

    return QueueShovel(
      queue = queue,
      previousQueue = previousQueue,
      registry = registry,
      activator = discoveryActivator,
      config = dynamicConfigService,
      executionRepository = null
    )
  }

  /**
   * [sqlToRedisQueueShovel] only needs to construct a SqlQueue, so works with both
   * [RedisQueue] or [RedisClusterQueue] primary queues.
   */
  @Bean
  @ConditionalOnBean(AbstractRedisQueue::class)
  @ConditionalOnProperty(value = ["queue.shovel.kind"], havingValue = "sql-to-redis")
  fun sqlToRedisQueueShovel(
    queue: AbstractRedisQueue,
    jooq: DSLContext,
    clock: Clock,
    publisher: EventPublisher,
    mapper: ObjectMapper,
    serializationMigrator: Optional,
    sqlQueueProperties: SqlQueueProperties,
    registry: Registry,
    discoveryActivator: Activator,
    dynamicConfigService: DynamicConfigService
  ): QueueShovel {
    val previousQueue = SqlQueue(
      queueName = sqlQueueProperties.queueName,
      schemaVersion = 1,
      jooq = jooq,
      clock = clock,
      lockTtlSeconds = sqlQueueProperties.lockTtlSeconds,
      mapper = mapper,
      serializationMigrator = serializationMigrator,
      deadMessageHandlers = emptyList(),
      publisher = publisher,
      sqlRetryProperties = sqlQueueProperties.retries
    )

    return QueueShovel(
      queue = queue,
      previousQueue = previousQueue,
      registry = registry,
      activator = discoveryActivator,
      config = dynamicConfigService,
      executionRepository = null
    )
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy