Search in sources :

Example 46 with StreamInput

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

the class IndexShardTests method testShardStats.

public void testShardStats() throws IOException {
    IndexShard shard = newStartedShard();
    ShardStats stats = new ShardStats(shard.routingEntry(), shard.shardPath(), new CommonStats(new IndicesQueryCache(Settings.EMPTY), shard, new CommonStatsFlags()), shard.commitStats(), shard.seqNoStats());
    assertEquals(shard.shardPath().getRootDataPath().toString(), stats.getDataPath());
    assertEquals(shard.shardPath().getRootStatePath().toString(), stats.getStatePath());
    assertEquals(shard.shardPath().isCustomDataPath(), stats.isCustomDataPath());
    if (randomBoolean() || true) {
        // try to serialize it to ensure values survive the serialization
        BytesStreamOutput out = new BytesStreamOutput();
        stats.writeTo(out);
        StreamInput in = out.bytes().streamInput();
        stats = ShardStats.readShardStats(in);
    }
    XContentBuilder builder = jsonBuilder();
    builder.startObject();
    stats.toXContent(builder, EMPTY_PARAMS);
    builder.endObject();
    String xContent = builder.string();
    StringBuilder expectedSubSequence = new StringBuilder("\"shard_path\":{\"state_path\":\"");
    expectedSubSequence.append(shard.shardPath().getRootStatePath().toString());
    expectedSubSequence.append("\",\"data_path\":\"");
    expectedSubSequence.append(shard.shardPath().getRootDataPath().toString());
    expectedSubSequence.append("\",\"is_custom_data_path\":").append(shard.shardPath().isCustomDataPath()).append("}");
    if (Constants.WINDOWS) {
    // Some path weirdness on windows
    } else {
        assertTrue(xContent.contains(expectedSubSequence));
    }
    closeShards(shard);
}
Also used : ShardStats(org.elasticsearch.action.admin.indices.stats.ShardStats) IndicesQueryCache(org.elasticsearch.indices.IndicesQueryCache) CommonStats(org.elasticsearch.action.admin.indices.stats.CommonStats) CommonStatsFlags(org.elasticsearch.action.admin.indices.stats.CommonStatsFlags) StreamInput(org.elasticsearch.common.io.stream.StreamInput) Matchers.containsString(org.hamcrest.Matchers.containsString) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 47 with StreamInput

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

the class TranslogTests method testTranslogOpSerialization.

