/** * Serializes an Object into a byte array. Along with {@link #read(byte[])}, * can be used to serialize an Object and deserialize it into an Object of * the same type without needing to specify the Object's type, * as long as it is one of the JSON-compatible objects understood by * ObjectMapper. * * @param o An Object * @return A byte array representation of the Object * @throws IOException if there is a write error */ public static byte[] write(Object o) throws IOException { if (o == null) { return EMPTY_BYTES; } return OBJECT_WRITER.writeValueAsBytes(o); }
/** * Serializes an Object into a byte array. Along with {@link #read(byte[])}, * can be used to serialize an Object and deserialize it into an Object of * the same type without needing to specify the Object's type, * as long as it is one of the JSON-compatible objects understood by * ObjectMapper. * * @param o An Object * @return A byte array representation of the Object * @throws IOException if there is a write error */ public static byte[] write(Object o) throws IOException { if (o == null) { return EMPTY_BYTES; } return OBJECT_WRITER.writeValueAsBytes(o); }
/** * Writes the list of live processors in the system to the blob. * Write is successful only if the lease ID passed is valid and the processor holds the lease. * Called only by the leader. * @param processors List of live processors to be published on the blob. * @param leaseId LeaseID of the valid lease that the processor holds on the blob. Null if there is no lease. * @return true if write to the blob is successful, false if leaseID is null or an Azure storage service error or IO exception occurred. */ public boolean publishLiveProcessorList(List<String> processors, String leaseId) { try { if (leaseId == null) { return false; } byte[] data = SamzaObjectMapper.getObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsBytes(processors); byte[] pageData = Arrays.copyOf(data, (int) BARRIER_STATE_BLOCK_SIZE); InputStream is = new ByteArrayInputStream(pageData); blob.uploadPages(is, JOB_MODEL_BLOCK_SIZE + BARRIER_STATE_BLOCK_SIZE, PROCESSOR_LIST_BLOCK_SIZE, AccessCondition.generateLeaseCondition(leaseId), null, null); LOG.info("Uploaded list of live processors to blob."); return true; } catch (StorageException | IOException e) { LOG.error("Processor list: " + processors + "publish failed", e); return false; } }
/** * Writes the barrier state to the blob. * Write is successful only if the lease ID passed is valid and the processor holds the lease. * Called only by the leader. * @param state Barrier state to be published to the blob. * @param leaseId LeaseID of the valid lease that the processor holds on the blob. Null if there is no lease. * @return true if write to the blob is successful, false if leaseID is null or an Azure storage service error or IO exception occurred. */ public boolean publishBarrierState(String state, String leaseId) { try { if (leaseId == null) { return false; } byte[] data = SamzaObjectMapper.getObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsBytes(state); byte[] pageData = Arrays.copyOf(data, (int) BARRIER_STATE_BLOCK_SIZE); InputStream is = new ByteArrayInputStream(pageData); //uploadPages is only successful when the AccessCondition provided has an active and valid lease ID. It fails otherwise. blob.uploadPages(is, JOB_MODEL_BLOCK_SIZE, BARRIER_STATE_BLOCK_SIZE, AccessCondition.generateLeaseCondition(leaseId), null, null); LOG.info("Uploaded barrier state {} to blob", state); return true; } catch (StorageException | IOException e) { LOG.error("Barrier state " + state + " publish failed", e); return false; } }
/** * Writes the job model to the blob. * Write is successful only if the lease ID passed is valid and the processor holds the lease. * Called by the leader. * @param prevJM Previous job model version that the processor was operating on. * @param currJM Current job model version that the processor is operating on. * @param prevJMV Previous job model version that the processor was operating on. * @param currJMV Current job model version that the processor is operating on. * @param leaseId LeaseID of the lease that the processor holds on the blob. Null if there is no lease. * @return true if write to the blob is successful, false if leaseID is null or an Azure storage service error or IO exception occurred. */ public boolean publishJobModel(JobModel prevJM, JobModel currJM, String prevJMV, String currJMV, String leaseId) { try { if (leaseId == null) { return false; } JobModelBundle bundle = new JobModelBundle(prevJM, currJM, prevJMV, currJMV); byte[] data = SamzaObjectMapper.getObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsBytes(bundle); byte[] pageData = Arrays.copyOf(data, (int) JOB_MODEL_BLOCK_SIZE); InputStream is = new ByteArrayInputStream(pageData); blob.uploadPages(is, 0, JOB_MODEL_BLOCK_SIZE, AccessCondition.generateLeaseCondition(leaseId), null, null); LOG.info("Uploaded {} jobModel to blob", bundle.getCurrJobModel()); return true; } catch (StorageException | IOException e) { LOG.error("JobModel publish failed for version = " + currJMV, e); return false; } }