Search in sources :

Example 1 with Serializer

use of net.morimekta.providence.serializer.Serializer in project providence by morimekta.

the class MessageStreamsTest method testFileCollector.

@Test
public void testFileCollector() throws IOException {
    File file = tmp.newFile("tmp");
    Collector<CompactFields, OutputStream, Integer> collector = MessageCollectors.toFile(file, new PrettySerializer().config());
    OutputStream out = mock(OutputStream.class);
    doThrow(new IOException("oops")).when(out).write(MessageStreams.READABLE_ENTRY_SEP);
    doThrow(new IOException("close")).when(out).close();
    assertThat(collector.combiner().apply(out, out), is(sameInstance(out)));
    try {
        collector.accumulator().accept(out, list.get(0));
        fail("no exception");
    } catch (UncheckedIOException e) {
        assertThat(e.getMessage(), is("Unable to write to tmp"));
        assertThat(e.getCause(), is(instanceOf(IOException.class)));
        assertThat(e.getCause().getMessage(), is("oops"));
    }
    try {
        collector.finisher().apply(out);
        fail("no exception");
    } catch (UncheckedIOException e) {
        assertThat(e.getMessage(), is("Unable to close tmp"));
        assertThat(e.getCause(), is(instanceOf(IOException.class)));
        assertThat(e.getCause().getMessage(), is("close"));
    }
    Serializer ms = mock(Serializer.class);
    doThrow(new SerializerException("oops2")).when(ms).serialize(out, list.get(0));
    collector = MessageCollectors.toFile(file, ms);
    try {
        collector.accumulator().accept(out, list.get(0));
        fail("no exception");
    } catch (UncheckedIOException e) {
        assertThat(e.getMessage(), is("Bad data"));
        assertThat(e.getCause(), is(instanceOf(SerializerException.class)));
        assertThat(e.getCause().getMessage(), is("oops2"));
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PrettySerializer(net.morimekta.providence.serializer.PrettySerializer) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) File(java.io.File) SerializerException(net.morimekta.providence.serializer.SerializerException) CompactFields(net.morimekta.test.providence.core.CompactFields) BinarySerializer(net.morimekta.providence.serializer.BinarySerializer) Serializer(net.morimekta.providence.serializer.Serializer) JsonSerializer(net.morimekta.providence.serializer.JsonSerializer) PrettySerializer(net.morimekta.providence.serializer.PrettySerializer) Test(org.junit.Test)

Example 2 with Serializer

use of net.morimekta.providence.serializer.Serializer in project providence by morimekta.

the class SocketServer method process.

@SuppressWarnings("unchecked")
private void process(long startTime, Socket socket) {
    try (Socket ignore = socket;
        BufferedInputStream in = new BufferedInputStream(socket.getInputStream());
        BufferedOutputStream out = new BufferedOutputStream(socket.getOutputStream());
        IOMessageReader reader = new IOMessageReader(in, serializer);
        IOMessageWriter writer = new IOMessageWriter(out, serializer)) {
        while (socket.isConnected()) {
            AtomicReference<PServiceCall> callRef = new AtomicReference<>();
            AtomicReference<PServiceCall> responseRef = new AtomicReference<>();
            try {
                DefaultProcessorHandler handler = new DefaultProcessorHandler(new WrappedProcessor(processor, (c, p) -> {
                    callRef.set(c);
                    responseRef.set(p.handleCall(c));
                    return responseRef.get();
                }));
                handler.process(reader, writer);
                out.flush();
                long endTime = System.nanoTime();
                double duration = ((double) (endTime - startTime)) / NS_IN_MILLIS;
                try {
                    instrumentation.onComplete(duration, callRef.get(), responseRef.get());
                } catch (Throwable th) {
                    LOGGER.error("Exception in service instrumentation", th);
                }
            } catch (IOException e) {
                long endTime = System.nanoTime();
                double duration = ((double) (endTime - startTime)) / NS_IN_MILLIS;
                try {
                    instrumentation.onTransportException(e, duration, callRef.get(), responseRef.get());
                } catch (Throwable th) {
                    LOGGER.error("Exception in service instrumentation", th);
                }
                throw new UncheckedIOException(e.getMessage(), e);
            }
            in.mark(1);
            if (in.read() < 0) {
                return;
            }
            in.reset();
            startTime = System.nanoTime();
        }
    } catch (IOException e) {
        throw new UncheckedIOException(e.getMessage(), e);
    }
}
Also used : ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) Socket(java.net.Socket) BufferedInputStream(java.io.BufferedInputStream) DefaultProcessorHandler(net.morimekta.providence.server.DefaultProcessorHandler) LoggerFactory(org.slf4j.LoggerFactory) BinarySerializer(net.morimekta.providence.serializer.BinarySerializer) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOMessageWriter(net.morimekta.providence.mio.IOMessageWriter) BufferedOutputStream(java.io.BufferedOutputStream) ServerSocket(java.net.ServerSocket) PProcessor(net.morimekta.providence.PProcessor) SocketTimeoutException(java.net.SocketTimeoutException) NS_IN_MILLIS(net.morimekta.providence.util.ServiceCallInstrumentation.NS_IN_MILLIS) WrappedProcessor(net.morimekta.providence.server.WrappedProcessor) ThreadFactory(java.util.concurrent.ThreadFactory) Nonnull(javax.annotation.Nonnull) ExecutorService(java.util.concurrent.ExecutorService) Serializer(net.morimekta.providence.serializer.Serializer) Logger(org.slf4j.Logger) IOException(java.io.IOException) InetSocketAddress(java.net.InetSocketAddress) ServiceCallInstrumentation(net.morimekta.providence.util.ServiceCallInstrumentation) Executors(java.util.concurrent.Executors) UncheckedIOException(java.io.UncheckedIOException) TimeUnit(java.util.concurrent.TimeUnit) PServiceCall(net.morimekta.providence.PServiceCall) IOMessageReader(net.morimekta.providence.mio.IOMessageReader) DefaultProcessorHandler(net.morimekta.providence.server.DefaultProcessorHandler) IOMessageReader(net.morimekta.providence.mio.IOMessageReader) AtomicReference(java.util.concurrent.atomic.AtomicReference) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) IOMessageWriter(net.morimekta.providence.mio.IOMessageWriter) BufferedInputStream(java.io.BufferedInputStream) WrappedProcessor(net.morimekta.providence.server.WrappedProcessor) PServiceCall(net.morimekta.providence.PServiceCall) BufferedOutputStream(java.io.BufferedOutputStream) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket)

