public void syncCatalogs()

in polaris-synchronizer/api/src/main/java/org/apache/polaris/tools/sync/polaris/PolarisSynchronizer.java [531:666]


  public void syncCatalogs() {
    List<Catalog> catalogsSource;

    try {
      catalogsSource = source.listCatalogs();
      clientLogger.info("Listed {} catalogs from source.", catalogsSource.size());
    } catch (Exception e) {
      if (haltOnFailure) throw e;
      clientLogger.error("Failed to list catalogs from source.", e);
      return;
    }

    List<Catalog> catalogsTarget;

    try {
      catalogsTarget = target.listCatalogs();
      clientLogger.info("Listed {} catalogs from target.", catalogsTarget.size());
    } catch (Exception e) {
      if (haltOnFailure) throw e;
      clientLogger.error("Failed to list catalogs from target.", e);
      return;
    }

    SynchronizationPlan<Catalog> catalogSyncPlan =
        syncPlanner.planCatalogSync(catalogsSource, catalogsTarget);

    catalogSyncPlan
        .entitiesToSkip()
        .forEach(catalog -> clientLogger.info("Skipping catalog {}.", catalog.getName()));

    catalogSyncPlan
        .entitiesToSkipAndSkipChildren()
        .forEach(
            catalog ->
                clientLogger.info(
                    "Skipping catalog {} and all child entities.", catalog.getName()));

    catalogSyncPlan
        .entitiesNotModified()
        .forEach(
            catalog ->
                clientLogger.info(
                    "No change detected in catalog {}. Skipping.", catalog.getName()));

    int syncsCompleted = 0;
    int totalSyncsToComplete = totalSyncsToComplete(catalogSyncPlan);

    for (Catalog catalog : catalogSyncPlan.entitiesToCreate()) {
      try {
        target.createCatalog(catalog);
        clientLogger.info(
            "Created catalog {}. - {}/{}",
            catalog.getName(),
            ++syncsCompleted,
            totalSyncsToComplete);
      } catch (Exception e) {
        if (haltOnFailure) throw e;
        clientLogger.error(
            "Failed to create catalog {}. - {}/{}",
            catalog.getName(),
            ++syncsCompleted,
            totalSyncsToComplete,
            e);
      }
    }

    for (Catalog catalog : catalogSyncPlan.entitiesToOverwrite()) {
      try {
        target.dropCatalogCascade(catalog.getName());
        target.createCatalog(catalog);
        clientLogger.info(
            "Overwrote catalog {}. - {}/{}",
            catalog.getName(),
            ++syncsCompleted,
            totalSyncsToComplete);
      } catch (Exception e) {
        if (haltOnFailure) throw e;
        clientLogger.error(
            "Failed to overwrite catalog {}. - {}/{}",
            catalog.getName(),
            ++syncsCompleted,
            totalSyncsToComplete,
            e);
      }
    }

    for (Catalog catalog : catalogSyncPlan.entitiesToRemove()) {
      try {
        target.dropCatalogCascade(catalog.getName());
        clientLogger.info(
            "Removed catalog {}. - {}/{}",
            catalog.getName(),
            ++syncsCompleted,
            totalSyncsToComplete);
      } catch (Exception e) {
        if (haltOnFailure) throw e;
        clientLogger.error(
            "Failed to remove catalog {}. - {}/{}",
            catalog.getName(),
            ++syncsCompleted,
            totalSyncsToComplete,
            e);
      }
    }

    for (Catalog catalog : catalogSyncPlan.entitiesToSyncChildren()) {

      try (IcebergCatalogService sourceIcebergCatalogService = source.initializeIcebergCatalogService(catalog.getName())) {
        clientLogger.info(
                "Initialized Iceberg REST catalog for Polaris catalog {} on source.",
                catalog.getName());

        try (IcebergCatalogService targetIcebergCatalogService = target.initializeIcebergCatalogService(catalog.getName())) {
          clientLogger.info(
                  "Initialized Iceberg REST catalog for Polaris catalog {} on target.",
                  catalog.getName());

          syncNamespaces(
                  catalog.getName(), Namespace.empty(), sourceIcebergCatalogService, targetIcebergCatalogService);
        }

      } catch (Exception e) {
        clientLogger.error(
                "Failed to synchronize Iceberg REST catalog for Polaris catalog {}.",
                catalog.getName(),
                e);
        if (haltOnFailure) throw new RuntimeException(e);
        continue;
      }

      // NOTE: Grants are synced on a per catalog role basis, so we need to ensure that catalog roles
      // are only synced AFTER Iceberg catalog entities, because they may depend on the Iceberg catalog
      // entities already existing
      syncCatalogRoles(catalog.getName());
    }
  }