mirror of https://github.com/grafana/grafana.git
[xorm] Rmove engine group (#60761)
This commit is contained in:
parent
572e5a76ef
commit
33dbbbad6b
|
|
@ -49,8 +49,6 @@ type Engine struct {
|
|||
|
||||
tagHandlers map[string]tagHandler
|
||||
|
||||
engineGroup *EngineGroup
|
||||
|
||||
cachers map[string]core.Cacher
|
||||
cacherLock sync.RWMutex
|
||||
|
||||
|
|
@ -384,7 +382,7 @@ func (engine *Engine) Sql(querystring string, args ...interface{}) *Session {
|
|||
// SQL method let's you manually write raw SQL and operate
|
||||
// For example:
|
||||
//
|
||||
// engine.SQL("select * from user").Find(&users)
|
||||
// engine.SQL("select * from user").Find(&users)
|
||||
//
|
||||
// This code will execute "select * from user" and set the records to users
|
||||
func (engine *Engine) SQL(query interface{}, args ...interface{}) *Session {
|
||||
|
|
@ -805,9 +803,8 @@ func (engine *Engine) Desc(colNames ...string) *Session {
|
|||
// Asc will generate "ORDER BY column1,column2 Asc"
|
||||
// This method can chainable use.
|
||||
//
|
||||
// engine.Desc("name").Asc("age").Find(&users)
|
||||
// // SELECT * FROM user ORDER BY name DESC, age ASC
|
||||
//
|
||||
// engine.Desc("name").Asc("age").Find(&users)
|
||||
// // SELECT * FROM user ORDER BY name DESC, age ASC
|
||||
func (engine *Engine) Asc(colNames ...string) *Session {
|
||||
session := engine.NewSession()
|
||||
session.isAutoClose = true
|
||||
|
|
@ -1455,9 +1452,10 @@ func (engine *Engine) InsertOne(bean interface{}) (int64, error) {
|
|||
// Update records, bean's non-empty fields are updated contents,
|
||||
// condiBean' non-empty filds are conditions
|
||||
// CAUTION:
|
||||
// 1.bool will defaultly be updated content nor conditions
|
||||
// You should call UseBool if you have bool to use.
|
||||
// 2.float32 & float64 may be not inexact as conditions
|
||||
//
|
||||
// 1.bool will defaultly be updated content nor conditions
|
||||
// You should call UseBool if you have bool to use.
|
||||
// 2.float32 & float64 may be not inexact as conditions
|
||||
func (engine *Engine) Update(bean interface{}, condiBeans ...interface{}) (int64, error) {
|
||||
session := engine.NewSession()
|
||||
defer session.Close()
|
||||
|
|
|
|||
|
|
@ -1,219 +0,0 @@
|
|||
// Copyright 2017 The Xorm Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package xorm
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"xorm.io/core"
|
||||
)
|
||||
|
||||
// EngineGroup defines an engine group
|
||||
type EngineGroup struct {
|
||||
*Engine
|
||||
slaves []*Engine
|
||||
policy GroupPolicy
|
||||
}
|
||||
|
||||
// NewEngineGroup creates a new engine group
|
||||
func NewEngineGroup(args1 interface{}, args2 interface{}, policies ...GroupPolicy) (*EngineGroup, error) {
|
||||
var eg EngineGroup
|
||||
if len(policies) > 0 {
|
||||
eg.policy = policies[0]
|
||||
} else {
|
||||
eg.policy = RoundRobinPolicy()
|
||||
}
|
||||
|
||||
driverName, ok1 := args1.(string)
|
||||
conns, ok2 := args2.([]string)
|
||||
if ok1 && ok2 {
|
||||
engines := make([]*Engine, len(conns))
|
||||
for i, conn := range conns {
|
||||
engine, err := NewEngine(driverName, conn)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
engine.engineGroup = &eg
|
||||
engines[i] = engine
|
||||
}
|
||||
|
||||
eg.Engine = engines[0]
|
||||
eg.slaves = engines[1:]
|
||||
return &eg, nil
|
||||
}
|
||||
|
||||
master, ok3 := args1.(*Engine)
|
||||
slaves, ok4 := args2.([]*Engine)
|
||||
if ok3 && ok4 {
|
||||
master.engineGroup = &eg
|
||||
for i := 0; i < len(slaves); i++ {
|
||||
slaves[i].engineGroup = &eg
|
||||
}
|
||||
eg.Engine = master
|
||||
eg.slaves = slaves
|
||||
return &eg, nil
|
||||
}
|
||||
return nil, ErrParamsType
|
||||
}
|
||||
|
||||
// Close the engine
|
||||
func (eg *EngineGroup) Close() error {
|
||||
err := eg.Engine.Close()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for i := 0; i < len(eg.slaves); i++ {
|
||||
err := eg.slaves[i].Close()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Context returned a group session
|
||||
func (eg *EngineGroup) Context(ctx context.Context) *Session {
|
||||
sess := eg.NewSession()
|
||||
sess.isAutoClose = true
|
||||
return sess.Context(ctx)
|
||||
}
|
||||
|
||||
// NewSession returned a group session
|
||||
func (eg *EngineGroup) NewSession() *Session {
|
||||
sess := eg.Engine.NewSession()
|
||||
sess.sessionType = groupSession
|
||||
return sess
|
||||
}
|
||||
|
||||
// Master returns the master engine
|
||||
func (eg *EngineGroup) Master() *Engine {
|
||||
return eg.Engine
|
||||
}
|
||||
|
||||
// Ping tests if database is alive
|
||||
func (eg *EngineGroup) Ping() error {
|
||||
if err := eg.Engine.Ping(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, slave := range eg.slaves {
|
||||
if err := slave.Ping(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetColumnMapper set the column name mapping rule
|
||||
func (eg *EngineGroup) SetColumnMapper(mapper core.IMapper) {
|
||||
eg.Engine.ColumnMapper = mapper
|
||||
for i := 0; i < len(eg.slaves); i++ {
|
||||
eg.slaves[i].ColumnMapper = mapper
|
||||
}
|
||||
}
|
||||
|
||||
// SetConnMaxLifetime sets the maximum amount of time a connection may be reused.
|
||||
func (eg *EngineGroup) SetConnMaxLifetime(d time.Duration) {
|
||||
eg.Engine.SetConnMaxLifetime(d)
|
||||
for i := 0; i < len(eg.slaves); i++ {
|
||||
eg.slaves[i].SetConnMaxLifetime(d)
|
||||
}
|
||||
}
|
||||
|
||||
// SetDefaultCacher set the default cacher
|
||||
func (eg *EngineGroup) SetDefaultCacher(cacher core.Cacher) {
|
||||
eg.Engine.SetDefaultCacher(cacher)
|
||||
for i := 0; i < len(eg.slaves); i++ {
|
||||
eg.slaves[i].SetDefaultCacher(cacher)
|
||||
}
|
||||
}
|
||||
|
||||
// SetLogger set the new logger
|
||||
func (eg *EngineGroup) SetLogger(logger core.ILogger) {
|
||||
eg.Engine.SetLogger(logger)
|
||||
for i := 0; i < len(eg.slaves); i++ {
|
||||
eg.slaves[i].SetLogger(logger)
|
||||
}
|
||||
}
|
||||
|
||||
// SetLogLevel sets the logger level
|
||||
func (eg *EngineGroup) SetLogLevel(level core.LogLevel) {
|
||||
eg.Engine.SetLogLevel(level)
|
||||
for i := 0; i < len(eg.slaves); i++ {
|
||||
eg.slaves[i].SetLogLevel(level)
|
||||
}
|
||||
}
|
||||
|
||||
// SetMapper set the name mapping rules
|
||||
func (eg *EngineGroup) SetMapper(mapper core.IMapper) {
|
||||
eg.Engine.SetMapper(mapper)
|
||||
for i := 0; i < len(eg.slaves); i++ {
|
||||
eg.slaves[i].SetMapper(mapper)
|
||||
}
|
||||
}
|
||||
|
||||
// SetMaxIdleConns set the max idle connections on pool, default is 2
|
||||
func (eg *EngineGroup) SetMaxIdleConns(conns int) {
|
||||
eg.Engine.db.SetMaxIdleConns(conns)
|
||||
for i := 0; i < len(eg.slaves); i++ {
|
||||
eg.slaves[i].db.SetMaxIdleConns(conns)
|
||||
}
|
||||
}
|
||||
|
||||
// SetMaxOpenConns is only available for go 1.2+
|
||||
func (eg *EngineGroup) SetMaxOpenConns(conns int) {
|
||||
eg.Engine.db.SetMaxOpenConns(conns)
|
||||
for i := 0; i < len(eg.slaves); i++ {
|
||||
eg.slaves[i].db.SetMaxOpenConns(conns)
|
||||
}
|
||||
}
|
||||
|
||||
// SetPolicy set the group policy
|
||||
func (eg *EngineGroup) SetPolicy(policy GroupPolicy) *EngineGroup {
|
||||
eg.policy = policy
|
||||
return eg
|
||||
}
|
||||
|
||||
// SetTableMapper set the table name mapping rule
|
||||
func (eg *EngineGroup) SetTableMapper(mapper core.IMapper) {
|
||||
eg.Engine.TableMapper = mapper
|
||||
for i := 0; i < len(eg.slaves); i++ {
|
||||
eg.slaves[i].TableMapper = mapper
|
||||
}
|
||||
}
|
||||
|
||||
// ShowExecTime show SQL statement and execute time or not on logger if log level is great than INFO
|
||||
func (eg *EngineGroup) ShowExecTime(show ...bool) {
|
||||
eg.Engine.ShowExecTime(show...)
|
||||
for i := 0; i < len(eg.slaves); i++ {
|
||||
eg.slaves[i].ShowExecTime(show...)
|
||||
}
|
||||
}
|
||||
|
||||
// ShowSQL show SQL statement or not on logger if log level is great than INFO
|
||||
func (eg *EngineGroup) ShowSQL(show ...bool) {
|
||||
eg.Engine.ShowSQL(show...)
|
||||
for i := 0; i < len(eg.slaves); i++ {
|
||||
eg.slaves[i].ShowSQL(show...)
|
||||
}
|
||||
}
|
||||
|
||||
// Slave returns one of the physical databases which is a slave according the policy
|
||||
func (eg *EngineGroup) Slave() *Engine {
|
||||
switch len(eg.slaves) {
|
||||
case 0:
|
||||
return eg.Engine
|
||||
case 1:
|
||||
return eg.slaves[0]
|
||||
}
|
||||
return eg.policy.Slave(eg)
|
||||
}
|
||||
|
||||
// Slaves returns all the slaves
|
||||
func (eg *EngineGroup) Slaves() []*Engine {
|
||||
return eg.slaves
|
||||
}
|
||||
|
|
@ -1,116 +0,0 @@
|
|||
// Copyright 2017 The Xorm Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package xorm
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// GroupPolicy is be used by chosing the current slave from slaves
|
||||
type GroupPolicy interface {
|
||||
Slave(*EngineGroup) *Engine
|
||||
}
|
||||
|
||||
// GroupPolicyHandler should be used when a function is a GroupPolicy
|
||||
type GroupPolicyHandler func(*EngineGroup) *Engine
|
||||
|
||||
// Slave implements the chosen of slaves
|
||||
func (h GroupPolicyHandler) Slave(eg *EngineGroup) *Engine {
|
||||
return h(eg)
|
||||
}
|
||||
|
||||
// RandomPolicy implmentes randomly chose the slave of slaves
|
||||
func RandomPolicy() GroupPolicyHandler {
|
||||
var r = rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||
return func(g *EngineGroup) *Engine {
|
||||
return g.Slaves()[r.Intn(len(g.Slaves()))]
|
||||
}
|
||||
}
|
||||
|
||||
// WeightRandomPolicy implmentes randomly chose the slave of slaves
|
||||
func WeightRandomPolicy(weights []int) GroupPolicyHandler {
|
||||
var rands = make([]int, 0, len(weights))
|
||||
for i := 0; i < len(weights); i++ {
|
||||
for n := 0; n < weights[i]; n++ {
|
||||
rands = append(rands, i)
|
||||
}
|
||||
}
|
||||
var r = rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||
|
||||
return func(g *EngineGroup) *Engine {
|
||||
var slaves = g.Slaves()
|
||||
idx := rands[r.Intn(len(rands))]
|
||||
if idx >= len(slaves) {
|
||||
idx = len(slaves) - 1
|
||||
}
|
||||
return slaves[idx]
|
||||
}
|
||||
}
|
||||
|
||||
func RoundRobinPolicy() GroupPolicyHandler {
|
||||
var pos = -1
|
||||
var lock sync.Mutex
|
||||
return func(g *EngineGroup) *Engine {
|
||||
var slaves = g.Slaves()
|
||||
|
||||
lock.Lock()
|
||||
defer lock.Unlock()
|
||||
pos++
|
||||
if pos >= len(slaves) {
|
||||
pos = 0
|
||||
}
|
||||
|
||||
return slaves[pos]
|
||||
}
|
||||
}
|
||||
|
||||
func WeightRoundRobinPolicy(weights []int) GroupPolicyHandler {
|
||||
var rands = make([]int, 0, len(weights))
|
||||
for i := 0; i < len(weights); i++ {
|
||||
for n := 0; n < weights[i]; n++ {
|
||||
rands = append(rands, i)
|
||||
}
|
||||
}
|
||||
var pos = -1
|
||||
var lock sync.Mutex
|
||||
|
||||
return func(g *EngineGroup) *Engine {
|
||||
var slaves = g.Slaves()
|
||||
lock.Lock()
|
||||
defer lock.Unlock()
|
||||
pos++
|
||||
if pos >= len(rands) {
|
||||
pos = 0
|
||||
}
|
||||
|
||||
idx := rands[pos]
|
||||
if idx >= len(slaves) {
|
||||
idx = len(slaves) - 1
|
||||
}
|
||||
return slaves[idx]
|
||||
}
|
||||
}
|
||||
|
||||
// LeastConnPolicy implements GroupPolicy, every time will get the least connections slave
|
||||
func LeastConnPolicy() GroupPolicyHandler {
|
||||
return func(g *EngineGroup) *Engine {
|
||||
var slaves = g.Slaves()
|
||||
connections := 0
|
||||
idx := 0
|
||||
for i := 0; i < len(slaves); i++ {
|
||||
openConnections := slaves[i].DB().Stats().OpenConnections
|
||||
if i == 0 {
|
||||
connections = openConnections
|
||||
idx = i
|
||||
} else if openConnections <= connections {
|
||||
connections = openConnections
|
||||
idx = i
|
||||
}
|
||||
}
|
||||
return slaves[idx]
|
||||
}
|
||||
}
|
||||
|
|
@ -13,7 +13,7 @@ import (
|
|||
"xorm.io/core"
|
||||
)
|
||||
|
||||
// Interface defines the interface which Engine, EngineGroup and Session will implementate.
|
||||
// Interface defines the interface which Engine and Session will implementate.
|
||||
type Interface interface {
|
||||
AllCols() *Session
|
||||
Alias(alias string) *Session
|
||||
|
|
@ -67,7 +67,7 @@ type Interface interface {
|
|||
Where(interface{}, ...interface{}) *Session
|
||||
}
|
||||
|
||||
// EngineInterface defines the interface which Engine, EngineGroup will implementate.
|
||||
// EngineInterface defines the interface which Engine will implementate.
|
||||
type EngineInterface interface {
|
||||
Interface
|
||||
|
||||
|
|
@ -116,5 +116,4 @@ type EngineInterface interface {
|
|||
var (
|
||||
_ Interface = &Session{}
|
||||
_ EngineInterface = &Engine{}
|
||||
_ EngineInterface = &EngineGroup{}
|
||||
)
|
||||
|
|
|
|||
|
|
@ -21,7 +21,6 @@ type sessionType int
|
|||
|
||||
const (
|
||||
engineSession sessionType = iota
|
||||
groupSession
|
||||
)
|
||||
|
||||
// Session keep a pointer to sql.DB and provides all execution of all
|
||||
|
|
|
|||
|
|
@ -50,13 +50,7 @@ func (session *Session) queryRows(sqlStr string, args ...interface{}) (*core.Row
|
|||
}
|
||||
|
||||
if session.isAutoCommit {
|
||||
var db *core.DB
|
||||
if session.sessionType == groupSession {
|
||||
db = session.engine.engineGroup.Slave().DB()
|
||||
} else {
|
||||
db = session.DB()
|
||||
}
|
||||
|
||||
db := session.DB()
|
||||
if session.prepareStmt {
|
||||
// don't clear stmt since session will cache them
|
||||
stmt, err := session.doPrepare(db, sqlStr)
|
||||
|
|
|
|||
Loading…
Reference in New Issue