Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
impala::BufferedBlockMgr Class Reference

#include <buffered-block-mgr.h>

Collaboration diagram for impala::BufferedBlockMgr:

Classes

class  Block
 
struct  BufferDescriptor
 Descriptor for a single memory buffer in the pool. More...
 
struct  Client
 

Public Member Functions

 ~BufferedBlockMgr ()
 
Status RegisterClient (int num_reserved_buffers, MemTracker *tracker, RuntimeState *state, Client **client)
 
void ClearReservations (Client *client)
 Clears all reservations for this client. More...
 
bool TryAcquireTmpReservation (Client *client, int num_buffers)
 
void ClearTmpReservation (Client *client)
 Sets tmp reservation to 0 on this client. More...
 
Status GetNewBlock (Client *client, Block *unpin_block, Block **block, int64_t len=-1)
 
void Cancel ()
 
std::string DebugString (Client *client=NULL)
 Dumps block mgr state. Grabs lock. If client is not NULL, also dumps its state. More...
 
bool ConsumeMemory (Client *client, int64_t size)
 
void ReleaseMemory (Client *client, int64_t size)
 
int64_t available_buffers (Client *client) const
 
Status MemLimitTooLowError (Client *client)
 
int available_allocated_buffers () const
 
int num_free_buffers () const
 
int num_pinned_buffers (Client *client) const
 
int num_reserved_buffers_remaining (Client *client) const
 
MemTrackerget_tracker (Client *client) const
 
int64_t max_block_size () const
 
int64_t bytes_allocated () const
 
RuntimeProfileprofile ()
 
int writes_issued () const
 

Static Public Member Functions

static Status Create (RuntimeState *state, MemTracker *parent, RuntimeProfile *profile, int64_t mem_limit, int64_t buffer_size, boost::shared_ptr< BufferedBlockMgr > *block_mgr)
 
static int GetNumReservedBlocks ()
 Return the number of blocks a block manager will reserve for its I/O buffers. More...
 

Private Types

typedef boost::unordered_map
< TUniqueId, boost::weak_ptr
< BufferedBlockMgr > > 
BlockMgrsMap
 

Private Member Functions

 BufferedBlockMgr (RuntimeState *state, int64_t block_size)
 
void Init (DiskIoMgr *io_mgr, RuntimeProfile *profile, MemTracker *parent_tracker, int64_t mem_limit)
 Initializes the block mgr. Idempotent and thread-safe. More...
 
Status InitTmpFiles ()
 
Status PinBlock (Block *block, bool *pinned, Block *src, bool unpin)
 
Status UnpinBlock (Block *block)
 
Status DeleteBlock (Block *block)
 
Status DeleteOrUnpinBlock (Block *block, bool unpin)
 
Status TransferBuffer (Block *dst, Block *src, bool unpin)
 
int64_t remaining_unreserved_buffers () const
 
Status FindBufferForBlock (Block *block, bool *in_mem)
 
Status FindBuffer (boost::unique_lock< boost::mutex > &lock, BufferDescriptor **buffer)
 
Status WriteUnpinnedBlocks ()
 
Status WriteUnpinnedBlock (Block *block)
 Issues the write for this block to the DiskIoMgr. More...
 
void WriteComplete (Block *block, const Status &write_status)
 
void ReturnUnusedBlock (Block *block)
 
BlockGetUnusedBlock (Client *client)
 
bool Validate () const
 Used to debug the state of the block manager. Lock must already be taken. More...
 
std::string DebugInternal () const
 
Status Encrypt (Block *block, uint8_t **outbuf)
 
void EncryptDone (Block *block)
 Deallocates temporary buffer alloced in Encrypt(). More...
 
Status Decrypt (Block *block)
 Decrypts the contents of buffer() in place. More...
 
void SetHash (Block *block)
 Takes a cryptographic hash of the data and sets hash_ with it. More...
 
Status VerifyHash (Block *block)
 Verifies that the contents of buffer() match those that were set by SetHash() More...
 

Private Attributes

const int64_t max_block_size_
 Size of the largest/default block in bytes. More...
 
const int block_write_threshold_
 
const bool disable_spill_
 
const TUniqueId query_id_
 
ObjectPool obj_pool_
 
boost::scoped_ptr< MemTrackermem_tracker_
 Track buffers allocated by the block manager. More...
 
boost::mutex lock_
 
bool initialized_
 If true, Init() has been called. More...
 
int unfullfilled_reserved_buffers_
 The total number of reserved buffers across all clients that are not pinned. More...
 
int total_pinned_buffers_
 The total number of pinned buffers across all clients. More...
 
int non_local_outstanding_writes_
 
boost::condition_variable buffer_available_cv_
 Signal availability of free buffers. More...
 
InternalQueue< Blockunpinned_blocks_
 
InternalQueue< Blockunused_blocks_
 
InternalQueue< BufferDescriptorfree_io_buffers_
 
std::list< BufferDescriptor * > all_io_buffers_
 All allocated io-sized buffers. More...
 
boost::ptr_vector
< TmpFileMgr::File
tmp_files_
 
int next_block_index_
 
DiskIoMgrio_mgr_
 DiskIoMgr handles to read and write blocks. More...
 
DiskIoMgr::RequestContextio_request_context_
 
bool is_cancelled_
 
boost::scoped_ptr< RuntimeProfileprofile_
 Counters and timers to track behavior. More...
 
