Search in sources :

Example 6 with SnapshotMetaData

use of org.apache.solr.core.snapshots.SolrSnapshotMetaDataManager.SnapshotMetaData in project lucene-solr by apache.

the class SolrSnapshotManager method deleteNonSnapshotIndexFiles.

/**
   * This method deletes index files not associated with the specified <code>snapshots</code>.
   *
   * @param core The Solr core
   * @param dir The index directory storing the snapshot.
   * @param snapshots The snapshots to be preserved.
   * @throws IOException in case of I/O errors.
   */
public static void deleteNonSnapshotIndexFiles(SolrCore core, Directory dir, Collection<SnapshotMetaData> snapshots) throws IOException {
    final Set<Long> genNumbers = new HashSet<>();
    for (SnapshotMetaData m : snapshots) {
        genNumbers.add(m.getGenerationNumber());
    }
    deleteSnapshotIndexFiles(core, dir, new IndexDeletionPolicy() {

        @Override
        public void onInit(List<? extends IndexCommit> commits) throws IOException {
            for (IndexCommit ic : commits) {
                if (!genNumbers.contains(ic.getGeneration())) {
                    log.info("Deleting non-snapshotted index commit with generation {}", ic.getGeneration());
                    ic.delete();
                }
            }
        }

        @Override
        public void onCommit(List<? extends IndexCommit> commits) throws IOException {
        }
    });
}
Also used : IndexDeletionPolicy(org.apache.lucene.index.IndexDeletionPolicy) SnapshotMetaData(org.apache.solr.core.snapshots.SolrSnapshotMetaDataManager.SnapshotMetaData) IOException(java.io.IOException) IndexCommit(org.apache.lucene.index.IndexCommit) HashSet(java.util.HashSet)

Example 7 with SnapshotMetaData

use of org.apache.solr.core.snapshots.SolrSnapshotMetaDataManager.SnapshotMetaData in project lucene-solr by apache.

the class TestSolrCoreSnapshots method createSnapshot.

private SnapshotMetaData createSnapshot(SolrClient adminClient, String coreName, String commitName) throws Exception {
    CreateSnapshot req = new CreateSnapshot(commitName);
    req.setCoreName(coreName);
    adminClient.request(req);
    Collection<SnapshotMetaData> snapshots = listSnapshots(adminClient, coreName);
    Optional<SnapshotMetaData> metaData = snapshots.stream().filter(x -> commitName.equals(x.getName())).findFirst();
    assertTrue(metaData.isPresent());
    return metaData.get();
}
Also used : IndexCommit(org.apache.lucene.index.IndexCommit) IndexNotFoundException(org.apache.lucene.index.IndexNotFoundException) BeforeClass(org.junit.BeforeClass) Slow(org.apache.lucene.util.LuceneTestCase.Slow) DocCollection(org.apache.solr.common.cloud.DocCollection) TestUtil(org.apache.lucene.util.TestUtil) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) SolrTestCaseJ4(org.apache.solr.SolrTestCaseJ4) ArrayList(java.util.ArrayList) BackupRestoreUtils(org.apache.solr.handler.BackupRestoreUtils) BASE_URL_PROP(org.apache.solr.common.cloud.ZkStateReader.BASE_URL_PROP) Map(java.util.Map) DeleteSnapshot(org.apache.solr.client.solrj.request.CoreAdminRequest.DeleteSnapshot) CloudSolrClient(org.apache.solr.client.solrj.impl.CloudSolrClient) SimpleFSDirectory(org.apache.lucene.store.SimpleFSDirectory) ZkStateReader(org.apache.solr.common.cloud.ZkStateReader) AfterClass(org.junit.AfterClass) Slice(org.apache.solr.common.cloud.Slice) Logger(org.slf4j.Logger) MethodHandles(java.lang.invoke.MethodHandles) Collection(java.util.Collection) DirectoryReader(org.apache.lucene.index.DirectoryReader) SolrCloudTestCase(org.apache.solr.cloud.SolrCloudTestCase) Test(org.junit.Test) CreateSnapshot(org.apache.solr.client.solrj.request.CoreAdminRequest.CreateSnapshot) Replica(org.apache.solr.common.cloud.Replica) NamedList(org.apache.solr.common.util.NamedList) SolrClient(org.apache.solr.client.solrj.SolrClient) SnapshotMetaData(org.apache.solr.core.snapshots.SolrSnapshotMetaDataManager.SnapshotMetaData) List(java.util.List) ListSnapshots(org.apache.solr.client.solrj.request.CoreAdminRequest.ListSnapshots) Paths(java.nio.file.Paths) CoreAdminAction(org.apache.solr.common.params.CoreAdminParams.CoreAdminAction) Optional(java.util.Optional) Collections(java.util.Collections) CollectionAdminRequest(org.apache.solr.client.solrj.request.CollectionAdminRequest) SolrInputDocument(org.apache.solr.common.SolrInputDocument) SnapshotMetaData(org.apache.solr.core.snapshots.SolrSnapshotMetaDataManager.SnapshotMetaData) CreateSnapshot(org.apache.solr.client.solrj.request.CoreAdminRequest.CreateSnapshot)

