cockroach.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. import os
  10. from materialize import MZ_ROOT
  11. from materialize.mzcompose import (
  12. DEFAULT_CRDB_ENVIRONMENT,
  13. loader,
  14. )
  15. from materialize.mzcompose.service import (
  16. Service,
  17. ServiceHealthcheck,
  18. )
  19. class Cockroach(Service):
  20. # TODO: Bump version to >= v24.3.0 when https://github.com/cockroachdb/cockroach/issues/136678 is fixed
  21. DEFAULT_COCKROACH_TAG = "v24.2.0"
  22. def __init__(
  23. self,
  24. name: str = "cockroach",
  25. aliases: list[str] = ["cockroach"],
  26. image: str | None = None,
  27. command: list[str] | None = None,
  28. setup_materialize: bool = True,
  29. in_memory: bool = False,
  30. healthcheck: ServiceHealthcheck | None = None,
  31. # Workaround for database-issues#5898, should be "no" otherwise
  32. restart: str = "on-failure:5",
  33. stop_grace_period: str = "120s",
  34. ):
  35. volumes = []
  36. if image is None:
  37. image = f"cockroachdb/cockroach:{Cockroach.DEFAULT_COCKROACH_TAG}"
  38. if command is None:
  39. command = ["start-single-node", "--insecure"]
  40. if setup_materialize:
  41. path = os.path.relpath(
  42. MZ_ROOT / "misc" / "cockroach" / "setup_materialize.sql",
  43. loader.composition_path,
  44. )
  45. volumes += [f"{path}:/docker-entrypoint-initdb.d/setup_materialize.sql"]
  46. if in_memory:
  47. command.append("--store=type=mem,size=2G")
  48. if healthcheck is None:
  49. healthcheck = {
  50. # init_success is a file created by the Cockroach container entrypoint
  51. "test": "[ -f init_success ] && curl --fail 'http://localhost:8080/health?ready=1'",
  52. "interval": "1s",
  53. "start_period": "30s",
  54. }
  55. super().__init__(
  56. name=name,
  57. config={
  58. "image": image,
  59. "networks": {"default": {"aliases": aliases}},
  60. "ports": [26257],
  61. "command": command,
  62. "volumes": volumes,
  63. "init": True,
  64. "healthcheck": healthcheck,
  65. "restart": restart,
  66. "environment": DEFAULT_CRDB_ENVIRONMENT,
  67. "stop_grace_period": stop_grace_period,
  68. },
  69. )