1 package org.apache.lucene.index;
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with this
6 * work for additional information regarding copyright ownership. The ASF
7 * licenses this file to You under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16 * License for the specific language governing permissions and limitations under
20 import java.io.IOException;
22 import java.util.Map.Entry;
24 import org.apache.lucene.document.Document;
25 import org.apache.lucene.index.IndexWriterConfig.OpenMode;
26 import org.apache.lucene.store.Directory;
27 import org.apache.lucene.store.LockObtainFailedException;
28 import org.junit.After;
29 import org.junit.Before;
30 import org.junit.Test;
32 public class TestPersistentSnapshotDeletionPolicy extends TestSnapshotDeletionPolicy {
34 // Keep it a class member so that getDeletionPolicy can use it
35 private Directory snapshotDir;
37 // so we can close it if called by SDP tests
38 private PersistentSnapshotDeletionPolicy psdp;
42 public void setUp() throws Exception {
44 snapshotDir = newDirectory();
49 public void tearDown() throws Exception {
50 if (psdp != null) psdp.close();
56 protected SnapshotDeletionPolicy getDeletionPolicy() throws IOException {
57 if (psdp != null) psdp.close();
59 snapshotDir = newDirectory();
60 return psdp = new PersistentSnapshotDeletionPolicy(
61 new KeepOnlyLastCommitDeletionPolicy(), snapshotDir, OpenMode.CREATE,
62 TEST_VERSION_CURRENT);
66 protected SnapshotDeletionPolicy getDeletionPolicy(Map<String, String> snapshots) throws IOException {
67 SnapshotDeletionPolicy sdp = getDeletionPolicy();
68 if (snapshots != null) {
69 for (Entry<String, String> e: snapshots.entrySet()) {
70 sdp.registerSnapshotInfo(e.getKey(), e.getValue(), null);
78 public void testExistingSnapshots() throws Exception {
80 Directory dir = newDirectory();
81 PersistentSnapshotDeletionPolicy psdp = (PersistentSnapshotDeletionPolicy) getDeletionPolicy();
82 IndexWriter writer = new IndexWriter(dir, getConfig(random, psdp));
83 prepareIndexAndSnapshots(psdp, writer, numSnapshots, "snapshot");
87 // Re-initialize and verify snapshots were persisted
88 psdp = new PersistentSnapshotDeletionPolicy(
89 new KeepOnlyLastCommitDeletionPolicy(), snapshotDir, OpenMode.APPEND,
90 TEST_VERSION_CURRENT);
91 new IndexWriter(dir, getConfig(random, psdp)).close();
93 assertSnapshotExists(dir, psdp, numSnapshots);
94 assertEquals(numSnapshots, psdp.getSnapshots().size());
99 @Test(expected=IllegalArgumentException.class)
100 public void testIllegalSnapshotId() throws Exception {
101 getDeletionPolicy().snapshot("$SNAPSHOTS_DOC$");
105 public void testInvalidSnapshotInfos() throws Exception {
106 // Add the correct number of documents (1), but without snapshot information
107 IndexWriter writer = new IndexWriter(snapshotDir, getConfig(random, null));
108 writer.addDocument(new Document());
111 new PersistentSnapshotDeletionPolicy(
112 new KeepOnlyLastCommitDeletionPolicy(), snapshotDir, OpenMode.APPEND,
113 TEST_VERSION_CURRENT);
114 fail("should not have succeeded to read from an invalid Directory");
115 } catch (IllegalStateException e) {
120 public void testNoSnapshotInfos() throws Exception {
121 // Initialize an empty index in snapshotDir - PSDP should initialize successfully.
122 new IndexWriter(snapshotDir, getConfig(random, null)).close();
123 new PersistentSnapshotDeletionPolicy(
124 new KeepOnlyLastCommitDeletionPolicy(), snapshotDir, OpenMode.APPEND,
125 TEST_VERSION_CURRENT).close();
128 @Test(expected=IllegalStateException.class)
129 public void testTooManySnapshotInfos() throws Exception {
130 // Write two documents to the snapshots directory - illegal.
131 IndexWriter writer = new IndexWriter(snapshotDir, getConfig(random, null));
132 writer.addDocument(new Document());
133 writer.addDocument(new Document());
136 new PersistentSnapshotDeletionPolicy(
137 new KeepOnlyLastCommitDeletionPolicy(), snapshotDir, OpenMode.APPEND,
138 TEST_VERSION_CURRENT).close();
139 fail("should not have succeeded to open an invalid directory");
143 public void testSnapshotRelease() throws Exception {
144 Directory dir = newDirectory();
145 PersistentSnapshotDeletionPolicy psdp = (PersistentSnapshotDeletionPolicy) getDeletionPolicy();
146 IndexWriter writer = new IndexWriter(dir, getConfig(random, psdp));
147 prepareIndexAndSnapshots(psdp, writer, 1, "snapshot");
150 psdp.release("snapshot0");
153 psdp = new PersistentSnapshotDeletionPolicy(
154 new KeepOnlyLastCommitDeletionPolicy(), snapshotDir, OpenMode.APPEND,
155 TEST_VERSION_CURRENT);
156 assertEquals("Should have no snapshots !", 0, psdp.getSnapshots().size());
162 public void testStaticRead() throws Exception {
163 // While PSDP is open, it keeps a lock on the snapshots directory and thus
164 // prevents reading the snapshots information. This test checks that the
165 // static read method works.
166 int numSnapshots = 1;
167 Directory dir = newDirectory();
168 PersistentSnapshotDeletionPolicy psdp = (PersistentSnapshotDeletionPolicy) getDeletionPolicy();
169 IndexWriter writer = new IndexWriter(dir, getConfig(random, psdp));
170 prepareIndexAndSnapshots(psdp, writer, numSnapshots, "snapshot");
175 // This should fail, since the snapshots directory is locked - we didn't close it !
176 new PersistentSnapshotDeletionPolicy(
177 new KeepOnlyLastCommitDeletionPolicy(), snapshotDir, OpenMode.APPEND,
178 TEST_VERSION_CURRENT);
179 fail("should not have reached here - the snapshots directory should be locked!");
180 } catch (LockObtainFailedException e) {
186 // Reading the snapshots info should succeed though
187 Map<String, String> snapshots = PersistentSnapshotDeletionPolicy.readSnapshotsInfo(snapshotDir);
188 assertEquals("expected " + numSnapshots + " snapshots, got " + snapshots.size(), numSnapshots, snapshots.size());