Package org.apache.cassandra.streaming
Class StreamSession
java.lang.Object
org.apache.cassandra.streaming.StreamSession
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 symmetrical
StreamSession. A streaming
session has the following life-cycle:
1. Session Initialization (a) A node (the initiator in the following) create a newIn 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 aStreamInitMessageto 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) APrepareSynMessageis 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 aPrepareAckMessageto 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 callingstartStreamingFiles(PrepareDirection). This will send, sequentially on each outbound streaming connection (seeStreamingMultiplexedChannel), anOutgoingStreamMessagefor each stream in each of theStreamTransferTask. EachOutgoingStreamMessageconsists of aStreamMessageHeaderthat contains metadata about the stream, followed by the stream content itself. Once all the files for aStreamTransferTaskare 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 theStreamReceiveTaskhave 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 aSessionFailedMessageto 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_COMPLETEstate, and waits for the follower to send aCompleteMessageonce it finishes streaming too. Once theCompleteMessageis received, initiator sets its own state toStreamSession.State.COMPLETEand 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: CompleteMessageAll messages which derive from
StreamMessage are sent by the standard internode messaging
(via MessagingService, while the actual files themselves are sent by a special
"streaming" connection type. See StreamingMultiplexedChannel for details. Because of the asynchronous-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfacestatic enumstatic enumState Transition: -
Field Summary
FieldsModifier and TypeFieldDescriptionfinal InetAddressAndPortStreaming endpoint.protected final Set<StreamRequest>protected final ConcurrentHashMap<TableId,StreamTransferTask> -
Constructor Summary
ConstructorsConstructorDescriptionStreamSession(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
Modifier and TypeMethodDescriptionvoidabort()voidaddStreamRequest(String keyspace, RangesAtEndpoint fullRanges, RangesAtEndpoint transientRanges, Collection<String> columnFamilies) Request data fetch task to this session.booleanattachInbound(StreamingChannel channel) Attach a channel to this session upon receiving the first inbound message.booleanattachOutbound(StreamingChannel channel) Attach a channel to this session upon sending the first outbound message.static StringBuilderboundStackTrace(Throwable e, int limit, int counter, Set<Throwable> visited, StringBuilder out) static StringBuilderboundStackTrace(Throwable e, int limit, StringBuilder out) static booleancheckAvailableDiskSpaceAndCompactions(Collection<StreamSummary> summaries, TimeUUID planId, 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.voidcomplete()Check if session is completed on receivingStreamMessage.Type.COMPLETEmessage.voidcountStreamedIn(boolean isEntireSSTable) static StringcreateLogTag(StreamSession session) static StringcreateLogTag(StreamSession session, io.netty.channel.Channel channel) static StringcreateLogTag(StreamSession session, Object channelId) static StringcreateLogTag(StreamSession session, StreamingChannel channel) getAggregator(TableId tableId) intintgetOutgoingStreamsForRanges(RangesAtEndpoint replicas, Collection<ColumnFamilyStore> stores, TimeUUID pendingRepair, PreviewKind previewKind) voidinit(StreamResultFuture streamResult) Bind this session to report to specificStreamResultFutureand perform pre-streaming initialization.booleanReturn if this session was failed or abortedbooleanbooleanbooleanReturn if this session completed successfully.voidmessageReceived(StreamMessage message) io.netty.util.concurrent.Future<?>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 aSessionFailedMessageand closes the session asStreamSession.State.FAILED.voidCall back when connection initialization is complete to start the prepare phase.planId()voidprepare(Collection<StreamRequest> requests, Collection<StreamSummary> summaries) Prepare this session for sending/receiving files.voidprepareAsync(Collection<StreamRequest> requests, Collection<StreamSummary> summaries) Finish preparing the session.voidprepareReceiving(StreamSummary summary) voidprogress(String filename, ProgressInfo.Direction direction, long bytes, long delta, long total) voidreceive(IncomingStreamMessage message) Call back after receiving a stream.voidprotected io.netty.util.concurrent.Future<?>sendControlMessage(StreamMessage message) voidCall back on receivingStreamMessage.Type.SESSION_FAILEDmessage.intvoidCall back on receivingStreamMessage.Type.SESSION_FAILEDmessage.voidstart()invoked by the node that begins the stream session (it may be sending files, receiving files, or both)state()voidstate(StreamSession.State newState) Set current state tonewState.voidstreamSent(OutgoingStreamMessage message) Call back after sending StreamMessageHeader.voidtaskCompleted(StreamReceiveTask completedTask) voidtaskCompleted(StreamTransferTask completedTask) toString()
-
Field Details
-
sink
-
peer
Streaming endpoint. EachStreamSessionis identified by this InetAddressAndPort which is broadcast address of the node streaming. -
requests
-
transfers
-
failureReason
-
-
Constructor Details
-
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 Details
-
isFollower
public boolean isFollower() -
planId
-
sessionIndex
public int sessionIndex() -
streamOperation
-
getStreamOperation
-
getPendingRepair
-
isPreview
public boolean isPreview() -
getPreviewKind
-
getAggregator
-
init
Bind this session to report to specificStreamResultFutureand perform pre-streaming initialization.- Parameters:
streamResult- result to report to
-
attachInbound
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
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(String keyspace, RangesAtEndpoint fullRanges, RangesAtEndpoint transientRanges, Collection<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 List<OutgoingStream> getOutgoingStreamsForRanges(RangesAtEndpoint replicas, Collection<ColumnFamilyStore> stores, TimeUUID pendingRepair, PreviewKind previewKind) -
state
Set current state tonewState.- Parameters:
newState- new state to set
-
state
- Returns:
- current state
-
getChannel
-
isSuccess
public boolean isSuccess()Return if this session completed successfully.- Returns:
- true if session completed successfully.
-
isFailedOrAborted
public boolean isFailedOrAborted()Return if this session was failed or aborted- Returns:
- true if session was failed or aborted
-
messageReceived
-
onInitializationComplete
public void onInitializationComplete()Call back when connection initialization is complete to start the prepare phase. -
onError
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 aSessionFailedMessageand closes the session asStreamSession.State.FAILED. -
prepare
Prepare this session for sending/receiving files. -
countStreamedIn
public void countStreamedIn(boolean isEntireSSTable) -
prepareAsync
Finish preparing the session. This method is blocking (memtables are flushed inaddTransferRanges(java.lang.String, org.apache.cassandra.locator.RangesAtEndpoint, java.util.Collection<java.lang.String>, boolean)), so the logic should not execute on the main IO thread (read: netty event loop). -
checkAvailableDiskSpaceAndCompactions
public static boolean checkAvailableDiskSpaceAndCompactions(Collection<StreamSummary> summaries, @Nullable TimeUUID planId, @Nullable 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
Call back after sending StreamMessageHeader.- Parameters:
message- sent stream message
-
sendControlMessage
-
receive
Call back after receiving a stream.- Parameters:
message- received stream
-
progress
public void progress(String filename, ProgressInfo.Direction direction, long bytes, long delta, long total) -
received
-
complete
public void complete()Check if session is completed on receivingStreamMessage.Type.COMPLETEmessage. -
sessionFailed
public void sessionFailed()Call back on receivingStreamMessage.Type.SESSION_FAILEDmessage. -
sessionTimeout
public void sessionTimeout()Call back on receivingStreamMessage.Type.SESSION_FAILEDmessage. -
getSessionInfo
- Returns:
- Current snapshot of this session info.
-
taskCompleted
-
taskCompleted
-
prepareReceiving
-
getNumRequests
public int getNumRequests() -
getNumTransfers
public int getNumTransfers() -
createLogTag
-
createLogTag
-
createLogTag
-
createLogTag
-
abort
public void abort() -
toString
-
boundStackTrace
-
boundStackTrace
public static StringBuilder boundStackTrace(Throwable e, int limit, int counter, Set<Throwable> visited, StringBuilder out)
-