Skip to content
This repository has been archived by the owner on Jul 10, 2024. It is now read-only.

SUBMARINE-567. Fix travis test failure #347

Closed
wants to merge 19 commits into from
12 changes: 0 additions & 12 deletions docs/database/metastore.sql
Original file line number Diff line number Diff line change
Expand Up @@ -963,18 +963,6 @@ CREATE TABLE WRITE_SET (
WS_OPERATION_TYPE char(1) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE `environment` (
`id` varchar(64) NOT NULL COMMENT 'Id of the Environment',
`environment_name` varchar(255) NOT NULL COMMENT 'Name of the Environment',
`environment_spec` text NOT NULL COMMENT 'Spec of the Environment',
`create_by` varchar(32) DEFAULT NULL COMMENT 'create user',
`create_time` datetime DEFAULT NULL COMMENT 'create time',
`update_by` varchar(32) DEFAULT NULL COMMENT 'last update user',
`update_time` datetime DEFAULT NULL COMMENT 'last update time',
PRIMARY KEY `id` (`id`),
UNIQUE KEY `environment_name` (`environment_name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- -----------------------------------------------------------------
-- Record schema version. Should be the last step in the init script
-- -----------------------------------------------------------------
Expand Down
18 changes: 17 additions & 1 deletion docs/database/submarine.sql
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,23 @@ CREATE TABLE `job` (
`update_time` datetime default NULL COMMENT 'last update time',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


-- ----------------------------
-- Table structure for environment
-- ----------------------------
DROP TABLE IF EXISTS `environment`;
CREATE TABLE `environment` (
`id` varchar(64) NOT NULL COMMENT 'Id of the Environment',
`environment_name` varchar(255) NOT NULL COMMENT 'Name of the Environment',
`environment_spec` text NOT NULL COMMENT 'Spec of the Environment',
`create_by` varchar(32) DEFAULT NULL COMMENT 'create user',
`create_time` datetime DEFAULT NULL COMMENT 'create time',
`update_by` varchar(32) DEFAULT NULL COMMENT 'last update user',
`update_time` datetime DEFAULT NULL COMMENT 'last update time',
PRIMARY KEY `id` (`id`),
UNIQUE KEY `environment_name` (`environment_name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Table structure for metric
-- ----------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,39 +34,33 @@ public class MyBatisUtil {
private static final Logger LOG = LoggerFactory.getLogger(MyBatisUtil.class);

private static SqlSessionFactory sqlSessionFactory;
private static SqlSessionFactory metastoreSqlSessionFactory;

static {
SubmarineConfiguration conf = SubmarineConfiguration.getInstance();
sqlSessionFactory = buildSqlSessionFactory("mybatis-config.xml",
conf.getJdbcUrl(), conf.getJdbcUserName(), conf.getJdbcPassword());
metastoreSqlSessionFactory = buildSqlSessionFactory(
"mybatis-config-metastore.xml", conf.getMetastoreJdbcUrl(),
conf.getMetastoreJdbcUserName(), conf.getMetastoreJdbcPassword());
}

private static SqlSessionFactory buildSqlSessionFactory(String configFile,
String jdbcUrl, String jdbcUserName, String jdbcPassword) {
Reader reader = null;
SqlSessionFactory sqlSessionFactory = null;
try {
try {
reader = Resources.getResourceAsReader(configFile);
reader = Resources.getResourceAsReader("mybatis-config.xml");
} catch (IOException e) {
LOG.error(e.getMessage(), e);
throw new RuntimeException(e.getMessage());
}
checkCalledByTestMethod(jdbcUrl, jdbcUserName, jdbcPassword);
String jdbcClassName =
SubmarineConfiguration.getInstance().getJdbcDriverClassName();
LOG.info(
"MyBatisUtil -> jdbcClassName: {}, jdbcUrl: {}, jdbcUserName: {}, jdbcPassword: {}",
jdbcClassName, jdbcUrl, jdbcUserName, jdbcPassword);

checkCalledByTestMethod();

SubmarineConfiguration conf = SubmarineConfiguration.getInstance();
String jdbcClassName = conf.getJdbcDriverClassName();
String jdbcUrl = conf.getJdbcUrl();
String jdbcUserName = conf.getJdbcUserName();
String jdbcPassword = conf.getJdbcPassword();
LOG.info("MyBatisUtil -> jdbcClassName: {}, jdbcUrl: {}, jdbcUserName: {}, jdbcPassword: {}",
jdbcClassName, jdbcUrl, jdbcUserName, jdbcPassword);

Properties props = new Properties();
props.setProperty("jdbc.driverClassName", jdbcClassName);
props.setProperty("jdbc.url", jdbcUrl);
props.setProperty("jdbc.username", jdbcUserName);
props.setProperty("jdbc.password", jdbcPassword);

sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader, props);
} finally {
try {
Expand All @@ -77,7 +71,6 @@ private static SqlSessionFactory buildSqlSessionFactory(String configFile,
LOG.error(e.getMessage(), e);
}
}
return sqlSessionFactory;
}

/**
Expand All @@ -88,31 +81,25 @@ private static SqlSessionFactory buildSqlSessionFactory(String configFile,
public static SqlSession getSqlSession() {
return sqlSessionFactory.openSession();
}

public static SqlSession getMetastoreSqlSession() {
return metastoreSqlSessionFactory.openSession();
}

private static void checkCalledByTestMethod(String jdbcUrl,
String jdbcUserName, String jdbcPassword) {
StackTraceElement[] stackTraceElements =
Thread.currentThread().getStackTrace();
private static void checkCalledByTestMethod() {
StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
for (StackTraceElement element : stackTraceElements) {
if (element.getClassName().endsWith("Test")) {
usingTestDatabase(jdbcUrl, jdbcUserName, jdbcPassword);
usingTestDatabase();
return;
}
}
}

private static void usingTestDatabase(String jdbcUrl, String jdbcUserName,
String jdbcPassword) {
private static void usingTestDatabase() {
LOG.info("Run the test unit using the test database");
String jdbcPropertiesSuffix = "_test";
String finalJdbcUrl = jdbcUrl.replace("?", jdbcPropertiesSuffix + "?");
// Run the test unit using the test database
SubmarineConfiguration conf = SubmarineConfiguration.getInstance();
conf.setJdbcUrl(finalJdbcUrl);
conf.setJdbcUserName(jdbcUserName + jdbcPropertiesSuffix);
conf.setJdbcPassword(jdbcPassword + jdbcPropertiesSuffix);
conf.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/submarine_test?" +
"useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&" +
"failOverReadOnly=false&zeroDateTimeBehavior=convertToNull&useSSL=false");
conf.setJdbcUserName("submarine_test");
conf.setJdbcPassword("password_test");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,20 @@
* Environment Management
*/
public class EnvironmentManager {

private static final Logger LOG =
LoggerFactory.getLogger(EnvironmentManager.class);

private static volatile EnvironmentManager manager;

private final AtomicInteger environmentIdCounter = new AtomicInteger(0);

/**
* Environment Cache
*/
private final ConcurrentMap<String, Environment> cachedEnvironments =
new ConcurrentHashMap<>();

/**
* Get the singleton instance
* @return object
Expand All @@ -76,23 +76,22 @@ public static EnvironmentManager getInstance() {
}

private EnvironmentManager() {

}

/**
* Create Environment
* @param spec environment spec
* @return Environment environment
* @throws SubmarineRuntimeException the service error
* @throws MetaException
*/
public Environment createEnvironment(EnvironmentSpec spec)
throws SubmarineRuntimeException {
checkSpec(spec);
LOG.info("Create Environment using spec: " + spec.toString());
return createOrUpdateEnvironment(spec, "c");
}

/**
* Update environment
* @param name Name of the environment
Expand All @@ -111,7 +110,7 @@ public Environment updateEnvironment(String name, EnvironmentSpec spec)
LOG.info("Update Environment using spec: " + spec.toString());
return createOrUpdateEnvironment(spec, "u");
}

private Environment createOrUpdateEnvironment(EnvironmentSpec spec,
String operation) {
EnvironmentEntity entity = new EnvironmentEntity();
Expand All @@ -120,7 +119,7 @@ private Environment createOrUpdateEnvironment(EnvironmentSpec spec,
entity.setEnvironmentName(spec.getName());
entity.setEnvironmentSpec(
new GsonBuilder().disableHtmlEscaping().create().toJson(spec));
try (SqlSession sqlSession = MyBatisUtil.getMetastoreSqlSession()) {
try (SqlSession sqlSession = MyBatisUtil.getSqlSession()) {
EnvironmentMapper environmentMapper =
sqlSession.getMapper(EnvironmentMapper.class);
if (operation.equals("c")) {
Expand All @@ -144,12 +143,12 @@ private Environment createOrUpdateEnvironment(EnvironmentSpec spec,
"Unable to process the environment spec.");
}
}

private EnvironmentId generateEnvironmentId() {
return EnvironmentId.newInstance(SubmarineServer.getServerTimeStamp(),
environmentIdCounter.incrementAndGet());
}

/**
* Delete environment
* @param name Name of the environment
Expand All @@ -163,14 +162,14 @@ public Environment deleteEnvironment(String name)
throw new SubmarineRuntimeException(Status.NOT_FOUND.getStatusCode(),
"Environment not found.");
}

LOG.info("Delete Environment for " + name);
try (SqlSession sqlSession = MyBatisUtil.getMetastoreSqlSession()) {
try (SqlSession sqlSession = MyBatisUtil.getSqlSession()) {
EnvironmentMapper environmentMapper =
sqlSession.getMapper(EnvironmentMapper.class);
environmentMapper.delete(name);
sqlSession.commit();

// Invalidate cache
cachedEnvironments.remove(name);
return env;
Expand All @@ -180,7 +179,7 @@ public Environment deleteEnvironment(String name)
"Unable to delete the environment.");
}
}

/**
* Get Environment
* @param name Name of the environment
Expand Down Expand Up @@ -212,7 +211,7 @@ public List<Environment> listEnvironments(String status)
private void checkSpec(EnvironmentSpec spec)
throws SubmarineRuntimeException {
if (spec == null) {
throw new SubmarineRuntimeException(Status.OK.getStatusCode(),
throw new SubmarineRuntimeException(Status.BAD_REQUEST.getStatusCode(),
"Invalid environment spec.");
}
}
Expand All @@ -225,10 +224,9 @@ private Environment getEnvironmentDetails(String name)
if (env != null) {
return env;
}

try (SqlSession sqlSession = MyBatisUtil.getMetastoreSqlSession()) {
EnvironmentMapper environmentMapper =
sqlSession.getMapper(EnvironmentMapper.class);

try (SqlSession sqlSession = MyBatisUtil.getSqlSession()) {
EnvironmentMapper environmentMapper = sqlSession.getMapper(EnvironmentMapper.class);
EnvironmentEntity environmentEntity = environmentMapper.select(name);

if (environmentEntity != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,6 @@
<mapper resource='org/apache/submarine/database/mappers/JobMapper.xml'/>
<mapper resource='org/apache/submarine/database/mappers/MetricMapper.xml'/>
<mapper resource='org/apache/submarine/database/mappers/ParamMapper.xml'/>
<mapper resource='org/apache/submarine/database/mappers/EnvironmentMapper.xml'/>
</mappers>
</configuration>
Loading