|
| 1 | +package misk.vitess |
| 2 | + |
| 3 | +import com.google.common.base.Supplier |
| 4 | +import com.google.common.base.Suppliers |
| 5 | +import misk.jdbc.DataSourceService |
| 6 | +import misk.jdbc.mapNotNull |
| 7 | +import misk.logging.getLogger |
| 8 | +import java.sql.SQLException |
| 9 | +import java.sql.SQLRecoverableException |
| 10 | +import java.sql.SQLTimeoutException |
| 11 | +import java.sql.SQLTransientException |
| 12 | +import java.util.concurrent.TimeUnit |
| 13 | +import kotlin.random.Random |
| 14 | + |
| 15 | +object ShardsLoader { |
| 16 | + private val logger = getLogger<ShardsLoader>() |
| 17 | + |
| 18 | + fun shards(dataSourceService: DataSourceService): Supplier<Set<Shard>> = |
| 19 | + Suppliers.memoizeWithExpiration({ |
| 20 | + if (!dataSourceService.config().type.isVitess) { |
| 21 | + Shard.SINGLE_SHARD_SET |
| 22 | + } else { |
| 23 | + loadVitessShards(dataSourceService) |
| 24 | + } |
| 25 | + }, 5, TimeUnit.MINUTES) |
| 26 | + |
| 27 | + private fun loadVitessShards(dataSourceService: DataSourceService): Set<Shard> { |
| 28 | + val maxRetries = 3 |
| 29 | + var lastException: Exception? = null |
| 30 | + val retryableErrorMessages = listOf( |
| 31 | + "timeout", |
| 32 | + "context canceled", |
| 33 | + "connection closed" |
| 34 | + ) |
| 35 | + |
| 36 | + repeat(maxRetries) { attempt -> |
| 37 | + try { |
| 38 | + return dataSourceService.dataSource.connection.use { connection -> |
| 39 | + connection.createStatement().use { statement -> |
| 40 | + // Set aggressive 5-second timeout for fast failure |
| 41 | + statement.queryTimeout = 5 |
| 42 | + |
| 43 | + val resultSet = statement.executeQuery("SHOW VITESS_SHARDS") |
| 44 | + |
| 45 | + val shards = resultSet.mapNotNull { rs -> |
| 46 | + try { |
| 47 | + val shardName = rs.getString(1) |
| 48 | + Shard.parse(shardName) |
| 49 | + } catch (e: Exception) { |
| 50 | + logger.warn(e) { "Failed to parse shard from result: ${rs.getString(1)}" } |
| 51 | + null |
| 52 | + } |
| 53 | + }.toSet() |
| 54 | + |
| 55 | + if (shards.isEmpty()) { |
| 56 | + throw SQLRecoverableException("SHOW VITESS_SHARDS returned empty result set") |
| 57 | + } |
| 58 | + |
| 59 | + shards |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + } catch (e: Exception) { |
| 64 | + lastException = e |
| 65 | + |
| 66 | + val isRetryableException = |
| 67 | + e.cause is SQLTimeoutException || e is SQLTimeoutException || |
| 68 | + e.cause is SQLRecoverableException || e is SQLRecoverableException |
| 69 | + e.cause is SQLTransientException || e is SQLTransientException |
| 70 | + |
| 71 | + val isRetryableMessage = retryableErrorMessages.any { errorMessage -> |
| 72 | + e.message?.contains(errorMessage, ignoreCase = true) == true |
| 73 | + } |
| 74 | + |
| 75 | + val isRetryable = isRetryableException || isRetryableMessage |
| 76 | + |
| 77 | + logger.warn(e) { |
| 78 | + "Failed to load Vitess shards on attempt ${attempt + 1}/$maxRetries" + |
| 79 | + if (isRetryable) " (retryable exception detected)" else "" |
| 80 | + } |
| 81 | + |
| 82 | + // Only retry on timeouts/connectivity issues, and not on the last attempt |
| 83 | + if (isRetryable && attempt < maxRetries - 1) { |
| 84 | + val backoffMs = calculateBackoff(attempt) |
| 85 | + logger.info { "Retrying shard loading in ${backoffMs}ms..." } |
| 86 | + |
| 87 | + try { |
| 88 | + Thread.sleep(backoffMs) |
| 89 | + } catch (interrupted: InterruptedException) { |
| 90 | + Thread.currentThread().interrupt() |
| 91 | + logger.error { "Shard loading interrupted" } |
| 92 | + throw interrupted |
| 93 | + } |
| 94 | + } else { |
| 95 | + throw e // immediately rethrow without retrying |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + // All retries exhausted -- shouldn't reach here |
| 101 | + throw SQLException( |
| 102 | + "Failed to load Vitess shards after $maxRetries attempts. " + |
| 103 | + "Check Vitess connectivity and query timeout settings.", |
| 104 | + lastException |
| 105 | + ) |
| 106 | + } |
| 107 | + |
| 108 | + private fun calculateBackoff(attempt: Int): Long { |
| 109 | + val baseDelayMs = 500L // Start with 500ms |
| 110 | + val maxDelayMs = 5000L // Cap at 5 seconds |
| 111 | + val exponentialDelay = baseDelayMs * (1L shl attempt) // 500ms, 1s, 2s, 4s... |
| 112 | + val jitter = Random.nextLong(0, 200) // Add up to 200ms jitter |
| 113 | + return minOf(exponentialDelay + jitter, maxDelayMs) |
| 114 | + } |
| 115 | +} |
0 commit comments