Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support new format of peer meta #1074

Merged
merged 4 commits into from
Sep 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion contrib/client-c
2 changes: 0 additions & 2 deletions dbms/src/Storages/Transaction/ApplySnapshot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,6 @@ static const metapb::Peer & findPeer(const metapb::Region & region, UInt64 peer_
{
if (peer.id() == peer_id)
{
if (!peer.is_learner())
throw Exception(std::string(__PRETTY_FUNCTION__) + ": peer is not learner, should not happen", ErrorCodes::LOGICAL_ERROR);
return peer;
}
}
Expand Down
12 changes: 6 additions & 6 deletions dbms/src/Storages/Transaction/Region.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,13 @@ RegionPtr Region::splitInto(RegionMeta && meta)
void RegionRaftCommandDelegate::execChangePeer(
const raft_cmdpb::AdminRequest & request, const raft_cmdpb::AdminResponse & response, const UInt64 index, const UInt64 term)
{
const auto & change_peer_request = request.change_peer();

LOG_INFO(log, toString(false) << " execute change peer type: " << eraftpb::ConfChangeType_Name(change_peer_request.change_type()));

LOG_INFO(log,
toString(false) << " execute change peer cmd {"
<< (request.has_change_peer_v2() ? request.change_peer_v2().ShortDebugString()
: request.change_peer().ShortDebugString())
<< "}");
meta.makeRaftCommandDelegate().execChangePeer(request, response, index, term);
LOG_INFO(log, "After execute change peer cmd, current region info: "; getDebugString(oss_internal_rare));
}

static const metapb::Peer & findPeer(const metapb::Region & region, UInt64 store_id)
Expand Down Expand Up @@ -500,8 +502,6 @@ void Region::assignRegion(Region && new_region)
meta.notifyAll();
}

bool Region::isPeerRemoved() const { return meta.isPeerRemoved(); }

void Region::compareAndCompleteSnapshot(HandleMap & handle_map, const Timestamp safe_point)
{
std::unique_lock<std::shared_mutex> lock(mutex);
Expand Down
1 change: 0 additions & 1 deletion dbms/src/Storages/Transaction/Region.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ class Region : public std::enable_shared_from_this<Region>

bool isPendingRemove() const;
void setPendingRemove();
bool isPeerRemoved() const;
raft_serverpb::PeerState peerState() const;

bool isMerging() const;
Expand Down
40 changes: 8 additions & 32 deletions dbms/src/Storages/Transaction/RegionMeta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,36 +196,14 @@ void RegionMeta::assignRegionMeta(RegionMeta && rhs)
}

