Search in sources :

Example 21 with ShardId

use of org.elasticsearch.index.shard.ShardId in project elasticsearch by elastic.

the class NodeEnvironmentTests method testShardLock.

public void testShardLock() throws Exception {
    final NodeEnvironment env = newNodeEnvironment();
    Index index = new Index("foo", "fooUUID");
    ShardLock fooLock = env.shardLock(new ShardId(index, 0));
    assertEquals(new ShardId(index, 0), fooLock.getShardId());
    try {
        env.shardLock(new ShardId(index, 0));
        fail("shard is locked");
    } catch (ShardLockObtainFailedException ex) {
    // expected
    }
    for (Path path : env.indexPaths(index)) {
        Files.createDirectories(path.resolve("0"));
        Files.createDirectories(path.resolve("1"));
    }
    try {
        env.lockAllForIndex(index, idxSettings, randomIntBetween(0, 10));
        fail("shard 0 is locked");
    } catch (ShardLockObtainFailedException ex) {
    // expected
    }
    fooLock.close();
    // can lock again?
    env.shardLock(new ShardId(index, 0)).close();
    List<ShardLock> locks = env.lockAllForIndex(index, idxSettings, randomIntBetween(0, 10));
    try {
        env.shardLock(new ShardId(index, 0));
        fail("shard is locked");
    } catch (ShardLockObtainFailedException ex) {
    // expected
    }
    IOUtils.close(locks);
    assertTrue("LockedShards: " + env.lockedShards(), env.lockedShards().isEmpty());
    env.close();
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) Path(java.nio.file.Path) Index(org.elasticsearch.index.Index)

Example 22 with ShardId

use of org.elasticsearch.index.shard.ShardId in project elasticsearch by elastic.

the class NodeEnvironmentTests method testDeleteSafe.

public void testDeleteSafe() throws Exception {
    final NodeEnvironment env = newNodeEnvironment();
    final Index index = new Index("foo", "fooUUID");
    ShardLock fooLock = env.shardLock(new ShardId(index, 0));
    assertEquals(new ShardId(index, 0), fooLock.getShardId());
    for (Path path : env.indexPaths(index)) {
        Files.createDirectories(path.resolve("0"));
        Files.createDirectories(path.resolve("1"));
    }
    try {
        env.deleteShardDirectorySafe(new ShardId(index, 0), idxSettings);
        fail("shard is locked");
    } catch (ShardLockObtainFailedException ex) {
    // expected
    }
    for (Path path : env.indexPaths(index)) {
        assertTrue(Files.exists(path.resolve("0")));
        assertTrue(Files.exists(path.resolve("1")));
    }
    env.deleteShardDirectorySafe(new ShardId(index, 1), idxSettings);
    for (Path path : env.indexPaths(index)) {
        assertTrue(Files.exists(path.resolve("0")));
        assertFalse(Files.exists(path.resolve("1")));
    }
    try {
        env.deleteIndexDirectorySafe(index, randomIntBetween(0, 10), idxSettings);
        fail("shard is locked");
    } catch (ShardLockObtainFailedException ex) {
    // expected
    }
    fooLock.close();
    for (Path path : env.indexPaths(index)) {
        assertTrue(Files.exists(path));
    }
    final AtomicReference<Throwable> threadException = new AtomicReference<>();
    final CountDownLatch latch = new CountDownLatch(1);
    final CountDownLatch blockLatch = new CountDownLatch(1);
    final CountDownLatch start = new CountDownLatch(1);
    if (randomBoolean()) {
        Thread t = new Thread(new AbstractRunnable() {

            @Override
            public void onFailure(Exception e) {
                logger.error("unexpected error", e);
                threadException.set(e);
                latch.countDown();
                blockLatch.countDown();
            }

            @Override
            protected void doRun() throws Exception {
                start.await();
                try (ShardLock autoCloses = env.shardLock(new ShardId(index, 0))) {
                    blockLatch.countDown();
                    Thread.sleep(randomIntBetween(1, 10));
                }
                latch.countDown();
            }
        });
        t.start();
    } else {
        latch.countDown();
        blockLatch.countDown();
    }
    start.countDown();
    blockLatch.await();
    env.deleteIndexDirectorySafe(index, 5000, idxSettings);
    assertNull(threadException.get());
    for (Path path : env.indexPaths(index)) {
        assertFalse(Files.exists(path));
    }
    latch.await();
    assertTrue("LockedShards: " + env.lockedShards(), env.lockedShards().isEmpty());
    env.close();
}
Also used : Path(java.nio.file.Path) AbstractRunnable(org.elasticsearch.common.util.concurrent.AbstractRunnable) Index(org.elasticsearch.index.Index) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) ShardId(org.elasticsearch.index.shard.ShardId)

