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

SUBMARINE-598. Support get environment list from database #374

Closed
wants to merge 10 commits into from
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ public class EnvironmentManager {

private final AtomicInteger environmentIdCounter = new AtomicInteger(0);

private static Boolean readedDB = true;

/**
* Environment Cache
*/
Expand Down Expand Up @@ -206,6 +208,31 @@ public List<Environment> listEnvironments(String status)
throws SubmarineRuntimeException {
List<Environment> environmentList =
new ArrayList<Environment>(cachedEnvironments.values());

// Is it available in cache?
pingsutw marked this conversation as resolved.
Show resolved Hide resolved
if (this.readedDB == true) {
try (SqlSession sqlSession = MyBatisUtil.getSqlSession()) {
EnvironmentMapper environmentMapper =
sqlSession.getMapper(EnvironmentMapper.class);
List<EnvironmentEntity> environmentEntitys = environmentMapper.selectAll();
for (EnvironmentEntity environmentEntity : environmentEntitys) {
if (environmentEntity != null) {
Environment env = new Environment();
env.setEnvironmentSpec(new Gson().fromJson(
environmentEntity.getEnvironmentSpec(), EnvironmentSpec.class));
env.setEnvironmentId(
EnvironmentId.fromString(environmentEntity.getId()));
environmentList.add(env);
cachedEnvironments.put(env.getEnvironmentSpec().getName(), env);
}
}
} catch (Exception e) {
LOG.error(e.getMessage(), e);
throw new SubmarineRuntimeException(Status.BAD_REQUEST.getStatusCode(),
"Unable to get the environment list.");
}
}
this.readedDB = false;
return environmentList;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@

import org.apache.submarine.server.environment.database.entity.EnvironmentEntity;

import java.util.List;

public interface EnvironmentMapper {

List<EnvironmentEntity> selectAll();

EnvironmentEntity select(String environmentName);

int insert(EnvironmentEntity environment);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@
id, environment_name, environment_spec, create_by, create_time, update_by, update_time
</sql>

<select id="selectAll" parameterType="java.lang.String" resultMap="resultMap">
select
<include refid="Base_Column_List" />
from environment
</select>

<select id="select" parameterType="java.lang.String" resultMap="resultMap">
select
<include refid="Base_Column_List" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,11 @@ public void listEnvironment() {
Response getEnvResponse = environmentStoreApi.listEnvironment("");
String entity = (String) getEnvResponse.getEntity();
JsonResponse jsonResponse = gson.fromJson(entity, JsonResponse.class);

// environments.length = 2; One is created in this test, one is get from database
Environment[] environments = gson
.fromJson(gson.toJson(jsonResponse.getResult()), Environment[].class);
assertEquals(1, environments.length);
assertEquals(2, environments.length);

Environment environment = environments[0];
assertEquals("foo", environment.getEnvironmentSpec().getName());
Expand Down