public void testTranslogOpSerialization() throws Exception {
    BytesReference B_1 = new BytesArray(new byte[] { 1 });
    SeqNoFieldMapper.SequenceID seqID = SeqNoFieldMapper.SequenceID.emptySeqID();
    assert Version.CURRENT.major <= 6 : "Using UNASSIGNED_SEQ_NO can be removed in 7.0, because 6.0+ nodes have actual sequence numbers";
    long randomSeqNum = randomBoolean() ? SequenceNumbersService.UNASSIGNED_SEQ_NO : randomNonNegativeLong();
    long randomPrimaryTerm = randomBoolean() ? 0 : randomNonNegativeLong();
    seqID.seqNo.setLongValue(randomSeqNum);
    seqID.seqNoDocValue.setLongValue(randomSeqNum);
    seqID.primaryTerm.setLongValue(randomPrimaryTerm);
    Field uidField = new Field("_uid", Uid.createUid("test", "1"), UidFieldMapper.Defaults.FIELD_TYPE);
    Field versionField = new NumericDocValuesField("_version", 1);
    Document document = new Document();
    document.add(new TextField("value", "test", Field.Store.YES));
    document.add(uidField);
    document.add(versionField);
    document.add(seqID.seqNo);
    document.add(seqID.seqNoDocValue);
    document.add(seqID.primaryTerm);
    ParsedDocument doc = new ParsedDocument(versionField, seqID, "1", "type", null, Arrays.asList(document), B_1, XContentType.JSON, null);
    Engine.Index eIndex = new Engine.Index(newUid(doc), doc, randomSeqNum, randomPrimaryTerm, 1, VersionType.INTERNAL, Origin.PRIMARY, 0, 0, false);
    Engine.IndexResult eIndexResult = new Engine.IndexResult(1, randomSeqNum, true);
    Translog.Index index = new Translog.Index(eIndex, eIndexResult);
    BytesStreamOutput out = new BytesStreamOutput();
    index.writeTo(out);
    StreamInput in = out.bytes().streamInput();
    Translog.Index serializedIndex = new Translog.Index(in);
    assertEquals(index, serializedIndex);
    Engine.Delete eDelete = new Engine.Delete(doc.type(), doc.id(), newUid(doc), randomSeqNum, randomPrimaryTerm, 2, VersionType.INTERNAL, Origin.PRIMARY, 0);
    Engine.DeleteResult eDeleteResult = new Engine.DeleteResult(2, randomSeqNum, true);
    Translog.Delete delete = new Translog.Delete(eDelete, eDeleteResult);
    out = new BytesStreamOutput();
    delete.writeTo(out);
    in = out.bytes().streamInput();
    Translog.Delete serializedDelete = new Translog.Delete(in);
    assertEquals(delete, serializedDelete);
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) SeqNoFieldMapper(org.elasticsearch.index.mapper.SeqNoFieldMapper) BytesArray(org.elasticsearch.common.bytes.BytesArray) Document(org.elasticsearch.index.mapper.ParseContext.Document) ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) Field(org.apache.lucene.document.Field) TextField(org.apache.lucene.document.TextField) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) StreamInput(org.elasticsearch.common.io.stream.StreamInput) TextField(org.apache.lucene.document.TextField) Engine(org.elasticsearch.index.engine.Engine)

Example 48 with StreamInput

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

the class TranslogTests method stats.

protected TranslogStats stats() throws IOException {
    // force flushing and updating of stats
    translog.sync();
    TranslogStats stats = translog.stats();
    if (randomBoolean()) {
        BytesStreamOutput out = new BytesStreamOutput();
        stats.writeTo(out);
        StreamInput in = out.bytes().streamInput();
        stats = new TranslogStats();
        stats.readFrom(in);
    }
    return stats;
}
Also used : StreamInput(org.elasticsearch.common.io.stream.StreamInput) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput)

Example 49 with StreamInput

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

the class TermsLookupTests method testSerialization.

public void testSerialization() throws IOException {
    TermsLookup termsLookup = randomTermsLookup();
    try (BytesStreamOutput output = new BytesStreamOutput()) {
        termsLookup.writeTo(output);
        try (StreamInput in = output.bytes().streamInput()) {
            TermsLookup deserializedLookup = new TermsLookup(in);
            assertEquals(deserializedLookup, termsLookup);
            assertEquals(deserializedLookup.hashCode(), termsLookup.hashCode());
            assertNotSame(deserializedLookup, termsLookup);
        }
    }
}
Also used : StreamInput(org.elasticsearch.common.io.stream.StreamInput) TermsLookup(org.elasticsearch.indices.TermsLookup) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput)

Example 50 with StreamInput

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

the class OsStatsTests method testSerialization.