Example 8 with SnapshotMetaData

use of org.apache.solr.core.snapshots.SolrSnapshotMetaDataManager.SnapshotMetaData in project lucene-solr by apache.

the class TestSolrCoreSnapshots method testBackupRestore.

@Test
public void testBackupRestore() throws Exception {
    CloudSolrClient solrClient = cluster.getSolrClient();
    String collectionName = "SolrCoreSnapshots";
    CollectionAdminRequest.Create create = CollectionAdminRequest.createCollection(collectionName, "conf1", 1, 1);
    create.process(solrClient);
    String location = createTempDir().toFile().getAbsolutePath();
    int nDocs = BackupRestoreUtils.indexDocs(cluster.getSolrClient(), collectionName, docsSeed);
    DocCollection collectionState = solrClient.getZkStateReader().getClusterState().getCollection(collectionName);
    assertEquals(1, collectionState.getActiveSlices().size());
    Slice shard = collectionState.getActiveSlices().iterator().next();
    assertEquals(1, shard.getReplicas().size());
    Replica replica = shard.getReplicas().iterator().next();
    String replicaBaseUrl = replica.getStr(BASE_URL_PROP);
    String coreName = replica.getStr(ZkStateReader.CORE_NAME_PROP);
    String backupName = TestUtil.randomSimpleString(random(), 1, 5);
    String commitName = TestUtil.randomSimpleString(random(), 1, 5);
    String duplicateName = commitName.concat("_duplicate");
    try (SolrClient adminClient = getHttpSolrClient(cluster.getJettySolrRunners().get(0).getBaseUrl().toString());
        SolrClient masterClient = getHttpSolrClient(replica.getCoreUrl())) {
        SnapshotMetaData metaData = createSnapshot(adminClient, coreName, commitName);
        // Create another snapshot referring to the same index commit to verify the
        // reference counting implementation during snapshot deletion.
        SnapshotMetaData duplicateCommit = createSnapshot(adminClient, coreName, duplicateName);
        assertEquals(metaData.getIndexDirPath(), duplicateCommit.getIndexDirPath());
        assertEquals(metaData.getGenerationNumber(), duplicateCommit.getGenerationNumber());
        // Delete all documents
        masterClient.deleteByQuery("*:*");
        masterClient.commit();
        BackupRestoreUtils.verifyDocs(0, cluster.getSolrClient(), collectionName);
        // Verify that the index directory contains at least 2 index commits - one referred by the snapshots
        // and the other containing document deletions.
        {
            List<IndexCommit> commits = listCommits(metaData.getIndexDirPath());
            assertTrue(commits.size() >= 2);
        }
        // Backup the earlier created snapshot.
        {
            Map<String, String> params = new HashMap<>();
            params.put("name", backupName);
            params.put("commitName", commitName);
            params.put("location", location);
            BackupRestoreUtils.runCoreAdminCommand(replicaBaseUrl, coreName, CoreAdminAction.BACKUPCORE.toString(), params);
        }
        // Restore the backup
        {
            Map<String, String> params = new HashMap<>();
            params.put("name", "snapshot." + backupName);
            params.put("location", location);
            BackupRestoreUtils.runCoreAdminCommand(replicaBaseUrl, coreName, CoreAdminAction.RESTORECORE.toString(), params);
            BackupRestoreUtils.verifyDocs(nDocs, cluster.getSolrClient(), collectionName);
        }
        // Verify that the old index directory (before restore) contains only those index commits referred by snapshots.
        // The IndexWriter (used to cleanup index files) creates an additional commit during closing. Hence we expect 2 commits (instead
        // of 1).
        {
            List<IndexCommit> commits = listCommits(metaData.getIndexDirPath());
            assertEquals(2, commits.size());
            assertEquals(metaData.getGenerationNumber(), commits.get(0).getGeneration());
        }
        // Delete first snapshot
        deleteSnapshot(adminClient, coreName, commitName);
        // Verify that corresponding index files have NOT been deleted (due to reference counting).
        assertFalse(listCommits(metaData.getIndexDirPath()).isEmpty());
        // Delete second snapshot
        deleteSnapshot(adminClient, coreName, duplicateCommit.getName());
        // Verify that corresponding index files have been deleted. Ideally this directory should
        // be removed immediately. But the current DirectoryFactory impl waits until the
        // closing the core (or the directoryFactory) for actual removal. Since the IndexWriter
        // (used to cleanup index files) creates an additional commit during closing, we expect a single
        // commit (instead of 0).
        assertEquals(1, listCommits(duplicateCommit.getIndexDirPath()).size());
    }
}
Also used : CollectionAdminRequest(org.apache.solr.client.solrj.request.CollectionAdminRequest) SnapshotMetaData(org.apache.solr.core.snapshots.SolrSnapshotMetaDataManager.SnapshotMetaData) Replica(org.apache.solr.common.cloud.Replica) CloudSolrClient(org.apache.solr.client.solrj.impl.CloudSolrClient) CloudSolrClient(org.apache.solr.client.solrj.impl.CloudSolrClient) SolrClient(org.apache.solr.client.solrj.SolrClient) Slice(org.apache.solr.common.cloud.Slice) ArrayList(java.util.ArrayList) NamedList(org.apache.solr.common.util.NamedList) List(java.util.List) DocCollection(org.apache.solr.common.cloud.DocCollection) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 9 with SnapshotMetaData

