Skip to content

Commit

Permalink
use cshared< Chunk > for both select/add api
Browse files Browse the repository at this point in the history
  • Loading branch information
JacksonYao287 committed Sep 12, 2023
1 parent 38d5399 commit 87070a1
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 15 deletions.
6 changes: 3 additions & 3 deletions src/include/homestore/chunk_selector.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ class Chunk;
class ChunkSelector {
public:
ChunkSelector() = default;
virtual void add_chunk(VChunk chunk) = 0;
virtual void foreach_chunks(std::function< void(VChunk&) >&& cb) = 0;
virtual Chunk* select_chunk(blk_count_t nblks, const blk_alloc_hints& hints) = 0;
virtual void add_chunk(cshared< Chunk >&) = 0;
virtual void foreach_chunks(std::function< void(cshared< Chunk >&) >&& cb) = 0;
virtual cshared< Chunk > select_chunk(blk_count_t nblks, const blk_alloc_hints& hints) = 0;

virtual ~ChunkSelector() = default;
};
Expand Down
2 changes: 1 addition & 1 deletion src/include/homestore/vchunk.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class VChunk {
const uint8_t* get_user_private() const;
blk_cap_t available_blks() const;
uint32_t get_pdev_id() const;
cshared< Chunk > get_internal_chunk () const;
cshared< Chunk > get_internal_chunk() const;

private:
cshared< Chunk > internalChunk;
Expand Down
8 changes: 4 additions & 4 deletions src/lib/device/round_robin_chunk_selector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ RoundRobinChunkSelector::RoundRobinChunkSelector(bool dynamic_chunk_add) : m_dyn
"Dynamically adding chunk to chunkselector is not supported, need RCU to make it thread safe");
}

void RoundRobinChunkSelector::add_chunk(VChunk chunk) { m_chunks.push_back(chunk); }
void RoundRobinChunkSelector::add_chunk(cshared< Chunk >& chunk) { m_chunks.emplace_back(std::move(chunk)); }

Chunk* RoundRobinChunkSelector::select_chunk(blk_count_t, const blk_alloc_hints&) {
cshared< Chunk > RoundRobinChunkSelector::select_chunk(blk_count_t, const blk_alloc_hints&) {
if (*m_next_chunk_index >= m_chunks.size()) { *m_next_chunk_index = 0; }
return m_chunks[(*m_next_chunk_index)++].get_internal_chunk().get();
return m_chunks[(*m_next_chunk_index)++];
}

void RoundRobinChunkSelector::foreach_chunks(std::function< void(VChunk&) >&& cb) {
void RoundRobinChunkSelector::foreach_chunks(std::function< void(cshared< Chunk >&) >&& cb) {
for (auto& chunk : m_chunks) {
cb(chunk);
}
Expand Down
10 changes: 4 additions & 6 deletions src/lib/device/round_robin_chunk_selector.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
#include "device/chunk.h"

namespace homestore {
class Chunk;

class RoundRobinChunkSelector : public ChunkSelector {
public:
RoundRobinChunkSelector(bool dynamic_chunk_add = false);
Expand All @@ -35,12 +33,12 @@ class RoundRobinChunkSelector : public ChunkSelector {
RoundRobinChunkSelector& operator=(RoundRobinChunkSelector&&) noexcept = delete;
~RoundRobinChunkSelector() = default;

void add_chunk(VChunk chunk) override;
Chunk* select_chunk(blk_count_t nblks, const blk_alloc_hints& hints) override;
void foreach_chunks(std::function< void(VChunk&) >&& cb) override;
void add_chunk(cshared< Chunk >&) override;
cshared< Chunk > select_chunk(blk_count_t nblks, const blk_alloc_hints& hints) override;
void foreach_chunks(std::function< void(cshared< Chunk >&) >&& cb) override;

private:
std::vector< VChunk > m_chunks;
std::vector< shared< Chunk > > m_chunks;
folly::ThreadLocal< uint32_t > m_next_chunk_index;
bool m_dynamic_chunk_add; // Can we add chunk dynamically
};
Expand Down
2 changes: 1 addition & 1 deletion src/lib/device/virtual_dev.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ BlkAllocStatus VirtualDev::do_alloc_blk(blk_count_t nblks, const blk_alloc_hints
size_t attempt{0};

do {
chunk = m_chunk_selector->select_chunk(nblks, hints);
chunk = m_chunk_selector->select_chunk(nblks, hints).get();
if (chunk == nullptr) { status = BlkAllocStatus::SPACE_FULL; }

status = alloc_blk_from_chunk(nblks, hints, out_blkid, chunk);
Expand Down

0 comments on commit 87070a1

Please sign in to comment.