Skip to content

Commit

Permalink
#696 zk集群管理增加删除功能
Browse files Browse the repository at this point in the history
  • Loading branch information
RolfHeG committed Jun 16, 2020
1 parent 6d4f978 commit 9c455bc
Show file tree
Hide file tree
Showing 14 changed files with 184 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,16 @@ public SuccessResponseEntity updateZkCluster(@AuditParam("zkClusterKey") @Reques
return new SuccessResponseEntity();
}

@ApiResponses(value = {@ApiResponse(code = 200, message = "Success/Fail", response = RequestResult.class)})
@Audit
@PutMapping(value = "/zkClusters/delete")
public SuccessResponseEntity deleteZkCluster(@AuditParam("zkClusterKey") @RequestParam String zkClusterKey)
throws SaturnJobConsoleException {
assertIsPermitted(PermissionKeys.registryCenterRemoveZkCluster);
registryCenterService.deleteZkCluster(zkClusterKey);
return new SuccessResponseEntity();
}

/**
* 获取单个或所有zk集群信息
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,33 @@ public boolean equals(Object o) {

}


/**
* 某些信息相等时不需要更新
*/
public boolean equalsNoNeedReconnect(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof ZkCluster)) {
return false;
}

ZkCluster zkCluster = (ZkCluster) o;

if (zkClusterKey != null ? !zkClusterKey.equals(zkCluster.zkClusterKey) : zkCluster.zkClusterKey != null) {
return false;
}
if (zkAlias != null ? !zkAlias.equals(zkCluster.zkAlias) : zkCluster.zkAlias != null) {
return false;
}
if (zkAddr != null ? !zkAddr.equals(zkCluster.zkAddr) : zkCluster.zkAddr != null) {
return false;
}
return digest != null ? digest.equals(zkCluster.digest) : zkCluster.digest == null;

}

