forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reduce TiDB dependencies (pingcap#158)
* utils: exclude mock_cluster outside of unit test * utils: remove unused ResultSetToStringSlice() * *: abstract away dependencies of tidb/session into a Glue interface * *: fix hound lint * util,mock: move utils.MockCluster to mock.Cluster * restore: fix test build failure Co-authored-by: 3pointer <[email protected]>
- Loading branch information
Showing
22 changed files
with
186 additions
and
131 deletions.
There are no files selected for viewing
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
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,24 @@ | ||
package glue | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/pingcap/parser/model" | ||
"github.com/pingcap/tidb/domain" | ||
"github.com/pingcap/tidb/kv" | ||
"github.com/pingcap/tidb/meta/autoid" | ||
) | ||
|
||
// Glue is an abstraction of TiDB function calls used in BR. | ||
type Glue interface { | ||
BootstrapSession(store kv.Storage) (*domain.Domain, error) | ||
CreateSession(store kv.Storage) (Session, error) | ||
} | ||
|
||
// Session is an abstraction of the session.Session interface. | ||
type Session interface { | ||
Execute(ctx context.Context, sql string) error | ||
ShowCreateDatabase(schema *model.DBInfo) (string, error) | ||
ShowCreateTable(table *model.TableInfo, allocator autoid.Allocator) (string, error) | ||
Close() | ||
} |
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,65 @@ | ||
package gluetidb | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
|
||
"github.com/pingcap/parser/model" | ||
"github.com/pingcap/tidb/domain" | ||
"github.com/pingcap/tidb/executor" | ||
"github.com/pingcap/tidb/kv" | ||
"github.com/pingcap/tidb/meta/autoid" | ||
"github.com/pingcap/tidb/session" | ||
|
||
"github.com/pingcap/br/pkg/glue" | ||
) | ||
|
||
// Glue is an implementation of glue.Glue using a new TiDB session. | ||
type Glue struct{} | ||
|
||
type tidbSession struct { | ||
se session.Session | ||
} | ||
|
||
// BootstrapSession implements glue.Glue | ||
func (Glue) BootstrapSession(store kv.Storage) (*domain.Domain, error) { | ||
return session.BootstrapSession(store) | ||
} | ||
|
||
// CreateSession implements glue.Glue | ||
func (Glue) CreateSession(store kv.Storage) (glue.Session, error) { | ||
se, err := session.CreateSession(store) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &tidbSession{se: se}, nil | ||
} | ||
|
||
// Execute implements glue.Session | ||
func (gs *tidbSession) Execute(ctx context.Context, sql string) error { | ||
_, err := gs.se.Execute(ctx, sql) | ||
return err | ||
} | ||
|
||
// ShowCreateDatabase implements glue.Session | ||
func (gs *tidbSession) ShowCreateDatabase(schema *model.DBInfo) (string, error) { | ||
var buf bytes.Buffer | ||
if err := executor.ConstructResultOfShowCreateDatabase(gs.se, schema, true, &buf); err != nil { | ||
return "", err | ||
} | ||
return buf.String(), nil | ||
} | ||
|
||
// ShowCreateTable implements glue.Session | ||
func (gs *tidbSession) ShowCreateTable(table *model.TableInfo, allocator autoid.Allocator) (string, error) { | ||
var buf bytes.Buffer | ||
if err := executor.ConstructResultOfShowCreateTable(gs.se, table, allocator, &buf); err != nil { | ||
return "", err | ||
} | ||
return buf.String(), nil | ||
} | ||
|
||
// Close implements glue.Session | ||
func (gs *tidbSession) Close() { | ||
gs.se.Close() | ||
} |
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,27 @@ | ||
package mock | ||
|
||
import ( | ||
. "github.com/pingcap/check" | ||
"github.com/pingcap/tidb/util/testleak" | ||
) | ||
|
||
var _ = Suite(&testClusterSuite{}) | ||
|
||
type testClusterSuite struct { | ||
mock *Cluster | ||
} | ||
|
||
func (s *testClusterSuite) SetUpSuite(c *C) { | ||
var err error | ||
s.mock, err = NewCluster() | ||
c.Assert(err, IsNil) | ||
} | ||
|
||
func (s *testClusterSuite) TearDownSuite(c *C) { | ||
testleak.AfterTest(c)() | ||
} | ||
|
||
func (s *testClusterSuite) TestSmoke(c *C) { | ||
c.Assert(s.mock.Start(), IsNil) | ||
s.mock.Stop() | ||
} |
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
Oops, something went wrong.