mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Valery Kharseko
22 hours ago 2863452733f1815ce5557ff01cfe148b49e182b0
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
#!/usr/bin/env python3
#
# The contents of this file are subject to the terms of the Common Development and
# Distribution License (the License). You may not use this file except in compliance with the
# License.
#
# You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
# specific language governing permission and limitations under the License.
#
# When distributing Covered Software, include this CDDL Header Notice in each file and include
# the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
# Header, with the fields enclosed by brackets [] replaced by your own identifying
# information: "Portions copyright [year] [name of copyright owner]".
#
# Copyright 2026 3A Systems, LLC.
 
"""Compare the last fill operation CSN with a surviving RS's changelog position."""
 
import base64
import importlib.util
from pathlib import Path
import re
import sys
 
sys.dont_write_bytecode = True
spec = importlib.util.spec_from_file_location('checkpoint', Path(__file__).with_name('changelog-checkpoint.py'))
checkpoint = importlib.util.module_from_spec(spec)
spec.loader.exec_module(checkpoint)
 
 
def writer_position(path, expected='scale-50001'):
    lines = []
    for line in Path(path).read_text().splitlines():
        if line.startswith(' '):
            if not lines or not lines[-1]:
                raise ValueError('orphan LDIF continuation')
            lines[-1] += line[1:]
        else:
            lines.append(line)
    descriptions, histories, dns = [], [], 0
    for line in lines:
        name, separator, value = line.partition(':')
        if not separator:
            continue
        name = name.lower()
        if name not in ('dn', 'description', 'ds-sync-hist'):
            continue
        if value.startswith(':'):
            value = base64.b64decode(value[1:].strip(), validate=True).decode('utf-8')
        else:
            value = value.lstrip()
        if name == 'dn':
            dns += 1
        elif name == 'description':
            descriptions.append(value)
        else:
            histories.append(value)
    if dns != 1 or descriptions != [expected]:
        raise ValueError('missing or ambiguous final fill entry')
    matches = []
    for value in histories:
        match = re.fullmatch(r'description:([0-9a-fA-F]{28}):(?:repl|add):' + re.escape(expected), value,
                             re.IGNORECASE)
        if match:
            matches.append(match[1].lower())
    if len(matches) != 1:
        raise ValueError('missing or ambiguous final fill CSN')
    return int(matches[0][16:20], 16), matches[0]
 
 
def main(argv):
    if len(argv) != 4:
        print('usage: scale-fence.py <base DN> <fill entry LDIF> <RS monitor LDIF>', file=sys.stderr)
        return 2
    try:
        server_id, wanted = writer_position(argv[2])
        current = checkpoint.changelog_positions(argv[3]).get((argv[1].lower(), server_id))
        if current is None or checkpoint.position(current) < checkpoint.position(wanted):
            print('DS(%s): needs %s, RS has %s' % (server_id, wanted, current or 'no position'))
            return 1
        print('DS(%s): needs %s, RS has %s (reached)' % (server_id, wanted, current))
        return 0
    except (OSError, ValueError) as error:
        print('cannot read fence: %s' % error, file=sys.stderr)
        return 2
 
 
if __name__ == '__main__':
    sys.exit(main(sys.argv))