-
Notifications
You must be signed in to change notification settings - Fork 13
/
neo4j.py
213 lines (183 loc) · 7.5 KB
/
neo4j.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
"""Neo4j instance test configuration (no runtime properties)."""
import os
import re
from dataclasses import dataclass
from os.path import join
import docker
@dataclass
class Config:
"""Configuration for a Neo4j instance."""
name: str
image: str
version: str
edition: str
cluster: bool
suite: str
scheme: str
stress_test_duration: int
username = "neo4j"
password = "pass"
class Standalone:
"""Single instance Neo4j server."""
def __init__(self, image, name, artifacts_path, hostname, port, version,
edition):
self.name = name
self._image = image
self._artifacts_path = join(artifacts_path, name)
self._container = None
self._hostname = hostname
self._port = port
match = re.match(r"(\d+)\.dev", version)
if match:
self._version = (int(match.group(1)), float("inf"))
else:
self._version = tuple(int(i) for i in version.split("."))
assert len(self._version) == 2
self._edition = edition
def start(self, network):
# Environment variables passed to the Neo4j docker container
env_map = {
"NEO4J_AUTH": f"{username}/{password}",
}
if self._version < (5, 0):
env_map.update({
"NEO4J_dbms_connector_bolt_advertised__address":
"%s:%d" % (self._hostname, self._port),
})
else:
# Config options renamed in 5.0
env_map.update({
"NEO4J_server_bolt_advertised__address":
f"{self._hostname}:{self._port}",
})
if self._version >= (5, 3) and len(password) < 8:
env_map["NEO4J_dbms_security_auth__minimum__password__length"] = \
str(len(password))
if self._version >= (5, 13):
env_map["NEO4J_server_bolt_telemetry_enabled"] = "true"
if self._edition != "community":
env_map["NEO4J_ACCEPT_LICENSE_AGREEMENT"] = "yes"
logs_path = join(self._artifacts_path, "logs")
self._container = docker.run(
self._image, self._hostname,
mount_map={logs_path: "/logs"},
env_map=env_map,
network=network
)
def addresses(self):
return [(self._hostname, self._port)]
def stop(self):
self._container.rm()
self._container = None
class Cluster:
"""Cluster of Neo4j servers."""
def __init__(self, image, name, artifacts_path, version, num_cores=3):
self.name = name
self._image = image
self._artifacts_path = join(artifacts_path, name)
self._version = version
self._num_cores = num_cores
self._cores = []
def start(self, network):
for i in range(self._num_cores):
core = Core(i, self._artifacts_path, self._version)
self._cores.append(core)
initial_members = [c.discover for c in self._cores]
for core in self._cores:
core.start(self._image, initial_members, network)
def addresses(self):
return [("core%d" % i, 7687) for i in range(self._num_cores)]
def stop(self):
for core in self._cores:
core.stop()
class Core:
"""Core member of Neo4j cluster."""
DISCOVERY_PORT = 5000
TRANSACTION_PORT = 6000
RAFT_PORT = 7000
def __init__(self, index, artifacts_path, version):
self.name = "core%d" % index
self.discover = "%s:%d" % (self.name, Core.DISCOVERY_PORT + index)
self.transaction = "%s:%d" % (self.name, Core.TRANSACTION_PORT + index)
self.raft = "%s:%d" % (self.name, Core.RAFT_PORT + index)
self._index = index
self._artifacts_path = join(artifacts_path, self.name)
self._container = None
match = re.match(r"(\d+)\.dev", version)
if match:
self._version = (int(match.group(1)), float("inf"))
else:
self._version = tuple(int(i) for i in version.split("."))
assert len(self._version) == 2
def start(self, image, initial_members, network):
env_map = {
"NEO4J_dbms_mode": "CORE",
"NEO4J_ACCEPT_LICENSE_AGREEMENT": "yes",
"NEO4J_AUTH": f"{username}/{password}",
}
if self._version >= (5, 3) and len(password) < 8:
env_map["NEO4J_dbms_security_auth__minimum__password__length"] = \
str(len(password))
if self._version < (5, 0):
env_map.update({
"NEO4J_dbms_connector_bolt_advertised__address":
f"{self.name}:7687",
"NEO4J_causal__clustering_discovery__type":
"LIST",
"NEO4J_causal__clustering_initial__discovery__members":
",".join(initial_members),
"NEO4J_causal__clustering_discovery__advertised__address":
self.discover,
"NEO4J_causal__clustering_raft__advertised__address":
self.raft,
"NEO4J_causal__clustering_transaction__advertised__address":
self.transaction,
"NEO4J_causal__clustering_discovery__listen__address":
"0.0.0.0:%d" % (Core.DISCOVERY_PORT + self._index),
"NEO4J_causal__clustering_raft__listen__address":
"0.0.0.0:%d" % (Core.RAFT_PORT + self._index),
"NEO4J_causal__clustering_transaction__listen__address":
"0.0.0.0:%d" % (Core.TRANSACTION_PORT + self._index),
})
else:
# Config options renamed in 5.0
env_map.update({
"NEO4J_server_bolt_advertised__address":
f"{self.name}:7687",
"NEO4J_dbms_cluster_discovery_type":
"LIST",
"NEO4J_dbms_cluster_discovery_initial__members":
",".join(initial_members),
"NEO4J_server_discovery_advertised__address":
self.discover,
"NEO4J_server_cluster_raft_advertised__address":
self.raft,
"NEO4J_server_cluster_advertised__address":
self.transaction,
"NEO4J_server_discovery_listen__address":
"0.0.0.0:%d" % (Core.DISCOVERY_PORT + self._index),
"NEO4J_server_cluster_raft_listen__address":
"0.0.0.0:%d" % (Core.RAFT_PORT + self._index),
"NEO4J_server_cluster_listen__address":
"0.0.0.0:%d" % (Core.TRANSACTION_PORT + self._index),
"NEO4J_initial_dbms_default__primaries__count":
str(len(initial_members)),
})
if self._version < (5, 16):
# Bug in server which can lead to the server SSRing a query
# to itself in an infinite loop.
# https://trello.com/c/NvCIKscB/1216-erroneous-auto-ssring
env_map.update({
"NEO4J_dbms_cluster_raft_leader__transfer_balancing__strategy": # noqa: E501
"NO_BALANCING", # noqa: E131
})
logs_path = join(self._artifacts_path, "logs")
os.makedirs(logs_path, exist_ok=True)
self._container = docker.run(
image, self.name,
env_map=env_map, network=network, mount_map={logs_path: "/logs"},
log_path=self._artifacts_path, background=True
)
def stop(self):
self._container.rm()
self._container = None