Skip to content
This repository has been archived by the owner on Aug 31, 2022. It is now read-only.

[multi-DB] Part 5: Golang API changes and replacement #24

Merged
merged 7 commits into from
Nov 22, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,25 @@ export GOPATH=/tmp/go
endif

INSTALL := /usr/bin/install
DBDIR := /var/run/redis/sonic-db/

all: sonic-telemetry

sonic-telemetry:
Copy link
Contributor

@qiluo-msft qiluo-msft Nov 12, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sonic-telemetry [](start = 0, length = 15)

I am not familiar with golang.
Seems this is a build target, why not use go build directly, instead of go get. Then we don't need mkdir and cp #Closed

Copy link
Contributor Author

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

Copy link
Contributor

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)

Copy link
Contributor Author

@dzhangalibaba dzhangalibaba Nov 13, 2019

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.

# copy sonic-telemetry source code into ${GOPATH}/src directory for building, otherwise it is not using committed codes
mkdir -p ${GOPATH}/src/github.com/Azure
cp -r ../sonic-telemetry ${GOPATH}/src/github.com/Azure/
# go get won't overwrite existing ${GOPATH}/src/sonic-telemetry directory and download other package
/usr/local/go/bin/go get -v github.com/Azure/sonic-telemetry/telemetry
/usr/local/go/bin/go get -v github.com/Azure/sonic-telemetry/dialout/dialout_client_cli

check:
sudo mkdir -p ${DBDIR}
sudo cp ./testdata/database_config.json ${DBDIR}
/usr/local/go/bin/go get -v -t github.com/Azure/sonic-telemetry/gnmi_server/...
/usr/local/go/bin/go test -v ${GOPATH}/src/github.com/Azure/sonic-telemetry/gnmi_server
/usr/local/go/bin/go get -v -t github.com/Azure/sonic-telemetry/dialout/dialout_client/...
/usr/local/go/bin/go test -v ${GOPATH}/src/github.com/Azure/sonic-telemetry/dialout/dialout_client

install:
$(INSTALL) -D ${GOPATH}/bin/telemetry $(DESTDIR)/usr/sbin/telemetry
Expand Down
5 changes: 3 additions & 2 deletions dialout/dialout_client/dialout_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
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"
"github.com/go-redis/redis"
log "github.com/golang/glog"
gpb "github.com/openconfig/gnmi/proto/gnmi"
Expand Down Expand Up @@ -647,15 +648,15 @@ func DialOutRun(ctx context.Context, ccfg *ClientConfig) error {
if sdc.UseRedisLocalTcpPort == false {
redisDb = redis.NewClient(&redis.Options{
Network: "unix",
Addr: sdc.Default_REDIS_UNIXSOCKET,
Addr: sdcfg.GetDbSock("CONFIG_DB"),
Password: "", // no password set
DB: int(dbn),
DialTimeout: 0,
})
} else {
redisDb = redis.NewClient(&redis.Options{
Network: "tcp",
Addr: sdc.Default_REDIS_LOCAL_TCP_PORT,
Addr: sdcfg.GetDbTcpAddr("CONFIG_DB"),
Password: "", // no password set
DB: int(dbn),
DialTimeout: 0,
Expand Down
9 changes: 5 additions & 4 deletions dialout/dialout_client/dialout_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
sds "github.com/Azure/sonic-telemetry/dialout/dialout_server"
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/openconfig/gnmi/client/gnmi"
)

Expand Down Expand Up @@ -98,7 +99,7 @@ func getRedisClient(t *testing.T) *redis.Client {
dbn := spb.Target_value["COUNTERS_DB"]
rclient := redis.NewClient(&redis.Options{
Network: "tcp",
Addr: "localhost:6379",
Addr: sdcfg.GetDbTcpAddr("COUNTERS_DB"),
Password: "", // no password set
DB: int(dbn),
DialTimeout: 0,
Expand Down Expand Up @@ -128,7 +129,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,
Expand Down Expand Up @@ -157,7 +158,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)
Expand All @@ -179,7 +180,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", "$PATH:/usr/bin:/sbin:/bin:/usr/local/bin:/usr/local/Cellar/redis/4.0.8/bin")
cmd := exec.Command("redis-cli", "config", "set", "notify-keyspace-events", "KEA")
Expand Down
9 changes: 5 additions & 4 deletions gnmi_server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -158,7 +159,7 @@ func getRedisClient(t *testing.T) *redis.Client {
dbn := spb.Target_value["COUNTERS_DB"]
Copy link
Contributor

@qiluo-msft qiluo-msft Nov 11, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Target_value [](start = 12, length = 12)

Should we remove enum Target? Everything should come from json config. #Closed

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest follow the design of c++ or python. something like sdcfg.GetDbId("COUNTERS_DB")


In reply to: 344860708 [](ancestors = 344860708)

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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,
Expand All @@ -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,
Expand Down Expand Up @@ -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)
Expand All @@ -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")
Expand Down
41 changes: 11 additions & 30 deletions proto/sonic.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 4 additions & 13 deletions sonic_data_client/db_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
log "github.com/golang/glog"

spb "github.com/Azure/sonic-telemetry/proto"
sdcfg "github.com/Azure/sonic-telemetry/sonic_db_config"
"github.com/go-redis/redis"
gnmipb "github.com/openconfig/gnmi/proto/gnmi"
"github.com/workiva/go-datastructures/queue"
Expand All @@ -24,8 +25,6 @@ const (
// indentString represents the default indentation string used for
// JSON. Two spaces are used here.
indentString string = " "
Default_REDIS_UNIXSOCKET string = "/var/run/redis/redis.sock"
Default_REDIS_LOCAL_TCP_PORT string = "localhost:6379"
)

// Client defines a set of methods which every client must implement.
Expand Down Expand Up @@ -304,15 +303,7 @@ func GetTableKeySeparator(target string) (string, error) {
return "", fmt.Errorf("%v not a valid path target", target)
}

var separator string
switch target {
case "CONFIG_DB":
separator = "|"
case "STATE_DB":
separator = "|"
default:
separator = ":"
}
var separator string = sdcfg.GetDbSeparator(target)
return separator, nil
}

Expand All @@ -325,7 +316,7 @@ func useRedisTcpClient() {
if UseRedisLocalTcpPort {
redisDb = redis.NewClient(&redis.Options{
Network: "tcp",
Addr: Default_REDIS_LOCAL_TCP_PORT,
Addr: sdcfg.GetDbTcpAddr(dbName),
Password: "", // no password set
DB: int(dbn),
DialTimeout: 0,
Expand All @@ -345,7 +336,7 @@ func init() {

redisDb = redis.NewClient(&redis.Options{
Network: "unix",
Addr: Default_REDIS_UNIXSOCKET,
Addr: sdcfg.GetDbSock(dbName),
Password: "", // no password set
DB: int(dbn),
DialTimeout: 0,
Expand Down
123 changes: 123 additions & 0 deletions sonic_db_config/db_config.go
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
}
Loading