2019-04-26 18:27:27 +03:00
|
|
|
# Copyright (c) Microsoft Corporation. All rights reserved.
|
|
|
|
# Licensed under the Apache 2.0 License.
|
2021-05-11 19:13:07 +03:00
|
|
|
from ccf.tx_id import TxID
|
2020-08-21 19:29:14 +03:00
|
|
|
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
|
2020-01-29 18:09:28 +03:00
|
|
|
import infra.e2e_args
|
2020-07-27 19:34:16 +03:00
|
|
|
import infra.checker
|
2020-07-03 14:02:26 +03:00
|
|
|
import suite.test_requirements as reqs
|
2021-09-09 20:04:35 +03:00
|
|
|
from infra.runner import ConcurrentRunner
|
|
|
|
import copy
|
2022-04-14 20:31:14 +03:00
|
|
|
import infra.service_load
|
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
|
2022-04-22 17:18:46 +03:00
|
|
|
# as F > N/2).
|
2019-04-26 18:27:27 +03:00
|
|
|
|
|
|
|
|
2021-12-20 20:29:35 +03:00
|
|
|
@reqs.description("Stop current primary and wait for a new one to be elected")
|
|
|
|
def test_kill_primary_no_reqs(network, args):
|
2022-04-22 17:18:46 +03:00
|
|
|
old_primary, _ = network.find_primary_and_any_backup()
|
|
|
|
old_primary.stop()
|
|
|
|
new_primary, _ = network.wait_for_new_primary(old_primary)
|
2020-07-03 14:02:26 +03:00
|
|
|
|
2021-07-08 13:44:34 +03:00
|
|
|
# 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)
|
|
|
|
|
2022-04-22 17:18:46 +03:00
|
|
|
# Also verify that reported last ack time are as expected
|
|
|
|
r = c.get("/node/consensus")
|
|
|
|
acks = r.body.json()["details"]["acks"]
|
|
|
|
for ack in acks.values():
|
|
|
|
if node is new_primary:
|
|
|
|
assert (
|
|
|
|
ack["last_received_ms"] < network.args.election_timeout_ms
|
|
|
|
), acks
|
|
|
|
else:
|
|
|
|
assert (
|
|
|
|
ack["last_received_ms"] == 0
|
|
|
|
), f"Backup {node.local_node_id} should report time of last acks of 0: {acks}"
|
|
|
|
|
2020-07-03 14:02:26 +03:00
|
|
|
return network
|
|
|
|
|
|
|
|
|
2021-12-20 20:29:35 +03:00
|
|
|
# Called by test suite. Election test deliberately makes service unusable.
|
|
|
|
@reqs.can_kill_n_nodes(1)
|
|
|
|
def test_kill_primary(network, args):
|
|
|
|
return test_kill_primary_no_reqs(network, args)
|
|
|
|
|
|
|
|
|
2019-04-26 18:27:27 +03:00
|
|
|
def run(args):
|
2022-04-14 20:31:14 +03:00
|
|
|
with infra.service_load.load() as load:
|
|
|
|
with infra.network.network(
|
|
|
|
args.nodes,
|
|
|
|
args.binary_dir,
|
|
|
|
args.debug_nodes,
|
|
|
|
args.perf_nodes,
|
|
|
|
pdb=args.pdb,
|
|
|
|
service_load=load,
|
|
|
|
) as network:
|
|
|
|
check = infra.checker.Checker()
|
|
|
|
|
|
|
|
network.start_and_open(args)
|
|
|
|
current_view = None
|
2021-10-14 12:28:50 +03:00
|
|
|
primary, current_view = network.find_primary()
|
2019-04-26 18:27:27 +03:00
|
|
|
|
2022-04-14 20:31:14 +03:00
|
|
|
# Number of nodes F to stop until network cannot make progress
|
|
|
|
nodes_to_stop = math.ceil(len(args.nodes) / 2)
|
|
|
|
if args.consensus == "BFT":
|
|
|
|
nodes_to_stop = math.ceil(len(args.nodes) / 3)
|
2021-05-11 19:13:07 +03:00
|
|
|
|
2022-04-14 20:31:14 +03:00
|
|
|
primary_is_known = True
|
|
|
|
for node_to_stop in range(nodes_to_stop):
|
|
|
|
primary, current_view = network.find_primary()
|
2019-04-26 18:27:27 +03:00
|
|
|
|
2022-04-14 20:31:14 +03:00
|
|
|
LOG.debug(
|
|
|
|
"Commit new transactions, primary:{}, current_view:{}".format(
|
|
|
|
primary.local_node_id, current_view
|
|
|
|
)
|
|
|
|
)
|
|
|
|
with primary.client("user0") as c:
|
|
|
|
res = c.post(
|
|
|
|
"/app/log/private",
|
|
|
|
{
|
|
|
|
"id": current_view,
|
|
|
|
"msg": "This log is committed in view {}".format(
|
|
|
|
current_view
|
|
|
|
),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
check(res, result=True)
|
|
|
|
|
|
|
|
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))
|
|
|
|
|
|
|
|
try:
|
|
|
|
test_kill_primary_no_reqs(network, args)
|
|
|
|
except PrimaryNotFound:
|
|
|
|
if node_to_stop < nodes_to_stop - 1:
|
|
|
|
raise
|
|
|
|
else:
|
|
|
|
primary_is_known = False
|
|
|
|
|
|
|
|
assert not primary_is_known, "Primary is still known"
|
|
|
|
LOG.success("Test ended successfully.")
|
2019-04-26 18:27:27 +03:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
2021-09-09 20:04:35 +03:00
|
|
|
cr = ConcurrentRunner()
|
|
|
|
|
|
|
|
args = copy.deepcopy(cr.args)
|
|
|
|
|
2021-11-23 20:35:01 +03:00
|
|
|
if cr.args.consensus in ("CFT", "ALL"):
|
|
|
|
args.consensus = "CFT"
|
2021-09-10 13:20:04 +03:00
|
|
|
cr.add(
|
|
|
|
"cft",
|
|
|
|
run,
|
2021-09-17 11:51:25 +03:00
|
|
|
package="samples/apps/logging/liblogging",
|
2021-09-10 13:20:04 +03:00
|
|
|
nodes=infra.e2e_args.min_nodes(args, f=1),
|
2021-11-23 20:35:01 +03:00
|
|
|
election_timeout_ms=1000,
|
|
|
|
consensus="CFT",
|
2021-09-10 13:20:04 +03:00
|
|
|
)
|
|
|
|
|
2021-11-23 20:35:01 +03:00
|
|
|
if cr.args.consensus in ("BFT", "ALL"):
|
|
|
|
args.consensus = "BFT"
|
2021-09-10 13:20:04 +03:00
|
|
|
cr.add(
|
|
|
|
"bft",
|
|
|
|
run,
|
2021-09-17 11:51:25 +03:00
|
|
|
package="samples/apps/logging/liblogging",
|
2021-09-10 13:20:04 +03:00
|
|
|
nodes=infra.e2e_args.min_nodes(args, f=1),
|
2021-11-23 20:35:01 +03:00
|
|
|
consensus="BFT",
|
2021-09-10 13:20:04 +03:00
|
|
|
)
|
2021-09-09 20:04:35 +03:00
|
|
|
|
2021-09-21 00:19:24 +03:00
|
|
|
cr.run(1)
|