You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2020/11/27 13:29:45 UTC

[shardingsphere] branch master updated: Optimize channel (#8386)

This is an automated email from the ASF dual-hosted git repository.

zhangyonglun pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new f62d8fe  Optimize channel (#8386)
f62d8fe is described below

commit f62d8feea2cca123cf37dc0c342e20aa1dc00cd8
Author: 邱鹿 Lucas <lu...@163.com>
AuthorDate: Fri Nov 27 21:24:59 2020 +0800

    Optimize channel (#8386)
    
    * Optimize ManualBitSet
    
    * Optimize ManualBitSet
    
    * Optimize AutoAcknowledgeBitSetChannelTest
    
    Co-authored-by: qiulu3 <Lucas209910>
---
 .../executor/channel/bitset/ManualBitSet.java      |  3 ++
 .../executor/channel/MemoryChannelTest.java        | 56 ++++++++++++++++++++++
 .../bitset/AutoAcknowledgeBitSetChannelTest.java   | 54 +++++++++++++++++++++
 .../executor/channel/bitset/ManualBitSetTest.java  | 19 +++++---
 4 files changed, 126 insertions(+), 6 deletions(-)

diff --git a/shardingsphere-scaling/shardingsphere-scaling-core/src/main/java/org/apache/shardingsphere/scaling/core/execute/executor/channel/bitset/ManualBitSet.java b/shardingsphere-scaling/shardingsphere-scaling-core/src/main/java/org/apache/shardingsphere/scaling/core/execute/executor/channel/bitset/ManualBitSet.java
index 7af10ee..31c50b4 100644
--- a/shardingsphere-scaling/shardingsphere-scaling-core/src/main/java/org/apache/shardingsphere/scaling/core/execute/executor/channel/bitset/ManualBitSet.java
+++ b/shardingsphere-scaling/shardingsphere-scaling-core/src/main/java/org/apache/shardingsphere/scaling/core/execute/executor/channel/bitset/ManualBitSet.java
@@ -63,6 +63,9 @@ public final class ManualBitSet {
         int listFromIndex = correctFromIndex / BIT_SET_SIZE;
         int listToIndex = correctToIndex / BIT_SET_SIZE;
         for (int i = listFromIndex, k = 0; i <= listToIndex; i++) {
+            if (i >= bitSets.size()) {
+                break;
+            }
             BitSet bitSet = bitSets.get(i);
             int begin = i == listFromIndex ? correctFromIndex % BIT_SET_SIZE : 0;
             int end = i == listToIndex ? correctToIndex % BIT_SET_SIZE : BIT_SET_SIZE;
diff --git a/shardingsphere-scaling/shardingsphere-scaling-core/src/test/java/org/apache/shardingsphere/scaling/core/execute/executor/channel/MemoryChannelTest.java b/shardingsphere-scaling/shardingsphere-scaling-core/src/test/java/org/apache/shardingsphere/scaling/core/execute/executor/channel/MemoryChannelTest.java
new file mode 100644
index 0000000..81ae5d7
--- /dev/null
+++ b/shardingsphere-scaling/shardingsphere-scaling-core/src/test/java/org/apache/shardingsphere/scaling/core/execute/executor/channel/MemoryChannelTest.java
@@ -0,0 +1,56 @@
+/*
+ * 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.scaling.core.execute.executor.channel;
+
+import org.apache.shardingsphere.scaling.core.config.ScalingContext;
+import org.apache.shardingsphere.scaling.core.config.ServerConfiguration;
+import org.apache.shardingsphere.scaling.core.execute.executor.record.DataRecord;
+import org.apache.shardingsphere.scaling.core.execute.executor.record.Record;
+import org.apache.shardingsphere.scaling.core.job.position.PlaceholderPosition;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.util.List;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+
+public final class MemoryChannelTest {
+    
+    @Before
+    public void setUp() {
+        ScalingContext.getInstance().init(new ServerConfiguration());
+    }
+    
+    @Test
+    public void assertFetchRecords() throws InterruptedException {
+        MemoryChannel memoryChannel = new MemoryChannel(records -> {
+        });
+        memoryChannel.pushRecord(new DataRecord(new PlaceholderPosition(), 1));
+        List<Record> records = memoryChannel.fetchRecords(10, 1);
+        assertThat(records.size(), is(1));
+    }
+    
+    @Test
+    public void assertAck() throws InterruptedException {
+        MemoryChannel memoryChannel = new MemoryChannel(records -> assertThat(records.size(), is(1)));
+        memoryChannel.pushRecord(new DataRecord(new PlaceholderPosition(), 1));
+        memoryChannel.fetchRecords(1, 1);
+        memoryChannel.ack();
+    }
+}
diff --git a/shardingsphere-scaling/shardingsphere-scaling-core/src/test/java/org/apache/shardingsphere/scaling/core/execute/executor/channel/bitset/AutoAcknowledgeBitSetChannelTest.java b/shardingsphere-scaling/shardingsphere-scaling-core/src/test/java/org/apache/shardingsphere/scaling/core/execute/executor/channel/bitset/AutoAcknowledgeBitSetChannelTest.java
new file mode 100644
index 0000000..c72264f
--- /dev/null
+++ b/shardingsphere-scaling/shardingsphere-scaling-core/src/test/java/org/apache/shardingsphere/scaling/core/execute/executor/channel/bitset/AutoAcknowledgeBitSetChannelTest.java
@@ -0,0 +1,54 @@
+/*
+ * 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.scaling.core.execute.executor.channel.bitset;
+
+import org.apache.shardingsphere.scaling.core.execute.executor.record.DataRecord;
+import org.apache.shardingsphere.scaling.core.job.position.PlaceholderPosition;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.util.BitSet;
+
+import static org.junit.Assert.assertTrue;
+
+public final class AutoAcknowledgeBitSetChannelTest {
+    
+    private AutoAcknowledgeBitSetChannel channel;
+    
+    @Before
+    public void setUp() {
+        channel = new AutoAcknowledgeBitSetChannel();
+    }
+    
+    @Test
+    public void assertPushRecord() {
+        channel.pushRecord(new DataRecord(new PlaceholderPosition(), 1), 0);
+        BitSet bitSet = channel.getAckBitSet(0);
+        assertTrue(bitSet.get(0));
+    }
+    
+    @Test(expected = UnsupportedOperationException.class)
+    public void assertFetchRecordsFailure() {
+        channel.fetchRecords(1, 1);
+    }
+    
+    @Test(expected = UnsupportedOperationException.class)
+    public void assertAckFailure() {
+        channel.ack();
+    }
+}
diff --git a/shardingsphere-scaling/shardingsphere-scaling-core/src/test/java/org/apache/shardingsphere/scaling/core/execute/executor/channel/bitset/ManualBitSetTest.java b/shardingsphere-scaling/shardingsphere-scaling-core/src/test/java/org/apache/shardingsphere/scaling/core/execute/executor/channel/bitset/ManualBitSetTest.java
index 61f896f..cf87251 100644
--- a/shardingsphere-scaling/shardingsphere-scaling-core/src/test/java/org/apache/shardingsphere/scaling/core/execute/executor/channel/bitset/ManualBitSetTest.java
+++ b/shardingsphere-scaling/shardingsphere-scaling-core/src/test/java/org/apache/shardingsphere/scaling/core/execute/executor/channel/bitset/ManualBitSetTest.java
@@ -36,20 +36,27 @@ public final class ManualBitSetTest {
     @Test
     public void assertGet() {
         ManualBitSet bitSet = new ManualBitSet();
-        IntStream.range(0, 10).forEach(bitSet::set);
-        assertFalse(bitSet.get(0, 9).get(9));
-        assertTrue(bitSet.get(0, 10).get(9));
-        assertFalse(bitSet.get(0, 10).get(10));
-        assertFalse(bitSet.get(0, 11).get(10));
+        IntStream.range(0, 1024).forEach(bitSet::set);
+        assertFalse(bitSet.get(0, 1023).get(1023));
+        assertTrue(bitSet.get(0, 1024).get(1023));
+        assertFalse(bitSet.get(0, 1024).get(1024));
+        assertFalse(bitSet.get(0, 1025).get(1024));
     }
     
     @Test
-    public void assertGetEndIndex() {
+    public void assertGetEndIndexSuccess() {
         ManualBitSet bitSet = new ManualBitSet();
         IntStream.range(0, 10).filter(each -> each % 2 == 1).forEach(bitSet::set);
         assertThat(bitSet.getEndIndex(0L, 5), is(10L));
     }
     
+    @Test(expected = IndexOutOfBoundsException.class)
+    public void assertGetEndIndexFailure() {
+        ManualBitSet bitSet = new ManualBitSet();
+        IntStream.range(0, 10).filter(each -> each % 2 == 1).forEach(bitSet::set);
+        bitSet.getEndIndex(0L, 10);
+    }
+    
     @Test
     @SuppressWarnings("unchecked")
     @SneakyThrows(ReflectiveOperationException.class)