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"));
}
}
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);
}
}
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;
}
}
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);
}
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);
}
Aggregations