RuntimeProfile::Countermem_limit_counter_
 These have a fixed value for the lifetime of the manager and show memory usage. More...
 
RuntimeProfile::Counterblock_size_counter_
 
RuntimeProfile::Countercreated_block_counter_
 Total number of blocks created. More...
 
RuntimeProfile::Counterrecycled_blocks_counter_
 Number of deleted blocks reused. More...
 
RuntimeProfile::Counterbuffered_pin_counter_
 Number of Pin() calls that did not require a disk read. More...
 
RuntimeProfile::Counterdisk_read_timer_
 Time taken for disk reads. More...
 
RuntimeProfile::Counterbuffer_wait_timer_
 Time spent waiting for a free buffer. More...
 
RuntimeProfile::Counterbytes_written_counter_
 Number of bytes written to disk (includes writes still queued in the IO manager). More...
 
RuntimeProfile::Counteroutstanding_writes_counter_
 Number of writes outstanding (issued but not completed). More...
 
RuntimeProfile::Counterencryption_timer_
 Time spent in disk spill encryption and decryption. More...
 
RuntimeProfile::Counterintegrity_check_timer_
 Time spent in disk spill integrity generation and checking. More...
 
int writes_issued_
 Number of writes issued. More...
 
const bool encryption_
 
const bool check_integrity_
 

Static Private Attributes

static SpinLock static_block_mgrs_lock_
 Protects query_to_block_mgrs_. More...
 
static BlockMgrsMap query_to_block_mgrs_
 

Friends

struct Client
 

Detailed Description

The BufferedBlockMgr is used to allocate and manage blocks of data using a fixed memory budget. Available memory is split into a pool of fixed-size memory buffers. When a client allocates or requests a block, the block is assigned a buffer from this pool and is 'pinned' in memory. Clients can also unpin a block, allowing the manager to reassign its buffer to a different block. The BufferedBlockMgr typically allocates blocks in IO buffer size to get maximal IO efficiency when spilling. Clients can also request smaller buffers that cannot spill (note that it would be possible to spill small buffers, but we currently do not allow it). This is useful to present the same block API and mem tracking for clients (one can use the block mgr API to mem track non-spillable (smaller) buffers). Clients that do partitioning (e.g. PHJ and PAGG) will start with these smaller buffer sizes to reduce the minimum buffering requirements and grow to max sized buffers as the input grows. For simplicity, these small buffers are not recycled (there's also not really a need since they are allocated all at once on query startup). These buffers are not counted against the reservation. The BufferedBlockMgr reserves one buffer per disk ('block_write_threshold_') for itself. When the number of free buffers falls below 'block_write_threshold', unpinned blocks are flushed in Last-In-First-Out order. (It is assumed that unpinned blocks are re-read in FIFO order). The TmpFileMgr is used to obtain file handles to write to within the tmp directories configured for Impala. It is expected to have one BufferedBlockMgr per query. All allocations that can grow proportional to the input size and that might need to spill to disk should allocate from the same BufferedBlockMgr. A client must pin a block in memory to read/write its contents and unpin it when it is no longer in active use. The BufferedBlockMgr guarantees that: a) The memory buffer assigned to a block is not removed or released while it is pinned. b) The contents of an unpinned block will be available on a subsequent call to pin. The Client supports the following operations: GetNewBlock(): Returns a new pinned block. Close(): Frees all memory and disk space. Called when a query is closed or cancelled. Close() is idempotent. A Block supports the following operations: Pin(): Pins a block to a buffer in memory, and reads its contents from disk if necessary. If there are no free buffers, waits for a buffer to become available. Invoked before the contents of a block are read or written. The block will be maintained in memory until Unpin() is called. Unpin(): Invoked to indicate the block is not in active use. The block is added to a list of unpinned blocks. Unpinned blocks are only written when the number of free blocks falls below the 'block_write_threshold'. Delete(): Invoked to deallocate a block. The buffer associated with the block is immediately released and its on-disk location (if any) reused. The block manager is thread-safe with the following caveat: A single block cannot be used simultaneously by multiple clients in any capacity. However, the block manager client is not thread-safe. That is, the block manager allows multiple single-threaded block manager clients. TODO: When a block is read from disk, data is copied from the IOMgr buffer to the block manager's buffer. This should be avoided in the common case where these buffers are of the same size. TODO: See if the one big lock is a bottleneck. Break it up. This object is shared by all operators within a query (across fragments), see IMPALA-1883. TODO: No reason we can't spill the smaller buffers. Add it if we need to (it's likely just removing dchecks). TODO: The requirements on this object has grown organically. Consider a major reworking.

Definition at line 92 of file buffered-block-mgr.h.

Member Typedef Documentation

typedef boost::unordered_map<TUniqueId, boost::weak_ptr<BufferedBlockMgr> > impala::BufferedBlockMgr::BlockMgrsMap
private

All per-query BufferedBlockMgr objects that are in use. For memory management, this map contains only weak ptrs. BufferedBlockMgrs that are handed out are shared ptrs. When all the shared ptrs are no longer referenced, the BufferedBlockMgr d'tor will be called at which point the weak ptr will be removed from the map.

Definition at line 620 of file buffered-block-mgr.h.

Constructor & Destructor Documentation

impala::BufferedBlockMgr::BufferedBlockMgr ( RuntimeState state,
int64_t  block_size 
)
private

Definition at line 182 of file buffered-block-mgr.cc.

Referenced by Create().

Member Function Documentation

int impala::BufferedBlockMgr::available_allocated_buffers ( ) const
inline