Example 23 with ShardId

use of org.elasticsearch.index.shard.ShardId in project elasticsearch by elastic.

the class NodeEnvironmentTests method testCustomDataPaths.

public void testCustomDataPaths() throws Exception {
    String[] dataPaths = tmpPaths();
    NodeEnvironment env = newNodeEnvironment(dataPaths, "/tmp", Settings.EMPTY);
    final Settings indexSettings = Settings.builder().put(IndexMetaData.SETTING_INDEX_UUID, "myindexUUID").build();
    IndexSettings s1 = IndexSettingsModule.newIndexSettings("myindex", indexSettings);
    IndexSettings s2 = IndexSettingsModule.newIndexSettings("myindex", Settings.builder().put(indexSettings).put(IndexMetaData.SETTING_DATA_PATH, "/tmp/foo").build());
    Index index = new Index("myindex", "myindexUUID");
    ShardId sid = new ShardId(index, 0);
    assertFalse("no settings should mean no custom data path", s1.hasCustomDataPath());
    assertTrue("settings with path_data should have a custom data path", s2.hasCustomDataPath());
    assertThat(env.availableShardPaths(sid), equalTo(env.availableShardPaths(sid)));
    assertThat(env.resolveCustomLocation(s2, sid), equalTo(PathUtils.get("/tmp/foo/0/" + index.getUUID() + "/0")));
    assertThat("shard paths with a custom data_path should contain only regular paths", env.availableShardPaths(sid), equalTo(stringsToPaths(dataPaths, "nodes/0/indices/" + index.getUUID() + "/0")));
    assertThat("index paths uses the regular template", env.indexPaths(index), equalTo(stringsToPaths(dataPaths, "nodes/0/indices/" + index.getUUID())));
    IndexSettings s3 = new IndexSettings(s2.getIndexMetaData(), Settings.builder().put(NodeEnvironment.ADD_NODE_LOCK_ID_TO_CUSTOM_PATH.getKey(), false).build());
    assertThat(env.availableShardPaths(sid), equalTo(env.availableShardPaths(sid)));
    assertThat(env.resolveCustomLocation(s3, sid), equalTo(PathUtils.get("/tmp/foo/" + index.getUUID() + "/0")));
    assertThat("shard paths with a custom data_path should contain only regular paths", env.availableShardPaths(sid), equalTo(stringsToPaths(dataPaths, "nodes/0/indices/" + index.getUUID() + "/0")));
    assertThat("index paths uses the regular template", env.indexPaths(index), equalTo(stringsToPaths(dataPaths, "nodes/0/indices/" + index.getUUID())));
    env.close();
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) IndexSettings(org.elasticsearch.index.IndexSettings) Index(org.elasticsearch.index.Index) Matchers.containsString(org.hamcrest.Matchers.containsString) Settings(org.elasticsearch.common.settings.Settings) IndexSettings(org.elasticsearch.index.IndexSettings)

Example 24 with ShardId

use of org.elasticsearch.index.shard.ShardId in project elasticsearch by elastic.

the class BitSetFilterCacheTests method testRejectOtherIndex.

