Skip to content

Commit

Permalink
feat(11115): http support cas publish. (#11120)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bo-Qiu authored Oct 11, 2023
1 parent d85e3f7 commit 0e43a93
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ public Boolean publishConfig(HttpServletRequest request, HttpServletResponse res
configRequestInfo.setSrcIp(RequestUtil.getRemoteIp(request));
configRequestInfo.setRequestIpApp(RequestUtil.getAppName(request));
configRequestInfo.setBetaIps(request.getHeader("betaIps"));
configRequestInfo.setCasMd5(request.getHeader("casMd5"));

return configOperationService.publishConfig(configForm, configRequestInfo, encryptedDataKeyFinal);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ public Result<Boolean> publishConfig(ConfigForm configForm, HttpServletRequest r
configRequestInfo.setSrcIp(RequestUtil.getRemoteIp(request));
configRequestInfo.setRequestIpApp(RequestUtil.getAppName(request));
configRequestInfo.setBetaIps(request.getHeader("betaIps"));
configRequestInfo.setCasMd5(request.getHeader("casMd5"));

String encryptedDataKey = pair.getFirst();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,13 @@ public class ConfigRequestInfo implements Serializable {

private String betaIps;

public ConfigRequestInfo(String srcIp, String requestIpApp, String betaIps) {
private String casMd5;

public ConfigRequestInfo(String srcIp, String requestIpApp, String betaIps, String casMd5) {
this.srcIp = srcIp;
this.requestIpApp = requestIpApp;
this.betaIps = betaIps;
this.casMd5 = casMd5;
}

public ConfigRequestInfo() {
Expand Down Expand Up @@ -67,6 +70,14 @@ public void setBetaIps(String betaIps) {
this.betaIps = betaIps;
}

public String getCasMd5() {
return casMd5;
}

public void setCasMd5(String casMd5) {
this.casMd5 = casMd5;
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand All @@ -76,18 +87,18 @@ public boolean equals(Object o) {
return false;
}
ConfigRequestInfo that = (ConfigRequestInfo) o;
return Objects.equals(srcIp, that.srcIp) && Objects.equals(requestIpApp, that.requestIpApp) && Objects
.equals(betaIps, that.betaIps);
return Objects.equals(srcIp, that.srcIp) && Objects.equals(requestIpApp, that.requestIpApp) && Objects.equals(
betaIps, that.betaIps) && Objects.equals(casMd5, that.casMd5);
}

@Override
public int hashCode() {
return Objects.hash(srcIp, requestIpApp, betaIps);
return Objects.hash(srcIp, requestIpApp, betaIps, casMd5);
}

@Override
public String toString() {
return "ConfigRequestInfoVo{" + "srcIp='" + srcIp + '\'' + ", requestIpApp='" + requestIpApp + '\''
+ ", betaIps='" + betaIps + '\'' + '}';
+ ", betaIps='" + betaIps + '\'' + ", casMd5='" + casMd5 + '\'' + '}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
import com.alibaba.nacos.common.utils.StringUtils;
import com.alibaba.nacos.config.server.model.ConfigInfo;
import com.alibaba.nacos.config.server.model.ConfigOperateResult;
import com.alibaba.nacos.config.server.model.event.ConfigDataChangeEvent;
import com.alibaba.nacos.config.server.model.ConfigRequestInfo;
import com.alibaba.nacos.config.server.model.event.ConfigDataChangeEvent;
import com.alibaba.nacos.config.server.model.form.ConfigForm;
import com.alibaba.nacos.config.server.service.repository.ConfigInfoBetaPersistService;
import com.alibaba.nacos.config.server.service.repository.ConfigInfoPersistService;
Expand Down Expand Up @@ -91,40 +91,75 @@ public Boolean publishConfig(ConfigForm configForm, ConfigRequestInfo configRequ

configInfo.setType(configForm.getType());
configInfo.setEncryptedDataKey(encryptedDataKey);
ConfigOperateResult configOperateResult = null;
ConfigOperateResult configOperateResult;

String persistEvent = ConfigTraceService.PERSISTENCE_EVENT;

if (StringUtils.isBlank(configRequestInfo.getBetaIps())) {
if (StringUtils.isBlank(configForm.getTag())) {
configOperateResult = configInfoPersistService.insertOrUpdate(configRequestInfo.getSrcIp(),
configForm.getSrcUser(), configInfo, configAdvanceInfo);
if (StringUtils.isNotBlank(configRequestInfo.getCasMd5())) {
configOperateResult = configInfoPersistService.insertOrUpdateCas(configRequestInfo.getSrcIp(),
configForm.getSrcUser(), configInfo, configAdvanceInfo);
if (!configOperateResult.isSuccess()) {
LOGGER.warn(
"[cas-publish-config-fail] srcIp = {}, dataId= {}, casMd5 = {}, msg = server md5 may have changed.",
configRequestInfo.getSrcIp(), configForm.getDataId(), configRequestInfo.getCasMd5());
throw new NacosApiException(HttpStatus.INTERNAL_SERVER_ERROR.value(),
ErrorCode.RESOURCE_CONFLICT, "Cas publish fail, server md5 may have changed.");
}
} else {
configOperateResult = configInfoPersistService.insertOrUpdate(configRequestInfo.getSrcIp(),
configForm.getSrcUser(), configInfo, configAdvanceInfo);
}
ConfigChangePublisher.notifyConfigChange(
new ConfigDataChangeEvent(false, configForm.getDataId(), configForm.getGroup(),
configForm.getNamespaceId(), configOperateResult.getLastModified()));
} else {
if (StringUtils.isNotBlank(configRequestInfo.getCasMd5())) {
configOperateResult = configInfoTagPersistService.insertOrUpdateTagCas(configInfo,
configForm.getTag(), configRequestInfo.getSrcIp(), configForm.getSrcUser());
if (!configOperateResult.isSuccess()) {
LOGGER.warn(
"[cas-publish-tag-config-fail] srcIp = {}, dataId= {}, casMd5 = {}, msg = server md5 may have changed.",
configRequestInfo.getSrcIp(), configForm.getDataId(), configRequestInfo.getCasMd5());
throw new NacosApiException(HttpStatus.INTERNAL_SERVER_ERROR.value(),
ErrorCode.RESOURCE_CONFLICT,
"Cas publish tag config fail, server md5 may have changed.");
}
} else {
configOperateResult = configInfoTagPersistService.insertOrUpdateTag(configInfo, configForm.getTag(),
configRequestInfo.getSrcIp(), configForm.getSrcUser());
}
persistEvent = ConfigTraceService.PERSISTENCE_EVENT_TAG + "-" + configForm.getTag();
configOperateResult = configInfoTagPersistService.insertOrUpdateTag(configInfo, configForm.getTag(),
configRequestInfo.getSrcIp(), configForm.getSrcUser());
ConfigChangePublisher.notifyConfigChange(
new ConfigDataChangeEvent(false, configForm.getDataId(), configForm.getGroup(),
configForm.getNamespaceId(), configForm.getTag(),
configOperateResult.getLastModified()));
}
} else {
persistEvent = ConfigTraceService.PERSISTENCE_EVENT_BETA;

// beta publish
configOperateResult = configInfoBetaPersistService.insertOrUpdateBeta(configInfo,
configRequestInfo.getBetaIps(), configRequestInfo.getSrcIp(), configForm.getSrcUser());
if (StringUtils.isNotBlank(configRequestInfo.getCasMd5())) {
configOperateResult = configInfoBetaPersistService.insertOrUpdateBetaCas(configInfo,
configRequestInfo.getBetaIps(), configRequestInfo.getSrcIp(), configForm.getSrcUser());
if (!configOperateResult.isSuccess()) {
LOGGER.warn(
"[cas-publish-beta-config-fail] srcIp = {}, dataId= {}, casMd5 = {}, msg = server md5 may have changed.",
configRequestInfo.getSrcIp(), configForm.getDataId(), configRequestInfo.getCasMd5());
throw new NacosApiException(HttpStatus.INTERNAL_SERVER_ERROR.value(), ErrorCode.RESOURCE_CONFLICT,
"Cas publish beta config fail, server md5 may have changed.");
}
} else {
configOperateResult = configInfoBetaPersistService.insertOrUpdateBeta(configInfo,
configRequestInfo.getBetaIps(), configRequestInfo.getSrcIp(), configForm.getSrcUser());
}
persistEvent = ConfigTraceService.PERSISTENCE_EVENT_BETA;
ConfigChangePublisher.notifyConfigChange(
new ConfigDataChangeEvent(true, configForm.getDataId(), configForm.getGroup(),
configForm.getNamespaceId(), configOperateResult.getLastModified()));
}
ConfigTraceService.logPersistenceEvent(configForm.getDataId(), configForm.getGroup(),
configForm.getNamespaceId(), configRequestInfo.getRequestIpApp(), configOperateResult.getLastModified(),
InetUtils.getSelfIP(), persistEvent, ConfigTraceService.PERSISTENCE_TYPE_PUB, configForm.getContent());

return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.config.server.model.ConfigInfo;
import com.alibaba.nacos.config.server.model.ConfigOperateResult;
import com.alibaba.nacos.config.server.model.ConfigRequestInfo;
import com.alibaba.nacos.config.server.model.form.ConfigForm;
import com.alibaba.nacos.config.server.service.repository.ConfigInfoBetaPersistService;
Expand All @@ -35,6 +36,7 @@
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

/**
* ConfigServiceTest.
Expand Down Expand Up @@ -73,25 +75,59 @@ public void testPublishConfig() throws NacosException {

ConfigRequestInfo configRequestInfo = new ConfigRequestInfo();

// if betaIps is blank and tag is blank
// if betaIps is blank, tag is blank and casMd5 is blank
when(configInfoPersistService.insertOrUpdate(any(), any(), any(ConfigInfo.class), any())).thenReturn(
new ConfigOperateResult());
Boolean aResult = configOperationService.publishConfig(configForm, configRequestInfo, "");
verify(configInfoPersistService)
.insertOrUpdate(any(), any(), any(ConfigInfo.class), any());
verify(configInfoPersistService).insertOrUpdate(any(), any(), any(ConfigInfo.class), any());
Assert.assertEquals(true, aResult);

// if betaIps is blank and tag is not blank
configForm.setTag("test tag");
// if betaIps is blank, tag is blank and casMd5 is not blank
configRequestInfo.setCasMd5("test casMd5");
when(configInfoPersistService.insertOrUpdateCas(any(), any(), any(ConfigInfo.class), any())).thenReturn(
new ConfigOperateResult());
Boolean bResult = configOperationService.publishConfig(configForm, configRequestInfo, "");
verify(configInfoTagPersistService)
.insertOrUpdateTag(any(ConfigInfo.class), eq("test tag"), any(), any());
verify(configInfoPersistService).insertOrUpdateCas(any(), any(), any(ConfigInfo.class), any());
Assert.assertEquals(true, bResult);
configRequestInfo.setCasMd5("");

// if betaIps is not blank
configRequestInfo.setBetaIps("test-betaIps");
// if betaIps is blank, tag is not blank and casMd5 is blank
configForm.setTag("test tag");
when(configInfoTagPersistService.insertOrUpdateTag(any(ConfigInfo.class), eq("test tag"), any(),
any())).thenReturn(new ConfigOperateResult());
Boolean cResult = configOperationService.publishConfig(configForm, configRequestInfo, "");
verify(configInfoBetaPersistService)
.insertOrUpdateBeta(any(ConfigInfo.class), eq("test-betaIps"), any(), any());
verify(configInfoTagPersistService).insertOrUpdateTag(any(ConfigInfo.class), eq("test tag"), any(), any());
Assert.assertEquals(true, cResult);

// if betaIps is blank, tag is not blank and casMd5 is not blank
configForm.setTag("test tag");
configRequestInfo.setCasMd5("test casMd5");
when(configInfoTagPersistService.insertOrUpdateTagCas(any(ConfigInfo.class), eq("test tag"), any(),
any())).thenReturn(new ConfigOperateResult());
Boolean dResult = configOperationService.publishConfig(configForm, configRequestInfo, "");
verify(configInfoTagPersistService).insertOrUpdateTagCas(any(ConfigInfo.class), eq("test tag"), any(), any());
Assert.assertEquals(true, dResult);
configRequestInfo.setCasMd5("");
configForm.setTag("");

// if betaIps is not blank and casMd5 is blank
configRequestInfo.setBetaIps("test-betaIps");
when(configInfoBetaPersistService.insertOrUpdateBeta(any(ConfigInfo.class), eq("test-betaIps"), any(),
any())).thenReturn(new ConfigOperateResult());
Boolean eResult = configOperationService.publishConfig(configForm, configRequestInfo, "");
verify(configInfoBetaPersistService).insertOrUpdateBeta(any(ConfigInfo.class), eq("test-betaIps"), any(),
any());
Assert.assertEquals(true, eResult);

// if betaIps is not blank and casMd5 is not blank
configRequestInfo.setBetaIps("test-betaIps");
configRequestInfo.setCasMd5("test casMd5");
when(configInfoBetaPersistService.insertOrUpdateBetaCas(any(ConfigInfo.class), eq("test-betaIps"), any(),
any())).thenReturn(new ConfigOperateResult());
Boolean fResult = configOperationService.publishConfig(configForm, configRequestInfo, "");
verify(configInfoBetaPersistService).insertOrUpdateBetaCas(any(ConfigInfo.class), eq("test-betaIps"), any(),
any());
Assert.assertEquals(true, fResult);
}

@Test
Expand Down

0 comments on commit 0e43a93

Please sign in to comment.