TODO: Remove these two. Not clear what the sorter really needs. TODO: Those are dirty, dangerous reads to two lists whose all other accesses are protected by the lock_. Using those two functions is looking for trouble.

Definition at line 380 of file buffered-block-mgr.h.

References all_io_buffers_.

Referenced by impala::Sorter::InputDone(), and impala::Sorter::MergeIntermediateRuns().

int64_t impala::BufferedBlockMgr::available_buffers ( Client client) const

The number of buffers available for client. That is, if all other clients were stopped, the number of buffers this client could get.

Definition at line 221 of file buffered-block-mgr.cc.

References impala::BufferedBlockMgr::Client::num_pinned_buffers_, impala::BufferedBlockMgr::Client::num_reserved_buffers_, impala::BufferedBlockMgr::Client::num_tmp_reserved_buffers_, and remaining_unreserved_buffers().

Referenced by TryAcquireTmpReservation().

int64_t impala::BufferedBlockMgr::bytes_allocated ( ) const

Definition at line 510 of file buffered-block-mgr.cc.

References mem_tracker_.

void impala::BufferedBlockMgr::Cancel ( )

Cancels the block mgr. All subsequent calls fail with Status::CANCELLED. Idempotent.

Definition at line 359 of file buffered-block-mgr.cc.

References impala::DiskIoMgr::CancelContext(), io_mgr_, io_request_context_, is_cancelled_, and lock_.

Referenced by impala::BufferedBlockMgrTest::TestRandomInternalImpl().

void impala::BufferedBlockMgr::ClearTmpReservation ( Client client)

Sets tmp reservation to 0 on this client.

Definition at line 272 of file buffered-block-mgr.cc.

References lock_, impala::BufferedBlockMgr::Client::num_tmp_reserved_buffers_, and unfullfilled_reserved_buffers_.

bool impala::BufferedBlockMgr::ConsumeMemory ( Client client,
int64_t  size 
)
Status impala::BufferedBlockMgr::Create ( RuntimeState state,
MemTracker parent,
RuntimeProfile profile,
int64_t  mem_limit,
int64_t  buffer_size,
boost::shared_ptr< BufferedBlockMgr > *  block_mgr 
)
static

Create a block manager with the specified mem_limit. If a block mgr with the same query id has already been created, that block mgr is returned.

  • mem_limit: maximum memory that will be used by the block mgr.
  • buffer_size: maximum size of each buffer.

Definition at line 199 of file buffered-block-mgr.cc.

References BufferedBlockMgr(), impala::RuntimeState::io_mgr(), impala::Status::OK, profile(), impala::RuntimeState::query_id(), query_to_block_mgrs_, and static_block_mgrs_lock_.

Referenced by impala::RuntimeState::CreateBlockMgr(), impala::SimpleTupleStreamTest::CreateMgr(), and impala::BufferedBlockMgrTest::CreateMgr().

string impala::BufferedBlockMgr::DebugString ( Client client = NULL)

Dumps block mgr state. Grabs lock. If client is not NULL, also dumps its state.

Definition at line 1082 of file buffered-block-mgr.cc.

References DebugInternal(), impala::BufferedBlockMgr::Client::DebugString(), and lock_.

Referenced by impala::BufferedTupleStream::NextBlockForRead(), impala::BufferedTupleStream::PinStream(), and impala::BufferedBlockMgr::Block::Validate().

Status impala::BufferedBlockMgr::DeleteOrUnpinBlock ( Block block,
bool  unpin 
)
private

If the 'block' is NULL, checks if cancelled and returns. Otherwise, depending on 'unpin' calls either DeleteBlock() or UnpinBlock(), which both first check for cancellation. It should be called without the lock_ acquired.

Definition at line 528 of file buffered-block-mgr.cc.

References impala::Status::CANCELLED, impala::BufferedBlockMgr::Block::Delete(), is_cancelled_, lock_, impala::Status::OK, and impala::BufferedBlockMgr::Block::Unpin().

Referenced by PinBlock().

void impala::BufferedBlockMgr::EncryptDone ( Block block)
private

Deallocates temporary buffer alloced in Encrypt().

Definition at line 1233 of file buffered-block-mgr.cc.

References impala::BufferedBlockMgr::Block::encrypted_write_buffer_, and encryption_.

Referenced by WriteComplete().

Status impala::BufferedBlockMgr::FindBuffer ( boost::unique_lock< boost::mutex > &  lock,
BufferDescriptor **  buffer 
)
private

Returns a new buffer that can be used. *buffer is set to NULL if there was no memory. Otherwise, this function gets a new buffer by:

  1. Allocating a new buffer if possible
  2. Using a buffer from the free list (which is populated by moving blocks from the unpinned list by writing them out). Must be called with the lock_ already taken. This function can block.

Definition at line 936 of file buffered-block-mgr.cc.

References impala::ObjectPool::Add(), all_io_buffers_, block_write_threshold_, buffer_available_cv_, buffer_wait_timer_, impala::Status::CANCELLED, DebugInternal(), disable_spill_, free_io_buffers_, is_cancelled_, max_block_size_, mem_tracker_, non_local_outstanding_writes_, obj_pool_, impala::Status::OK, RETURN_IF_ERROR, SCOPED_TIMER, unpinned_blocks_, and WriteUnpinnedBlocks().

Referenced by ConsumeMemory(), and FindBufferForBlock().

