diff --git a/pom.xml b/pom.xml
index 85a236fce0f65..66cd041761393 100644
--- a/pom.xml
+++ b/pom.xml
@@ -26,33 +26,41 @@
+ * Licensed 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 io.shardingsphere.spi; -import io.shardingsphere.core.exception.ShardingException; import lombok.AccessLevel; import lombok.NoArgsConstructor; +import lombok.SneakyThrows; import java.util.Collection; import java.util.LinkedList; import java.util.ServiceLoader; -import java.util.concurrent.CopyOnWriteArrayList; /** * SPI service loader for new instance for every call. @@ -18,7 +34,7 @@ @NoArgsConstructor(access = AccessLevel.PRIVATE) public final class NewInstanceServiceLoader- * Licensed 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 io.shardingsphere.plugin.keygen; - -import io.shardingsphere.core.keygen.DefaultKeyGenerator; -import io.shardingsphere.core.keygen.KeyGenerator; - -import java.net.InetAddress; -import java.net.UnknownHostException; - -/** - * 根据机器名最后的数字编号获取工作进程Id.如果线上机器命名有统一规范,建议使用此种方式. - * 列如机器的HostName为:dangdang-db-sharding-dev-01(公司名-部门名-服务名-环境名-编号) - * ,会截取HostName最后的编号01作为workerId. - * - * @author DonneyYoung - **/ -public final class HostNameKeyGenerator implements KeyGenerator { - - private final DefaultKeyGenerator defaultKeyGenerator = new DefaultKeyGenerator(); - - static { - initWorkerId(); - } - - static void initWorkerId() { - InetAddress address; - Long workerId; - try { - address = InetAddress.getLocalHost(); - } catch (final UnknownHostException e) { - throw new IllegalStateException("Cannot get LocalHost InetAddress, please check your network!"); - } - String hostName = address.getHostName(); - try { - workerId = Long.valueOf(hostName.replace(hostName.replaceAll("\\d+$", ""), "")); - } catch (final NumberFormatException e) { - throw new IllegalArgumentException(String.format("Wrong hostname:%s, hostname must be end with number!", hostName)); - } - DefaultKeyGenerator.setWorkerId(workerId); - } - - @Override - public Number generateKey() { - return defaultKeyGenerator.generateKey(); - } -} diff --git a/sharding-jdbc-plugin/src/main/java/io/shardingsphere/plugin/keygen/IPKeyGenerator.java b/sharding-jdbc-plugin/src/main/java/io/shardingsphere/plugin/keygen/IPKeyGenerator.java deleted file mode 100644 index 43bfabd535cd5..0000000000000 --- a/sharding-jdbc-plugin/src/main/java/io/shardingsphere/plugin/keygen/IPKeyGenerator.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *- * Licensed 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 io.shardingsphere.plugin.keygen; - -import io.shardingsphere.core.keygen.DefaultKeyGenerator; -import io.shardingsphere.core.keygen.KeyGenerator; - -import java.net.InetAddress; -import java.net.UnknownHostException; - -/** - * 根据机器IP获取工作进程Id,如果线上机器的IP二进制表示的最后10位不重复,建议使用此种方式 - * ,列如机器的IP为192.168.1.108,二进制表示:11000000 10101000 00000001 01101100 - * ,截取最后10位 01 01101100,转为十进制364,设置workerId为364. - * - * @author DonneyYoung - */ -public final class IPKeyGenerator implements KeyGenerator { - - private final DefaultKeyGenerator defaultKeyGenerator = new DefaultKeyGenerator(); - - static { - initWorkerId(); - } - - static void initWorkerId() { - InetAddress address; - try { - address = InetAddress.getLocalHost(); - } catch (final UnknownHostException e) { - throw new IllegalStateException("Cannot get LocalHost InetAddress, please check your network!"); - } - byte[] ipAddressByteArray = address.getAddress(); - DefaultKeyGenerator.setWorkerId((long) (((ipAddressByteArray[ipAddressByteArray.length - 2] & 0B11) << Byte.SIZE) + (ipAddressByteArray[ipAddressByteArray.length - 1] & 0xFF))); - } - - @Override - public Number generateKey() { - return defaultKeyGenerator.generateKey(); - } -} diff --git a/sharding-jdbc-plugin/src/main/java/io/shardingsphere/plugin/keygen/IPSectionKeyGenerator.java b/sharding-jdbc-plugin/src/main/java/io/shardingsphere/plugin/keygen/IPSectionKeyGenerator.java deleted file mode 100644 index 653a0f82c857f..0000000000000 --- a/sharding-jdbc-plugin/src/main/java/io/shardingsphere/plugin/keygen/IPSectionKeyGenerator.java +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *- * Licensed 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 io.shardingsphere.plugin.keygen; - -import io.shardingsphere.core.keygen.DefaultKeyGenerator; -import io.shardingsphere.core.keygen.KeyGenerator; - -import java.net.InetAddress; -import java.net.UnknownHostException; - -/** - * 浏览 {@link IPKeyGenerator} workerId生成的规则后,感觉对服务器IP后10位(特别是IPV6)数值比较约束. - * - *- * 有以下优化思路: - * 因为workerId最大限制是2^10,我们生成的workerId只要满足小于最大workerId即可。 - * 1.针对IPV4: - * ....IP最大 255.255.255.255。而(255+255+255+255) < 1024。 - * ....因此采用IP段数值相加即可生成唯一的workerId,不受IP位限制。 - * 2.针对IPV6: - * ....IP最大ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff - * ....为了保证相加生成出的workerId < 1024,思路是将每个bit位的后6位相加。这样在一定程度上也可以满足workerId不重复的问题。 - *
- * 使用这种IP生成workerId的方法,必须保证IP段相加不能重复 - * - * @author DogFc - */ -public final class IPSectionKeyGenerator implements KeyGenerator { - - private final DefaultKeyGenerator defaultKeyGenerator = new DefaultKeyGenerator(); - - static { - initWorkerId(); - } - - static void initWorkerId() { - InetAddress address; - try { - address = InetAddress.getLocalHost(); - } catch (final UnknownHostException e) { - throw new IllegalStateException("Cannot get LocalHost InetAddress, please check your network!"); - } - byte[] ipAddressByteArray = address.getAddress(); - long workerId = 0L; - switch (ipAddressByteArray.length) { - case 4: - for (byte byteNum : ipAddressByteArray) { - workerId += byteNum & 0xFF; - } - break; - case 16: - for (byte byteNum : ipAddressByteArray) { - workerId += byteNum & 0B111111; - } - break; - default: - throw new IllegalStateException("Bad LocalHost InetAddress, please check your network!"); - } - DefaultKeyGenerator.setWorkerId(workerId); - } - - @Override - public Number generateKey() { - return defaultKeyGenerator.generateKey(); - } -} diff --git a/sharding-jdbc-plugin/src/test/java/io/shardingsphere/plugin/keygen/HostNameKeyGeneratorTest.java b/sharding-jdbc-plugin/src/test/java/io/shardingsphere/plugin/keygen/HostNameKeyGeneratorTest.java deleted file mode 100644 index cba6869479b0c..0000000000000 --- a/sharding-jdbc-plugin/src/test/java/io/shardingsphere/plugin/keygen/HostNameKeyGeneratorTest.java +++ /dev/null @@ -1,121 +0,0 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *- * Licensed 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 io.shardingsphere.plugin.keygen; - -import io.shardingsphere.core.keygen.DefaultKeyGenerator; -import org.junit.BeforeClass; -import org.junit.FixMethodOrder; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.junit.runner.RunWith; -import org.junit.runners.MethodSorters; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; - -import java.lang.reflect.Field; -import java.net.InetAddress; -import java.net.UnknownHostException; -import java.util.HashSet; -import java.util.Set; -import java.util.concurrent.Callable; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; - -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertThat; - -@RunWith(PowerMockRunner.class) -@PrepareForTest(HostNameKeyGenerator.class) -@FixMethodOrder(MethodSorters.NAME_ASCENDING) -public class HostNameKeyGeneratorTest { - - private static InetAddress rightAddress; - - private static InetAddress wrongAddress; - - @Rule - public ExpectedException exception = ExpectedException.none(); - - @BeforeClass - public static void init() throws UnknownHostException { - String ipv4 = "192.168.1.108"; - byte[] ipv4Byte = new byte[4]; - String[] ipv4StingArray = ipv4.split("\\."); - for (int i = 0; i < 4; i++) { - ipv4Byte[i] = (byte) Integer.valueOf(ipv4StingArray[i]).intValue(); - } - rightAddress = InetAddress.getByAddress("dangdang-db-sharding-dev-233", ipv4Byte); - wrongAddress = InetAddress.getByAddress("dangdang-db-sharding-dev", ipv4Byte); - PowerMockito.mockStatic(InetAddress.class); - PowerMockito.when(InetAddress.getLocalHost()).thenReturn(rightAddress); - HostNameKeyGenerator.initWorkerId(); - } - - @Test - public void assertRightHostName() throws UnknownHostException, NoSuchFieldException, IllegalAccessException { - PowerMockito.mockStatic(InetAddress.class); - PowerMockito.when(InetAddress.getLocalHost()).thenReturn(rightAddress); - HostNameKeyGenerator.initWorkerId(); - Field workerIdField = DefaultKeyGenerator.class.getDeclaredField("workerId"); - workerIdField.setAccessible(true); - assertThat(workerIdField.getLong(DefaultKeyGenerator.class), is(233L)); - } - - @Test - public void assertUnknownHost() throws UnknownHostException { - PowerMockito.mockStatic(InetAddress.class); - PowerMockito.when(InetAddress.getLocalHost()).thenThrow(new UnknownHostException()); - exception.expect(IllegalStateException.class); - exception.expectMessage("Cannot get LocalHost InetAddress, please check your network!"); - HostNameKeyGenerator.initWorkerId(); - } - - @Test - public void assertWrongHostName() throws UnknownHostException { - PowerMockito.mockStatic(InetAddress.class); - PowerMockito.when(InetAddress.getLocalHost()).thenReturn(wrongAddress); - exception.expect(IllegalArgumentException.class); - exception.expectMessage(String.format("Wrong hostname:%s, hostname must be end with number!", wrongAddress.getHostName())); - HostNameKeyGenerator.initWorkerId(); - } - - @Test - public void assertGenerateId() throws UnknownHostException, ExecutionException, InterruptedException { - PowerMockito.mockStatic(InetAddress.class); - PowerMockito.when(InetAddress.getLocalHost()).thenReturn(rightAddress); - HostNameKeyGenerator.initWorkerId(); - int threadNumber = Runtime.getRuntime().availableProcessors() << 1; - ExecutorService executor = Executors.newFixedThreadPool(threadNumber); - final int taskNumber = threadNumber << 2; - final HostNameKeyGenerator keyGenerator = new HostNameKeyGenerator(); - Set- * Licensed 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 io.shardingsphere.plugin.keygen; - -import io.shardingsphere.core.keygen.DefaultKeyGenerator; -import org.junit.BeforeClass; -import org.junit.FixMethodOrder; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.junit.runner.RunWith; -import org.junit.runners.MethodSorters; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; - -import java.lang.reflect.Field; -import java.net.InetAddress; -import java.net.UnknownHostException; -import java.util.HashSet; -import java.util.Set; -import java.util.concurrent.Callable; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; - -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertThat; - -@RunWith(PowerMockRunner.class) -@PrepareForTest(IPKeyGenerator.class) -@FixMethodOrder(MethodSorters.NAME_ASCENDING) -public class IPKeyGeneratorTest { - - private static InetAddress address; - - @Rule - public ExpectedException exception = ExpectedException.none(); - - @BeforeClass - public static void init() throws UnknownHostException { - String ipv4 = "192.168.1.108"; - byte[] ipv4Byte = new byte[4]; - String[] ipv4StingArray = ipv4.split("\\."); - for (int i = 0; i < 4; i++) { - ipv4Byte[i] = (byte) Integer.valueOf(ipv4StingArray[i]).intValue(); - } - address = InetAddress.getByAddress("dangdang-db-sharding-dev-233", ipv4Byte); - PowerMockito.mockStatic(InetAddress.class); - PowerMockito.when(InetAddress.getLocalHost()).thenReturn(address); - IPKeyGenerator.initWorkerId(); - } - - @Test - public void assertIP() throws UnknownHostException, NoSuchFieldException, IllegalAccessException { - PowerMockito.mockStatic(InetAddress.class); - PowerMockito.when(InetAddress.getLocalHost()).thenReturn(address); - IPKeyGenerator.initWorkerId(); - Field workerIdField = DefaultKeyGenerator.class.getDeclaredField("workerId"); - workerIdField.setAccessible(true); - assertThat(workerIdField.getLong(DefaultKeyGenerator.class), is(364L)); - } - - @Test - public void assertUnknownHost() throws UnknownHostException { - PowerMockito.mockStatic(InetAddress.class); - PowerMockito.when(InetAddress.getLocalHost()).thenThrow(new UnknownHostException()); - exception.expect(IllegalStateException.class); - exception.expectMessage("Cannot get LocalHost InetAddress, please check your network!"); - IPKeyGenerator.initWorkerId(); - } - - @Test - public void generateId() throws UnknownHostException, ExecutionException, InterruptedException { - PowerMockito.mockStatic(InetAddress.class); - PowerMockito.when(InetAddress.getLocalHost()).thenReturn(address); - IPKeyGenerator.initWorkerId(); - int threadNumber = Runtime.getRuntime().availableProcessors() << 1; - ExecutorService executor = Executors.newFixedThreadPool(threadNumber); - final int taskNumber = threadNumber << 2; - final IPKeyGenerator keyGenerator = new IPKeyGenerator(); - Set- * Licensed 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 io.shardingsphere.plugin.keygen; - -import io.shardingsphere.core.keygen.DefaultKeyGenerator; -import org.junit.BeforeClass; -import org.junit.FixMethodOrder; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.junit.runner.RunWith; -import org.junit.runners.MethodSorters; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; - -import java.lang.reflect.Field; -import java.net.InetAddress; -import java.net.UnknownHostException; -import java.util.HashSet; -import java.util.Set; -import java.util.concurrent.Callable; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; - -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertThat; - -@RunWith(PowerMockRunner.class) -@PrepareForTest(IPSectionKeyGenerator.class) -@FixMethodOrder(MethodSorters.NAME_ASCENDING) -@SuppressWarnings("all") -public class IPSectionKeyGeneratorTest { - - private static InetAddress ipv4Address; - - private static InetAddress ipv6Address; - - @Rule - public ExpectedException exception = ExpectedException.none(); - - @BeforeClass - public static void init() throws UnknownHostException { - String ipv4 = "192.168.1.1"; - byte[] ipv4Byte = new byte[4]; - String[] ipv4StingArray = ipv4.split("\\."); - for (int i = 0; i < 4; i++) { - ipv4Byte[i] = (byte) Integer.valueOf(ipv4StingArray[i]).intValue(); - } - ipv4Address = InetAddress.getByAddress("dangdang-db-sharding-dev-233", ipv4Byte); - - String ipv6 = "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"; - ipv6Address = InetAddress.getByName(ipv6); - } - - @Test - public void assertIPV4() throws UnknownHostException, NoSuchFieldException, IllegalAccessException { - PowerMockito.mockStatic(InetAddress.class); - PowerMockito.when(InetAddress.getLocalHost()).thenReturn(ipv4Address); - IPSectionKeyGenerator.initWorkerId(); - Field workerIdField = DefaultKeyGenerator.class.getDeclaredField("workerId"); - workerIdField.setAccessible(true); - assertThat(workerIdField.getLong(DefaultKeyGenerator.class), is(362L)); - } - - @Test - public void assertIPV6() throws UnknownHostException, NoSuchFieldException, IllegalAccessException { - PowerMockito.mockStatic(InetAddress.class); - PowerMockito.when(InetAddress.getLocalHost()).thenReturn(ipv6Address); - IPSectionKeyGenerator.initWorkerId(); - Field workerIdField = DefaultKeyGenerator.class.getDeclaredField("workerId"); - workerIdField.setAccessible(true); - assertThat(workerIdField.getLong(DefaultKeyGenerator.class), is(1008L)); - } - - @Test - public void assertUnknownHost() throws UnknownHostException { - PowerMockito.mockStatic(InetAddress.class); - PowerMockito.when(InetAddress.getLocalHost()).thenThrow(new UnknownHostException()); - exception.expect(IllegalStateException.class); - exception.expectMessage("Cannot get LocalHost InetAddress, please check your network!"); - IPSectionKeyGenerator.initWorkerId(); - } - - @Test - public void generateId() throws UnknownHostException, ExecutionException, InterruptedException { - PowerMockito.mockStatic(InetAddress.class); - PowerMockito.when(InetAddress.getLocalHost()).thenReturn(ipv4Address); - IPSectionKeyGenerator.initWorkerId(); - int threadNumber = Runtime.getRuntime().availableProcessors() << 1; - ExecutorService executor = Executors.newFixedThreadPool(threadNumber); - - final int taskNumber = threadNumber << 2; - final IPSectionKeyGenerator ipSectionKeyGenerator = new IPSectionKeyGenerator(); - Set