From a8f4d9f9c1889dace9c64bea450768a90dfdbe1f Mon Sep 17 00:00:00 2001 From: philo Date: Fri, 10 Nov 2017 14:04:06 +0800 Subject: [PATCH 01/12] configure the ports for pd, tikv & tidb in smart-default.xml --- conf/smart-default.xml | 25 +++++++++++++++++++ .../java/org/smartdata/agent/SmartAgent.java | 5 +--- .../org/smartdata/conf/SmartConfKeys.java | 9 +++++++ .../cmdlet/agent/messages/MasterToAgent.java | 2 +- .../org/smartdata/server/SmartServer.java | 7 ++---- .../java/org/smartdata/tidb/LaunchDB.java | 12 +++------ .../java/org/smartdata/tidb/PdServer.java | 12 +++++++-- .../java/org/smartdata/tidb/TidbServer.java | 13 ++++++++-- .../java/org/smartdata/tidb/TikvServer.java | 12 +++++++-- 9 files changed, 72 insertions(+), 25 deletions(-) diff --git a/conf/smart-default.xml b/conf/smart-default.xml index 2d37b2281ef..cc496814532 100644 --- a/conf/smart-default.xml +++ b/conf/smart-default.xml @@ -72,4 +72,29 @@ false Actions will not be executed on SSM server if true. + + + pd.client.port + 2379 + Pd port in client-url option + + + + pd.peer.port + 2380 + Pd port in peer-url option + + + + tikv.service.port + 20160 + Tikv port in addr option + + + + tidb.service.port + 4000 + SmartAgent master port + + diff --git a/smart-agent/src/main/java/org/smartdata/agent/SmartAgent.java b/smart-agent/src/main/java/org/smartdata/agent/SmartAgent.java index 003fc3c3fc5..e350b35256f 100644 --- a/smart-agent/src/main/java/org/smartdata/agent/SmartAgent.java +++ b/smart-agent/src/main/java/org/smartdata/agent/SmartAgent.java @@ -174,13 +174,10 @@ public void onReceive(Object message) throws Exception { } public boolean launchTikv(String masterHost) throws InterruptedException, IOException { - //TODO: configure in file String agentAddress = AgentUtils.getAgentAddress(conf); InetAddress address = InetAddress.getByName(new AgentUtils.HostPort(agentAddress).getHost()); String ip = address.getHostAddress(); - String tikvArgs = String.format( - "--pd=%s:2379 --addr=%s:20160 --data-dir=tikv", masterHost, ip); - TikvServer tikvServer = new TikvServer(tikvArgs, conf); + TikvServer tikvServer = new TikvServer(masterHost, ip, conf); Thread tikvThread = new Thread(tikvServer); tikvThread.start(); while (!tikvServer.isReady()) { diff --git a/smart-common/src/main/java/org/smartdata/conf/SmartConfKeys.java b/smart-common/src/main/java/org/smartdata/conf/SmartConfKeys.java index 1b9e263f9e5..a54a76de046 100644 --- a/smart-common/src/main/java/org/smartdata/conf/SmartConfKeys.java +++ b/smart-common/src/main/java/org/smartdata/conf/SmartConfKeys.java @@ -104,4 +104,13 @@ public class SmartConfKeys { //Tidb public static final String SMART_TIDB_ENABLED = "smart.tidb.enable"; public static final boolean SMART_TIDB_ENABLED_DEFAULT = false; + + public static final String PD_CLIENT_PORT_KEY = "pd.client.port"; + public static final String PD_CLIENT_PORT_DEFAULT = "2379"; + public static final String PD_PEER_PORT_KEY = "pd.peer.port"; + public static final String PD_PEER_PORT_DEFAULT = "2380"; + public static final String TIKV_SERVICE_PORT_KEY = "tikv.service.port"; + public static final String TIKV_SERVICE_PORT_DEFAULT = "20160"; + public static final String TIDB_SERVICE_PORT_KEY = "tidb.service.port"; + public static final String TIDB_SERVICE_PORT_KEY_DEFAULT = "4000"; } diff --git a/smart-engine/src/main/java/org/smartdata/server/engine/cmdlet/agent/messages/MasterToAgent.java b/smart-engine/src/main/java/org/smartdata/server/engine/cmdlet/agent/messages/MasterToAgent.java index 9d39103dee2..2bb87dec9f2 100644 --- a/smart-engine/src/main/java/org/smartdata/server/engine/cmdlet/agent/messages/MasterToAgent.java +++ b/smart-engine/src/main/java/org/smartdata/server/engine/cmdlet/agent/messages/MasterToAgent.java @@ -55,7 +55,7 @@ public int hashCode() { @Override public String toString() { - return "AgentId{ id=" + id + "}"; + return "AgentId{id=" + id + "}"; } } diff --git a/smart-server/src/main/java/org/smartdata/server/SmartServer.java b/smart-server/src/main/java/org/smartdata/server/SmartServer.java index 8c08e04601f..a8488e375b2 100644 --- a/smart-server/src/main/java/org/smartdata/server/SmartServer.java +++ b/smart-server/src/main/java/org/smartdata/server/SmartServer.java @@ -147,9 +147,7 @@ public static void startDB(SmartConf conf, AgentMaster agentMaster) String host = conf.get(SmartConfKeys.SMART_AGENT_MASTER_ADDRESS_KEY); InetAddress address = InetAddress.getByName(host); String ip = address.getHostAddress(); - String pdArgs = String.format( - "--client-urls=http://%s:2379 --peer-urls=http://%s:2380 --data-dir=pd", ip, ip); - PdServer pdServer = new PdServer(pdArgs, conf); + PdServer pdServer = new PdServer(ip, conf); Thread pdThread = new Thread(pdServer); pdThread.start(); while (!pdServer.isReady() || !agentMaster.isAgentRegisterReady(conf)) { @@ -161,8 +159,7 @@ public static void startDB(SmartConf conf, AgentMaster agentMaster) Thread.sleep(100); } LOG.info("Tikv server is ready."); - String tidbArgs = String.format("--store=tikv --path=%s:2379 --lease=10s", host); - TidbServer tidbServer = new TidbServer(tidbArgs, conf); + TidbServer tidbServer = new TidbServer(host, conf); Thread tidbThread = new Thread(tidbServer); tidbThread.start(); while (!tidbServer.isReady()) { diff --git a/smart-tidb/src/main/java/org/smartdata/tidb/LaunchDB.java b/smart-tidb/src/main/java/org/smartdata/tidb/LaunchDB.java index e063471e00e..c357bc98f4a 100644 --- a/smart-tidb/src/main/java/org/smartdata/tidb/LaunchDB.java +++ b/smart-tidb/src/main/java/org/smartdata/tidb/LaunchDB.java @@ -29,15 +29,9 @@ public class LaunchDB implements Runnable { private final static Logger LOG = LoggerFactory.getLogger(LaunchDB.class); public LaunchDB(SmartConf conf) { - String pdArgs = "--data-dir=pd"; - String tikvArgs = "--pd=127.0.0.1:2379 --data-dir=tikv"; - //The default lease time is 10s. Setting a smaller value may decrease the time of executing ddl statement, but it's dangerous to do so. - String tidbArgs = "--store=tikv --path=127.0.0.1:2379 --lease=10s"; - //String tidbArgs = new String("--log-file=logs/tidb.log"); - - pdServer = new PdServer(pdArgs, conf); - tikvServer = new TikvServer(tikvArgs, conf); - tidbServer = new TidbServer(tidbArgs, conf); + pdServer = new PdServer(conf); + tikvServer = new TikvServer(conf); + tidbServer = new TidbServer(conf); } public boolean isCompleted() { diff --git a/smart-tidb/src/main/java/org/smartdata/tidb/PdServer.java b/smart-tidb/src/main/java/org/smartdata/tidb/PdServer.java index b30f0576b01..bdb4867b1e5 100644 --- a/smart-tidb/src/main/java/org/smartdata/tidb/PdServer.java +++ b/smart-tidb/src/main/java/org/smartdata/tidb/PdServer.java @@ -35,9 +35,17 @@ public interface Pd extends Library { boolean isPdServerReady(); } - public PdServer(String args, SmartConf conf) { + public PdServer(SmartConf conf) { + this("127.0.0.1", conf); + } + + public PdServer(String ip, SmartConf conf) { + String clientPort = conf.get(SmartConfKeys.PD_CLIENT_PORT_KEY, SmartConfKeys.PD_CLIENT_PORT_DEFAULT); + String peerPort = conf.get(SmartConfKeys.PD_PEER_PORT_KEY, SmartConfKeys.PD_PEER_PORT_DEFAULT); String logDir = conf.get(SmartConfKeys.SMART_LOG_DIR_KEY, SmartConfKeys.SMART_LOG_DIR_DEFAULT); - this.args = args + " --log-file=" + logDir + "/pd.log"; + args = String.format("--client-urls=http://%s:%s --peer-urls=http://%s:%s --data-dir=pd --log-file=%s/pd.log", + ip, clientPort, ip, peerPort, logDir); + try { pd = (Pd) Native.loadLibrary("libpd.so", Pd.class); } catch (UnsatisfiedLinkError ex) { diff --git a/smart-tidb/src/main/java/org/smartdata/tidb/TidbServer.java b/smart-tidb/src/main/java/org/smartdata/tidb/TidbServer.java index 767a3771eb2..4d09cc04ec7 100644 --- a/smart-tidb/src/main/java/org/smartdata/tidb/TidbServer.java +++ b/smart-tidb/src/main/java/org/smartdata/tidb/TidbServer.java @@ -35,9 +35,18 @@ public interface Tidb extends Library { boolean isTidbServerReady(); } - public TidbServer(String args, SmartConf conf) { + public TidbServer(SmartConf conf) { + this("127.0.0.1", conf); + } + + public TidbServer(String pdHost, SmartConf conf) { + String pdPort = conf.get(SmartConfKeys.PD_CLIENT_PORT_KEY, SmartConfKeys.PD_CLIENT_PORT_DEFAULT); + String servePort = conf.get(SmartConfKeys.TIDB_SERVICE_PORT_KEY, SmartConfKeys.TIDB_SERVICE_PORT_KEY_DEFAULT); String logDir = conf.get(SmartConfKeys.SMART_LOG_DIR_KEY, SmartConfKeys.SMART_LOG_DIR_DEFAULT); - this.args = args + " --log-file=" + logDir + "/tidb.log"; + //The default lease time is 10s. Setting a smaller value may decrease the time of executing ddl statement, but it's dangerous to do so. + args = String.format("--store=tikv --path=%s:%s --P=%s --lease=10s --log-file=%s/tidb.log", + pdHost, pdPort, servePort, logDir); + try { tidb = (Tidb) Native.loadLibrary("libtidb.so", Tidb.class); } catch (UnsatisfiedLinkError ex) { diff --git a/smart-tidb/src/main/java/org/smartdata/tidb/TikvServer.java b/smart-tidb/src/main/java/org/smartdata/tidb/TikvServer.java index a030955a7f7..87175fdff20 100644 --- a/smart-tidb/src/main/java/org/smartdata/tidb/TikvServer.java +++ b/smart-tidb/src/main/java/org/smartdata/tidb/TikvServer.java @@ -35,9 +35,17 @@ public interface Tikv extends Library { boolean isTikvServerReady(); } - public TikvServer(String args, SmartConf conf) { + public TikvServer(SmartConf conf) { + this("127.0.0.1", "127.0.0.1", conf); + } + + public TikvServer(String pdHost, String ip, SmartConf conf) { + String pdPort = conf.get(SmartConfKeys.PD_CLIENT_PORT_KEY, SmartConfKeys.PD_CLIENT_PORT_DEFAULT); + String servePort = conf.get(SmartConfKeys.TIKV_SERVICE_PORT_KEY, SmartConfKeys.TIKV_SERVICE_PORT_DEFAULT); String logDir = conf.get(SmartConfKeys.SMART_LOG_DIR_KEY, SmartConfKeys.SMART_LOG_DIR_DEFAULT); - this.args = args + " --log-file=" + logDir + "/tikv.log"; + args = String.format("--pd=%s:%s --addr=%s:%s --data-dir=tikv --log-file=%s/tikv.log", + pdHost, pdPort, ip, servePort, logDir); + try { tikv = (Tikv) Native.loadLibrary("libtikv.so", Tikv.class); } catch (UnsatisfiedLinkError ex) { From fb20b71da015cf3b87828b9e14bd857e4be83803 Mon Sep 17 00:00:00 2001 From: philo Date: Fri, 10 Nov 2017 21:02:21 +0800 Subject: [PATCH 02/12] create database ssm and set password for tidb --- .../metastore/utils/MetaStoreUtils.java | 44 ++++++++++++++++++- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/smart-metastore/src/main/java/org/smartdata/metastore/utils/MetaStoreUtils.java b/smart-metastore/src/main/java/org/smartdata/metastore/utils/MetaStoreUtils.java index 188860eb846..b771761969f 100644 --- a/smart-metastore/src/main/java/org/smartdata/metastore/utils/MetaStoreUtils.java +++ b/smart-metastore/src/main/java/org/smartdata/metastore/utils/MetaStoreUtils.java @@ -56,6 +56,7 @@ public class MetaStoreUtils { "PERFORMANCE_SCHEMA" }; static final Logger LOG = LoggerFactory.getLogger(MetaStoreUtils.class); + private static Boolean tidbInited = false; public static Connection createConnection(String url, String userName, String password) @@ -440,9 +441,26 @@ public static MetaStore getDBAdapter( try { p.loadFromXML(new FileInputStream(cpConfigFile)); - String url = conf.get(SmartConfKeys.SMART_METASTORE_DB_URL_KEY); - if (url != null) { + boolean tidbEnabled = conf.getBoolean( + SmartConfKeys.SMART_TIDB_ENABLED, SmartConfKeys.SMART_TIDB_ENABLED_DEFAULT); + if (tidbEnabled) { + String tidbPort = conf.get( + SmartConfKeys.TIDB_SERVICE_PORT_KEY, SmartConfKeys.TIDB_SERVICE_PORT_KEY_DEFAULT); + String url = String.format("jdbc:mysql://127.0.0.1:%s", tidbPort); + String dbName = "ssm"; + if (!tidbInited) { + String user = p.getProperty("username"); + String password = p.getProperty("password"); + initTidb(url, user, password, dbName); + } + url = url + "/" + dbName; p.setProperty("url", url); + LOG.info("The jdbc url for Tidb is {}", url); + } else { + String url = conf.get(SmartConfKeys.SMART_METASTORE_DB_URL_KEY); + if (url != null) { + p.setProperty("url", url); + } } String purl = p.getProperty("url"); @@ -578,4 +596,26 @@ public static void dropAllTablesMysql(Connection conn, throw new MetaStoreException(e); } } + + public static void initTidb(String url, String user, String password, String dbName) + throws MetaStoreException { + Connection conn; + try { + conn = DriverManager.getConnection(url + "/?user=" + user); + Statement stat = conn.createStatement(); + stat.executeUpdate(String.format("CREATE DATABASE IF NOT EXISTS %s", dbName)); + stat.executeQuery(String.format("SET PASSWORD FOR root = PASSWORD('%s')", password)); + } catch (SQLException ex) { + try { + conn = DriverManager.getConnection(url, user, password); + Statement stat = conn.createStatement(); + stat.executeUpdate(String.format("CREATE DATABASE IF NOT EXISTS %s", dbName)); + } catch (Exception e) { + throw new MetaStoreException(ex); + } + } + tidbInited = true; + LOG.info("Tidb is initialized."); + } } + From 768a0729c3d380cd92deb2a6ce1249d13c8987a0 Mon Sep 17 00:00:00 2001 From: philo Date: Fri, 10 Nov 2017 21:21:02 +0800 Subject: [PATCH 03/12] update druid.xml --- conf/druid.xml | 6 +++--- conf/smart-site.xml | 4 +--- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/conf/druid.xml b/conf/druid.xml index ab978f2580d..09b530d3e4b 100644 --- a/conf/druid.xml +++ b/conf/druid.xml @@ -22,9 +22,9 @@ - - "" - "" + + root + 10 4 diff --git a/conf/smart-site.xml b/conf/smart-site.xml index 83033569260..22c3abdcbd2 100644 --- a/conf/smart-site.xml +++ b/conf/smart-site.xml @@ -34,9 +34,7 @@ smart.tidb.enable false - This decides whether Tidb is enabled and used as metastore. - If it is enabled, the jdbc url configured in druid.xml must be a valid service address of Tidb. + This decides whether Tidb is enabled. - \ No newline at end of file From 71ae570ae6f4f70b090de915aa77f022ad534378 Mon Sep 17 00:00:00 2001 From: philo Date: Fri, 10 Nov 2017 21:41:28 +0800 Subject: [PATCH 04/12] modify the default port for pd, tikv & tidb --- conf/smart-default.xml | 7 +++---- .../src/main/java/org/smartdata/conf/SmartConfKeys.java | 6 +++--- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/conf/smart-default.xml b/conf/smart-default.xml index cc496814532..115a1542131 100644 --- a/conf/smart-default.xml +++ b/conf/smart-default.xml @@ -75,13 +75,13 @@ pd.client.port - 2379 + 7060 Pd port in client-url option pd.peer.port - 2380 + 7061 Pd port in peer-url option @@ -93,8 +93,7 @@ tidb.service.port - 4000 + 7070 SmartAgent master port - diff --git a/smart-common/src/main/java/org/smartdata/conf/SmartConfKeys.java b/smart-common/src/main/java/org/smartdata/conf/SmartConfKeys.java index a54a76de046..69b95fff626 100644 --- a/smart-common/src/main/java/org/smartdata/conf/SmartConfKeys.java +++ b/smart-common/src/main/java/org/smartdata/conf/SmartConfKeys.java @@ -106,11 +106,11 @@ public class SmartConfKeys { public static final boolean SMART_TIDB_ENABLED_DEFAULT = false; public static final String PD_CLIENT_PORT_KEY = "pd.client.port"; - public static final String PD_CLIENT_PORT_DEFAULT = "2379"; + public static final String PD_CLIENT_PORT_DEFAULT = "7060"; public static final String PD_PEER_PORT_KEY = "pd.peer.port"; - public static final String PD_PEER_PORT_DEFAULT = "2380"; + public static final String PD_PEER_PORT_DEFAULT = "7061"; public static final String TIKV_SERVICE_PORT_KEY = "tikv.service.port"; public static final String TIKV_SERVICE_PORT_DEFAULT = "20160"; public static final String TIDB_SERVICE_PORT_KEY = "tidb.service.port"; - public static final String TIDB_SERVICE_PORT_KEY_DEFAULT = "4000"; + public static final String TIDB_SERVICE_PORT_KEY_DEFAULT = "7070"; } From a36aeba08a0cb8ce931c6600c54c036d18d16d03 Mon Sep 17 00:00:00 2001 From: philo Date: Fri, 10 Nov 2017 22:16:42 +0800 Subject: [PATCH 05/12] update depoyment doc --- docs/ssm-deployment-guide.md | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/docs/ssm-deployment-guide.md b/docs/ssm-deployment-guide.md index 1918bd047be..9a4b80e2de1 100644 --- a/docs/ssm-deployment-guide.md +++ b/docs/ssm-deployment-guide.md @@ -117,11 +117,11 @@ Configure SSM For SSM with multiple agents mode, Smart Server will run PD and TiDB instance and each agent will run a TiKV instance. So the storage capacity of SSM-TiDB can easily be scaled up by just adding more agent server. This is a great advantage over MySQL. - If TiDB is enabled, the jdbc url should be the following default one. The configuration in druid.xml is shown as follows. + If TiDB is enabled, there is no need to configure jdbc url in druid.xml. You can set a password for root user in druid.xml. ```xml - jdbc:mysql://127.0.0.1:4000/test + root ...... @@ -129,7 +129,12 @@ Configure SSM ``` TiDB supports the usage of MySQL shell. The way of MySQL shell connecting to TiDB server is as same as that for MySQL. - The default command to enter into MySQL shell on Smart Server is `mysql -h 127.0.0.1 -u root -P 4000`. + If user password is not set in druid, by default the command to enter into MySQL shell on Smart Server is `mysql -h 127.0.0.1 -u root -P 7070`. + The 7070 port is the default one configured for tidb.service.port in smart-default.xml. + If you modified it, the port in the above command should also be modified accordingly. + In TiDB, the database named ssm is used to store metadata. + + By default, the logs of Pd, TiKV and TiDB are under logs/ directory. You can refer to these logs if encountering database fault. * **Configure user account to authenticate to Web UI** From 15c73377d796b189be57a12ff8450e7e602ea20f Mon Sep 17 00:00:00 2001 From: philo Date: Fri, 10 Nov 2017 22:21:58 +0800 Subject: [PATCH 06/12] update doc --- docs/ssm-deployment-guide.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/ssm-deployment-guide.md b/docs/ssm-deployment-guide.md index 9a4b80e2de1..b081b2de838 100644 --- a/docs/ssm-deployment-guide.md +++ b/docs/ssm-deployment-guide.md @@ -131,7 +131,7 @@ Configure SSM TiDB supports the usage of MySQL shell. The way of MySQL shell connecting to TiDB server is as same as that for MySQL. If user password is not set in druid, by default the command to enter into MySQL shell on Smart Server is `mysql -h 127.0.0.1 -u root -P 7070`. The 7070 port is the default one configured for tidb.service.port in smart-default.xml. - If you modified it, the port in the above command should also be modified accordingly. + If you modify it, the port in the above command should also be modified accordingly. In TiDB, the database named ssm is used to store metadata. By default, the logs of Pd, TiKV and TiDB are under logs/ directory. You can refer to these logs if encountering database fault. From 5dd918ba1150b9522e8a048fb1734133d7ae3c9c Mon Sep 17 00:00:00 2001 From: philo Date: Fri, 10 Nov 2017 22:44:51 +0800 Subject: [PATCH 07/12] connect to database with empty paassword --- .../java/org/smartdata/metastore/utils/MetaStoreUtils.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/smart-metastore/src/main/java/org/smartdata/metastore/utils/MetaStoreUtils.java b/smart-metastore/src/main/java/org/smartdata/metastore/utils/MetaStoreUtils.java index b771761969f..64841441cf5 100644 --- a/smart-metastore/src/main/java/org/smartdata/metastore/utils/MetaStoreUtils.java +++ b/smart-metastore/src/main/java/org/smartdata/metastore/utils/MetaStoreUtils.java @@ -455,7 +455,7 @@ public static MetaStore getDBAdapter( } url = url + "/" + dbName; p.setProperty("url", url); - LOG.info("The jdbc url for Tidb is {}", url); + LOG.info("\t" + "The jdbc url for Tidb is " + url); } else { String url = conf.get(SmartConfKeys.SMART_METASTORE_DB_URL_KEY); if (url != null) { @@ -601,7 +601,7 @@ public static void initTidb(String url, String user, String password, String dbN throws MetaStoreException { Connection conn; try { - conn = DriverManager.getConnection(url + "/?user=" + user); + conn = DriverManager.getConnection(url, user, ""); Statement stat = conn.createStatement(); stat.executeUpdate(String.format("CREATE DATABASE IF NOT EXISTS %s", dbName)); stat.executeQuery(String.format("SET PASSWORD FOR root = PASSWORD('%s')", password)); From 2148466d7d13adbc76fcc6166b4d65d0136736de Mon Sep 17 00:00:00 2001 From: philo Date: Sat, 11 Nov 2017 21:55:08 +0800 Subject: [PATCH 08/12] getDBAdapter return metastore if not null --- .../metastore/utils/MetaStoreUtils.java | 174 +++++++++--------- 1 file changed, 89 insertions(+), 85 deletions(-) diff --git a/smart-metastore/src/main/java/org/smartdata/metastore/utils/MetaStoreUtils.java b/smart-metastore/src/main/java/org/smartdata/metastore/utils/MetaStoreUtils.java index 64841441cf5..aaec9be66aa 100644 --- a/smart-metastore/src/main/java/org/smartdata/metastore/utils/MetaStoreUtils.java +++ b/smart-metastore/src/main/java/org/smartdata/metastore/utils/MetaStoreUtils.java @@ -56,7 +56,8 @@ public class MetaStoreUtils { "PERFORMANCE_SCHEMA" }; static final Logger LOG = LoggerFactory.getLogger(MetaStoreUtils.class); - private static Boolean tidbInited = false; + private static MetaStore metaStore = null; + private static final String dbName = "ssm"; public static Connection createConnection(String url, String userName, String password) @@ -427,103 +428,105 @@ public static String getMysqlDBName(String url) throws SQLException { public static MetaStore getDBAdapter( SmartConf conf) throws MetaStoreException { - URL pathUrl = ClassLoader.getSystemResource(""); - String path = pathUrl.getPath(); + if (metaStore != null) { + return metaStore; + } else { + URL pathUrl = ClassLoader.getSystemResource(""); + String path = pathUrl.getPath(); - String fileName = "druid.xml"; - String expectedCpPath = path + fileName; - LOG.info("Expected DB connection pool configuration path = " - + expectedCpPath); - File cpConfigFile = new File(expectedCpPath); - if (cpConfigFile.exists()) { - LOG.info("Using pool configure file: " + expectedCpPath); - Properties p = new Properties(); - try { - p.loadFromXML(new FileInputStream(cpConfigFile)); + String fileName = "druid.xml"; + String expectedCpPath = path + fileName; + LOG.info("Expected DB connection pool configuration path = " + + expectedCpPath); + File cpConfigFile = new File(expectedCpPath); + if (cpConfigFile.exists()) { + LOG.info("Using pool configure file: " + expectedCpPath); + Properties p = new Properties(); + try { + p.loadFromXML(new FileInputStream(cpConfigFile)); - boolean tidbEnabled = conf.getBoolean( - SmartConfKeys.SMART_TIDB_ENABLED, SmartConfKeys.SMART_TIDB_ENABLED_DEFAULT); - if (tidbEnabled) { - String tidbPort = conf.get( - SmartConfKeys.TIDB_SERVICE_PORT_KEY, SmartConfKeys.TIDB_SERVICE_PORT_KEY_DEFAULT); - String url = String.format("jdbc:mysql://127.0.0.1:%s", tidbPort); - String dbName = "ssm"; - if (!tidbInited) { + boolean tidbEnabled = conf.getBoolean( + SmartConfKeys.SMART_TIDB_ENABLED, SmartConfKeys.SMART_TIDB_ENABLED_DEFAULT); + if (tidbEnabled) { + String tidbPort = conf.get(SmartConfKeys.TIDB_SERVICE_PORT_KEY, + SmartConfKeys.TIDB_SERVICE_PORT_KEY_DEFAULT); + String url = String.format("jdbc:mysql://127.0.0.1:%s", tidbPort); String user = p.getProperty("username"); String password = p.getProperty("password"); - initTidb(url, user, password, dbName); - } - url = url + "/" + dbName; - p.setProperty("url", url); - LOG.info("\t" + "The jdbc url for Tidb is " + url); - } else { - String url = conf.get(SmartConfKeys.SMART_METASTORE_DB_URL_KEY); - if (url != null) { + initTidb(url, user, password); + url = url + "/" + dbName; p.setProperty("url", url); + LOG.info("\t" + "The jdbc url for Tidb is " + url); + } else { + String url = conf.get(SmartConfKeys.SMART_METASTORE_DB_URL_KEY); + if (url != null) { + p.setProperty("url", url); + } } - } - String purl = p.getProperty("url"); - if (purl == null || purl.length() == 0) { - purl = getDefaultSqliteDB(); // For testing - p.setProperty("url", purl); - LOG.warn("Database URL not specified, using " + purl); - } + String purl = p.getProperty("url"); + if (purl == null || purl.length() == 0) { + purl = getDefaultSqliteDB(); // For testing + p.setProperty("url", purl); + LOG.warn("Database URL not specified, using " + purl); + } - if (purl.startsWith(MetaStoreUtils.MYSQL_URL_PREFIX)) { - String dbName = getMysqlDBName(purl); - for (String name : DB_NAME_NOT_ALLOWED) { - if (dbName.equals(name)) { - throw new MetaStoreException( - String.format( - "The database %s in mysql is for DB system use, " - + "please appoint other database in druid.xml.", - name)); + if (purl.startsWith(MetaStoreUtils.MYSQL_URL_PREFIX)) { + String dbName = getMysqlDBName(purl); + for (String name : DB_NAME_NOT_ALLOWED) { + if (dbName.equals(name)) { + throw new MetaStoreException( + String.format( + "The database %s in mysql is for DB system use, " + + "please appoint other database in druid.xml.", + name)); + } } } - } - for (String key : p.stringPropertyNames()) { - if (key.equals("password")) { - LOG.info("\t" + key + " = **********"); + for (String key : p.stringPropertyNames()) { + if (key.equals("password")) { + LOG.info("\t" + key + " = **********"); + } else { + LOG.info("\t" + key + " = " + p.getProperty(key)); + } + } + return new MetaStore(new DruidPool(p)); + } catch (Exception e) { + if (e instanceof InvalidPropertiesFormatException) { + throw new MetaStoreException( + "Malformat druid.xml, please check the file.", e); } else { - LOG.info("\t" + key + " = " + p.getProperty(key)); + throw new MetaStoreException(e); } } - return new MetaStore(new DruidPool(p)); + } else { + LOG.info("DB connection pool config file " + expectedCpPath + + " NOT found."); + } + // Get Default configure from druid-template.xml + fileName = "druid-template.xml"; + expectedCpPath = path + fileName; + LOG.info("Expected DB connection pool configuration path = " + + expectedCpPath); + cpConfigFile = new File(expectedCpPath); + LOG.info("Using pool configure file: " + expectedCpPath); + Properties p = new Properties(); + try { + p.loadFromXML(new FileInputStream(cpConfigFile)); } catch (Exception e) { - if (e instanceof InvalidPropertiesFormatException) { - throw new MetaStoreException( - "Malformat druid.xml, please check the file.", e); - } else { - throw new MetaStoreException(e); - } + throw new MetaStoreException(e); } - } else { - LOG.info("DB connection pool config file " + expectedCpPath - + " NOT found."); - } - // Get Default configure from druid-template.xml - fileName = "druid-template.xml"; - expectedCpPath = path + fileName; - LOG.info("Expected DB connection pool configuration path = " - + expectedCpPath); - cpConfigFile = new File(expectedCpPath); - LOG.info("Using pool configure file: " + expectedCpPath); - Properties p = new Properties(); - try { - p.loadFromXML(new FileInputStream(cpConfigFile)); - } catch (Exception e) { - throw new MetaStoreException(e); - } - String url = conf.get(SmartConfKeys.SMART_METASTORE_DB_URL_KEY); - if (url != null) { - p.setProperty("url", url); - } - for (String key : p.stringPropertyNames()) { - LOG.info("\t" + key + " = " + p.getProperty(key)); + String url = conf.get(SmartConfKeys.SMART_METASTORE_DB_URL_KEY); + if (url != null) { + p.setProperty("url", url); + } + for (String key : p.stringPropertyNames()) { + LOG.info("\t" + key + " = " + p.getProperty(key)); + } + metaStore = new MetaStore(new DruidPool(p)); + return metaStore; } - return new MetaStore(new DruidPool(p)); } public static Integer getKey(Map map, String value) { @@ -597,24 +600,25 @@ public static void dropAllTablesMysql(Connection conn, } } - public static void initTidb(String url, String user, String password, String dbName) + public static void initTidb(String url, String user, String password) throws MetaStoreException { Connection conn; try { - conn = DriverManager.getConnection(url, user, ""); + conn = createConnection(url, user, ""); Statement stat = conn.createStatement(); stat.executeUpdate(String.format("CREATE DATABASE IF NOT EXISTS %s", dbName)); stat.executeQuery(String.format("SET PASSWORD FOR root = PASSWORD('%s')", password)); } catch (SQLException ex) { try { - conn = DriverManager.getConnection(url, user, password); + conn = createConnection(url, user, password); Statement stat = conn.createStatement(); stat.executeUpdate(String.format("CREATE DATABASE IF NOT EXISTS %s", dbName)); } catch (Exception e) { throw new MetaStoreException(ex); } + } catch (Exception ex) { + throw new MetaStoreException(ex); } - tidbInited = true; LOG.info("Tidb is initialized."); } } From 31edcf6fe6aae3aceec209604ee1c9d313141453 Mon Sep 17 00:00:00 2001 From: philo Date: Mon, 13 Nov 2017 16:41:19 +0800 Subject: [PATCH 09/12] update deployment doc --- docs/ssm-deployment-guide.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/ssm-deployment-guide.md b/docs/ssm-deployment-guide.md index b081b2de838..22fed535955 100644 --- a/docs/ssm-deployment-guide.md +++ b/docs/ssm-deployment-guide.md @@ -102,7 +102,8 @@ Configure SSM * Option 2. Use SSM-TiDB - Since TiDB has been integrated with SSM, you do not need to install TiDB beforehand. + To use TiDB, three shared libraries should be built beforehand and put into ${SMART_HOME}/lib. For build guide, you can refer to https://github.com/Intel-bigdata/ssm-tidb.git. + TiDB can be enabled in smart-site.xml. ```xml From f93fa16241934147fa1e73323e553a897d0e4bb3 Mon Sep 17 00:00:00 2001 From: philo Date: Tue, 14 Nov 2017 17:21:43 +0800 Subject: [PATCH 10/12] modify TestDBUtil.java --- .../metastore/utils/MetaStoreUtils.java | 175 +++++++++--------- .../org/smartdata/metastore/TestDBUtil.java | 2 +- 2 files changed, 87 insertions(+), 90 deletions(-) diff --git a/smart-metastore/src/main/java/org/smartdata/metastore/utils/MetaStoreUtils.java b/smart-metastore/src/main/java/org/smartdata/metastore/utils/MetaStoreUtils.java index aaec9be66aa..cb45335b7e3 100644 --- a/smart-metastore/src/main/java/org/smartdata/metastore/utils/MetaStoreUtils.java +++ b/smart-metastore/src/main/java/org/smartdata/metastore/utils/MetaStoreUtils.java @@ -55,9 +55,8 @@ public class MetaStoreUtils { "performance_schema", "PERFORMANCE_SCHEMA" }; + public static final String TIDB_DB_NAME = "ssm"; static final Logger LOG = LoggerFactory.getLogger(MetaStoreUtils.class); - private static MetaStore metaStore = null; - private static final String dbName = "ssm"; public static Connection createConnection(String url, String userName, String password) @@ -427,106 +426,104 @@ public static String getMysqlDBName(String url) throws SQLException { } public static MetaStore getDBAdapter( - SmartConf conf) throws MetaStoreException { - if (metaStore != null) { - return metaStore; - } else { - URL pathUrl = ClassLoader.getSystemResource(""); - String path = pathUrl.getPath(); + SmartConf conf) throws MetaStoreException { + URL pathUrl = ClassLoader.getSystemResource(""); + String path = pathUrl.getPath(); - String fileName = "druid.xml"; - String expectedCpPath = path + fileName; - LOG.info("Expected DB connection pool configuration path = " - + expectedCpPath); - File cpConfigFile = new File(expectedCpPath); - if (cpConfigFile.exists()) { - LOG.info("Using pool configure file: " + expectedCpPath); - Properties p = new Properties(); - try { - p.loadFromXML(new FileInputStream(cpConfigFile)); + String fileName = "druid.xml"; + String expectedCpPath = path + fileName; + LOG.info("Expected DB connection pool configuration path = " + + expectedCpPath); + File cpConfigFile = new File(expectedCpPath); + if (cpConfigFile.exists()) { + LOG.info("Using pool configure file: " + expectedCpPath); + Properties p = new Properties(); + try { + p.loadFromXML(new FileInputStream(cpConfigFile)); - boolean tidbEnabled = conf.getBoolean( - SmartConfKeys.SMART_TIDB_ENABLED, SmartConfKeys.SMART_TIDB_ENABLED_DEFAULT); - if (tidbEnabled) { - String tidbPort = conf.get(SmartConfKeys.TIDB_SERVICE_PORT_KEY, - SmartConfKeys.TIDB_SERVICE_PORT_KEY_DEFAULT); - String url = String.format("jdbc:mysql://127.0.0.1:%s", tidbPort); - String user = p.getProperty("username"); - String password = p.getProperty("password"); - initTidb(url, user, password); - url = url + "/" + dbName; + boolean tidbEnabled = conf.getBoolean( + SmartConfKeys.SMART_TIDB_ENABLED, SmartConfKeys.SMART_TIDB_ENABLED_DEFAULT); + if (tidbEnabled) { + String tidbPort = conf.get(SmartConfKeys.TIDB_SERVICE_PORT_KEY, + SmartConfKeys.TIDB_SERVICE_PORT_KEY_DEFAULT); + String url = String.format("jdbc:mysql://127.0.0.1:%s", tidbPort); + String user = p.getProperty("username"); + String password = p.getProperty("password"); + initTidb(url, user, password); + url = url + "/" + TIDB_DB_NAME; + p.setProperty("url", url); + LOG.info("\t" + "The jdbc url for Tidb is " + url); + } else { + String url = conf.get(SmartConfKeys.SMART_METASTORE_DB_URL_KEY); + if (url != null) { p.setProperty("url", url); - LOG.info("\t" + "The jdbc url for Tidb is " + url); - } else { - String url = conf.get(SmartConfKeys.SMART_METASTORE_DB_URL_KEY); - if (url != null) { - p.setProperty("url", url); - } } + } - String purl = p.getProperty("url"); - if (purl == null || purl.length() == 0) { - purl = getDefaultSqliteDB(); // For testing - p.setProperty("url", purl); - LOG.warn("Database URL not specified, using " + purl); - } + String purl = p.getProperty("url"); + if (purl == null || purl.length() == 0) { + purl = getDefaultSqliteDB(); // For testing + p.setProperty("url", purl); + LOG.warn("Database URL not specified, using " + purl); + } - if (purl.startsWith(MetaStoreUtils.MYSQL_URL_PREFIX)) { - String dbName = getMysqlDBName(purl); - for (String name : DB_NAME_NOT_ALLOWED) { - if (dbName.equals(name)) { - throw new MetaStoreException( - String.format( - "The database %s in mysql is for DB system use, " - + "please appoint other database in druid.xml.", - name)); - } + if (purl.startsWith(MetaStoreUtils.MYSQL_URL_PREFIX)) { + String dbName = getMysqlDBName(purl); + for (String name : DB_NAME_NOT_ALLOWED) { + if (dbName.equals(name)) { + throw new MetaStoreException( + String.format( + "The database %s in mysql is for DB system use, " + + "please appoint other database in druid.xml.", + name)); } } + } - for (String key : p.stringPropertyNames()) { - if (key.equals("password")) { - LOG.info("\t" + key + " = **********"); - } else { - LOG.info("\t" + key + " = " + p.getProperty(key)); - } - } - return new MetaStore(new DruidPool(p)); - } catch (Exception e) { - if (e instanceof InvalidPropertiesFormatException) { - throw new MetaStoreException( - "Malformat druid.xml, please check the file.", e); + for (String key : p.stringPropertyNames()) { + if (key.equals("password")) { + LOG.info("\t" + key + " = **********"); } else { - throw new MetaStoreException(e); + LOG.info("\t" + key + " = " + p.getProperty(key)); } } - } else { - LOG.info("DB connection pool config file " + expectedCpPath - + " NOT found."); - } - // Get Default configure from druid-template.xml - fileName = "druid-template.xml"; - expectedCpPath = path + fileName; - LOG.info("Expected DB connection pool configuration path = " - + expectedCpPath); - cpConfigFile = new File(expectedCpPath); - LOG.info("Using pool configure file: " + expectedCpPath); - Properties p = new Properties(); - try { - p.loadFromXML(new FileInputStream(cpConfigFile)); + return new MetaStore(new DruidPool(p)); } catch (Exception e) { - throw new MetaStoreException(e); - } - String url = conf.get(SmartConfKeys.SMART_METASTORE_DB_URL_KEY); - if (url != null) { - p.setProperty("url", url); - } - for (String key : p.stringPropertyNames()) { - LOG.info("\t" + key + " = " + p.getProperty(key)); + if (e instanceof InvalidPropertiesFormatException) { + throw new MetaStoreException( + "Malformat druid.xml, please check the file.", e); + } else { + throw new MetaStoreException(e); + } } - metaStore = new MetaStore(new DruidPool(p)); - return metaStore; + } else { + LOG.info("DB connection pool config file " + expectedCpPath + + " NOT found."); + } + // Get Default configure from druid-template.xml + fileName = "druid-template.xml"; + expectedCpPath = path + fileName; + LOG.info("Expected DB connection pool configuration path = " + + expectedCpPath); + cpConfigFile = new File(expectedCpPath); + LOG.info("Using pool configure file: " + expectedCpPath); + Properties p = new Properties(); + try { + p.loadFromXML(new FileInputStream(cpConfigFile)); + } catch (Exception e) { + throw new MetaStoreException(e); + } + String url = conf.get(SmartConfKeys.SMART_METASTORE_DB_URL_KEY); +// String url = "jdbc:mysql://127.0.0.1:4000/test"; + if (url != null) { + p.setProperty("url", url); + } + p.setProperty("username", "root"); + p.setProperty("password", ""); + for (String key : p.stringPropertyNames()) { + LOG.info("\t" + key + " = " + p.getProperty(key)); } + return new MetaStore(new DruidPool(p)); } public static Integer getKey(Map map, String value) { @@ -606,13 +603,13 @@ public static void initTidb(String url, String user, String password) try { conn = createConnection(url, user, ""); Statement stat = conn.createStatement(); - stat.executeUpdate(String.format("CREATE DATABASE IF NOT EXISTS %s", dbName)); + stat.executeUpdate(String.format("CREATE DATABASE IF NOT EXISTS %s", TIDB_DB_NAME)); stat.executeQuery(String.format("SET PASSWORD FOR root = PASSWORD('%s')", password)); } catch (SQLException ex) { try { conn = createConnection(url, user, password); Statement stat = conn.createStatement(); - stat.executeUpdate(String.format("CREATE DATABASE IF NOT EXISTS %s", dbName)); + stat.executeUpdate(String.format("CREATE DATABASE IF NOT EXISTS %s", TIDB_DB_NAME)); } catch (Exception e) { throw new MetaStoreException(ex); } diff --git a/smart-metastore/src/test/java/org/smartdata/metastore/TestDBUtil.java b/smart-metastore/src/test/java/org/smartdata/metastore/TestDBUtil.java index 00401b21b23..e4e3d1bb73f 100644 --- a/smart-metastore/src/test/java/org/smartdata/metastore/TestDBUtil.java +++ b/smart-metastore/src/test/java/org/smartdata/metastore/TestDBUtil.java @@ -59,7 +59,7 @@ public static String getTestDir() { } public static String getUniqueFilePath() { - return getTestDir() + "/" + UUID.randomUUID().toString(); + return getTestDir() + "/" + UUID.randomUUID().toString() + System.currentTimeMillis(); } public static String getUniqueDBFilePath() { From 6adf6b71cddfa10647751822a6f76b4fe2b3b6a2 Mon Sep 17 00:00:00 2001 From: philo Date: Tue, 14 Nov 2017 17:25:48 +0800 Subject: [PATCH 11/12] delete unused code for local test --- .../java/org/smartdata/metastore/utils/MetaStoreUtils.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/smart-metastore/src/main/java/org/smartdata/metastore/utils/MetaStoreUtils.java b/smart-metastore/src/main/java/org/smartdata/metastore/utils/MetaStoreUtils.java index cb45335b7e3..da6cf57ecb4 100644 --- a/smart-metastore/src/main/java/org/smartdata/metastore/utils/MetaStoreUtils.java +++ b/smart-metastore/src/main/java/org/smartdata/metastore/utils/MetaStoreUtils.java @@ -514,12 +514,9 @@ public static MetaStore getDBAdapter( throw new MetaStoreException(e); } String url = conf.get(SmartConfKeys.SMART_METASTORE_DB_URL_KEY); -// String url = "jdbc:mysql://127.0.0.1:4000/test"; if (url != null) { p.setProperty("url", url); } - p.setProperty("username", "root"); - p.setProperty("password", ""); for (String key : p.stringPropertyNames()) { LOG.info("\t" + key + " = " + p.getProperty(key)); } From 714e05af55bc08ff75816e453ec71e7c2339ef16 Mon Sep 17 00:00:00 2001 From: philo Date: Tue, 14 Nov 2017 18:44:01 +0800 Subject: [PATCH 12/12] refine druid.xml and deployment doc --- conf/druid.xml | 2 +- docs/ssm-deployment-guide.md | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/conf/druid.xml b/conf/druid.xml index 09b530d3e4b..3b68686bea0 100644 --- a/conf/druid.xml +++ b/conf/druid.xml @@ -23,7 +23,7 @@ - root + 10 diff --git a/docs/ssm-deployment-guide.md b/docs/ssm-deployment-guide.md index 22fed535955..1b34d778995 100644 --- a/docs/ssm-deployment-guide.md +++ b/docs/ssm-deployment-guide.md @@ -118,7 +118,9 @@ Configure SSM For SSM with multiple agents mode, Smart Server will run PD and TiDB instance and each agent will run a TiKV instance. So the storage capacity of SSM-TiDB can easily be scaled up by just adding more agent server. This is a great advantage over MySQL. - If TiDB is enabled, there is no need to configure jdbc url in druid.xml. You can set a password for root user in druid.xml. + If TiDB is enabled, there is no need to configure jdbc url in druid.xml. In TiDB only root user is created initially, so you should set username as root. Optionally, you can set a password for root user in druid.xml. + + An example of configuration in druid.xml for using TiDB is shown as follows. ```xml