Status impala::BufferedBlockMgr::FindBufferForBlock ( Block block,
bool in_mem 
)
private

Finds a buffer for a block and pins it. If the block's buffer has not been evicted, it removes the block from the unpinned list and sets *in_mem = true. If the block is not in memory, it will call FindBuffer() that may block. If we can't get a buffer (e.g. no more memory, nothing in the unpinned and free lists) this function returns with the block unpinned. Uses the lock_, the caller should not have already acquired the lock_.

Definition at line 836 of file buffered-block-mgr.cc.

References impala::RuntimeProfile::Counter::Add(), impala::Status::AddDetail(), impala::BufferedBlockMgr::BufferDescriptor::block, impala::BufferedBlockMgr::Block::buffer_desc_, buffered_pin_counter_, impala::Status::CANCELLED, impala::BufferedBlockMgr::Block::client_, DebugInternal(), impala::BufferedBlockMgr::Client::DebugString(), impala::BufferedBlockMgr::Block::DebugString(), FindBuffer(), free_io_buffers_, impala::GetStackTrace(), impala::BufferedBlockMgr::Block::in_write_, is_cancelled_, impala::BufferedBlockMgr::Block::is_deleted_, impala::BufferedBlockMgr::Block::is_pinned_, lock_, impala::Status::MEM_LIMIT_EXCEEDED, impala::BufferedBlockMgr::Client::num_pinned_buffers_, impala::BufferedBlockMgr::Client::num_reserved_buffers_, impala::BufferedBlockMgr::Client::num_tmp_reserved_buffers_, impala::Status::OK, impala::BufferedBlockMgr::Client::PinBuffer(), query_id_, remaining_unreserved_buffers(), RETURN_IF_ERROR, total_pinned_buffers_, unfullfilled_reserved_buffers_, unpinned_blocks_, impala::BufferedBlockMgr::Block::Validate(), Validate(), VLOG_QUERY, VLOG_QUERY_IS_ON, and WriteUnpinnedBlocks().

Referenced by GetNewBlock(), and PinBlock().

MemTracker * impala::BufferedBlockMgr::get_tracker ( Client client) const
Status impala::BufferedBlockMgr::GetNewBlock ( Client client,
Block unpin_block,
Block **  block,
int64_t  len = -1 
)

Return a new pinned block. If there is no memory for this block, *block will be set to NULL. If len > 0, GetNewBlock() will return a block with a buffer of size len. len must be less than max_block_size and this block cannot be unpinned. This function will try to allocate new memory for the block up to the limit. Otherwise it will (conceptually) write out an unpinned block and use that memory. The caller can pass a non-NULL 'unpin_block' to transfer memory from 'unpin_block' to the new block. If 'unpin_block' is non-NULL, the new block can never fail to get a buffer. The semantics of this are:

  • If 'unpin_block' is non-NULL, it must be pinned.
  • If the call succeeds, 'unpin_block' is unpinned.
  • If there is no memory pressure, block will get a newly allocated buffer.
  • If there is memory pressure, block will get the buffer from 'unpin_block'.

Definition at line 382 of file buffered-block-mgr.cc.

References impala::ObjectPool::Add(), impala::BufferedBlockMgr::BufferDescriptor::block, impala::BufferedBlockMgr::Block::buffer_desc_, impala::Status::CANCELLED, impala::BufferedBlockMgr::Block::client_, impala::BufferedBlockMgr::Block::DebugString(), FindBufferForBlock(), GetUnusedBlock(), is_cancelled_, impala::BufferedBlockMgr::Block::is_deleted_, impala::BufferedBlockMgr::Block::is_pinned(), impala::BufferedBlockMgr::Block::is_pinned_, lock_, max_block_size_, obj_pool_, impala::Status::OK, impala::BufferedBlockMgr::Client::PinBuffer(), RETURN_IF_ERROR, ReturnUnusedBlock(), total_pinned_buffers_, impala::BufferedBlockMgr::Client::tracker_, TransferBuffer(), impala::MemTracker::TryConsume(), impala::BufferedBlockMgr::Block::Unpin(), and impala::BufferedBlockMgr::Block::Validate().

Referenced by impala::BufferedBlockMgrTest::AllocateBlocks(), impala::BufferedBlockMgrTest::GetFreeBlock(), impala::HashTable::GrowNodeArray(), impala::BufferedTupleStream::NewBlockForWrite(), and impala::BufferedBlockMgrTest::TestRandomInternalImpl().

static int impala::BufferedBlockMgr::GetNumReservedBlocks ( )
inlinestatic

Return the number of blocks a block manager will reserve for its I/O buffers.

Definition at line 331 of file buffered-block-mgr.h.

References impala::TmpFileMgr::num_tmp_devices().

BufferedBlockMgr::Block * impala::BufferedBlockMgr::GetUnusedBlock ( Client client)
private

Checks unused_blocks_ for an unused block object, else allocates a new one. Non-blocking and needs no lock_.

Definition at line 979 of file buffered-block-mgr.cc.

References impala::ObjectPool::Add(), impala::RuntimeProfile::Counter::Add(), impala::BufferedBlockMgr::Block::client_, created_block_counter_, impala::BufferedBlockMgr::Block::Init(), obj_pool_, recycled_blocks_counter_, and unused_blocks_.

Referenced by GetNewBlock().

Status impala::BufferedBlockMgr::InitTmpFiles ( )
private

Initializes tmp_files_. This is initialized the first time we need to write to disk. Must be called with lock_ taken.

