Skip to content

Commit 55e74ae

Browse files
author
junchao
committed
merge master
1 parent 2a7bcdc commit 55e74ae

File tree

3 files changed

+184
-0
lines changed

3 files changed

+184
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
#
18+
19+
package(default_visibility = ["//visibility:private"])
20+
21+
cc_library(
22+
name = "consensus",
23+
srcs = ["consensus.cpp"],
24+
hdrs = ["consensus.h"],
25+
visibility = [
26+
"//visibility:public",
27+
],
28+
deps = [
29+
"//common/utils",
30+
"//platform/consensus/ordering/common/framework:consensus",
31+
"//platform/consensus/ordering/poe/algorithm:poe",
32+
],
33+
)
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
#include "platform/consensus/ordering/poe/framework/consensus.h"
21+
22+
#include <glog/logging.h>
23+
#include <unistd.h>
24+
25+
#include "common/utils/utils.h"
26+
27+
namespace resdb {
28+
namespace poe {
29+
30+
Consensus::Consensus(const ResDBConfig& config,
31+
std::unique_ptr<TransactionManager> executor)
32+
: common::Consensus(config, std::move(executor)) {
33+
int total_replicas = config_.GetReplicaNum();
34+
int f = (total_replicas - 1) / 3;
35+
36+
Init();
37+
38+
start_ = 0;
39+
40+
if (config_.GetPublicKeyCertificateInfo()
41+
.public_key()
42+
.public_key_info()
43+
.type() != CertificateKeyInfo::CLIENT) {
44+
poe_ = std::make_unique<PoE>(config_.GetSelfInfo().id(), f, total_replicas,
45+
GetSignatureVerifier());
46+
InitProtocol(poe_.get());
47+
}
48+
}
49+
50+
int Consensus::ProcessCustomConsensus(std::unique_ptr<Request> request) {
51+
if (request->user_type() == MessageType::Propose) {
52+
std::unique_ptr<Transaction> txn = std::make_unique<Transaction>();
53+
if (!txn->ParseFromString(request->data())) {
54+
assert(1 == 0);
55+
LOG(ERROR) << "parse proposal fail";
56+
return -1;
57+
}
58+
poe_->ReceivePropose(std::move(txn));
59+
return 0;
60+
} else if (request->user_type() == MessageType::Prepare) {
61+
std::unique_ptr<Proposal> proposal = std::make_unique<Proposal>();
62+
if (!proposal->ParseFromString(request->data())) {
63+
LOG(ERROR) << "parse proposal fail";
64+
assert(1 == 0);
65+
return -1;
66+
}
67+
poe_->ReceivePrepare(std::move(proposal));
68+
return 0;
69+
}
70+
return 0;
71+
}
72+
73+
int Consensus::ProcessNewTransaction(std::unique_ptr<Request> request) {
74+
std::unique_ptr<Transaction> txn = std::make_unique<Transaction>();
75+
txn->set_data(request->data());
76+
txn->set_hash(request->hash());
77+
txn->set_proxy_id(request->proxy_id());
78+
txn->set_uid(request->uid());
79+
return poe_->ReceiveTransaction(std::move(txn));
80+
}
81+
82+
int Consensus::CommitMsg(const google::protobuf::Message& msg) {
83+
return CommitMsgInternal(dynamic_cast<const Transaction&>(msg));
84+
}
85+
86+
int Consensus::CommitMsgInternal(const Transaction& txn) {
87+
std::unique_ptr<Request> request = std::make_unique<Request>();
88+
request->set_data(txn.data());
89+
request->set_seq(txn.seq());
90+
request->set_uid(txn.uid());
91+
request->set_proxy_id(txn.proxy_id());
92+
93+
transaction_executor_->Commit(std::move(request));
94+
return 0;
95+
}
96+
97+
} // namespace poe
98+
} // namespace resdb
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
#pragma once
21+
22+
#include "executor/common/transaction_manager.h"
23+
#include "platform/consensus/ordering/common/framework/consensus.h"
24+
#include "platform/consensus/ordering/poe/algorithm/poe.h"
25+
#include "platform/networkstrate/consensus_manager.h"
26+
27+
namespace resdb {
28+
namespace poe {
29+
30+
class Consensus : public common::Consensus {
31+
public:
32+
Consensus(const ResDBConfig& config,
33+
std::unique_ptr<TransactionManager> transaction_manager);
34+
virtual ~Consensus() = default;
35+
36+
private:
37+
int ProcessCustomConsensus(std::unique_ptr<Request> request) override;
38+
int ProcessNewTransaction(std::unique_ptr<Request> request) override;
39+
int CommitMsg(const google::protobuf::Message& msg) override;
40+
int CommitMsgInternal(const Transaction& txn);
41+
42+
int Prepare(const Transaction& txn);
43+
44+
protected:
45+
std::unique_ptr<PoE> poe_;
46+
Stats* global_stats_;
47+
int64_t start_;
48+
std::mutex mutex_;
49+
int send_num_[200];
50+
};
51+
52+
} // namespace poe
53+
} // namespace resdb

0 commit comments

Comments
 (0)