-
Notifications
You must be signed in to change notification settings - Fork 94
[multi-DB] Part 5: Golang API changes and replacement #24
Changes from 5 commits
824ee76
1051d14
84ee240
113256c
049e6ec
d47d1a7
b0fb1c7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,7 @@ import ( | |
// Register supported client types. | ||
spb "github.com/Azure/sonic-telemetry/proto" | ||
sdc "github.com/Azure/sonic-telemetry/sonic_data_client" | ||
sdcfg "github.com/Azure/sonic-telemetry/sonic_db_config" | ||
gclient "github.com/jipanyang/gnmi/client/gnmi" | ||
) | ||
|
||
|
@@ -158,7 +159,7 @@ func getRedisClient(t *testing.T) *redis.Client { | |
dbn := spb.Target_value["COUNTERS_DB"] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Should we remove enum Target? Everything should come from json config. #Closed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't remove it. Instead, updating the map values with the data read from database_config.json file when do init() in proto pkg. After that the we register use the same name and struct. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suggest follow the design of c++ or python. something like In reply to: 344860708 [](ancestors = 344860708) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added GetDbId() |
||
rclient := redis.NewClient(&redis.Options{ | ||
Network: "tcp", | ||
Addr: "localhost:6379", | ||
Addr: sdcfg.GetDbTcpAddr("COUNTERS_DB"), | ||
Password: "", // no password set | ||
DB: int(dbn), | ||
DialTimeout: 0, | ||
|
@@ -174,7 +175,7 @@ func getConfigDbClient(t *testing.T) *redis.Client { | |
dbn := spb.Target_value["CONFIG_DB"] | ||
rclient := redis.NewClient(&redis.Options{ | ||
Network: "tcp", | ||
Addr: "localhost:6379", | ||
Addr: sdcfg.GetDbTcpAddr("CONFIG_DB"), | ||
Password: "", // no password set | ||
DB: int(dbn), | ||
DialTimeout: 0, | ||
|
@@ -203,7 +204,7 @@ func loadConfigDB(t *testing.T, rclient *redis.Client, mpi map[string]interface{ | |
func prepareConfigDb(t *testing.T) { | ||
rclient := getConfigDbClient(t) | ||
defer rclient.Close() | ||
rclient.FlushDb() | ||
rclient.FlushDB() | ||
|
||
fileName := "../testdata/COUNTERS_PORT_ALIAS_MAP.txt" | ||
countersPortAliasMapByte, err := ioutil.ReadFile(fileName) | ||
|
@@ -225,7 +226,7 @@ func prepareConfigDb(t *testing.T) { | |
func prepareDb(t *testing.T) { | ||
rclient := getRedisClient(t) | ||
defer rclient.Close() | ||
rclient.FlushDb() | ||
rclient.FlushDB() | ||
//Enable keysapce notification | ||
os.Setenv("PATH", "/usr/bin:/sbin:/bin:/usr/local/bin") | ||
cmd := exec.Command("redis-cli", "config", "set", "notify-keyspace-events", "KEA") | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
// Package dbconfig provides a generic functions for parsing sonic database config file in system | ||
package dbconfig | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"strconv" | ||
io "io/ioutil" | ||
) | ||
|
||
const ( | ||
SONIC_DB_CONFIG_FILE string = "/var/run/redis/sonic-db/database_config.json" | ||
) | ||
|
||
var sonic_db_config = make(map[string]interface{}) | ||
var sonic_db_init bool | ||
|
||
func GetDbList()(map[string]interface{}) { | ||
if !sonic_db_init { | ||
DbInit() | ||
} | ||
db_list, ok := sonic_db_config["DATABASES"].(map[string]interface{}) | ||
if !ok { | ||
panic(fmt.Errorf("DATABASES' is not valid key in database_config.json file!")) | ||
} | ||
return db_list | ||
} | ||
|
||
func GetDbInst(db_name string)(map[string]interface{}) { | ||
if !sonic_db_init { | ||
DbInit() | ||
} | ||
db, ok := sonic_db_config["DATABASES"].(map[string]interface{})[db_name] | ||
if !ok { | ||
panic(fmt.Errorf("database name '%v' is not valid in database_config.json file!", db_name)) | ||
} | ||
inst_name, ok := db.(map[string]interface{})["instance"] | ||
if !ok { | ||
panic(fmt.Errorf("'instance' is not a valid field in database_config.json file!")) | ||
} | ||
inst, ok := sonic_db_config["INSTANCES"].(map[string]interface{})[inst_name.(string)] | ||
if !ok { | ||
panic(fmt.Errorf("instance name '%v' is not valid in database_config.json file!", inst_name)) | ||
} | ||
return inst.(map[string]interface{}) | ||
} | ||
|
||
func GetDbSeparator(db_name string)(string) { | ||
if !sonic_db_init { | ||
DbInit() | ||
} | ||
db_list := GetDbList() | ||
separator, ok := db_list[db_name].(map[string]interface{})["separator"] | ||
if !ok { | ||
panic(fmt.Errorf("'separator' is not a valid field in database_config.json file!")) | ||
} | ||
return separator.(string) | ||
} | ||
|
||
func GetDbSock(db_name string)(string) { | ||
if !sonic_db_init { | ||
DbInit() | ||
} | ||
inst := GetDbInst(db_name) | ||
unix_socket_path, ok := inst["unix_socket_path"] | ||
if !ok { | ||
panic(fmt.Errorf("'unix_socket_path' is not a valid field in database_config.json file!")) | ||
} | ||
return unix_socket_path.(string) | ||
} | ||
|
||
func GetDbHostName(db_name string)(string) { | ||
if !sonic_db_init { | ||
DbInit() | ||
} | ||
inst := GetDbInst(db_name) | ||
hostname, ok := inst["hostname"] | ||
if !ok { | ||
panic(fmt.Errorf("'hostname' is not a valid field in database_config.json file!")) | ||
} | ||
return hostname.(string) | ||
} | ||
|
||
func GetDbPort(db_name string)(int) { | ||
if !sonic_db_init { | ||
DbInit() | ||
} | ||
inst := GetDbInst(db_name) | ||
port, ok := inst["port"] | ||
if !ok { | ||
panic(fmt.Errorf("'port' is not a valid field in database_config.json file!")) | ||
} | ||
return int(port.(float64)) | ||
} | ||
|
||
func GetDbTcpAddr(db_name string)(string) { | ||
if !sonic_db_init { | ||
DbInit() | ||
} | ||
hostname := GetDbHostName(db_name) | ||
port := GetDbPort(db_name) | ||
return hostname + ":" + strconv.Itoa(port) | ||
} | ||
|
||
func DbInit() { | ||
if sonic_db_init { | ||
return | ||
} | ||
data, err := io.ReadFile(SONIC_DB_CONFIG_FILE) | ||
if err != nil { | ||
panic(err) | ||
} else { | ||
err = json.Unmarshal([]byte(data), &sonic_db_config) | ||
if err != nil { | ||
panic(err) | ||
} | ||
sonic_db_init = true | ||
} | ||
} | ||
|
||
func init() { | ||
sonic_db_init = false | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not familiar with golang.
Seems this is a build target, why not use
go build
directly, instead ofgo get
. Then we don't needmkdir
andcp
#ClosedThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
go build still require ${GOPATH}/src as source codes workspace, we still need mkdir and cp
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My understanding is that you can run
go build
without a target, it just compile *.go in current directory.In reply to: 344968232 [](ancestors = 344968232)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did some search online, looks after go version 1.11, go modules are introduced to solve the $GOPATH problem. We can use go mod init and go install to remove the dependency on GOPATH. Also looks our go version in docker is 1.11.5 which is updated this July. Then we are able to build using local source files without cp. Makefile is updated.