public void testRejectOtherIndex() throws IOException {
    BitsetFilterCache cache = new BitsetFilterCache(INDEX_SETTINGS, new BitsetFilterCache.Listener() {

        @Override
        public void onCache(ShardId shardId, Accountable accountable) {
        }

        @Override
        public void onRemoval(ShardId shardId, Accountable accountable) {
        }
    });
    Directory dir = newDirectory();
    IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig());
    writer.addDocument(new Document());
    DirectoryReader reader = DirectoryReader.open(writer);
    writer.close();
    reader = ElasticsearchDirectoryReader.wrap(reader, new ShardId("test2", "_na_", 0));
    BitSetProducer producer = cache.getBitSetProducer(new MatchAllDocsQuery());
    try {
        producer.getBitSet(reader.leaves().get(0));
        fail();
    } catch (IllegalStateException expected) {
        assertEquals("Trying to load bit set for index [test2] with cache of index [test]", expected.getMessage());
    } finally {
        IOUtils.close(reader, dir);
    }
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) IndexWriter(org.apache.lucene.index.IndexWriter) ElasticsearchDirectoryReader(org.elasticsearch.common.lucene.index.ElasticsearchDirectoryReader) DirectoryReader(org.apache.lucene.index.DirectoryReader) BitSetProducer(org.apache.lucene.search.join.BitSetProducer) Accountable(org.apache.lucene.util.Accountable) Document(org.apache.lucene.document.Document) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) RAMDirectory(org.apache.lucene.store.RAMDirectory) Directory(org.apache.lucene.store.Directory)

Example 25 with ShardId

use of org.elasticsearch.index.shard.ShardId in project elasticsearch by elastic.

the class AbstractStringFieldDataTestCase method testNestedSorting.

