You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by "sandynz (via GitHub)" <gi...@apache.org> on 2023/03/13 11:23:02 UTC

[GitHub] [shardingsphere] sandynz commented on a diff in pull request #24578: Extraction of code for reuse at CDC

sandynz commented on code in PR #24578:
URL: https://github.com/apache/shardingsphere/pull/24578#discussion_r1133764560


##########
kernel/data-pipeline/cdc/core/src/test/java/org/apache/shardingsphere/data/pipeline/cdc/util/CDCSchemaTableUtilTest.java:
##########
@@ -0,0 +1,89 @@
+/*
+ * 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 org.apache.shardingsphere.data.pipeline.cdc.util;
+
+import org.apache.shardingsphere.data.pipeline.cdc.protocol.request.StreamDataRequestBody.SchemaTable;
+import org.apache.shardingsphere.infra.database.type.dialect.MySQLDatabaseType;
+import org.apache.shardingsphere.infra.database.type.dialect.OpenGaussDatabaseType;
+import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
+import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable;
+import org.junit.Test;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.mockito.Mockito.mock;
+
+public class CDCSchemaTableUtilTest {

Review Comment:
   Class should be `final`



##########
kernel/data-pipeline/cdc/core/src/test/java/org/apache/shardingsphere/data/pipeline/cdc/util/CDCSchemaTableUtilTest.java:
##########
@@ -0,0 +1,89 @@
+/*
+ * 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 org.apache.shardingsphere.data.pipeline.cdc.util;
+
+import org.apache.shardingsphere.data.pipeline.cdc.protocol.request.StreamDataRequestBody.SchemaTable;
+import org.apache.shardingsphere.infra.database.type.dialect.MySQLDatabaseType;
+import org.apache.shardingsphere.infra.database.type.dialect.OpenGaussDatabaseType;
+import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
+import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable;
+import org.junit.Test;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.mockito.Mockito.mock;
+
+public class CDCSchemaTableUtilTest {
+    
+    @Test
+    public void assertParseSchemaTableExpression() {
+        Map<String, ShardingSphereSchema> schemas = new HashMap<>();
+        schemas.put("test", mockSchema());
+        schemas.put("public", mockSchema());

Review Comment:
   Could we put different schema for different schema names? It's confusing when schema is the same for test and public schema



##########
kernel/data-pipeline/cdc/core/src/test/java/org/apache/shardingsphere/data/pipeline/cdc/util/CDCSchemaTableUtilTest.java:
##########
@@ -0,0 +1,89 @@
+/*
+ * 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 org.apache.shardingsphere.data.pipeline.cdc.util;
+
+import org.apache.shardingsphere.data.pipeline.cdc.protocol.request.StreamDataRequestBody.SchemaTable;
+import org.apache.shardingsphere.infra.database.type.dialect.MySQLDatabaseType;
+import org.apache.shardingsphere.infra.database.type.dialect.OpenGaussDatabaseType;
+import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
+import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable;
+import org.junit.Test;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.mockito.Mockito.mock;
+
+public class CDCSchemaTableUtilTest {
+    
+    @Test
+    public void assertParseSchemaTableExpression() {
+        Map<String, ShardingSphereSchema> schemas = new HashMap<>();
+        schemas.put("test", mockSchema());
+        schemas.put("public", mockSchema());
+        ShardingSphereDatabase database = new ShardingSphereDatabase("sharding_db", new OpenGaussDatabaseType(), null, null, schemas);
+        List<SchemaTable> schemaTables = Arrays.asList(SchemaTable.newBuilder().setSchema("public").setTable("t_order").build(),
+                SchemaTable.newBuilder().setSchema("test").setTable("*").build());
+        Map<String, Set<String>> expected = new HashMap<>();
+        expected.put("test", new HashSet<>(Arrays.asList("t_order", "t_order_item")));
+        expected.put("public", Collections.singleton("t_order"));
+        Map<String, Set<String>> actual = CDCSchemaTableUtil.parseSchemaTableExpression(database, schemaTables);
+        assertThat(actual, is(expected));
+        schemaTables = Collections.singletonList(SchemaTable.newBuilder().setTable("t_order").build());
+        actual = CDCSchemaTableUtil.parseSchemaTableExpression(database, schemaTables);
+        expected = Collections.singletonMap("", Collections.singleton("t_order"));
+        assertThat(actual, is(expected));
+        schemaTables = Collections.singletonList(SchemaTable.newBuilder().setSchema("*").setTable("t_order").build());
+        actual = CDCSchemaTableUtil.parseSchemaTableExpression(database, schemaTables);
+        expected = new HashMap<>();
+        expected.put("test", Collections.singleton("t_order"));
+        expected.put("public", Collections.singleton("t_order"));
+        assertThat(actual, is(expected));
+    }
+    
+    private ShardingSphereSchema mockSchema() {
+        Map<String, ShardingSphereTable> tables = new HashMap<>();
+        tables.put("t_order", mock(ShardingSphereTable.class));
+        tables.put("t_order_item", mock(ShardingSphereTable.class));
+        return new ShardingSphereSchema(tables, Collections.emptyMap());
+    }
+    
+    @Test
+    public void assertParseTableExpression() {
+        Map<String, ShardingSphereSchema> schemas = new HashMap<>();
+        schemas.put("sharding_db", mockSchema());

Review Comment:
   What type of `schemas`, it's `sharding_db` here, but it's `test` and `public` in `assertParseSchemaTableExpression`



##########
kernel/data-pipeline/cdc/core/src/test/java/org/apache/shardingsphere/data/pipeline/cdc/util/DataRecordResultConvertUtilTest.java:
##########
@@ -0,0 +1,74 @@
+/*
+ * 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 org.apache.shardingsphere.data.pipeline.cdc.util;
+
+import com.google.protobuf.InvalidProtocolBufferException;
+import com.google.protobuf.TimestampProto;
+import com.google.protobuf.TypeRegistry;
+import com.google.protobuf.WrappersProto;
+import com.google.protobuf.util.JsonFormat;
+import org.apache.shardingsphere.data.pipeline.api.ingest.position.IntegerPrimaryKeyPosition;
+import org.apache.shardingsphere.data.pipeline.api.ingest.record.Column;
+import org.apache.shardingsphere.data.pipeline.api.ingest.record.DataRecord;
+import org.apache.shardingsphere.data.pipeline.cdc.protocol.response.CDCResponseProtocol;
+import org.apache.shardingsphere.data.pipeline.cdc.protocol.response.DataRecordResult.Record;
+import org.apache.shardingsphere.data.pipeline.cdc.protocol.response.DataRecordResult.Record.Builder;
+import org.junit.Test;
+
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.sql.Blob;
+import java.sql.Clob;
+import java.sql.SQLException;
+import java.sql.Timestamp;
+import java.time.LocalTime;
+
+import static org.junit.Assert.assertEquals;
+import static org.mockito.ArgumentMatchers.anyInt;
+import static org.mockito.ArgumentMatchers.anyLong;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public final class DataRecordResultConvertUtilTest {
+    
+    @Test
+    public void assertConvertDataRecordToRecord() throws InvalidProtocolBufferException, SQLException {
+        DataRecord dataRecord = new DataRecord(new IntegerPrimaryKeyPosition(0, 1), 2);
+        dataRecord.addColumn(new Column("BigInteger", BigInteger.ONE, false, true));
+        dataRecord.addColumn(new Column("BigDecimal", BigDecimal.valueOf(123), false, false));
+        dataRecord.addColumn(new Column("Long", Long.MAX_VALUE, false, false));
+        dataRecord.addColumn(new Column("Integer", Integer.MAX_VALUE, false, false));
+        dataRecord.addColumn(new Column("LocalTime", LocalTime.now(), false, false));
+        Blob mockBlob = mock(Blob.class);
+        when(mockBlob.getBytes(anyLong(), anyInt())).thenReturn(new byte[0]);
+        dataRecord.addColumn(new Column("Blob", mockBlob, false, false));
+        Clob mockClob = mock(Clob.class);
+        when(mockClob.getSubString(anyLong(), anyInt())).thenReturn("");

Review Comment:
   Could we return more meaningful values for `mockBlob` and `mockClob`?
   e.g. new byte[]{-1, 0, 1}



##########
kernel/data-pipeline/cdc/core/src/test/java/org/apache/shardingsphere/data/pipeline/cdc/util/DataRecordResultConvertUtilTest.java:
##########
@@ -0,0 +1,74 @@
+/*
+ * 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 org.apache.shardingsphere.data.pipeline.cdc.util;
+
+import com.google.protobuf.InvalidProtocolBufferException;
+import com.google.protobuf.TimestampProto;
+import com.google.protobuf.TypeRegistry;
+import com.google.protobuf.WrappersProto;
+import com.google.protobuf.util.JsonFormat;
+import org.apache.shardingsphere.data.pipeline.api.ingest.position.IntegerPrimaryKeyPosition;
+import org.apache.shardingsphere.data.pipeline.api.ingest.record.Column;
+import org.apache.shardingsphere.data.pipeline.api.ingest.record.DataRecord;
+import org.apache.shardingsphere.data.pipeline.cdc.protocol.response.CDCResponseProtocol;
+import org.apache.shardingsphere.data.pipeline.cdc.protocol.response.DataRecordResult.Record;
+import org.apache.shardingsphere.data.pipeline.cdc.protocol.response.DataRecordResult.Record.Builder;
+import org.junit.Test;
+
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.sql.Blob;
+import java.sql.Clob;
+import java.sql.SQLException;
+import java.sql.Timestamp;
+import java.time.LocalTime;
+
+import static org.junit.Assert.assertEquals;
+import static org.mockito.ArgumentMatchers.anyInt;
+import static org.mockito.ArgumentMatchers.anyLong;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public final class DataRecordResultConvertUtilTest {
+    
+    @Test
+    public void assertConvertDataRecordToRecord() throws InvalidProtocolBufferException, SQLException {
+        DataRecord dataRecord = new DataRecord(new IntegerPrimaryKeyPosition(0, 1), 2);
+        dataRecord.addColumn(new Column("BigInteger", BigInteger.ONE, false, true));
+        dataRecord.addColumn(new Column("BigDecimal", BigDecimal.valueOf(123), false, false));
+        dataRecord.addColumn(new Column("Long", Long.MAX_VALUE, false, false));
+        dataRecord.addColumn(new Column("Integer", Integer.MAX_VALUE, false, false));
+        dataRecord.addColumn(new Column("LocalTime", LocalTime.now(), false, false));

Review Comment:
   It's better to put real column name, but not `BigInteger` etc



##########
kernel/data-pipeline/cdc/core/src/test/java/org/apache/shardingsphere/data/pipeline/cdc/util/DataRecordResultConvertUtilTest.java:
##########
@@ -0,0 +1,74 @@
+/*
+ * 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 org.apache.shardingsphere.data.pipeline.cdc.util;
+
+import com.google.protobuf.InvalidProtocolBufferException;
+import com.google.protobuf.TimestampProto;
+import com.google.protobuf.TypeRegistry;
+import com.google.protobuf.WrappersProto;
+import com.google.protobuf.util.JsonFormat;
+import org.apache.shardingsphere.data.pipeline.api.ingest.position.IntegerPrimaryKeyPosition;
+import org.apache.shardingsphere.data.pipeline.api.ingest.record.Column;
+import org.apache.shardingsphere.data.pipeline.api.ingest.record.DataRecord;
+import org.apache.shardingsphere.data.pipeline.cdc.protocol.response.CDCResponseProtocol;
+import org.apache.shardingsphere.data.pipeline.cdc.protocol.response.DataRecordResult.Record;
+import org.apache.shardingsphere.data.pipeline.cdc.protocol.response.DataRecordResult.Record.Builder;
+import org.junit.Test;
+
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.sql.Blob;
+import java.sql.Clob;
+import java.sql.SQLException;
+import java.sql.Timestamp;
+import java.time.LocalTime;
+
+import static org.junit.Assert.assertEquals;
+import static org.mockito.ArgumentMatchers.anyInt;
+import static org.mockito.ArgumentMatchers.anyLong;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public final class DataRecordResultConvertUtilTest {
+    
+    @Test
+    public void assertConvertDataRecordToRecord() throws InvalidProtocolBufferException, SQLException {
+        DataRecord dataRecord = new DataRecord(new IntegerPrimaryKeyPosition(0, 1), 2);
+        dataRecord.addColumn(new Column("BigInteger", BigInteger.ONE, false, true));
+        dataRecord.addColumn(new Column("BigDecimal", BigDecimal.valueOf(123), false, false));
+        dataRecord.addColumn(new Column("Long", Long.MAX_VALUE, false, false));
+        dataRecord.addColumn(new Column("Integer", Integer.MAX_VALUE, false, false));
+        dataRecord.addColumn(new Column("LocalTime", LocalTime.now(), false, false));
+        Blob mockBlob = mock(Blob.class);
+        when(mockBlob.getBytes(anyLong(), anyInt())).thenReturn(new byte[0]);
+        dataRecord.addColumn(new Column("Blob", mockBlob, false, false));
+        Clob mockClob = mock(Clob.class);
+        when(mockClob.getSubString(anyLong(), anyInt())).thenReturn("");
+        dataRecord.addColumn(new Column("Clob", mockClob, false, false));
+        dataRecord.addColumn(new Column("Timestamp", new Timestamp(System.currentTimeMillis()), false, false));
+        dataRecord.setTableName("t_order");
+        dataRecord.setType("INSERT");
+        Record originRecord = DataRecordResultConvertUtil.convertDataRecordToRecord("test", null, dataRecord);
+        TypeRegistry registry = TypeRegistry.newBuilder().add(CDCResponseProtocol.getDescriptor().getFile().getMessageTypes()).add(WrappersProto.getDescriptor().getMessageTypes())
+                .add(TimestampProto.getDescriptor().getMessageTypes()).build();
+        String print = JsonFormat.printer().usingTypeRegistry(registry).print(originRecord);
+        Builder newRecord = Record.newBuilder();
+        JsonFormat.parser().usingTypeRegistry(registry).merge(print, newRecord);
+        assertEquals(newRecord.build(), originRecord);

Review Comment:
   `newRecord` could be `actualRecord`; `originRecord` could be `expectedRecord`



##########
kernel/data-pipeline/cdc/core/src/test/java/org/apache/shardingsphere/data/pipeline/cdc/util/DataRecordResultConvertUtilTest.java:
##########
@@ -0,0 +1,74 @@
+/*
+ * 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 org.apache.shardingsphere.data.pipeline.cdc.util;
+
+import com.google.protobuf.InvalidProtocolBufferException;
+import com.google.protobuf.TimestampProto;
+import com.google.protobuf.TypeRegistry;
+import com.google.protobuf.WrappersProto;
+import com.google.protobuf.util.JsonFormat;
+import org.apache.shardingsphere.data.pipeline.api.ingest.position.IntegerPrimaryKeyPosition;
+import org.apache.shardingsphere.data.pipeline.api.ingest.record.Column;
+import org.apache.shardingsphere.data.pipeline.api.ingest.record.DataRecord;
+import org.apache.shardingsphere.data.pipeline.cdc.protocol.response.CDCResponseProtocol;
+import org.apache.shardingsphere.data.pipeline.cdc.protocol.response.DataRecordResult.Record;
+import org.apache.shardingsphere.data.pipeline.cdc.protocol.response.DataRecordResult.Record.Builder;
+import org.junit.Test;
+
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.sql.Blob;
+import java.sql.Clob;
+import java.sql.SQLException;
+import java.sql.Timestamp;
+import java.time.LocalTime;
+
+import static org.junit.Assert.assertEquals;
+import static org.mockito.ArgumentMatchers.anyInt;
+import static org.mockito.ArgumentMatchers.anyLong;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public final class DataRecordResultConvertUtilTest {
+    
+    @Test
+    public void assertConvertDataRecordToRecord() throws InvalidProtocolBufferException, SQLException {
+        DataRecord dataRecord = new DataRecord(new IntegerPrimaryKeyPosition(0, 1), 2);
+        dataRecord.addColumn(new Column("BigInteger", BigInteger.ONE, false, true));
+        dataRecord.addColumn(new Column("BigDecimal", BigDecimal.valueOf(123), false, false));
+        dataRecord.addColumn(new Column("Long", Long.MAX_VALUE, false, false));
+        dataRecord.addColumn(new Column("Integer", Integer.MAX_VALUE, false, false));
+        dataRecord.addColumn(new Column("LocalTime", LocalTime.now(), false, false));
+        Blob mockBlob = mock(Blob.class);
+        when(mockBlob.getBytes(anyLong(), anyInt())).thenReturn(new byte[0]);
+        dataRecord.addColumn(new Column("Blob", mockBlob, false, false));
+        Clob mockClob = mock(Clob.class);
+        when(mockClob.getSubString(anyLong(), anyInt())).thenReturn("");

Review Comment:
   `mockBlob` could be `mockedBlob`, and also `mockClob`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org