Skip to content

Commit

Permalink
Remove invalid db type definitions: ERROR_DB, USER_DB (sonic-net#94)
Browse files Browse the repository at this point in the history
* Removed ERROR_DB & USER_DB db types and all their references
* Return error from NewDB() if unsupported DbNum is passed
* Handle gaps in DBNum values while creating 'all db objects' array
* Ignore nil db objects in DeleteDB()
  • Loading branch information
sachinholla authored Jul 5, 2023
1 parent 38eef09 commit 341fd73
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 18 deletions.
17 changes: 7 additions & 10 deletions translib/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,6 @@ const (
FlexCounterDB // 5
StateDB // 6
SnmpDB // 7
ErrorDB // 8
UserDB // 9
// All DBs added above this line, please ----
MaxDB // The Number of DBs
)
Expand Down Expand Up @@ -322,10 +320,6 @@ func getDBInstName (dbNo DBNum) string {
return "STATE_DB"
case SnmpDB:
return "SNMP_OVERLAY_DB"
case ErrorDB:
return "ERROR_DB"
case UserDB:
return "USER_DB"
}
return ""
}
Expand All @@ -345,8 +339,6 @@ func GetdbNameToIndex(dbName string) DBNum {
dbIndex = FlexCounterDB
case "STATE_DB":
dbIndex = StateDB
case "ERROR_DB":
dbIndex = ErrorDB
}
return dbIndex
}
Expand Down Expand Up @@ -382,7 +374,9 @@ func NewDB(opt Options) (*DB, error) {
glog.Warning("Database instance not present for the Db name: ", dbInstName)
}
} else {
glog.Error(fmt.Errorf("Invalid database number %d", dbId))
glog.Errorf("NewDB: invalid database number: %d", dbId)
e = tlerr.TranslibDBCannotOpen{}
goto NewDBExit
}

if opt.IsOnChangeEnabled && !opt.IsWriteDisabled {
Expand Down Expand Up @@ -428,7 +422,7 @@ func NewDB(opt Options) (*DB, error) {

if len(d.Opts.InitIndicator) == 0 {

glog.Info("NewDB: Init indication not requested")
glog.V(5).Info("NewDB: Init indication not requested")

} else if init, _ := d.client.Get(d.Opts.InitIndicator).Int(); init != 1 {

Expand All @@ -450,6 +444,9 @@ NewDBExit:

// DeleteDB is the gentle way to close the DB connection.
func (d *DB) DeleteDB() error {
if d == nil {
return nil
}

if glog.V(3) {
glog.Info("DeleteDB: Begin: d: ", d)
Expand Down
5 changes: 0 additions & 5 deletions translib/db/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,6 @@ var dbConfig = `
"id" : 7,
"separator": "|",
"instance" : "redis"
},
"ERROR_DB" : {
"id" : 8,
"separator": ":",
"instance" : "redis"
}
},
"VERSION" : "1.0"
Expand Down
2 changes: 1 addition & 1 deletion translib/transformer/transformer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func getDBOptions(dbNo db.DBNum, isWriteDisabled bool) db.Options {
case db.ApplDB, db.CountersDB, db.AsicDB, db.FlexCounterDB:
opt = getDBOptionsWithSeparator(dbNo, "", ":", ":", isWriteDisabled)
break
case db.ConfigDB, db.StateDB, db.ErrorDB:
case db.ConfigDB, db.StateDB:
opt = getDBOptionsWithSeparator(dbNo, "", "|", "|", isWriteDisabled)
break
}
Expand Down
4 changes: 2 additions & 2 deletions translib/transformer/xlate_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -562,9 +562,9 @@ func getDBOptions(dbNo db.DBNum) db.Options {
var opt db.Options

switch dbNo {
case db.ApplDB, db.CountersDB:
case db.ApplDB, db.CountersDB, db.FlexCounterDB, db.AsicDB:
opt = getDBOptionsWithSeparator(dbNo, "", ":", ":")
case db.FlexCounterDB, db.AsicDB, db.ConfigDB, db.StateDB, db.ErrorDB, db.UserDB:
case db.ConfigDB, db.StateDB:
opt = getDBOptionsWithSeparator(dbNo, "", "|", "|")
}

Expand Down
3 changes: 3 additions & 0 deletions translib/translib.go
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,9 @@ func getAllDbs(opts ...func(*db.Options)) ([db.MaxDB]*db.DB, error) {
var dbs [db.MaxDB]*db.DB
var err error
for dbNum := db.DBNum(0); dbNum < db.MaxDB; dbNum++ {
if len(dbNum.Name()) == 0 {
continue
}
dbs[dbNum], err = db.NewDB(getDBOptions(dbNum, opts...))
if err != nil {
closeAllDbs(dbs[:])
Expand Down

0 comments on commit 341fd73

Please sign in to comment.