Search in sources :

Example 1 with LocalFileSystemRepository

use of org.apache.solr.core.backup.repository.LocalFileSystemRepository in project lucene-solr by apache.

the class ReplicationHandler method restore.

private void restore(SolrParams params, SolrQueryResponse rsp, SolrQueryRequest req) throws IOException {
    if (restoreFuture != null && !restoreFuture.isDone()) {
        throw new SolrException(ErrorCode.BAD_REQUEST, "Restore in progress. Cannot run multiple restore operations" + "for the same core");
    }
    String name = params.get(NAME);
    String location = params.get(CoreAdminParams.BACKUP_LOCATION);
    String repoName = params.get(CoreAdminParams.BACKUP_REPOSITORY);
    CoreContainer cc = core.getCoreContainer();
    BackupRepository repo = null;
    if (repoName != null) {
        repo = cc.newBackupRepository(Optional.of(repoName));
        location = repo.getBackupLocation(location);
        if (location == null) {
            throw new IllegalArgumentException("location is required");
        }
    } else {
        repo = new LocalFileSystemRepository();
    }
    //If location is not provided then assume that the restore index is present inside the data directory.
    if (location == null) {
        location = core.getDataDir();
    }
    URI locationUri = repo.createURI(location);
    //snapshot folder since we allow snapshots to be taken without providing a name. Pick the latest timestamp.
    if (name == null) {
        String[] filePaths = repo.listAll(locationUri);
        List<OldBackupDirectory> dirs = new ArrayList<>();
        for (String f : filePaths) {
            OldBackupDirectory obd = new OldBackupDirectory(locationUri, f);
            if (obd.getTimestamp().isPresent()) {
                dirs.add(obd);
            }
        }
        Collections.sort(dirs);
        if (dirs.size() == 0) {
            throw new SolrException(ErrorCode.BAD_REQUEST, "No backup name specified and none found in " + core.getDataDir());
        }
        name = dirs.get(0).getDirName();
    } else {
        //"snapshot." is prefixed by snapshooter
        name = "snapshot." + name;
    }
    RestoreCore restoreCore = new RestoreCore(repo, core, locationUri, name);
    try {
        MDC.put("RestoreCore.core", core.getName());
        MDC.put("RestoreCore.backupLocation", location);
        MDC.put("RestoreCore.backupName", name);
        restoreFuture = restoreExecutor.submit(restoreCore);
        currentRestoreName = name;
    } finally {
        MDC.remove("RestoreCore.core");
        MDC.remove("RestoreCore.backupLocation");
        MDC.remove("RestoreCore.backupName");
    }
}
Also used : BackupRepository(org.apache.solr.core.backup.repository.BackupRepository) CoreContainer(org.apache.solr.core.CoreContainer) LocalFileSystemRepository(org.apache.solr.core.backup.repository.LocalFileSystemRepository) ArrayList(java.util.ArrayList) URI(java.net.URI) SolrException(org.apache.solr.common.SolrException)

Example 2 with LocalFileSystemRepository

use of org.apache.solr.core.backup.repository.LocalFileSystemRepository in project lucene-solr by apache.

the class TestBackupRepositoryFactory method testRepositoryConfig.

@Test
public void testRepositoryConfig() {
    PluginInfo[] plugins = new PluginInfo[1];
    {
        Map<String, Object> attrs = new HashMap<>();
        attrs.put(CoreAdminParams.NAME, "repo1");
        attrs.put(FieldType.CLASS_NAME, LocalFileSystemRepository.class.getName());
        attrs.put("default", "true");
        attrs.put("location", "/tmp");
        plugins[0] = new PluginInfo("repository", attrs);
    }
    BackupRepositoryFactory f = new BackupRepositoryFactory(plugins);
    {
        BackupRepository repo = f.newInstance(loader);
        assertTrue(repo instanceof LocalFileSystemRepository);
        assertEquals("/tmp", repo.getConfigProperty("location"));
    }
    {
        BackupRepository repo = f.newInstance(loader, "repo1");
        assertTrue(repo instanceof LocalFileSystemRepository);
        assertEquals("/tmp", repo.getConfigProperty("location"));
    }
}
Also used : BackupRepositoryFactory(org.apache.solr.core.backup.repository.BackupRepositoryFactory) BackupRepository(org.apache.solr.core.backup.repository.BackupRepository) LocalFileSystemRepository(org.apache.solr.core.backup.repository.LocalFileSystemRepository) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 3 with LocalFileSystemRepository