public void testNestedSorting(MultiValueMode sortMode) throws IOException {
    final String[] values = new String[randomIntBetween(2, 20)];
    for (int i = 0; i < values.length; ++i) {
        values[i] = TestUtil.randomSimpleString(random());
    }
    final int numParents = scaledRandomIntBetween(10, 3072);
    List<Document> docs = new ArrayList<>();
    FixedBitSet parents = new FixedBitSet(64);
    for (int i = 0; i < numParents; ++i) {
        docs.clear();
        final int numChildren = randomInt(4);
        for (int j = 0; j < numChildren; ++j) {
            final Document child = new Document();
            final int numValues = randomInt(3);
            for (int k = 0; k < numValues; ++k) {
                final String value = RandomPicks.randomFrom(random(), values);
                addField(child, "text", value);
            }
            docs.add(child);
        }
        final Document parent = new Document();
        parent.add(new StringField("type", "parent", Store.YES));
        final String value = RandomPicks.randomFrom(random(), values);
        if (value != null) {
            addField(parent, "text", value);
        }
        docs.add(parent);
        int bit = parents.prevSetBit(parents.length() - 1) + docs.size();
        parents = FixedBitSet.ensureCapacity(parents, bit);
        parents.set(bit);
        writer.addDocuments(docs);
        if (randomInt(10) == 0) {
            writer.commit();
        }
    }
    DirectoryReader directoryReader = DirectoryReader.open(writer);
    directoryReader = ElasticsearchDirectoryReader.wrap(directoryReader, new ShardId(indexService.index(), 0));
    IndexSearcher searcher = new IndexSearcher(directoryReader);
    IndexFieldData<?> fieldData = getForField("text");
    final Object missingValue;
    switch(randomInt(4)) {
        case 0:
            missingValue = "_first";
            break;
        case 1:
            missingValue = "_last";
            break;
        case 2:
            missingValue = new BytesRef(RandomPicks.randomFrom(random(), values));
            break;
        default:
            missingValue = new BytesRef(TestUtil.randomSimpleString(random()));
            break;
    }
    Query parentFilter = new TermQuery(new Term("type", "parent"));
    Query childFilter = Queries.not(parentFilter);
    Nested nested = createNested(searcher, parentFilter, childFilter);
    BytesRefFieldComparatorSource nestedComparatorSource = new BytesRefFieldComparatorSource(fieldData, missingValue, sortMode, nested);
    ToParentBlockJoinQuery query = new ToParentBlockJoinQuery(new ConstantScoreQuery(childFilter), new QueryBitSetProducer(parentFilter), ScoreMode.None);
    Sort sort = new Sort(new SortField("text", nestedComparatorSource));
    TopFieldDocs topDocs = searcher.search(query, randomIntBetween(1, numParents), sort);
    assertTrue(topDocs.scoreDocs.length > 0);
    BytesRef previous = null;
    for (int i = 0; i < topDocs.scoreDocs.length; ++i) {
        final int docID = topDocs.scoreDocs[i].doc;
        assertTrue("expected " + docID + " to be a parent", parents.get(docID));
        BytesRef cmpValue = null;
        for (int child = parents.prevSetBit(docID - 1) + 1; child < docID; ++child) {
            String[] sVals = searcher.doc(child).getValues("text");
            final BytesRef[] vals;
            if (sVals.length == 0) {
                vals = new BytesRef[0];
            } else {
                vals = new BytesRef[sVals.length];
                for (int j = 0; j < vals.length; ++j) {
                    vals[j] = new BytesRef(sVals[j]);
                }
            }
            for (BytesRef value : vals) {
                if (cmpValue == null) {
                    cmpValue = value;
                } else if (sortMode == MultiValueMode.MIN && value.compareTo(cmpValue) < 0) {
                    cmpValue = value;
                } else if (sortMode == MultiValueMode.MAX && value.compareTo(cmpValue) > 0) {
                    cmpValue = value;
                }
            }
        }
        if (cmpValue == null) {
            if ("_first".equals(missingValue)) {
                cmpValue = new BytesRef();
            } else if ("_last".equals(missingValue) == false) {
                cmpValue = (BytesRef) missingValue;
            }
        }
        if (previous != null && cmpValue != null) {
            assertTrue(previous.utf8ToString() + "   /   " + cmpValue.utf8ToString(), previous.compareTo(cmpValue) <= 0);
        }
        previous = cmpValue;
    }
    searcher.getIndexReader().close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) Query(org.apache.lucene.search.Query) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) TermQuery(org.apache.lucene.search.TermQuery) ToParentBlockJoinQuery(org.apache.lucene.search.join.ToParentBlockJoinQuery) ArrayList(java.util.ArrayList) Nested(org.elasticsearch.index.fielddata.IndexFieldData.XFieldComparatorSource.Nested) TopFieldDocs(org.apache.lucene.search.TopFieldDocs) SortField(org.apache.lucene.search.SortField) Document(org.apache.lucene.document.Document) ShardId(org.elasticsearch.index.shard.ShardId) FixedBitSet(org.apache.lucene.util.FixedBitSet) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) QueryBitSetProducer(org.apache.lucene.search.join.QueryBitSetProducer) Sort(org.apache.lucene.search.Sort) BytesRef(org.apache.lucene.util.BytesRef) TermQuery(org.apache.lucene.search.TermQuery) ElasticsearchDirectoryReader(org.elasticsearch.common.lucene.index.ElasticsearchDirectoryReader) DirectoryReader(org.apache.lucene.index.DirectoryReader) BytesRefFieldComparatorSource(org.elasticsearch.index.fielddata.fieldcomparator.BytesRefFieldComparatorSource) Term(org.apache.lucene.index.Term) ToParentBlockJoinQuery(org.apache.lucene.search.join.ToParentBlockJoinQuery) StringField(org.apache.lucene.document.StringField)

Aggregations

ShardId (org.elasticsearch.index.shard.ShardId)293 ClusterState (org.elasticsearch.cluster.ClusterState)60 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)60 Index (org.elasticsearch.index.Index)59 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)45 ArrayList (java.util.ArrayList)42 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)41 IndexShardRoutingTable (org.elasticsearch.cluster.routing.IndexShardRoutingTable)37 HashMap (java.util.HashMap)36 IOException (java.io.IOException)31 UnassignedInfo (org.elasticsearch.cluster.routing.UnassignedInfo)30 Document (org.apache.lucene.document.Document)28 DirectoryReader (org.apache.lucene.index.DirectoryReader)28 Directory (org.apache.lucene.store.Directory)28 PlainActionFuture (org.elasticsearch.action.support.PlainActionFuture)28 Path (java.nio.file.Path)26 Settings (org.elasticsearch.common.settings.Settings)26 IndexWriter (org.apache.lucene.index.IndexWriter)24 ElasticsearchDirectoryReader (org.elasticsearch.common.lucene.index.ElasticsearchDirectoryReader)24 IndexShard (org.elasticsearch.index.shard.IndexShard)24