Definition at line 1147 of file buffered-block-mgr.cc.

References impala::TmpFileMgr::GetFile(), next_block_index_, impala::TmpFileMgr::num_tmp_devices(), impala::Status::OK, query_id_, RETURN_IF_ERROR, and tmp_files_.

Referenced by WriteUnpinnedBlock().

int impala::BufferedBlockMgr::num_free_buffers ( ) const
inline

Definition at line 381 of file buffered-block-mgr.h.

References free_io_buffers_.

Referenced by impala::Sorter::InputDone().

int impala::BufferedBlockMgr::num_pinned_buffers ( Client client) const
int impala::BufferedBlockMgr::num_reserved_buffers_remaining ( Client client) const
Status impala::BufferedBlockMgr::PinBlock ( Block block,
bool pinned,
Block src,
bool  unpin 
)
private

PinBlock(), UnpinBlock(), DeleteBlock() perform the actual work of Block::Pin(), Unpin() and Delete(). Must be called with the lock_ taken.

Definition at line 536 of file buffered-block-mgr.cc.

References impala::ObjectPool::Add(), impala::DiskIoMgr::AddScanRanges(), impala::BufferedBlockMgr::Block::buffer(), impala::DiskIoMgr::BufferDescriptor::buffer(), impala::BufferedBlockMgr::Block::buffer_desc_, check_integrity_, impala::BufferedBlockMgr::Block::client_, impala::BufferedBlockMgr::Block::DebugString(), Decrypt(), DeleteOrUnpinBlock(), impala::DiskIoMgr::RequestRange::disk_id(), disk_read_timer_, encryption_, impala::DiskIoMgr::BufferDescriptor::eosr(), impala::DiskIoMgr::RequestRange::file(), FindBufferForBlock(), free_io_buffers_, impala::DiskIoMgr::ScanRange::GetNext(), impala::BufferedBlockMgr::Block::in_write_, io_mgr_, io_request_context_, impala::BufferedBlockMgr::Block::is_deleted_, impala::BufferedBlockMgr::Block::is_pinned_, impala::DiskIoMgr::BufferDescriptor::len(), impala::DiskIoMgr::RequestRange::len(), lock_, impala::DiskIoMgr::ScanRange::NEVER_CACHE, obj_pool_, offset, impala::DiskIoMgr::RequestRange::offset(), impala::Status::OK, impala::BufferedBlockMgr::Client::PinBuffer(), impala::DiskIoMgr::ScanRange::Reset(), impala::DiskIoMgr::BufferDescriptor::Return(), RETURN_IF_ERROR, SCOPED_TIMER, total_pinned_buffers_, TransferBuffer(), unpinned_blocks_, impala::BufferedBlockMgr::Block::valid_data_len_, VerifyHash(), impala::BufferedBlockMgr::Block::write_range_, and WriteUnpinnedBlocks().

RuntimeProfile* impala::BufferedBlockMgr::profile ( )
inline

Definition at line 388 of file buffered-block-mgr.h.

References profile_.

Referenced by Create(), and Init().

Status impala::BufferedBlockMgr::RegisterClient ( int  num_reserved_buffers,
MemTracker tracker,
RuntimeState state,
Client **  client 
)

Registers a client with num_reserved_buffers. The returned client is owned by the BufferedBlockMgr and has the same lifetime as it. We allow oversubscribing the reserved buffers. It is likely that the num_reserved_buffers will be very pessimistic for small queries and we don't want to fail all of them with mem limit exceeded. The min reserved buffers is often independent of data size and we still want to run small queries with very small limits. If tracker is non-NULL, buffers used by this client are reflected in tracker. TODO: The fact that we allow oversubscription is problematic. as the code expects the reservations to always be granted (currently not the case).

Definition at line 235 of file buffered-block-mgr.cc.

References impala::ObjectPool::Add(), Client, lock_, obj_pool_, impala::Status::OK, and unfullfilled_reserved_buffers_.

Referenced by impala::Sorter::Init(), impala::AnalyticEvalNode::Open(), impala::PartitionedHashJoinNode::Prepare(), impala::PartitionedAggregationNode::Prepare(), and impala::BufferedBlockMgrTest::TestRandomInternalImpl().

void impala::BufferedBlockMgr::ReleaseMemory ( Client client,
int64_t  size 
)
int64_t impala::BufferedBlockMgr::remaining_unreserved_buffers ( ) const
private

Returns the total number of unreserved buffers. This is the sum of unpinned, free and buffers we can still allocate minus the total number of reserved buffers that are not pinned. Note this can be negative if the buffers are oversubscribed. Must be called with lock_ taken.

Definition at line 227 of file buffered-block-mgr.cc.

References free_io_buffers_, max_block_size(), mem_tracker_, non_local_outstanding_writes_, unfullfilled_reserved_buffers_, and unpinned_blocks_.

Referenced by available_buffers(), ConsumeMemory(), DebugInternal(), and FindBufferForBlock().

void impala::BufferedBlockMgr::ReturnUnusedBlock ( Block block)
private

Returns a deleted block to the list of free blocks. Assumes the block's buffer has already been returned to the free buffers list. Non-blocking. Thread-safe and does not need the lock_ acquired.

Definition at line 828 of file buffered-block-mgr.cc.

References impala::BufferedBlockMgr::Block::buffer_desc_, impala::BufferedBlockMgr::Block::DebugString(), impala::BufferedBlockMgr::Block::Init(), impala::BufferedBlockMgr::Block::is_deleted_, impala::BufferedBlockMgr::Block::is_pinned_, and unused_blocks_.

