scenarios.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. # Copyright Materialize, Inc. and contributors. All rights reserved.
  2. #
  3. # Use of this software is governed by the Business Source License
  4. # included in the LICENSE file at the root of this repository.
  5. #
  6. # As of the Change Date specified in that file, in accordance with
  7. # the Business Source License, use of this software will be governed
  8. # by the Apache License, Version 2.0.
  9. from dataclasses import dataclass
  10. from random import Random
  11. from materialize.checks.actions import Action, Initialize, Manipulate, Validate
  12. from materialize.checks.checks import Check
  13. from materialize.checks.cloudtest_actions import ReplaceEnvironmentdStatefulSet
  14. from materialize.checks.executors import Executor
  15. from materialize.checks.features import Features
  16. from materialize.checks.mzcompose_actions import (
  17. ConfigureMz,
  18. KillClusterdCompute,
  19. KillMz,
  20. StartClusterdCompute,
  21. StartMz,
  22. UseClusterdCompute,
  23. )
  24. from materialize.checks.mzcompose_actions import (
  25. DropCreateDefaultReplica as DropCreateDefaultReplicaAction,
  26. )
  27. from materialize.checks.mzcompose_actions import (
  28. KillClusterdStorage as KillClusterdStorageAction,
  29. )
  30. from materialize.checks.mzcompose_actions import (
  31. RestartCockroach as RestartCockroachAction,
  32. )
  33. from materialize.checks.mzcompose_actions import (
  34. RestartRedpandaDebezium as RestartRedpandaDebeziumAction,
  35. )
  36. from materialize.checks.mzcompose_actions import (
  37. RestartSourcePostgres as RestartSourcePostgresAction,
  38. )
  39. from materialize.checks.mzcompose_actions import (
  40. SystemVarChange as SystemVarChangeAction,
  41. )
  42. from materialize.mz_version import MzVersion
  43. class Scenario:
  44. def __init__(
  45. self,
  46. checks: list[type[Check]],
  47. executor: Executor,
  48. features: Features,
  49. seed: str | None = None,
  50. ) -> None:
  51. self._checks = checks
  52. self.executor = executor
  53. self.features = features
  54. self.rng = None if seed is None else Random(seed)
  55. self._base_version = MzVersion.parse_cargo()
  56. filtered_check_classes = []
  57. for check_class in self.checks():
  58. if self._include_check_class(check_class):
  59. filtered_check_classes.append(check_class)
  60. # Use base_version() here instead of _base_version so that overwriting
  61. # upgrade scenarios can specify another base version.
  62. self.check_objects = [
  63. check_class(self.base_version(), self.rng, self.features)
  64. for check_class in filtered_check_classes
  65. ]
  66. def checks(self) -> list[type[Check]]:
  67. if self.rng:
  68. self.rng.shuffle(self._checks)
  69. return self._checks
  70. def actions(self) -> list[Action]:
  71. raise NotImplementedError
  72. def base_version(self) -> MzVersion:
  73. return self._base_version
  74. def run(self) -> None:
  75. actions = self.actions()
  76. # Configure implicitly for cloud scenarios
  77. if not isinstance(actions[0], StartMz):
  78. actions.insert(0, ConfigureMz(self))
  79. for index, action in enumerate(actions):
  80. # Implicitly call configure to raise version-dependent limits
  81. if isinstance(action, StartMz) and not action.deploy_generation:
  82. actions.insert(
  83. index + 1, ConfigureMz(self, mz_service=action.mz_service)
  84. )
  85. elif isinstance(action, ReplaceEnvironmentdStatefulSet):
  86. actions.insert(index + 1, ConfigureMz(self))
  87. for action in actions:
  88. action.execute(self.executor)
  89. action.join(self.executor)
  90. def requires_external_idempotence(self) -> bool:
  91. return False
  92. def _include_check_class(self, check_class: type[Check]) -> bool:
  93. return not check_class.__name__.endswith("Base") and (
  94. not self.requires_external_idempotence()
  95. or check_class.externally_idempotent
  96. )
  97. class NoRestartNoUpgrade(Scenario):
  98. def actions(self) -> list[Action]:
  99. return [
  100. StartMz(self),
  101. Initialize(self),
  102. Manipulate(self, phase=1),
  103. Manipulate(self, phase=2),
  104. Validate(self),
  105. ]
  106. class RestartEntireMz(Scenario):
  107. def actions(self) -> list[Action]:
  108. return [
  109. StartMz(self),
  110. Initialize(self),
  111. KillMz(),
  112. StartMz(self),
  113. Manipulate(self, phase=1),
  114. KillMz(),
  115. StartMz(self),
  116. Manipulate(self, phase=2),
  117. KillMz(),
  118. StartMz(self),
  119. Validate(self),
  120. ]
  121. class DropCreateDefaultReplica(Scenario):
  122. def actions(self) -> list[Action]:
  123. return [
  124. StartMz(self),
  125. Initialize(self),
  126. Manipulate(self, phase=1),
  127. DropCreateDefaultReplicaAction(self),
  128. Manipulate(self, phase=2),
  129. Validate(self),
  130. ]
  131. class RestartClusterdCompute(Scenario):
  132. """Restart clusterd by having it run in a separate container that is then killed and restarted."""
  133. def actions(self) -> list[Action]:
  134. return [
  135. StartMz(self),
  136. StartClusterdCompute(),
  137. UseClusterdCompute(self),
  138. Initialize(self),
  139. KillClusterdCompute(),
  140. StartClusterdCompute(),
  141. Manipulate(self, phase=1),
  142. KillClusterdCompute(),
  143. StartClusterdCompute(),
  144. Manipulate(self, phase=2),
  145. KillClusterdCompute(),
  146. StartClusterdCompute(),
  147. Validate(self),
  148. ]
  149. class RestartEnvironmentdClusterdStorage(Scenario):
  150. """Restart environmentd and storage clusterds (as spawned from it), while keeping computed running by placing it in a separate container."""
  151. def actions(self) -> list[Action]:
  152. return [
  153. StartMz(self),
  154. StartClusterdCompute(),
  155. UseClusterdCompute(self),
  156. Initialize(self),
  157. KillMz(),
  158. StartMz(self),
  159. Manipulate(self, phase=1),
  160. KillMz(),
  161. StartMz(self),
  162. Manipulate(self, phase=2),
  163. KillMz(),
  164. StartMz(self),
  165. Validate(self),
  166. # Validate again so that introducing non-idempotent validate()s
  167. # will cause the CI to fail.
  168. Validate(self),
  169. ]
  170. class KillClusterdStorage(Scenario):
  171. """Kill storage clusterd while it is running inside the enviromentd container. The process orchestrator will (try to) start it again."""
  172. def actions(self) -> list[Action]:
  173. return [
  174. StartMz(self),
  175. StartClusterdCompute(),
  176. UseClusterdCompute(self),
  177. Initialize(self),
  178. KillClusterdStorageAction(),
  179. Manipulate(self, phase=1),
  180. KillClusterdStorageAction(),
  181. Manipulate(self, phase=2),
  182. KillClusterdStorageAction(),
  183. Validate(self),
  184. ]
  185. class RestartCockroach(Scenario):
  186. def actions(self) -> list[Action]:
  187. return [
  188. StartMz(self),
  189. Initialize(self),
  190. RestartCockroachAction(),
  191. Manipulate(self, phase=1),
  192. RestartCockroachAction(),
  193. Manipulate(self, phase=2),
  194. RestartCockroachAction(),
  195. Validate(self),
  196. ]
  197. class RestartSourcePostgres(Scenario):
  198. def actions(self) -> list[Action]:
  199. return [
  200. StartMz(self),
  201. Initialize(self),
  202. RestartSourcePostgresAction(),
  203. Manipulate(self, phase=1),
  204. RestartSourcePostgresAction(),
  205. Manipulate(self, phase=2),
  206. RestartSourcePostgresAction(),
  207. Validate(self),
  208. ]
  209. class RestartRedpandaDebezium(Scenario):
  210. def actions(self) -> list[Action]:
  211. return [
  212. StartMz(self),
  213. Initialize(self),
  214. RestartRedpandaDebeziumAction(),
  215. Manipulate(self, phase=1),
  216. RestartRedpandaDebeziumAction(),
  217. Manipulate(self, phase=2),
  218. RestartRedpandaDebeziumAction(),
  219. Validate(self),
  220. ]
  221. @dataclass
  222. class SystemVarChangeEntry:
  223. name: str
  224. value_for_manipulate_phase_1: str
  225. value_for_manipulate_phase_2: str
  226. def value_for_validation_1(self) -> str:
  227. return self.value_for_manipulate_phase_1
  228. def value_for_validation_2(self) -> str:
  229. return self.value_for_manipulate_phase_2
  230. # This scenario is not instantiated. Create a subclass to use it.
  231. class SystemVarChange(Scenario):
  232. def __init__(
  233. self,
  234. checks: list[type[Check]],
  235. executor: Executor,
  236. features: Features,
  237. seed: str | None,
  238. change_entries: list[SystemVarChangeEntry],
  239. ):
  240. super().__init__(checks, executor, features, seed)
  241. self.change_entries = change_entries
  242. def actions(self) -> list[Action]:
  243. validations = []
  244. for var in self.change_entries:
  245. validations.append(
  246. SystemVarChangeAction(
  247. name=var.name, value=var.value_for_manipulate_phase_2
  248. )
  249. )
  250. validations.append(Validate(self))
  251. return [
  252. StartMz(self),
  253. Initialize(self),
  254. # manipulate phase 1
  255. *[
  256. SystemVarChangeAction(
  257. name=var.name, value=var.value_for_manipulate_phase_1
  258. )
  259. for var in self.change_entries
  260. ],
  261. Manipulate(self, phase=1),
  262. # manipulate phase 2
  263. *[
  264. SystemVarChangeAction(
  265. name=var.name, value=var.value_for_manipulate_phase_2
  266. )
  267. for var in self.change_entries
  268. ],
  269. Manipulate(self, phase=2),
  270. # validation 1
  271. *[
  272. SystemVarChangeAction(name=var.name, value=var.value_for_validation_1())
  273. for var in self.change_entries
  274. ],
  275. Validate(self),
  276. # validation 2
  277. *[
  278. SystemVarChangeAction(name=var.name, value=var.value_for_validation_2())
  279. for var in self.change_entries
  280. ],
  281. Validate(self),
  282. ]