Skip to content

Commit

Permalink
Add vtrpc.ErrorCodes to TabletErrors everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
aaijazi committed Aug 21, 2015
1 parent 230f6b1 commit abae528
Show file tree
Hide file tree
Showing 20 changed files with 330 additions and 238 deletions.
67 changes: 38 additions & 29 deletions go/vt/proto/vtrpc/vtrpc.pb.go

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

19 changes: 10 additions & 9 deletions go/vt/tabletserver/cache_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/youtube/vitess/go/pools"
"github.com/youtube/vitess/go/stats"
"github.com/youtube/vitess/go/sync2"
"github.com/youtube/vitess/go/vt/proto/vtrpc"
"golang.org/x/net/context"
)

Expand Down Expand Up @@ -100,10 +101,10 @@ func (cp *CachePool) Open() {
cp.mu.Lock()
defer cp.mu.Unlock()
if cp.pool != nil {
panic(NewTabletError(ErrFatal, "rowcache is already open"))
panic(NewTabletError(ErrFatal, vtrpc.ErrorCode_INTERNAL_ERROR, "rowcache is already open"))
}
if cp.rowCacheConfig.Binary == "" {
panic(NewTabletError(ErrFatal, "rowcache binary not specified"))
panic(NewTabletError(ErrFatal, vtrpc.ErrorCode_INTERNAL_ERROR, "rowcache binary not specified"))
}
cp.socket = generateFilename(cp.rowCacheConfig.Socket)
cp.startCacheService()
Expand All @@ -127,16 +128,16 @@ func generateFilename(hint string) string {
dir, base := path.Split(hint)
f, err := ioutil.TempFile(dir, base)
if err != nil {
panic(NewTabletError(ErrFatal, "error creating socket file: %v", err))
panic(NewTabletError(ErrFatal, vtrpc.ErrorCode_INTERNAL_ERROR, "error creating socket file: %v", err))
}
name := f.Name()
err = f.Close()
if err != nil {
panic(NewTabletError(ErrFatal, "error closing socket file: %v", err))
panic(NewTabletError(ErrFatal, vtrpc.ErrorCode_INTERNAL_ERROR, "error closing socket file: %v", err))
}
err = os.Remove(name)
if err != nil {
panic(NewTabletError(ErrFatal, "error removing socket file: %v", err))
panic(NewTabletError(ErrFatal, vtrpc.ErrorCode_INTERNAL_ERROR, "error removing socket file: %v", err))
}
log.Infof("sock filename: %v", name)
return name
Expand All @@ -146,7 +147,7 @@ func (cp *CachePool) startCacheService() {
commandLine := cp.rowCacheConfig.GetSubprocessFlags(cp.socket)
cp.cmd = exec.Command(commandLine[0], commandLine[1:]...)
if err := cp.cmd.Start(); err != nil {
panic(NewTabletError(ErrFatal, "can't start memcache: %v", err))
panic(NewTabletError(ErrFatal, vtrpc.ErrorCode_INTERNAL_ERROR, "can't start memcache: %v", err))
}
attempts := 0
for {
Expand All @@ -168,7 +169,7 @@ func (cp *CachePool) startCacheService() {
continue
}
if _, err = c.Set("health", 0, 0, []byte("ok")); err != nil {
panic(NewTabletError(ErrFatal, "can't communicate with cache service: %v", err))
panic(NewTabletError(ErrFatal, vtrpc.ErrorCode_INTERNAL_ERROR, "can't communicate with cache service: %v", err))
}
c.Close()
break
Expand Down Expand Up @@ -224,11 +225,11 @@ func (cp *CachePool) getPool() *pools.ResourcePool {
func (cp *CachePool) Get(ctx context.Context) cacheservice.CacheService {
pool := cp.getPool()
if pool == nil {
panic(NewTabletError(ErrFatal, "cache pool is not open"))
panic(NewTabletError(ErrFatal, vtrpc.ErrorCode_INTERNAL_ERROR, "cache pool is not open"))
}
r, err := pool.Get(ctx)
if err != nil {
panic(NewTabletErrorSql(ErrFatal, err))
panic(NewTabletErrorSql(ErrFatal, vtrpc.ErrorCode_INTERNAL_ERROR, err))
}
return r.(cacheservice.CacheService)
}
Expand Down
25 changes: 13 additions & 12 deletions go/vt/tabletserver/codex.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

log "github.com/golang/glog"
"github.com/youtube/vitess/go/sqltypes"
"github.com/youtube/vitess/go/vt/proto/vtrpc"
"github.com/youtube/vitess/go/vt/schema"
"github.com/youtube/vitess/go/vt/sqlparser"
)
Expand Down Expand Up @@ -44,7 +45,7 @@ func resolvePKValues(tableInfo *TableInfo, pkValues []interface{}, bindVars map[
if length == -1 {
length = len(list)
} else if len(list) != length {
return NewTabletError(ErrFail, "mismatched lengths for values %v", pkValues)
return NewTabletError(ErrFail, vtrpc.ErrorCode_BAD_INPUT, "mismatched lengths for values %v", pkValues)
}
return nil
}
Expand Down Expand Up @@ -95,14 +96,14 @@ func resolvePKValues(tableInfo *TableInfo, pkValues []interface{}, bindVars map[
func resolveListArg(col *schema.TableColumn, key string, bindVars map[string]interface{}) ([]sqltypes.Value, error) {
val, _, err := sqlparser.FetchBindVar(key, bindVars)
if err != nil {
return nil, NewTabletError(ErrFail, "%v", err)
return nil, NewTabletError(ErrFail, vtrpc.ErrorCode_BAD_INPUT, "%v", err)
}
list := val.([]interface{})
resolved := make([]sqltypes.Value, len(list))
for i, v := range list {
sqlval, err := sqltypes.BuildValue(v)
if err != nil {
return nil, NewTabletError(ErrFail, "%v", err)
return nil, NewTabletError(ErrFail, vtrpc.ErrorCode_BAD_INPUT, "%v", err)
}
if err = validateValue(col, sqlval); err != nil {
return nil, err
Expand Down Expand Up @@ -139,17 +140,17 @@ func resolveValue(col *schema.TableColumn, value interface{}, bindVars map[strin
case string:
val, _, err := sqlparser.FetchBindVar(v, bindVars)
if err != nil {
return result, NewTabletError(ErrFail, "%v", err)
return result, NewTabletError(ErrFail, vtrpc.ErrorCode_BAD_INPUT, "%v", err)
}
sqlval, err := sqltypes.BuildValue(val)
if err != nil {
return result, NewTabletError(ErrFail, "%v", err)
return result, NewTabletError(ErrFail, vtrpc.ErrorCode_BAD_INPUT, "%v", err)
}
result = sqlval
case sqltypes.Value:
result = v
default:
return result, NewTabletError(ErrFail, "incompatible value type %v", v)
return result, NewTabletError(ErrFail, vtrpc.ErrorCode_BAD_INPUT, "incompatible value type %v", v)
}

if err = validateValue(col, result); err != nil {
Expand All @@ -160,7 +161,7 @@ func resolveValue(col *schema.TableColumn, value interface{}, bindVars map[strin

func validateRow(tableInfo *TableInfo, columnNumbers []int, row []sqltypes.Value) error {
if len(row) != len(columnNumbers) {
return NewTabletError(ErrFail, "data inconsistency %d vs %d", len(row), len(columnNumbers))
return NewTabletError(ErrFail, vtrpc.ErrorCode_BAD_INPUT, "data inconsistency %d vs %d", len(row), len(columnNumbers))
}
for j, value := range row {
if err := validateValue(&tableInfo.Columns[columnNumbers[j]], value); err != nil {
Expand All @@ -178,11 +179,11 @@ func validateValue(col *schema.TableColumn, value sqltypes.Value) error {
switch col.Category {
case schema.CAT_NUMBER:
if !value.IsNumeric() {
return NewTabletError(ErrFail, "type mismatch, expecting numeric type for %v for column: %v", value, col)
return NewTabletError(ErrFail, vtrpc.ErrorCode_BAD_INPUT, "type mismatch, expecting numeric type for %v for column: %v", value, col)
}
case schema.CAT_VARBINARY:
if !value.IsString() {
return NewTabletError(ErrFail, "type mismatch, expecting string type for %v for column: %v", value, col)
return NewTabletError(ErrFail, vtrpc.ErrorCode_BAD_INPUT, "type mismatch, expecting string type for %v for column: %v", value, col)
}
}
return nil
Expand All @@ -195,7 +196,7 @@ func getLimit(limit interface{}, bv map[string]interface{}) (int64, error) {
case string:
lookup, ok := bv[lim[1:]]
if !ok {
return -1, NewTabletError(ErrFail, "missing bind var %s", lim)
return -1, NewTabletError(ErrFail, vtrpc.ErrorCode_BAD_INPUT, "missing bind var %s", lim)
}
var newlim int64
switch l := lookup.(type) {
Expand All @@ -206,10 +207,10 @@ func getLimit(limit interface{}, bv map[string]interface{}) (int64, error) {
case int:
newlim = int64(l)
default:
return -1, NewTabletError(ErrFail, "want number type for %s, got %T", lim, lookup)
return -1, NewTabletError(ErrFail, vtrpc.ErrorCode_BAD_INPUT, "want number type for %s, got %T", lim, lookup)
}
if newlim < 0 {
return -1, NewTabletError(ErrFail, "negative limit %d", newlim)
return -1, NewTabletError(ErrFail, vtrpc.ErrorCode_BAD_INPUT, "negative limit %d", newlim)
}
return newlim, nil
case int64:
Expand Down
19 changes: 13 additions & 6 deletions go/vt/tabletserver/dbconn.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/youtube/vitess/go/sqldb"
"github.com/youtube/vitess/go/sync2"
"github.com/youtube/vitess/go/vt/dbconnpool"
"github.com/youtube/vitess/go/vt/proto/vtrpc"
"golang.org/x/net/context"
)

Expand Down Expand Up @@ -58,17 +59,21 @@ func (dbc *DBConn) Exec(ctx context.Context, query string, maxrows int, wantfiel
case err == nil:
return r, nil
case !IsConnErr(err):
return nil, NewTabletErrorSql(ErrFail, err)
// MySQL error that isn't due to a connection issue
return nil, NewTabletErrorSql(ErrFail, vtrpc.ErrorCode_UNKNOWN_ERROR, err)
case attempt == 2:
return nil, NewTabletErrorSql(ErrFatal, err)
// If the MySQL connection is bad, we assume that there is nothing wrong with
// the query itself, and retrying it might succeed. The MySQL connection might
// fix itself, or the query could succeed on a different VtTablet.
return nil, NewTabletErrorSql(ErrFatal, vtrpc.ErrorCode_INTERNAL_ERROR, err)
}
err2 := dbc.reconnect()
if err2 != nil {
go checkMySQL()
return nil, NewTabletErrorSql(ErrFatal, err)
return nil, NewTabletErrorSql(ErrFatal, vtrpc.ErrorCode_INTERNAL_ERROR, err)
}
}
return nil, NewTabletErrorSql(ErrFatal, errors.New("dbconn.Exec: unreachable code"))
return nil, NewTabletErrorSql(ErrFatal, vtrpc.ErrorCode_INTERNAL_ERROR, errors.New("dbconn.Exec: unreachable code"))
}

func (dbc *DBConn) execOnce(ctx context.Context, query string, maxrows int, wantfields bool) (*mproto.QueryResult, error) {
Expand Down Expand Up @@ -134,14 +139,16 @@ func (dbc *DBConn) Kill() error {
killConn, err := dbc.pool.dbaPool.Get(0)
if err != nil {
log.Warningf("Failed to get conn from dba pool: %v", err)
return NewTabletError(ErrFail, "Failed to get conn from dba pool: %v", err)
// TODO(aaijazi): Find the right error code for an internal error that we don't want to retry
return NewTabletError(ErrFail, vtrpc.ErrorCode_INTERNAL_ERROR, "Failed to get conn from dba pool: %v", err)
}
defer killConn.Recycle()
sql := fmt.Sprintf("kill %d", dbc.conn.ID())
_, err = killConn.ExecuteFetch(sql, 10000, false)
if err != nil {
log.Errorf("Could not kill query %s: %v", dbc.Current(), err)
return NewTabletError(ErrFail, "Could not kill query %s: %v", dbc.Current(), err)
// TODO(aaijazi): Find the right error code for an internal error that we don't want to retry
return NewTabletError(ErrFail, vtrpc.ErrorCode_INTERNAL_ERROR, "Could not kill query %s: %v", dbc.Current(), err)
}
return nil
}
Expand Down
4 changes: 3 additions & 1 deletion go/vt/tabletserver/memcache_stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"math/rand"
"testing"
"time"

"github.com/youtube/vitess/go/vt/proto/vtrpc"
)

func TestMemcacheStats(t *testing.T) {
Expand Down Expand Up @@ -157,7 +159,7 @@ func TestMemcacheStatsTabletError(t *testing.T) {
memcacheStats := NewMemcacheStats(
statsPrefix, 100*time.Second, enableMain, queryServiceStats,
func(key string) string {
panic(NewTabletError(ErrFail, "unknown tablet error"))
panic(NewTabletError(ErrFail, vtrpc.ErrorCode_UNKNOWN_ERROR, "unknown tablet error"))
},
)
errCountBefore := queryServiceStats.InternalErrors.Counts()["MemcacheStats"]
Expand Down
8 changes: 6 additions & 2 deletions go/vt/tabletserver/query_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/youtube/vitess/go/vt/dbconnpool"
"github.com/youtube/vitess/go/vt/logutil"
"github.com/youtube/vitess/go/vt/mysqlctl"
"github.com/youtube/vitess/go/vt/proto/vtrpc"
)

// spotCheckMultiplier determines the precision of the
Expand Down Expand Up @@ -102,7 +103,10 @@ func getOrPanic(ctx context.Context, pool *ConnPool) *DBConn {
if err == ErrConnPoolClosed {
panic(ErrConnPoolClosed)
}
panic(NewTabletErrorSql(ErrFatal, err))
// If there's a problem with getting a connection out of the pool, that is
// probably not due to the query itself. The query might succeed on a different
// tablet.
panic(NewTabletErrorSql(ErrFatal, vtrpc.ErrorCode_INTERNAL_ERROR, err))
}

// NewQueryEngine creates a new QueryEngine.
Expand Down Expand Up @@ -224,7 +228,7 @@ func (qe *QueryEngine) Open(dbconfigs *dbconfigs.DBConfigs, schemaOverrides []Sc
strictMode = true
}
if !strictMode && dbconfigs.App.EnableRowcache {
panic(NewTabletError(ErrFatal, "Rowcache cannot be enabled when queryserver-config-strict-mode is false"))
panic(NewTabletError(ErrFatal, vtrpc.ErrorCode_INTERNAL_ERROR, "Rowcache cannot be enabled when queryserver-config-strict-mode is false"))
}
if dbconfigs.App.EnableRowcache {
qe.cachePool.Open()
Expand Down

0 comments on commit abae528

Please sign in to comment.