Search in sources :

Example 36 with BytesStreamOutput

use of org.elasticsearch.common.io.stream.BytesStreamOutput in project elasticsearch by elastic.

the class IndexMetaDataTests method testIndexMetaDataSerialization.

public void testIndexMetaDataSerialization() throws IOException {
    Integer numShard = randomFrom(1, 2, 4, 8, 16);
    int numberOfReplicas = randomIntBetween(0, 10);
    IndexMetaData metaData = IndexMetaData.builder("foo").settings(Settings.builder().put("index.version.created", 1).put("index.number_of_shards", numShard).put("index.number_of_replicas", numberOfReplicas).build()).creationDate(randomLong()).primaryTerm(0, 2).setRoutingNumShards(32).build();
    final XContentBuilder builder = JsonXContent.contentBuilder();
    builder.startObject();
    metaData.toXContent(builder, ToXContent.EMPTY_PARAMS);
    builder.endObject();
    XContentParser parser = createParser(JsonXContent.jsonXContent, builder.bytes());
    final IndexMetaData fromXContentMeta = IndexMetaData.fromXContent(parser);
    assertEquals(metaData, fromXContentMeta);
    assertEquals(metaData.hashCode(), fromXContentMeta.hashCode());
    assertEquals(metaData.getNumberOfReplicas(), fromXContentMeta.getNumberOfReplicas());
    assertEquals(metaData.getNumberOfShards(), fromXContentMeta.getNumberOfShards());
    assertEquals(metaData.getCreationVersion(), fromXContentMeta.getCreationVersion());
    assertEquals(metaData.getRoutingNumShards(), fromXContentMeta.getRoutingNumShards());
    assertEquals(metaData.getCreationDate(), fromXContentMeta.getCreationDate());
    assertEquals(metaData.getRoutingFactor(), fromXContentMeta.getRoutingFactor());
    assertEquals(metaData.primaryTerm(0), fromXContentMeta.primaryTerm(0));
    final BytesStreamOutput out = new BytesStreamOutput();
    metaData.writeTo(out);
    IndexMetaData deserialized = IndexMetaData.readFrom(out.bytes().streamInput());
    assertEquals(metaData, deserialized);
    assertEquals(metaData.hashCode(), deserialized.hashCode());
    assertEquals(metaData.getNumberOfReplicas(), deserialized.getNumberOfReplicas());
    assertEquals(metaData.getNumberOfShards(), deserialized.getNumberOfShards());
    assertEquals(metaData.getCreationVersion(), deserialized.getCreationVersion());
    assertEquals(metaData.getRoutingNumShards(), deserialized.getRoutingNumShards());
    assertEquals(metaData.getCreationDate(), deserialized.getCreationDate());
    assertEquals(metaData.getRoutingFactor(), deserialized.getRoutingFactor());
    assertEquals(metaData.primaryTerm(0), deserialized.primaryTerm(0));
}
Also used : XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) XContentParser(org.elasticsearch.common.xcontent.XContentParser) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput)

Example 37 with BytesStreamOutput

use of org.elasticsearch.common.io.stream.BytesStreamOutput in project elasticsearch by elastic.

the class ClusterBlockTests method testSerialization.

public void testSerialization() throws Exception {
    int iterations = randomIntBetween(10, 100);
    for (int i = 0; i < iterations; i++) {
        // Get a random version
        Version version = randomVersion(random());
        // Get a random list of ClusterBlockLevels
        EnumSet<ClusterBlockLevel> levels = EnumSet.noneOf(ClusterBlockLevel.class);
        int nbLevels = randomIntBetween(1, ClusterBlockLevel.values().length);
        for (int j = 0; j < nbLevels; j++) {
            levels.add(randomFrom(ClusterBlockLevel.values()));
        }
        ClusterBlock clusterBlock = new ClusterBlock(randomInt(), "cluster block #" + randomInt(), randomBoolean(), randomBoolean(), randomFrom(RestStatus.values()), levels);
        BytesStreamOutput out = new BytesStreamOutput();
        out.setVersion(version);
        clusterBlock.writeTo(out);
        StreamInput in = out.bytes().streamInput();
        in.setVersion(version);
        ClusterBlock result = ClusterBlock.readClusterBlock(in);
        assertThat(result.id(), equalTo(clusterBlock.id()));
        assertThat(result.status(), equalTo(clusterBlock.status()));
        assertThat(result.description(), equalTo(clusterBlock.description()));
        assertThat(result.retryable(), equalTo(clusterBlock.retryable()));
        assertThat(result.disableStatePersistence(), equalTo(clusterBlock.disableStatePersistence()));
        assertArrayEquals(result.levels().toArray(), clusterBlock.levels().toArray());
    }
}
Also used : Version(org.elasticsearch.Version) VersionUtils.randomVersion(org.elasticsearch.test.VersionUtils.randomVersion) StreamInput(org.elasticsearch.common.io.stream.StreamInput) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput)

Example 38 with BytesStreamOutput

use of org.elasticsearch.common.io.stream.BytesStreamOutput in project elasticsearch by elastic.

the class ClusterStateHealthTests method maybeSerialize.