Example 3 with Serializer

use of net.morimekta.providence.serializer.Serializer in project providence by morimekta.

the class HttpClientHandler method handleCall.

@Override
public <Request extends PMessage<Request, RequestField>, Response extends PMessage<Response, ResponseField>, RequestField extends PField, ResponseField extends PField> PServiceCall<Response, ResponseField> handleCall(PServiceCall<Request, RequestField> call, PService service) throws IOException {
    if (call.getType() == PServiceCallType.EXCEPTION || call.getType() == PServiceCallType.REPLY) {
        throw new PApplicationException("Request with invalid call type: " + call.getType(), PApplicationExceptionType.INVALID_MESSAGE_TYPE);
    }
    long startTime = System.nanoTime();
    PServiceCall<Response, ResponseField> reply = null;
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        requestSerializer.serialize(baos, call);
        ByteArrayContent content = new ByteArrayContent(requestSerializer.mediaType(), baos.toByteArray());
        @Nonnull GenericUrl url = urlSupplier.get();
        try {
            HttpRequest request = factory.buildPostRequest(url, content);
            request.getHeaders().setAccept(requestSerializer.mediaType());
            HttpResponse response = request.execute();
            try {
                if (call.getType() == PServiceCallType.CALL) {
                    Serializer responseSerializer = requestSerializer;
                    if (response.getContentType() != null) {
                        try {
                            MediaType mediaType = MediaType.parse(response.getContentType());
                            responseSerializer = serializerProvider.getSerializer(mediaType.withoutParameters().toString());
                        } catch (IllegalArgumentException e) {
                            throw new PApplicationException("Unknown content-type in response: " + response.getContentType(), PApplicationExceptionType.INVALID_PROTOCOL).initCause(e);
                        }
                    }
                    // non 200 responses should have triggered a HttpResponseException,
                    // so this is safe.
                    reply = responseSerializer.deserialize(response.getContent(), service);
                    if (reply.getType() == PServiceCallType.CALL || reply.getType() == PServiceCallType.ONEWAY) {
                        throw new PApplicationException("Reply with invalid call type: " + reply.getType(), PApplicationExceptionType.INVALID_MESSAGE_TYPE);
                    }
                    if (reply.getSequence() != call.getSequence()) {
                        throw new PApplicationException("Reply sequence out of order: call = " + call.getSequence() + ", reply = " + reply.getSequence(), PApplicationExceptionType.BAD_SEQUENCE_ID);
                    }
                }
                long endTime = System.nanoTime();
                double duration = ((double) (endTime - startTime)) / NS_IN_MILLIS;
                try {
                    instrumentation.onComplete(duration, call, reply);
                } catch (Exception ignore) {
                }
                return reply;
            } finally {
                // Ignore whatever is left of the response when returning, in
                // case we did'nt read the whole response.
                response.ignore();
            }
        } catch (HttpHostConnectException e) {
            throw e;
        } catch (ConnectException e) {
            // The native exception is not helpful (for when using NetHttpTransport).
            throw new HttpHostConnectException(e, new HttpHost(url.getHost(), url.getPort(), url.getScheme()));
        }
    } catch (Exception e) {
        long endTime = System.nanoTime();
        double duration = ((double) (endTime - startTime)) / NS_IN_MILLIS;
        try {
            instrumentation.onTransportException(e, duration, call, reply);
        } catch (Throwable ie) {
            e.addSuppressed(ie);
        }
        throw e;
    }
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) Nonnull(javax.annotation.Nonnull) HttpResponse(com.google.api.client.http.HttpResponse) ByteArrayOutputStream(java.io.ByteArrayOutputStream) GenericUrl(com.google.api.client.http.GenericUrl) PApplicationException(net.morimekta.providence.PApplicationException) HttpHostConnectException(org.apache.http.conn.HttpHostConnectException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) HttpResponse(com.google.api.client.http.HttpResponse) HttpHost(org.apache.http.HttpHost) PApplicationException(net.morimekta.providence.PApplicationException) HttpHostConnectException(org.apache.http.conn.HttpHostConnectException) MediaType(com.google.common.net.MediaType) ByteArrayContent(com.google.api.client.http.ByteArrayContent) Serializer(net.morimekta.providence.serializer.Serializer) HttpHostConnectException(org.apache.http.conn.HttpHostConnectException) ConnectException(java.net.ConnectException)

