-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7370 from planetscale/gen4-fail
Gen4 fallback planning
- Loading branch information
Showing
10 changed files
with
419 additions
and
241 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
Copyright 2021 The Vitess Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package planbuilder | ||
|
||
import ( | ||
"fmt" | ||
|
||
"vitess.io/vitess/go/vt/sqlparser" | ||
"vitess.io/vitess/go/vt/vtgate/engine" | ||
) | ||
|
||
type fallbackPlanner struct { | ||
primary, fallback selectPlanner | ||
} | ||
|
||
var _ selectPlanner = (*fallbackPlanner)(nil).plan | ||
|
||
func (fp *fallbackPlanner) safePrimary(query string) func(sqlparser.Statement, ContextVSchema) (engine.Primitive, error) { | ||
primaryF := fp.primary(query) | ||
return func(stmt sqlparser.Statement, vschema ContextVSchema) (res engine.Primitive, err error) { | ||
defer func() { | ||
// if the primary planner panics, we want to catch it here so we can fall back | ||
if r := recover(); r != nil { | ||
err = fmt.Errorf("%v", r) // not using vterror since this will only be used for logging | ||
} | ||
}() | ||
res, err = primaryF(stmt, vschema) | ||
return | ||
} | ||
} | ||
|
||
func (fp *fallbackPlanner) plan(query string) func(sqlparser.Statement, ContextVSchema) (engine.Primitive, error) { | ||
primaryF := fp.safePrimary(query) | ||
backupF := fp.fallback(query) | ||
|
||
return func(stmt sqlparser.Statement, vschema ContextVSchema) (engine.Primitive, error) { | ||
res, err := primaryF(stmt, vschema) | ||
if err != nil { | ||
return backupF(stmt, vschema) | ||
} | ||
return res, nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
Copyright 2021 The Vitess Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package planbuilder | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"vitess.io/vitess/go/vt/sqlparser" | ||
|
||
"vitess.io/vitess/go/vt/vtgate/engine" | ||
) | ||
|
||
type testPlanner struct { | ||
panic interface{} | ||
err error | ||
res engine.Primitive | ||
called bool | ||
} | ||
|
||
var _ selectPlanner = (*testPlanner)(nil).plan | ||
|
||
func (tp *testPlanner) plan(_ string) func(sqlparser.Statement, ContextVSchema) (engine.Primitive, error) { | ||
return func(statement sqlparser.Statement, schema ContextVSchema) (engine.Primitive, error) { | ||
tp.called = true | ||
if tp.panic != nil { | ||
panic(tp.panic) | ||
} | ||
return tp.res, tp.err | ||
} | ||
} | ||
|
||
func TestFallbackPlanner(t *testing.T) { | ||
a := &testPlanner{} | ||
b := &testPlanner{} | ||
fb := &fallbackPlanner{ | ||
primary: a.plan, | ||
fallback: b.plan, | ||
} | ||
|
||
stmt := &sqlparser.Select{} | ||
var vschema ContextVSchema | ||
|
||
// first planner succeeds | ||
_, _ = fb.plan("query")(stmt, vschema) | ||
assert.True(t, a.called) | ||
assert.False(t, b.called) | ||
a.called = false | ||
|
||
// first planner errors | ||
a.err = fmt.Errorf("fail") | ||
_, _ = fb.plan("query")(stmt, vschema) | ||
assert.True(t, a.called) | ||
assert.True(t, b.called) | ||
|
||
a.called = false | ||
b.called = false | ||
|
||
// first planner panics | ||
a.panic = "oh noes" | ||
_, _ = fb.plan("query")(stmt, vschema) | ||
assert.True(t, a.called) | ||
assert.True(t, b.called) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters