Skip to content

Commit

Permalink
Use swsscommon API to read database configuration (#176)
Browse files Browse the repository at this point in the history
Why I did it
sonic_db_config/db_config.go reads from database configuration json to get database configuration, but swsscommon has provided the same feature.

How I did it
Use swsscommon API to read database configuration.
Pending on below PR to clear database configuration for unit test.
sonic-net/sonic-swss-common#843

How to verify it
Run unit test.
  • Loading branch information
ganglyu authored Dec 19, 2023
1 parent 88e82d4 commit 752f1fc
Show file tree
Hide file tree
Showing 11 changed files with 446 additions and 249 deletions.
29 changes: 24 additions & 5 deletions dialout/dialout_client/dialout_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,11 @@ func setupDestGroupClients(ctx context.Context, destGroupName string) {
// start/stop/update telemetry publist client as requested
// TODO: more validation on db data
func processTelemetryClientConfig(ctx context.Context, redisDb *redis.Client, key string, op string) error {
separator, _ := sdc.GetTableKeySeparator("CONFIG_DB", sdcfg.GetDbDefaultNamespace())
ns, _ := sdcfg.GetDbDefaultNamespace()
separator, err := sdc.GetTableKeySeparator("CONFIG_DB", ns)
if err != nil {
return err
}
tableKey := "TELEMETRY_CLIENT" + separator + key
fv, err := redisDb.HGetAll(tableKey).Result()
if err != nil {
Expand Down Expand Up @@ -640,28 +644,43 @@ func processTelemetryClientConfig(ctx context.Context, redisDb *redis.Client, ke
// read configDB data for telemetry client and start publishing service for client subscription
func DialOutRun(ctx context.Context, ccfg *ClientConfig) error {
clientCfg = ccfg
dbn := sdcfg.GetDbId("CONFIG_DB", sdcfg.GetDbDefaultNamespace())
ns, _ := sdcfg.GetDbDefaultNamespace()
dbn, err := sdcfg.GetDbId("CONFIG_DB", ns)
if err != nil {
return err
}

var redisDb *redis.Client
if sdc.UseRedisLocalTcpPort == false {
addr, err := sdcfg.GetDbSock("CONFIG_DB", ns)
if err != nil {
return err
}
redisDb = redis.NewClient(&redis.Options{
Network: "unix",
Addr: sdcfg.GetDbSock("CONFIG_DB", sdcfg.GetDbDefaultNamespace()),
Addr: addr,
Password: "", // no password set
DB: dbn,
DialTimeout: 0,
})
} else {
addr, err := sdcfg.GetDbTcpAddr("CONFIG_DB", ns)
if err != nil {
return err
}
redisDb = redis.NewClient(&redis.Options{
Network: "tcp",
Addr: sdcfg.GetDbTcpAddr("CONFIG_DB", sdcfg.GetDbDefaultNamespace()),
Addr: addr,
Password: "", // no password set
DB: dbn,
DialTimeout: 0,
})
}

separator, _ := sdc.GetTableKeySeparator("CONFIG_DB", sdcfg.GetDbDefaultNamespace())
separator, err := sdc.GetTableKeySeparator("CONFIG_DB", ns)
if err != nil {
return err
}
pattern := "__keyspace@" + strconv.Itoa(int(dbn)) + "__:TELEMETRY_CLIENT" + separator
prefixLen := len(pattern)
pattern += "*"
Expand Down
30 changes: 24 additions & 6 deletions dialout/dialout_client/dialout_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,23 @@ func runServer(t *testing.T, s *sds.Server) {
}

func getRedisClient(t *testing.T) *redis.Client {
ns, _ := sdcfg.GetDbDefaultNamespace()
addr, err := sdcfg.GetDbTcpAddr("COUNTERS_DB", ns)
if err != nil {
t.Fatal("failed to get addr ", err)
}
db, err := sdcfg.GetDbId("COUNTERS_DB", ns)
if err != nil {
t.Fatal("failed to get db ", err)
}
rclient := redis.NewClient(&redis.Options{
Network: "tcp",
Addr: sdcfg.GetDbTcpAddr("COUNTERS_DB", sdcfg.GetDbDefaultNamespace()),
Addr: addr,
Password: "", // no password set
DB: sdcfg.GetDbId("COUNTERS_DB", sdcfg.GetDbDefaultNamespace()),
DB: db,
DialTimeout: 0,
})
_, err := rclient.Ping().Result()
_, err = rclient.Ping().Result()
if err != nil {
t.Fatal("failed to connect to redis server ", err)
}
Expand All @@ -124,14 +133,23 @@ func exe_cmd(t *testing.T, cmd string) {
}

func getConfigDbClient(t *testing.T) *redis.Client {
ns, _ := sdcfg.GetDbDefaultNamespace()
addr, err := sdcfg.GetDbTcpAddr("CONFIG_DB", ns)
if err != nil {
t.Fatal("failed to get addr ", err)
}
db, err := sdcfg.GetDbId("CONFIG_DB", ns)
if err != nil {
t.Fatal("failed to get db ", err)
}
rclient := redis.NewClient(&redis.Options{
Network: "tcp",
Addr: sdcfg.GetDbTcpAddr("CONFIG_DB", sdcfg.GetDbDefaultNamespace()),
Addr: addr,
Password: "", // no password set
DB: sdcfg.GetDbId("CONFIG_DB", sdcfg.GetDbDefaultNamespace()),
DB: db,
DialTimeout: 0,
})
_, err := rclient.Ping().Result()
_, err = rclient.Ping().Result()
if err != nil {
t.Fatalf("failed to connect to redis server %v", err)
}
Expand Down
16 changes: 13 additions & 3 deletions gnmi_server/connection_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,22 @@ func (cm *ConnectionManager) GetThreshold() int {
}

func (cm *ConnectionManager) PrepareRedis() {
ns := sdcfg.GetDbDefaultNamespace()
ns, _ := sdcfg.GetDbDefaultNamespace()
addr, err := sdcfg.GetDbTcpAddr("STATE_DB", ns)
if err != nil {
log.Errorf("Addr err: %v", err)
return
}
db, err := sdcfg.GetDbId("STATE_DB", ns)
if err != nil {
log.Errorf("DB err: %v", err)
return
}
rclient = redis.NewClient(&redis.Options{
Network: "tcp",
Addr: sdcfg.GetDbTcpAddr("STATE_DB", ns),
Addr: addr,
Password: "",
DB: sdcfg.GetDbId("STATE_DB", ns),
DB: db,
DialTimeout: 0,
})

Expand Down
63 changes: 43 additions & 20 deletions gnmi_server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,46 +402,64 @@ func runServer(t *testing.T, s *Server) {
}

func getRedisClientN(t *testing.T, n int, namespace string) *redis.Client {
addr, err := sdcfg.GetDbTcpAddr("COUNTERS_DB", namespace)
if err != nil {
t.Fatalf("failed to get addr %v", err)
}
rclient := redis.NewClient(&redis.Options{
Network: "tcp",
Addr: sdcfg.GetDbTcpAddr("COUNTERS_DB", namespace),
Addr: addr,
Password: "", // no password set
DB: n,
DialTimeout: 0,
})
_, err := rclient.Ping().Result()
_, err = rclient.Ping().Result()
if err != nil {
t.Fatalf("failed to connect to redis server %v", err)
}
return rclient
}

func getRedisClient(t *testing.T, namespace string) *redis.Client {

addr, err := sdcfg.GetDbTcpAddr("COUNTERS_DB", namespace)
if err != nil {
t.Fatalf("failed to get addr %v", err)
}
db, err := sdcfg.GetDbId("COUNTERS_DB", namespace)
if err != nil {
t.Fatalf("failed to get db %v", err)
}
rclient := redis.NewClient(&redis.Options{
Network: "tcp",
Addr: sdcfg.GetDbTcpAddr("COUNTERS_DB", namespace),
Addr: addr,
Password: "", // no password set
DB: sdcfg.GetDbId("COUNTERS_DB", namespace),
DB: db,
DialTimeout: 0,
})
_, err := rclient.Ping().Result()
_, err = rclient.Ping().Result()
if err != nil {
t.Fatalf("failed to connect to redis server %v", err)
}
return rclient
}

func getConfigDbClient(t *testing.T, namespace string) *redis.Client {

addr, err := sdcfg.GetDbTcpAddr("CONFIG_DB", namespace)
if err != nil {
t.Fatalf("failed to get addr %v", err)
}
db, err := sdcfg.GetDbId("CONFIG_DB", namespace)
if err != nil {
t.Fatalf("failed to get db %v", err)
}
rclient := redis.NewClient(&redis.Options{
Network: "tcp",
Addr: sdcfg.GetDbTcpAddr("CONFIG_DB", namespace),
Addr: addr,
Password: "", // no password set
DB: sdcfg.GetDbId("CONFIG_DB", namespace),
DB: db,
DialTimeout: 0,
})
_, err := rclient.Ping().Result()
_, err = rclient.Ping().Result()
if err != nil {
t.Fatalf("failed to connect to redis server %v", err)
}
Expand Down Expand Up @@ -678,7 +696,8 @@ func prepareDb(t *testing.T, namespace string) {
}

func prepareDbTranslib(t *testing.T) {
rclient := getRedisClient(t, sdcfg.GetDbDefaultNamespace())
ns, _ := sdcfg.GetDbDefaultNamespace()
rclient := getRedisClient(t, ns)
rclient.FlushDB()
rclient.Close()

Expand All @@ -698,7 +717,7 @@ func prepareDbTranslib(t *testing.T) {
t.Fatalf("read file %v err: %v", fileName, err)
}
for n, v := range rj {
rclient := getRedisClientN(t, n, sdcfg.GetDbDefaultNamespace())
rclient := getRedisClientN(t, n, ns)
loadDBNotStrict(t, rclient, v)
rclient.Close()
}
Expand Down Expand Up @@ -1208,7 +1227,8 @@ func runGnmiTestGet(t *testing.T, namespace string) {

stateDBPath := "STATE_DB"

if namespace != sdcfg.GetDbDefaultNamespace() {
ns, _ := sdcfg.GetDbDefaultNamespace()
if namespace != ns {
stateDBPath = "STATE_DB" + "/" + namespace
}

Expand Down Expand Up @@ -1444,9 +1464,10 @@ func TestGnmiGet(t *testing.T) {
s := createServer(t, 8081)
go runServer(t, s)

prepareDb(t, sdcfg.GetDbDefaultNamespace())
ns, _ := sdcfg.GetDbDefaultNamespace()
prepareDb(t, ns)

runGnmiTestGet(t, sdcfg.GetDbDefaultNamespace())
runGnmiTestGet(t, ns)

s.s.Stop()
}
Expand Down Expand Up @@ -2715,7 +2736,8 @@ func TestGnmiSubscribe(t *testing.T) {
s := createServer(t, 8081)
go runServer(t, s)

runTestSubscribe(t, sdcfg.GetDbDefaultNamespace())
ns, _ := sdcfg.GetDbDefaultNamespace()
runTestSubscribe(t, ns)

s.s.Stop()
}
Expand Down Expand Up @@ -3125,7 +3147,7 @@ func TestTableKeyOnDeletion(t *testing.T) {
var neighStateTableDeletedJson61 interface{}
json.Unmarshal(neighStateTableDeletedByte61, &neighStateTableDeletedJson61)

namespace := sdcfg.GetDbDefaultNamespace()
namespace, _ := sdcfg.GetDbDefaultNamespace()
rclient := getRedisClientN(t, 6, namespace)
defer rclient.Close()
prepareStateDb(t, namespace)
Expand Down Expand Up @@ -3411,7 +3433,7 @@ func TestConnectionDataSet(t *testing.T) {
},
},
}
namespace := sdcfg.GetDbDefaultNamespace()
namespace, _ := sdcfg.GetDbDefaultNamespace()
rclient := getRedisClientN(t, 6, namespace)
defer rclient.Close()

Expand Down Expand Up @@ -3828,8 +3850,9 @@ print('%s')
s := createServer(t, 8080)
go runServer(t, s)
defer s.s.Stop()
initFullConfigDb(t, sdcfg.GetDbDefaultNamespace())
initFullCountersDb(t, sdcfg.GetDbDefaultNamespace())
ns, _ := sdcfg.GetDbDefaultNamespace()
initFullConfigDb(t, ns)
initFullCountersDb(t, ns)

path, _ := os.Getwd()
path = filepath.Dir(path)
Expand Down
8 changes: 6 additions & 2 deletions gnmi_server/transl_sub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -890,9 +890,13 @@ type DbDataMap map[string]map[string]map[string]interface{}

func updateDb(t *testing.T, data DbDataMap) {
t.Helper()
ns, _ := dbconfig.GetDbDefaultNamespace()
for dbName, tableData := range data {
n := dbconfig.GetDbId(dbName, dbconfig.GetDbDefaultNamespace())
redis := getRedisClientN(t, n, dbconfig.GetDbDefaultNamespace())
n, err := dbconfig.GetDbId(dbName, ns)
if err != nil {
t.Fatalf("GetDbId failed: %v", err)
}
redis := getRedisClientN(t, n, ns)
defer redis.Close()
for key, fields := range tableData {
if fields == nil {
Expand Down
Loading

0 comments on commit 752f1fc

Please sign in to comment.