Skip to content
This repository has been archived by the owner on Jan 3, 2023. It is now read-only.

Solved #1386 and make tidb ports & db user's password configurable #1392

Merged
merged 13 commits into from
Nov 15, 2017
6 changes: 3 additions & 3 deletions conf/druid.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@

<properties>
<!-- <entry key="url">jdbc:sqlite:sqlit-db-path</entry> -->
<!-- <entry key="url">jdbc:mysql://127.0.0.1:4000/test</entry> default url for Tidb-->
<entry key="username">""</entry>
<entry key="password">""</entry>
<!-- <entry key="url">jdbc:mysql://127.0.0.1:3306/test</entry> -->
<entry key="username"></entry>
<entry key="password"></entry>

<entry key="initialSize">10</entry>
<entry key="minIdle">4</entry>
Expand Down
24 changes: 24 additions & 0 deletions conf/smart-default.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,28 @@
<value>false</value>
<description>Actions will not be executed on SSM server if true.</description>
</property>

<property>
<name>pd.client.port</name>
<value>7060</value>
<description>Pd port in client-url option</description>
</property>

<property>
<name>pd.peer.port</name>
<value>7061</value>
<description>Pd port in peer-url option</description>
</property>

<property>
<name>tikv.service.port</name>
<value>20160</value>
<description>Tikv port in addr option</description>
</property>

<property>
<name>tidb.service.port</name>
<value>7070</value>
<description>SmartAgent master port</description>
</property>
</configuration>
4 changes: 1 addition & 3 deletions conf/smart-site.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@
<name>smart.tidb.enable</name>
<value>false</value>
<description>
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.
</description>
</property>

</configuration>
16 changes: 12 additions & 4 deletions docs/ssm-deployment-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -117,19 +118,26 @@ 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. 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
<properties>
<entry key="url">jdbc:mysql://127.0.0.1:4000/test</entry>
<!-- <entry key="url">jdbc:mysql://127.0.0.1:4000/test</entry> no need to configure url for TiDB -->
<entry key="username">root</entry>
<entry key="password"></entry>
......
<properties>
```

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 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.

* **Configure user account to authenticate to Web UI**

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "7060";
public static final String PD_PEER_PORT_KEY = "pd.peer.port";
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 = "7070";
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public int hashCode() {

@Override
public String toString() {
return "AgentId{ id=" + id + "}";
return "AgentId{id=" + id + "}";
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public class MetaStoreUtils {
"performance_schema",
"PERFORMANCE_SCHEMA"
};
public static final String TIDB_DB_NAME = "ssm";
static final Logger LOG = LoggerFactory.getLogger(MetaStoreUtils.class);

public static Connection createConnection(String url,
Expand Down Expand Up @@ -425,24 +426,38 @@ public static String getMysqlDBName(String url) throws SQLException {
}

public static MetaStore getDBAdapter(
SmartConf conf) throws MetaStoreException {
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);
+ 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 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 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);
}
}

String purl = p.getProperty("url");
Expand All @@ -457,10 +472,10 @@ public static MetaStore getDBAdapter(
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));
String.format(
"The database %s in mysql is for DB system use, "
+ "please appoint other database in druid.xml.",
name));
}
}
}
Expand All @@ -476,20 +491,20 @@ public static MetaStore getDBAdapter(
} catch (Exception e) {
if (e instanceof InvalidPropertiesFormatException) {
throw new MetaStoreException(
"Malformat druid.xml, please check the file.", e);
"Malformat druid.xml, please check the file.", e);
} else {
throw new MetaStoreException(e);
}
}
} else {
LOG.info("DB connection pool config file " + expectedCpPath
+ " NOT found.");
+ " 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);
+ expectedCpPath);
cpConfigFile = new File(expectedCpPath);
LOG.info("Using pool configure file: " + expectedCpPath);
Properties p = new Properties();
Expand Down Expand Up @@ -578,4 +593,27 @@ public static void dropAllTablesMysql(Connection conn,
throw new MetaStoreException(e);
}
}

public static void initTidb(String url, String user, String password)
throws MetaStoreException {
Connection conn;
try {
conn = createConnection(url, user, "");
Statement stat = conn.createStatement();
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", TIDB_DB_NAME));
} catch (Exception e) {
throw new MetaStoreException(ex);
}
} catch (Exception ex) {
throw new MetaStoreException(ex);
}
LOG.info("Tidb is initialized.");
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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()) {
Expand Down
12 changes: 3 additions & 9 deletions smart-tidb/src/main/java/org/smartdata/tidb/LaunchDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
12 changes: 10 additions & 2 deletions smart-tidb/src/main/java/org/smartdata/tidb/PdServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
13 changes: 11 additions & 2 deletions smart-tidb/src/main/java/org/smartdata/tidb/TidbServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
12 changes: 10 additions & 2 deletions smart-tidb/src/main/java/org/smartdata/tidb/TikvServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down