|
| 1 | +package db |
| 2 | + |
| 3 | +import ( |
| 4 | + "flag" |
| 5 | + "fmt" |
| 6 | + "sync" |
| 7 | + "sync/atomic" |
| 8 | + |
| 9 | + lvl "github.com/Azure/sonic-mgmt-common/translib/log" |
| 10 | + "github.com/go-redis/redis/v7" |
| 11 | + log "github.com/golang/glog" |
| 12 | +) |
| 13 | + |
| 14 | +var usePools = flag.Bool("use_connection_pools", true, "use connection pools for Redis Clients") |
| 15 | + |
| 16 | +const ( |
| 17 | + POOL_SIZE = 25 |
| 18 | +) |
| 19 | + |
| 20 | +var rcm *redisClientManager |
| 21 | +var initMu = &sync.Mutex{} |
| 22 | + |
| 23 | +type redisClientManager struct { |
| 24 | + // clients holds one Redis Client for each DBNum |
| 25 | + clients [MaxDB + 1]*redis.Client |
| 26 | + mu *sync.RWMutex |
| 27 | + curTransactionalClients atomic.Int32 |
| 28 | + totalPoolClientsRequested atomic.Uint64 |
| 29 | + totalTransactionalClientsRequested atomic.Uint64 |
| 30 | +} |
| 31 | + |
| 32 | +type RedisCounters struct { |
| 33 | + CurTransactionalClients uint32 // The number of transactional clients currently opened. |
| 34 | + TotalPoolClientsRequested uint64 // The total number of Redis Clients using a connection pool requested. |
| 35 | + TotalTransactionalClientsRequested uint64 // The total number of Transactional Redis Clients requested. |
| 36 | + PoolStatsPerDB map[string]*redis.PoolStats // The pool counters for each Redis Client in the cache. |
| 37 | +} |
| 38 | + |
| 39 | +func init() { |
| 40 | + initializeRedisOpts() |
| 41 | + initializeRedisClientManager() |
| 42 | +} |
| 43 | + |
| 44 | +func initializeRedisClientManager() { |
| 45 | + initMu.Lock() |
| 46 | + defer initMu.Unlock() |
| 47 | + if rcm != nil { |
| 48 | + return |
| 49 | + } |
| 50 | + rcm = &redisClientManager{ |
| 51 | + clients: [MaxDB + 1]*redis.Client{}, |
| 52 | + mu: &sync.RWMutex{}, |
| 53 | + curTransactionalClients: atomic.Int32{}, |
| 54 | + totalPoolClientsRequested: atomic.Uint64{}, |
| 55 | + totalTransactionalClientsRequested: atomic.Uint64{}, |
| 56 | + } |
| 57 | + rcm.mu.Lock() |
| 58 | + defer rcm.mu.Unlock() |
| 59 | + for dbNum := DBNum(0); dbNum < MaxDB; dbNum++ { |
| 60 | + if len(getDBInstName(dbNum)) == 0 { |
| 61 | + continue |
| 62 | + } |
| 63 | + // Create a Redis Client for each database. |
| 64 | + rcm.clients[int(dbNum)] = createRedisClient(dbNum, POOL_SIZE) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +func createRedisClient(db DBNum, poolSize int) *redis.Client { |
| 69 | + opts := adjustRedisOpts(&Options{DBNo: db}) |
| 70 | + opts.PoolSize = poolSize |
| 71 | + client := redis.NewClient(opts) |
| 72 | + if _, err := client.Ping().Result(); err != nil { |
| 73 | + log.V(lvl.ERROR).Infof("RCM error during Redis Client creation for DBNum=%v: %v", db, err) |
| 74 | + } |
| 75 | + return client |
| 76 | +} |
| 77 | + |
| 78 | +func createRedisClientWithOpts(opts *redis.Options) *redis.Client { |
| 79 | + client := redis.NewClient(opts) |
| 80 | + if _, err := client.Ping().Result(); err != nil { |
| 81 | + log.V(lvl.ERROR).Infof("RCM error during Redis Client creation for DBNum=%v: %v", opts.DB, err) |
| 82 | + } |
| 83 | + return client |
| 84 | +} |
| 85 | + |
| 86 | +func getClient(db DBNum) *redis.Client { |
| 87 | + rcm.mu.RLock() |
| 88 | + defer rcm.mu.RUnlock() |
| 89 | + return rcm.clients[int(db)] |
| 90 | +} |
| 91 | + |
| 92 | +// RedisClient will return a Redis Client that can be used for non-transactional Redis operations. |
| 93 | +// The client returned by this function is shared among many DB readers/writers and uses |
| 94 | +// a connection pool. For transactional Redis operations, please use GetRedisClientForTransaction(). |
| 95 | +func RedisClient(db DBNum) *redis.Client { |
| 96 | + if rcm == nil { |
| 97 | + initializeRedisClientManager() |
| 98 | + } |
| 99 | + if !(*usePools) { // Connection Pooling is disabled. |
| 100 | + return TransactionalRedisClient(db) |
| 101 | + } |
| 102 | + if len(getDBInstName(db)) == 0 { |
| 103 | + log.V(lvl.ERROR).Infof("Invalid DBNum requested: %v", db) |
| 104 | + return nil |
| 105 | + } |
| 106 | + rcm.totalPoolClientsRequested.Add(1) |
| 107 | + rc := getClient(db) |
| 108 | + if rc == nil { |
| 109 | + log.V(lvl.ERROR).Infof("RCM Redis client for DBNum=%v is nil!", db) |
| 110 | + rcm.mu.Lock() |
| 111 | + defer rcm.mu.Unlock() |
| 112 | + if rc = rcm.clients[int(db)]; rc != nil { |
| 113 | + return rc |
| 114 | + } |
| 115 | + rc = createRedisClient(db, POOL_SIZE) |
| 116 | + rcm.clients[int(db)] = rc |
| 117 | + } |
| 118 | + return rc |
| 119 | +} |
| 120 | + |
| 121 | +// TransactionalRedisClient will create and return a unique Redis client. This client can be used |
| 122 | +// for transactional operations. These operations include MULTI, PSUBSCRIBE (PubSub), and SCAN. This |
| 123 | +// client must be closed using CloseRedisClient when it is no longer needed. |
| 124 | +func TransactionalRedisClient(db DBNum) *redis.Client { |
| 125 | + if rcm == nil { |
| 126 | + initializeRedisClientManager() |
| 127 | + } |
| 128 | + if len(getDBInstName(db)) == 0 { |
| 129 | + log.V(lvl.ERROR).Infof("Invalid DBNum requested: %v", db) |
| 130 | + return nil |
| 131 | + } |
| 132 | + rcm.totalTransactionalClientsRequested.Add(1) |
| 133 | + client := createRedisClient(db, 1) |
| 134 | + rcm.curTransactionalClients.Add(1) |
| 135 | + return client |
| 136 | +} |
| 137 | + |
| 138 | +func TransactionalRedisClientWithOpts(opts *redis.Options) *redis.Client { |
| 139 | + if rcm == nil { |
| 140 | + initializeRedisClientManager() |
| 141 | + } |
| 142 | + rcm.totalTransactionalClientsRequested.Add(1) |
| 143 | + opts.PoolSize = 1 |
| 144 | + client := createRedisClientWithOpts(opts) |
| 145 | + rcm.curTransactionalClients.Add(1) |
| 146 | + return client |
| 147 | +} |
| 148 | + |
| 149 | +// CloseUniqueRedisClient will close the Redis client that is passed in. |
| 150 | +func CloseRedisClient(rc *redis.Client) error { |
| 151 | + if rcm == nil { |
| 152 | + return fmt.Errorf("RCM is nil when trying to close Redis Client: %v", rc) |
| 153 | + } |
| 154 | + if rc == nil { |
| 155 | + return nil |
| 156 | + } |
| 157 | + // Closing a Redis Client with a connection pool is a no-op because these clients need to stay open. |
| 158 | + if !IsTransactionalClient(rc) { |
| 159 | + return nil |
| 160 | + } |
| 161 | + if err := rc.Close(); err != nil { |
| 162 | + return err |
| 163 | + } |
| 164 | + rcm.curTransactionalClients.Add(-1) |
| 165 | + return nil |
| 166 | +} |
| 167 | + |
| 168 | +// IsTransactionalClient returns true if rc is a transactional client and false otherwise. |
| 169 | +func IsTransactionalClient(rc *redis.Client) bool { |
| 170 | + if rc == nil { |
| 171 | + return false |
| 172 | + } |
| 173 | + return rc.Options().PoolSize == 1 |
| 174 | +} |
| 175 | + |
| 176 | +// RedisClientManagerCounters returns the counters stored in the RCM. |
| 177 | +func RedisClientManagerCounters() *RedisCounters { |
| 178 | + if rcm == nil { |
| 179 | + initializeRedisClientManager() |
| 180 | + } |
| 181 | + counters := &RedisCounters{ |
| 182 | + CurTransactionalClients: uint32(rcm.curTransactionalClients.Load()), |
| 183 | + TotalPoolClientsRequested: rcm.totalPoolClientsRequested.Load(), |
| 184 | + TotalTransactionalClientsRequested: rcm.totalTransactionalClientsRequested.Load(), |
| 185 | + PoolStatsPerDB: map[string]*redis.PoolStats{}, |
| 186 | + } |
| 187 | + rcm.mu.RLock() |
| 188 | + defer rcm.mu.RUnlock() |
| 189 | + for db, client := range rcm.clients { |
| 190 | + dbName := getDBInstName(DBNum(db)) |
| 191 | + if dbName == "" || client == nil { |
| 192 | + continue |
| 193 | + } |
| 194 | + counters.PoolStatsPerDB[dbName] = client.PoolStats() |
| 195 | + } |
| 196 | + return counters |
| 197 | +} |
0 commit comments