@Test public void testTaskSerialize() { UUID proxyId = UUID.randomUUID(); LineDelimitedDataSubmissionTask task = new LineDelimitedDataSubmissionTask(null, proxyId, new DefaultEntityPropertiesForTesting(), null, "wavefront", ReportableEntityType.POINT, "2878", ImmutableList.of("item1", "item2", "item3"), () -> 12345L); RetryTaskConverter<LineDelimitedDataSubmissionTask> converter = new RetryTaskConverter<>( "2878", RetryTaskConverter.CompressionType.NONE); assertNull(converter.fromBytes(new byte[] {0, 0, 0})); assertNull(converter.fromBytes(new byte[] {'W', 'F', 0})); assertNull(converter.fromBytes(new byte[] {'W', 'F', 1})); assertNull(converter.fromBytes(new byte[] {'W', 'F', 1, 0})); }
@SuppressWarnings("unchecked") @Nullable @Override public T fromBytes(@Nonnull byte[] bytes) { ByteArrayInputStream input = new ByteArrayInputStream(bytes); int len = TASK_HEADER.length; byte[] prefix = new byte[len]; if (input.read(prefix, 0, len) == len && Arrays.equals(prefix, TASK_HEADER)) { int bytesToRead = input.read(); if (bytesToRead > 0) { byte[] header = new byte[bytesToRead]; if (input.read(header, 0, bytesToRead) == bytesToRead) { InputStream stream = null; byte compression = header[0] == WRAPPED && bytesToRead > 1 ? header[1] : header[0]; try { switch (compression) { case FORMAT_LZ4: stream = new LZ4BlockInputStream(input); break; case FORMAT_GZIP: stream = new GZIPInputStream(input); break; case FORMAT_RAW: stream = input; break; default: logger.warning("Unable to restore persisted task - unsupported data format " + "header detected: " + Arrays.toString(header)); return null; } return (T) objectMapper.readValue(stream, DataSubmissionTask.class); } catch (Throwable t) { logger.warning("Unable to restore persisted task: " + t); } finally { IOUtils.closeQuietly(stream); } } else { logger.warning("Unable to restore persisted task - corrupted header, ignoring"); } } else { logger.warning("Unable to restore persisted task - missing header, ignoring"); } } else { logger.warning("Unable to restore persisted task - invalid or missing header, ignoring"); } errorCounter.inc(); return null; }
RetryTaskConverter implements TaskConverter<T> { @SuppressWarnings("unchecked") @Nullable @Override public T fromBytes(@Nonnull byte[] bytes) { ByteArrayInputStream input = new ByteArrayInputStream(bytes); int len = TASK_HEADER.length; byte[] prefix = new byte[len]; if (input.read(prefix, 0, len) == len && Arrays.equals(prefix, TASK_HEADER)) { int bytesToRead = input.read(); if (bytesToRead > 0) { byte[] header = new byte[bytesToRead]; if (input.read(header, 0, bytesToRead) == bytesToRead) { InputStream stream = null; byte compression = header[0] == WRAPPED && bytesToRead > 1 ? header[1] : header[0]; try { switch (compression) { case FORMAT_LZ4: stream = new LZ4BlockInputStream(input); break; case FORMAT_GZIP: stream = new GZIPInputStream(input); break; case FORMAT_RAW: stream = input; break; default: logger.warning("Unable to restore persisted task - unsupported data format " + "header detected: " + Arrays.toString(header)); return null; } return (T) objectMapper.readValue(stream, DataSubmissionTask.class); } catch (Throwable t) { logger.warning("Unable to restore persisted task: " + t); } finally { IOUtils.closeQuietly(stream); } } else { logger.warning("Unable to restore persisted task - corrupted header, ignoring"); } } else { logger.warning("Unable to restore persisted task - missing header, ignoring"); } } else { logger.warning("Unable to restore persisted task - invalid or missing header, ignoring"); } errorCounter.inc(); return null; } }
RetryTaskConverter implements TaskConverter<T> { @SuppressWarnings("unchecked") @Nullable @Override public T fromBytes(@Nonnull byte[] bytes) { ByteArrayInputStream input = new ByteArrayInputStream(bytes); int len = TASK_HEADER.length; byte[] prefix = new byte[len]; if (input.read(prefix, 0, len) == len && Arrays.equals(prefix, TASK_HEADER)) { int bytesToRead = input.read(); if (bytesToRead > 0) { byte[] header = new byte[bytesToRead]; if (input.read(header, 0, bytesToRead) == bytesToRead) { InputStream stream = null; byte compression = header[0] == WRAPPED && bytesToRead > 1 ? header[1] : header[0]; try { switch (compression) { case FORMAT_LZ4: stream = new LZ4BlockInputStream(input); break; case FORMAT_GZIP: stream = new GZIPInputStream(input); break; case FORMAT_RAW: stream = input; break; default: logger.warning("Unable to restore persisted task - unsupported data format " + "header detected: " + Arrays.toString(header)); return null; } return (T) objectMapper.readValue(stream, DataSubmissionTask.class); } catch (Throwable t) { logger.warning("Unable to restore persisted task: " + t); } finally { IOUtils.closeQuietly(stream); } } else { logger.warning("Unable to restore persisted task - corrupted header, ignoring"); } } else { logger.warning("Unable to restore persisted task - missing header, ignoring"); } } else { logger.warning("Unable to restore persisted task - invalid or missing header, ignoring"); } errorCounter.inc(); return null; } RetryTaskConverter(String handle, CompressionType compressionType); }
RetryTaskConverter implements TaskConverter<T> { @SuppressWarnings("unchecked") @Nullable @Override public T fromBytes(@Nonnull byte[] bytes) { ByteArrayInputStream input = new ByteArrayInputStream(bytes); int len = TASK_HEADER.length; byte[] prefix = new byte[len]; if (input.read(prefix, 0, len) == len && Arrays.equals(prefix, TASK_HEADER)) { int bytesToRead = input.read(); if (bytesToRead > 0) { byte[] header = new byte[bytesToRead]; if (input.read(header, 0, bytesToRead) == bytesToRead) { InputStream stream = null; byte compression = header[0] == WRAPPED && bytesToRead > 1 ? header[1] : header[0]; try { switch (compression) { case FORMAT_LZ4: stream = new LZ4BlockInputStream(input); break; case FORMAT_GZIP: stream = new GZIPInputStream(input); break; case FORMAT_RAW: stream = input; break; default: logger.warning("Unable to restore persisted task - unsupported data format " + "header detected: " + Arrays.toString(header)); return null; } return (T) objectMapper.readValue(stream, DataSubmissionTask.class); } catch (Throwable t) { logger.warning("Unable to restore persisted task: " + t); } finally { IOUtils.closeQuietly(stream); } } else { logger.warning("Unable to restore persisted task - corrupted header, ignoring"); } } else { logger.warning("Unable to restore persisted task - missing header, ignoring"); } } else { logger.warning("Unable to restore persisted task - invalid or missing header, ignoring"); } errorCounter.inc(); return null; } RetryTaskConverter(String handle, CompressionType compressionType); @SuppressWarnings("unchecked") @Nullable @Override T fromBytes(@Nonnull byte[] bytes); @Override void serializeToStream(@Nonnull T t, @Nonnull OutputStream bytes); @Nullable @Override Integer getWeight(@Nonnull byte[] bytes); }
RetryTaskConverter implements TaskConverter<T> { @SuppressWarnings("unchecked") @Nullable @Override public T fromBytes(@Nonnull byte[] bytes) { ByteArrayInputStream input = new ByteArrayInputStream(bytes); int len = TASK_HEADER.length; byte[] prefix = new byte[len]; if (input.read(prefix, 0, len) == len && Arrays.equals(prefix, TASK_HEADER)) { int bytesToRead = input.read(); if (bytesToRead > 0) { byte[] header = new byte[bytesToRead]; if (input.read(header, 0, bytesToRead) == bytesToRead) { InputStream stream = null; byte compression = header[0] == WRAPPED && bytesToRead > 1 ? header[1] : header[0]; try { switch (compression) { case FORMAT_LZ4: stream = new LZ4BlockInputStream(input); break; case FORMAT_GZIP: stream = new GZIPInputStream(input); break; case FORMAT_RAW: stream = input; break; default: logger.warning("Unable to restore persisted task - unsupported data format " + "header detected: " + Arrays.toString(header)); return null; } return (T) objectMapper.readValue(stream, DataSubmissionTask.class); } catch (Throwable t) { logger.warning("Unable to restore persisted task: " + t); } finally { IOUtils.closeQuietly(stream); } } else { logger.warning("Unable to restore persisted task - corrupted header, ignoring"); } } else { logger.warning("Unable to restore persisted task - missing header, ignoring"); } } else { logger.warning("Unable to restore persisted task - invalid or missing header, ignoring"); } errorCounter.inc(); return null; } RetryTaskConverter(String handle, CompressionType compressionType); @SuppressWarnings("unchecked") @Nullable @Override T fromBytes(@Nonnull byte[] bytes); @Override void serializeToStream(@Nonnull T t, @Nonnull OutputStream bytes); @Nullable @Override Integer getWeight(@Nonnull byte[] bytes);  }