Search in sources :

Example 1 with MigrationCallback

use of com.zimbra.cs.ephemeral.migrate.AttributeMigration.MigrationCallback in project zm-mailbox by Zimbra.

the class MigrateAttributesTest method testFallbackEphemeralStoreWhenMigrating.

@Test
public void testFallbackEphemeralStoreWhenMigrating() throws Exception {
    EphemeralStore destination = EphemeralStore.getFactory().getStore();
    EntrySource source = new DummyEntrySource(acct);
    Multimap<String, Object> deletedAttrs = LinkedListMultimap.create();
    List<String> attrsToMigrate = Arrays.asList(new String[] { Provisioning.A_zimbraAuthTokens, Provisioning.A_zimbraCsrfTokenData, Provisioning.A_zimbraLastLogonTimestamp });
    //DummyMigrationCallback will store attributes in InMemoryEphemeralStore, and track deletions in deletedAttrs map
    MigrationCallback callback = new DummyMigrationCallback(destination, deletedAttrs);
    AttributeMigration migration = new AttributeMigration(attrsToMigrate, source, callback, null);
    migration.beginMigration();
    //set to in-memory backend because fallback won't be enabled with default LDAP backend
    EphemeralStore.setFactory(InMemoryEphemeralStore.Factory.class);
    Factory factory = EphemeralStore.getFactory();
    EphemeralStore store = factory.getStore();
    //in-memory backend will be wrapped in a FallbackEphemeralStore, with LDAP as the fallback
    assertTrue(store instanceof FallbackEphemeralStore);
    FallbackEphemeralStore fallbackStore = (FallbackEphemeralStore) store;
    assertTrue(fallbackStore.getPrimaryStore() instanceof InMemoryEphemeralStore);
    assertTrue(fallbackStore.getSecondaryStore() instanceof LdapEphemeralStore);
    migration.endMigration();
    EphemeralStore.setFactory(InMemoryEphemeralStore.Factory.class);
    //when migration is finished, fallback won't be enabled anymore
    factory = EphemeralStore.getFactory();
    store = factory.getStore();
    assertTrue(store instanceof InMemoryEphemeralStore);
}
Also used : InMemoryEphemeralStore(com.zimbra.cs.ephemeral.InMemoryEphemeralStore) LdapEphemeralStore(com.zimbra.cs.ephemeral.LdapEphemeralStore) Factory(com.zimbra.cs.ephemeral.EphemeralStore.Factory) MigrationCallback(com.zimbra.cs.ephemeral.migrate.AttributeMigration.MigrationCallback) EntrySource(com.zimbra.cs.ephemeral.migrate.AttributeMigration.EntrySource) FallbackEphemeralStore(com.zimbra.cs.ephemeral.FallbackEphemeralStore) FallbackEphemeralStore(com.zimbra.cs.ephemeral.FallbackEphemeralStore) LdapEphemeralStore(com.zimbra.cs.ephemeral.LdapEphemeralStore) EphemeralStore(com.zimbra.cs.ephemeral.EphemeralStore) InMemoryEphemeralStore(com.zimbra.cs.ephemeral.InMemoryEphemeralStore) Test(org.junit.Test)

Example 2 with MigrationCallback

use of com.zimbra.cs.ephemeral.migrate.AttributeMigration.MigrationCallback in project zm-mailbox by Zimbra.

the class MigrateAttributesTest method testMigrationInfo.

@Test
public void testMigrationInfo() throws Exception {
    MigrationInfo info = MigrationInfo.getFactory().getInfo();
    try {
        EphemeralStore destination = EphemeralStore.getFactory().getStore();
        EntrySource source = new DummyEntrySource(acct);
        List<String> attrsToMigrate = new ArrayList<String>();
        MigrationCallback callback = new DummyMigrationCallback(destination);
        AttributeMigration.setCallback(callback);
        AttributeMigration migration = new AttributeMigration("testAttributeMigration", attrsToMigrate, source, null);
        assertEquals(Status.NONE, info.getStatus());
        migration.beginMigration();
        assertEquals(Status.IN_PROGRESS, info.getStatus());
        migration.endMigration();
        assertEquals(Status.COMPLETED, info.getStatus());
    } finally {
        info.clearData();
    }
}
Also used : ArrayList(java.util.ArrayList) MigrationCallback(com.zimbra.cs.ephemeral.migrate.AttributeMigration.MigrationCallback) EphemeralStore(com.zimbra.cs.ephemeral.EphemeralStore) EntrySource(com.zimbra.cs.ephemeral.migrate.AttributeMigration.EntrySource) Test(org.junit.Test)

Example 3 with MigrationCallback

use of com.zimbra.cs.ephemeral.migrate.AttributeMigration.MigrationCallback in project zm-mailbox by Zimbra.

the class MigrateAttributesTest method testAttributeMigration.

/*
     * Test end-to-end AttributeMigration
     */
