shardingsphere icon indicating copy to clipboard operation
shardingsphere copied to clipboard

sharding scaling didn't sync the increment data

Open taojintianxia opened this issue 6 years ago • 13 comments

Bug Report

Please answer these questions before submitting your issue. Thanks!

Which version of ShardingSphere did you use?

4.1.0 or 5.0.0 SNAPSHOT , MySQL 5.7.28

Which project did you use? ShardingSphere-JDBC or ShardingSphere-Proxy?

sharding scaling

Expected behavior

Sync increment data

Actual behavior

Does'n work

Reason analyze (If you can)

Maybe the handleUpdateRowsEvent doesn't work well

Steps to reproduce the behavior, such as: SQL to execute, sharding rule configuration, when exception occur etc.

before start this issue , I already ensure the binlog sync mode is ROW , and test table has PK

  1. insert some test data into table source_schema.test
  2. start sharding scaling, and post the scaling/job/start request
  3. scaling start to sync the inventory data , this works well
  4. during scaling sync the inventory data , try to update one of the data in source_schema.test , such as update test set amount = amount +1 where id = 123
  5. wait for a moment , all inventory data have been transtered to new sharded database through proxy, but the update change doesn't work

I grant the root user all privileges , and config root user in proxy.I'm not sure if there is any other configuration for database or proxy I should set up ?

I tried insert data ,during the inventory sync , if insert some new data , these could be sync to sharded part , but update data does't work

taojintianxia avatar Jun 09 '20 03:06 taojintianxia

@taojintianxia I just tried with 4.1.1 version, scaling work well for update, could you use the 4.1.1 version to test again? image

menghaoranss avatar Jun 09 '20 06:06 menghaoranss

hi , @menghaoranss Haoran , just now I tried to show the proxy log with config sql.show: true, and I found it's not the scaling issue.

actually proxy received the update sql, and following is the log for that sql

Actual SQL: ds_1 ::: UPDATE `test` SET `f_money` = 123 WHERE `f_id` = 1234

however the corresponding data for f_id = 1234 is not in ds1 , it's in ds0, that's why it works for insert but doesn't work for update clause . but I'm not sure the root case of the proxy route to wrong datasource

followings are my proxy config :

schemaName: sharding_db

dataSources:
 ds_0:
   url: jdbc:mysql://127.0.0.1:3306/target_schema_0?useSSL=false&socketTimeout=10000
   username: root
   password: root
   connectionTimeoutMilliseconds: 30000
   idleTimeoutMilliseconds: 60000
   maxLifetimeMilliseconds: 1800000
   maxPoolSize: 5
 ds_1:
   url: jdbc:mysql://127.0.0.1:3306/target_schema_1?useSSL=false&socketTimeout=10000
   username: root
   password: root
   connectionTimeoutMilliseconds: 30000
   idleTimeoutMilliseconds: 60000
   maxLifetimeMilliseconds: 1800000
   maxPoolSize: 5

shardingRule:
  tables:
    test_shard:
      actualDataNodes: ds_$->{0..1}.test
      databaseStrategy:
        inline:
          shardingColumn: f_user_id
          algorithmExpression: ds_${f_user_id % 2}
      logicTable: test

taojintianxia avatar Jun 09 '20 06:06 taojintianxia

@menghaoranss would you please provide your configurations for proxy and the request body of scaling/job/start request

taojintianxia avatar Jun 09 '20 07:06 taojintianxia

@taojintianxia sharing-proxy:

schemaName: sharding_db

dataSources:
  ds_0:
    url: jdbc:mysql://127.0.0.1:3306/demo_ds_0?serverTimezone=UTC&useSSL=false
    username: root
    password: 123456
    connectionTimeoutMilliseconds: 30000
    idleTimeoutMilliseconds: 60000
    maxLifetimeMilliseconds: 1800000
    maxPoolSize: 50
  ds_1:
    url: jdbc:mysql://127.0.0.1:3306/demo_ds_1?serverTimezone=UTC&useSSL=false
    username: root
    password: 123456
    connectionTimeoutMilliseconds: 30000
    idleTimeoutMilliseconds: 60000
    maxLifetimeMilliseconds: 1800000
    maxPoolSize: 50

shardingRule:
  tables:
    t_order:
      actualDataNodes: ds_${0..1}.t_order_${0..1}
      tableStrategy:
        inline:
          shardingColumn: order_id
          algorithmExpression: t_order_${order_id % 2}
      keyGenerator:
        type: SNOWFLAKE
        column: order_id
    t_order_item:
      actualDataNodes: ds_${0..1}.t_order_item_${0..1}
      tableStrategy:
        inline:
          shardingColumn: order_id
          algorithmExpression: t_order_item_${order_id % 2}
      keyGenerator:
        type: SNOWFLAKE
        column: order_item_id
  bindingTables:
    - t_order,t_order_item
  defaultDatabaseStrategy:
    inline:
      shardingColumn: user_id
      algorithmExpression: ds_${user_id % 2}
  defaultTableStrategy:
    none:

requetst body:

curl -X POST http://localhost:8888/shardingscaling/job/start -H 'content-type: application/json' -d '{"ruleConfiguration": {"sourceDatasource":"ds_1: !!org.apache.shardingsphere.orchestration.core.configuration.YamlDataSourceConfiguration\n dataSourceClassName: com.zaxxer.hikari.HikariDataSource\n properties:\n     jdbcUrl: jdbc:mysql://127.0.0.1:3306/demo_ds_1?serverTimezone=UTC&useSSL=false\n     username: root\n     password: '\''123456'\''\n     connectionTimeout: 30000\n     idleTimeout: 60000\n     maxLifetime: 1800000\n     maxPoolSize: 50\n     minPoolSize: 1\n     maintenanceIntervalMilliseconds: 30000\n     readOnly: false\nds_0: !!org.apache.shardingsphere.orchestration.core.configuration.YamlDataSourceConfiguration\n   dataSourceClassName: com.zaxxer.hikari.HikariDataSource\n   properties:\n     jdbcUrl: jdbc:mysql://127.0.0.1:3306/demo_ds_0?serverTimezone=UTC&useSSL=false\n     username: root\n     password: '\''123456'\''\n     connectionTimeout: 30000\n     idleTimeout: 60000\n     maxLifetime: 1800000\n     maxPoolSize: 50\n     minPoolSize: 1\n     maintenanceIntervalMilliseconds: 30000\n     readOnly: false", "sourceRule": "bindingTables:\n - t_order,t_order_item\ndefaultDatabaseStrategy:\n   inline:\n     algorithmExpression: ds_${user_id % 2}\n     shardingColumn: user_id\ndefaultTableStrategy:\n   none: '\'''\''\ntables:\n   t_order:\n     actualDataNodes: ds_${0..1}.t_order_${0..1}\n     keyGenerator:\n       column: order_id\n       type: SNOWFLAKE\n     logicTable: t_order\n     tableStrategy:\n       inline:\n         algorithmExpression: t_order_${order_id % 2}\n         shardingColumn: order_id\n   t_order_item:\n     actualDataNodes: ds_${0..1}.t_order_item_${0..1}\n     keyGenerator:\n       column: order_item_id\n       type: SNOWFLAKE\n     logicTable: t_order_item\n     tableStrategy:\n       inline:\n         algorithmExpression: t_order_item_${order_id % 2}\n         shardingColumn: order_id\n",     "destinationDataSources": { "name": "demo_ds", "password": "123456", "url": "jdbc:mysql://127.0.0.1:3306/demo_ds?serverTimezone=UTC&useSSL=false", "username": "root" } }, "jobConfiguration": { "concurrency": 3 } }'

menghaoranss avatar Jun 09 '20 10:06 menghaoranss

Please change the proxy configuration as follow, and retry:

shardingRule:
  tables:
    test:
      actualDataNodes: ds_$->{0..1}.test
      databaseStrategy:
        inline:
          shardingColumn: f_user_id
          algorithmExpression: ds_${f_user_id % 2}
      logicTable: test

KomachiSion avatar Jun 09 '20 11:06 KomachiSion

Please change the proxy configuration as follow, and retry:

shardingRule:
  tables:
    test:
      actualDataNodes: ds_$->{0..1}.test
      databaseStrategy:
        inline:
          shardingColumn: f_user_id
          algorithmExpression: ds_${f_user_id % 2}
      logicTable: test

got an error in proxy log , as followings :

