apolloconfig/apollo · error · BadRequestException

cluster not unique

Error message

cluster not unique

What it means

A BadRequestException (HTTP 400) thrown from ClusterService.saveWithoutInstanceOfAppNamespaces() when a cluster with the same name already exists for the given appId. The uniqueness check isClusterNameUnique() queries clusterRepository.findByAppIdAndName(). This is the user-facing cluster creation path; a duplicate cluster name within the same app is rejected as a client error (400).

Source

Thrown at apollo-biz/src/main/java/com/ctrip/framework/apollo/biz/service/ClusterService.java:93

    return clusters;
  }

  @Transactional
  public Cluster saveWithInstanceOfAppNamespaces(Cluster entity) {

    Cluster savedCluster = saveWithoutInstanceOfAppNamespaces(entity);

    namespaceService.instanceOfAppNamespaces(savedCluster.getAppId(), savedCluster.getName(),
        savedCluster.getDataChangeCreatedBy());

    return savedCluster;
  }

  @Transactional
  public Cluster saveWithoutInstanceOfAppNamespaces(Cluster entity) {
    if (!isClusterNameUnique(entity.getAppId(), entity.getName())) {
      throw new BadRequestException("cluster not unique");
    }
    entity.setId(0);// protection
    Cluster cluster = clusterRepository.save(entity);

    auditService.audit(Cluster.class.getSimpleName(), cluster.getId(), Audit.OP.INSERT,
        cluster.getDataChangeCreatedBy());

    return cluster;
  }

  @Transactional
  public void delete(long id, String operator) {
    Cluster cluster = clusterRepository.findById(id).orElse(null);
    if (cluster == null) {
      throw BadRequestException.clusterNotExists("");
    }

    // delete linked namespaces

View on GitHub (pinned to d95fc18d11)

Solutions

  1. Check isClusterNameUnique(appId, clusterName) before calling save.
  2. If the cluster already exists, retrieve and use it.
  3. Ensure cluster-creation requests are not retried without checking existence.

Example fix

// before: unconditional creation
clusterService.saveWithInstanceOfAppNamespaces(newCluster);

// after: guard with uniqueness check
if (clusterService.isClusterNameUnique(appId, clusterName)) {
    clusterService.saveWithInstanceOfAppNamespaces(newCluster);
} else {
    return clusterService.findOne(appId, clusterName);
}
Defensive patterns

Strategy: validation

Validate before calling

// Check cluster name uniqueness before creating
if (clusterService.isClusterNameUnique(appId, clusterName)) {
    clusterService.saveWithInstanceOfAppNamespaces(newCluster);
} else {
    return clusterService.findOne(appId, clusterName);
}

Prevention

When it happens

Trigger: POST to create a cluster (ClusterService.saveWithInstanceOfAppNamespaces or saveWithoutInstanceOfAppNamespaces) with a name that already exists for the same appId. For example, creating cluster 'DC2' when 'DC2' already exists for appId 'myApp'.

Common situations: Retried cluster-creation request after a timeout; two operators creating the same cluster concurrently; a migration script re-run; attempting to recreate a cluster that was soft-deleted but whose uniqueness-check row still exists.

Related errors


AI-assisted analysis of apolloconfig/apollo@d95fc18d11 (2026-08-14). Data as JSON: /api/errors/33a201675a394617. Report an issue: GitHub.