2019-08-09 15:53:46 +03:00
|
|
|
# Copyright (c) Microsoft Corporation. All rights reserved.
|
|
|
|
# Licensed under the Apache 2.0 License.
|
2020-01-29 18:09:28 +03:00
|
|
|
import infra.e2e_args
|
2019-08-09 15:53:46 +03:00
|
|
|
import time
|
2020-07-07 17:46:44 +03:00
|
|
|
import infra.network
|
2019-08-09 15:53:46 +03:00
|
|
|
import infra.proc
|
2020-07-27 19:34:16 +03:00
|
|
|
import infra.checker
|
2021-11-23 20:35:01 +03:00
|
|
|
import infra.interfaces
|
2019-08-09 15:53:46 +03:00
|
|
|
import contextlib
|
|
|
|
import resource
|
|
|
|
import psutil
|
2022-01-12 12:32:26 +03:00
|
|
|
from infra.log_capture import flush_info
|
|
|
|
from infra.clients import CCFConnectionException
|
2021-04-16 20:40:08 +03:00
|
|
|
import random
|
2021-05-12 16:33:34 +03:00
|
|
|
import http
|
2021-08-09 19:00:40 +03:00
|
|
|
import functools
|
2021-09-01 22:09:12 +03:00
|
|
|
import httpx
|
2022-01-24 15:15:52 +03:00
|
|
|
import os
|
2019-08-09 15:53:46 +03:00
|
|
|
|
|
|
|
from loguru import logger as LOG
|
|
|
|
|
|
|
|
|
2021-05-12 16:33:34 +03:00
|
|
|
class AllConnectionsCreatedException(Exception):
|
|
|
|
"""
|
|
|
|
Raised if we expected a node to refuse connections, but it didn't
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
2021-06-23 21:54:04 +03:00
|
|
|
def get_session_metrics(node, timeout=3):
|
2021-05-17 13:13:20 +03:00
|
|
|
with node.client() as c:
|
2021-06-23 21:54:04 +03:00
|
|
|
end_time = time.time() + timeout
|
|
|
|
while time.time() < end_time:
|
|
|
|
r = c.get("/node/metrics")
|
|
|
|
if r.status_code == http.HTTPStatus.OK:
|
|
|
|
return r.body.json()["sessions"]
|
|
|
|
time.sleep(0.1)
|
|
|
|
assert r.status_code == http.HTTPStatus.OK, r
|
2021-05-17 13:13:20 +03:00
|
|
|
|
|
|
|
|
2021-08-09 19:00:40 +03:00
|
|
|
def interface_caps(i):
|
|
|
|
return {
|
2021-12-15 13:40:13 +03:00
|
|
|
"first_interface": {
|
|
|
|
"bind_address": f"127.{i}.0.1",
|
|
|
|
"max_open_sessions_soft": 2,
|
|
|
|
},
|
|
|
|
"second_interface": {
|
|
|
|
"bind_address": f"127.{i}.0.2",
|
|
|
|
"max_open_sessions_soft": 5,
|
|
|
|
},
|
2021-08-09 19:00:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-09 15:53:46 +03:00
|
|
|
def run(args):
|
2021-08-09 19:00:40 +03:00
|
|
|
# Listen on additional RPC interfaces with even lower session caps
|
|
|
|
for i, node_spec in enumerate(args.nodes):
|
|
|
|
caps = interface_caps(i)
|
2021-12-15 13:40:13 +03:00
|
|
|
for interface_name, interface in caps.items():
|
|
|
|
node_spec.rpc_interfaces[interface_name] = infra.interfaces.RPCInterface(
|
|
|
|
host=interface["bind_address"],
|
|
|
|
max_open_sessions_soft=interface["max_open_sessions_soft"],
|
2021-11-23 20:35:01 +03:00
|
|
|
)
|
2021-08-09 19:00:40 +03:00
|
|
|
|
2021-04-16 20:40:08 +03:00
|
|
|
# Chunk often, so that new fds are regularly requested
|
2021-12-15 13:40:13 +03:00
|
|
|
args.ledger_chunk_bytes = "500B"
|
2021-04-16 20:40:08 +03:00
|
|
|
|
2022-01-24 15:15:52 +03:00
|
|
|
supp_file = os.path.join(
|
|
|
|
os.path.abspath(os.path.dirname(__file__)), "connections.supp"
|
|
|
|
)
|
|
|
|
args.ubsan_options = "suppressions=" + str(supp_file)
|
|
|
|
|
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-08-09 15:53:46 +03:00
|
|
|
) as network:
|
2020-07-27 19:34:16 +03:00
|
|
|
check = infra.checker.Checker()
|
2022-02-18 18:42:26 +03:00
|
|
|
network.start_and_open(args)
|
2020-04-17 12:03:29 +03:00
|
|
|
primary, _ = network.find_nodes()
|
2019-08-09 15:53:46 +03:00
|
|
|
|
2021-08-09 19:00:40 +03:00
|
|
|
caps = interface_caps(primary.local_node_id)
|
|
|
|
|
2019-08-09 15:53:46 +03:00
|
|
|
primary_pid = primary.remote.remote.proc.pid
|
2021-04-16 20:40:08 +03:00
|
|
|
|
|
|
|
initial_fds = psutil.Process(primary_pid).num_fds()
|
|
|
|
assert (
|
|
|
|
initial_fds < args.max_open_sessions
|
|
|
|
), f"Initial number of file descriptors has already reached session limit: {initial_fds} >= {args.max_open_sessions}"
|
|
|
|
|
|
|
|
num_fds = initial_fds
|
2020-08-05 15:39:52 +03:00
|
|
|
LOG.success(f"{primary_pid} has {num_fds} open file descriptors")
|
2019-08-09 15:53:46 +03:00
|
|
|
|
2021-05-17 13:13:20 +03:00
|
|
|
initial_metrics = get_session_metrics(primary)
|
|
|
|
assert initial_metrics["active"] <= initial_metrics["peak"], initial_metrics
|
2021-11-23 20:35:01 +03:00
|
|
|
|
2021-12-15 13:40:13 +03:00
|
|
|
for interface_name, rpc_interface in primary.host.rpc_interfaces.items():
|
|
|
|
metrics = initial_metrics["interfaces"][interface_name]
|
2021-11-23 20:35:01 +03:00
|
|
|
assert metrics["soft_cap"] == rpc_interface.max_open_sessions_soft, metrics
|
|
|
|
assert metrics["hard_cap"] == rpc_interface.max_open_sessions_hard, metrics
|
2021-05-17 13:13:20 +03:00
|
|
|
|
2021-08-09 19:00:40 +03:00
|
|
|
max_fds = args.max_open_sessions + (initial_fds * 2)
|
|
|
|
|
|
|
|
def create_connections_until_exhaustion(
|
|
|
|
target, continue_to_hard_cap=False, client_fn=primary.client
|
|
|
|
):
|
2021-04-16 20:40:08 +03:00
|
|
|
with contextlib.ExitStack() as es:
|
|
|
|
clients = []
|
|
|
|
LOG.success(f"Creating {target} clients")
|
|
|
|
consecutive_failures = 0
|
2021-08-09 19:00:40 +03:00
|
|
|
i = 1
|
2021-09-01 22:09:12 +03:00
|
|
|
healthy_clients = []
|
2021-08-09 19:00:40 +03:00
|
|
|
while i <= target:
|
2021-04-16 20:40:08 +03:00
|
|
|
logs = []
|
|
|
|
try:
|
|
|
|
clients.append(
|
|
|
|
es.enter_context(
|
2021-09-01 22:09:12 +03:00
|
|
|
client_fn(
|
|
|
|
identity="user0",
|
|
|
|
connection_timeout=1,
|
|
|
|
limits=httpx.Limits(
|
|
|
|
max_connections=1,
|
|
|
|
max_keepalive_connections=1,
|
|
|
|
keepalive_expiry=30,
|
|
|
|
),
|
|
|
|
)
|
2021-04-16 20:40:08 +03:00
|
|
|
)
|
|
|
|
)
|
2021-05-12 16:33:34 +03:00
|
|
|
r = clients[-1].post(
|
2022-08-23 18:43:35 +03:00
|
|
|
"/log/private",
|
2021-05-12 16:33:34 +03:00
|
|
|
{"id": 42, "msg": "foo"},
|
|
|
|
log_capture=logs,
|
2021-04-16 20:40:08 +03:00
|
|
|
)
|
2021-05-12 16:33:34 +03:00
|
|
|
if r.status_code == http.HTTPStatus.OK:
|
|
|
|
check(
|
|
|
|
r,
|
|
|
|
result=True,
|
|
|
|
)
|
|
|
|
consecutive_failures = 0
|
2021-08-09 19:00:40 +03:00
|
|
|
i += 1
|
2021-09-01 22:09:12 +03:00
|
|
|
healthy_clients.append(clients[-1])
|
2021-05-12 16:33:34 +03:00
|
|
|
elif r.status_code == http.HTTPStatus.SERVICE_UNAVAILABLE:
|
|
|
|
if continue_to_hard_cap:
|
|
|
|
consecutive_failures = 0
|
2021-08-09 19:00:40 +03:00
|
|
|
i += 1
|
2021-05-12 16:33:34 +03:00
|
|
|
continue
|
|
|
|
raise RuntimeError(r.body.text())
|
|
|
|
else:
|
|
|
|
flush_info(logs)
|
|
|
|
raise ValueError(
|
|
|
|
f"Unexpected response status code: {r.status_code}"
|
|
|
|
)
|
2021-04-16 20:40:08 +03:00
|
|
|
except (CCFConnectionException, RuntimeError) as e:
|
|
|
|
flush_info(logs)
|
2021-08-09 19:00:40 +03:00
|
|
|
LOG.warning(f"Hit exception at client {i}/{target}: {e}")
|
2021-04-16 20:40:08 +03:00
|
|
|
clients.pop(-1)
|
|
|
|
if consecutive_failures < 5:
|
|
|
|
# Maybe got unlucky and tried to create a session while many files were open - keep trying
|
|
|
|
consecutive_failures += 1
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
# Ok you've really hit a wall, stop trying to create clients
|
|
|
|
break
|
|
|
|
else:
|
2021-05-12 16:33:34 +03:00
|
|
|
raise AllConnectionsCreatedException(
|
2021-04-16 20:40:08 +03:00
|
|
|
f"Successfully created {target} clients without exception - expected this to exhaust available connections"
|
|
|
|
)
|
|
|
|
|
|
|
|
num_fds = psutil.Process(primary_pid).num_fds()
|
|
|
|
LOG.success(
|
|
|
|
f"{primary_pid} has {num_fds}/{max_fds} open file descriptors"
|
|
|
|
)
|
2021-09-01 22:09:12 +03:00
|
|
|
r = clients[0].get("/node/metrics")
|
2021-05-17 13:13:20 +03:00
|
|
|
assert r.status_code == http.HTTPStatus.OK, r.status_code
|
|
|
|
peak_metrics = r.body.json()["sessions"]
|
|
|
|
assert peak_metrics["active"] <= peak_metrics["peak"], peak_metrics
|
2021-09-01 22:09:12 +03:00
|
|
|
assert peak_metrics["active"] == len(healthy_clients), (
|
2021-05-17 13:13:20 +03:00
|
|
|
peak_metrics,
|
2021-09-01 22:09:12 +03:00
|
|
|
len(healthy_clients),
|
2021-05-17 13:13:20 +03:00
|
|
|
)
|
|
|
|
|
2021-04-16 20:40:08 +03:00
|
|
|
# Submit many requests, and at least enough to trigger additional snapshots
|
|
|
|
more_requests = max(len(clients) * 3, args.snapshot_tx_interval * 2)
|
|
|
|
LOG.info(
|
|
|
|
f"Submitting an additional {more_requests} requests from existing clients"
|
|
|
|
)
|
|
|
|
for _ in range(more_requests):
|
2021-09-01 22:09:12 +03:00
|
|
|
client = random.choice(healthy_clients)
|
2021-04-16 20:40:08 +03:00
|
|
|
logs = []
|
|
|
|
try:
|
|
|
|
client.post(
|
2022-08-23 18:43:35 +03:00
|
|
|
"/log/private",
|
2021-04-16 20:40:08 +03:00
|
|
|
{"id": 42, "msg": "foo"},
|
|
|
|
timeout=1,
|
|
|
|
log_capture=logs,
|
|
|
|
)
|
|
|
|
except Exception as e:
|
|
|
|
flush_info(logs)
|
|
|
|
LOG.error(e)
|
|
|
|
raise e
|
|
|
|
|
|
|
|
time.sleep(1)
|
|
|
|
num_fds = psutil.Process(primary_pid).num_fds()
|
|
|
|
LOG.success(
|
|
|
|
f"{primary_pid} has {num_fds}/{max_fds} open file descriptors"
|
|
|
|
)
|
|
|
|
|
|
|
|
LOG.info("Disconnecting clients")
|
|
|
|
clients = []
|
|
|
|
|
|
|
|
time.sleep(1)
|
|
|
|
num_fds = psutil.Process(primary_pid).num_fds()
|
|
|
|
LOG.success(f"{primary_pid} has {num_fds}/{max_fds} open file descriptors")
|
|
|
|
return num_fds
|
|
|
|
|
|
|
|
# For initial safe tests, we have many more fds than the maximum sessions, so file operations should still succeed even when network is saturated
|
2019-08-09 15:53:46 +03:00
|
|
|
resource.prlimit(primary_pid, resource.RLIMIT_NOFILE, (max_fds, max_fds))
|
2021-04-16 20:40:08 +03:00
|
|
|
LOG.success(f"Setting max fds to safe initial value {max_fds} on {primary_pid}")
|
2019-08-09 15:53:46 +03:00
|
|
|
|
|
|
|
nb_conn = (max_fds - num_fds) * 2
|
2021-04-16 20:40:08 +03:00
|
|
|
num_fds = create_connections_until_exhaustion(nb_conn)
|
2019-08-09 15:53:46 +03:00
|
|
|
|
2021-04-16 20:40:08 +03:00
|
|
|
to_create = max_fds - num_fds + 1
|
|
|
|
num_fds = create_connections_until_exhaustion(to_create)
|
2019-08-09 15:53:46 +03:00
|
|
|
|
2021-11-23 20:35:01 +03:00
|
|
|
LOG.info("Check that lower caps are enforced on each interface")
|
2021-12-15 13:40:13 +03:00
|
|
|
for (name, interface) in caps.items():
|
2021-08-09 19:00:40 +03:00
|
|
|
create_connections_until_exhaustion(
|
2021-12-15 13:40:13 +03:00
|
|
|
interface["max_open_sessions_soft"] + 1,
|
|
|
|
client_fn=functools.partial(primary.client, interface_name=name),
|
2021-08-09 19:00:40 +03:00
|
|
|
)
|
|
|
|
|
2021-05-12 16:33:34 +03:00
|
|
|
try:
|
|
|
|
create_connections_until_exhaustion(to_create, True)
|
2021-08-23 12:59:42 +03:00
|
|
|
except AllConnectionsCreatedException:
|
2021-05-12 16:33:34 +03:00
|
|
|
# This is fine! The soft cap means this test no longer reaches the hard cap.
|
|
|
|
# It gets HTTP errors but then _closes_ sockets, fast enough that we never hit the hard cap
|
|
|
|
pass
|
|
|
|
|
2021-05-17 13:13:20 +03:00
|
|
|
final_metrics = get_session_metrics(primary)
|
|
|
|
assert final_metrics["active"] <= final_metrics["peak"], final_metrics
|
|
|
|
assert final_metrics["peak"] > initial_metrics["peak"], (
|
|
|
|
initial_metrics,
|
|
|
|
final_metrics,
|
|
|
|
)
|
|
|
|
assert final_metrics["peak"] >= args.max_open_sessions, final_metrics
|
|
|
|
assert final_metrics["peak"] < args.max_open_sessions_hard, final_metrics
|
|
|
|
|
2021-11-23 20:35:01 +03:00
|
|
|
LOG.info(
|
|
|
|
"Set a low fd limit, so network sessions completely exhaust them - expect this to cause node failures"
|
|
|
|
)
|
2021-04-16 20:40:08 +03:00
|
|
|
max_fds = args.max_open_sessions // 2
|
|
|
|
resource.prlimit(primary_pid, resource.RLIMIT_NOFILE, (max_fds, max_fds))
|
|
|
|
LOG.success(f"Setting max fds to dangerously low {max_fds} on {primary_pid}")
|
2019-08-09 15:53:46 +03:00
|
|
|
|
2021-04-16 20:40:08 +03:00
|
|
|
try:
|
|
|
|
num_fds = create_connections_until_exhaustion(to_create)
|
|
|
|
except Exception as e:
|
|
|
|
LOG.warning(
|
|
|
|
f"Node with only {max_fds} fds crashed when allowed to created {args.max_open_sessions} sessions, as expected"
|
|
|
|
)
|
|
|
|
LOG.warning(e)
|
|
|
|
network.ignore_errors_on_shutdown()
|
|
|
|
else:
|
2021-06-25 13:43:32 +03:00
|
|
|
LOG.warning("Expected a fatal crash and saw none!")
|
2019-08-09 15:53:46 +03:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
2020-10-05 13:00:31 +03:00
|
|
|
args = infra.e2e_args.cli_args()
|
2021-09-17 11:51:25 +03:00
|
|
|
args.package = "samples/apps/logging/liblogging"
|
2021-11-23 20:35:01 +03:00
|
|
|
|
|
|
|
# Set a relatively low cap on max open sessions, so we can saturate it in a reasonable amount of time
|
|
|
|
args.max_open_sessions = 40
|
|
|
|
args.max_open_sessions_hard = args.max_open_sessions + 5
|
|
|
|
|
2021-09-07 16:56:08 +03:00
|
|
|
args.nodes = infra.e2e_args.nodes(args, 1)
|
|
|
|
args.initial_user_count = 1
|
2019-08-09 15:53:46 +03:00
|
|
|
run(args)
|