@mark1russell7/harness 0.16.0 → 0.16.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/build/index.js CHANGED
@@ -2111,6 +2111,15 @@ const measurePorts = (run, expected) => {
2111
2111
  };
2112
2112
  // src/recovery.ts
2113
2113
  /**
2114
+ * This function gives the state of a set that did not elect a primary after a restore. The text has
2115
+ * the view of the old primary (the output of `STATUS_SCRIPT`). It also has the last `ELECTION_LINES`
2116
+ * lines of the components ELECTION and REPL, from index `from` of the log.
2117
+ */
2118
+ const electionStateText = (primary, status, log, from) => {
2119
+ const lines = log.slice(from).filter((line) => line.c === "ELECTION" || line.c === "REPL").slice(-8).map((line) => `${line.member} ${line.c} ${line.msg}`);
2120
+ return `The view of ${primary}: ${status.trim()}. Its last lines of the election: ${lines.length === 0 ? "none" : lines.join(" | ")}`;
2121
+ };
2122
+ /**
2114
2123
  * This module holds the pure parts of a recovery drill. These are the scripts of the shell, the reader
2115
2124
  * of the output of `bsondump`, and the set check of a recovery. It has no container, thus unit tests
2116
2125
  * run it. `drill.ts` does the drill on a lab.
@@ -4047,8 +4056,10 @@ const CONFIRM_MS = 1e4;
4047
4056
  /** This function gives the arguments of the injector for the options of a fault. */
4048
4057
  const optionArgs = (options) => [
4049
4058
  ...options.direction === void 0 ? [] : ["--direction", options.direction],
4059
+ ...options.protocol === void 0 ? [] : ["--protocol", options.protocol],
4050
4060
  ...options.code === void 0 ? [] : ["--code", options.code],
4051
- ...options.above === void 0 ? [] : ["--above", String(options.above)]
4061
+ ...options.above === void 0 ? [] : ["--above", String(options.above)],
4062
+ ...options.handshake === void 0 ? [] : ["--handshake", options.handshake]
4052
4063
  ];
4053
4064
  /** These are the actions of the injector that lift each network fault. */