Referenced by DeleteBlock(), GetNewBlock(), TransferBuffer(), and WriteComplete().

void impala::BufferedBlockMgr::SetHash ( Block block)
private
Status impala::BufferedBlockMgr::TransferBuffer ( Block dst,
Block src,
bool  unpin 
)
private

Transfers the buffer from 'src' to 'dst'. 'src' must be pinned. If unpin == false, 'src' is simply deleted. If unpin == true, 'src' is unpinned and it may block until the write of 'src' is completed. In that case it will use the lock_ for the condvar. Thus, the lock_ needs to not have been taken when this function is called.

Definition at line 440 of file buffered-block-mgr.cc.

References impala::BufferedBlockMgr::BufferDescriptor::block, impala::BufferedBlockMgr::Block::buffer_desc_, impala::Status::CANCELLED, impala::BufferedBlockMgr::Block::client_local_, impala::BufferedBlockMgr::Block::in_write_, is_cancelled_, impala::BufferedBlockMgr::Block::is_deleted_, impala::BufferedBlockMgr::Block::is_pinned_, impala::BufferedBlockMgr::BufferDescriptor::len, lock_, max_block_size_, impala::Status::OK, impala::Status::ok(), ReturnUnusedBlock(), impala::BufferedBlockMgr::Block::write_complete_cv_, and WriteUnpinnedBlock().

Referenced by GetNewBlock(), and PinBlock().

bool impala::BufferedBlockMgr::TryAcquireTmpReservation ( Client client,
int  num_buffers 
)

Tries to acquire a one-time reservation of num_buffers. The semantics are:

  • If this call fails, the next 'num_buffers' calls to Pin()/GetNewBlock() might not have enough memory.
  • If this call succeeds, the next 'num_buffers' call to Pin()/GetNewBlock() will be guaranteed to get the block. Once these blocks have been pinned, the reservation from this call has no more effect. Blocks coming from the tmp reservation also count towards the regular reservation. This is useful to Pin() a number of blocks and guarantee all or nothing behavior.

Definition at line 257 of file buffered-block-mgr.cc.

References available_buffers(), lock_, impala::BufferedBlockMgr::Client::num_pinned_buffers_, impala::BufferedBlockMgr::Client::num_reserved_buffers_, impala::BufferedBlockMgr::Client::num_tmp_reserved_buffers_, and unfullfilled_reserved_buffers_.

Referenced by impala::BufferedTupleStream::PinStream().

Status impala::BufferedBlockMgr::VerifyHash ( Block block)
private
void impala::BufferedBlockMgr::WriteComplete ( Block block,
const Status write_status 
)
private
int impala::BufferedBlockMgr::writes_issued ( ) const
inline

Definition at line 389 of file buffered-block-mgr.h.

References writes_issued_.

Status impala::BufferedBlockMgr::WriteUnpinnedBlocks ( )
private

Writes unpinned blocks via DiskIoMgr until one of the following is true: 1) The number of outstanding writes >= (block_write_threshold_ - num free buffers) 2) There are no more unpinned blocks Must be called with the lock_ already taken. Is not blocking.

Definition at line 643 of file buffered-block-mgr.cc.

References block_write_threshold_, impala::BufferedBlockMgr::Block::client_local_, DebugInternal(), disable_spill_, free_io_buffers_, non_local_outstanding_writes_, impala::Status::OK, RETURN_IF_ERROR, unpinned_blocks_, Validate(), and WriteUnpinnedBlock().

Referenced by ConsumeMemory(), FindBuffer(), FindBufferForBlock(), PinBlock(), UnpinBlock(), and WriteComplete().

Friends And Related Function Documentation

friend struct Client
friend

Definition at line 392 of file buffered-block-mgr.h.

Referenced by RegisterClient().

Member Data Documentation

std::list<BufferDescriptor*> impala::BufferedBlockMgr::all_io_buffers_
private

All allocated io-sized buffers.

Definition at line 556 of file buffered-block-mgr.h.

Referenced by available_allocated_buffers(), ConsumeMemory(), FindBuffer(), Validate(), and ~BufferedBlockMgr().

RuntimeProfile::Counter* impala::BufferedBlockMgr::block_size_counter_
private

Definition at line 580 of file buffered-block-mgr.h.

Referenced by Init().

const int impala::BufferedBlockMgr::block_write_threshold_
private

Unpinned blocks are written when the number of free buffers is below this threshold. Equal to the number of disks.

Definition at line 500 of file buffered-block-mgr.h.

Referenced by DebugInternal(), FindBuffer(), Validate(), and WriteUnpinnedBlocks().

boost::condition_variable impala::BufferedBlockMgr::buffer_available_cv_
private

Signal availability of free buffers.

Definition at line 534 of file buffered-block-mgr.h.

Referenced by DeleteBlock(), FindBuffer(), and WriteComplete().

RuntimeProfile::Counter* impala::BufferedBlockMgr::buffer_wait_timer_
private

Time spent waiting for a free buffer.

Definition at line 595 of file buffered-block-mgr.h.

Referenced by FindBuffer(), and Init().

RuntimeProfile::Counter* impala::BufferedBlockMgr::buffered_pin_counter_
private

Number of Pin() calls that did not require a disk read.

Definition at line 589 of file buffered-block-mgr.h.