@Override
public int hashCode() {
int result = zkClusterKey != null ? zkClusterKey.hashCode() : 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ public interface ZkClusterInfoRepository {

int update(ZkClusterInfo zkClusterInfo);

int deleteByClusterKey(String zkClusterKey);

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

int updateZkCluster(ZkClusterInfo zkClusterInfo);

int deleteZkCluster(String zkClusterKey);

}
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,10 @@ public int createZkCluster(String clusterKey, String alias, String connectString
public int updateZkCluster(ZkClusterInfo zkClusterInfo) {
return zkClusterInfoRepository.update(zkClusterInfo);
}

@Transactional
@Override
public int deleteZkCluster(String zkClusterKey) {
return zkClusterInfoRepository.deleteByClusterKey(zkClusterKey);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,5 @@ void bindNamespaceAndZkCluster(String namespace, String zkClusterKey, String upd

void refreshRegistryCenterForNamespace(String zkCluster, String namespace);

void deleteZkCluster(String zkClusterKey) throws SaturnJobConsoleException ;
}
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,9 @@ private void closeInvalidZkClient(Map<String, ZkCluster> newClusterMap) {
ZkCluster newZkCluster = newClusterMap.get(zkClusterKey);
if (zkCluster.equals(newZkCluster)) {
newClusterMap.put(zkClusterKey, zkCluster);
} else if (zkCluster.equalsNoNeedReconnect(newZkCluster)){
zkCluster.setDescription(newZkCluster.getDescription());
newClusterMap.put(zkClusterKey, zkCluster);
} else {
iterator.remove();
closeZkCluster(zkCluster);
Expand Down Expand Up @@ -1441,6 +1444,22 @@ public synchronized void refreshRegistryCenterForNamespace(String zkClusterName,
log.info("refreshRegistryCenterForNamespace done : {}, {}", zkClusterName, namespace);
}

@Transactional(rollbackFor = Exception.class)
@Override
public void deleteZkCluster(String zkClusterKey) throws SaturnJobConsoleException {
if (getZkCluster(zkClusterKey) == null) {
throw new SaturnJobConsoleException("fail to delete.for ZkCluster does not exist");
}
if (domainCount(zkClusterKey) > 0) {
throw new SaturnJobConsoleException("fail to delete.for ZkCluster still has domains");
}
if (!CollectionUtils.isEmpty(namespaceZkClusterMapping4SqlService.getAllNamespacesOfCluster(zkClusterKey))) {
throw new SaturnJobConsoleException("fail to delete.for ZkCluster still has domains");
}
zkClusterInfoService.deleteZkCluster(zkClusterKey);
notifyRefreshRegCenter();
}

private Map<String, ZkCluster> getTargetZkCluster(String zkCluster) {
Map<String, ZkCluster> result = new HashMap<>(1);
Map<String, ZkCluster> zkClusterInfo = getZkClusterInfo();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.apache.zookeeper.data.Stat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class PermissionKeys {
public static final String registryCenterBatchMoveNamespaces = "registryCenter:batchMoveNamespaces";
public static final String registryCenterExportNamespaces = "registryCenter:exportNamespaces";
public static final String registryCenterAddZkCluster = "registryCenter:addZkCluster";
public static final String registryCenterRemoveZkCluster = "registryCenter:removeZkCluster";

public static final String systemConfig = "systemConfig";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,8 @@
last_update_time = #{lastUpdateTime,jdbcType=TIMESTAMP}
where zk_cluster_key = #{zkClusterKey, jdbcType=VARCHAR}
</update>
<delete id="deleteByClusterKey" parameterType="java.lang.String">
delete from zk_cluster_info
where zk_cluster_key = #{zkClusterKey, jdbcType=VARCHAR} limit 1
</delete>
</mapper>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.vip.saturn.job.console.domain;

import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.BeanUtils;

public class ZkClusterTest {

@Test
public void testEqualsNoNeedReconnect() {
ZkCluster zkCluster1 = new ZkCluster();
zkCluster1.setZkAlias("saturn");
zkCluster1.setZkAddr("127.0.0.1:2181");
zkCluster1.setZkClusterKey("saturn-zk");
zkCluster1.setDescription("cluster1");

ZkCluster zkCluster2 = new ZkCluster();
BeanUtils.copyProperties(zkCluster1, zkCluster2);
zkCluster2.setDescription("cluster2");

Assert.assertFalse(zkCluster1.equals(zkCluster2));
Assert.assertTrue(zkCluster1.equalsNoNeedReconnect(zkCluster2));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.vip.saturn.job.console.service.impl;

import com.vip.saturn.job.console.domain.RegistryCenterConfiguration;
import com.vip.saturn.job.console.domain.ZkCluster;
import com.vip.saturn.job.console.exception.SaturnJobConsoleException;
import com.vip.saturn.job.console.mybatis.service.NamespaceZkClusterMapping4SqlService;
import com.vip.saturn.job.console.mybatis.service.ZkClusterInfoService;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;

import static org.junit.Assert.fail;
import static org.mockito.Mockito.*;

@RunWith(MockitoJUnitRunner.class)
public class RegistryCenterServiceImplTest {

@Mock
private LinkedHashMap<String, ZkCluster> zkClusterMap;

@Mock
private NamespaceZkClusterMapping4SqlService namespaceZkClusterMapping4SqlService;

@Mock
private ZkClusterInfoService zkClusterInfoService;

@Rule
public ExpectedException expectedException = ExpectedException.none();

@InjectMocks
private RegistryCenterServiceImpl registryCenterService;

@Test
public void testDeleteZkClusterFail() throws Exception {
//delete fail
when(zkClusterMap.get("empty")).thenReturn(null);
try {
registryCenterService.deleteZkCluster("empty");
fail("should not be here");
} catch (SaturnJobConsoleException e) {
Assert.assertEquals("fail to delete.for ZkCluster does not exist", e.getMessage());
}

//delete fail
ZkCluster zkCluster = new ZkCluster();
ArrayList<RegistryCenterConfiguration> regCenterConfList = new ArrayList();
zkCluster.setRegCenterConfList(regCenterConfList);
regCenterConfList.add(new RegistryCenterConfiguration());
when(zkClusterMap.get("hasDomains")).thenReturn(zkCluster);
try {
registryCenterService.deleteZkCluster("hasDomains");
fail("should not be here");
} catch (SaturnJobConsoleException e) {
Assert.assertEquals("fail to delete.for ZkCluster still has domains", e.getMessage());
}

//delete fail
when(zkClusterMap.get("noDomainsInMemory")).thenReturn(new ZkCluster());
when(namespaceZkClusterMapping4SqlService.getAllNamespacesOfCluster("noDomainsInMemory")).thenReturn(Arrays.asList(""));
try {
registryCenterService.deleteZkCluster("noDomainsInMemory");
fail("should not be here");
} catch (SaturnJobConsoleException e) {
Assert.assertEquals("fail to delete.for ZkCluster still has domains", e.getMessage());
}

//delete success
when(namespaceZkClusterMapping4SqlService.getAllNamespacesOfCluster("noDomainsInMemory")).thenReturn(null);
registryCenterService.deleteZkCluster("noDomainsInMemory");
verify(zkClusterInfoService, times(1)).deleteZkCluster("noDomainsInMemory");
}

}
2 changes: 2 additions & 0 deletions saturn-console/src/main/resources/db/h2/data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ INSERT INTO `permission`(`permission_key`) VALUES('registryCenter:addNamespace')
INSERT INTO `permission`(`permission_key`) VALUES('registryCenter:batchMoveNamespaces');
INSERT INTO `permission`(`permission_key`) VALUES('registryCenter:exportNamespaces');
INSERT INTO `permission`(`permission_key`) VALUES('registryCenter:addZkCluster');
INSERT INTO `permission`(`permission_key`) VALUES('registryCenter:removeZkCluster');
INSERT INTO `permission`(`permission_key`) VALUES('systemConfig');
INSERT INTO `permission`(`permission_key`) VALUES('authorizationManage');

Expand Down Expand Up @@ -84,6 +85,7 @@ INSERT INTO `role_permission`(`role_key`, `permission_key`) VALUES('system_admin
INSERT INTO `role_permission`(`role_key`, `permission_key`) VALUES('system_admin', 'registryCenter:batchMoveNamespaces');
INSERT INTO `role_permission`(`role_key`, `permission_key`) VALUES('system_admin', 'registryCenter:exportNamespaces');
INSERT INTO `role_permission`(`role_key`, `permission_key`) VALUES('system_admin', 'registryCenter:addZkCluster');
INSERT INTO `role_permission`(`role_key`, `permission_key`) VALUES('system_admin', 'registryCenter:removeZkCluster');
INSERT INTO `role_permission`(`role_key`, `permission_key`) VALUES('system_admin', 'systemConfig');
INSERT INTO `role_permission`(`role_key`, `permission_key`) VALUES('system_admin', 'authorizationManage');

Expand Down
2 changes: 2 additions & 0 deletions saturn-console/src/main/resources/db/mysql/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ INSERT INTO `permission`(`permission_key`) VALUES('registryCenter:addNamespace')
INSERT INTO `permission`(`permission_key`) VALUES('registryCenter:batchMoveNamespaces');
INSERT INTO `permission`(`permission_key`) VALUES('registryCenter:exportNamespaces');
INSERT INTO `permission`(`permission_key`) VALUES('registryCenter:addZkCluster');
INSERT INTO `permission`(`permission_key`) VALUES('registryCenter:removeZkCluster');
INSERT INTO `permission`(`permission_key`) VALUES('systemConfig');
INSERT INTO `permission`(`permission_key`) VALUES('authorizationManage');

Expand Down Expand Up @@ -386,6 +387,7 @@ INSERT INTO `role_permission`(`role_key`, `permission_key`) VALUES('system_admin
INSERT INTO `role_permission`(`role_key`, `permission_key`) VALUES('system_admin', 'registryCenter:batchMoveNamespaces');
INSERT INTO `role_permission`(`role_key`, `permission_key`) VALUES('system_admin', 'registryCenter:exportNamespaces');
INSERT INTO `role_permission`(`role_key`, `permission_key`) VALUES('system_admin', 'registryCenter:addZkCluster');
INSERT INTO `role_permission`(`role_key`, `permission_key`) VALUES('system_admin', 'registryCenter:removeZkCluster');
INSERT INTO `role_permission`(`role_key`, `permission_key`) VALUES('system_admin', 'systemConfig');
INSERT INTO `role_permission`(`role_key`, `permission_key`) VALUES('system_admin', 'authorizationManage');

Expand Down

0 comments on commit 9c455bc

Please sign in to comment.