DB driver implementation via RPC (#17779)

This PR builds up on the pass-through DB driver to a fully functioning DB driver implementation via our RPC layer.

To keep things separate from the plugin RPC API, and have the ability to move fast with changes, a separate field Driver is added to MattermostPlugin. Typically the field which is required to be compatible are the API and Helpers. It would be well-documented that Driver is purely for internal use by Mattermost plugins.

A new Driver interface was created which would have a client and server implementation. Every object (connection, statement, etc.) is created and added to a map on the server side. On the client side, the wrapper structs hold the object id, and communicate via the RPC API using this id.

When the server gets the object id, it picks up the appropriate object from its map and performs the operation, and sends back the data.

Some things that need to be handled are errors. Typical error types like pq.Error and mysql.MySQLError are registered with encoding/gob. But for error variables like sql.ErrNoRows, a special integer is encoded with the ErrorString struct. And on the cilent side, the integer is checked, and the appropriate error variable is returned.

Some pending things:

- Context support. This is tricky. Since context.Context is an interface, it's not possible to marshal it. We have to find a way to get the timeout value from the context and pass it.
- RowsColumnScanType(rowsID string, index int) reflect.Type API. Again, reflect.Type is an interface.
- Master/Replica API support.
Этот коммит содержится в:
Agniva De Sarker
2021-06-17 08:53:52 +05:30
коммит произвёл GitHub
родитель f0bd973a5c
Коммит 4b95d47923
20 изменённых файлов: 1176 добавлений и 174 удалений

Просмотреть файл

@@ -5,15 +5,16 @@ package driver
import (
"context"
"database/sql"
"database/sql/driver"
"github.com/mattermost/mattermost-server/v5/plugin"
)
// Conn is a DB driver conn implementation
// which will just pass-through all queries to its
// underlying connection.
// which executes queries using the Plugin DB API.
type Conn struct {
conn *sql.Conn
id string
api plugin.Driver
}
// driverConn is a super-interface combining the basic
@@ -33,66 +34,84 @@ var (
)
func (c *Conn) Begin() (tx driver.Tx, err error) {
err = c.conn.Raw(func(innerConn interface{}) error {
tx, err = innerConn.(driver.Conn).Begin() //nolint:staticcheck
return err
})
return tx, err
txID, err := c.api.Tx(c.id, driver.TxOptions{})
if err != nil {
return nil, err
}
t := &wrapperTx{
id: txID,
api: c.api,
}
return t, nil
}
func (c *Conn) BeginTx(ctx context.Context, opts driver.TxOptions) (_ driver.Tx, err error) {
t := &wrapperTx{}
err = c.conn.Raw(func(innerConn interface{}) error {
t.Tx, err = innerConn.(driver.ConnBeginTx).BeginTx(ctx, opts)
return err
})
return t, err
func (c *Conn) BeginTx(_ context.Context, opts driver.TxOptions) (driver.Tx, error) {
txID, err := c.api.Tx(c.id, opts)
if err != nil {
return nil, err
}
t := &wrapperTx{
id: txID,
api: c.api,
}
return t, nil
}
func (c *Conn) Prepare(q string) (_ driver.Stmt, err error) {
st := &wrapperStmt{}
err = c.conn.Raw(func(innerConn interface{}) error {
st.Stmt, err = innerConn.(driver.Conn).Prepare(q)
return err
})
return st, err
func (c *Conn) Prepare(q string) (driver.Stmt, error) {
stID, err := c.api.Stmt(c.id, q)
if err != nil {
return nil, err
}
st := &wrapperStmt{
id: stID,
api: c.api,
}
return st, nil
}
func (c *Conn) PrepareContext(ctx context.Context, q string) (_ driver.Stmt, err error) {
st := &wrapperStmt{}
err = c.conn.Raw(func(innerConn interface{}) error {
st.Stmt, err = innerConn.(driver.ConnPrepareContext).PrepareContext(ctx, q)
return err
})
return st, err
func (c *Conn) PrepareContext(_ context.Context, q string) (driver.Stmt, error) {
stID, err := c.api.Stmt(c.id, q)
if err != nil {
return nil, err
}
st := &wrapperStmt{
id: stID,
api: c.api,
}
return st, nil
}
func (c *Conn) ExecContext(ctx context.Context, q string, args []driver.NamedValue) (_ driver.Result, err error) {
res := &wrapperResult{}
err = c.conn.Raw(func(innerConn interface{}) error {
res.Result, err = innerConn.(driver.ExecerContext).ExecContext(ctx, q, args)
return err
})
return res, err
func (c *Conn) ExecContext(_ context.Context, q string, args []driver.NamedValue) (driver.Result, error) {
resultContainer, err := c.api.ConnExec(c.id, q, args)
if err != nil {
return nil, err
}
res := &wrapperResult{
res: resultContainer,
}
return res, nil
}
func (c *Conn) QueryContext(ctx context.Context, q string, args []driver.NamedValue) (_ driver.Rows, err error) {
rows := &wrapperRows{}
err = c.conn.Raw(func(innerConn interface{}) error {
rows.Rows, err = innerConn.(driver.QueryerContext).QueryContext(ctx, q, args)
return err
})
return rows, err
func (c *Conn) QueryContext(_ context.Context, q string, args []driver.NamedValue) (driver.Rows, error) {
rowsID, err := c.api.ConnQuery(c.id, q, args)
if err != nil {
return nil, err
}
rows := &wrapperRows{
id: rowsID,
api: c.api,
}
return rows, nil
}
func (c *Conn) Ping(ctx context.Context) error {
return c.conn.Raw(func(innerConn interface{}) error {
return innerConn.(driver.Pinger).Ping(ctx)
})
func (c *Conn) Ping(_ context.Context) error {
return c.api.ConnPing(c.id)
}
func (c *Conn) Close() error {
return c.conn.Raw(func(innerConn interface{}) error {
return innerConn.(driver.Conn).Close()
})
return c.api.ConnClose(c.id)
}

Просмотреть файл

@@ -1,12 +1,18 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
// package driver implements a DB driver that can be used by plugins
// to make SQL queries using RPC. This helps to avoid opening new connections
// for every plugin, and lets everyone use the central connection
// pool in the server.
// The tests for this package are at app/plugin_api_tests/test_db_driver/main.go.
package driver
import (
"context"
"database/sql"
"database/sql/driver"
"github.com/mattermost/mattermost-server/v5/plugin"
)
var (
@@ -15,32 +21,22 @@ var (
)
// Connector is the DB connector which is used to
// initialize the underlying DB.
// communicate with the DB API.
type Connector struct {
driverName string
dsn string
db *sql.DB
api plugin.Driver
}
func NewConnector(driverName, dsn string) (*Connector, error) {
db, err := sql.Open(driverName, dsn)
if err != nil {
return nil, err
}
return &Connector{
driverName: driverName,
dsn: dsn,
db: db,
}, nil
func NewConnector(api plugin.Driver) *Connector {
return &Connector{api: api}
}
func (c *Connector) Connect(ctx context.Context) (driver.Conn, error) {
conn, err := c.db.Conn(ctx)
func (c *Connector) Connect(_ context.Context) (driver.Conn, error) {
connID, err := c.api.Conn()
if err != nil {
return nil, err
}
return &Conn{conn: conn}, nil
return &Conn{id: connID, api: c.api}, nil
}
func (c *Connector) Driver() driver.Driver {

Просмотреть файл

@@ -6,93 +6,109 @@ package driver
import (
"context"
"database/sql/driver"
"reflect"
"github.com/mattermost/mattermost-server/v5/plugin"
)
type wrapperTx struct {
driver.Tx
id string
api plugin.Driver
}
func (t *wrapperTx) Commit() error {
return t.Tx.Commit()
return t.api.TxCommit(t.id)
}
func (t *wrapperTx) Rollback() error {
return t.Tx.Rollback()
return t.api.TxRollback(t.id)
}
type wrapperStmt struct {
driver.Stmt
id string
api plugin.Driver
}
func (s *wrapperStmt) Close() error {
return s.Stmt.Close()
return s.api.StmtClose(s.id)
}
func (s *wrapperStmt) NumInput() int {
return s.Stmt.NumInput()
return s.api.StmtNumInput(s.id)
}
func (s *wrapperStmt) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error) {
return s.Stmt.(driver.StmtExecContext).ExecContext(ctx, args)
func (s *wrapperStmt) ExecContext(_ context.Context, args []driver.NamedValue) (driver.Result, error) {
resultContainer, err := s.api.StmtExec(s.id, args)
if err != nil {
return nil, err
}
res := &wrapperResult{
res: resultContainer,
}
return res, nil
}
func (s *wrapperStmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error) {
return s.Stmt.(driver.StmtQueryContext).QueryContext(ctx, args)
func (s *wrapperStmt) QueryContext(_ context.Context, args []driver.NamedValue) (driver.Rows, error) {
rowsID, err := s.api.StmtQuery(s.id, args)
if err != nil {
return nil, err
}
rows := &wrapperRows{
id: rowsID,
api: s.api,
}
return rows, nil
}
// wrapperResult implements the driver.Result interface.
// This differs from other objects because it already contains the
// information for its methods. This does two things:
//
// 1. Simplifies server-side code by avoiding to track result ids
// in a map.
// 2. Avoids round-trip to compute result methods.
type wrapperResult struct {
driver.Result
res plugin.ResultContainer
}
func (r *wrapperResult) LastInsertId() (int64, error) {
return r.Result.LastInsertId()
return r.res.LastID, r.res.LastIDError
}
func (r *wrapperResult) RowsAffected() (int64, error) {
return r.Result.RowsAffected()
return r.res.RowsAffected, r.res.RowsAffectedError
}
type wrapperRows struct {
driver.Rows
id string
api plugin.Driver
}
func (r *wrapperRows) Columns() []string {
return r.Rows.Columns()
return r.api.RowsColumns(r.id)
}
func (r *wrapperRows) Close() error {
return r.Rows.Close()
return r.api.RowsClose(r.id)
}
func (r *wrapperRows) Next(dest []driver.Value) error {
return r.Rows.Next(dest)
return r.api.RowsNext(r.id, dest)
}
func (r *wrapperRows) HasNextResultSet() bool {
return r.Rows.(driver.RowsNextResultSet).HasNextResultSet()
return r.api.RowsHasNextResultSet(r.id)
}
func (r *wrapperRows) NextResultSet() error {
return r.Rows.(driver.RowsNextResultSet).NextResultSet()
}
func (r *wrapperRows) ColumnTypeScanType(index int) reflect.Type {
return r.Rows.(driver.RowsColumnTypeScanType).ColumnTypeScanType(index)
return r.api.RowsNextResultSet(r.id)
}
func (r *wrapperRows) ColumnTypeDatabaseTypeName(index int) string {
return r.Rows.(driver.RowsColumnTypeDatabaseTypeName).ColumnTypeDatabaseTypeName(index)
}
func (r *wrapperRows) ColumnTypeLength(index int) (length int64, ok bool) {
return r.Rows.(driver.RowsColumnTypeLength).ColumnTypeLength(index)
}
func (r *wrapperRows) ColumnTypeNullable(index int) (nullable, ok bool) {
return r.Rows.(driver.RowsColumnTypeNullable).ColumnTypeNullable(index)
return r.api.RowsColumnTypeDatabaseTypeName(r.id, index)
}
func (r *wrapperRows) ColumnTypePrecisionScale(index int) (precision, scale int64, ok bool) {
return r.Rows.(driver.RowsColumnTypePrecisionScale).ColumnTypePrecisionScale(index)
return r.api.RowsColumnTypePrecisionScale(r.id, index)
}