Chinaunix首页 | 论坛 | 博客
  • 博客访问: 239365
  • 博文数量: 13
  • 博客积分: 3327
  • 博客等级: 中校
  • 技术积分: 525
  • 用 户 组: 普通用户
  • 注册时间: 2005-12-22 11:36
文章分类

全部博文(13)

文章存档

2012年(2)

2011年(10)

2010年(1)

我的朋友

分类: 数据库开发技术

2011-11-18 01:39:01

TimesTen在 active-active 复制模式下,主、备库都可写,实际生产环境中很可能产生复制冲突。以下仅为测试:

--主机
drop replication rep_test;
create replication rep_test
element ds1 datastore
  MASTER ocs_test ON "OCSDB1"
  SUBSCRIBER ocs_test ON "OCSDB2"
  RETURN TWOSAFE
  STORE ocs_test ON "OCSDB1"
  disable return all 1
  RETURN WAIT TIME 10
  return services off when stopped
element ds2 datastore
  MASTER ocs_test ON "OCSDB2"
  SUBSCRIBER ocs_test ON "OCSDB1"
  RETURN TWOSAFE
  STORE ocs_test ON "OCSDB2"
  disable return all 1
  RETURN WAIT TIME 10
  return services off when stopped;

ttadmin -repstart ocs_test

--备机
ttdestroy ocs_test
ttrepadmin -duplicate  -from ocs_test -host "OCSDB1"  -ramLoad -delXla -compression 1 -verbosity 2 -dsn ocs_test
ttadmin -repstart ocs_test


Normal 0 7.8 磅 0 2 false false false EN-US ZH-CN X-NONE MicrosoftInternetExplorer4 /* Style Definitions */ table.MsoNormalTable {mso-style-name:普通表格; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-qformat:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:widow-orphan; font-size:10.5pt; mso-bidi-font-size:11.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:minor-bidi; mso-font-kerning:1.0pt;}

以下测试场景使用如下测试表和sequence

测试表

create table TEST.TEST (

        ID          TT_BIGINT NOT NULL,

        VALUE       TT_BIGINT NOT NULL,

primary key (ID));

 

 

测试sequence

create sequence TEST.SEQ_TEST

    increment by 1

    minvalue 1

    maxvalue 99999999999999999

    start with 1

    cache 0;

 

场景1:在主机上执行insert操作,看数据是否复制到备机

主机:

Command> autocommit 0;

Command> insert into test values (1,1);

1 row inserted.

Command> commit;

 

备机:

Command> select * from test;

< 1, 1 >

1 row found.

结论:数据可以从主机复制到备机。

场景2:在备机上执行insert操作,看数据是否复制到主机

备机:

Command> autocommit 0;

Command> insert into test values (2,2);   

1 row inserted.

Command> commit;

 

主机:

Command> select * from test;

< 1, 1 >

< 2, 2 >

2 rows found.

结论:数据可以从备机复制到主机。

场景3:主、备机同时update同一个值,确认是否有复制冲突

主机:

Command> update test set value=10 where id=2;

 

备机:

Command> update test set value=20 where id=2;

 

主机:

Command> commit;

 8170: Receipt or commit acknowledgement not returned in the specified timeout interval

The command failed.

 

备机:

Command> commit;

 8170: Receipt or commit acknowledgement not returned in the specified timeout interval

The command failed.

 

主机:

Command> select * from test;

< 1, 1 >

< 2, 10 >

2 rows found.

 

备机:

Command> select * from test;

< 1, 1 >

< 2, 20 >

2 rows found

结论:主备机如果同时更改同一个值,会有复制冲突产生,导致主备数据不一致。

 

场景4:测试一个节点挂掉,如何恢复

假设主机TT挂掉,恢复步骤如下:

1、    从备机复制datastore

ttdestroy ocs_test_ps

ttrepadmin -duplicate  -from ocs_test_ps -host "OCSDB2"  -ramLoad -delXla -compression 1 -verbosity 2 -dsn ocs_test_ps

2、    起主机复制代理

ttadmin -repstart ocs_test_ps

 

场景5:测试sequence复制,看sequence值是否复制到备机,备机sequence当前值是否有变化

主机:

Command> sequences;

 

Sequence TEST.SEQ_TEST:

  Minimum Value: 1

  Maximum Value: 99999999999999999

  Current Value: 1

  Increment:     1

  Cache:         0

  Cycle:         Off

 

Command> select * from test;       

0 rows found.

 

