forked from apache/incubator-seata-go
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(fence): implement fence use datasource proxy (apache#420)
- Loading branch information
Showing
10 changed files
with
456 additions
and
45 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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 fence | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"database/sql/driver" | ||
|
||
"github.com/go-sql-driver/mysql" | ||
"github.com/pkg/errors" | ||
"github.com/seata/seata-go/pkg/util/log" | ||
) | ||
|
||
const ( | ||
// SeataFenceMySQLDriver MySQL driver for fence | ||
SeataFenceMySQLDriver = "seata-fence-mysql" | ||
) | ||
|
||
func init() { | ||
sql.Register(SeataFenceMySQLDriver, &FenceDriver{ | ||
TargetDriver: &mysql.MySQLDriver{}, | ||
}) | ||
} | ||
|
||
type FenceDriver struct { | ||
TargetDriver driver.Driver | ||
TargetDB *sql.DB | ||
} | ||
|
||
func (fd *FenceDriver) Open(name string) (driver.Conn, error) { | ||
return nil, errors.New("operation unsupported") | ||
} | ||
|
||
func (fd *FenceDriver) OpenConnector(name string) (connector driver.Connector, re error) { | ||
connector = &dsnConnector{dsn: name, driver: fd.TargetDriver} | ||
if driverCtx, ok := fd.TargetDriver.(driver.DriverContext); ok { | ||
connector, re = driverCtx.OpenConnector(name) | ||
if re != nil { | ||
log.Errorf("open connector: %w", re) | ||
return nil, re | ||
} | ||
} | ||
|
||
fd.TargetDB = sql.OpenDB(connector) | ||
|
||
return &SeataFenceConnector{ | ||
TargetConnector: connector, | ||
TargetDB: fd.TargetDB, | ||
}, nil | ||
} | ||
|
||
type dsnConnector struct { | ||
dsn string | ||
driver driver.Driver | ||
} | ||
|
||
func (connector *dsnConnector) Connect(_ context.Context) (driver.Conn, error) { | ||
return connector.driver.Open(connector.dsn) | ||
} | ||
|
||
func (connector *dsnConnector) Driver() driver.Driver { | ||
return connector.driver | ||
} | ||
|
||
type SeataFenceConnector struct { | ||
TargetConnector driver.Connector | ||
TargetDB *sql.DB | ||
} | ||
|
||
func (connector *SeataFenceConnector) Connect(ctx context.Context) (driver.Conn, error) { | ||
targetConn, err := connector.TargetConnector.Connect(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &FenceConn{ | ||
TargetConn: targetConn, | ||
TargetDB: connector.TargetDB, | ||
}, nil | ||
} | ||
|
||
func (connector *SeataFenceConnector) Driver() driver.Driver { | ||
return connector.TargetConnector.Driver() | ||
} |
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,159 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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 fence | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"database/sql/driver" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/seata/seata-go/pkg/tm" | ||
"github.com/seata/seata-go/pkg/util/log" | ||
) | ||
|
||
type FenceConn struct { | ||
TargetConn driver.Conn | ||
TargetDB *sql.DB | ||
} | ||
|
||
func (c *FenceConn) ResetSession(ctx context.Context) error { | ||
resetter, ok := c.TargetConn.(driver.SessionResetter) | ||
if !ok { | ||
return driver.ErrSkip | ||
} | ||
|
||
return resetter.ResetSession(ctx) | ||
} | ||
|
||
func (c *FenceConn) Prepare(query string) (driver.Stmt, error) { | ||
return c.TargetConn.Prepare(query) | ||
} | ||
|
||
func (c *FenceConn) PrepareContext(ctx context.Context, query string) (driver.Stmt, error) { | ||
return c.TargetConn.Prepare(query) | ||
} | ||
|
||
func (c *FenceConn) Exec(query string, args []driver.Value) (driver.Result, error) { | ||
execer, ok := c.TargetConn.(driver.Execer) | ||
if !ok { | ||
return nil, driver.ErrSkip | ||
} | ||
|
||
return execer.Exec(query, args) | ||
} | ||
|
||
func (c *FenceConn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) { | ||
execerContext, ok := c.TargetConn.(driver.ExecerContext) | ||
if !ok { | ||
values := make([]driver.Value, 0, len(args)) | ||
for i := range args { | ||
values = append(values, args[i].Value) | ||
} | ||
return c.Exec(query, values) | ||
} | ||
|
||
return execerContext.ExecContext(ctx, query, args) | ||
} | ||
|
||
func (c *FenceConn) Query(query string, args []driver.Value) (driver.Rows, error) { | ||
queryer, ok := c.TargetConn.(driver.Queryer) | ||
if !ok { | ||
return nil, driver.ErrSkip | ||
} | ||
|
||
return queryer.Query(query, args) | ||
} | ||
|
||
func (c *FenceConn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) { | ||
QueryerContext, ok := c.TargetConn.(driver.QueryerContext) | ||
if !ok { | ||
values := make([]driver.Value, 0, len(args)) | ||
|
||
for i := range args { | ||
values = append(values, args[i].Value) | ||
} | ||
|
||
return c.Query(query, values) | ||
} | ||
|
||
return QueryerContext.QueryContext(ctx, query, args) | ||
} | ||
|
||
func (c *FenceConn) Begin() (driver.Tx, error) { | ||
return nil, errors.New("operation unsupport") | ||
} | ||
|
||
func (c *FenceConn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error) { | ||
beginer, ok := c.TargetConn.(driver.ConnBeginTx) | ||
if !ok { | ||
return nil, errors.New("operation unsupported") | ||
} | ||
|
||
tx, err := beginer.BeginTx(ctx, opts) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if !tm.IsSeataContext(ctx) { | ||
return nil, errors.New("there is not seata context") | ||
} | ||
|
||
// check if have been begin fence tx | ||
if tm.IsFenceTxBegin(ctx) { | ||
return tx, nil | ||
} | ||
|
||
tm.SetFenceTxBeginedFlag(ctx, true) | ||
|
||
fenceTx, err := c.TargetDB.BeginTx(ctx, &sql.TxOptions{}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer func() { | ||
if err != nil { | ||
if err := fenceTx.Rollback(); err != nil { | ||
log.Error(err) | ||
} | ||
|
||
// although it have not any db operations yet, is still rollback to avoid leak tx. | ||
if err := tx.Rollback(); err != nil { | ||
log.Error(err) | ||
} | ||
} | ||
}() | ||
|
||
// do fence operations | ||
emptyCallback := func() error { | ||
return nil | ||
} | ||
|
||
if err := WithFence(ctx, fenceTx, emptyCallback); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &FenceTx{ | ||
Ctx: ctx, | ||
TargetTx: tx, | ||
TargetFenceTx: fenceTx, | ||
}, nil | ||
} | ||
|
||
func (c *FenceConn) Close() error { | ||
return c.TargetConn.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,30 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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 fence | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestBegin(t *testing.T) { | ||
tx, err := (&FenceConn{}).Begin() | ||
assert.NotNil(t, err) | ||
assert.Nil(t, tx) | ||
} |
Oops, something went wrong.