Referenced by FindBufferForBlock(), and Init().

RuntimeProfile::Counter* impala::BufferedBlockMgr::bytes_written_counter_
private

Number of bytes written to disk (includes writes still queued in the IO manager).

Definition at line 598 of file buffered-block-mgr.h.

Referenced by Init(), and WriteUnpinnedBlock().

const bool impala::BufferedBlockMgr::check_integrity_
private

Set to true if –disk_spill_encryption is true. We can split this into a different flag in the future, but there is little performance overhead in the integrity check and hence no real reason to keep this separate from encryption. When true, blocks will have an integrity check (SHA-256) performed after being read from disk.

Definition at line 647 of file buffered-block-mgr.h.

Referenced by PinBlock(), SetHash(), VerifyHash(), and WriteUnpinnedBlock().

RuntimeProfile::Counter* impala::BufferedBlockMgr::created_block_counter_
private

Total number of blocks created.

Definition at line 583 of file buffered-block-mgr.h.

Referenced by GetUnusedBlock(), and Init().

const bool impala::BufferedBlockMgr::disable_spill_
private

If true, spilling is disabled. The client calls will fail if there is not enough memory.

Definition at line 504 of file buffered-block-mgr.h.

Referenced by FindBuffer(), Validate(), and WriteUnpinnedBlocks().

RuntimeProfile::Counter* impala::BufferedBlockMgr::disk_read_timer_
private

Time taken for disk reads.

Definition at line 592 of file buffered-block-mgr.h.

Referenced by Init(), and PinBlock().

const bool impala::BufferedBlockMgr::encryption_
private

Set to true if –disk_spill_encryption is true. When true, blocks will be encrypted before being written to disk.

Definition at line 641 of file buffered-block-mgr.h.

Referenced by Decrypt(), Encrypt(), EncryptDone(), Init(), PinBlock(), SetHash(), WriteComplete(), and WriteUnpinnedBlock().

RuntimeProfile::Counter* impala::BufferedBlockMgr::encryption_timer_
private

Time spent in disk spill encryption and decryption.

Definition at line 604 of file buffered-block-mgr.h.

Referenced by Decrypt(), Encrypt(), and Init().

InternalQueue<BufferDescriptor> impala::BufferedBlockMgr::free_io_buffers_
private

List of buffers that can be assigned to a block in Pin() or GetNewBlock(). These buffers either have no block associated with them or are associated with an an unpinned block that has been persisted. That is, either block = NULL or (!block->is_pinned_ && !block->in_write_ && !unpinned_blocks_.Contains(block)). All of these buffers are io sized.

Definition at line 553 of file buffered-block-mgr.h.

Referenced by DebugInternal(), DeleteBlock(), FindBuffer(), FindBufferForBlock(), num_free_buffers(), PinBlock(), remaining_unreserved_buffers(), Validate(), WriteComplete(), and WriteUnpinnedBlocks().

bool impala::BufferedBlockMgr::initialized_
private

If true, Init() has been called.

Definition at line 521 of file buffered-block-mgr.h.

Referenced by Init().

RuntimeProfile::Counter* impala::BufferedBlockMgr::integrity_check_timer_
private

Time spent in disk spill integrity generation and checking.

Definition at line 607 of file buffered-block-mgr.h.

Referenced by Init(), SetHash(), and VerifyHash().

DiskIoMgr* impala::BufferedBlockMgr::io_mgr_
private

DiskIoMgr handles to read and write blocks.

Definition at line 567 of file buffered-block-mgr.h.

Referenced by Cancel(), PinBlock(), WriteUnpinnedBlock(), and ~BufferedBlockMgr().

DiskIoMgr::RequestContext* impala::BufferedBlockMgr::io_request_context_
private

Definition at line 568 of file buffered-block-mgr.h.

Referenced by Cancel(), Init(), PinBlock(), WriteUnpinnedBlock(), and ~BufferedBlockMgr().

bool impala::BufferedBlockMgr::is_cancelled_
private

If true, a disk write failed and all API calls return. Status::CANCELLED. Set to true if there was an error writing a block, or if WriteComplete() needed to reissue the write and that failed.

Definition at line 573 of file buffered-block-mgr.h.

Referenced by Cancel(), DeleteBlock(), DeleteOrUnpinBlock(), FindBuffer(), FindBufferForBlock(), GetNewBlock(), TransferBuffer(), UnpinBlock(), Validate(), and WriteComplete().

boost::mutex impala::BufferedBlockMgr::lock_
private

This lock protects the block and buffer lists below, except for unused_blocks_. It also protects the various counters and changes to block state. Additionally, it is used for the blocking condvars: buffer_available_cv_ and block->write_complete_cv_. TODO: We should break the protection of the various structures and usages to different spinlocks and a mutex to be used in the wait()s, see IMPALA-1883.

Definition at line 518 of file buffered-block-mgr.h.

Referenced by Cancel(), ClearReservations(), ClearTmpReservation(), ConsumeMemory(), DebugString(), DeleteBlock(), DeleteOrUnpinBlock(), FindBufferForBlock(), GetNewBlock(), Init(), PinBlock(), RegisterClient(), TransferBuffer(), TryAcquireTmpReservation(), UnpinBlock(), and WriteComplete().

const int64_t impala::BufferedBlockMgr::max_block_size_
private

Size of the largest/default block in bytes.

Definition at line 496 of file buffered-block-mgr.h.

