CCF/tests/election.py

123 строки
3.9 KiB
Python
Исходник Обычный вид История

2019-04-26 18:27:27 +03:00
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the Apache 2.0 License.
from ccf.tx_id import TxID
from infra.network import PrimaryNotFound
2019-04-26 18:27:27 +03:00
import math
2020-07-07 17:46:44 +03:00
import infra.network
2019-04-26 18:27:27 +03:00
import infra.proc
import infra.e2e_args
import infra.checker
import suite.test_requirements as reqs
from infra.runner import ConcurrentRunner
import copy
2019-04-26 18:27:27 +03:00
from loguru import logger as LOG
# This test starts from a given number of nodes (hosts), commits
2019-08-15 19:52:43 +03:00
# a transaction, stops the current primary, waits for an election and repeats
# this process until no progress can be made (i.e. no primary can be elected
2019-04-26 18:27:27 +03:00
# as F > N/2).
@reqs.description("Stopping current primary and waiting for a new one to be elected")
@reqs.can_kill_n_nodes(1)
def test_kill_primary(network, args):
primary, _ = network.find_primary_and_any_backup()
primary.stop()
network.wait_for_new_primary(primary)
# Verify that the TxID reported just after an election is valid
# Note that the first TxID read after an election may be of a signature
# Tx (time-based signature generation) in the new term rather than the
# last entry in the previous term
for node in network.get_joined_nodes():
with node.client() as c:
r = c.get("/node/network")
c.wait_for_commit(r)
return network
2019-04-26 18:27:27 +03:00
def run(args):
2020-07-07 17:46:44 +03:00
with infra.network.network(
2020-10-22 16:54:43 +03:00
args.nodes, args.binary_dir, args.debug_nodes, args.perf_nodes, pdb=args.pdb
2019-04-26 18:27:27 +03:00
) as network:
check = infra.checker.Checker()
2019-04-26 18:27:27 +03:00
network.start_and_join(args)
2020-05-22 16:56:21 +03:00
current_view = None
2019-04-26 18:27:27 +03:00
# Number of nodes F to stop until network cannot make progress
2020-10-22 16:54:43 +03:00
nodes_to_stop = math.ceil(len(args.nodes) / 2)
2020-09-09 12:59:57 +03:00
if args.consensus == "bft":
nodes_to_stop = math.ceil(len(args.nodes) / 3)
2019-04-26 18:27:27 +03:00
primary_is_known = True
for node_to_stop in range(nodes_to_stop):
2019-08-15 19:52:43 +03:00
# Note that for the first iteration, the primary is known in advance anyway
LOG.debug("Find freshly elected primary")
# After a view change in bft, finding the new primary takes longer
2020-05-22 16:56:21 +03:00
primary, current_view = network.find_primary(
2020-09-09 12:59:57 +03:00
timeout=(30 if args.consensus == "bft" else 3)
)
2019-04-26 18:27:27 +03:00
LOG.debug(
2020-05-22 16:56:21 +03:00
"Commit new transactions, primary:{}, current_view:{}".format(
primary.local_node_id, current_view
)
)
2020-07-03 15:52:56 +03:00
with primary.client("user0") as c:
2020-07-20 16:07:33 +03:00
res = c.post(
2020-07-03 15:52:56 +03:00
"/app/log/private",
2019-04-26 18:27:27 +03:00
{
2020-05-22 16:56:21 +03:00
"id": current_view,
"msg": "This log is committed in view {}".format(current_view),
2019-04-26 18:27:27 +03:00
},
)
2020-03-23 13:53:05 +03:00
check(res, result=True)
2019-04-26 18:27:27 +03:00
LOG.debug("Waiting for transaction to be committed by all nodes")
network.wait_for_all_nodes_to_commit(tx_id=TxID(res.view, res.seqno))
2019-04-26 18:27:27 +03:00
try:
test_kill_primary(network, args)
except PrimaryNotFound:
if node_to_stop < nodes_to_stop - 1:
raise
else:
primary_is_known = False
2019-04-26 18:27:27 +03:00
assert not primary_is_known, "Primary is still known"
2020-05-26 16:11:49 +03:00
LOG.success("Test ended successfully.")
2019-04-26 18:27:27 +03:00
if __name__ == "__main__":
cr = ConcurrentRunner()
args = copy.deepcopy(cr.args)
if cr.args.consensus in ("cft", "all"):
args.consensus = "cft"
cr.add(
"cft",
run,
package="liblogging",
nodes=infra.e2e_args.min_nodes(args, f=1),
raft_election_timeout_ms=500,
consensus="cft",
)
if cr.args.consensus in ("bft", "all"):
args.consensus = "bft"
cr.add(
"bft",
run,
package="liblogging",
nodes=infra.e2e_args.min_nodes(args, f=1),
consensus="bft",
)
cr.run()