Add more runtime configurable settings (#1183)

server name, query timeout settings, log.level, log.to_stderr
This commit is contained in:
andrejtonev
2023-09-11 17:30:54 +02:00
committed by GitHub
parent 060b9d1c16
commit 5e5f4ffc5d
42 changed files with 949 additions and 97 deletions

View File

@@ -25,3 +25,4 @@ python3 docs_how_to_query.py || exit 1
python3 max_query_length.py || exit 1
python3 transactions.py || exit 1
python3 path.py || exit 1
python3 server_name.py || exit 1

View File

@@ -0,0 +1,52 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright 2021 Memgraph Ltd.
#
# Use of this software is governed by the Business Source License
# included in the file licenses/BSL.txt; by using this file, you agree to be bound by the terms of the Business Source
# License, and you may not use this file except in compliance with the Business Source License.
#
# As of the Change Date specified in that file, in accordance with
# the Business Source License, use of this software will be governed
# by the Apache License, Version 2.0, included in the file
# licenses/APL.txt.
from neo4j import GraphDatabase, basic_auth
from neo4j.exceptions import ClientError, TransientError
def get_server_name(tx):
res = tx.run("SHOW DATABASE SETTINGS").values()
for setting in res:
if setting[0] == "server.name":
return setting[1]
assert False, "No setting named server.name"
def set_server_name(tx, name):
tx.run("SET DATABASE SETTING 'server.name' TO '{}'".format(name)).consume()
# Connect, check name, set a new name and recheck
with GraphDatabase.driver("bolt://localhost:7687", auth=None, encrypted=False) as driver:
with driver.session() as session:
default_name = get_server_name(session)
assert driver.get_server_info().agent == default_name, "Wrong server name! Expected {} and got {}".format(
default_name, driver.get_server_info().agent
)
with driver.session() as session:
set_server_name(session, "Neo4j/1.1 compatible database")
with GraphDatabase.driver("bolt://localhost:7687", auth=None, encrypted=False) as driver:
assert (
driver.get_server_info().agent == "Neo4j/1.1 compatible database"
), 'Wrong server name! Expected "Neo4j/1.1 compatible database" and got {}'.format(driver.get_server_info().agent)
with driver.session() as session:
set_server_name(session, default_name)
print("All ok!")

View File

@@ -12,6 +12,8 @@
# by the Apache License, Version 2.0, included in the file
# licenses/APL.txt.
import time
from neo4j import GraphDatabase, basic_auth
from neo4j.exceptions import ClientError, TransientError
@@ -35,6 +37,44 @@ def tx_too_long(tx):
tx.run("MATCH (a), (b), (c), (d), (e), (f) RETURN COUNT(*) AS cnt")
def assert_timeout(set_timeout, measure_timeout):
print(measure_timeout)
print(set_timeout)
assert (
measure_timeout >= set_timeout and measure_timeout < set_timeout * 1.2
), "Wrong timeout; expected {}s and measured {}s".format(set_timeout, measure_timeout)
def get_timeout(tx):
res = tx.run("SHOW DATABASE SETTINGS").values()
for setting in res:
if setting[0] == "query.timeout":
return float(setting[1])
assert False, "No setting named query.timeout"
def set_timeout(tx, timeout):
tx.run("SET DATABASE SETTING 'query.timeout' TO '{}'".format(timeout)).consume()
def test_timeout(driver, set_timeout):
# Query that will run for a very long time, transient error expected.
timed_out = False
try:
with driver.session() as session:
start_time = time.time()
session.run("MATCH (a), (b), (c), (d), (e), (f) RETURN COUNT(*) AS cnt").consume()
except TransientError:
end_time = time.time()
assert_timeout(set_timeout, end_time - start_time)
timed_out = True
if timed_out:
print("The query timed out as was expected.")
else:
raise Exception("The query should have timed out, but it didn't!")
with GraphDatabase.driver("bolt://localhost:7687", auth=None, encrypted=False) as driver:
def add_person(f, name, name2):
@@ -53,17 +93,16 @@ with GraphDatabase.driver("bolt://localhost:7687", auth=None, encrypted=False) a
with driver.session() as session:
session.run("UNWIND range(1, 100000) AS x CREATE ()").consume()
# Query that will run for a very long time, transient error expected.
timed_out = False
try:
with driver.session() as session:
session.run("MATCH (a), (b), (c), (d), (e), (f) RETURN COUNT(*) AS cnt").consume()
except TransientError:
timed_out = True
# Test changing the timeout at run-time
with driver.session() as session:
default_timeout = get_timeout(session)
test_timeout(driver, default_timeout)
if timed_out:
print("The query timed out as was expected.")
else:
raise Exception("The query should have timed out, but it didn't!")
with driver.session() as session:
set_timeout(session, 1)
test_timeout(driver, 1)
with driver.session() as session:
set_timeout(session, default_timeout)
print("All ok!")