@Test
public void testAttributeMigration() throws Exception {
    EphemeralStore destination = EphemeralStore.getFactory().getStore();
    EntrySource source = new DummyEntrySource(acct);
    List<String> attrsToMigrate = Arrays.asList(new String[] { Provisioning.A_zimbraAuthTokens, Provisioning.A_zimbraCsrfTokenData, Provisioning.A_zimbraLastLogonTimestamp });
    // DummyMigrationCallback will store attributes in InMemoryEphemeralStore, and track deletions in deletedAttrs map
    MigrationCallback callback = new DummyMigrationCallback(destination);
    AttributeMigration.setCallback(callback);
    AttributeMigration migration = new AttributeMigration("testAttributeMigration", attrsToMigrate, source, null);
    MigrationInfo info = MigrationInfo.getFactory().getInfo();
    // disable running in separate thread
    // run migration
    migration.migrateAllAccounts();
    EphemeralLocation location = new LdapEntryLocation(acct);
    EphemeralResult result = destination.get(new EphemeralKey(Provisioning.A_zimbraAuthTokens, "1234"), location);
    assertEquals("server_1", result.getValue());
    result = destination.get(new EphemeralKey(Provisioning.A_zimbraAuthTokens, "5678"), location);
    assertEquals("server_2", result.getValue());
    result = destination.get(new EphemeralKey(Provisioning.A_zimbraCsrfTokenData, "crumb1"), location);
    assertEquals("data1", result.getValue());
    result = destination.get(new EphemeralKey(Provisioning.A_zimbraCsrfTokenData, "crumb2"), location);
    assertEquals("data2", result.getValue());
    result = destination.get(new EphemeralKey(Provisioning.A_zimbraLastLogonTimestamp), location);
    assertEquals("currentdate", result.getValue());
    assertTrue(info.getStatus() == Status.COMPLETED);
}
Also used : EphemeralLocation(com.zimbra.cs.ephemeral.EphemeralLocation) MigrationCallback(com.zimbra.cs.ephemeral.migrate.AttributeMigration.MigrationCallback) EphemeralKey(com.zimbra.cs.ephemeral.EphemeralKey) EntrySource(com.zimbra.cs.ephemeral.migrate.AttributeMigration.EntrySource) EphemeralResult(com.zimbra.cs.ephemeral.EphemeralResult) LdapEntryLocation(com.zimbra.cs.ephemeral.LdapEntryLocation) EphemeralStore(com.zimbra.cs.ephemeral.EphemeralStore) Test(org.junit.Test)

Example 4 with MigrationCallback

use of com.zimbra.cs.ephemeral.migrate.AttributeMigration.MigrationCallback in project zm-mailbox by Zimbra.

the class AttributeMigrationUtil method main.