Example 4 with Serializer

use of net.morimekta.providence.serializer.Serializer in project providence by morimekta.

the class HttpClientHandlerTest method setUp.

@Before
public void setUp() throws Exception {
    Awaitility.setDefaultPollDelay(50, TimeUnit.MILLISECONDS);
    Log.setLog(new NoLogging());
    impl = mock(net.morimekta.test.thrift.client.TestService.Iface.class);
    TProcessor processor = new net.morimekta.test.thrift.client.TestService.Processor<>(impl);
    instrumentation = mock(ServiceCallInstrumentation.class);
    provider = new DefaultSerializerProvider();
    server = new Server(0);
    ServletContextHandler handler = new ServletContextHandler();
    handler.addServlet(new ServletHolder(new TServlet(processor, new TBinaryProtocol.Factory())), "/" + ENDPOINT);
    handler.addServlet(new ServletHolder(new HttpServlet() {

        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            resp.sendError(HttpServletResponse.SC_NOT_FOUND);
        }
    }), "/" + NOT_FOUND);
    handler.addServlet(new ServletHolder(new HttpServlet() {

        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            resp.setStatus(HttpServletResponse.SC_OK);
            resp.setContentType("text/html");
            resp.getWriter().print("<html></html>");
        }
    }), "/" + HTML);
    handler.addServlet(new ServletHolder(new HttpServlet() {

        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            resp.setStatus(HttpServletResponse.SC_OK);
            Serializer serializer = provider.getDefault();
            resp.setContentType(serializer.mediaType());
            serializer.serialize(resp.getOutputStream(), reply.get());
        }
    }), "/" + RESPONSE);
    contentTypes = new ArrayList<>();
    reply = new AtomicReference<>();
    server.setHandler(handler);
    server.setRequestLog((request, response) -> contentTypes.addAll(Collections.list(request.getHeaders("Content-Type")).stream().map(Object::toString).collect(Collectors.toList())));
    server.start();
    port = getExposedPort(server);
}
Also used : TProcessor(org.apache.thrift.TProcessor) Server(org.eclipse.jetty.server.Server) NoLogging(net.morimekta.providence.client.internal.NoLogging) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) HttpServlet(javax.servlet.http.HttpServlet) DefaultSerializerProvider(net.morimekta.providence.serializer.DefaultSerializerProvider) HttpServletResponse(javax.servlet.http.HttpServletResponse) TServlet(org.apache.thrift.server.TServlet) HttpServletRequest(javax.servlet.http.HttpServletRequest) TProcessor(org.apache.thrift.TProcessor) TBinaryProtocol(org.apache.thrift.protocol.TBinaryProtocol) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) ServiceCallInstrumentation(net.morimekta.providence.util.ServiceCallInstrumentation) Serializer(net.morimekta.providence.serializer.Serializer) Before(org.junit.Before)

