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

Commit

Permalink
SUBMARINE-598. Support get environment list from database
Browse files Browse the repository at this point in the history
### What is this PR for?
Improve environment swagger api.
When cache is null. Will get environment data from database.

### What type of PR is it?
[Improvement]

### Todos

### What is the Jira issue?
https://issues.apache.org/jira/browse/SUBMARINE-598

### How should this be tested?
https://travis-ci.org/github/kobe860219/submarine/builds/716904008?utm_source=github_status&utm_medium=notification

### Screenshots (if appropriate)
<img width="1358" alt="截圖 2020-08-16 上午12 11 07" src="https://user-images.githubusercontent.com/48027290/90316540-71a3dd00-df55-11ea-9752-0eeead919962.png">

### Questions:
* Does the licenses files need update? No
* Is there breaking changes for older versions? No
* Does this needs documentation? No

Author: kobe860219 <kobe860219@gmail.com>

Closes #374 from kobe860219/SUBMARINE-598 and squashes the following commits:

d4d4938 [kobe860219] SUBMARINE-598. Support get environment list from database
8c07802 [kobe860219] SUBMARINE-598. Support get environment list from database
e755201 [kobe860219] SUBMARINE-598. Support get environment list from database
d535c66 [kobe860219] SUBMARINE-598. Support get environment list from database
736c192 [kobe860219] SUBMARINE-598. Support get environment list from database
065ef0d [kobe860219] SUBMARINE-598. Support get environment list from database
730c0b8 [kobe860219] SUBMARINE-598. Support get environment list from database
19920f2 [kobe860219] Edit .gitignore
9d5506c [kobe860219] Edit .gitignore
22b2bfe [kobe860219] SUBMARINE-598. Support get environment list from database
  • Loading branch information
kobe860219 authored and xunliu committed Aug 19, 2020
1 parent 3060ef7 commit 689ae3c
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 1 deletion.
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?
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

0 comments on commit 689ae3c

Please sign in to comment.