Skip to content

Commit

Permalink
Raft handle region broken (#28)
Browse files Browse the repository at this point in the history
* Implment PageStorage

* Refactor RegionPersister based on PageStorage

* Remove some useless code, and dereference PageFile from Reader
  • Loading branch information
flowbehappy authored Apr 2, 2019
1 parent 4b2264a commit 722a1e5
Show file tree
Hide file tree
Showing 17 changed files with 1,611 additions and 759 deletions.
1 change: 1 addition & 0 deletions dbms/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ add_headers_and_sources(dbms src/Storages)
add_headers_and_sources(dbms src/Storages/Distributed)
add_headers_and_sources(dbms src/Storages/MergeTree)
add_headers_and_sources(dbms src/Storages/Transaction)
add_headers_and_sources(dbms src/Storages/Page)
add_headers_and_sources(dbms src/Raft)
add_headers_and_sources(dbms src/TiDB)
add_headers_and_sources(dbms src/Client)
Expand Down
2 changes: 2 additions & 0 deletions dbms/src/Common/ErrorCodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,8 @@ namespace ErrorCodes
extern const int THEFLASH_SESSION_ERROR = 9003;
extern const int DECIMAL_OVERFLOW_ERROR = 9004;
extern const int FILE_SIZE_NOT_MATCH = 9005;
extern const int PAGE_SIZE_NOT_MATCH = 9006;
extern const int ILLFORMED_PAGE_NAME = 9007;
extern const int LOCK_EXCEPTION = 10000;
extern const int VERSION_ERROR = 10001;
extern const int REGION_MISS = 10002;
Expand Down
13 changes: 12 additions & 1 deletion dbms/src/Common/ProfileEvents.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,18 @@
M(RWLockAcquiredReadLocks) \
M(RWLockAcquiredWriteLocks) \
M(RWLockReadersWaitMilliseconds) \
M(RWLockWritersWaitMilliseconds)
M(RWLockWritersWaitMilliseconds) \
\
M(PSMWritePages) \
M(PSMWritePageCalls) \
M(PSMWriteIOCalls) \
M(PSMWriteBytes) \
M(PSMReadPages) \
M(PSMReadPageCalls) \
M(PSMReadIOCalls) \
M(PSMReadBytes) \
M(PSMWriteFailed) \
M(PSMReadFailed)

namespace ProfileEvents
{
Expand Down
89 changes: 89 additions & 0 deletions dbms/src/Storages/Page/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
---
BasedOnStyle: Google
Language: Cpp
AlignAfterOpenBracket: true
BreakBeforeBraces: Custom
BraceWrapping: {
AfterClass: 'true'
AfterControlStatement: 'true'
AfterEnum : 'true'
AfterFunction : 'true'
AfterNamespace : 'true'
AfterStruct : 'true'
AfterUnion : 'true'
BeforeCatch : 'true'
BeforeElse : 'true'
IndentBraces : 'false'
}

BreakConstructorInitializersBeforeComma: false
Cpp11BracedListStyle: true
ColumnLimit: 140
ConstructorInitializerAllOnOneLineOrOnePerLine: true
ExperimentalAutoDetectBinPacking: true
UseTab: Never
TabWidth: 4
IndentWidth: 4
Standard: Cpp11
PointerAlignment: Middle
MaxEmptyLinesToKeep: 2
KeepEmptyLinesAtTheStartOfBlocks: true
AllowShortFunctionsOnASingleLine: Inline
#AllowShortFunctionsOnASingleLine: Empty
AlwaysBreakTemplateDeclarations: true
IndentCaseLabels: false
#SpaceAfterTemplateKeyword: true
#SortIncludes: true
FixNamespaceComments: true

ReflowComments: false
AlignEscapedNewlinesLeft: true

# Not changed:
AccessModifierOffset: -4
AlignConsecutiveAssignments: true
AlignConsecutiveDeclarations: true
AlignOperands: false
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: true
AllowShortCaseLabelsOnASingleLine: false
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakBeforeMultilineStrings: false
BinPackArguments: false
BinPackParameters: false
BreakBeforeBinaryOperators: All
BreakBeforeTernaryOperators: true
CommentPragmas: '^ IWYU pragma:'
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
DerivePointerAlignment: false
DisableFormat: false
ForEachMacros: [ foreach, Q_FOREACH, BOOST_FOREACH ]
IndentWidth: 4
IndentWrappedFunctionNames: false
MacroBlockBegin: ''
MacroBlockEnd: ''
NamespaceIndentation: None
ObjCBlockIndentWidth: 4
ObjCSpaceAfterProperty: true
ObjCSpaceBeforeProtocolList: true
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60
SpaceAfterCStyleCast: false
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
...

48 changes: 48 additions & 0 deletions dbms/src/Storages/Page/Page.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#pragma once

#include <unordered_map>

#include <IO/BufferBase.h>
#include <IO/MemoryReadWriteBuffer.h>

#include <Storages/Page/PageDefines.h>

namespace DB
{

using MemHolder = std::shared_ptr<char>;
inline MemHolder createMemHolder(char * memory, std::function<void(char *)> free)
{
return std::shared_ptr<char>(memory, free);
}

struct Page
{
PageId page_id;
ByteBuffer data;

MemHolder mem_holder;
};
using Pages = std::vector<Page>;
using PageMap = std::unordered_map<PageId, Page>;

struct PageCache
{
PageFileId file_id;
UInt32 level;
UInt32 size;
UInt64 version;
UInt64 offset;
UInt64 checksum;

bool isValid() { return file_id; }
PageFileIdAndLevel fileIdLevel() const { return std::make_pair(file_id, level); }
};
static_assert(std::is_trivially_copyable_v<PageCache>);

using PageCacheMap = std::unordered_map<PageId, PageCache>;
using PageCaches = std::vector<PageCache>;
using PageIdAndCache = std::pair<PageId, PageCache>;
using PageIdAndCaches = std::vector<PageIdAndCache>;

} // namespace DB
54 changes: 54 additions & 0 deletions dbms/src/Storages/Page/PageDefines.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#pragma once

#include <unordered_set>
#include <vector>

#include <Core/Types.h>

namespace DB
{

#define MB 1048576ULL;

static constexpr UInt64 PAGE_SIZE_STEP = (1 << 10) * 16; // 16 KB
static constexpr UInt64 PAGE_BUFFER_SIZE = DBMS_DEFAULT_BUFFER_SIZE;
static constexpr UInt64 PAGE_MAX_BUFFER_SIZE = 128 * MB;
static constexpr UInt64 PAGE_SPLIT_SIZE = 1 * MB;
static constexpr UInt64 PAGE_FILE_MAX_SIZE = 1024 * 2 * MB;
static constexpr UInt64 PAGE_FILE_SMALL_SIZE = 2 * MB;
static constexpr UInt64 PAGE_FILE_ROLL_SIZE = 128 * MB;

static_assert(PAGE_SIZE_STEP >= ((1 << 10) * 16), "PAGE_SIZE_STEP should be at least 16 KB");
static_assert((PAGE_SIZE_STEP & (PAGE_SIZE_STEP - 1)) == 0, "PAGE_SIZE_STEP should be power of 2");
static_assert(PAGE_BUFFER_SIZE % PAGE_SIZE_STEP == 0, "PAGE_BUFFER_SIZE should be dividable by PAGE_SIZE_STEP");

using PageId = UInt64;
using PageIds = std::vector<PageId>;
using PageIdSet = std::unordered_set<PageId>;
using PageFileId = UInt64;
using PageFileIdAndLevel = std::pair<PageFileId, UInt32>;
using PageFileIdAndLevels = std::vector<PageFileIdAndLevel>;

struct ByteBuffer
{
using Pos = char *;

ByteBuffer() = default;
ByteBuffer(Pos begin_pos_, Pos end_pos_) : begin_pos(begin_pos_), end_pos(end_pos_) {}

inline Pos begin() const { return begin_pos; }
inline Pos end() const { return end_pos; }
inline size_t size() const { return end_pos - begin_pos; }

private:
Pos begin_pos;
Pos end_pos; /// 1 byte after the end of the buffer
};

/// https://stackoverflow.com/a/13938417
inline size_t alignPage(size_t n)
{
return (n + PAGE_SIZE_STEP - 1) & ~(PAGE_SIZE_STEP - 1);
}

} // namespace DB
Loading

0 comments on commit 722a1e5

Please sign in to comment.