ClusterStateHealth maybeSerialize(ClusterStateHealth clusterStateHealth) throws IOException {
    if (randomBoolean()) {
        BytesStreamOutput out = new BytesStreamOutput();
        clusterStateHealth.writeTo(out);
        StreamInput in = out.bytes().streamInput();
        clusterStateHealth = new ClusterStateHealth(in);
    }
    return clusterStateHealth;
}
Also used : StreamInput(org.elasticsearch.common.io.stream.StreamInput) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput)

Example 39 with BytesStreamOutput

use of org.elasticsearch.common.io.stream.BytesStreamOutput in project elasticsearch by elastic.

the class PublishClusterStateAction method serializeFullClusterState.

public static BytesReference serializeFullClusterState(ClusterState clusterState, Version nodeVersion) throws IOException {
    BytesStreamOutput bStream = new BytesStreamOutput();
    try (StreamOutput stream = CompressorFactory.COMPRESSOR.streamOutput(bStream)) {
        stream.setVersion(nodeVersion);
        stream.writeBoolean(true);
        clusterState.writeTo(stream);
    }
    return bStream.bytes();
}
Also used : StreamOutput(org.elasticsearch.common.io.stream.StreamOutput) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput)

Example 40 with BytesStreamOutput

use of org.elasticsearch.common.io.stream.BytesStreamOutput in project elasticsearch by elastic.

the class IndexShard method doCheckIndex.

private void doCheckIndex() throws IOException {
    long timeNS = System.nanoTime();
    if (!Lucene.indexExists(store.directory())) {
        return;
    }
    BytesStreamOutput os = new BytesStreamOutput();
    PrintStream out = new PrintStream(os, false, StandardCharsets.UTF_8.name());
    if ("checksum".equals(checkIndexOnStartup)) {
        // physical verification only: verify all checksums for the latest commit
        IOException corrupt = null;
        MetadataSnapshot metadata = snapshotStoreMetadata();
        for (Map.Entry<String, StoreFileMetaData> entry : metadata.asMap().entrySet()) {
            try {
                Store.checkIntegrity(entry.getValue(), store.directory());
                out.println("checksum passed: " + entry.getKey());
            } catch (IOException exc) {
                out.println("checksum failed: " + entry.getKey());
                exc.printStackTrace(out);
                corrupt = exc;
            }
        }
        out.flush();
        if (corrupt != null) {
            logger.warn("check index [failure]\n{}", os.bytes().utf8ToString());
            throw corrupt;
        }
    } else {
        // full checkindex
        try (CheckIndex checkIndex = new CheckIndex(store.directory())) {
            checkIndex.setInfoStream(out);
            CheckIndex.Status status = checkIndex.checkIndex();
            out.flush();
            if (!status.clean) {
                if (state == IndexShardState.CLOSED) {
                    // ignore if closed....
                    return;
                }
                logger.warn("check index [failure]\n{}", os.bytes().utf8ToString());
                if ("fix".equals(checkIndexOnStartup)) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("fixing index, writing new segments file ...");
                    }
                    checkIndex.exorciseIndex(status);
                    if (logger.isDebugEnabled()) {
                        logger.debug("index fixed, wrote new segments file \"{}\"", status.segmentsFileName);
                    }
                } else {
                    // only throw a failure if we are not going to fix the index
                    throw new IllegalStateException("index check failure but can't fix it");
                }
            }
        }
    }
    if (logger.isDebugEnabled()) {
        logger.debug("check index [success]\n{}", os.bytes().utf8ToString());
    }
    recoveryState.getVerifyIndex().checkIndexTime(Math.max(0, TimeValue.nsecToMSec(System.nanoTime() - timeNS)));
}
Also used : PrintStream(java.io.PrintStream) StoreFileMetaData(org.elasticsearch.index.store.StoreFileMetaData) IOException(java.io.IOException) Map(java.util.Map) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) CheckIndex(org.apache.lucene.index.CheckIndex) MetadataSnapshot(org.elasticsearch.index.store.Store.MetadataSnapshot)

Aggregations

BytesStreamOutput (org.elasticsearch.common.io.stream.BytesStreamOutput)222 StreamInput (org.elasticsearch.common.io.stream.StreamInput)147 Test (org.junit.Test)45 CrateUnitTest (io.crate.test.integration.CrateUnitTest)36 NamedWriteableAwareStreamInput (org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput)24 IOException (java.io.IOException)21 BytesArray (org.elasticsearch.common.bytes.BytesArray)21 BytesReference (org.elasticsearch.common.bytes.BytesReference)18 Version (org.elasticsearch.Version)15 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)14 ArrayList (java.util.ArrayList)12 BytesRef (org.apache.lucene.util.BytesRef)11 Map (java.util.Map)10 UUID (java.util.UUID)9 Symbol (io.crate.analyze.symbol.Symbol)8 HashMap (java.util.HashMap)8 NamedWriteableRegistry (org.elasticsearch.common.io.stream.NamedWriteableRegistry)8 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)7 StreamOutput (org.elasticsearch.common.io.stream.StreamOutput)7 List (java.util.List)6