@Override public boolean mark(TridentTuple tuple, long offset) { return mark(offset); }
public TimedRotationPolicy(float count, TimeUnit units) { this.interval = (long) (count * units.getMilliSeconds()); }
public FileSizeRotationPolicy(float count, Units units) { this.maxBytes = (long) (count * units.getByteCount()); }
throw new IllegalStateException("RotationPolicy must be specified."); } else if (this.rotationPolicy instanceof FileSizeRotationPolicy) { long rotationBytes = ((FileSizeRotationPolicy) rotationPolicy).getMaxBytes(); LOG.warn("FileSizeRotationPolicy specified with {} bytes.", rotationBytes); LOG.warn("Recovery will fail if data files cannot be copied within topology.message.timeout.secs."); LOG.warn("Ensure that the data files does not grow too big with the FileSizeRotationPolicy."); } else if (this.rotationPolicy instanceof TimedRotationPolicy) { LOG.warn("TimedRotationPolicy specified with interval {} ms.", ((TimedRotationPolicy) rotationPolicy).getInterval()); LOG.warn("Recovery will fail if data files cannot be copied within topology.message.timeout.secs."); LOG.warn("Ensure that the data files does not grow too big with the TimedRotationPolicy."); rotationPolicy.start();
private HdfsState createHdfsState() { Fields hdfsFields = new Fields("f1"); RecordFormat recordFormat = new DelimitedRecordFormat().withFields(hdfsFields); FileRotationPolicy rotationPolicy = new FileSizeRotationPolicy(5.0f, FileSizeRotationPolicy.Units.MB); HdfsState.Options options = new HdfsState.HdfsFileOptions() .withFileNameFormat(fileNameFormat) .withRecordFormat(recordFormat) .withRotationPolicy(rotationPolicy) .withFsUrl("file://" + TEST_OUT_DIR); Map<String, Object> conf = new HashMap<>(); conf.put(Config.TOPOLOGY_NAME, TEST_TOPOLOGY_NAME); HdfsState state = new HdfsState(options); state.prepare(conf, null, 0, 1); return state; }
/** * Recovers nBytes from srcFile to the new file created by calling rotateOutputFile and then deletes the srcFile. */ private void recover(String srcFile, long nBytes) { try { Path srcPath = new Path(srcFile); rotateOutputFile(false); this.rotationPolicy.reset(); if (nBytes > 0) { doRecover(srcPath, nBytes); LOG.info("Recovered {} bytes from {} to {}", nBytes, srcFile, currentFile); } else { LOG.info("Nothing to recover from {}", srcFile); } fs.delete(srcPath, false); LOG.info("Deleted file {} that had partial commits.", srcFile); } catch (Exception e) { LOG.warn("Recovery failed.", e); throw new RuntimeException(e); } }
@Override public void doCommit(Long txId) throws IOException { if (this.rotationPolicy.mark(this.offset)) { rotateOutputFile(); this.offset = 0; this.rotationPolicy.reset(); } else { if (this.out instanceof HdfsDataOutputStream) { ((HdfsDataOutputStream) this.out).hsync(EnumSet.of(HdfsDataOutputStream.SyncFlag.UPDATE_LENGTH)); } else { this.out.hsync(); } } }
throw new IllegalStateException("RotationPolicy must be specified."); } else if (this.rotationPolicy instanceof FileSizeRotationPolicy) { long rotationBytes = ((FileSizeRotationPolicy) rotationPolicy).getMaxBytes(); LOG.warn("FileSizeRotationPolicy specified with {} bytes.", rotationBytes); LOG.warn("Recovery will fail if data files cannot be copied within topology.message.timeout.secs."); LOG.warn("Ensure that the data files does not grow too big with the FileSizeRotationPolicy."); } else if (this.rotationPolicy instanceof TimedRotationPolicy) { LOG.warn("TimedRotationPolicy specified with interval {} ms.", ((TimedRotationPolicy) rotationPolicy).getInterval()); LOG.warn("Recovery will fail if data files cannot be copied within topology.message.timeout.secs."); LOG.warn("Ensure that the data files does not grow too big with the TimedRotationPolicy."); rotationPolicy.start();
public TimedRotationPolicy(float count, TimeUnit units){ this.interval = (long)(count * units.getMilliSeconds()); } /**
public FileSizeRotationPolicy(float count, Units units){ this.maxBytes = (long)(count * units.getByteCount()); }
public static StormTopology buildTopology(String hdfsUrl) { FixedBatchSpout spout = new FixedBatchSpout(new Fields("sentence", "key"), 1000, new Values("the cow jumped over the moon", 1l), new Values("the man went to the store and bought some candy", 2l), new Values("four score and seven years ago", 3l), new Values("how many apples can you eat", 4l), new Values("to be or not to be the person", 5l)); spout.setCycle(true); TridentTopology topology = new TridentTopology(); Stream stream = topology.newStream("spout1", spout); Fields hdfsFields = new Fields("sentence", "key"); FileNameFormat fileNameFormat = new DefaultFileNameFormat() .withPath("/tmp/trident") .withPrefix("trident") .withExtension(".txt"); RecordFormat recordFormat = new DelimitedRecordFormat() .withFields(hdfsFields); FileRotationPolicy rotationPolicy = new FileSizeRotationPolicy(5.0f, FileSizeRotationPolicy.Units.MB); HdfsState.Options options = new HdfsState.HdfsFileOptions() .withFileNameFormat(fileNameFormat) .withRecordFormat(recordFormat) .withRotationPolicy(rotationPolicy) .withFsUrl(hdfsUrl) .withConfigKey("hdfs.config"); StateFactory factory = new HdfsStateFactory().withOptions(options); TridentState state = stream .partitionPersist(factory, hdfsFields, new HdfsUpdater(), new Fields()); return topology.build(); }
/** * Recovers nBytes from srcFile to the new file created * by calling rotateOutputFile and then deletes the srcFile. */ private void recover(String srcFile, long nBytes) { try { Path srcPath = new Path(srcFile); rotateOutputFile(false); this.rotationPolicy.reset(); if (nBytes > 0) { doRecover(srcPath, nBytes); LOG.info("Recovered {} bytes from {} to {}", nBytes, srcFile, currentFile); } else { LOG.info("Nothing to recover from {}", srcFile); } fs.delete(srcPath, false); LOG.info("Deleted file {} that had partial commits.", srcFile); } catch (Exception e) { LOG.warn("Recovery failed.", e); throw new RuntimeException(e); } }
@Override public boolean mark(TridentTuple tuple, long offset) { return mark(offset); }
public TimedRotationPolicy(float count, TimeUnit units){ this.interval = (long)(count * units.getMilliSeconds()); } /**
public FileSizeRotationPolicy(float count, Units units){ this.maxBytes = (long)(count * units.getByteCount()); }
public static StormTopology buildTopology(String hdfsUrl) { FixedBatchSpout spout = new FixedBatchSpout(new Fields("sentence", "key"), 1000, new Values("the cow jumped over the moon", 1l), new Values("the man went to the store and bought some candy", 2l), new Values("four score and seven years ago", 3l), new Values("how many apples can you eat", 4l), new Values("to be or not to be the person", 5l)); spout.setCycle(true); TridentTopology topology = new TridentTopology(); Stream stream = topology.newStream("spout1", spout); Fields hdfsFields = new Fields("sentence", "key"); FileNameFormat fileNameFormat = new DefaultFileNameFormat() .withPath("/tmp/trident") .withPrefix("trident") .withExtension(".seq"); FileRotationPolicy rotationPolicy = new FileSizeRotationPolicy(5.0f, FileSizeRotationPolicy.Units.MB); HdfsState.Options seqOpts = new HdfsState.SequenceFileOptions() .withFileNameFormat(fileNameFormat) .withSequenceFormat(new DefaultSequenceFormat("key", "sentence")) .withRotationPolicy(rotationPolicy) .withFsUrl(hdfsUrl) .withConfigKey("hdfs.config") .addRotationAction(new MoveFileAction().toDestination("/tmp/dest2/")); StateFactory factory = new HdfsStateFactory().withOptions(seqOpts); TridentState state = stream .partitionPersist(factory, hdfsFields, new HdfsUpdater(), new Fields()); return topology.build(); }
@Override public void doCommit(Long txId) throws IOException { if (this.rotationPolicy.mark(this.offset)) { rotateOutputFile(); this.offset = 0; this.rotationPolicy.reset(); } else { if (this.out instanceof HdfsDataOutputStream) { ((HdfsDataOutputStream) this.out).hsync(EnumSet.of(HdfsDataOutputStream.SyncFlag.UPDATE_LENGTH)); } else { this.out.hsync(); } } }
@Override public void execute(List<TridentTuple> tuples) throws IOException { long offset; for(TridentTuple tuple : tuples) { synchronized (this.writeLock) { this.writer.append(this.format.key(tuple), this.format.value(tuple)); offset = this.writer.getLength(); } if (this.rotationPolicy.mark(tuple, offset)) { rotateOutputFile(); this.rotationPolicy.reset(); } } }