Skip to content

Commit 29b4c96

Browse files
Implement RCM code
Signed-off-by: Niranjani Vivek <niranjaniv@google.com>
1 parent 2744e68 commit 29b4c96

File tree

8 files changed

+728
-36
lines changed

8 files changed

+728
-36
lines changed

translib/db/db.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,11 @@ type Options struct {
168168
KeySeparator string //Overriden by the DB config file's separator.
169169
IsWriteDisabled bool //Is write/set mode disabled ?
170170
IsCacheEnabled bool //Is cache (Per Connection) allowed?
171+
// Redis connection pools do not support transactional Redis operations.
172+
// If the DB object will be used to perform transactions, the Transacation
173+
// flag must be set to request a unique Redis client. Transactions include
174+
// SCAN and MULTI.
175+
TransactionsRequired bool
171176

172177
// OnChange caching for the DBs passed from Translib's Subscribe Infra
173178
// to the Apps. SDB is the SubscribeDB() returned handle on which
@@ -402,7 +407,15 @@ func NewDB(opt Options) (*DB, error) {
402407
var dur time.Duration
403408
now = time.Now()
404409

405-
d := DB{client: redis.NewClient(adjustRedisOpts(&opt)),
410+
var rc *redis.Client
411+
if opt.TransactionsRequired {
412+
rc = TransactionalRedisClient(opt.DBNo)
413+
} else {
414+
rc = RedisClient(opt.DBNo)
415+
}
416+
417+
d := DB{
418+
client: rc,
406419
Opts: &opt,
407420
txState: txStateNone,
408421
txCmds: make([]_txCmd, 0, InitialTxPipelineSize),
@@ -455,7 +468,7 @@ func NewDB(opt Options) (*DB, error) {
455468
if opt.IsSession && opt.IsOnChangeEnabled {
456469
glog.Error("NewDB: Subscription on Config Session not supported : ",
457470
d.Name())
458-
d.client.Close()
471+
CloseRedisClient(d.client)
459472
e = tlerr.TranslibDBNotSupported{
460473
Description: "Subscription on Config Session not supported"}
461474
goto NewDBExit
@@ -464,7 +477,7 @@ func NewDB(opt Options) (*DB, error) {
464477
if opt.IsSession && opt.DBNo != ConfigDB {
465478
glog.Error("NewDB: Non-Config DB on Config Session not supported : ",
466479
d.Name())
467-
d.client.Close()
480+
CloseRedisClient(d.client)
468481
e = tlerr.TranslibDBNotSupported{
469482
Description: "Non-Config DB on Config Session not supported"}
470483
goto NewDBExit
@@ -498,7 +511,7 @@ func NewDB(opt Options) (*DB, error) {
498511
glog.Error("NewDB: Database not inited: ", d.Name(), ": GET ",
499512
d.Opts.InitIndicator, " returns err: ", err)
500513
}
501-
d.client.Close()
514+
CloseRedisClient(d.client)
502515
e = tlerr.TranslibDBNotInit{}
503516
goto NewDBExit
504517
}
@@ -512,7 +525,7 @@ func NewDB(opt Options) (*DB, error) {
512525

513526
if e = ConfigDBTryLock(noSessionToken); e != nil {
514527
glog.Errorf("NewDB: ConfigDB possibly locked: %s", e)
515-
d.client.Close()
528+
CloseRedisClient(d.client)
516529
goto NewDBExit
517530
}
518531
d.configDBLocked = true
@@ -570,7 +583,7 @@ func (d *DB) DeleteDB() error {
570583
d.unRegisterSessionDB()
571584
}
572585

573-
err := d.client.Close()
586+
err := CloseRedisClient(d.client)
574587
d.client = nil
575588
return err
576589
}

translib/db/db_lock.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func (lt *LockStruct) tryLock() error {
7676
if client, err = getStateDB(); err != nil {
7777
return err
7878
}
79-
defer client.Close()
79+
defer CloseRedisClient(client)
8080

8181
// HSETNX: Set Hash Field if Not Exist
8282
args := []interface{}{"HSETNX", lockTableKey, lt.Name,
@@ -116,7 +116,7 @@ func (lt *LockStruct) unlock() error {
116116
if client, err = getStateDB(); err != nil {
117117
return err
118118
}
119-
defer client.Close()
119+
defer CloseRedisClient(client)
120120

121121
// Run the LUA Script to HDEL if we set the key
122122
if reply, err = luaScriptUnlock.Run(client,
@@ -265,8 +265,7 @@ func dumpStack(begin, end int) {
265265
func getStateDB() (*redis.Client, error) {
266266
var client *redis.Client
267267
var err error
268-
if client = redis.NewClient(adjustRedisOpts(&Options{
269-
DBNo: StateDB})); client == nil {
268+
if client = TransactionalRedisClient(StateDB); client == nil {
270269

271270
glog.Error("getStateDB: Could not create redis client: STATE_DB")
272271
err = tlerr.TranslibDBCannotOpen{}

translib/db/db_redis_opts.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ func adjustRedisOpts(dbOpt *Options) *redis.Options {
144144
return &redisOpts
145145
}
146146

147-
func init() {
147+
func initializeRedisOpts() {
148148
flag.StringVar(&goRedisOpts, "go_redis_opts", "", "Options for go-redis")
149149
}
150150

@@ -197,7 +197,10 @@ func (config *_DBRedisOptsConfig) handleReconfigureSignal() error {
197197
////////////////////////////////////////////////////////////////////////////////
198198

199199
func (config *_DBRedisOptsConfig) readFromDB() error {
200-
fields, e := readRedis("TRANSLIB_DB|default")
200+
rc := TransactionalRedisClient(ConfigDB)
201+
defer CloseRedisClient(rc)
202+
fields, e := rc.HGetAll("TRANSLIB_DB|default").Result()
203+
201204
if e == nil {
202205
if optsString, ok := fields["go_redis_opts"]; ok {
203206
// Parse optsString into config.opts

translib/db/db_stats.go

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ import (
2323
"reflect"
2424
"sync"
2525
"time"
26-
27-
"github.com/go-redis/redis/v7"
2826
)
2927

3028
////////////////////////////////////////////////////////////////////////////////
@@ -524,29 +522,9 @@ func (config *DBStatsConfig) readFromDB() error {
524522

525523
func readRedis(key string) (map[string]string, error) {
526524

527-
ipAddr := DefaultRedisLocalTCPEP
528-
dbId := int(ConfigDB)
529-
dbPassword := ""
530-
if dbInstName := getDBInstName(ConfigDB); dbInstName != "" {
531-
if isDbInstPresent(dbInstName) {
532-
ipAddr = getDbTcpAddr(dbInstName)
533-
dbId = getDbId(dbInstName)
534-
dbPassword = getDbPassword(dbInstName)
535-
}
536-
}
537-
538-
client := redis.NewClient(&redis.Options{
539-
Network: "tcp",
540-
Addr: ipAddr,
541-
Password: dbPassword,
542-
DB: dbId,
543-
DialTimeout: 0,
544-
PoolSize: 1,
545-
})
546-
525+
client := RedisClient(ConfigDB)
526+
defer CloseRedisClient(client)
547527
fields, e := client.HGetAll(key).Result()
548528

549-
client.Close()
550-
551529
return fields, e
552530
}

0 commit comments

Comments
 (0)