本文整理汇总了Golang中github.com/youtube/vitess/go/vt/mysqlctl/replication.EncodePosition函数的典型用法代码示例。如果您正苦于以下问题:Golang EncodePosition函数的具体用法?Golang EncodePosition怎么用?Golang EncodePosition使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了EncodePosition函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: PromoteSlaveWhenCaughtUp
// PromoteSlaveWhenCaughtUp waits for this slave to be caught up on
// replication up to the provided point, and then makes the slave the
// shard master.
func (agent *ActionAgent) PromoteSlaveWhenCaughtUp(ctx context.Context, position string) (string, error) {
pos, err := replication.DecodePosition(position)
if err != nil {
return "", err
}
// TODO(alainjobart) change the flavor API to take the context directly
// For now, extract the timeout from the context, or wait forever
var waitTimeout time.Duration
if deadline, ok := ctx.Deadline(); ok {
waitTimeout = deadline.Sub(time.Now())
if waitTimeout <= 0 {
waitTimeout = time.Millisecond
}
}
if err := agent.MysqlDaemon.WaitMasterPos(pos, waitTimeout); err != nil {
return "", err
}
pos, err = agent.MysqlDaemon.PromoteSlave(agent.hookExtraEnv())
if err != nil {
return "", err
}
if err := agent.MysqlDaemon.SetReadOnly(false); err != nil {
return "", err
}
if _, err := topotools.ChangeType(ctx, agent.TopoServer, agent.TabletAlias, topodatapb.TabletType_MASTER, topotools.ClearHealthMap); err != nil {
return "", err
}
return replication.EncodePosition(pos), nil
}
示例2: TestStreamerParseEventsDMLWithoutBegin
func TestStreamerParseEventsDMLWithoutBegin(t *testing.T) {
input := []replication.BinlogEvent{
rotateEvent{},
formatEvent{},
queryEvent{query: replication.Query{
Database: "vt_test_keyspace",
SQL: "insert into vt_a(eid, id) values (1, 1) /* _stream vt_a (eid id ) (1 1 ); */"}},
xidEvent{},
}
events := make(chan replication.BinlogEvent)
want := []binlogdatapb.BinlogTransaction{
{
Statements: []*binlogdatapb.BinlogTransaction_Statement{
{Category: binlogdatapb.BinlogTransaction_Statement_BL_SET, Sql: []byte("SET TIMESTAMP=1407805592")},
{Category: binlogdatapb.BinlogTransaction_Statement_BL_DML, Sql: []byte("insert into vt_a(eid, id) values (1, 1) /* _stream vt_a (eid id ) (1 1 ); */")},
},
EventToken: &querypb.EventToken{
Timestamp: 1407805592,
Position: replication.EncodePosition(replication.Position{
GTIDSet: replication.MariadbGTID{
Domain: 0,
Server: 62344,
Sequence: 0x0d,
},
}),
},
},
{
Statements: nil,
EventToken: &querypb.EventToken{
Timestamp: 1407805592,
Position: replication.EncodePosition(replication.Position{
GTIDSet: replication.MariadbGTID{
Domain: 0,
Server: 62344,
Sequence: 0x0d,
},
}),
},
},
}
var got []binlogdatapb.BinlogTransaction
sendTransaction := func(trans *binlogdatapb.BinlogTransaction) error {
got = append(got, *trans)
return nil
}
bls := NewStreamer("vt_test_keyspace", nil, nil, replication.Position{}, 0, sendTransaction)
go sendTestEvents(events, input)
if _, err := bls.parseEvents(context.Background(), events); err != ErrServerEOF {
t.Errorf("unexpected error: %v", err)
}
if !reflect.DeepEqual(got, want) {
t.Errorf("binlogConnStreamer.parseEvents(): got %v, want %v", got, want)
}
}
示例3: updateBlpCheckpoint
// updateBlpCheckpoint returns a statement to update a value in the
// _vt.blp_checkpoint table.
func updateBlpCheckpoint(uid uint32, pos replication.Position, timeUpdated int64, txTimestamp int64) string {
if txTimestamp != 0 {
return fmt.Sprintf(
"UPDATE _vt.blp_checkpoint "+
"SET pos='%v', time_updated=%v, transaction_timestamp=%v "+
"WHERE source_shard_uid=%v",
replication.EncodePosition(pos), timeUpdated, txTimestamp, uid)
}
return fmt.Sprintf(
"UPDATE _vt.blp_checkpoint "+
"SET pos='%v', time_updated=%v "+
"WHERE source_shard_uid=%v",
replication.EncodePosition(pos), timeUpdated, uid)
}
示例4: PromoteSlave
// PromoteSlave makes the current tablet the master
func (agent *ActionAgent) PromoteSlave(ctx context.Context) (string, error) {
if err := agent.lock(ctx); err != nil {
return "", err
}
defer agent.unlock()
pos, err := agent.MysqlDaemon.PromoteSlave(agent.hookExtraEnv())
if err != nil {
return "", err
}
// If using semi-sync, we need to enable it before going read-write.
if *enableSemiSync {
if err := agent.enableSemiSync(true); err != nil {
return "", err
}
}
// Set the server read-write
if err := agent.MysqlDaemon.SetReadOnly(false); err != nil {
return "", err
}
if _, err := topotools.ChangeType(ctx, agent.TopoServer, agent.TabletAlias, topodatapb.TabletType_MASTER); err != nil {
return "", err
}
if err := agent.refreshTablet(ctx, "PromoteSlave"); err != nil {
return "", err
}
return replication.EncodePosition(pos), nil
}
示例5: MasterPosition
// MasterPosition returns the master position
// Should be called under RPCWrap.
func (agent *ActionAgent) MasterPosition(ctx context.Context) (string, error) {
pos, err := agent.MysqlDaemon.MasterPosition()
if err != nil {
return "", err
}
return replication.EncodePosition(pos), nil
}
示例6: PromoteSlaveWhenCaughtUp
// PromoteSlaveWhenCaughtUp waits for this slave to be caught up on
// replication up to the provided point, and then makes the slave the
// shard master.
func (agent *ActionAgent) PromoteSlaveWhenCaughtUp(ctx context.Context, position string) (string, error) {
pos, err := replication.DecodePosition(position)
if err != nil {
return "", err
}
if err := agent.MysqlDaemon.WaitMasterPos(ctx, pos); err != nil {
return "", err
}
pos, err = agent.MysqlDaemon.PromoteSlave(agent.hookExtraEnv())
if err != nil {
return "", err
}
// If using semi-sync, we need to enable it before going read-write.
if *enableSemiSync {
if err := agent.enableSemiSync(true); err != nil {
return "", err
}
}
if err := agent.MysqlDaemon.SetReadOnly(false); err != nil {
return "", err
}
if _, err := topotools.ChangeType(ctx, agent.TopoServer, agent.TabletAlias, topodatapb.TabletType_MASTER); err != nil {
return "", err
}
return replication.EncodePosition(pos), nil
}
示例7: InitMaster
// InitMaster breaks slaves replication, get the current MySQL replication
// position, insert a row in the reparent_journal table, and returns
// the replication position
func (agent *ActionAgent) InitMaster(ctx context.Context) (string, error) {
// we need to insert something in the binlogs, so we can get the
// current position. Let's just use the mysqlctl.CreateReparentJournal commands.
cmds := mysqlctl.CreateReparentJournal()
if err := agent.MysqlDaemon.ExecuteSuperQueryList(cmds); err != nil {
return "", err
}
// get the current replication position
pos, err := agent.MysqlDaemon.MasterPosition()
if err != nil {
return "", err
}
// Set the server read-write, from now on we can accept real
// client writes. Note that if semi-sync replication is enabled,
// we'll still need some slaves to be able to commit
// transactions.
if err := agent.MysqlDaemon.SetReadOnly(false); err != nil {
return "", err
}
// Change our type to master if not already
if _, err := agent.TopoServer.UpdateTabletFields(ctx, agent.TabletAlias, func(tablet *topodatapb.Tablet) error {
tablet.Type = topodatapb.TabletType_MASTER
tablet.HealthMap = nil
return nil
}); err != nil {
return "", err
}
agent.initReplication = true
return replication.EncodePosition(pos), nil
}
示例8: DemoteMaster
// DemoteMaster marks the server read-only, wait until it is done with
// its current transactions, and returns its master position.
// Should be called under RPCWrapLockAction.
func (agent *ActionAgent) DemoteMaster(ctx context.Context) (string, error) {
// Set the server read-only. Note all active connections are not
// affected.
if err := agent.MysqlDaemon.SetReadOnly(true); err != nil {
return "", err
}
// Now disallow queries, to make sure nobody is writing to the
// database.
tablet := agent.Tablet()
// We don't care if the QueryService state actually changed because we'll
// let vtgate keep serving read traffic from this master (see comment below).
if _ /* state changed */, err := agent.disallowQueries(tablet.Type, "DemoteMaster marks server rdonly"); err != nil {
return "", fmt.Errorf("disallowQueries failed: %v", err)
}
// If using semi-sync, we need to disable master-side.
if *enableSemiSync {
if err := agent.enableSemiSync(false); err != nil {
return "", err
}
}
pos, err := agent.MysqlDaemon.DemoteMaster()
if err != nil {
return "", err
}
return replication.EncodePosition(pos), nil
// There is no serving graph update - the master tablet will
// be replaced. Even though writes may fail, reads will
// succeed. It will be less noisy to simply leave the entry
// until we'll promote the master.
}
示例9: RunBlpUntil
// RunBlpUntil runs the binlog player server until the position is reached,
// and returns the current mysql master replication position.
func (agent *ActionAgent) RunBlpUntil(ctx context.Context, bpl []*tabletmanagerdatapb.BlpPosition, waitTime time.Duration) (string, error) {
if agent.BinlogPlayerMap == nil {
return "", fmt.Errorf("No BinlogPlayerMap configured")
}
if err := agent.BinlogPlayerMap.RunUntil(ctx, bpl, waitTime); err != nil {
return "", err
}
pos, err := agent.MysqlDaemon.MasterPosition()
if err != nil {
return "", err
}
return replication.EncodePosition(pos), nil
}
示例10: InitMaster
// InitMaster enables writes and returns the replication position.
func (agent *ActionAgent) InitMaster(ctx context.Context) (string, error) {
if err := agent.lock(ctx); err != nil {
return "", err
}
defer agent.unlock()
// Initializing as master implies undoing any previous "do not replicate".
agent.setSlaveStopped(false)
// we need to insert something in the binlogs, so we can get the
// current position. Let's just use the mysqlctl.CreateReparentJournal commands.
cmds := mysqlctl.CreateReparentJournal()
if err := agent.MysqlDaemon.ExecuteSuperQueryList(ctx, cmds); err != nil {
return "", err
}
// get the current replication position
pos, err := agent.MysqlDaemon.MasterPosition()
if err != nil {
return "", err
}
// If using semi-sync, we need to enable it before going read-write.
if *enableSemiSync {
if err := agent.enableSemiSync(true); err != nil {
return "", err
}
}
// Set the server read-write, from now on we can accept real
// client writes. Note that if semi-sync replication is enabled,
// we'll still need some slaves to be able to commit transactions.
if err := agent.MysqlDaemon.SetReadOnly(false); err != nil {
return "", err
}
// Change our type to master if not already
if _, err := agent.TopoServer.UpdateTabletFields(ctx, agent.TabletAlias, func(tablet *topodatapb.Tablet) error {
tablet.Type = topodatapb.TabletType_MASTER
return nil
}); err != nil {
return "", err
}
// and refresh our state
agent.initReplication = true
if err := agent.refreshTablet(ctx, "InitMaster"); err != nil {
return "", err
}
return replication.EncodePosition(pos), nil
}
示例11: StopSlaveMinimum
// StopSlaveMinimum will stop the slave after it reaches at least the
// provided position. Works both when Vitess manages
// replication or not (using hook if not).
func (agent *ActionAgent) StopSlaveMinimum(ctx context.Context, position string, waitTime time.Duration) (string, error) {
pos, err := replication.DecodePosition(position)
if err != nil {
return "", err
}
if err := agent.MysqlDaemon.WaitMasterPos(pos, waitTime); err != nil {
return "", err
}
if err := mysqlctl.StopSlave(agent.MysqlDaemon, agent.hookExtraEnv()); err != nil {
return "", err
}
pos, err = agent.MysqlDaemon.MasterPosition()
if err != nil {
return "", err
}
return replication.EncodePosition(pos), nil
}
示例12: PromoteSlave
// PromoteSlave makes the current tablet the master
func (agent *ActionAgent) PromoteSlave(ctx context.Context) (string, error) {
pos, err := agent.MysqlDaemon.PromoteSlave(agent.hookExtraEnv())
if err != nil {
return "", err
}
// Set the server read-write
if err := agent.MysqlDaemon.SetReadOnly(false); err != nil {
return "", err
}
if _, err := topotools.ChangeType(ctx, agent.TopoServer, agent.TabletAlias, topodatapb.TabletType_MASTER, topotools.ClearHealthMap); err != nil {
return "", err
}
return replication.EncodePosition(pos), nil
}
示例13: TestStreamerParseEventsMariadbBeginGTID
func TestStreamerParseEventsMariadbBeginGTID(t *testing.T) {
input := []replication.BinlogEvent{
mariadbRotateEvent,
mariadbFormatEvent,
mariadbBeginGTIDEvent,
mariadbInsertEvent,
mariadbXidEvent,
}
events := make(chan replication.BinlogEvent)
want := []binlogdatapb.BinlogTransaction{
{
Statements: []*binlogdatapb.BinlogTransaction_Statement{
{Category: binlogdatapb.BinlogTransaction_Statement_BL_SET, Charset: charset, Sql: []byte("SET TIMESTAMP=1409892744")},
{Category: binlogdatapb.BinlogTransaction_Statement_BL_DML, Charset: charset, Sql: []byte("insert into vt_insert_test(msg) values ('test 0') /* _stream vt_insert_test (id ) (null ); */")},
},
EventToken: &querypb.EventToken{
Timestamp: 1409892744,
Position: replication.EncodePosition(replication.Position{
GTIDSet: replication.MariadbGTID{
Domain: 0,
Server: 62344,
Sequence: 10,
},
}),
},
},
}
var got []binlogdatapb.BinlogTransaction
sendTransaction := func(trans *binlogdatapb.BinlogTransaction) error {
got = append(got, *trans)
return nil
}
bls := NewStreamer("vt_test_keyspace", nil, nil, replication.Position{}, 0, sendTransaction)
go sendTestEvents(events, input)
if _, err := bls.parseEvents(context.Background(), events); err != ErrServerEOF {
t.Errorf("unexpected error: %v", err)
}
if !reflect.DeepEqual(got, want) {
t.Errorf("binlogConnStreamer.parseEvents(): got %v, want %v", got, want)
}
}
示例14: PromoteSlave
// PromoteSlave makes the current tablet the master
func (agent *ActionAgent) PromoteSlave(ctx context.Context) (string, error) {
tablet, err := agent.TopoServer.GetTablet(ctx, agent.TabletAlias)
if err != nil {
return "", err
}
pos, err := agent.MysqlDaemon.PromoteSlave(agent.hookExtraEnv())
if err != nil {
return "", err
}
// Set the server read-write
if err := agent.MysqlDaemon.SetReadOnly(false); err != nil {
return "", err
}
return replication.EncodePosition(pos), agent.updateReplicationGraphForPromotedSlave(ctx, tablet)
}
示例15: TestStreamerParseEventsMariadbStandaloneGTID
func TestStreamerParseEventsMariadbStandaloneGTID(t *testing.T) {
input := []replication.BinlogEvent{
mariadbRotateEvent,
mariadbFormatEvent,
mariadbStandaloneGTIDEvent,
mariadbCreateEvent,
}
events := make(chan replication.BinlogEvent)
want := []binlogdatapb.BinlogTransaction{
{
Statements: []*binlogdatapb.BinlogTransaction_Statement{
{Category: binlogdatapb.BinlogTransaction_Statement_BL_SET, Charset: &binlogdatapb.Charset{Client: 8, Conn: 8, Server: 33}, Sql: []byte("SET TIMESTAMP=1409892744")},
{Category: binlogdatapb.BinlogTransaction_Statement_BL_DDL, Charset: &binlogdatapb.Charset{Client: 8, Conn: 8, Server: 33}, Sql: []byte("create table if not exists vt_insert_test (\nid bigint auto_increment,\nmsg varchar(64),\nprimary key (id)\n) Engine=InnoDB")},
},
EventToken: &querypb.EventToken{
Timestamp: 1409892744,
Position: replication.EncodePosition(replication.Position{
GTIDSet: replication.MariadbGTID{
Domain: 0,
Server: 62344,
Sequence: 9,
},
}),
},
},
}
var got []binlogdatapb.BinlogTransaction
sendTransaction := func(trans *binlogdatapb.BinlogTransaction) error {
got = append(got, *trans)
return nil
}
bls := NewStreamer("vt_test_keyspace", nil, nil, replication.Position{}, 0, sendTransaction)
go sendTestEvents(events, input)
if _, err := bls.parseEvents(context.Background(), events); err != ErrServerEOF {
t.Errorf("unexpected error: %v", err)
}
if !reflect.DeepEqual(got, want) {
t.Errorf("binlogConnStreamer.parseEvents(): got %v, want %v", got, want)
}
}