use of org.apache.solr.core.snapshots.SolrSnapshotMetaDataManager.SnapshotMetaData in project lucene-solr by apache.

the class TestSolrCoreSnapshots method listSnapshots.

private Collection<SnapshotMetaData> listSnapshots(SolrClient adminClient, String coreName) throws Exception {
    ListSnapshots req = new ListSnapshots();
    req.setCoreName(coreName);
    NamedList resp = adminClient.request(req);
    assertTrue(resp.get("snapshots") instanceof NamedList);
    NamedList apiResult = (NamedList) resp.get("snapshots");
    List<SnapshotMetaData> result = new ArrayList<>(apiResult.size());
    for (int i = 0; i < apiResult.size(); i++) {
        String commitName = apiResult.getName(i);
        String indexDirPath = (String) ((NamedList) apiResult.get(commitName)).get("indexDirPath");
        long genNumber = Long.parseLong((String) ((NamedList) apiResult.get(commitName)).get("generation"));
        result.add(new SnapshotMetaData(commitName, indexDirPath, genNumber));
    }
    return result;
}
Also used : NamedList(org.apache.solr.common.util.NamedList) ArrayList(java.util.ArrayList) ListSnapshots(org.apache.solr.client.solrj.request.CoreAdminRequest.ListSnapshots) SnapshotMetaData(org.apache.solr.core.snapshots.SolrSnapshotMetaDataManager.SnapshotMetaData)