Referenced by DebugInternal(), DeleteBlock(), FindBuffer(), GetNewBlock(), Init(), max_block_size(), TransferBuffer(), UnpinBlock(), Validate(), WriteComplete(), and WriteUnpinnedBlock().

RuntimeProfile::Counter* impala::BufferedBlockMgr::mem_limit_counter_
private

These have a fixed value for the lifetime of the manager and show memory usage.

Definition at line 579 of file buffered-block-mgr.h.

Referenced by Init().

boost::scoped_ptr<MemTracker> impala::BufferedBlockMgr::mem_tracker_
private

Track buffers allocated by the block manager.

Definition at line 511 of file buffered-block-mgr.h.

Referenced by bytes_allocated(), ConsumeMemory(), DebugInternal(), FindBuffer(), Init(), ReleaseMemory(), remaining_unreserved_buffers(), and ~BufferedBlockMgr().

int impala::BufferedBlockMgr::next_block_index_
private

Index into tmp_files_ denoting the file to which the next block to be persisted will be written.

Definition at line 564 of file buffered-block-mgr.h.

Referenced by InitTmpFiles(), and WriteUnpinnedBlock().

int impala::BufferedBlockMgr::non_local_outstanding_writes_
private

Number of outstanding writes (Writes issued but not completed). This does not include client-local writes.

Definition at line 531 of file buffered-block-mgr.h.

Referenced by FindBuffer(), remaining_unreserved_buffers(), Validate(), WriteComplete(), WriteUnpinnedBlocks(), and ~BufferedBlockMgr().

ObjectPool impala::BufferedBlockMgr::obj_pool_
private
RuntimeProfile::Counter* impala::BufferedBlockMgr::outstanding_writes_counter_
private

Number of writes outstanding (issued but not completed).

Definition at line 601 of file buffered-block-mgr.h.

Referenced by DebugInternal(), Init(), WriteComplete(), and WriteUnpinnedBlock().

boost::scoped_ptr<RuntimeProfile> impala::BufferedBlockMgr::profile_
private

Counters and timers to track behavior.

Definition at line 576 of file buffered-block-mgr.h.

Referenced by Init(), and profile().

const TUniqueId impala::BufferedBlockMgr::query_id_
private
BufferedBlockMgr::BlockMgrsMap impala::BufferedBlockMgr::query_to_block_mgrs_
staticprivate

Definition at line 621 of file buffered-block-mgr.h.

Referenced by Create(), and ~BufferedBlockMgr().

RuntimeProfile::Counter* impala::BufferedBlockMgr::recycled_blocks_counter_
private

Number of deleted blocks reused.

Definition at line 586 of file buffered-block-mgr.h.

Referenced by GetUnusedBlock(), and Init().

SpinLock impala::BufferedBlockMgr::static_block_mgrs_lock_
staticprivate

Protects query_to_block_mgrs_.

Definition at line 613 of file buffered-block-mgr.h.

Referenced by Create(), and ~BufferedBlockMgr().

boost::ptr_vector<TmpFileMgr::File> impala::BufferedBlockMgr::tmp_files_
private

Temporary physical file handle, (one per tmp device) to which blocks may be written. Blocks are round-robined across these files.

Definition at line 560 of file buffered-block-mgr.h.

Referenced by InitTmpFiles(), WriteUnpinnedBlock(), and ~BufferedBlockMgr().

int impala::BufferedBlockMgr::total_pinned_buffers_
private

The total number of pinned buffers across all clients.

Definition at line 527 of file buffered-block-mgr.h.

Referenced by DebugInternal(), DeleteBlock(), FindBufferForBlock(), GetNewBlock(), PinBlock(), UnpinBlock(), and Validate().

int impala::BufferedBlockMgr::unfullfilled_reserved_buffers_
private

The total number of reserved buffers across all clients that are not pinned.

Definition at line 524 of file buffered-block-mgr.h.

Referenced by ClearReservations(), ClearTmpReservation(), ConsumeMemory(), DebugInternal(), DeleteBlock(), FindBufferForBlock(), RegisterClient(), remaining_unreserved_buffers(), TryAcquireTmpReservation(), and UnpinBlock().

InternalQueue<Block> impala::BufferedBlockMgr::unpinned_blocks_
private

List of blocks is_pinned_ = false AND are not on DiskIoMgr's write queue. Blocks are added to and removed from the back of the list. (i.e. in LIFO order). Blocks in this list must have is_pinned_ = false, in_write_ = false, is_deleted_ = false.

Definition at line 540 of file buffered-block-mgr.h.

Referenced by DebugInternal(), DeleteBlock(), FindBuffer(), FindBufferForBlock(), PinBlock(), remaining_unreserved_buffers(), UnpinBlock(), Validate(), and WriteUnpinnedBlocks().

InternalQueue<Block> impala::BufferedBlockMgr::unused_blocks_
private

List of blocks that have been deleted and are no longer in use. Can be reused in GetNewBlock(). Blocks in this list must be in the Init'ed state, i.e. buffer_desc_ = NULL, is_pinned_ = false, in_write_ = false, is_deleted_ = false, valid_data_len = 0.

Definition at line 546 of file buffered-block-mgr.h.

Referenced by GetUnusedBlock(), and ReturnUnusedBlock().

int impala::BufferedBlockMgr::writes_issued_
private

Number of writes issued.

Definition at line 610 of file buffered-block-mgr.h.

Referenced by writes_issued(), and WriteUnpinnedBlock().


The documentation for this class was generated from the following files: