Skip to content

Commit

Permalink
pass on the error from lookup execute and wrap if needed
Browse files Browse the repository at this point in the history
Signed-off-by: Harshit Gangal <[email protected]>
  • Loading branch information
harshit-gangal committed Dec 8, 2023
1 parent 27f1ac2 commit 38e8f90
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 9 deletions.
43 changes: 40 additions & 3 deletions go/test/endtoend/vtgate/misc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ import (
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"vitess.io/vitess/go/mysql"
"vitess.io/vitess/go/mysql/sqlerror"
"vitess.io/vitess/go/test/endtoend/utils"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestInsertNeg(t *testing.T) {
Expand Down Expand Up @@ -789,3 +789,40 @@ func TestRowCountExceed(t *testing.T) {

utils.AssertContainsError(t, conn, "select id1 from t1 where id1 < 1000", `Row count exceeded 100`)
}

func TestLookupErrorMetric(t *testing.T) {
conn, closer := start(t)
defer closer()

var errCount float64
apiErr := getVar(t, "VtgateApiErrorCounts")
if apiErr != nil {
mapErrors := apiErr.(map[string]interface{})
val, exists := mapErrors["Execute.ks.primary.ALREADY_EXISTS"]
if exists {
errCount = val.(float64)
}
}

utils.Exec(t, conn, `insert into t1 values (1,1)`)
_, err := utils.ExecAllowError(t, conn, `insert into t1 values (2,1)`)
require.ErrorContains(t, err, `(errno 1062) (sqlstate 23000)`)

apiErr = getVar(t, "VtgateApiErrorCounts")
require.NotNil(t, apiErr)
mapErrors := apiErr.(map[string]interface{})
val, exists := mapErrors["Execute.ks.primary.ALREADY_EXISTS"]
require.True(t, exists)
require.EqualValues(t, errCount+1, val)
}

func getVar(t *testing.T, key string) interface{} {
vars, err := clusterInstance.VtgateProcess.GetVars()
require.NoError(t, err)

val, exists := vars[key]
if !exists {
return nil
}
return val
}
2 changes: 1 addition & 1 deletion go/vt/vterrors/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var (
VT03003 = errorWithState("VT03003", vtrpcpb.Code_INVALID_ARGUMENT, UnknownTable, "unknown table '%s' in MULTI DELETE", "The specified table in this DELETE statement is unknown.")
VT03004 = errorWithState("VT03004", vtrpcpb.Code_INVALID_ARGUMENT, NonUpdateableTable, "the target table %s of the DELETE is not updatable", "You cannot delete something that is not a real MySQL table.")
VT03005 = errorWithState("VT03005", vtrpcpb.Code_INVALID_ARGUMENT, WrongGroupField, "cannot group on '%s'", "The planner does not allow grouping on certain field. For instance, aggregation function.")
VT03006 = errorWithState("VT03006", vtrpcpb.Code_INVALID_ARGUMENT, WrongValueCountOnRow, "column count does not match value count at row 1", "The number of columns you want to insert do not match the number of columns of your SELECT query.")
VT03006 = errorWithState("VT03006", vtrpcpb.Code_INVALID_ARGUMENT, WrongValueCountOnRow, "column count does not match value count with the row", "The number of columns you want to insert do not match the number of columns of your SELECT query.")
VT03007 = errorWithoutState("VT03007", vtrpcpb.Code_INVALID_ARGUMENT, "keyspace not specified", "You need to add a keyspace qualifier.")
VT03008 = errorWithState("VT03008", vtrpcpb.Code_INVALID_ARGUMENT, CantUseOptionHere, "incorrect usage/placement of '%s'", "The given token is not usable in this situation. Please refer to the MySQL documentation to learn more about your token's syntax.")
VT03009 = errorWithState("VT03009", vtrpcpb.Code_INVALID_ARGUMENT, WrongValueForVar, "unexpected value type for '%s': %v", "You cannot assign this type to the given variable.")
Expand Down
8 changes: 3 additions & 5 deletions go/vt/vtgate/vindexes/lookup_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,11 @@ import (
"strconv"
"strings"

"vitess.io/vitess/go/vt/vterrors"

"vitess.io/vitess/go/sqltypes"

querypb "vitess.io/vitess/go/vt/proto/query"
vtgatepb "vitess.io/vitess/go/vt/proto/vtgate"
vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc"
"vitess.io/vitess/go/vt/vterrors"
)

const (
Expand Down Expand Up @@ -304,7 +302,7 @@ nextRow:
// We only need to check the first row. Number of cols per row
// is guaranteed by the engine to be uniform.
if len(trimmedRowsCols[0]) != len(lkp.FromColumns) {
return fmt.Errorf("lookup.Create: column vindex count does not match the columns in the lookup: %d vs %v", len(trimmedRowsCols[0]), lkp.FromColumns)
return vterrors.Wrapf(vterrors.VT03006(), "lookup.Create: column vindex count does not match the columns in the lookup: %d vs %v", len(trimmedRowsCols[0]), lkp.FromColumns)
}
sort.Sort(&sorter{rowsColValues: trimmedRowsCols, toValues: trimmedToValues})

Expand Down Expand Up @@ -348,7 +346,7 @@ nextRow:
}

if _, err := vcursor.Execute(ctx, "VindexCreate", buf.String(), bindVars, true /* rollbackOnError */, co); err != nil {
return fmt.Errorf("lookup.Create: %v", err)
return vterrors.Wrap(err, "lookup.Create")
}
return nil
}
Expand Down

0 comments on commit 38e8f90

Please sign in to comment.