/** * Constructor over the given {@link IndexWriter}. Uses the last * {@link IndexCommit} found in the {@link Directory} managed by the given * writer. */ public IndexAndTaxonomyRevision(IndexWriter indexWriter, SnapshotDirectoryTaxonomyWriter taxoWriter) throws IOException { IndexDeletionPolicy delPolicy = indexWriter.getConfig().getIndexDeletionPolicy(); if (!(delPolicy instanceof SnapshotDeletionPolicy)) { throw new IllegalArgumentException("IndexWriter must be created with SnapshotDeletionPolicy"); } this.indexWriter = indexWriter; this.taxoWriter = taxoWriter; this.indexSDP = (SnapshotDeletionPolicy) delPolicy; this.taxoSDP = taxoWriter.getDeletionPolicy(); this.indexCommit = indexSDP.snapshot(); this.taxoCommit = taxoSDP.snapshot(); this.version = revisionVersion(indexCommit, taxoCommit); this.sourceFiles = revisionFiles(indexCommit, taxoCommit); }
@Test public void testNoCommit() throws Exception { Directory indexDir = newDirectory(); IndexWriterConfig conf = new IndexWriterConfig(TEST_VERSION_CURRENT, null); conf.setIndexDeletionPolicy(new SnapshotDeletionPolicy(conf.getIndexDeletionPolicy())); IndexWriter indexWriter = new IndexWriter(indexDir, conf); Directory taxoDir = newDirectory(); SnapshotDirectoryTaxonomyWriter taxoWriter = new SnapshotDirectoryTaxonomyWriter(taxoDir); try { assertNotNull(new IndexAndTaxonomyRevision(indexWriter, taxoWriter)); fail("should have failed when there are no commits to snapshot"); } catch (IllegalStateException e) { // expected } finally { IOUtils.close(indexWriter, taxoWriter, taxoDir, indexDir); } }
@Test public void testSegmentsFileLast() throws Exception { Directory indexDir = newDirectory(); IndexWriterConfig conf = new IndexWriterConfig(TEST_VERSION_CURRENT, null); conf.setIndexDeletionPolicy(new SnapshotDeletionPolicy(conf.getIndexDeletionPolicy())); IndexWriter indexWriter = new IndexWriter(indexDir, conf); Directory taxoDir = newDirectory(); SnapshotDirectoryTaxonomyWriter taxoWriter = new SnapshotDirectoryTaxonomyWriter(taxoDir); try { indexWriter.addDocument(newDocument(taxoWriter)); indexWriter.commit(); taxoWriter.commit(); Revision rev = new IndexAndTaxonomyRevision(indexWriter, taxoWriter); Map<String,List<RevisionFile>> sourceFiles = rev.getSourceFiles(); assertEquals(2, sourceFiles.size()); for (List<RevisionFile> files : sourceFiles.values()) { String lastFile = files.get(files.size() - 1).fileName; assertTrue(lastFile.startsWith(IndexFileNames.SEGMENTS) && !lastFile.equals(IndexFileNames.SEGMENTS_GEN)); } } finally { IOUtils.close(indexWriter, taxoWriter, taxoDir, indexDir); } }
@Override @Before public void setUp() throws Exception { super.setUp(); publishDir = newMockDirectory(); handlerDir = newMockDirectory(); sourceDirFactory = new PerSessionDirectoryFactory(createTempDir("replicationClientTest")); replicator = new LocalReplicator(); callback = new IndexReadyCallback(handlerDir); handler = new IndexReplicationHandler(handlerDir, callback); client = new ReplicationClient(replicator, handler, sourceDirFactory); IndexWriterConfig conf = newIndexWriterConfig(null); conf.setIndexDeletionPolicy(new SnapshotDeletionPolicy(conf.getIndexDeletionPolicy())); publishWriter = new IndexWriter(publishDir, conf); }
@Test public void testSegmentsFileLast() throws Exception { Directory dir = newDirectory(); IndexWriterConfig conf = new IndexWriterConfig(TEST_VERSION_CURRENT, null); conf.setIndexDeletionPolicy(new SnapshotDeletionPolicy(conf.getIndexDeletionPolicy())); IndexWriter writer = new IndexWriter(dir, conf); try { writer.addDocument(new Document()); writer.commit(); Revision rev = new IndexRevision(writer); @SuppressWarnings("unchecked") Map<String, List<RevisionFile>> sourceFiles = rev.getSourceFiles(); assertEquals(1, sourceFiles.size()); List<RevisionFile> files = sourceFiles.values().iterator().next(); String lastFile = files.get(files.size() - 1).fileName; assertTrue(lastFile.startsWith(IndexFileNames.SEGMENTS) && !lastFile.equals(IndexFileNames.SEGMENTS_GEN)); } finally { IOUtils.close(writer, dir); } }
@Before @Override public void setUp() throws Exception { super.setUp(); if (VERBOSE) { System.setProperty("org.eclipse.jetty.LEVEL", "DEBUG"); // sets stderr logging to DEBUG level } clientWorkDir = createTempDir("httpReplicatorTest"); handlerIndexDir = newDirectory(); serverIndexDir = newDirectory(); serverReplicator = new LocalReplicator(); startServer(); IndexWriterConfig conf = newIndexWriterConfig(null); conf.setIndexDeletionPolicy(new SnapshotDeletionPolicy(conf.getIndexDeletionPolicy())); writer = new IndexWriter(serverIndexDir, conf); reader = DirectoryReader.open(writer, false); }
@Test public void testSerialization() throws IOException { Directory dir = newDirectory(); IndexWriterConfig conf = new IndexWriterConfig(TEST_VERSION_CURRENT, null); conf.setIndexDeletionPolicy(new SnapshotDeletionPolicy(conf.getIndexDeletionPolicy())); IndexWriter writer = new IndexWriter(dir, conf); writer.addDocument(new Document()); writer.commit(); Revision rev = new IndexRevision(writer); SessionToken session1 = new SessionToken("17", rev); ByteArrayOutputStream baos = new ByteArrayOutputStream(); session1.serialize(new DataOutputStream(baos)); byte[] b = baos.toByteArray(); SessionToken session2 = new SessionToken(new DataInputStream(new ByteArrayInputStream(b))); assertEquals(session1.id, session2.id); assertEquals(session1.version, session2.version); assertEquals(1, session2.sourceFiles.size()); assertEquals(session1.sourceFiles.size(), session2.sourceFiles.size()); assertEquals(session1.sourceFiles.keySet(), session2.sourceFiles.keySet()); List<RevisionFile> files1 = session1.sourceFiles.values().iterator().next(); List<RevisionFile> files2 = session2.sourceFiles.values().iterator().next(); assertEquals(files1, files2); IOUtils.close(writer, dir); }
@Override @Before public void setUp() throws Exception { super.setUp(); publishIndexDir = newDirectory(); publishTaxoDir = newDirectory(); handlerIndexDir = newMockDirectory(); handlerTaxoDir = newMockDirectory(); clientWorkDir = createTempDir("replicationClientTest"); sourceDirFactory = new PerSessionDirectoryFactory(clientWorkDir); replicator = new LocalReplicator(); callback = new IndexAndTaxonomyReadyCallback(handlerIndexDir, handlerTaxoDir); handler = new IndexAndTaxonomyReplicationHandler(handlerIndexDir, handlerTaxoDir, callback); client = new ReplicationClient(replicator, handler, sourceDirFactory); IndexWriterConfig conf = newIndexWriterConfig(null); conf.setIndexDeletionPolicy(new SnapshotDeletionPolicy(conf.getIndexDeletionPolicy())); publishIndexWriter = new IndexWriter(publishIndexDir, conf); publishTaxoWriter = new SnapshotDirectoryTaxonomyWriter(publishTaxoDir); config = new FacetsConfig(); config.setHierarchical("A", true); }
ResourceIterator<File> snapshot( File indexDir, LuceneIndexWriter writer ) throws IOException { SnapshotDeletionPolicy deletionPolicy = (SnapshotDeletionPolicy) writer.getIndexDeletionPolicy(); try { return new LuceneSnapshotIterator( indexDir, deletionPolicy.snapshot(), deletionPolicy ); } catch(IllegalStateException e) { if(e.getMessage().equals( NO_INDEX_COMMIT_TO_SNAPSHOT )) { return emptyIterator(); } throw e; } }
private IndexWriter open(boolean create) throws IOException, SearchLibException { indexWriterLock.lock(); final IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_36, null); config.setOpenMode(create ? OpenMode.CREATE_OR_APPEND : OpenMode.APPEND); config.setMergeScheduler(new SerialMergeScheduler()); config.setWriteLockTimeout(indexConfig.getWriteLockTimeout()); config.setRAMBufferSizeMB(128); final Similarity similarity = indexConfig.getNewSimilarityInstance(); if (similarity != null) config.setSimilarity(similarity); if (!create) { final SnapshotDeletionPolicy snapshotDeletionPolicy = new SnapshotDeletionPolicy(config.getIndexDeletionPolicy()); config.setIndexDeletionPolicy(snapshotDeletionPolicy); } Logging.debug("WriteLocal open " + indexDirectory.getDirectory()); return new IndexWriter(indexDirectory.getDirectory(), config); }
@Override @Before public void setUp() throws Exception { super.setUp(); publishDir = newMockDirectory(); handlerDir = newMockDirectory(); sourceDirFactory = new PerSessionDirectoryFactory(_TestUtil.getTempDir("replicationClientTest")); replicator = new LocalReplicator(); callback = new IndexReadyCallback(handlerDir); handler = new IndexReplicationHandler(handlerDir, callback); client = new ReplicationClient(replicator, handler, sourceDirFactory); IndexWriterConfig conf = newIndexWriterConfig(TEST_VERSION_CURRENT, null); conf.setIndexDeletionPolicy(new SnapshotDeletionPolicy(conf.getIndexDeletionPolicy())); publishWriter = new IndexWriter(publishDir, conf); }
@Test public void testRevisionRelease() throws Exception { Directory dir = newDirectory(); IndexWriterConfig conf = new IndexWriterConfig(TEST_VERSION_CURRENT, null); conf.setIndexDeletionPolicy(new SnapshotDeletionPolicy(conf.getIndexDeletionPolicy())); IndexWriter writer = new IndexWriter(dir, conf); try { writer.addDocument(new Document()); writer.commit(); Revision rev1 = new IndexRevision(writer); // releasing that revision should not delete the files rev1.release(); assertTrue(dir.fileExists(IndexFileNames.SEGMENTS + "_1")); rev1 = new IndexRevision(writer); // create revision again, so the files are snapshotted writer.addDocument(new Document()); writer.commit(); assertNotNull(new IndexRevision(writer)); rev1.release(); // this release should trigger the delete of segments_1 assertFalse(dir.fileExists(IndexFileNames.SEGMENTS + "_1")); } finally { IOUtils.close(writer, dir); } }
@Before @Override public void setUp() throws Exception { super.setUp(); System.setProperty("org.eclipse.jetty.LEVEL", "DEBUG"); // sets stderr logging to DEBUG level clientWorkDir = _TestUtil.getTempDir("httpReplicatorTest"); handlerIndexDir = newDirectory(); serverIndexDir = newDirectory(); serverReplicator = new LocalReplicator(); startServer(); IndexWriterConfig conf = newIndexWriterConfig(TEST_VERSION_CURRENT, null); conf.setIndexDeletionPolicy(new SnapshotDeletionPolicy(conf.getIndexDeletionPolicy())); writer = new IndexWriter(serverIndexDir, conf); reader = DirectoryReader.open(writer, false); }
@Override @Before public void setUp() throws Exception { super.setUp(); publishIndexDir = newDirectory(); publishTaxoDir = newDirectory(); handlerIndexDir = newMockDirectory(); handlerTaxoDir = newMockDirectory(); clientWorkDir = _TestUtil.getTempDir("replicationClientTest"); sourceDirFactory = new PerSessionDirectoryFactory(clientWorkDir); replicator = new LocalReplicator(); callback = new IndexAndTaxonomyReadyCallback(handlerIndexDir, handlerTaxoDir); handler = new IndexAndTaxonomyReplicationHandler(handlerIndexDir, handlerTaxoDir, callback); client = new ReplicationClient(replicator, handler, sourceDirFactory); IndexWriterConfig conf = newIndexWriterConfig(TEST_VERSION_CURRENT, null); conf.setIndexDeletionPolicy(new SnapshotDeletionPolicy(conf.getIndexDeletionPolicy())); publishIndexWriter = new IndexWriter(publishIndexDir, conf); publishTaxoWriter = new SnapshotDirectoryTaxonomyWriter(publishTaxoDir); }
public void test() throws Exception { Directory dir = null; IndexDeletionPolicy policy = new KeepOnlyLastCommitDeletionPolicy(); SnapshotDeletionPolicy snapshotter = new SnapshotDeletionPolicy(policy); IndexWriterConfig conf = new IndexWriterConfig(Version.LUCENE_41, AosAnalyser.NO_LIMIT_TOKEN_COUNT_SIMPLE_ANALYSER); conf.setIndexDeletionPolicy(snapshotter); IndexWriter writer = new IndexWriter(dir, conf); try { IndexCommit commit = snapshotter.snapshot("unique-id"); Collection<String> fileNames = commit.getFileNames(); /* <iterate over & copy files from fileNames> */ } finally { snapshotter.release("unique-id"); } }
/** * Creates a new {@link org.elasticsearch.index.engine.EngineConfig} */ public EngineConfig(OpenMode openMode, ShardId shardId, ThreadPool threadPool, IndexSettings indexSettings, Engine.Warmer warmer, Store store, SnapshotDeletionPolicy deletionPolicy, MergePolicy mergePolicy, Analyzer analyzer, Similarity similarity, CodecService codecService, Engine.EventListener eventListener, TranslogRecoveryPerformer translogRecoveryPerformer, QueryCache queryCache, QueryCachingPolicy queryCachingPolicy, TranslogConfig translogConfig, TimeValue flushMergesAfter, ReferenceManager.RefreshListener refreshListeners, long maxUnsafeAutoIdTimestamp) { if (openMode == null) { throw new IllegalArgumentException("openMode must not be null"); } this.shardId = shardId; this.indexSettings = indexSettings; this.threadPool = threadPool; this.warmer = warmer == null ? (a) -> {} : warmer; this.store = store; this.deletionPolicy = deletionPolicy; this.mergePolicy = mergePolicy; this.analyzer = analyzer; this.similarity = similarity; this.codecService = codecService; this.eventListener = eventListener; codecName = indexSettings.getValue(INDEX_CODEC_SETTING); // We give IndexWriter a "huge" (256 MB) buffer, so it won't flush on its own unless the ES indexing buffer is also huge and/or // there are not too many shards allocated to this node. Instead, IndexingMemoryController periodically checks // and refreshes the most heap-consuming shards when total indexing heap usage across all shards is too high: indexingBufferSize = new ByteSizeValue(256, ByteSizeUnit.MB); this.translogRecoveryPerformer = translogRecoveryPerformer; this.queryCache = queryCache; this.queryCachingPolicy = queryCachingPolicy; this.translogConfig = translogConfig; this.flushMergesAfter = flushMergesAfter; this.openMode = openMode; this.refreshListeners = refreshListeners; assert maxUnsafeAutoIdTimestamp >= IndexRequest.UNSET_AUTO_GENERATED_TIMESTAMP : "maxUnsafeAutoIdTimestamp must be >= -1 but was " + maxUnsafeAutoIdTimestamp; this.maxUnsafeAutoIdTimestamp = maxUnsafeAutoIdTimestamp; }
public void testUserDataRead() throws IOException { final ShardId shardId = new ShardId("index", "_na_", 1); DirectoryService directoryService = new LuceneManagedDirectoryService(random()); Store store = new Store(shardId, INDEX_SETTINGS, directoryService, new DummyShardLock(shardId)); IndexWriterConfig config = newIndexWriterConfig(random(), new MockAnalyzer(random())).setCodec(TestUtil.getDefaultCodec()); SnapshotDeletionPolicy deletionPolicy = new SnapshotDeletionPolicy(new KeepOnlyLastCommitDeletionPolicy()); config.setIndexDeletionPolicy(deletionPolicy); IndexWriter writer = new IndexWriter(store.directory(), config); Document doc = new Document(); doc.add(new TextField("id", "1", Field.Store.NO)); writer.addDocument(doc); Map<String, String> commitData = new HashMap<>(2); String syncId = "a sync id"; String translogId = "a translog id"; commitData.put(Engine.SYNC_COMMIT_ID, syncId); commitData.put(Translog.TRANSLOG_GENERATION_KEY, translogId); writer.setCommitData(commitData); writer.commit(); writer.close(); Store.MetadataSnapshot metadata; metadata = store.getMetadata(randomBoolean() ? null : deletionPolicy.snapshot()); assertFalse(metadata.asMap().isEmpty()); // do not check for correct files, we have enough tests for that above assertThat(metadata.getCommitUserData().get(Engine.SYNC_COMMIT_ID), equalTo(syncId)); assertThat(metadata.getCommitUserData().get(Translog.TRANSLOG_GENERATION_KEY), equalTo(translogId)); TestUtil.checkIndex(store.directory()); assertDeleteContent(store, directoryService); IOUtils.close(store); }
@Override protected IndexWriterConfig createIndexWriterConfig(OpenMode openMode) { IndexWriterConfig conf = super.createIndexWriterConfig(openMode); sdp = new SnapshotDeletionPolicy(conf.getIndexDeletionPolicy()); conf.setIndexDeletionPolicy(sdp); return conf; }
/** * Constructor over the given {@link IndexWriter}. Uses the last * {@link IndexCommit} found in the {@link Directory} managed by the given * writer. */ public IndexRevision(IndexWriter writer) throws IOException { IndexDeletionPolicy delPolicy = writer.getConfig().getIndexDeletionPolicy(); if (!(delPolicy instanceof SnapshotDeletionPolicy)) { throw new IllegalArgumentException("IndexWriter must be created with SnapshotDeletionPolicy"); } this.writer = writer; this.sdp = (SnapshotDeletionPolicy) delPolicy; this.commit = sdp.snapshot(); this.version = revisionVersion(commit); this.sourceFiles = revisionFiles(commit); }
@Test public void testRevisionRelease() throws Exception { Directory indexDir = newDirectory(); IndexWriterConfig conf = new IndexWriterConfig(TEST_VERSION_CURRENT, null); conf.setIndexDeletionPolicy(new SnapshotDeletionPolicy(conf.getIndexDeletionPolicy())); IndexWriter indexWriter = new IndexWriter(indexDir, conf); Directory taxoDir = newDirectory(); SnapshotDirectoryTaxonomyWriter taxoWriter = new SnapshotDirectoryTaxonomyWriter(taxoDir); // we look to see that certain files are deleted: if (indexDir instanceof MockDirectoryWrapper) { ((MockDirectoryWrapper)indexDir).setEnableVirusScanner(false); } try { indexWriter.addDocument(newDocument(taxoWriter)); indexWriter.commit(); taxoWriter.commit(); Revision rev1 = new IndexAndTaxonomyRevision(indexWriter, taxoWriter); // releasing that revision should not delete the files rev1.release(); assertTrue(slowFileExists(indexDir, IndexFileNames.SEGMENTS + "_1")); assertTrue(slowFileExists(taxoDir, IndexFileNames.SEGMENTS + "_1")); rev1 = new IndexAndTaxonomyRevision(indexWriter, taxoWriter); // create revision again, so the files are snapshotted indexWriter.addDocument(newDocument(taxoWriter)); indexWriter.commit(); taxoWriter.commit(); assertNotNull(new IndexAndTaxonomyRevision(indexWriter, taxoWriter)); rev1.release(); // this release should trigger the delete of segments_1 assertFalse(slowFileExists(indexDir, IndexFileNames.SEGMENTS + "_1")); } finally { IOUtils.close(indexWriter, taxoWriter, taxoDir, indexDir); if (indexDir instanceof MockDirectoryWrapper) { // set back to on for other tests ((MockDirectoryWrapper)indexDir).setEnableVirusScanner(true); } } }
@Before @Override public void setUp() throws Exception { super.setUp(); sourceDir = newDirectory(); IndexWriterConfig conf = newIndexWriterConfig(null); conf.setIndexDeletionPolicy(new SnapshotDeletionPolicy(conf.getIndexDeletionPolicy())); sourceWriter = new IndexWriter(sourceDir, conf); replicator = new LocalReplicator(); }
@Test public void testNoCommit() throws Exception { Directory dir = newDirectory(); IndexWriterConfig conf = new IndexWriterConfig(TEST_VERSION_CURRENT, null); conf.setIndexDeletionPolicy(new SnapshotDeletionPolicy(conf.getIndexDeletionPolicy())); IndexWriter writer = new IndexWriter(dir, conf); try { assertNotNull(new IndexRevision(writer)); fail("should have failed when there are no commits to snapshot"); } catch (IllegalStateException e) { // expected } finally { IOUtils.close(writer, dir); } }
@Test public void testRevisionRelease() throws Exception { Directory dir = newDirectory(); IndexWriterConfig conf = new IndexWriterConfig(TEST_VERSION_CURRENT, null); // we look to see that certain files are deleted: if (dir instanceof MockDirectoryWrapper) { ((MockDirectoryWrapper)dir).setEnableVirusScanner(false); } conf.setIndexDeletionPolicy(new SnapshotDeletionPolicy(conf.getIndexDeletionPolicy())); IndexWriter writer = new IndexWriter(dir, conf); try { writer.addDocument(new Document()); writer.commit(); Revision rev1 = new IndexRevision(writer); // releasing that revision should not delete the files rev1.release(); assertTrue(slowFileExists(dir, IndexFileNames.SEGMENTS + "_1")); rev1 = new IndexRevision(writer); // create revision again, so the files are snapshotted writer.addDocument(new Document()); writer.commit(); assertNotNull(new IndexRevision(writer)); rev1.release(); // this release should trigger the delete of segments_1 assertFalse(slowFileExists(dir, IndexFileNames.SEGMENTS + "_1")); } finally { IOUtils.close(writer, dir); } }
@Test public void testOpen() throws Exception { Directory dir = newDirectory(); IndexWriterConfig conf = new IndexWriterConfig(TEST_VERSION_CURRENT, null); conf.setIndexDeletionPolicy(new SnapshotDeletionPolicy(conf.getIndexDeletionPolicy())); IndexWriter writer = new IndexWriter(dir, conf); try { writer.addDocument(new Document()); writer.commit(); Revision rev = new IndexRevision(writer); @SuppressWarnings("unchecked") Map<String, List<RevisionFile>> sourceFiles = rev.getSourceFiles(); String source = sourceFiles.keySet().iterator().next(); for (RevisionFile file : sourceFiles.values().iterator().next()) { IndexInput src = dir.openInput(file.fileName, IOContext.READONCE); InputStream in = rev.open(source, file.fileName); assertEquals(src.length(), in.available()); byte[] srcBytes = new byte[(int) src.length()]; byte[] inBytes = new byte[(int) src.length()]; int offset = 0; if (random().nextBoolean()) { int skip = random().nextInt(10); if (skip >= src.length()) { skip = 0; } in.skip(skip); src.seek(skip); offset = skip; } src.readBytes(srcBytes, offset, srcBytes.length - offset); in.read(inBytes, offset, inBytes.length - offset); assertArrayEquals(srcBytes, inBytes); IOUtils.close(src, in); } } finally { IOUtils.close(writer, dir); } }
LuceneSnapshotIterator( File indexDirectory, IndexCommit snapshotPoint, SnapshotDeletionPolicy deletionPolicy ) throws IOException { this.indexDirectory = indexDirectory; this.deletionPolicy = deletionPolicy; this.snapshotPoint = snapshotPoint; this.fileNames = snapshotPoint.getFileNames().iterator(); }
@Before public void setup() throws IOException { writer = mock( LuceneIndexWriter.class ); snapshotPolicy = mock(SnapshotDeletionPolicy.class); luceneSnapshot = mock(IndexCommit.class); IndexWriterConfig config = new IndexWriterConfig( null ); when( writer.getIndexDeletionPolicy() ).thenReturn( snapshotPolicy ); when(snapshotPolicy.snapshot()).thenReturn( luceneSnapshot ); }