void MetaRaftCommandDelegate::execChangePeer(
const raft_cmdpb::AdminRequest & request, const raft_cmdpb::AdminResponse & response, UInt64 index, UInt64 term)
const raft_cmdpb::AdminRequest &, const raft_cmdpb::AdminResponse & response, UInt64 index, UInt64 term)
{
std::lock_guard<std::mutex> lock(mutex);

const auto & change_peer_request = request.change_peer();
const auto & new_region = response.change_peer().region();

bool pending_remove = false;
switch (change_peer_request.change_type())
{
case eraftpb::ConfChangeType::AddNode:
case eraftpb::ConfChangeType::AddLearnerNode:
{
// change the peers of region, add conf_ver.
doSetRegion(new_region);
break;
}
case eraftpb::ConfChangeType::RemoveNode:
{
if (peer.id() == change_peer_request.peer().id())
pending_remove = true;

doSetRegion(new_region);
break;
}
default:
throw Exception(std::string(__PRETTY_FUNCTION__) + ": unsupported cmd", ErrorCodes::LOGICAL_ERROR);
}

if (pending_remove)
doSetRegion(new_region);
if (doCheckPeerRemoved())
region_state.setState(raft_serverpb::PeerState::Tombstone);
else
region_state.setState(raft_serverpb::PeerState::Normal);
Expand Down Expand Up @@ -274,8 +252,8 @@ RegionMergeResult MetaRaftCommandDelegate::checkBeforeCommitMerge(
static void CheckRegionForMergeCmd(const raft_cmdpb::AdminResponse & response, const RegionState & region_state)
{
if (response.has_split() && !(response.split().left() == region_state.getRegion()))
throw Exception(std::string(__PRETTY_FUNCTION__) + ": current region:\n" + region_state.getRegion().DebugString() + "\nexpect:\n"
+ response.split().left().DebugString() + "\nshould not happen",
throw Exception(std::string(__PRETTY_FUNCTION__) + ": current region:\n" + region_state.getRegion().ShortDebugString()
+ "\nexpect:\n" + response.split().left().ShortDebugString() + "\nshould not happen",
ErrorCodes::LOGICAL_ERROR);
}

Expand Down Expand Up @@ -344,12 +322,10 @@ void MetaRaftCommandDelegate::execPrepareMerge(
CheckRegionForMergeCmd(response, region_state);
}

bool RegionMeta::isPeerRemoved() const
bool RegionMeta::doCheckPeerRemoved() const
{
std::lock_guard<std::mutex> lock(mutex);

if (region_state.getState() == raft_serverpb::PeerState::Tombstone)
return true;
if (region_state.getRegion().peers().empty())
throw Exception(std::string(__PRETTY_FUNCTION__) + ": got empty peers, should not happen", ErrorCodes::LOGICAL_ERROR);

for (const auto & region_peer : region_state.getRegion().peers())
{
Expand Down
3 changes: 1 addition & 2 deletions dbms/src/Storages/Transaction/RegionMeta.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@ class RegionMeta
TerminateWaitIndex waitIndex(UInt64 index, const std::atomic_bool & terminated) const;
bool checkIndex(UInt64 index) const;

bool isPeerRemoved() const;

std::tuple<RegionVersion, RegionVersion, ImutRegionRangePtr> dumpVersionRange() const;
MetaRaftCommandDelegate & makeRaftCommandDelegate();

Expand All @@ -80,6 +78,7 @@ class RegionMeta
void doSetRegion(const metapb::Region & region);
void doSetApplied(UInt64 index, UInt64 term);
bool doCheckIndex(UInt64 index) const;
bool doCheckPeerRemoved() const;

private:
metapb::Peer peer;
Expand Down
6 changes: 3 additions & 3 deletions dbms/src/Storages/Transaction/SerializationHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ size_t writeBinary2(const metapb::Peer & peer, WriteBuffer & buf)
{
writeIntBinary((UInt64)peer.id(), buf);
writeIntBinary((UInt64)peer.store_id(), buf);
writeBinary(peer.is_learner(), buf);
writeIntBinary((UInt8)peer.role(), buf);
return sizeof(UInt64) + sizeof(UInt64) + sizeof(bool);
}

Expand All @@ -16,7 +16,7 @@ metapb::Peer readPeer(ReadBuffer & buf)
metapb::Peer peer;
peer.set_id(readBinary2<UInt64>(buf));
peer.set_store_id(readBinary2<UInt64>(buf));
peer.set_is_learner(readBinary2<bool>(buf));
peer.set_role(static_cast<metapb::PeerRole>(readBinary2<UInt8>(buf)));
return peer;
}

Expand Down Expand Up @@ -125,7 +125,7 @@ raft_serverpb::RaftApplyState readApplyState(ReadBuffer & buf)

bool operator==(const metapb::Peer & peer1, const metapb::Peer & peer2)
{
return peer1.id() == peer2.id() && peer1.store_id() == peer2.store_id() && peer1.is_learner() == peer2.is_learner();
return peer1.id() == peer2.id() && peer1.store_id() == peer2.store_id() && peer1.role() == peer2.role();
}

bool operator==(const metapb::Region & region1, const metapb::Region & region2)
Expand Down
3 changes: 1 addition & 2 deletions dbms/src/Storages/Transaction/tests/region_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,10 @@ using namespace DB;
} while (0)


inline metapb::Peer createPeer(UInt64 id, bool is_learner)
inline metapb::Peer createPeer(UInt64 id, bool)
{
metapb::Peer peer;
peer.set_id(id);
peer.set_is_learner(is_learner);
return peer;
}

Expand Down
2 changes: 1 addition & 1 deletion dbms/src/Storages/Transaction/tests/region_persister.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <Storages/Transaction/Region.h>
#include <Storages/Transaction/RegionManager.h>
#include <Storages/Transaction/TiKVRecordFormat.h>

#include <ext/scope_guard.h>

#include "region_helper.h"
Expand Down Expand Up @@ -89,7 +90,6 @@ int main(int, char **)
raft_serverpb::RaftApplyState apply_state;

peer.set_id(6666);
peer.set_is_learner(true);
peer.set_store_id(6667);

{
Expand Down