Package org.apache.cassandra.streaming
Class StreamSession
- java.lang.Object
-
- org.apache.cassandra.streaming.StreamSession
-
public class StreamSession extends java.lang.Object
Handles the streaming a one or more streams to and from a specific remote node. Both this node and the remote one will create a similar symmetricalStreamSession
. A streaming session has the following life-cycle:1. Session Initialization (a) A node (the initiator in the following) create a new
In brief, the message passing looks like this (I for initiator, F for follwer):StreamSession
, initialize itinit(StreamResultFuture)
, and then start it (start()
). Starting a session causes aStreamInitMessage
to be sent. (b) Upon reception of thatStreamInitMessage
, the follower creates its ownStreamSession
, and initializes it if it still does not exist. (c) After the initiator sends theStreamInitMessage
, it invokesonInitializationComplete()
to start the streaming prepare phase. 2. Streaming preparation phase (a) APrepareSynMessage
is sent that includes a) what files/sections this node will stream to the follower (stored locally in aStreamTransferTask
, one for each table) and b) what the follower needs to stream back (stored locally in aStreamReceiveTask
, one for each table). (b) Upon reception of thePrepareSynMessage
, the follower records which files/sections it will receive and send back aPrepareSynAckMessage
, which contains a summary of the files/sections that will be sent to the initiator. (c) When the initiator receives thePrepareSynAckMessage
, it records which files/sections it will receive, and then goes to it's Streaming phase (see next section). If the intiator is to receive files, it sends aPrepareAckMessage
to the follower to indicate that it can start streaming to the initiator. (d) (Optional) If the follower receives aPrepareAckMessage
, it enters it's Streaming phase. 3. Streaming phase (a) The streaming phase is started at each node by callingStreamSession#startStreamingFiles(boolean)
. This will send, sequentially on each outbound streaming connection (seeStreamingMultiplexedChannel
), anOutgoingStreamMessage
for each stream in each of theStreamTransferTask
. EachOutgoingStreamMessage
consists of aStreamMessageHeader
that contains metadata about the stream, followed by the stream content itself. Once all the files for aStreamTransferTask
are sent, the task is marked completeStreamTransferTask.complete(int)
. (b) On the receiving side, the incoming data is written to disk, and once the stream is fully received, it will be marked as complete (StreamReceiveTask.received(IncomingStream)
). When all streams for theStreamReceiveTask
have been received, the data is added to the CFS (and 2ndary indexes/MV are built), and the task is marked complete (taskCompleted(StreamReceiveTask)
). (b) If during the streaming of a particular stream an error occurs on the receiving end of a stream (it may be either the initiator or the follower), the node will send aSessionFailedMessage
to the sender and close the stream session. (c) When all transfer and receive tasks for a session are complete, the session moves to the Completion phase (maybeCompleted()
). 4. Completion phase (a) When the initiator finishes streaming, it enters theStreamSession.State.WAIT_COMPLETE
state, and waits for the follower to send aCompleteMessage
once it finishes streaming too. Once theCompleteMessage
is received, initiator sets its own state toStreamSession.State.COMPLETE
and closes all channels attached to this session.(session init) I: StreamInitMessage (session prepare) I: PrepareSynMessage F: PrepareSynAckMessage I: PrepareAckMessage (stream - this can happen in both directions) I: OutgoingStreamMessage F: ReceivedMessage (completion) F: CompleteMessage
All messages which derive fromStreamMessage
are sent by the standard internode messaging (viaMessagingService
, while the actual files themselves are sent by a special "streaming" connection type. SeeStreamingMultiplexedChannel
for details. Because of the asynchronous
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
StreamSession.MessageStateSink
static class
StreamSession.PrepareDirection
static class
StreamSession.State
State Transition:
-
Field Summary
Fields Modifier and Type Field Description java.lang.String
failureReason
InetAddressAndPort
peer
Streaming endpoint.protected java.util.Set<StreamRequest>
requests
static StreamSession.MessageStateSink
sink
protected java.util.concurrent.ConcurrentHashMap<TableId,StreamTransferTask>
transfers
-
Constructor Summary
Constructors Constructor Description StreamSession(StreamOperation streamOperation, InetAddressAndPort peer, StreamingChannel.Factory factory, StreamingChannel controlChannel, int messagingVersion, boolean isFollower, int index, TimeUUID pendingRepair, PreviewKind previewKind)
Create new streaming session with the peer.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
abort()
void
addStreamRequest(java.lang.String keyspace, RangesAtEndpoint fullRanges, RangesAtEndpoint transientRanges, java.util.Collection<java.lang.String> columnFamilies)
Request data fetch task to this session.boolean
attachInbound(StreamingChannel channel)
Attach a channel to this session upon receiving the first inbound message.boolean
attachOutbound(StreamingChannel channel)
Attach a channel to this session upon sending the first outbound message.static java.lang.StringBuilder
boundStackTrace(java.lang.Throwable e, int limit, int counter, java.util.Set<java.lang.Throwable> visited, java.lang.StringBuilder out)
static java.lang.StringBuilder
boundStackTrace(java.lang.Throwable e, int limit, java.lang.StringBuilder out)
static boolean
checkAvailableDiskSpaceAndCompactions(java.util.Collection<StreamSummary> summaries, TimeUUID planId, java.lang.String remoteAddress, boolean isForIncremental)
Makes sure that we expect to have enough disk space available for the new streams, taking into consideration the ongoing compactions and streams.void
complete()
Check if session is completed on receivingStreamMessage.Type.COMPLETE
message.void
countStreamedIn(boolean isEntireSSTable)
static java.lang.String
createLogTag(StreamSession session)
static java.lang.String
createLogTag(StreamSession session, io.netty.channel.Channel channel)
static java.lang.String
createLogTag(StreamSession session, java.lang.Object channelId)
static java.lang.String
createLogTag(StreamSession session, StreamingChannel channel)
StreamReceiver
getAggregator(TableId tableId)
StreamingMultiplexedChannel
getChannel()
int
getNumRequests()
int
getNumTransfers()
java.util.List<OutgoingStream>
getOutgoingStreamsForRanges(RangesAtEndpoint replicas, java.util.Collection<ColumnFamilyStore> stores, TimeUUID pendingRepair, PreviewKind previewKind)
TimeUUID
getPendingRepair()
PreviewKind
getPreviewKind()
SessionInfo
getSessionInfo()
StreamOperation
getStreamOperation()
void
init(StreamResultFuture streamResult)
Bind this session to report to specificStreamResultFuture
and perform pre-streaming initialization.boolean
isFollower()
boolean
isPreview()
boolean
isSuccess()
Return if this session completed successfully.void
messageReceived(StreamMessage message)
io.netty.util.concurrent.Future<?>
onError(java.lang.Throwable e)
Signal an error to this stream session: if it's an EOF exception, it tries to understand if the socket was closed after completion or because the peer was down, otherwise sends aSessionFailedMessage
and closes the session asStreamSession.State.FAILED
.void
onInitializationComplete()
Call back when connection initialization is complete to start the prepare phase.TimeUUID
planId()
void
prepare(java.util.Collection<StreamRequest> requests, java.util.Collection<StreamSummary> summaries)
Prepare this session for sending/receiving files.void
prepareReceiving(StreamSummary summary)
void
progress(java.lang.String filename, ProgressInfo.Direction direction, long bytes, long delta, long total)
void
receive(IncomingStreamMessage message)
Call back after receiving a stream.void
received(TableId tableId, int sequenceNumber)
void
sessionFailed()
Call back on receivingStreamMessage.Type.SESSION_FAILED
message.int
sessionIndex()
void
sessionTimeout()
Call back on receivingStreamMessage.Type.SESSION_FAILED
message.void
start()
invoked by the node that begins the stream session (it may be sending files, receiving files, or both)StreamSession.State
state()
void
state(StreamSession.State newState)
Set current state tonewState
.StreamOperation
streamOperation()
void
streamSent(OutgoingStreamMessage message)
Call back after sending StreamMessageHeader.void
taskCompleted(StreamReceiveTask completedTask)
void
taskCompleted(StreamTransferTask completedTask)
java.lang.String
toString()
-
-
-
Field Detail
-
sink
public static volatile StreamSession.MessageStateSink sink
-
peer
public final InetAddressAndPort peer
Streaming endpoint. EachStreamSession
is identified by this InetAddressAndPort which is broadcast address of the node streaming.
-
requests
protected final java.util.Set<StreamRequest> requests
-
transfers
protected final java.util.concurrent.ConcurrentHashMap<TableId,StreamTransferTask> transfers
-
failureReason
public java.lang.String failureReason
-
-
Constructor Detail
-
StreamSession
public StreamSession(StreamOperation streamOperation, InetAddressAndPort peer, StreamingChannel.Factory factory, @Nullable StreamingChannel controlChannel, int messagingVersion, boolean isFollower, int index, TimeUUID pendingRepair, PreviewKind previewKind)
Create new streaming session with the peer.
-
-
Method Detail
-
isFollower
public boolean isFollower()
-
planId
public TimeUUID planId()
-
sessionIndex
public int sessionIndex()
-
streamOperation
public StreamOperation streamOperation()
-
getStreamOperation
public StreamOperation getStreamOperation()
-
getPendingRepair
public TimeUUID getPendingRepair()
-
isPreview
public boolean isPreview()
-
getPreviewKind
public PreviewKind getPreviewKind()
-
getAggregator
public StreamReceiver getAggregator(TableId tableId)
-
init
public void init(StreamResultFuture streamResult)
Bind this session to report to specificStreamResultFuture
and perform pre-streaming initialization.- Parameters:
streamResult
- result to report to
-
attachInbound
public boolean attachInbound(StreamingChannel channel)
Attach a channel to this session upon receiving the first inbound message.- Parameters:
channel
- The channel to attach.- Returns:
- False if the channel was already attached, true otherwise.
-
attachOutbound
public boolean attachOutbound(StreamingChannel channel)
Attach a channel to this session upon sending the first outbound message.- Parameters:
channel
- The channel to attach.- Returns:
- False if the channel was already attached, true otherwise.
-
start
public void start()
invoked by the node that begins the stream session (it may be sending files, receiving files, or both)
-
addStreamRequest
public void addStreamRequest(java.lang.String keyspace, RangesAtEndpoint fullRanges, RangesAtEndpoint transientRanges, java.util.Collection<java.lang.String> columnFamilies)
Request data fetch task to this session. Here, we have to encode both _local_ range transientness (encoded in Replica itself, in RangesAtEndpoint) and _remote_ (source) range transientmess, which is encoded by splitting ranges into full and transient.- Parameters:
keyspace
- Requesting keyspacefullRanges
- Ranges to retrieve data that will return full data from the sourcetransientRanges
- Ranges to retrieve data that will return transient data from the sourcecolumnFamilies
- ColumnFamily names. Can be empty if requesting all CF under the keyspace.
-
getOutgoingStreamsForRanges
public java.util.List<OutgoingStream> getOutgoingStreamsForRanges(RangesAtEndpoint replicas, java.util.Collection<ColumnFamilyStore> stores, TimeUUID pendingRepair, PreviewKind previewKind)
-
state
public void state(StreamSession.State newState)
Set current state tonewState
.- Parameters:
newState
- new state to set
-
state
public StreamSession.State state()
- Returns:
- current state
-
getChannel
public StreamingMultiplexedChannel getChannel()
-
isSuccess
public boolean isSuccess()
Return if this session completed successfully.- Returns:
- true if session completed successfully.
-
messageReceived
public void messageReceived(StreamMessage message)
-
onInitializationComplete
public void onInitializationComplete()
Call back when connection initialization is complete to start the prepare phase.
-
onError
public io.netty.util.concurrent.Future<?> onError(java.lang.Throwable e)
Signal an error to this stream session: if it's an EOF exception, it tries to understand if the socket was closed after completion or because the peer was down, otherwise sends aSessionFailedMessage
and closes the session asStreamSession.State.FAILED
.
-
prepare
public void prepare(java.util.Collection<StreamRequest> requests, java.util.Collection<StreamSummary> summaries)
Prepare this session for sending/receiving files.
-
countStreamedIn
public void countStreamedIn(boolean isEntireSSTable)
-
checkAvailableDiskSpaceAndCompactions
public static boolean checkAvailableDiskSpaceAndCompactions(java.util.Collection<StreamSummary> summaries, @Nullable TimeUUID planId, @Nullable java.lang.String remoteAddress, boolean isForIncremental)
Makes sure that we expect to have enough disk space available for the new streams, taking into consideration the ongoing compactions and streams.
-
streamSent
public void streamSent(OutgoingStreamMessage message)
Call back after sending StreamMessageHeader.- Parameters:
message
- sent stream message
-
receive
public void receive(IncomingStreamMessage message)
Call back after receiving a stream.- Parameters:
message
- received stream
-
progress
public void progress(java.lang.String filename, ProgressInfo.Direction direction, long bytes, long delta, long total)
-
received
public void received(TableId tableId, int sequenceNumber)
-
complete
public void complete()
Check if session is completed on receivingStreamMessage.Type.COMPLETE
message.
-
sessionFailed
public void sessionFailed()
Call back on receivingStreamMessage.Type.SESSION_FAILED
message.
-
sessionTimeout
public void sessionTimeout()
Call back on receivingStreamMessage.Type.SESSION_FAILED
message.
-
getSessionInfo
public SessionInfo getSessionInfo()
- Returns:
- Current snapshot of this session info.
-
taskCompleted
public void taskCompleted(StreamReceiveTask completedTask)
-
taskCompleted
public void taskCompleted(StreamTransferTask completedTask)
-
prepareReceiving
public void prepareReceiving(StreamSummary summary)
-
getNumRequests
public int getNumRequests()
-
getNumTransfers
public int getNumTransfers()
-
createLogTag
public static java.lang.String createLogTag(StreamSession session)
-
createLogTag
public static java.lang.String createLogTag(StreamSession session, StreamingChannel channel)
-
createLogTag
public static java.lang.String createLogTag(StreamSession session, io.netty.channel.Channel channel)
-
createLogTag
public static java.lang.String createLogTag(StreamSession session, java.lang.Object channelId)
-
abort
public void abort()
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
boundStackTrace
public static java.lang.StringBuilder boundStackTrace(java.lang.Throwable e, int limit, java.lang.StringBuilder out)
-
boundStackTrace
public static java.lang.StringBuilder boundStackTrace(java.lang.Throwable e, int limit, int counter, java.util.Set<java.lang.Throwable> visited, java.lang.StringBuilder out)
-
-