Skip to content

Commit 12538f7

Browse files
committed
create default pool and session for nebula
1 parent 8464b60 commit 12538f7

File tree

5 files changed

+49
-29
lines changed

5 files changed

+49
-29
lines changed

deploy/internalstorage/nebula/clusterpedia_internalstorage_configmap.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ data:
1212
internalstorage-config.yaml: |
1313
type: "nebula"
1414
host: "clusterpedia-internalstorage-nebula"
15-
port: 3699
15+
port: 9669
1616
user: nebula
1717
database: "clusterpedia"

deploy/internalstorage/nebula/clusterpedia_internalstorage_deployment.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ metadata:
55
namespace: clusterpedia-system
66
spec:
77
ports:
8-
- port: 3699
8+
- port: 9669
99
selector:
1010
app: clusterpedia-internalstorage
1111
internalstorage.clusterpedia.io/type: nebula
@@ -59,7 +59,7 @@ spec:
5959
key: password
6060
ports:
6161
- name: nebula
62-
containerPort: 3699
62+
containerPort: 9669
6363
volumeMounts:
6464
- name: data
6565
mountPath: /var/lib/nebulaql/data

kustomize/internalstorage/nebula/clusterpedia_internalstorage_configmap.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ data:
77
internalstorage-config.yaml: |
88
type: "nebula"
99
host: "clusterpedia-internalstorage-nebula"
10-
port: 3699
10+
port: 9669
1111
user: nebula
1212
database: "clusterpedia"

pkg/storage/internalstorage/config.go

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
"github.com/go-sql-driver/mysql"
1414
"github.com/jackc/pgx/v4"
15+
gnebula "github.com/vesoft-inc/nebula-go/v3"
1516
"gopkg.in/natefinch/lumberjack.v2"
1617
"gorm.io/gorm/logger"
1718
"k8s.io/klog/v2"
@@ -94,7 +95,6 @@ type NebulaConfig struct {
9495
IdleTime time.Duration `yaml:"idleTime"`
9596
MaxConnPoolSize int `yaml:"maxConnPoolSize"`
9697
MinConnPoolSize int `yaml:"minConnPoolSize"`
97-
UseHTTP2 bool `yaml:"useHTTP2"`
9898
}
9999

100100
type ConnPoolConfig struct {
@@ -299,26 +299,21 @@ func (cfg *Config) genSQLiteDSN() (string, error) {
299299
return cfg.DSN, nil
300300
}
301301

302-
func (cfg *Config) genNebulaDSN() (string, error) {
302+
// Initialize logger for nebula
303+
var nebulalog = gnebula.DefaultLogger{}
304+
305+
func (cfg *Config) genNebulaConfig() (*gnebula.PoolConfig, error) {
303306
if cfg.DSN == "" {
304-
return "", errors.New("nebula: dsn is required")
305-
}
306-
if cfg.Nebula.IdleTime <= 0 {
307-
return "", errors.New("nebula:Idle Time can't be less than or equal to zero")
308-
}
309-
if cfg.Nebula.MaxConnPoolSize <= 0 {
310-
return "", errors.New("nebula: Max connection pool size can't be less than or equal to zero")
311-
}
312-
if cfg.Nebula.MinConnPoolSize <= 0 {
313-
return "", errors.New("nebula: Min connection pool size can't be less than or equal to zero")
314-
}
315-
if cfg.Nebula.TimeOut <= 0 {
316-
return "", errors.New("nebula:Time Out can't be less than or equal to zero")
307+
return nil, errors.New("nebula: dsn is required")
317308
}
318-
if !cfg.Nebula.UseHTTP2 {
319-
return "", errors.New("nebula:UseHTTP2 can't be false")
320-
}
321-
return cfg.DSN, nil
309+
310+
nebulaPoolConfig := gnebula.GetDefaultConf()
311+
nebulaPoolConfig.IdleTime = cfg.Nebula.IdleTime
312+
nebulaPoolConfig.MaxConnPoolSize = cfg.Nebula.MaxConnPoolSize
313+
nebulaPoolConfig.MinConnPoolSize = cfg.Nebula.MinConnPoolSize
314+
nebulaPoolConfig.TimeOut = cfg.Nebula.TimeOut
315+
316+
return &nebulaPoolConfig, nil
322317
}
323318

324319
func (cfg *Config) addMysqlErrorNumbers() {

pkg/storage/internalstorage/register.go

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ import (
66
"io"
77
"log"
88
"os"
9+
"strconv"
910

1011
"github.com/go-sql-driver/mysql"
1112
"github.com/jackc/pgx/v4/stdlib"
1213
"github.com/jinzhu/configor"
14+
gnebula "github.com/vesoft-inc/nebula-go/v3"
1315
"gopkg.in/natefinch/lumberjack.v2"
1416
gmysql "gorm.io/driver/mysql"
1517
gpostgres "gorm.io/driver/postgres"
@@ -68,12 +70,35 @@ func NewStorageFactory(configPath string) (storage.StorageFactory, error) {
6870
return nil, err
6971
}
7072
dialector = gsqlite.Open(dsn)
71-
// case "nebula":
72-
// dsn, err := cfg.genNebulaDSN()
73-
// if err != nil {
74-
// return nil, err
75-
// }
76-
// conn,err := gnebula.NewConnectionPool()
73+
case "nebula":
74+
nebulaconfig, err := cfg.genNebulaConfig()
75+
if err != nil {
76+
return nil, err
77+
}
78+
// TODO : dialector like for nebula
79+
port, err := strconv.Atoi(cfg.Port)
80+
if err != nil {
81+
return nil, err
82+
}
83+
84+
hostAddress := gnebula.HostAddress{Host: cfg.Host, Port: port}
85+
hostList := []gnebula.HostAddress{hostAddress}
86+
// Initialize connection pool
87+
pool, err := gnebula.NewConnectionPool(hostList, *nebulaconfig, nebulalog)
88+
if err != nil {
89+
nebulalog.Fatal(fmt.Sprintf("Fail to initialize the connection pool, host: %s, port: %d, %s", cfg.Host, port, err.Error()))
90+
}
91+
// Close all connections in the pool
92+
defer pool.Close()
93+
94+
// Create session
95+
session, err := pool.GetSession(cfg.User, cfg.Password)
96+
if err != nil {
97+
nebulalog.Fatal(fmt.Sprintf("Fail to create a new session from connection pool, username: %s, password: %s, %s",
98+
cfg.User, cfg.Password, err.Error()))
99+
}
100+
// Release session and return connection back to connection pool
101+
defer session.Release()
77102
default:
78103
return nil, fmt.Errorf("not support storage type: %s", cfg.Type)
79104
}

0 commit comments

Comments
 (0)