4054
4065
  const LIFT = {
@@ -4056,7 +4067,31 @@ const LIFT = {
4056
4067
  rst: "clear-rst",
4057
4068
  reject: "clear-reject",
4058
4069
  mtu: "clear-mtu",
4059
- "fin-ack": "clear-fin-ack"
4070
+ "fin-ack": "clear-fin-ack",
4071
+ silence: "clear-silence"
4072
+ };
4073
+ /**
4074
+ * This function gives the argv of the injector for a network fault at a place, or for its lift. The
4075
+ * lab gives the peer of the place and, at a router, its device (`--via`). The peers of the options
4076
+ * replace the peer.
4077
+ *
4078
+ * A silence names no peer and takes no option. It acts only at a hop, and the side of the hop has no
4079
+ * effect on it.
4080
+ */
4081
+ const faultArgs = (kind, at, peer, via, options = {}, lift = false) => {
4082
+ const action = lift ? LIFT[kind] : kind;
4083
+ if (kind === "silence") {
4084
+ if (!isHopPlace(at)) throw new Error(`a silence drops each packet of the namespace of a hop, not of ${JSON.stringify(at)}`);
4085
+ if (Object.values(options).some((value) => value !== void 0)) throw new Error("a silence takes no options");
4086
+ return ["/opt/harness/inject", action];
4087
+ }
4088
+ return [
4089
+ "/opt/harness/inject",
4090
+ action,
4091
+ options.peers?.join(",") ?? peer,
4092
+ ...optionArgs(options),
4093
+ ...via
4094
+ ];
4060
4095
  };
4061
4096
  /** This function collects the JSON lines of a program. It gives each line to `also` too, when the caller gives it. */
4062
4097
  const lines = (also) => {
@@ -4158,6 +4193,22 @@ const endpointAfter = (hops, element) => {
4158
4193
  while (at <= hops.length && hops[at - 1].kind === "router") at += 1;
4159
4194
  return at;
4160
4195
  };
4196
+ /**
4197
+ * This function gives the run topology of a chain of hops, or null for a direct pair. Each hop gets
4198
+ * its namespace, its peer on each side and its own address on each side.
4199
+ */
4200
+ const runTopologyOf = (targetPeer, hops) => hops.length === 0 ? null : {
4201
+ targetPeer,
4202
+ hops: Object.fromEntries(hops.map((hop) => [hop.hop.name, {
4203
+ netns: hop.netns,
4204
+ client: hop.peer.client,
4205
+ target: hop.peer.target,
4206
+ address: {
4207
+ client: hop.ip.client,
4208
+ target: hop.ip.target
4209
+ }
4210
+ }]))
4211
+ };
4161
4212
  const STOP = { timeout: 0 };
4162
4213
  /** These are the values of CONFIG_HZ that Linux offers. */
4163
4214
  const RATES = [
@@ -4520,14 +4571,7 @@ const openLab = async (target, kind = "tcp", topology) => {
4520
4571
  const targetIp = fabric !== void 0 ? FABRIC_TARGET : first === hops.length + 1 ? serverIp : inChain[first - 1].ip.client;
4521
4572
  const targetPort = first === hops.length + 1 ? target.port : inChain[first - 1].hop.port;
4522
4573
  const targetPeer = inChain.length === 0 ? clientIp : inChain.at(-1).ip.target;
4523
- const placed = inChain.length === 0 ? null : {
4524
- targetPeer,
4525
- hops: Object.fromEntries(inChain.map((hop) => [hop.hop.name, {
4526
- netns: hop.netns,
4527
- client: hop.peer.client,
4528
- target: hop.peer.target
4529
- }]))
4530
- };
4574
+ const placed = runTopologyOf(targetPeer, inChain);
4531
4575
  const paused = runtime.container.getById(server.getId());
4532
4576
  let targetAdmin;
4533
4577
  let signaller;
@@ -4726,23 +4770,11 @@ const openLab = async (target, kind = "tcp", topology) => {
4726
4770
  },
4727
4771
  async fault(kind, at = "target", options = {}) {
4728
4772
  const [where, peer, via] = placeOf(at);
4729
- return stamped(where, [
4730
- "/opt/harness/inject",
4731
- kind,
4732
- options.peers?.join(",") ?? peer,
4733
- ...optionArgs(options),
4734
- ...via
4735
- ]);
4773
+ return stamped(where, faultArgs(kind, at, peer, via, options));
4736
4774
  },
4737
4775
  async lift(kind, at = "target", options = {}) {
4738
4776
  const [where, peer, via] = placeOf(at);
4739
- await output(where, [
4740
- "/opt/harness/inject",
4741
- LIFT[kind],
4742
- options.peers?.join(",") ?? peer,
4743
- ...optionArgs(options),
4744
- ...via
4745
- ]);
4777
+ await output(where, faultArgs(kind, at, peer, via, options, true));
4746
4778
  },
4747
4779
  async netem(spec, at = "target") {
4748
4780
  const [where, device] = deviceAt(at);
@@ -6704,14 +6736,25 @@ const replicaSetOnce = async (lab, cell, environment, index) => {
6704
6736
  const VAULT = "/backup";
6705
6737
  /** This time is the longest wait for a server to answer, in ms. */
6706
6738
  const STARTS_WITHIN = 6e4;
6707
- /** This function waits until a check holds, or it stops the run after `within` ms. */
6708
- const until = async (what, check, within) => {
6739
+ /**
6740
+ * This function waits until a check holds, or it stops the run after `within` ms. At the stop, the
6741
+ * error also gives the text of `explain`: the state that tells why the check did not hold.
6742
+ */
6743
+ const until = async (what, check, within, explain) => {
6709
6744
  const started = Date.now();
6710
6745
  while (!await check().catch(() => false)) {
6711
- if (Date.now() - started > within) throw new Error(`${what} did not come in ${within} ms`);
6746
+ if (Date.now() - started > within) {
6747
+ const state = explain === void 0 ? "" : `. ${await explain().catch((error) => `No state: ${error instanceof Error ? error.message : String(error)}`)}`;
6748
+ throw new Error(`${what} did not come in ${within} ms${state}`);
6749
+ }
6712
6750
  await setTimeout$1(500);
6713
6751
  }
6714
6752
  };
6753
+ /** This function reads the state of a set that did not elect a primary after a restore (`electionStateText`). */
6754
+ const electionStateOf = async (lab, primary, logFrom) => {
6755
+ const status = await lab.script(primary, STATUS_SCRIPT).catch((error) => `no status (${error instanceof Error ? error.message : String(error)})`);
6756
+ return electionStateText(primary, status, lab.log, logFrom);
6757
+ };
6715
6758
  /** This function gives the time that the client sent a write. Zero is no write. */
6716
6759
  const writeOf = (events, n) => {
6717
6760
  const sent = n === 0 ? void 0 : events.find((event) => event.ev === "sent" && Number(event["n"]) === n);
@@ -6868,6 +6911,7 @@ const lose = async (lab, drill, primary, last) => {
6868
6911
  const restore = async (lab, drill, primary) => {
6869
6912
  const { set } = lab;
6870
6913
  const started = Date.now();
6914
+ const logFrom = lab.log.length;
6871
6915
  const port = set.port + 1;
6872
6916
  const slices = Array.from({ length: drill.slices }, (_, at) => `${VAULT}/slice-${at + 1}/local/oplog.rs.bson`);
6873
6917
  if (slices.length > 0) await lab.vault([
@@ -6898,7 +6942,7 @@ const restore = async (lab, drill, primary) => {
6898
6942
  const ips = Object.fromEntries(Object.entries(lab.members).map(([name, at]) => [name, at.ip]));
6899
6943
  const initiated = await lab.script(primary, initiateScript(set, ips));
6900
6944
  if (!initiated.includes("\"ok\":1")) throw new Error(`rs.initiate failed after the restore: ${initiated}`);
6901
- await until("the primary after the restore", async () => (await lab.script(primary, "print(db.hello().isWritablePrimary)")).includes("true"), STARTS_WITHIN);
6945
+ await until("the primary after the restore", async () => (await lab.script(primary, "print(db.hello().isWritablePrimary)")).includes("true"), STARTS_WITHIN, () => electionStateOf(lab, primary, logFrom));
6902
6946
  return {
6903
6947
  started,
6904
6948
  ended: Date.now()