From 5e49ed4ba38a50ac81487d9ecf8bb0e91ed71bb7 Mon Sep 17 00:00:00 2001 From: Honghua Zhu Date: Sat, 25 Apr 2015 23:47:13 +0800 Subject: [PATCH] =?UTF-8?q?=E9=92=88=E5=AF=B9=E4=B8=8D=E5=90=8C=E7=9A=84Ru?= =?UTF-8?q?nMode=E8=BF=9B=E8=A1=8C=E7=9B=B8=E5=BA=94=E7=9A=84=E5=8F=82?= =?UTF-8?q?=E6=95=B0=E6=A3=80=E6=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/lealone/cluster/config/Config.java | 6 +- .../cluster/config/DatabaseDescriptor.java | 146 +++++++++++------- .../client_server_mode/TcpServerStart.java | 57 ++----- .../src/test/resources/lealone-cs.yaml | 20 +++ 4 files changed, 126 insertions(+), 103 deletions(-) create mode 100644 lealone-test/src/test/resources/lealone-cs.yaml diff --git a/lealone-cluster/src/main/java/org/lealone/cluster/config/Config.java b/lealone-cluster/src/main/java/org/lealone/cluster/config/Config.java index 81bd1b93f..74e4f3ab0 100644 --- a/lealone-cluster/src/main/java/org/lealone/cluster/config/Config.java +++ b/lealone-cluster/src/main/java/org/lealone/cluster/config/Config.java @@ -42,13 +42,15 @@ public class Config { public Integer storage_port = 6210; public Integer ssl_storage_port = 6211; + public String listen_address; public String listen_interface; + public Boolean listen_interface_prefer_ipv6 = false; public String broadcast_address; - public String internode_authenticator; - public String broadcast_rpc_address; + public String internode_authenticator; + public Integer internode_send_buff_size_in_bytes; public Integer internode_recv_buff_size_in_bytes; diff --git a/lealone-cluster/src/main/java/org/lealone/cluster/config/DatabaseDescriptor.java b/lealone-cluster/src/main/java/org/lealone/cluster/config/DatabaseDescriptor.java index f186cfa35..f223bc97f 100644 --- a/lealone-cluster/src/main/java/org/lealone/cluster/config/DatabaseDescriptor.java +++ b/lealone-cluster/src/main/java/org/lealone/cluster/config/DatabaseDescriptor.java @@ -17,6 +17,8 @@ */ package org.lealone.cluster.config; +import java.net.Inet4Address; +import java.net.Inet6Address; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; @@ -93,6 +95,17 @@ public static Config loadConfig() throws ConfigurationException { private static void applyConfig(Config config) throws ConfigurationException { conf = config; + if (config.isClusterMode()) + applyClusterModeConfig(config); + else + applyClientServerModeConfig(config); + } + + private static void applyClientServerModeConfig(Config config) throws ConfigurationException { + applyAddressConfig(config); + } + + private static void applyClusterModeConfig(Config config) throws ConfigurationException { if (conf.internode_authenticator != null) internodeAuthenticator = Utils.construct(conf.internode_authenticator, "internode_authenticator"); else @@ -115,6 +128,55 @@ private static void applyConfig(Config config) throws ConfigurationException { throw new ConfigurationException("phi_convict_threshold must be between 5 and 16"); } + applyAddressConfig(config); + + /* end point snitch */ + if (conf.endpoint_snitch == null) { + throw new ConfigurationException("Missing endpoint_snitch directive"); + } + snitch = createEndpointSnitch(conf.endpoint_snitch); + EndpointSnitchInfo.create(); + + localDC = snitch.getDatacenter(Utils.getBroadcastAddress()); + localComparator = new Comparator() { + @Override + public int compare(InetAddress endpoint1, InetAddress endpoint2) { + boolean local1 = localDC.equals(snitch.getDatacenter(endpoint1)); + boolean local2 = localDC.equals(snitch.getDatacenter(endpoint2)); + if (local1 && !local2) + return -1; + if (local2 && !local1) + return 1; + return 0; + } + }; + + if (conf.num_tokens == null) + conf.num_tokens = 1; + else if (conf.num_tokens > MAX_NUM_TOKENS) + throw new ConfigurationException(String.format("A maximum number of %d tokens per node is supported", + MAX_NUM_TOKENS)); + + if (conf.seed_provider == null) { + throw new ConfigurationException("seeds configuration is missing; a minimum of one seed is required."); + } + try { + Class seedProviderClass = Class.forName(conf.seed_provider.class_name); + seedProvider = (SeedProvider) seedProviderClass.getConstructor(Map.class).newInstance( + conf.seed_provider.parameters); + } + // there are about 5 checked exceptions that could be thrown here. + catch (Exception e) { + throw new ConfigurationException(e.getMessage() + + "\nFatal configuration error; unable to start server. See log for stacktrace."); + } + if (seedProvider.getSeeds().size() == 0) + throw new ConfigurationException("The seed provider lists no seeds."); + + initDefaultReplicationStrategy(); + } + + private static void applyAddressConfig(Config conf) throws ConfigurationException { /* Local IP, hostname or interface to bind services to */ if (conf.listen_address != null && conf.listen_interface != null) { throw new ConfigurationException("Set listen_address OR listen_interface, not both"); @@ -129,17 +191,11 @@ private static void applyConfig(Config config) throws ConfigurationException { throw new ConfigurationException("listen_address cannot be a wildcard address (" + conf.listen_address + ")!"); } else if (conf.listen_interface != null) { - try { - Enumeration addrs = NetworkInterface.getByName(conf.listen_interface).getInetAddresses(); - listenAddress = addrs.nextElement(); - if (addrs.hasMoreElements()) - throw new ConfigurationException("Interface " + conf.listen_interface - + " can't have more than one address"); - } catch (SocketException e) { - throw new ConfigurationException("Unknown network interface in listen_interface " - + conf.listen_interface); - } - + listenAddress = getNetworkInterfaceAddress(conf.listen_interface, "listen_interface", + conf.listen_interface_prefer_ipv6); + } else { + listenAddress = Utils.getLocalAddress(); + conf.listen_address = listenAddress.getHostAddress(); } /* Gossip Address to broadcast */ @@ -174,51 +230,35 @@ private static void applyConfig(Config config) throws ConfigurationException { + "), then " + "you must set broadcast_rpc_address to a value other than " + broadcastRpcAddress); } + } - /* end point snitch */ - if (conf.endpoint_snitch == null) { - throw new ConfigurationException("Missing endpoint_snitch directive"); - } - snitch = createEndpointSnitch(conf.endpoint_snitch); - EndpointSnitchInfo.create(); - - localDC = snitch.getDatacenter(Utils.getBroadcastAddress()); - localComparator = new Comparator() { - @Override - public int compare(InetAddress endpoint1, InetAddress endpoint2) { - boolean local1 = localDC.equals(snitch.getDatacenter(endpoint1)); - boolean local2 = localDC.equals(snitch.getDatacenter(endpoint2)); - if (local1 && !local2) - return -1; - if (local2 && !local1) - return 1; - return 0; - } - }; - - if (conf.num_tokens == null) - conf.num_tokens = 1; - else if (conf.num_tokens > MAX_NUM_TOKENS) - throw new ConfigurationException(String.format("A maximum number of %d tokens per node is supported", - MAX_NUM_TOKENS)); - - if (conf.seed_provider == null) { - throw new ConfigurationException("seeds configuration is missing; a minimum of one seed is required."); - } + private static InetAddress getNetworkInterfaceAddress(String intf, String configName, boolean preferIPv6) + throws ConfigurationException { try { - Class seedProviderClass = Class.forName(conf.seed_provider.class_name); - seedProvider = (SeedProvider) seedProviderClass.getConstructor(Map.class).newInstance( - conf.seed_provider.parameters); - } - // there are about 5 checked exceptions that could be thrown here. - catch (Exception e) { - throw new ConfigurationException(e.getMessage() - + "\nFatal configuration error; unable to start server. See log for stacktrace."); + NetworkInterface ni = NetworkInterface.getByName(intf); + if (ni == null) + throw new ConfigurationException("Configured " + configName + " \"" + intf + "\" could not be found"); + Enumeration addrs = ni.getInetAddresses(); + if (!addrs.hasMoreElements()) + throw new ConfigurationException("Configured " + configName + " \"" + intf + + "\" was found, but had no addresses"); + /* + * Try to return the first address of the preferred type, otherwise return the first address + */ + InetAddress retval = null; + while (addrs.hasMoreElements()) { + InetAddress temp = addrs.nextElement(); + if (preferIPv6 && temp instanceof Inet6Address) + return temp; + if (!preferIPv6 && temp instanceof Inet4Address) + return temp; + if (retval == null) + retval = temp; + } + return retval; + } catch (SocketException e) { + throw new ConfigurationException("Configured " + configName + " \"" + intf + "\" caused an exception", e); } - if (seedProvider.getSeeds().size() == 0) - throw new ConfigurationException("The seed provider lists no seeds."); - - initDefaultReplicationStrategy(); } private static void initDefaultReplicationStrategy() throws ConfigurationException { diff --git a/lealone-test/src/test/java/org/lealone/test/start/client_server_mode/TcpServerStart.java b/lealone-test/src/test/java/org/lealone/test/start/client_server_mode/TcpServerStart.java index 065145d29..e0bde2d38 100644 --- a/lealone-test/src/test/java/org/lealone/test/start/client_server_mode/TcpServerStart.java +++ b/lealone-test/src/test/java/org/lealone/test/start/client_server_mode/TcpServerStart.java @@ -18,61 +18,22 @@ package org.lealone.test.start.client_server_mode; import java.sql.SQLException; -import java.util.ArrayList; -import org.lealone.server.PgServer; -import org.lealone.server.Server; -import org.lealone.server.TcpServer; +import org.lealone.bootstrap.Lealone; public class TcpServerStart { public static void main(String[] args) throws SQLException { + setProperty(); + Lealone.main(args); + } + + private static void setProperty() { + System.setProperty("lealone.config", "lealone-cs.yaml"); + // System.setProperty("DATABASE_TO_UPPER", "false"); System.setProperty("lealone.lobInDatabase", "false"); System.setProperty("lealone.lobClientMaxSizeMemory", "1024"); System.setProperty("java.io.tmpdir", "./target/test/tmp"); - System.setProperty("lealone.base.dir", "./lealone-test-data/cs"); - //System.setProperty("lealone.check2", "true"); - ArrayList list = new ArrayList<>(); - - list.add("-tcpPort"); - list.add("5210"); - - //list.add("-pg"); - list.add("-tcp"); - - start(list.toArray(new String[list.size()])); - } - - public static void start(String[] args) { - Server server = null; - String arg; - for (int i = 0; args != null && i < args.length; i++) { - arg = args[i]; - if (arg != null && !arg.isEmpty()) { - arg = arg.trim(); - switch (arg) { - case "-tcp": - server = new TcpServer(); - break; - case "-pg": - server = new PgServer(); - break; - } - if (server != null) - break; - } - } - - if (server == null) - server = new TcpServer(); - - try { - server.init(args); - server.start(); - System.out.println("Lealone " + server.getName() + " started, listening address: " // - + server.getListenAddress() + ", port: " + server.getPort()); - } catch (Exception e) { - e.printStackTrace(); - } + // System.setProperty("lealone.check2", "true"); } } diff --git a/lealone-test/src/test/resources/lealone-cs.yaml b/lealone-test/src/test/resources/lealone-cs.yaml new file mode 100644 index 000000000..124fd0e02 --- /dev/null +++ b/lealone-test/src/test/resources/lealone-cs.yaml @@ -0,0 +1,20 @@ +# 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. + +base_dir: lealone-test-data/cs +listen_address: +run_mode: client_server +pg_server_enabled: true