Search in sources :

Example 96 with Version

use of org.elasticsearch.Version in project elasticsearch by elastic.

the class IndexShardTests method testRestoreShard.

public void testRestoreShard() throws IOException {
    final IndexShard source = newStartedShard(true);
    IndexShard target = newStartedShard(true);
    indexDoc(source, "test", "0");
    if (randomBoolean()) {
        source.refresh("test");
    }
    indexDoc(target, "test", "1");
    target.refresh("test");
    assertDocs(target, new Uid("test", "1"));
    // only flush source
    flushShard(source);
    final ShardRouting origRouting = target.routingEntry();
    ShardRouting routing = ShardRoutingHelper.reinitPrimary(origRouting);
    final Snapshot snapshot = new Snapshot("foo", new SnapshotId("bar", UUIDs.randomBase64UUID()));
    routing = ShardRoutingHelper.newWithRestoreSource(routing, new RecoverySource.SnapshotRecoverySource(snapshot, Version.CURRENT, "test"));
    target = reinitShard(target, routing);
    Store sourceStore = source.store();
    Store targetStore = target.store();
    DiscoveryNode localNode = new DiscoveryNode("foo", buildNewFakeTransportAddress(), emptyMap(), emptySet(), Version.CURRENT);
    target.markAsRecovering("store", new RecoveryState(routing, localNode, null));
    assertTrue(target.restoreFromRepository(new RestoreOnlyRepository("test") {

        @Override
        public void restoreShard(IndexShard shard, SnapshotId snapshotId, Version version, IndexId indexId, ShardId snapshotShardId, RecoveryState recoveryState) {
            try {
                cleanLuceneIndex(targetStore.directory());
                for (String file : sourceStore.directory().listAll()) {
                    if (file.equals("write.lock") || file.startsWith("extra")) {
                        continue;
                    }
                    targetStore.directory().copyFrom(sourceStore.directory(), file, file, IOContext.DEFAULT);
                }
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        }
    }));
    target.updateRoutingEntry(routing.moveToStarted());
    assertDocs(target, new Uid("test", "0"));
    closeShards(source, target);
}
Also used : IndexId(org.elasticsearch.repositories.IndexId) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) Store(org.elasticsearch.index.store.Store) Matchers.containsString(org.hamcrest.Matchers.containsString) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) EngineException(org.elasticsearch.index.engine.EngineException) IOException(java.io.IOException) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) ExecutionException(java.util.concurrent.ExecutionException) CorruptIndexException(org.apache.lucene.index.CorruptIndexException) Uid(org.elasticsearch.index.mapper.Uid) Snapshot(org.elasticsearch.snapshots.Snapshot) SnapshotId(org.elasticsearch.snapshots.SnapshotId) Version(org.elasticsearch.Version) TestShardRouting(org.elasticsearch.cluster.routing.TestShardRouting) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) RecoveryState(org.elasticsearch.indices.recovery.RecoveryState)

Example 97 with Version

use of org.elasticsearch.Version in project elasticsearch by elastic.

the class SignificanceHeuristicTests method testStreamResponse.

// test that stream output can actually be read - does not replace bwc test
public void testStreamResponse() throws Exception {
    Version version = randomVersion(random());
    InternalMappedSignificantTerms<?, ?> sigTerms = getRandomSignificantTerms(getRandomSignificanceheuristic());
    // write
    ByteArrayOutputStream outBuffer = new ByteArrayOutputStream();
    OutputStreamStreamOutput out = new OutputStreamStreamOutput(outBuffer);
    out.setVersion(version);
    out.writeNamedWriteable(sigTerms);
    // read
    ByteArrayInputStream inBuffer = new ByteArrayInputStream(outBuffer.toByteArray());
    StreamInput in = new InputStreamStreamInput(inBuffer);
    // populates the registry through side effects
    SearchModule searchModule = new SearchModule(Settings.EMPTY, false, emptyList());
    NamedWriteableRegistry registry = new NamedWriteableRegistry(searchModule.getNamedWriteables());
    in = new NamedWriteableAwareStreamInput(in, registry);
    in.setVersion(version);
    InternalMappedSignificantTerms<?, ?> read = (InternalMappedSignificantTerms<?, ?>) in.readNamedWriteable(InternalAggregation.class);
    assertEquals(sigTerms.significanceHeuristic, read.significanceHeuristic);
    SignificantTerms.Bucket originalBucket = sigTerms.getBuckets().get(0);
    SignificantTerms.Bucket streamedBucket = read.getBuckets().get(0);
    assertThat(originalBucket.getKeyAsString(), equalTo(streamedBucket.getKeyAsString()));
    assertThat(originalBucket.getSupersetDf(), equalTo(streamedBucket.getSupersetDf()));
    assertThat(originalBucket.getSubsetDf(), equalTo(streamedBucket.getSubsetDf()));
    assertThat(streamedBucket.getSubsetSize(), equalTo(10L));
    assertThat(streamedBucket.getSupersetSize(), equalTo(20L));
}
Also used : NamedWriteableRegistry(org.elasticsearch.common.io.stream.NamedWriteableRegistry) ByteArrayOutputStream(java.io.ByteArrayOutputStream) InternalAggregation(org.elasticsearch.search.aggregations.InternalAggregation) OutputStreamStreamOutput(org.elasticsearch.common.io.stream.OutputStreamStreamOutput) Version(org.elasticsearch.Version) VersionUtils.randomVersion(org.elasticsearch.test.VersionUtils.randomVersion) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStreamStreamInput(org.elasticsearch.common.io.stream.InputStreamStreamInput) NamedWriteableAwareStreamInput(org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput) StreamInput(org.elasticsearch.common.io.stream.StreamInput) SearchModule(org.elasticsearch.search.SearchModule) NamedWriteableAwareStreamInput(org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput) InputStreamStreamInput(org.elasticsearch.common.io.stream.InputStreamStreamInput)

