@ThreadSafe public final class ConcurrentStreamWriter extends Object implements Closeable
1. Constructs a StreamEventData locally and enqueue it to a ConcurrentLinkedQueue.
2. Use CAS to set an AtomicBoolean flag to true.
3. If successfully set the flag to true, this thread becomes the writer and proceed to run step 4-7.
4. Keep polling StreamEventData from the concurrent queue and write to FileWriter with the current timestamp until
the queue is empty.
5. Perform a writer flush to make sure all data written are persisted.
6. Set the state of each StreamEventData that are written to COMPLETED (succeed/failure).
7. Set the AtomicBoolean flag back to false.
8. If the StreamEventData enqueued by this thread is NOT COMPLETED, go back to step 2.
The spin lock between step 2 to step 8 is necessary as it guarantees events enqueued by all threads would eventually
get written and flushed.| Modifier and Type | Method and Description |
|---|---|
void |
appendFile(Id.Stream streamId,
Location eventFile,
Location indexFile,
long eventCount,
TimestampCloseable timestampCloseable)
Appends a new stream file to the given stream.
|
void |
asyncEnqueue(Id.Stream streamId,
Map<String,String> headers,
ByteBuffer body,
Executor executor)
Writes an event to the given stream asynchronously.
|
void |
close() |
void |
enqueue(Id.Stream streamId,
Iterator<? extends co.cask.cdap.api.stream.StreamEventData> events)
Writes a list of events to the given stream.
|
void |
enqueue(Id.Stream streamId,
Map<String,String> headers,
ByteBuffer body)
Writes an event to the given stream.
|
public void enqueue(Id.Stream streamId, Map<String,String> headers, ByteBuffer body) throws IOException, NotFoundException
streamId - identifier of the streamheaders - header of the eventbody - content of the eventIOException - if failed to write to streamIllegalArgumentException - If the stream doesn't existsNotFoundExceptionpublic void enqueue(Id.Stream streamId, Iterator<? extends co.cask.cdap.api.stream.StreamEventData> events) throws IOException, NotFoundException
streamId - identifier of the streamevents - list of events to writeIOException - if failed to write to streamIllegalArgumentException - If the stream doesn't existsNotFoundExceptionpublic void asyncEnqueue(Id.Stream streamId, Map<String,String> headers, ByteBuffer body, Executor executor) throws IOException, NotFoundException
streamId - identifier of the streamheaders - header of the eventbody - content of the eventexecutor - The executor for performing the async write flush operationIOException - if fails to get stream informationIllegalArgumentException - If the stream doesn't existsNotFoundExceptionpublic void appendFile(Id.Stream streamId, Location eventFile, Location indexFile, long eventCount, TimestampCloseable timestampCloseable) throws IOException, NotFoundException
streamId - identifier of the streameventFile - location to the new stream data fileindexFile - location to the new stream index fileeventCount - number of events in the given stream filetimestampCloseable - a TimestampCloseable to close and return the stream file close timestampIOException - if failed to append the new stream fileNotFoundExceptionpublic void close()
throws IOException
close in interface Closeableclose in interface AutoCloseableIOExceptionCopyright © 2016 Cask Data, Inc. Licensed under the Apache License, Version 2.0.