Skip to content

Commit

Permalink
1. remove global attribute currentUser, which may cause concurrent bug
Browse files Browse the repository at this point in the history
2. import app's source owner name
  • Loading branch information
lepdou authored and nobodyiam committed Feb 18, 2022
1 parent a9579f1 commit 0f06662
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 39 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,6 @@ Apollo 2.0.0
* [Fix bug: associated namespace display incorrect in text view](https://github.com/apolloconfig/apollo/pull/4219)
* [Add Ordered interface to ProviderManager SPI](https://github.com/apolloconfig/apollo/pull/4218)
* [Using commons-lang3 to replace commons-lang](https://github.com/apolloconfig/apollo/pull/4225)
* [optimize import/export config](https://github.com/apolloconfig/apollo/pull/4231)
------------------
All issues and pull requests are [here](https://github.com/ctripcorp/apollo/milestone/8?closed=1)
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,6 @@ public App importAppInLocal(App app) {
return app;
}

UserInfo owner = userService.findByUserId(app.getOwnerName());
if (owner == null) {
throw new BadRequestException("Application's owner not exist.");
}

app.setOwnerEmail(owner.getEmail());

App createdApp = appRepository.save(app);

roleInitializationService.initAppRoles(createdApp);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,6 @@ public class ConfigsImportService {
private final ApplicationEventPublisher publisher;
private final UserInfoHolder userInfoHolder;
private final RoleInitializationService roleInitializationService;
private String currentUser;


public ConfigsImportService(
final ItemService itemService,
Expand Down Expand Up @@ -106,9 +104,9 @@ public void forceImportNamespaceFromFile(final Env env, final String standardFil
throw new ServiceException("Read config file errors:{}", e);
}

currentUser = userInfoHolder.getUser().getUserId();
String operator = userInfoHolder.getUser().getUserId();

this.importNamespaceFromText(env, standardFilename, configText, false);
this.importNamespaceFromText(env, standardFilename, configText, false, operator);
}

/**
Expand Down Expand Up @@ -179,15 +177,15 @@ public void importDataFromZipFile(List<Env> importEnvs, ZipInputStream dataZip,
private void doImport(List<Env> importEnvs, List<String> toImportApps, List<String> toImportAppNSs,
List<String> toImportClusters, List<NamespaceImportData> toImportNSs)
throws InterruptedException {
currentUser = userInfoHolder.getUser().getUserId();

LOGGER.info("Start to import app. size = {}", toImportApps.size());

String operator = userInfoHolder.getUser().getUserId();

long startTime = System.currentTimeMillis();
CountDownLatch appLatch = new CountDownLatch(toImportApps.size());
toImportApps.parallelStream().forEach(app -> {
try {
importApp(app, importEnvs);
importApp(app, importEnvs, operator);
} catch (Exception e) {
LOGGER.error("import app error. app = {}", app, e);
} finally {
Expand All @@ -203,7 +201,7 @@ private void doImport(List<Env> importEnvs, List<String> toImportApps, List<Stri
CountDownLatch appNSLatch = new CountDownLatch(toImportAppNSs.size());
toImportAppNSs.parallelStream().forEach(appNS -> {
try {
importAppNamespace(appNS);
importAppNamespace(appNS, operator);
} catch (Exception e) {
LOGGER.error("import appnamespace error. appnamespace = {}", appNS, e);
} finally {
Expand All @@ -219,7 +217,7 @@ private void doImport(List<Env> importEnvs, List<String> toImportApps, List<Stri
CountDownLatch clusterLatch = new CountDownLatch(toImportClusters.size());
toImportClusters.parallelStream().forEach(cluster -> {
try {
importCluster(cluster, importEnvs);
importCluster(cluster, importEnvs, operator);
} catch (Exception e) {
LOGGER.error("import cluster error. cluster = {}", cluster, e);
} finally {
Expand All @@ -236,7 +234,7 @@ private void doImport(List<Env> importEnvs, List<String> toImportApps, List<Stri
toImportNSs.parallelStream().forEach(namespace -> {
try {
importNamespaceFromText(namespace.getEnv(), namespace.getFileName(), namespace.getContent(),
namespace.isIgnoreConflictNamespace());
namespace.isIgnoreConflictNamespace(), operator);
} catch (Exception e) {
LOGGER.error("import namespace error. namespace = {}", namespace, e);
} finally {
Expand All @@ -248,14 +246,12 @@ private void doImport(List<Env> importEnvs, List<String> toImportApps, List<Stri
LOGGER.info("Finish to import namespace. duration = {}", System.currentTimeMillis() - startTime);
}

private void importApp(String appInfo, List<Env> importEnvs) {
private void importApp(String appInfo, List<Env> importEnvs, String operator) {
App toImportApp = gson.fromJson(appInfo, App.class);
String appId = toImportApp.getAppId();

//imported app set owner to current user.
toImportApp.setOwnerName(currentUser);
toImportApp.setDataChangeCreatedBy(currentUser);
toImportApp.setDataChangeLastModifiedBy(currentUser);
toImportApp.setDataChangeCreatedBy(operator);
toImportApp.setDataChangeLastModifiedBy(operator);
toImportApp.setDataChangeCreatedTime(new Date());
toImportApp.setDataChangeLastModifiedTime(new Date());

Expand All @@ -274,7 +270,7 @@ private void importApp(String appInfo, List<Env> importEnvs) {
});
}

private void importAppNamespace(String appNamespace) {
private void importAppNamespace(String appNamespace, String operator) {
AppNamespace toImportPubAppNS = gson.fromJson(appNamespace, AppNamespace.class);

String appId = toImportPubAppNS.getAppId();
Expand All @@ -292,8 +288,8 @@ private void importAppNamespace(String appNamespace) {
managedAppNamespace.setPublic(isPublic);
managedAppNamespace.setFormat(toImportPubAppNS.getFormat());
managedAppNamespace.setComment(toImportPubAppNS.getComment());
managedAppNamespace.setDataChangeCreatedBy(currentUser);
managedAppNamespace.setDataChangeLastModifiedBy(currentUser);
managedAppNamespace.setDataChangeCreatedBy(operator);
managedAppNamespace.setDataChangeLastModifiedBy(operator);
managedAppNamespace.setName(namespaceName);

AppNamespace createdAppNamespace = appNamespaceService.importAppNamespaceInLocal(managedAppNamespace);
Expand All @@ -305,11 +301,11 @@ private void importAppNamespace(String appNamespace) {
}
}

private void importCluster(String clusterInfo, List<Env> importEnvs) {
private void importCluster(String clusterInfo, List<Env> importEnvs, String operator) {
ClusterDTO toImportCluster = gson.fromJson(clusterInfo, ClusterDTO.class);

toImportCluster.setDataChangeCreatedBy(currentUser);
toImportCluster.setDataChangeLastModifiedBy(currentUser);
toImportCluster.setDataChangeCreatedBy(operator);
toImportCluster.setDataChangeLastModifiedBy(operator);
toImportCluster.setDataChangeCreatedTime(new Date());
toImportCluster.setDataChangeLastModifiedTime(new Date());

Expand Down Expand Up @@ -339,19 +335,19 @@ private void importCluster(String clusterInfo, List<Env> importEnvs) {
* @param configText config content
*/
private void importNamespaceFromText(final Env env, final String standardFilename, final String configText,
boolean ignoreConflictNamespace) {
boolean ignoreConflictNamespace, String operator) {
final String appId = ConfigFileUtils.getAppId(standardFilename);
final String clusterName = ConfigFileUtils.getClusterName(standardFilename);
final String namespace = ConfigFileUtils.getNamespace(standardFilename);
final String format = ConfigFileUtils.getFormat(standardFilename);

this.importNamespace(appId, env, clusterName, namespace, configText, format, ignoreConflictNamespace);
this.importNamespace(appId, env, clusterName, namespace, configText, format, ignoreConflictNamespace, operator);
}

private void importNamespace(final String appId, final Env env,
final String clusterName, final String namespaceName,
final String configText, final String format,
boolean ignoreConflictNamespace) {
boolean ignoreConflictNamespace, String operator) {
NamespaceDTO namespaceDTO;
try {
namespaceDTO = namespaceService.loadNamespaceBaseInfo(appId, env, clusterName, namespaceName);
Expand All @@ -365,12 +361,12 @@ private void importNamespace(final String appId, final Env env,
namespaceDTO.setAppId(appId);
namespaceDTO.setClusterName(clusterName);
namespaceDTO.setNamespaceName(namespaceName);
namespaceDTO.setDataChangeCreatedBy(currentUser);
namespaceDTO.setDataChangeLastModifiedBy(currentUser);
namespaceDTO.setDataChangeCreatedBy(operator);
namespaceDTO.setDataChangeLastModifiedBy(operator);
namespaceDTO = namespaceService.createNamespace(env, namespaceDTO);

roleInitializationService.initNamespaceRoles(appId, namespaceName, currentUser);
roleInitializationService.initNamespaceEnvRoles(appId, namespaceName, currentUser);
roleInitializationService.initNamespaceRoles(appId, namespaceName, operator);
roleInitializationService.initNamespaceEnvRoles(appId, namespaceName, operator);
}

List<ItemDTO> itemDTOS = itemService.findItems(appId, env, clusterName, namespaceName);
Expand All @@ -379,18 +375,18 @@ private void importNamespace(final String appId, final Env env,
return;
}

importItems(appId, env, clusterName, namespaceName, configText, namespaceDTO);
importItems(appId, env, clusterName, namespaceName, configText, namespaceDTO, operator);
}

private void importItems(String appId, Env env, String clusterName, String namespaceName, String configText,
NamespaceDTO namespaceDTO) {
NamespaceDTO namespaceDTO, String operator) {
List<ItemDTO> toImportItems = gson.fromJson(configText, GsonType.ITEM_DTOS);

toImportItems.parallelStream().forEach(newItem -> {
String key = newItem.getKey();
newItem.setNamespaceId(namespaceDTO.getId());
newItem.setDataChangeCreatedBy(currentUser);
newItem.setDataChangeLastModifiedBy(currentUser);
newItem.setDataChangeCreatedBy(operator);
newItem.setDataChangeLastModifiedBy(operator);
newItem.setDataChangeCreatedTime(new Date());
newItem.setDataChangeLastModifiedTime(new Date());

Expand Down

0 comments on commit 0f06662

Please sign in to comment.