public void testSerialization() throws IOException {
    int numLoadAverages = randomIntBetween(1, 5);
    double[] loadAverages = new double[numLoadAverages];
    for (int i = 0; i < loadAverages.length; i++) {
        loadAverages[i] = randomDouble();
    }
    OsStats.Cpu cpu = new OsStats.Cpu(randomShort(), loadAverages);
    OsStats.Mem mem = new OsStats.Mem(randomLong(), randomLong());
    OsStats.Swap swap = new OsStats.Swap(randomLong(), randomLong());
    OsStats.Cgroup cgroup = new OsStats.Cgroup(randomAsciiOfLength(8), randomNonNegativeLong(), randomAsciiOfLength(8), randomNonNegativeLong(), randomNonNegativeLong(), new OsStats.Cgroup.CpuStat(randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong()));
    OsStats osStats = new OsStats(System.currentTimeMillis(), cpu, mem, swap, cgroup);
    try (BytesStreamOutput out = new BytesStreamOutput()) {
        osStats.writeTo(out);
        try (StreamInput in = out.bytes().streamInput()) {
            OsStats deserializedOsStats = new OsStats(in);
            assertEquals(osStats.getTimestamp(), deserializedOsStats.getTimestamp());
            assertEquals(osStats.getCpu().getPercent(), deserializedOsStats.getCpu().getPercent());
            assertArrayEquals(osStats.getCpu().getLoadAverage(), deserializedOsStats.getCpu().getLoadAverage(), 0);
            assertEquals(osStats.getMem().getFree(), deserializedOsStats.getMem().getFree());
            assertEquals(osStats.getMem().getTotal(), deserializedOsStats.getMem().getTotal());
            assertEquals(osStats.getSwap().getFree(), deserializedOsStats.getSwap().getFree());
            assertEquals(osStats.getSwap().getTotal(), deserializedOsStats.getSwap().getTotal());
            assertEquals(osStats.getCgroup().getCpuAcctControlGroup(), deserializedOsStats.getCgroup().getCpuAcctControlGroup());
            assertEquals(osStats.getCgroup().getCpuAcctUsageNanos(), deserializedOsStats.getCgroup().getCpuAcctUsageNanos());
            assertEquals(osStats.getCgroup().getCpuControlGroup(), deserializedOsStats.getCgroup().getCpuControlGroup());
            assertEquals(osStats.getCgroup().getCpuCfsPeriodMicros(), deserializedOsStats.getCgroup().getCpuCfsPeriodMicros());
            assertEquals(osStats.getCgroup().getCpuCfsQuotaMicros(), deserializedOsStats.getCgroup().getCpuCfsQuotaMicros());
            assertEquals(osStats.getCgroup().getCpuStat().getNumberOfElapsedPeriods(), deserializedOsStats.getCgroup().getCpuStat().getNumberOfElapsedPeriods());
            assertEquals(osStats.getCgroup().getCpuStat().getNumberOfTimesThrottled(), deserializedOsStats.getCgroup().getCpuStat().getNumberOfTimesThrottled());
            assertEquals(osStats.getCgroup().getCpuStat().getTimeThrottledNanos(), deserializedOsStats.getCgroup().getCpuStat().getTimeThrottledNanos());
        }
    }
}
Also used : BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) StreamInput(org.elasticsearch.common.io.stream.StreamInput)

Aggregations

StreamInput (org.elasticsearch.common.io.stream.StreamInput)183 BytesStreamOutput (org.elasticsearch.common.io.stream.BytesStreamOutput)146 Test (org.junit.Test)52 CrateUnitTest (io.crate.test.integration.CrateUnitTest)37 NamedWriteableAwareStreamInput (org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput)30 BytesArray (org.elasticsearch.common.bytes.BytesArray)24 Version (org.elasticsearch.Version)21 IOException (java.io.IOException)13 BytesReference (org.elasticsearch.common.bytes.BytesReference)10 UUID (java.util.UUID)9 NamedWriteableRegistry (org.elasticsearch.common.io.stream.NamedWriteableRegistry)9 Symbol (io.crate.analyze.symbol.Symbol)8 BytesRef (org.apache.lucene.util.BytesRef)8 ArrayList (java.util.ArrayList)7 HashMap (java.util.HashMap)6 Map (java.util.Map)6 ShardId (org.elasticsearch.index.shard.ShardId)6 List (java.util.List)5 AliasFilter (org.elasticsearch.search.internal.AliasFilter)5 Aggregation (io.crate.analyze.symbol.Aggregation)4