Skip to content

Commit

Permalink
针对不同的RunMode进行相应的参数检查
Browse files Browse the repository at this point in the history
  • Loading branch information
codefollower committed Apr 25, 2015
1 parent 62145a6 commit 5e49ed4
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 103 deletions.
Expand Up @@ -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;

Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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<InetAddress>() {
@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");
Expand All @@ -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<InetAddress> 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 */
Expand Down Expand Up @@ -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<InetAddress>() {
@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<InetAddress> 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 {
Expand Down
Expand Up @@ -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<String> 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");
}
}
20 changes: 20 additions & 0 deletions 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

0 comments on commit 5e49ed4

Please sign in to comment.