备机:

Command> sequences;

 

Sequence TEST.SEQ_TEST:

  Minimum Value: 1

  Maximum Value: 99999999999999999

  Current Value: 1

  Increment:     1

  Cache:         0

  Cycle:         Off

 

Command> select * from test;       

0 rows found.

 

主机:

Command> autocommit 0;

Command> insert into test values (seq_test.nextval,1);

1 row inserted.

Command> commit;

 

备机:

Command> sequences;

 

Sequence TEST.SEQ_TEST:

  Minimum Value: 1

  Maximum Value: 99999999999999999

  Current Value: 1

  Increment:    1

  Cache:        0

  Cycle:        Off

 

主机:

Command> insert into test values (seq_test.nextval,1);

1 row inserted.

Command> insert into test values (seq_test.nextval,1);

1 row inserted.

Command> insert into test values (seq_test.nextval,1);

1 row inserted.

Command> insert into test values (seq_test.nextval,1);

1 row inserted.

Command> insert into test values (seq_test.nextval,1);

1 row inserted.

Command> insert into test values (seq_test.nextval,1);

1 row inserted.

Command> insert into test values (seq_test.nextval,1);

1 row inserted.

 

Command> commit;

 

Command> sequences;

 

Sequence TEST.SEQ_TEST:

  Minimum Value: 1

  Maximum Value: 99999999999999999

  Current Value: 8

  Increment:     1

  Cache:         0

  Cycle:         Off

备机:

Command> sequences;

 

Sequence TEST.SEQ_TEST:

  Minimum Value: 1

  Maximum Value: 99999999999999999

  Current Value: 1

  Increment:     1

  Cache:         0

  Cycle:         Off

Command> insert into test values (seq_test.nextval,1);

1 row inserted.

Command> commit;

Command> select * from test;

< 1, 1 >

< 2, 1 >

< 3, 1 >

< 4, 1 >

< 5, 1 >

< 6, 1 >

< 7, 1 >

< 8, 1 >

< 9, 1 >

9 rows found.

可见,sequencecurrent value可以被复制到备机。

 

场景6:测试sequence复制是否会有冲突

主、备机机插入数据,但不提交:

Command> autocommit 0;

Command> insert into test values (seq_test.nextval,1);

1 row inserted.

 

主备分别提交:

主机报错:

Command> commit;

 8170: Receipt or commit acknowledgement not returned in the specified timeout interval

The command failed.

 

备机报错:

Command> commit;

 8170: Receipt or commit acknowledgement not returned in the specified timeout interval

The command failed.

结论:说明sequenceactive-active模式下可能会复制冲突。

 

场景7:测试批量插入数据,在Active-Active模式和Active-Standby模式下的差异

数据规模:100万,主、备机都为空表,1条记录提交一次

Active-Active模式下,主备同时插入50万数据,耗时如下:

real     9:36.3

user     3:37.9

sys      1:28.0

 

Active-Active模式下,主备同时插入100万数据,耗时如下:

real    20:32.3

user     7:35.4

sys      3:00.8

 

Active-Active模式下,主插入100万数据,备机不插数据,耗时如下:

real    14:43.7

user     6:08.2

sys      3:06.9

 

Active-Standby模式下,在主机插入100万数据,耗时如下:

real    14:40.9

user     4:31.0

sys      4:17.7

结论:

1、  Active-Active模式下,如果只在主机插相同数量的数据,和在Active-Standby模式下没有区别

2、  Active-Active模式下,如果分摊负载,在主备机分别插一半数量的数据,比在Active-Standby模式下快很多

3、  Active-Active模式下,如果主备同时插相同数量的数据,比在Active-Standby模式下慢很多

场景8:测试批量更新数据,在Active-Active模式和Active-Standby模式下的差异

数据规模:100

Active-Active模式下,主备同时更新50万数据,耗时如下:

real    11m25.180s

user    4m43.180s

sys     1m31.740s

 

Active-Active模式下,主备同时更新100万条记录,耗时如下:

real    22:32.2

user     8:59.6

sys      2:51.3

 

Active-Active模式下,主机更新100万条记录,备机空闲,耗时如下:

real    19:21.9

user     9:19.5

sys      3:03.5

 

Active-Standby模式下,更新100万条记录,耗时如下:

real    18m8.916s

user    8m22.290s

sys     3m26.760s

结论:

1、    Active-Active模式下,如果只在主机更新相同数量的数据,备机空闲,和在Active-Standby模式下没有区别