Example 98 with Version

use of org.elasticsearch.Version in project elasticsearch by elastic.

the class PercolateQueryBuilderTests method testSerializationBwc.

public void testSerializationBwc() throws IOException {
    final byte[] data = Base64.getDecoder().decode("P4AAAAAFZmllbGQEdHlwZQAAAAAAAA57ImZvbyI6ImJhciJ9AAAAAA==");
    final Version version = randomFrom(Version.V_5_0_0, Version.V_5_0_1, Version.V_5_0_2, Version.V_5_0_3_UNRELEASED, Version.V_5_1_1_UNRELEASED, Version.V_5_1_2_UNRELEASED, Version.V_5_2_0_UNRELEASED);
    try (StreamInput in = StreamInput.wrap(data)) {
        in.setVersion(version);
        PercolateQueryBuilder queryBuilder = new PercolateQueryBuilder(in);
        assertEquals("type", queryBuilder.getDocumentType());
        assertEquals("field", queryBuilder.getField());
        assertEquals("{\"foo\":\"bar\"}", queryBuilder.getDocument().utf8ToString());
        assertEquals(XContentType.JSON, queryBuilder.getXContentType());
        try (BytesStreamOutput out = new BytesStreamOutput()) {
            out.setVersion(version);
            queryBuilder.writeTo(out);
            assertArrayEquals(data, out.bytes().toBytesRef().bytes);
        }
    }
}
Also used : Version(org.elasticsearch.Version) StreamInput(org.elasticsearch.common.io.stream.StreamInput) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput)

Example 99 with Version

use of org.elasticsearch.Version in project elasticsearch by elastic.

the class ESClientYamlSuiteTestCase method initAndResetContext.

