本文整理匯總了Golang中github.com/flike/kingshard/core/golog.Error函數的典型用法代碼示例。如果您正苦於以下問題:Golang Error函數的具體用法?Golang Error怎麽用?Golang Error使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Error函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: onConn
func (s *Server) onConn(c net.Conn) {
conn := s.newClientConn(c) //新建一個conn
defer func() {
err := recover()
if err != nil {
const size = 4096
buf := make([]byte, size)
buf = buf[:runtime.Stack(buf, false)] //獲得當前goroutine的stacktrace
golog.Error("server", "onConn", "error", 0,
"remoteAddr", c.RemoteAddr().String(),
"stack", string(buf),
)
}
conn.Close()
}()
if allowConnect := conn.IsAllowConnect(); allowConnect == false {
err := mysql.NewError(mysql.ER_ACCESS_DENIED_ERROR, "ip address access denied by kingshard.")
conn.writeError(err)
conn.Close()
return
}
if err := conn.Handshake(); err != nil {
golog.Error("server", "onConn", err.Error(), 0)
c.Close()
return
}
conn.Run()
}
示例2: Handshake
func (c *ClientConn) Handshake() error {
if err := c.writeInitialHandshake(); err != nil {
golog.Error("server", "Handshake", err.Error(),
c.connectionId, "msg", "send initial handshake error")
return err
}
if err := c.readHandshakeResponse(); err != nil {
golog.Error("server", "readHandshakeResponse",
err.Error(), c.connectionId,
"msg", "read Handshake Response error")
c.writeError(err)
return err
}
if err := c.writeOK(nil); err != nil {
golog.Error("server", "readHandshakeResponse",
"write ok fail",
c.connectionId, "error", err.Error())
return err
}
c.pkg.Sequence = 0
return nil
}
示例3: buildDeletePlan
func (r *Router) buildDeletePlan(db string, statement sqlparser.Statement) (*Plan, error) {
plan := &Plan{}
var where *sqlparser.Where
var err error
stmt := statement.(*sqlparser.Delete)
plan.Rule = r.GetRule(db, sqlparser.String(stmt.Table))
where = stmt.Where
if where != nil {
plan.Criteria = where.Expr //路由條件
err = plan.calRouteIndexs()
if err != nil {
golog.Error("Route", "BuildUpdatePlan", err.Error(), 0)
return nil, err
}
} else {
//if shard delete without where,send to all nodes and all tables
plan.RouteTableIndexs = plan.Rule.SubTableIndexs
plan.RouteNodeIndexs = makeList(0, len(plan.Rule.Nodes))
}
if plan.Rule.Type != DefaultRuleType && len(plan.RouteTableIndexs) == 0 {
golog.Error("Route", "BuildDeletePlan", errors.ErrNoCriteria.Error(), 0)
return nil, errors.ErrNoCriteria
}
//generate sql,如果routeTableindexs為空則表示不分表,不分表則發default node
err = r.generateDeleteSql(plan, stmt)
if err != nil {
return nil, err
}
return plan, nil
}
示例4: handleSelect
/*處理select語句*/
func (c *ClientConn) handleSelect(stmt *sqlparser.Select, sql string, args []interface{}) error {
bindVars := makeBindVars(args) //對於select語句,arg為空,不考慮
conns, err := c.getShardConns(true, stmt, bindVars)
if err != nil {
golog.Error("ClientConn", "handleSelect", err.Error(), c.connectionId)
return err
} else if conns == nil {
r := c.newEmptyResultset(stmt)
return c.writeResultset(c.status, r)
}
var rs []*Result
rs, err = c.executeInShard(conns, sql, args)
c.closeShardConns(conns, false)
if err != nil {
golog.Error("ClientConn", "handleSelect", err.Error(), c.connectionId)
return err
}
err = c.mergeSelectResult(rs, stmt)
if err != nil {
golog.Error("ClientConn", "handleSelect", err.Error(), c.connectionId)
}
return err
}
示例5: checkMaster
func (n *Node) checkMaster() {
db := n.Master
if db == nil {
golog.Error("Node", "checkMaster", "Master is no alive", 0)
return
}
if atomic.LoadInt32(&(db.state)) == Down {
return
}
if err := db.Ping(); err != nil {
golog.Error("Node", "checkMaster", "Ping", 0, "db.Addr", db.Addr(), "error", err.Error())
} else {
n.LastMasterPing = time.Now().Unix()
atomic.StoreInt32(&(db.state), Up)
return
}
if int64(n.DownAfterNoAlive) > 0 && time.Now().Unix()-n.LastMasterPing > int64(n.DownAfterNoAlive/time.Second) {
golog.Info("Node", "checkMaster", "Master down", 0,
"db.Addr", db.Addr(),
"Master_down_time", int64(n.DownAfterNoAlive/time.Second))
n.DownMaster(db.addr)
}
}
示例6: buildDeletePlan
func (r *Router) buildDeletePlan(statement sqlparser.Statement) (*Plan, error) {
plan := &Plan{}
var where *sqlparser.Where
stmt := statement.(*sqlparser.Delete)
plan.Rule = r.GetRule(sqlparser.String(stmt.Table))
where = stmt.Where
if where != nil {
plan.Criteria = where.Expr /*路由條件*/
} else {
plan.Rule = r.DefaultRule
}
plan.TableIndexs = makeList(0, len(plan.Rule.TableToNode))
err := plan.calRouteIndexs()
if err != nil {
golog.Error("Route", "BuildDeletePlan", err.Error(), 0)
return nil, err
}
if plan.Rule.Type != DefaultRuleType && len(plan.RouteTableIndexs) == 0 {
golog.Error("Route", "BuildDeletePlan", errors.ErrNoCriteria.Error(), 0)
return nil, errors.ErrNoCriteria
}
//generate sql,如果routeTableindexs為空則表示不分表,不分表則發default node
err = r.generateDeleteSql(plan, stmt)
if err != nil {
return nil, err
}
return plan, nil
}
示例7: handlePrepareSelect
func (c *ClientConn) handlePrepareSelect(stmt *sqlparser.Select, sql string, args []interface{}) error {
defaultRule := c.schema.rule.DefaultRule
if len(defaultRule.Nodes) == 0 {
return ErrNoDefaultNode
}
defaultNode := c.proxy.GetNode(defaultRule.Nodes[0])
//execute in Master DB
conn, err := c.getBackendConn(defaultNode, false)
if err != nil {
return err
}
if conn == nil {
r := c.newEmptyResultset(stmt)
return c.writeResultset(c.status, r)
}
var rs []*Result
rs, err = c.executeInNode(conn, sql, args)
c.closeConn(conn, false)
if err != nil {
golog.Error("ClientConn", "handlePrepareSelect", err.Error(), c.connectionId)
return err
}
err = c.mergeSelectResult(rs, stmt)
if err != nil {
golog.Error("ClientConn", "handlePrepareSelect", err.Error(), c.connectionId)
}
return err
}
示例8: handleQuery
/*處理query語句*/
func (c *ClientConn) handleQuery(sql string) (err error) {
defer func() {
if e := recover(); e != nil {
err = fmt.Errorf("execute %s error %v", sql, e)
golog.OutputSql("Error", "%s", sql)
return
}
golog.OutputSql("INFO", "%s", sql)
}()
sql = strings.TrimRight(sql, ";") //刪除sql語句最後的分號
hasHandled, err := c.handleUnsupport(sql)
if err != nil {
golog.Error("server", "parse", err.Error(), 0, "hasHandled", hasHandled)
return err
}
if hasHandled {
return nil
}
var stmt sqlparser.Statement
stmt, err = sqlparser.Parse(sql) //解析sql語句,得到的stmt是一個interface
if err != nil {
golog.Error("server", "parse", err.Error(), 0, "hasHandled", hasHandled)
return err
}
switch v := stmt.(type) {
case *sqlparser.Select:
return c.handleSelect(v, sql, nil)
case *sqlparser.Insert:
return c.handleExec(stmt, sql, nil)
case *sqlparser.Update:
return c.handleExec(stmt, sql, nil)
case *sqlparser.Delete:
return c.handleExec(stmt, sql, nil)
case *sqlparser.Replace:
return c.handleExec(stmt, sql, nil)
case *sqlparser.Set:
return c.handleSet(v)
case *sqlparser.Begin:
return c.handleBegin()
case *sqlparser.Commit:
return c.handleCommit()
case *sqlparser.Rollback:
return c.handleRollback()
case *sqlparser.SimpleSelect:
return c.handleSimpleSelect(sql, v)
case *sqlparser.Show:
return c.handleShow(sql, v)
case *sqlparser.Admin:
return c.handleAdmin(v)
default:
return fmt.Errorf("statement %T not support now", stmt)
}
return nil
}
示例9: buildSelectPlan
func (r *Router) buildSelectPlan(statement sqlparser.Statement) (*Plan, error) {
plan := &Plan{}
var where *sqlparser.Where
var err error
var tableName string
stmt := statement.(*sqlparser.Select)
switch v := (stmt.From[0]).(type) {
case *sqlparser.AliasedTableExpr:
tableName = sqlparser.String(v.Expr)
case *sqlparser.JoinTableExpr:
if ate, ok := (v.LeftExpr).(*sqlparser.AliasedTableExpr); ok {
tableName = sqlparser.String(ate.Expr)
} else {
tableName = sqlparser.String(v)
}
default:
tableName = sqlparser.String(v)
}
plan.Rule = r.GetRule(tableName) //根據表名獲得分表規則
where = stmt.Where
plan.TableIndexs = makeList(0, len(plan.Rule.TableToNode))
if where != nil {
plan.Criteria = where.Expr //路由條件
err = plan.calRouteIndexs()
if err != nil {
golog.Error("Route", "BuildSelectPlan", err.Error(), 0)
return nil, err
}
} else {
//if shard select without where,send to all nodes and all tables
plan.RouteTableIndexs = plan.TableIndexs
plan.RouteNodeIndexs = makeList(0, len(plan.Rule.Nodes))
}
if plan.Rule.Type != DefaultRuleType && len(plan.RouteTableIndexs) == 0 {
golog.Error("Route", "BuildSelectPlan", errors.ErrNoCriteria.Error(), 0)
return nil, errors.ErrNoCriteria
}
//generate sql,如果routeTableindexs為空則表示不分表,不分表則發default node
err = r.generateSelectSql(plan, stmt)
if err != nil {
return nil, err
}
return plan, nil
}
示例10: handleExec
func (c *ClientConn) handleExec(stmt sqlparser.Statement, args []interface{}) error {
plan, err := c.schema.rule.BuildPlan(stmt)
conns, err := c.getShardConns(false, plan)
if err != nil {
golog.Error("ClientConn", "handleExec", err.Error(), c.connectionId)
return err
}
if conns == nil {
return c.writeOK(nil)
}
var rs []*mysql.Result
if 1 < len(conns) {
return errors.ErrExecInMulti
}
if 1 < len(plan.RewrittenSqls) {
nodeIndex := plan.RouteNodeIndexs[0]
nodeName := plan.Rule.Nodes[nodeIndex]
txSqls := []string{"begin;"}
txSqls = append(txSqls, plan.RewrittenSqls[nodeName]...)
txSqls = append(txSqls, "commit;")
plan.RewrittenSqls[nodeName] = txSqls
}
rs, err = c.executeInMultiNodes(conns, plan.RewrittenSqls, args)
c.closeShardConns(conns, err != nil)
if err == nil {
err = c.mergeExecResult(rs)
}
return err
}
示例11: buildReplacePlan
func (r *Router) buildReplacePlan(statement sqlparser.Statement) (*Plan, error) {
plan := &Plan{}
stmt := statement.(*sqlparser.Replace)
if _, ok := stmt.Rows.(sqlparser.SelectStatement); ok {
panic(sqlparser.NewParserError("select in replace not allowed"))
}
plan.Rule = r.GetRule(sqlparser.String(stmt.Table))
plan.Criteria = plan.checkValuesType(stmt.Rows.(sqlparser.Values))
plan.TableIndexs = makeList(0, len(plan.Rule.TableToNode))
err := plan.calRouteIndexs()
if err != nil {
golog.Error("Route", "BuildReplacePlan", err.Error(), 0)
return nil, err
}
err = r.generateReplaceSql(plan, stmt)
if err != nil {
return nil, err
}
return plan, nil
}
示例12: buildInsertPlan
func (r *Router) buildInsertPlan(statement sqlparser.Statement) (*Plan, error) {
plan := &Plan{}
stmt := statement.(*sqlparser.Insert)
if _, ok := stmt.Rows.(sqlparser.SelectStatement); ok {
return nil, errors.ErrSelectInInsert
}
/*根據sql語句的表,獲得對應的分片規則*/
plan.Rule = r.GetRule(sqlparser.String(stmt.Table))
if stmt.OnDup != nil {
err := plan.Rule.checkUpdateExprs(sqlparser.UpdateExprs(stmt.OnDup))
if err != nil {
return nil, err
}
}
plan.Criteria = plan.checkValuesType(stmt.Rows.(sqlparser.Values))
plan.TableIndexs = makeList(0, len(plan.Rule.TableToNode))
err := plan.calRouteIndexs()
if err != nil {
golog.Error("Route", "BuildInsertPlan", err.Error(), 0)
return nil, err
}
err = r.generateInsertSql(plan, stmt)
if err != nil {
return nil, err
}
return plan, nil
}
示例13: calRouteIndexs
/*計算表下標和node下標 */
func (plan *Plan) calRouteIndexs() error {
nodesCount := len(plan.Rule.Nodes)
if plan.Rule.Type == DefaultRuleType {
plan.RouteNodeIndexs = []int{0}
return nil
}
if plan.Criteria == nil { //如果沒有分表條件,則是全子表掃描
if plan.Rule.Type != DefaultRuleType {
golog.Error("Plan", "calRouteIndexs", "plan have no criteria", 0,
"type", plan.Rule.Type)
return ErrNoCriteria
}
}
switch criteria := plan.Criteria.(type) {
case sqlparser.Values: //代表insert中values
tindex := plan.getInsertTableIndex(criteria)
plan.RouteTableIndexs = []int{tindex}
plan.RouteNodeIndexs = plan.TindexsToNindexs([]int{tindex})
return nil
case sqlparser.BoolExpr:
plan.RouteTableIndexs = plan.getTableIndexByBoolExpr(criteria)
plan.RouteNodeIndexs = plan.TindexsToNindexs(plan.RouteTableIndexs)
return nil
default:
plan.RouteTableIndexs = plan.TableIndexs
plan.RouteNodeIndexs = makeList(0, nodesCount)
return nil
}
}
示例14: buildReplacePlan
func (r *Router) buildReplacePlan(db string, statement sqlparser.Statement) (*Plan, error) {
plan := &Plan{}
plan.Rows = make(map[int]sqlparser.Values)
stmt := statement.(*sqlparser.Replace)
if _, ok := stmt.Rows.(sqlparser.SelectStatement); ok {
panic(sqlparser.NewParserError("select in replace not allowed"))
}
if stmt.Columns == nil {
return nil, errors.ErrIRNoColumns
}
plan.Rule = r.GetRule(db, sqlparser.String(stmt.Table))
err := plan.GetIRKeyIndex(stmt.Columns)
if err != nil {
return nil, err
}
plan.Criteria = plan.checkValuesType(stmt.Rows.(sqlparser.Values))
err = plan.calRouteIndexs()
if err != nil {
golog.Error("Route", "BuildReplacePlan", err.Error(), 0)
return nil, err
}
err = r.generateReplaceSql(plan, stmt)
if err != nil {
return nil, err
}
return plan, nil
}
示例15: checkSlave
func (n *Node) checkSlave() {
n.RLock()
if n.Slave == nil {
n.RUnlock()
return
}
slaves := make([]*DB, len(n.Slave))
copy(slaves, n.Slave)
n.RUnlock()
for i := 0; i < len(slaves); i++ {
if atomic.LoadInt32(&(slaves[i].state)) == Down {
continue
}
if err := slaves[i].Ping(); err != nil {
golog.Error("Node", "checkSlave", "Ping", 0, "db.Addr", slaves[i].Addr(), "error", err.Error())
} else {
n.LastSlavePing = time.Now().Unix()
atomic.StoreInt32(&(slaves[i].state), Up)
continue
}
if int64(n.DownAfterNoAlive) > 0 && time.Now().Unix()-n.LastSlavePing > int64(n.DownAfterNoAlive/time.Second) {
golog.Info("Node", "checkMaster", "Master down", 0,
"db.Addr", slaves[i].Addr(),
"slave_down_time", int64(n.DownAfterNoAlive/time.Second))
//If can't ping slave after DownAfterNoAlive, set slave Down
n.DownSlave(slaves[i].addr)
}
}
}