Skip to content

Commit

Permalink
#419 支持更新zk集群的connctionstring
Browse files Browse the repository at this point in the history
  • Loading branch information
kfchu committed Apr 29, 2018
1 parent 1e5f351 commit 88bed50
Show file tree
Hide file tree
Showing 8 changed files with 172 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,10 @@ private List<ZkCluster> filterOnlineZkClusters(Collection<ZkCluster> zkClusters)
@ApiResponses(value = {@ApiResponse(code = 200, message = "Success/Fail", response = RequestResult.class)})
@Audit
@PostMapping(value = "/zkClusters")
public SuccessResponseEntity createZkCluster(@RequestParam String zkClusterKey, @RequestParam String alias,
public SuccessResponseEntity createOrUpdateZkCluster(@RequestParam String zkClusterKey, @RequestParam String alias,
@RequestParam String connectString) throws SaturnJobConsoleException {
assertIsPermitted(Permissions.registryCenterAddZkCluster);
registryCenterService.createZkCluster(zkClusterKey, alias, connectString);
registryCenterService.createOrUpdateZkCluster(zkClusterKey, alias, connectString.trim());
return new SuccessResponseEntity();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ public interface ZkClusterInfoRepository {

int insert(ZkClusterInfo zkClusterInfo);

int update(ZkClusterInfo zkClusterInfo);

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.vip.saturn.job.console.mybatis.service;

import com.vip.saturn.job.console.domain.ZkCluster;
import com.vip.saturn.job.console.mybatis.entity.ZkClusterInfo;

import java.util.List;
Expand All @@ -15,4 +16,6 @@ public interface ZkClusterInfoService {

int createZkCluster(String clusterKey, String alias, String connectString, String createdBy);

int updateZkCluster(ZkClusterInfo zkClusterInfo);

}
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,10 @@ public int createZkCluster(String clusterKey, String alias, String connectString
zkClusterInfo.setConnectString(connectString);
return zkClusterInfoRepository.insert(zkClusterInfo);
}

@Transactional
@Override
public int updateZkCluster(ZkClusterInfo zkClusterInfo) {
return zkClusterInfoRepository.update(zkClusterInfo);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public interface RegistryCenterService {

ZkCluster getZkCluster(String key);

void createZkCluster(String zkClusterKey, String alias, String connectString) throws SaturnJobConsoleException;
void createOrUpdateZkCluster(String zkClusterKey, String alias, String connectString) throws SaturnJobConsoleException;

Collection<ZkCluster> getZkClusterList();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1035,9 +1035,17 @@ public ZkCluster getZkCluster(String zkClusterKey) {

@Transactional(rollbackFor = Exception.class)
@Override
public void createZkCluster(String zkClusterKey, String alias, String connectString)
public void createOrUpdateZkCluster(String zkClusterKey, String alias, String connectString)
throws SaturnJobConsoleException {
zkClusterInfoService.createZkCluster(zkClusterKey, alias, connectString, "");
ZkClusterInfo zkClusterInfo = zkClusterInfoService.getByClusterKey(zkClusterKey);
if (zkClusterInfo == null) {
zkClusterInfoService.createZkCluster(zkClusterKey, alias, connectString, "");
} else {
// cannot change alias but only connection string
zkClusterInfo.setConnectString(connectString);
zkClusterInfo.setLastUpdateTime(new Date());
zkClusterInfoService.updateZkCluster(zkClusterInfo);
}
notifyRefreshRegCenter();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,12 @@
insert into zk_cluster_info(create_time, created_by, last_update_time, last_updated_by, zk_cluster_key, alias, connect_string)
values(#{createTime, jdbcType=TIMESTAMP}, #{createdBy, jdbcType=VARCHAR},#{lastUpdateTime, jdbcType=TIMESTAMP},#{lastUpdatedBy, jdbcType=VARCHAR},#{zkClusterKey, jdbcType=VARCHAR},#{alias, jdbcType=VARCHAR},#{connectString, jdbcType=VARCHAR})
</insert>
<update id="update" parameterType="com.vip.saturn.job.console.mybatis.entity.ZkClusterInfo">
update zk_cluster_info set
alias = #{alias,jdbcType=VARCHAR},
connect_string = #{connectString,jdbcType=VARCHAR},
last_updated_by = #{lastUpdatedBy,jdbcType=VARCHAR},
last_update_time = #{lastUpdateTime,jdbcType=TIMESTAMP}
where zk_cluster_key = #{zkClusterKey, jdbcType=VARCHAR}
</update>
</mapper>
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package com.vip.saturn.job.console.springboot.test;

import com.alibaba.fastjson.JSONObject;
import com.vip.saturn.job.console.AbstractSaturnConsoleTest;
import com.vip.saturn.job.console.controller.rest.AlarmRestApiController;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;

import java.util.List;
import java.util.Map;

import static org.junit.Assert.assertEquals;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@RunWith(SpringRunner.class)
@WebMvcTest(AlarmRestApiController.class)
public class RegistryCenterControllerTest extends AbstractSaturnConsoleTest {

@Autowired
private MockMvc mvc;

@Test
public void testCreateAndUpdateZkClusterInfo() throws Exception {
// craete a new zkCluster
ZkClusterInfoForTest zkClusterInfo = new ZkClusterInfoForTest("clusterx", "alias1", "127.0.0.1:2181");
MvcResult result = mvc
.perform(post("/console/zkClusters").contentType(MediaType.APPLICATION_FORM_URLENCODED).content(zkClusterInfo.toContent()))
.andExpect(status().isOk()).andReturn();
String responseBody = result.getResponse().getContentAsString();
Map<String, Object> resultMap = JSONObject.parseObject(responseBody, Map.class);
assertEquals(0, resultMap.get("status"));

// refresh
mvc.perform(get("/console/registryCenter/refresh")).andExpect(status().isOk()).andReturn();
// get and compare
int size;
int count = 0;
List<Map<String, String>> objValue;
do {
Thread.sleep(3000L);
result = mvc.perform(get("/console/zkClusters")).andExpect(status().isOk()).andReturn();
responseBody = result.getResponse().getContentAsString();
resultMap = JSONObject.parseObject(responseBody, Map.class);
objValue = (List<Map<String, String>>) resultMap.get("obj");
size = objValue.size();
} while (size == 1 && count++ < 6);

assertEquals(2, size);

String connectionString = "";
for (Map<String, String> clusterInfo : objValue) {
String clusterKey = clusterInfo.get("zkClusterKey");
if (clusterKey.equals("clusterx")) {
connectionString = clusterInfo.get("zkAddr");
break;
}
}

assertEquals("127.0.0.1:2181", connectionString);

// update
zkClusterInfo = new ZkClusterInfoForTest("clusterx", "alias1", "127.0.0.1:2182 ");
result = mvc.perform(post("/console/zkClusters").contentType(MediaType.APPLICATION_FORM_URLENCODED).content(zkClusterInfo.toContent()))
.andExpect(status().isOk()).andReturn();
responseBody = result.getResponse().getContentAsString();
resultMap = JSONObject.parseObject(responseBody, Map.class);
assertEquals(0, resultMap.get("status"));

// refresh
mvc.perform(get("/console/registryCenter/refresh")).andExpect(status().isOk()).andReturn();
// get and compare
count = 0;
do {
Thread.sleep(3000L);
result = mvc.perform(get("/console/zkClusters")).andExpect(status().isOk()).andReturn();
responseBody = result.getResponse().getContentAsString();
resultMap = JSONObject.parseObject(responseBody, Map.class);
objValue = (List<Map<String, String>>) resultMap.get("obj");
size = objValue.size();
for (Map<String, String> clusterInfo : objValue) {
String clusterKey = clusterInfo.get("zkClusterKey");
if (clusterKey.equals("clusterx")) {
connectionString = clusterInfo.get("zkAddr");
break;
}
}
} while (!connectionString.equals("127.0.0.1:2182") && count++ < 6);

}

private static class ZkClusterInfoForTest {
private String zkClusterKey;

private String alias;

private String connectString;

public ZkClusterInfoForTest(String zkClusterKey, String alias, String connectString) {
this.zkClusterKey = zkClusterKey;
this.alias = alias;
this.connectString = connectString;
}

public String toContent() {
return String.format("zkClusterKey=%s&alias=%s&connectString=%s", zkClusterKey, alias, connectString);
}

public String getZkClusterKey() {
return zkClusterKey;
}

public void setZkClusterKey(String zkClusterKey) {
this.zkClusterKey = zkClusterKey;
}

public String getAlias() {
return alias;
}

public void setAlias(String alias) {
this.alias = alias;
}

public String getConnectString() {
return connectString;
}

public void setConnectString(String connectString) {
this.connectString = connectString;
}
}
}

0 comments on commit 88bed50

Please sign in to comment.