@Before
public void initAndResetContext() throws IOException {
    if (restTestExecutionContext == null) {
        assert adminExecutionContext == null;
        assert blacklistPathMatchers == null;
        String[] specPaths = resolvePathsProperty(REST_TESTS_SPEC, DEFAULT_SPEC_PATH);
        ClientYamlSuiteRestSpec restSpec = null;
        FileSystem fileSystem = getFileSystem();
        // ... and you can't close() the default filesystem
        try {
            restSpec = ClientYamlSuiteRestSpec.parseFrom(fileSystem, DEFAULT_SPEC_PATH, specPaths);
        } finally {
            IOUtils.close(fileSystem);
        }
        validateSpec(restSpec);
        List<HttpHost> hosts = getClusterHosts();
        RestClient restClient = client();
        Version infoVersion = readVersionsFromInfo(restClient, hosts.size());
        Version esVersion;
        try {
            Tuple<Version, Version> versionVersionTuple = readVersionsFromCatNodes(restClient);
            esVersion = versionVersionTuple.v1();
            Version masterVersion = versionVersionTuple.v2();
            logger.info("initializing yaml client, minimum es version: [{}] master version: [{}] hosts: {}", esVersion, masterVersion, hosts);
        } catch (ResponseException ex) {
            if (ex.getResponse().getStatusLine().getStatusCode() == 403) {
                logger.warn("Fallback to simple info '/' request, _cat/nodes is not authorized");
                esVersion = infoVersion;
                logger.info("initializing yaml client, minimum es version: [{}] hosts: {}", esVersion, hosts);
            } else {
                throw ex;
            }
        }
        ClientYamlTestClient clientYamlTestClient = new ClientYamlTestClient(restSpec, restClient, hosts, esVersion);
        restTestExecutionContext = new ClientYamlTestExecutionContext(clientYamlTestClient, randomizeContentType());
        adminExecutionContext = new ClientYamlTestExecutionContext(clientYamlTestClient, false);
        String[] blacklist = resolvePathsProperty(REST_TESTS_BLACKLIST, null);
        blacklistPathMatchers = new ArrayList<>();
        for (String entry : blacklist) {
            blacklistPathMatchers.add(new BlacklistedPathPatternMatcher(entry));
        }
    }
    assert restTestExecutionContext != null;
    assert adminExecutionContext != null;
    assert blacklistPathMatchers != null;
    // admin context must be available for @After always, regardless of whether the test was blacklisted
    adminExecutionContext.clear();
    //skip test if it matches one of the blacklist globs
    for (BlacklistedPathPatternMatcher blacklistedPathMatcher : blacklistPathMatchers) {
        String testPath = testCandidate.getSuitePath() + "/" + testCandidate.getTestSection().getName();
        assumeFalse("[" + testCandidate.getTestPath() + "] skipped, reason: blacklisted", blacklistedPathMatcher.isSuffixMatch(testPath));
    }
    restTestExecutionContext.clear();
    //skip test if the whole suite (yaml file) is disabled
    assumeFalse(testCandidate.getSetupSection().getSkipSection().getSkipMessage(testCandidate.getSuitePath()), testCandidate.getSetupSection().getSkipSection().skip(restTestExecutionContext.esVersion()));
    //skip test if the whole suite (yaml file) is disabled
    assumeFalse(testCandidate.getTeardownSection().getSkipSection().getSkipMessage(testCandidate.getSuitePath()), testCandidate.getTeardownSection().getSkipSection().skip(restTestExecutionContext.esVersion()));
    //skip test if test section is disabled
    assumeFalse(testCandidate.getTestSection().getSkipSection().getSkipMessage(testCandidate.getTestPath()), testCandidate.getTestSection().getSkipSection().skip(restTestExecutionContext.esVersion()));
}
Also used : ResponseException(org.elasticsearch.client.ResponseException) RestClient(org.elasticsearch.client.RestClient) ClientYamlSuiteRestSpec(org.elasticsearch.test.rest.yaml.restspec.ClientYamlSuiteRestSpec) Version(org.elasticsearch.Version) HttpHost(org.apache.http.HttpHost) FileSystem(java.nio.file.FileSystem) Before(org.junit.Before)

Example 100 with Version

use of org.elasticsearch.Version in project elasticsearch by elastic.

the class ESClientYamlSuiteTestCase method readVersionsFromCatNodes.

private static Tuple<Version, Version> readVersionsFromCatNodes(RestClient restClient) throws IOException {
    // we simply go to the _cat/nodes API and parse all versions in the cluster
    Response response = restClient.performRequest("GET", "/_cat/nodes", Collections.singletonMap("h", "version,master"));
    ClientYamlTestResponse restTestResponse = new ClientYamlTestResponse(response);
    String nodesCatResponse = restTestResponse.getBodyAsString();
    String[] split = nodesCatResponse.split("\n");
    Version version = null;
    Version masterVersion = null;
    for (String perNode : split) {
        final String[] versionAndMaster = perNode.split("\\s+");
        assert versionAndMaster.length == 2 : "invalid line: " + perNode + " length: " + versionAndMaster.length;
        final Version currentVersion = Version.fromString(versionAndMaster[0]);
        final boolean master = versionAndMaster[1].trim().equals("*");
        if (master) {
            assert masterVersion == null;
            masterVersion = currentVersion;
        }
        if (version == null) {
            version = currentVersion;
        } else if (version.onOrAfter(currentVersion)) {
            version = currentVersion;
        }
    }
    return new Tuple<>(version, masterVersion);
}
Also used : Response(org.elasticsearch.client.Response) Version(org.elasticsearch.Version) Tuple(org.elasticsearch.common.collect.Tuple)

Aggregations

Version (org.elasticsearch.Version)124 Settings (org.elasticsearch.common.settings.Settings)51 StreamInput (org.elasticsearch.common.io.stream.StreamInput)21 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)20 HashMap (java.util.HashMap)17 ArrayList (java.util.ArrayList)16 BytesStreamOutput (org.elasticsearch.common.io.stream.BytesStreamOutput)16 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)14 IOException (java.io.IOException)13 SearchResponse (org.elasticsearch.action.search.SearchResponse)12 List (java.util.List)11 Map (java.util.Map)11 ClusterState (org.elasticsearch.cluster.ClusterState)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)9 ClusterName (org.elasticsearch.cluster.ClusterName)9 TimeValue (org.elasticsearch.common.unit.TimeValue)9 VersionUtils.randomVersion (org.elasticsearch.test.VersionUtils.randomVersion)9 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)8 CompressedXContent (org.elasticsearch.common.compress.CompressedXContent)8 GeoPoint (org.elasticsearch.common.geo.GeoPoint)8