Search in sources :

Example 41 with StreamInput

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

the class TaskResultTests method testBinaryRoundTrip.

public void testBinaryRoundTrip() throws IOException {
    NamedWriteableRegistry registry = new NamedWriteableRegistry(Collections.singletonList(new NamedWriteableRegistry.Entry(Task.Status.class, RawTaskStatus.NAME, RawTaskStatus::new)));
    TaskResult result = randomTaskResult();
    TaskResult read;
    try (BytesStreamOutput out = new BytesStreamOutput()) {
        result.writeTo(out);
        try (StreamInput in = new NamedWriteableAwareStreamInput(out.bytes().streamInput(), registry)) {
            read = new TaskResult(in);
        }
    } catch (IOException e) {
        throw new IOException("Error processing [" + result + "]", e);
    }
    assertEquals(result, read);
}
Also used : NamedWriteableRegistry(org.elasticsearch.common.io.stream.NamedWriteableRegistry) NamedWriteableAwareStreamInput(org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput) StreamInput(org.elasticsearch.common.io.stream.StreamInput) NamedWriteableAwareStreamInput(org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput) IOException(java.io.IOException) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput)

Example 42 with StreamInput

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

the class ThreadPoolSerializationTests method testThatQueueSizeSerializationWorks.

public void testThatQueueSizeSerializationWorks() throws Exception {
    ThreadPool.Info info = new ThreadPool.Info("foo", threadPoolType, 1, 10, TimeValue.timeValueMillis(3000), SizeValue.parseSizeValue("10k"));
    output.setVersion(Version.CURRENT);
    info.writeTo(output);
    StreamInput input = output.bytes().streamInput();
    ThreadPool.Info newInfo = new ThreadPool.Info(input);
    assertThat(newInfo.getQueueSize().singles(), is(10000L));
}
Also used : StreamInput(org.elasticsearch.common.io.stream.StreamInput)

Example 43 with StreamInput

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

the class TCPTransportTests method testCompressRequest.

public void testCompressRequest() throws IOException {
    final boolean compressed = randomBoolean();
    final AtomicBoolean called = new AtomicBoolean(false);
    Req request = new Req(randomRealisticUnicodeOfLengthBetween(10, 100));
    ThreadPool threadPool = new TestThreadPool(TCPTransportTests.class.getName());
    try {
        TcpTransport transport = new TcpTransport("test", Settings.builder().put("transport.tcp.compress", compressed).build(), threadPool, new BigArrays(Settings.EMPTY, null), null, null, null) {

            @Override
            protected InetSocketAddress getLocalAddress(Object o) {
                return null;
            }

            @Override
            protected Object bind(String name, InetSocketAddress address) throws IOException {
                return null;
            }

            @Override
            protected void closeChannels(List channel) throws IOException {
            }

            @Override
            protected void sendMessage(Object o, BytesReference reference, Runnable sendListener) throws IOException {
                StreamInput streamIn = reference.streamInput();
                streamIn.skip(TcpHeader.MARKER_BYTES_SIZE);
                int len = streamIn.readInt();
                long requestId = streamIn.readLong();
                assertEquals(42, requestId);
                byte status = streamIn.readByte();
                Version version = Version.fromId(streamIn.readInt());
                assertEquals(Version.CURRENT, version);
                assertEquals(compressed, TransportStatus.isCompress(status));
                called.compareAndSet(false, true);
                if (compressed) {
                    final int bytesConsumed = TcpHeader.HEADER_SIZE;
                    streamIn = CompressorFactory.compressor(reference.slice(bytesConsumed, reference.length() - bytesConsumed)).streamInput(streamIn);
                }
                threadPool.getThreadContext().readHeaders(streamIn);
                assertEquals("foobar", streamIn.readString());
                Req readReq = new Req("");
                readReq.readFrom(streamIn);
                assertEquals(request.value, readReq.value);
            }

            @Override
            protected NodeChannels connectToChannels(DiscoveryNode node, ConnectionProfile profile) throws IOException {
                return new NodeChannels(node, new Object[profile.getNumConnections()], profile);
            }

            @Override
            protected boolean isOpen(Object o) {
                return false;
            }

            @Override
            public long serverOpen() {
                return 0;
            }

            @Override
            public NodeChannels getConnection(DiscoveryNode node) {
                return new NodeChannels(node, new Object[MockTcpTransport.LIGHT_PROFILE.getNumConnections()], MockTcpTransport.LIGHT_PROFILE);
            }
        };
        DiscoveryNode node = new DiscoveryNode("foo", buildNewFakeTransportAddress(), Version.CURRENT);
        Transport.Connection connection = transport.getConnection(node);
        connection.sendRequest(42, "foobar", request, TransportRequestOptions.EMPTY);
        assertTrue(called.get());
    } finally {
        ThreadPool.terminate(threadPool, 10, TimeUnit.SECONDS);
    }
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) InetSocketAddress(java.net.InetSocketAddress) TestThreadPool(org.elasticsearch.threadpool.TestThreadPool) ThreadPool(org.elasticsearch.threadpool.ThreadPool) TestThreadPool(org.elasticsearch.threadpool.TestThreadPool) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BigArrays(org.elasticsearch.common.util.BigArrays) Version(org.elasticsearch.Version) StreamInput(org.elasticsearch.common.io.stream.StreamInput) List(java.util.List)

Example 44 with StreamInput

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

the class RoundTripTests method roundTrip.

private void roundTrip(Version version, Streamable example, Streamable empty) throws IOException {
    BytesStreamOutput out = new BytesStreamOutput();
    out.setVersion(version);
    example.writeTo(out);
    StreamInput in = out.bytes().streamInput();
    in.setVersion(version);
    empty.readFrom(in);
}
Also used : StreamInput(org.elasticsearch.common.io.stream.StreamInput) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput)

Example 45 with StreamInput

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

the class FieldStatsTests method assertSerialization.

private void assertSerialization(FieldStats stats, Version version) throws IOException {
    BytesStreamOutput output = new BytesStreamOutput();
    output.setVersion(version);
    stats.writeTo(output);
    output.flush();
    StreamInput input = output.bytes().streamInput();
    input.setVersion(version);
    FieldStats deserializedStats = FieldStats.readFrom(input);
    assertEquals(stats, deserializedStats);
    assertEquals(stats.hashCode(), deserializedStats.hashCode());
}
Also used : StreamInput(org.elasticsearch.common.io.stream.StreamInput) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) FieldStats(org.elasticsearch.action.fieldstats.FieldStats)

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