Example 5 with Serializer

use of net.morimekta.providence.serializer.Serializer in project providence by morimekta.

the class ITRunner method runProvidence.

private void runProvidence(FormatStatistics statistics) throws IOException {
    Serializer serializer = statistics.format.serializer;
    ByteArrayOutputStream baos = new ByteArrayOutputStream(16 * 1024 * 1024);
    long totalTime = 0;
    for (PM pvd : pvdList) {
        long start = System.nanoTime();
        serializer.serialize(baos, pvd);
        if (!serializer.binaryProtocol()) {
            baos.write('\n');
        }
        long end = System.nanoTime();
        long time = end - start;
        totalTime += time;
        statistics.PwriteStat.addValue(time);
        baos.write(serializer.binaryProtocol() ? Char.FS : '\n');
    }
    statistics.PtotalWriteStat.addValue(totalTime);
    statistics.size = baos.size();
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    totalTime = 0;
    while (bais.available() > 0) {
        long start = System.nanoTime();
        serializer.deserialize(bais, descriptor);
        long end = System.nanoTime();
        long time = end - start;
        totalTime += time;
        statistics.PreadStat.addValue(time);
        if (bais.read() != (serializer.binaryProtocol() ? Char.FS : '\n')) {
            throw new AssertionError("Bad serialized data.");
        }
    }
    statistics.PtotalReadStat.addValue(totalTime);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Serializer(net.morimekta.providence.serializer.Serializer)

Aggregations

Serializer (net.morimekta.providence.serializer.Serializer)13 IOException (java.io.IOException)7 BinarySerializer (net.morimekta.providence.serializer.BinarySerializer)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 Test (org.junit.Test)5 UncheckedIOException (java.io.UncheckedIOException)4 InetSocketAddress (java.net.InetSocketAddress)4 JsonSerializer (net.morimekta.providence.serializer.JsonSerializer)4 PrettySerializer (net.morimekta.providence.serializer.PrettySerializer)4 BufferedInputStream (java.io.BufferedInputStream)3 InputStream (java.io.InputStream)3 ConnectException (java.net.ConnectException)3 GenericUrl (com.google.api.client.http.GenericUrl)2 MediaType (com.google.common.net.MediaType)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 OutputStream (java.io.OutputStream)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Nonnull (javax.annotation.Nonnull)2 ServiceCallInstrumentation (net.morimekta.providence.util.ServiceCallInstrumentation)2