Example 10 with SnapshotMetaData

use of org.apache.solr.core.snapshots.SolrSnapshotMetaDataManager.SnapshotMetaData in project lucene-solr by apache.

the class SolrCore method deleteNamedSnapshot.

/**
   * This method deletes the snapshot with the specified name. If the directory
   * storing the snapshot is not the same as the *current* core index directory,
   * then delete the files corresponding to this snapshot. Otherwise we leave the
   * index files related to snapshot as is (assuming the underlying Solr IndexDeletionPolicy
   * will clean them up appropriately).
   *
   * @param commitName The name of the snapshot to be deleted.
   * @throws IOException in case of I/O error.
   */
public void deleteNamedSnapshot(String commitName) throws IOException {
    // Note this lock is required to prevent multiple snapshot deletions from
    // opening multiple IndexWriter instances simultaneously.
    this.snapshotDelLock.lock();
    try {
        Optional<SnapshotMetaData> metadata = snapshotMgr.release(commitName);
        if (metadata.isPresent()) {
            long gen = metadata.get().getGenerationNumber();
            String indexDirPath = metadata.get().getIndexDirPath();
            if (!indexDirPath.equals(getIndexDir())) {
                Directory d = getDirectoryFactory().get(indexDirPath, DirContext.DEFAULT, "none");
                try {
                    Collection<SnapshotMetaData> snapshots = snapshotMgr.listSnapshotsInIndexDir(indexDirPath);
                    log.info("Following snapshots exist in the index directory {} : {}", indexDirPath, snapshots);
                    if (snapshots.isEmpty()) {
                        // No snapshots remain in this directory. Can be cleaned up!
                        log.info("Removing index directory {} since all named snapshots are deleted.", indexDirPath);
                        getDirectoryFactory().remove(d);
                    } else {
                        SolrSnapshotManager.deleteSnapshotIndexFiles(this, d, gen);
                    }
                } finally {
                    getDirectoryFactory().release(d);
                }
            }
        }
    } finally {
        snapshotDelLock.unlock();
    }
}
Also used : SnapshotMetaData(org.apache.solr.core.snapshots.SolrSnapshotMetaDataManager.SnapshotMetaData) Directory(org.apache.lucene.store.Directory)

Aggregations

SnapshotMetaData (org.apache.solr.core.snapshots.SolrSnapshotMetaDataManager.SnapshotMetaData)10 ArrayList (java.util.ArrayList)7 NamedList (org.apache.solr.common.util.NamedList)7 ListSnapshots (org.apache.solr.client.solrj.request.CoreAdminRequest.ListSnapshots)6 List (java.util.List)5 Map (java.util.Map)5 SolrClient (org.apache.solr.client.solrj.SolrClient)5 CloudSolrClient (org.apache.solr.client.solrj.impl.CloudSolrClient)5 CollectionAdminRequest (org.apache.solr.client.solrj.request.CollectionAdminRequest)5 DocCollection (org.apache.solr.common.cloud.DocCollection)5 Replica (org.apache.solr.common.cloud.Replica)5 Slice (org.apache.solr.common.cloud.Slice)5 Test (org.junit.Test)5 MethodHandles (java.lang.invoke.MethodHandles)4 Collection (java.util.Collection)4 HashMap (java.util.HashMap)4 Optional (java.util.Optional)4 IndexCommit (org.apache.lucene.index.IndexCommit)4 Slow (org.apache.lucene.util.LuceneTestCase.Slow)4 TestUtil (org.apache.lucene.util.TestUtil)4