Skip to content

Commit

Permalink
setup norm for Nebula
Browse files Browse the repository at this point in the history
  • Loading branch information
khareyash05 committed Oct 24, 2023
1 parent 12538f7 commit c3175f3
Show file tree
Hide file tree
Showing 58 changed files with 11,364 additions and 223 deletions.
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ require (
github.com/spf13/cobra v1.7.0
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.8.3
github.com/vesoft-inc/nebula-go/v3 v3.0.0
github.com/vesoft-inc/nebula-go/v3 v3.3.1
github.com/zhihu/norm/v3 v3.0.0
go.uber.org/atomic v1.10.0
gopkg.in/natefinch/lumberjack.v2 v2.0.0
gorm.io/datatypes v1.0.7
Expand Down Expand Up @@ -120,6 +121,8 @@ require (
github.com/shopspring/decimal v1.2.0 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/stoewer/go-strcase v1.2.0 // indirect
github.com/vesoft-inc/nebula-go/v2 v2.0.0-ga // indirect
github.com/zhihu/norm v0.1.11 // indirect
go.etcd.io/etcd/api/v3 v3.5.6 // indirect
go.etcd.io/etcd/client/pkg/v3 v3.5.6 // indirect
go.etcd.io/etcd/client/v2 v2.305.6 // indirect
Expand Down
15 changes: 13 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ github.com/evanphx/json-patch v4.12.0+incompatible h1:4onqiflcdA9EOZ4RxV643DvftH
github.com/evanphx/json-patch v4.12.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
github.com/evanphx/json-patch/v5 v5.6.0 h1:b91NhWfaz02IuVxO9faSllyAtNXHMPkC5J8sJCLunww=
github.com/evanphx/json-patch/v5 v5.6.0/go.mod h1:G79N1coSVB93tBe7j6PhzjmR3/2VvlbKOFpnXhI9Bw4=
github.com/facebook/fbthrift v0.0.0-20190922225929-2f9839604e25/go.mod h1:2tncLx5rmw69e5kMBv/yJneERbzrr1yr5fdlnTbu8lU=
github.com/facebook/fbthrift v0.31.1-0.20211129061412-801ed7f9f295 h1:ZA+qQ3d2In0RNzVpk+D/nq1sjDSv+s1Wy2zrAPQAmsg=
github.com/facebook/fbthrift v0.31.1-0.20211129061412-801ed7f9f295/go.mod h1:2tncLx5rmw69e5kMBv/yJneERbzrr1yr5fdlnTbu8lU=
github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=
Expand Down Expand Up @@ -214,6 +215,7 @@ github.com/golang/mock v1.4.0/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt
github.com/golang/mock v1.4.1/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw=
github.com/golang/mock v1.4.3/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw=
github.com/golang/mock v1.4.4/go.mod h1:l3mdAwkq5BuhzHwde/uurv3sEJeZMXNpwsxVWU71h+4=
github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
Expand Down Expand Up @@ -532,15 +534,18 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.3 h1:RP3t2pwF7cMEbC1dqtB6poj3niw/9gnV4Cjg5oW5gtY=
github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/tmc/grpc-websocket-proxy v0.0.0-20201229170055-e5319fda7802 h1:uruHq4dN7GR16kFc5fp3d1RIYzJW5onx8Ybykw2YQFA=
github.com/vesoft-inc/nebula-go/v3 v3.0.0 h1:ii5T3vps4xAQZkzPvGn6NuiUWlH/rm1zdIS5VTEA71A=
github.com/vesoft-inc/nebula-go/v3 v3.0.0/go.mod h1:+sXv05jYQBARdTbTcIEsWVXCnF/6ttOlDK35xQ6m54s=
github.com/vesoft-inc/nebula-go/v2 v2.0.0-ga h1:4t2V73o4mq2io6uJitNFtJeFc6xnr8pVoCCJd/FAiRM=
github.com/vesoft-inc/nebula-go/v2 v2.0.0-ga/go.mod h1:qvrlydV8O1Jbff7b7cXSLOvZNs9BcE8pFZa/1nvB3fo=
github.com/vesoft-inc/nebula-go/v3 v3.3.1 h1:5DxUxswEQvK9gkK6Y/X4fhX+bmIeHIJrn+b2q7tE3HM=
github.com/vesoft-inc/nebula-go/v3 v3.3.1/go.mod h1:+sXv05jYQBARdTbTcIEsWVXCnF/6ttOlDK35xQ6m54s=
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2 h1:eY9dn8+vbi4tKz5Qo6v2eYzo7kUS51QINcR5jNpbZS8=
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand All @@ -550,6 +555,10 @@ github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1
github.com/yuin/goldmark v1.4.1/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
github.com/zenazn/goji v0.9.0/go.mod h1:7S9M489iMyHBNxwZnk9/EHS098H4/F6TATF2mIxtB1Q=
github.com/zhihu/norm v0.1.11 h1:uFVvfkCuYOZP19ZG7hJOWqOVDSwDOoY+auZ8UPObJcU=
github.com/zhihu/norm v0.1.11/go.mod h1:VBH+aIV1TJfLIM5kyhCD812ghzjp0XIYYv5W/sdOA5A=
github.com/zhihu/norm/v3 v3.0.0 h1:YYpaYfgQ5ysryvsabAca7uD2OGr8F6s3joiMcPPbWm8=
github.com/zhihu/norm/v3 v3.0.0/go.mod h1:9HzBaVWF/Tw2VWaiGJgz4qxS6cr9C0SjHX0S10uFHEc=
go.etcd.io/bbolt v1.3.6 h1:/ecaJf0sk1l4l6V4awd65v2C3ILy7MSj+s/x1ADCIMU=
go.etcd.io/etcd/api/v3 v3.5.6 h1:Cy2qx3npLcYqTKqGJzMypnMv2tiRyifZJ17BlWIWA7A=
go.etcd.io/etcd/api/v3 v3.5.6/go.mod h1:KFtNaxGDw4Yx/BA4iPPwevUTAuqcsPxzyX8PHydchN8=
Expand Down Expand Up @@ -879,6 +888,7 @@ golang.org/x/tools v0.0.0-20200804011535-6c149bb5ef0d/go.mod h1:njjCfa9FT2d7l9Bc
golang.org/x/tools v0.0.0-20200825202427-b303f430e36d/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA=
golang.org/x/tools v0.0.0-20201224043029-2b0845dc783e/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.2/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.10/go.mod h1:Uh6Zz+xoGYZom868N8YTex3t7RhtHDBrE8Gzo9bV56E=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
Expand Down Expand Up @@ -993,6 +1003,7 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20200902074654-038fdea0a05b/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
Expand Down
22 changes: 10 additions & 12 deletions pkg/storage/internalstorage/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

"github.com/go-sql-driver/mysql"
"github.com/jackc/pgx/v4"
gnebula "github.com/vesoft-inc/nebula-go/v3"
"github.com/zhihu/norm/v3/dialectors"
"gopkg.in/natefinch/lumberjack.v2"
"gorm.io/gorm/logger"
"k8s.io/klog/v2"
Expand Down Expand Up @@ -299,21 +299,19 @@ func (cfg *Config) genSQLiteDSN() (string, error) {
return cfg.DSN, nil
}

// Initialize logger for nebula
var nebulalog = gnebula.DefaultLogger{}

func (cfg *Config) genNebulaConfig() (*gnebula.PoolConfig, error) {
func (cfg *Config) genNebulaConfig() (*dialectors.DialectorConfig, error) {
if cfg.DSN == "" {
return nil, errors.New("nebula: dsn is required")
}
var nebulaConfig dialectors.DialectorConfig
nebulaConfig.IdleTime = cfg.Nebula.IdleTime
nebulaConfig.MaxConnPoolSize = cfg.Nebula.MaxConnPoolSize
nebulaConfig.MinConnPoolSize = cfg.Nebula.MinConnPoolSize
nebulaConfig.Timeout = cfg.Nebula.TimeOut
nebulaConfig.Username = cfg.User
nebulaConfig.Password = cfg.Password

nebulaPoolConfig := gnebula.GetDefaultConf()
nebulaPoolConfig.IdleTime = cfg.Nebula.IdleTime
nebulaPoolConfig.MaxConnPoolSize = cfg.Nebula.MaxConnPoolSize
nebulaPoolConfig.MinConnPoolSize = cfg.Nebula.MinConnPoolSize
nebulaPoolConfig.TimeOut = cfg.Nebula.TimeOut

return &nebulaPoolConfig, nil
return &nebulaConfig, nil
}

func (cfg *Config) addMysqlErrorNumbers() {
Expand Down
76 changes: 31 additions & 45 deletions pkg/storage/internalstorage/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import (
"io"
"log"
"os"
"strconv"

"github.com/go-sql-driver/mysql"
"github.com/jackc/pgx/v4/stdlib"
"github.com/jinzhu/configor"
gnebula "github.com/vesoft-inc/nebula-go/v3"
"github.com/zhihu/norm/v3"
"github.com/zhihu/norm/v3/dialectors"
"gopkg.in/natefinch/lumberjack.v2"
gmysql "gorm.io/driver/mysql"
gpostgres "gorm.io/driver/postgres"
Expand Down Expand Up @@ -42,6 +42,7 @@ func NewStorageFactory(configPath string) (storage.StorageFactory, error) {
}

var dialector gorm.Dialector
var normDialector *dialectors.NebulaDialector
switch cfg.Type {
case "mysql":
mysqlConfig, err := cfg.genMySQLConfig()
Expand Down Expand Up @@ -71,34 +72,11 @@ func NewStorageFactory(configPath string) (storage.StorageFactory, error) {
}
dialector = gsqlite.Open(dsn)
case "nebula":
nebulaconfig, err := cfg.genNebulaConfig()
NebulaConfig, err := cfg.genNebulaConfig()
if err != nil {
return nil, err
}
// TODO : dialector like for nebula
port, err := strconv.Atoi(cfg.Port)
if err != nil {
return nil, err
}

hostAddress := gnebula.HostAddress{Host: cfg.Host, Port: port}
hostList := []gnebula.HostAddress{hostAddress}
// Initialize connection pool
pool, err := gnebula.NewConnectionPool(hostList, *nebulaconfig, nebulalog)
if err != nil {
nebulalog.Fatal(fmt.Sprintf("Fail to initialize the connection pool, host: %s, port: %d, %s", cfg.Host, port, err.Error()))
}
// Close all connections in the pool
defer pool.Close()

// Create session
session, err := pool.GetSession(cfg.User, cfg.Password)
if err != nil {
nebulalog.Fatal(fmt.Sprintf("Fail to create a new session from connection pool, username: %s, password: %s, %s",
cfg.User, cfg.Password, err.Error()))
}
// Release session and return connection back to connection pool
defer session.Release()
normDialector = dialectors.MustNewNebulaDialector(*NebulaConfig)
default:
return nil, fmt.Errorf("not support storage type: %s", cfg.Type)
}
Expand All @@ -108,27 +86,35 @@ func NewStorageFactory(configPath string) (storage.StorageFactory, error) {
return nil, err
}

db, err := gorm.Open(dialector, &gorm.Config{SkipDefaultTransaction: true, Logger: logger})
if err != nil {
return nil, err
}
sqlDB, err := db.DB()
if err != nil {
return nil, err
}
connPool, err := cfg.getConnPoolConfig()
if err != nil {
return nil, err
}
sqlDB.SetMaxIdleConns(connPool.MaxIdleConns)
sqlDB.SetMaxOpenConns(connPool.MaxOpenConns)
sqlDB.SetConnMaxLifetime(connPool.ConnMaxLifetime)
if cfg.Type != "nebula" {
db, err := gorm.Open(dialector, &gorm.Config{SkipDefaultTransaction: true, Logger: logger})
if err != nil {
return nil, err
}
sqlDB, err := db.DB()
if err != nil {
return nil, err
}
connPool, err := cfg.getConnPoolConfig()
if err != nil {
return nil, err
}
sqlDB.SetMaxIdleConns(connPool.MaxIdleConns)
sqlDB.SetMaxOpenConns(connPool.MaxOpenConns)
sqlDB.SetConnMaxLifetime(connPool.ConnMaxLifetime)

if err := db.AutoMigrate(&Resource{}); err != nil {
return nil, err
if err := db.AutoMigrate(&Resource{}); err != nil {
return nil, err
}

return &StorageFactory{db}, nil
}
if cfg.Type == "nebula" {
db := norm.MustOpen(normDialector, norm.Config{})

Check failure on line 113 in pkg/storage/internalstorage/register.go

View workflow job for this annotation

GitHub Actions / Lint with golangci-lint

cannot use normDialector (variable of type *"github.com/zhihu/norm/v3/dialectors".NebulaDialector) as "github.com/zhihu/norm/dialectors".IDialector value in argument to norm.MustOpen: *"github.com/zhihu/norm/v3/dialectors".NebulaDialector does not implement "github.com/zhihu/norm/dialectors".IDialector (wrong type for method Execute)

Check failure on line 113 in pkg/storage/internalstorage/register.go

View workflow job for this annotation

GitHub Actions / Lint with golangci-lint

cannot use normDialector (variable of type *"github.com/zhihu/norm/v3/dialectors".NebulaDialector) as "github.com/zhihu/norm/dialectors".IDialector value in argument to norm.MustOpen: *"github.com/zhihu/norm/v3/dialectors".NebulaDialector does not implement "github.com/zhihu/norm/dialectors".IDialector (wrong type for method Execute)
return &StorageFactory{db}, nil // have to create a new nebula storage factory and possibly this function to cater nebula needs

Check failure on line 114 in pkg/storage/internalstorage/register.go

View workflow job for this annotation

GitHub Actions / Lint with golangci-lint

cannot use db (variable of type *"github.com/zhihu/norm/v3".DB) as *gorm.DB value in struct literal (typecheck)

Check failure on line 114 in pkg/storage/internalstorage/register.go

View workflow job for this annotation

GitHub Actions / Lint with golangci-lint

cannot use db (variable of type *"github.com/zhihu/norm/v3".DB) as *gorm.DB value in struct literal) (typecheck)
}
return nil, fmt.Errorf("Cannot create a new storage factory based on the database specified")

return &StorageFactory{db}, nil
}

func newLogger(cfg *Config) (logger.Interface, error) {
Expand Down
12 changes: 12 additions & 0 deletions vendor/github.com/vesoft-inc/nebula-go/v2/.editorconfig

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

20 changes: 20 additions & 0 deletions vendor/github.com/vesoft-inc/nebula-go/v2/.gitignore

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

21 changes: 21 additions & 0 deletions vendor/github.com/vesoft-inc/nebula-go/v2/Makefile

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

52 changes: 52 additions & 0 deletions vendor/github.com/vesoft-inc/nebula-go/v2/README.md

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

Loading

0 comments on commit c3175f3

Please sign in to comment.