use of org.apache.solr.core.backup.repository.LocalFileSystemRepository in project lucene-solr by apache.

the class ReplicationHandler method doSnapShoot.

private void doSnapShoot(SolrParams params, SolrQueryResponse rsp, SolrQueryRequest req) {
    try {
        int numberToKeep = params.getInt(NUMBER_BACKUPS_TO_KEEP_REQUEST_PARAM, 0);
        if (numberToKeep > 0 && numberBackupsToKeep > 0) {
            throw new SolrException(ErrorCode.BAD_REQUEST, "Cannot use " + NUMBER_BACKUPS_TO_KEEP_REQUEST_PARAM + " if " + NUMBER_BACKUPS_TO_KEEP_INIT_PARAM + " was specified in the configuration.");
        }
        numberToKeep = Math.max(numberToKeep, numberBackupsToKeep);
        if (numberToKeep < 1) {
            numberToKeep = Integer.MAX_VALUE;
        }
        IndexCommit indexCommit = null;
        String commitName = params.get(CoreAdminParams.COMMIT_NAME);
        if (commitName != null) {
            SolrSnapshotMetaDataManager snapshotMgr = core.getSnapshotMetaDataManager();
            Optional<IndexCommit> commit = snapshotMgr.getIndexCommitByName(commitName);
            if (commit.isPresent()) {
                indexCommit = commit.get();
            } else {
                throw new SolrException(ErrorCode.BAD_REQUEST, "Unable to find an index commit with name " + commitName + " for core " + core.getName());
            }
        } else {
            IndexDeletionPolicyWrapper delPolicy = core.getDeletionPolicy();
            indexCommit = delPolicy.getLatestCommit();
            if (indexCommit == null) {
                indexCommit = req.getSearcher().getIndexReader().getIndexCommit();
            }
        }
        String location = params.get(CoreAdminParams.BACKUP_LOCATION);
        String repoName = params.get(CoreAdminParams.BACKUP_REPOSITORY);
        CoreContainer cc = core.getCoreContainer();
        BackupRepository repo = null;
        if (repoName != null) {
            repo = cc.newBackupRepository(Optional.of(repoName));
            location = repo.getBackupLocation(location);
            if (location == null) {
                throw new IllegalArgumentException("location is required");
            }
        } else {
            repo = new LocalFileSystemRepository();
            if (location == null) {
                location = core.getDataDir();
            } else {
                location = core.getCoreDescriptor().getInstanceDir().resolve(location).normalize().toString();
            }
        }
        // small race here before the commit point is saved
        URI locationUri = repo.createURI(location);
        SnapShooter snapShooter = new SnapShooter(repo, core, locationUri, params.get(NAME), commitName);
        snapShooter.validateCreateSnapshot();
        snapShooter.createSnapAsync(indexCommit, numberToKeep, (nl) -> snapShootDetails = nl);
    } catch (Exception e) {
        LOG.warn("Exception during creating a snapshot", e);
        rsp.add("exception", e);
    }
}
Also used : IndexDeletionPolicyWrapper(org.apache.solr.core.IndexDeletionPolicyWrapper) LocalFileSystemRepository(org.apache.solr.core.backup.repository.LocalFileSystemRepository) URI(java.net.URI) IndexCommit(org.apache.lucene.index.IndexCommit) NoSuchFileException(java.nio.file.NoSuchFileException) SolrException(org.apache.solr.common.SolrException) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) SolrSnapshotMetaDataManager(org.apache.solr.core.snapshots.SolrSnapshotMetaDataManager) BackupRepository(org.apache.solr.core.backup.repository.BackupRepository) CoreContainer(org.apache.solr.core.CoreContainer) SolrException(org.apache.solr.common.SolrException)

Aggregations

BackupRepository (org.apache.solr.core.backup.repository.BackupRepository)3 LocalFileSystemRepository (org.apache.solr.core.backup.repository.LocalFileSystemRepository)3 URI (java.net.URI)2 SolrException (org.apache.solr.common.SolrException)2 CoreContainer (org.apache.solr.core.CoreContainer)2 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 NoSuchFileException (java.nio.file.NoSuchFileException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 IndexCommit (org.apache.lucene.index.IndexCommit)1 IndexDeletionPolicyWrapper (org.apache.solr.core.IndexDeletionPolicyWrapper)1 BackupRepositoryFactory (org.apache.solr.core.backup.repository.BackupRepositoryFactory)1 SolrSnapshotMetaDataManager (org.apache.solr.core.snapshots.SolrSnapshotMetaDataManager)1 Test (org.junit.Test)1