Kudu C++ client API
|
Representation of a Kudu client session. More...
#include <client.h>
Public Types | |
enum | FlushMode { AUTO_FLUSH_SYNC, AUTO_FLUSH_BACKGROUND, MANUAL_FLUSH } |
Modes of flush operations. More... | |
enum | ExternalConsistencyMode { CLIENT_PROPAGATED, COMMIT_WAIT } |
The possible external consistency modes on which Kudu operates. More... | |
Public Member Functions | |
Status | SetFlushMode (FlushMode m) WARN_UNUSED_RESULT |
Status | SetExternalConsistencyMode (ExternalConsistencyMode m) WARN_UNUSED_RESULT |
Status | SetMutationBufferSpace (size_t size_bytes) WARN_UNUSED_RESULT |
void | SetTimeoutMillis (int millis) |
Status | Apply (KuduWriteOperation *write_op) WARN_UNUSED_RESULT |
void | ApplyAsync (KuduWriteOperation *write_op, KuduStatusCallback *cb) |
Status | Flush () WARN_UNUSED_RESULT |
void | FlushAsync (KuduStatusCallback *cb) |
Status | Close () WARN_UNUSED_RESULT |
bool | HasPendingOperations () const |
int | CountBufferedOperations () const |
int | CountPendingErrors () const |
void | GetPendingErrors (std::vector< KuduError * > *errors, bool *overflowed) |
KuduClient * | client () const |
Friends | |
class | KuduClient |
class | internal::Batcher |
Representation of a Kudu client session.
A KuduSession belongs to a specific KuduClient, and represents a context in which all read/write data access should take place. Within a session, multiple operations may be accumulated and batched together for better efficiency. Settings like timeouts, priorities, and trace IDs are also set per session.
A KuduSession's main purpose is for grouping together multiple data-access operations together into batches or transactions. It is important to note the distinction between these two:
KuduSession is separate from KuduTable because a given batch or transaction may span multiple tables. This is particularly important in the future when we add ACID support, but even in the context of batching, we may be able to coalesce writes to different tables hosted on the same server into the same RPC.
KuduSession is separate from KuduClient because, in a multi-threaded application, different threads may need to concurrently execute transactions. Similar to a JDBC "session", transaction boundaries will be delineated on a per-session basis – in between a "BeginTransaction" and "Commit" call on a given session, all operations will be part of the same transaction. Meanwhile another concurrent Session object can safely run non-transactional work or other transactions without interfering.
Additionally, there is a guarantee that writes from different sessions do not get batched together into the same RPCs – this means that latency-sensitive clients can run through the same KuduClient object as throughput-oriented clients, perhaps by setting the latency-sensitive session's timeouts low and priorities high. Without the separation of batches, a latency-sensitive single-row insert might get batched along with 10MB worth of inserts from the batch writer, thus delaying the response significantly.
Though we currently do not have transactional support, users will be forced to use a KuduSession to instantiate reads as well as writes. This will make it more straight-forward to add RW transactions in the future without significant modifications to the API.
Users who are familiar with the Hibernate ORM framework should find this concept of a Session familiar.
The possible external consistency modes on which Kudu operates.
Enumerator | |
---|---|
CLIENT_PROPAGATED |
The response to any write will contain a timestamp. Any further calls from the same client to other servers will update those servers with that timestamp. Following write operations from the same client will be assigned timestamps that are strictly higher, enforcing external consistency without having to wait or incur any latency penalties. In order to maintain external consistency for writes between two different clients in this mode, the user must forward the timestamp from the first client to the second by using KuduClient::GetLatestObservedTimestamp() and KuduClient::SetLatestObservedTimestamp(). This is the default external consistency mode.
|
COMMIT_WAIT |
The server will guarantee that write operations from the same or from other client are externally consistent, without the need to propagate timestamps across clients. This is done by making write operations wait until there is certainty that all follow up write operations (operations that start after the previous one finishes) will be assigned a timestamp that is strictly higher, enforcing external consistency.
|
Modes of flush operations.
Enumerator | |
---|---|
AUTO_FLUSH_SYNC |
Every write will be sent to the server in-band with the Apply() call. No batching will occur. In this mode, the Flush() call never has any effect, since each Apply() call has already flushed the buffer. This is the default flush mode. |
AUTO_FLUSH_BACKGROUND |
Apply() calls will return immediately, but the writes will be sent in the background, potentially batched together with other writes from the same session. If there is not sufficient buffer space, then Apply() will block for buffer space to be available. Because writes are applied in the background, any errors will be stored in a session-local buffer. Call CountPendingErrors() or GetPendingErrors() to retrieve them. The Flush() call can be used to block until the buffer is empty.
|
MANUAL_FLUSH |
Apply() calls will return immediately, and the writes will not be sent until the user calls Flush(). If the buffer runs past the configured space limit, then Apply() will return an error. |
Status kudu::client::KuduSession::Apply | ( | KuduWriteOperation * | write_op | ) |
Apply the write operation.
The behavior of this function depends on the current flush mode. Regardless of flush mode, however, Apply() may begin to perform processing in the background for the call (e.g. looking up the tablet, etc). Given that, an error may be queued into the PendingErrors structure prior to flushing, even in MANUAL_FLUSH
mode.
In case of any error, which may occur during flushing or because the write_op is malformed, the write_op is stored in the session's error collector which may be retrieved at any time.
[in] | write_op | Operation to apply. This method transfers the write_op's ownership to the KuduSession. |
void kudu::client::KuduSession::ApplyAsync | ( | KuduWriteOperation * | write_op, |
KuduStatusCallback * | cb | ||
) |
Apply the write operation asynchronously.
This method is similar to Apply(), except it never blocks. Even in the flush modes that return immediately, cb
is triggered with the result. The callback may be called by a reactor thread, or in some cases may be called inline by the same thread which calls ApplyAsync().
[in] | write_op | Operation to apply. This method transfers the write_op's ownership to the KuduSession. |
[in] | cb | Callback to report the status of the operation. The cb object must remain valid until it is called. |
KuduClient* kudu::client::KuduSession::client | ( | ) | const |
Status kudu::client::KuduSession::Close | ( | ) |
int kudu::client::KuduSession::CountBufferedOperations | ( | ) | const |
Get number of buffered operations (not the same as 'pending').
Note that this is different than HasPendingOperations() above, which includes operations which have been sent and not yet responded to. This is only relevant in MANUAL_FLUSH
mode, where the result will not decrease except for after a manual flush, after which point it will be 0. In the other flush modes, data is immediately put en-route to the destination, so this will return 0.
int kudu::client::KuduSession::CountPendingErrors | ( | ) | const |
Get error count for pending operations.
Errors may accumulate in session's lifetime; use this method to see how many errors happened since last call of GetPendingErrors() method.
Status kudu::client::KuduSession::Flush | ( | ) |
Flush any pending writes.
In AUTO_FLUSH_SYNC
mode, this has no effect, since every Apply() call flushes itself inline.
void kudu::client::KuduSession::FlushAsync | ( | KuduStatusCallback * | cb | ) |
Flush any pending writes asynchronously.
This method schedules a background flush of pending operations. Provided callback is invoked upon completion of the flush. If there were errors while flushing the operations, corresponding 'not OK' status is passed as a parameter for the callback invocation. Callers should then use GetPendingErrors() to determine which specific operations failed.
[in] | cb | Callback to call upon flush completion. The cb must remain valid until it is invoked. |
In the case that the async version of this method is used, then the callback will be called upon completion of the operations which were buffered since the last flush. In other words, in the following sequence:
... callback_2
will be triggered once b
has been inserted, regardless of whether a
has completed or not.
void kudu::client::KuduSession::GetPendingErrors | ( | std::vector< KuduError * > * | errors, |
bool * | overflowed | ||
) |
Get information on errors from previous session activity.
The information on errors are reset upon calling this method.
[out] | errors | Pointer to the container to fill with error info objects. Caller takes ownership of the returned errors in the container. |
[out] | overflowed | If there were more errors than could be held in the session's error storage, then overflowed is set to true . |
bool kudu::client::KuduSession::HasPendingOperations | ( | ) | const |
Check if there are any pending operations in this session.
true
if there are operations which have not yet been delivered to the cluster. This may include buffered operations (i.e. those that have not yet been flushed) as well as in-flight operations (i.e. those that are in the process of being sent to the servers).Status kudu::client::KuduSession::SetExternalConsistencyMode | ( | ExternalConsistencyMode | m | ) |
Set external consistency mode for the session.
[in] | m | External consistency mode to set. |
Set the flush mode.
[in] | m | Flush mode to set. |
Status kudu::client::KuduSession::SetMutationBufferSpace | ( | size_t | size_bytes | ) |
Set the amount of buffer space used by this session for outbound writes.
The effect of the buffer size varies based on the flush mode of the session:
[in] | size_bytes | Size of the buffer space to set (number of bytes). |
void kudu::client::KuduSession::SetTimeoutMillis | ( | int | millis | ) |
Set the timeout for writes made in this session.
[in] | millis | Timeout to set in milliseconds; should be greater than 0. |