2、    Active-Active模式下,如果分摊负载,在主备机分别更新一半数量的数据,比在Active-Standby模式下快很多

3、    Active-Active模式下,如果主备同时更新相同数量的数据,比在Active-Standby模式下慢很多

 

场景9 测试批量删除数据,在Active-Active模式和Active-Standby模式下的差异

数据规模:100

Active-Active模式下,主备机同时删除50万条记录,耗时如下:

real    10m32.698s

user    4m39.580s

sys     1m16.510s

 

Active-Active模式下,主备机删除100万条记录,耗时如下:

real    23m50.108s

user    9m35.790s

sys     2m57.710s

 

Active-Active模式下,主机删除100万条记录,备机空闲,耗时如下:

real    17m42.309s

user    7m13.147s

sys     3m08.036s

 

Active-Standby模式下,从主机删除100万条记录,耗时如下:

real    17m17.081s

user    7m34.200s

sys     3m17.500s

结论:

1、    Active-Active模式下,如果只在主机删除相同数量的数据,和在Active-Standby模式下没有区别

2、    Active-Active模式下,如果分摊负载,在主备机分别删除一半数量的数据,比在Active-Standby模式下快很多

3、    Active-Active模式下,如果主备同时删除相同数量的数据,比在Active-Standby模式下慢很多

 

场景10:测试复制代理停掉的情况下,主备能否保障数据同步

备机:

停备机复制代理;

Command> call ttrepstop;

 

主机:

Command> autocommit 0;

Command> insert into test values (1,1);

1 row inserted.

Command> commit;

 8170: Receipt or commit acknowledgement not returned in the specified timeout interval

The command failed.

Command> select * from test;

< 1, 1 >

1 row found.

主机上返回服务ACK超时,根据返回策略,返回服务ACK超时后将禁用返回服务,因此事务提交成功。

 

备机:

Command> insert into test values (1,2);

1 row inserted.

Command> commit;

Command> select * from test;

< 1, 2 >

1 row found.

备机上插入一个不同的值,可以成功提交。

 

然后打开复制代理,查看主备机上的数据,发现主机数据复制到备机,备机数据复制到主机,结果产生冲突。

Command> call ttrepstart;

Command> select * from test;

< 1, 1 >

1 row found.

主机:

Command> select * from test;

< 1, 2 >

1 row found.

Command>

结论:在复制代理停掉或返回服务超时的情况下,复制会产生冲突,主备数据不一致。

 

 

 

总结:

Timesten Active-Active复制模式的限制

1、    Timesten Active-Active复制模式只适用于分布式环境,即主、备机上的业务是独立,处理的表是不同的。否则,主、备机会产生复制冲突。这种情况等同于在主、备机上分别部署一套Active-Standby模式的TT库,但使用Active-Standby模式分库部署要比使用Active-Active模式不分库效率要高。

2、    在复制代理停止、返回服务响应超时的情况下,主、备机数据会不一致,出现问题后不能以任意一方数据为准,给系统恢复带来很大麻烦。

3、    虽然在twosafe的返回策略下,可以实现sequence的复制,并且可避免产生复制冲突,但在某些情况下,例如复制代理停止、返回服务响应超时的情况下,复制仍有可能产生冲突。

sequence的复制

复制机制:

当主机sequencecurrent_value增加后,将current_value复制到备机,作为备机的current_value

  

限制条件:

1sequenceCYCLE属性,不能被复制

2sequence定义在每个datastore上必须相同

3sequence上不能有冲突检测机制。

 

    在双向复制配置中,如果是towsafe模式,主备分别取sequencenextvalue,复制没有问题,但主备同时取sequencenextvalue而又没有及时提交,那么会产生复制冲突;另外在异常情况下,例如复制代理停掉或者返回服务超时,则很可能两边的sequence产生冲突。

    在双向复制配置中,如果不是towsafe模式,则很可能两边产生相同的nextval,因此不能进行Sequence的复制,需要把sequence在复制定义中exclude出来。

   

    解决冲突的办法有两种:

    1sequence步长为2,主备初始值按奇偶错开

    2、主备机设置不同的MINVALUE MAXVALUE,例如主机从1100,备机从101200

             

              这样,不管正常情况还是异常情况,sequence都不会产生冲突。

 

towsafe模式下,可以进行sequence复制,但存在一个问题,如果cache取值过大,主备机上的sequence会增长很快,应设置cache0

 


阅读(2698) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~