public static void main(String[] args) throws Exception {
    CliUtil.toolSetup();
    CommandLineParser parser = new GnuParser();
    CommandLine cl = parser.parse(OPTIONS, args);
    boolean dryRun = cl.hasOption('r');
    boolean useNumThreads = cl.hasOption('n');
    boolean debug = cl.hasOption('d');
    boolean help = cl.hasOption('h');
    boolean useAccount = cl.hasOption('a');
    boolean showStatus = cl.hasOption('s');
    boolean clear = cl.hasOption('c');
    List<String> clArgs = cl.getArgList();
    if (clArgs.isEmpty() && !help && !clear && !showStatus) {
        ZimbraLog.ephemeral.error("must specify URL of destination ephemeral store");
        return;
    }
    if (help || (clear && showStatus)) {
        usage();
        return;
    }
    if (debug) {
        ZimbraLog.ephemeral.setLevel(Level.debug);
    }
    if (dryRun && useNumThreads) {
        ZimbraLog.ephemeral.error("cannot specify --num-threads with --dry-run option");
        return;
    }
    if (clear && (dryRun || useNumThreads || useAccount || showStatus || !clArgs.isEmpty())) {
        ZimbraLog.ephemeral.error("cannot specify --clear with arguments or other options");
        return;
    }
    if (showStatus && (dryRun || useNumThreads || useAccount || clear || !clArgs.isEmpty())) {
        ZimbraLog.ephemeral.error("cannot specify --status with arguments or other options");
        return;
    }
    // a null numThreads value causes the migration process to run synchronously
    Integer numThreads = null;
    if (!dryRun) {
        // dry runs are always synchronous
        try {
            numThreads = Integer.valueOf(cl.getOptionValue('n', "1"));
            if (numThreads < 1) {
                ZimbraLog.ephemeral.error("invalid num-threads value: '%s'", numThreads);
                return;
            }
        } catch (NumberFormatException e) {
            ZimbraLog.ephemeral.error("invalid num-threads value: '%s'", cl.getOptionValue('n'));
            return;
        }
    }
    if (showStatus) {
        showMigrationInfo();
        return;
    } else if (clear) {
        clearMigrationInfo();
        return;
    }
    String destURL = clArgs.get(0);
    List<String> attrsToMigrate;
    MigrationCallback callback;
    if (clArgs.size() > 1) {
        attrsToMigrate = clArgs.subList(1, clArgs.size());
    } else {
        attrsToMigrate = new ArrayList<String>(AttributeManager.getInstance().getEphemeralAttributeNames());
    }
    if (!dryRun) {
        String backendName = null;
        String[] tokens = destURL.split(":");
        if (tokens != null && tokens.length > 0) {
            backendName = tokens[0];
            if (backendName.equalsIgnoreCase("ldap")) {
                ZimbraLog.ephemeral.info("migrating to LDAP is not supported");
                return;
            }
        }
        if (Provisioning.getInstance().getConfig().getEphemeralBackendURL().equalsIgnoreCase(destURL)) {
            ZimbraLog.ephemeral.info("destination URL cannot be the same as the current ephemeral backend URL");
            return;
        }
        ExtensionUtil.initEphemeralBackendExtension(backendName);
        try {
            callback = new ZimbraMigrationCallback(destURL);
        } catch (ServiceException e) {
            Zimbra.halt(String.format("unable to connect to ephemeral backend at %s; migration cannot proceed", destURL), e);
            return;
        }
    } else {
        callback = new DryRunMigrationCallback();
        MigrationInfo.setFactory(InMemoryMigrationInfo.Factory.class);
    }
    try {
        AttributeMigration migration = new AttributeMigration(destURL, attrsToMigrate, numThreads);
        AttributeMigration.setCallback(callback);
        EntrySource source;
        if (useAccount) {
            String[] acctValues = cl.getOptionValue('a').split(",");
            source = new SomeAccountsSource(acctValues);
        } else {
            source = new AllAccountsSource();
        }
        migration.setSource(source);
        migration.migrateAllAccounts();
    } catch (InvalidAttributeException e) {
        Zimbra.halt(String.format("Migration can't proceed: %s", e.getMessage()));
        return;
    } catch (ServiceException e) {
        Zimbra.halt(String.format("error encountered during migration to ephemeral backend at %s; migration cannot proceed", destURL), e);
        return;
    }
}
Also used : ZimbraMigrationCallback(com.zimbra.cs.ephemeral.migrate.AttributeMigration.ZimbraMigrationCallback) GnuParser(org.apache.commons.cli.GnuParser) MigrationCallback(com.zimbra.cs.ephemeral.migrate.AttributeMigration.MigrationCallback) DryRunMigrationCallback(com.zimbra.cs.ephemeral.migrate.AttributeMigration.DryRunMigrationCallback) ZimbraMigrationCallback(com.zimbra.cs.ephemeral.migrate.AttributeMigration.ZimbraMigrationCallback) EntrySource(com.zimbra.cs.ephemeral.migrate.AttributeMigration.EntrySource) SomeAccountsSource(com.zimbra.cs.ephemeral.migrate.AttributeMigration.SomeAccountsSource) CommandLine(org.apache.commons.cli.CommandLine) ServiceException(com.zimbra.common.service.ServiceException) AllAccountsSource(com.zimbra.cs.ephemeral.migrate.AttributeMigration.AllAccountsSource) DryRunMigrationCallback(com.zimbra.cs.ephemeral.migrate.AttributeMigration.DryRunMigrationCallback) CommandLineParser(org.apache.commons.cli.CommandLineParser)

Aggregations

EntrySource (com.zimbra.cs.ephemeral.migrate.AttributeMigration.EntrySource)4 MigrationCallback (com.zimbra.cs.ephemeral.migrate.AttributeMigration.MigrationCallback)4 EphemeralStore (com.zimbra.cs.ephemeral.EphemeralStore)3 Test (org.junit.Test)3 ServiceException (com.zimbra.common.service.ServiceException)1 EphemeralKey (com.zimbra.cs.ephemeral.EphemeralKey)1 EphemeralLocation (com.zimbra.cs.ephemeral.EphemeralLocation)1 EphemeralResult (com.zimbra.cs.ephemeral.EphemeralResult)1 Factory (com.zimbra.cs.ephemeral.EphemeralStore.Factory)1 FallbackEphemeralStore (com.zimbra.cs.ephemeral.FallbackEphemeralStore)1 InMemoryEphemeralStore (com.zimbra.cs.ephemeral.InMemoryEphemeralStore)1 LdapEntryLocation (com.zimbra.cs.ephemeral.LdapEntryLocation)1 LdapEphemeralStore (com.zimbra.cs.ephemeral.LdapEphemeralStore)1 AllAccountsSource (com.zimbra.cs.ephemeral.migrate.AttributeMigration.AllAccountsSource)1 DryRunMigrationCallback (com.zimbra.cs.ephemeral.migrate.AttributeMigration.DryRunMigrationCallback)1 SomeAccountsSource (com.zimbra.cs.ephemeral.migrate.AttributeMigration.SomeAccountsSource)1 ZimbraMigrationCallback (com.zimbra.cs.ephemeral.migrate.AttributeMigration.ZimbraMigrationCallback)1 ArrayList (java.util.ArrayList)1 CommandLine (org.apache.commons.cli.CommandLine)1 CommandLineParser (org.apache.commons.cli.CommandLineParser)1