apolloconfig / apollo

Apollo is a reliable configuration management system suitable for microservice configuration management scenarios.
https://www.apolloconfig.com
Apache License 2.0
28.94k stars 10.19k forks source link

操作同一个namespace情况下,如何避免界面操作与open API操作并发冲突导致覆盖配置的问题呢? #5058

Closed hezhaoye closed 5 months ago

hezhaoye commented 6 months ago

操作同一个namespace情况下,如何避免界面操作与open API操作并发冲突导致覆盖配置的问题呢?

dosubot[bot] commented 6 months ago

🤖

Hello @hezhaoye 👋,

I'm Dosu, a friendly bot here to assist you with your Apollo queries while we wait for a human maintainer to join us. I'm here to help answer your questions, solve bugs, and guide you on your journey to becoming a contributor. Looking forward to interacting with you!

在Apollo中,为了避免在同时通过界面和open API操作同一namespace时发生冲突和覆盖配置,Apollo使用了一种锁定机制。这是在NamespaceAcquireLockAspect类中实现的。

@Before注解用于指定在匹配方法执行之前运行的建议。在这种情况下,它在创建、更新或删除项目之前使用。PreAcquireNamespaceLock注解用于指示在操作之前应获取锁。

acquireLock方法用于获取特定namespace的锁。如果namespace锁定开关关闭,它会立即返回。如果namespace不存在,它会抛出BadRequestException。如果namespace锁不存在,它会尝试创建一个新的锁。如果由于DataIntegrityViolationException锁创建失败,它会再次检查锁。如果锁的所有者不是当前用户,它会抛出BadRequestException

以下是显示此内容的代码片段:

private void acquireLock(Namespace namespace, String currentUser) {
  if (namespace == null) {
    throw BadRequestException.namespaceNotExists();
  }

  long namespaceId = namespace.getId();

  NamespaceLock namespaceLock = namespaceLockService.findLock(namespaceId);
  if (namespaceLock == null) {
    try {
      tryLock(namespaceId, currentUser);
      //lock success
    } catch (DataIntegrityViolationException e) {
      //lock fail
      namespaceLock = namespaceLockService.findLock(namespaceId);
      checkLock(namespace, namespaceLock, currentUser);
    } catch (Exception e) {
      logger.error("try lock error", e);
      throw e;
    }
  } else {
    //check lock owner is current user
    checkLock(namespace, namespaceLock, currentUser);
  }
}

这种机制确保了一次只有一个用户可以修改namespace的配置,从而避免了冲突和覆盖。你可以在这个链接中查看更多关于这个机制的信息:NamespaceAcquireLockAspect.java

此外,你也可以参考以下已解决的类似问题:

  1. Portal侧如何防止多人并发修改配置项,相互覆盖的问题?
  2. open API支持并发操作吗?

希望这些信息对你有所帮助。如果你有更多的问题,或者需要进一步的解释,请随时告诉我。

Sources


This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

If you want to continue the conversation, start your reply with @dosu-bot.

stale[bot] commented 5 months ago

This issue has been automatically marked as stale because it has not had activity in the last 30 days. It will be closed in 7 days unless it is tagged "help wanted" or other activity occurs. Thank you for your contributions.

stale[bot] commented 5 months ago

This issue has been automatically closed because it has not had activity in the last 7 days. If this issue is still valid, please ping a maintainer and ask them to label it as "help wanted". Thank you for your contributions.