groovy.lang.MissingMethodException: No signature of method: java.lang.String.mod() is applicable for argument types: (java.lang.Integer) values: [2]
Possible solutions: drop(int), find(), any(), find(groovy.lang.Closure), any(groovy.lang.Closure), use([Ljava.lang.Object;)

seems that something wrong with inline ?

taojintianxia avatar Jun 09 '20 11:06 taojintianxia

what‘s the data type of f_user_id? It seems that inline get a string type.

KomachiSion avatar Jun 09 '20 11:06 KomachiSion

what‘s the data type of f_user_id? It seems that inline get a string type.

`f_user_id` bigint(20) unsigned NOT NULL COMMENT '用户id',

taojintianxia avatar Jun 09 '20 11:06 taojintianxia

seems that it's a scaling issue .

got the actual sql send to proxy , like followings :

INSERT INTO `test`(`f_id`,`f_user_id`,`f_money`) VALUES('6', '221', 1006.000000000000000000)

this should be the root case of inline issue , bigint treated as String

taojintianxia avatar Jun 09 '20 12:06 taojintianxia

following is the database structure for my testing :

CREATE TABLE `t_test` (
  `f_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键id',
  `f_user_id` bigint(20) unsigned NOT NULL COMMENT '用户id',
  `f_money` decimal(36,18) NOT NULL DEFAULT '0.000000000000000000' COMMENT '钱啊钱',
  PRIMARY KEY (`f_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='测试表';

I use this sql create 3 datasource , they are :

source_shema.t_test
target_schema_0.t_test
targete_shcema_1.t_test

what I'm trying to do is transfer the data in source_shema.t_test , and then shard to target_schema_0.t_test and targete_shcema_1.t_test

taojintianxia avatar Jun 10 '20 08:06 taojintianxia

@taojintianxia Hi, Sun Nianjun, there are 2 problems in this issue. First is config, logicTable must equals tablename. (actually, tablename means logicTable, you can remove directly.)

shardingRule:
  tables:
    test:
      actualDataNodes: ds_$->{0..1}.test
      databaseStrategy:
        inline:
          shardingColumn: f_user_id
          algorithmExpression: ds_${f_user_id % 2}
      logicTable: test # logicTable must equals tablename , or you can remove it.

Second is about mod() error. it's because f_user_id is transfered to string, string can't execute mod in sharding algorithm ${f_user_id % 2}.

groovy.lang.MissingMethodException: No signature of method: java.lang.String.mod() is applicable for argument types: (java.lang.Integer) values: [2]
Possible solutions: drop(int), find(), any(), find(groovy.lang.Closure), any(groovy.lang.Closure), use([Ljava.lang.Object;)

As you know, bigint transfered to string, is because we use ps.setObject to assemb data in jdbc statement, and mysql transfered it to string.

see: com.mysql.jdbc.PreparedStatement#setObject(int, java.lang.Object)

    public void setObject(int parameterIndex, Object parameterObj) throws SQLException {
        synchronized(this.checkClosed().getConnectionMutex()) {
            if (parameterObj == null) {
                this.setNull(parameterIndex, 1111);
            } else if (parameterObj instanceof Byte) {
                this.setInt(parameterIndex, ((Byte)parameterObj).intValue());
            } else if (parameterObj instanceof String) {
                this.setString(parameterIndex, (String)parameterObj);
            } else if (parameterObj instanceof BigDecimal) {
                this.setBigDecimal(parameterIndex, (BigDecimal)parameterObj);
            } else if (parameterObj instanceof Short) {
                this.setShort(parameterIndex, (Short)parameterObj);
            } else if (parameterObj instanceof Integer) {
                this.setInt(parameterIndex, (Integer)parameterObj);
            } else if (parameterObj instanceof Long) {
                this.setLong(parameterIndex, (Long)parameterObj);
            } else if (parameterObj instanceof Float) {
                this.setFloat(parameterIndex, (Float)parameterObj);
            } else if (parameterObj instanceof Double) {
                this.setDouble(parameterIndex, (Double)parameterObj);
            } else if (parameterObj instanceof byte[]) {
                this.setBytes(parameterIndex, (byte[])((byte[])parameterObj));
            } else if (parameterObj instanceof java.sql.Date) {
                this.setDate(parameterIndex, (java.sql.Date)parameterObj);
            } else if (parameterObj instanceof Time) {
                this.setTime(parameterIndex, (Time)parameterObj);
            } else if (parameterObj instanceof Timestamp) {
                this.setTimestamp(parameterIndex, (Timestamp)parameterObj);
            } else if (parameterObj instanceof Boolean) {
                this.setBoolean(parameterIndex, (Boolean)parameterObj);
            } else if (parameterObj instanceof InputStream) {
                this.setBinaryStream(parameterIndex, (InputStream)parameterObj, -1);
            } else if (parameterObj instanceof Blob) {
                this.setBlob(parameterIndex, (Blob)parameterObj);
            } else if (parameterObj instanceof Clob) {
                this.setClob(parameterIndex, (Clob)parameterObj);
            } else if (this.connection.getTreatUtilDateAsTimestamp() && parameterObj instanceof Date) {
                this.setTimestamp(parameterIndex, new Timestamp(((Date)parameterObj).getTime()));
            } else if (parameterObj instanceof BigInteger) {
// BigInteger treated as String, that's why bingint transfered to string in sql.
                this.setString(parameterIndex, parameterObj.toString());
            } else {
                this.setSerializableObject(parameterIndex, parameterObj);
            }
        }
    }

But In mysql, we can treated a string number as bigint like the sql, but not in ShardingSphere-Proxy.

insert into t_test(f_id, f_user_id,f_money) values(100,'1',1);

So, we decide to enhance proxy with string number, not in scaling only.

If you still interesting in it, you can try.

Lucas-307 avatar Jun 11 '20 12:06 Lucas-307

@taojintianxia Hi, Sun Nianjun, there are 2 problems in this issue. First is config, logicTable must equals tablename. (actually, tablename means logicTable, you can remove directly.)

shardingRule:
  tables:
    test:
      actualDataNodes: ds_$->{0..1}.test
      databaseStrategy:
        inline:
          shardingColumn: f_user_id
          algorithmExpression: ds_${f_user_id % 2}
      logicTable: test # logicTable must equals tablename , or you can remove it.

Second is about mod() error. it's because f_user_id is transfered to string, string can't execute mod in sharding algorithm ${f_user_id % 2}.

groovy.lang.MissingMethodException: No signature of method: java.lang.String.mod() is applicable for argument types: (java.lang.Integer) values: [2]
Possible solutions: drop(int), find(), any(), find(groovy.lang.Closure), any(groovy.lang.Closure), use([Ljava.lang.Object;)

As you know, bigint transfered to string, is because we use ps.setObject to assemb data in jdbc statement, and mysql transfered it to string.

see: com.mysql.jdbc.PreparedStatement#setObject(int, java.lang.Object)

    public void setObject(int parameterIndex, Object parameterObj) throws SQLException {
        synchronized(this.checkClosed().getConnectionMutex()) {
            if (parameterObj == null) {
                this.setNull(parameterIndex, 1111);
            } else if (parameterObj instanceof Byte) {
                this.setInt(parameterIndex, ((Byte)parameterObj).intValue());
            } else if (parameterObj instanceof String) {
                this.setString(parameterIndex, (String)parameterObj);
            } else if (parameterObj instanceof BigDecimal) {
                this.setBigDecimal(parameterIndex, (BigDecimal)parameterObj);
            } else if (parameterObj instanceof Short) {
                this.setShort(parameterIndex, (Short)parameterObj);
            } else if (parameterObj instanceof Integer) {
                this.setInt(parameterIndex, (Integer)parameterObj);
            } else if (parameterObj instanceof Long) {
                this.setLong(parameterIndex, (Long)parameterObj);
            } else if (parameterObj instanceof Float) {
                this.setFloat(parameterIndex, (Float)parameterObj);
            } else if (parameterObj instanceof Double) {
                this.setDouble(parameterIndex, (Double)parameterObj);
            } else if (parameterObj instanceof byte[]) {
                this.setBytes(parameterIndex, (byte[])((byte[])parameterObj));
            } else if (parameterObj instanceof java.sql.Date) {
                this.setDate(parameterIndex, (java.sql.Date)parameterObj);
            } else if (parameterObj instanceof Time) {
                this.setTime(parameterIndex, (Time)parameterObj);
            } else if (parameterObj instanceof Timestamp) {
                this.setTimestamp(parameterIndex, (Timestamp)parameterObj);
            } else if (parameterObj instanceof Boolean) {
                this.setBoolean(parameterIndex, (Boolean)parameterObj);
            } else if (parameterObj instanceof InputStream) {
                this.setBinaryStream(parameterIndex, (InputStream)parameterObj, -1);
            } else if (parameterObj instanceof Blob) {
                this.setBlob(parameterIndex, (Blob)parameterObj);
            } else if (parameterObj instanceof Clob) {
                this.setClob(parameterIndex, (Clob)parameterObj);
            } else if (this.connection.getTreatUtilDateAsTimestamp() && parameterObj instanceof Date) {
                this.setTimestamp(parameterIndex, new Timestamp(((Date)parameterObj).getTime()));
            } else if (parameterObj instanceof BigInteger) {
// BigInteger treated as String, that's why bingint transfered to string in sql.
                this.setString(parameterIndex, parameterObj.toString());
            } else {
                this.setSerializableObject(parameterIndex, parameterObj);
            }
        }
    }

But In mysql, we can treated a string number as bigint like the sql, but not in ShardingSphere-Proxy.

insert into t_test(f_id, f_user_id,f_money) values(100,'1',1);

So, we decide to enhance proxy with string number, not in scaling only.

If you still interesting in it, you can try.

quite clearly, I undertand .

taojintianxia avatar Jun 11 '20 12:06 taojintianxia

@taojintianxia hi, I find a easy way to do this.

f_user_id -> Long.parseLong(f_user_id)

shardingRule:
  tables:
    test:
      actualDataNodes: ds_$->{0..1}.test
      databaseStrategy:
        inline:
          shardingColumn: f_user_id
          algorithmExpression: ds_${Long.parseLong(f_user_id) % 2} # transfer to long
      logicTable: test

Lucas-307 avatar Jun 12 '20 07:06 Lucas-307