All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hubspot.singularity.data.zkmigrations.ZkDataMigrationRunner Maven / Gradle / Ivy

package com.hubspot.singularity.data.zkmigrations;

import com.google.inject.Inject;
import com.hubspot.mesos.JavaUtils;
import com.hubspot.singularity.data.MetadataManager;
import java.util.List;
import java.util.Optional;
import javax.inject.Singleton;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Singleton
public class ZkDataMigrationRunner {
  private static final Logger LOG = LoggerFactory.getLogger(ZkDataMigrationRunner.class);

  private final MetadataManager metadataManager;
  private final List migrations;

  @Inject
  public ZkDataMigrationRunner(
    MetadataManager metadataManager,
    List migrations
  ) {
    this.metadataManager = metadataManager;
    this.migrations = migrations;
  }

  public int checkMigrations() {
    final long start = System.currentTimeMillis();
    final Optional currentVersion = metadataManager.getZkDataVersion();
    final int intVersionNumber = Integer.parseInt(currentVersion.orElse("0"));

    LOG.info(
      "Current ZK data version is {}, known migrations: {}",
      intVersionNumber,
      migrations
    );

    int numMigrationsApplied = 0;
    int lastAppliedMigration = intVersionNumber;

    for (ZkDataMigration migration : migrations) {
      if (migration.getMigrationNumber() > intVersionNumber) {
        numMigrationsApplied++;

        final long migrationStart = System.currentTimeMillis();

        LOG.info("Applying {}", migration);

        migration.applyMigration();

        LOG.info("Applied {} in {}", migration, JavaUtils.duration(migrationStart));

        lastAppliedMigration = migration.getMigrationNumber();
      }
    }

    if (lastAppliedMigration > intVersionNumber) {
      LOG.info("Setting new version to {}", lastAppliedMigration);
      metadataManager.setZkDataVersion(Integer.toString(lastAppliedMigration));
    }

    LOG.info(
      "Applied {} migrations in {}",
      numMigrationsApplied,
      JavaUtils.duration(start)
    );

    return numMigrationsApplied;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy