forked from nocproject/noc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmigrate-liftbridge.py
289 lines (267 loc) · 12 KB
/
migrate-liftbridge.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# ----------------------------------------------------------------------
# Liftbridge streams synchronization tool
# ----------------------------------------------------------------------
# Copyright (C) 2007-2020 The NOC Project
# See LICENSE for details
# ----------------------------------------------------------------------
# Python modules
from typing import Tuple, Dict
import functools
# NOC modules
from noc.core.management.base import BaseCommand
from noc.core.liftbridge.base import LiftBridgeClient, Metadata, StreamMetadata, StartPosition
from noc.main.models.pool import Pool
from noc.pm.models.metricscope import MetricScope
from noc.core.mongo.connection import connect
from noc.core.service.loader import get_dcs
from noc.core.ioloop.util import run_sync
from noc.core.clickhouse.loader import loader as bi_loader
from noc.config import config
class Command(BaseCommand):
# List of single-partitioned streams
STREAMS = [
"revokedtokens",
]
# Streams, depending on slots
SLOT_STREAMS = [
# slot name, stream name
("mx", "message"),
("kafkasender", "kafkasender"),
]
# Pool-basend streams, depending on slots
POOLED_SLOT_STREAMS = [
# slot name, stream name
("classifier-%s", "events.%s"),
("correlator-%s", "dispose.%s"),
]
CURSOR_STREAM = {
"events": "classifier",
"dispose": "correlator",
"mx": "mx",
"kafkasender": "kafkasender",
}
def handle(self, *args, **options):
changed = False
# Get liftbridge metadata
meta = self.get_meta()
rf = min(len(meta.brokers), 2)
# Apply settings
for stream, partitions in self.iter_limits():
if not partitions:
self.print("Stream '%s' without partition. Skipping.." % partitions)
continue
self.print("Ensuring stream %s" % stream)
changed |= self.apply_stream_settings(meta, stream, partitions, rf)
if changed:
self.print("CHANGED")
else:
self.print("OK")
def get_meta(self) -> Metadata:
async def get_meta() -> Metadata:
async with LiftBridgeClient() as client:
return await client.fetch_metadata()
return run_sync(get_meta)
def iter_slot_streams(self) -> Tuple[str, str]:
# Common streams
for slot_name, stream_name in self.SLOT_STREAMS:
yield slot_name, stream_name
# Pooled streams
connect()
for pool in Pool.objects.all():
for slot_mask, stream_mask in self.POOLED_SLOT_STREAMS:
yield slot_mask % pool.name, stream_mask % pool.name
def iter_limits(self) -> Tuple[str, int]:
async def get_slot_limits():
nonlocal slot_name
return await dcs.get_slot_limit(slot_name)
dcs = get_dcs()
# Plain streams
for stream_name in self.STREAMS:
yield stream_name, 1
# Slot-based streams
for slot_name, stream_name in self.iter_slot_streams():
n_partitions = run_sync(get_slot_limits)
if n_partitions:
yield stream_name, n_partitions
# Metric scopes
n_ch_shards = len(config.clickhouse.cluster_topology.split(","))
for scope in MetricScope.objects.all():
yield f"ch.{scope.table_name}", n_ch_shards
# BI models
for name in bi_loader:
bi_model = bi_loader[name]
if not bi_model:
continue
yield f"ch.{bi_model._meta.db_table}", n_ch_shards
def apply_stream_settings(self, meta: Metadata, stream: str, partitions: int, rf: int) -> bool:
def delete_stream(name: str):
async def wrapper():
async with LiftBridgeClient() as client:
await client.delete_stream(client.get_offset_stream(name))
await client.delete_stream(name)
run_sync(wrapper)
def create_stream(name: str, n_partitions: int, replication_factor: int):
base_name = name.split(".")[0]
minisr = 0
if base_name == "ch":
replication_factor = min(
config.liftbridge.stream_ch_replication_factor, replication_factor
)
minisr = min(2, replication_factor)
async def wrapper():
async with LiftBridgeClient() as client:
await client.create_stream(
subject=name,
name=name,
partitions=n_partitions,
minisr=minisr,
replication_factor=replication_factor,
retention_max_bytes=getattr(
config.liftbridge, f"stream_{base_name}_retention_max_age", 0
),
retention_max_age=getattr(
config.liftbridge, f"stream_{base_name}_retention_max_bytes", 0
),
segment_max_bytes=getattr(
config.liftbridge, f"stream_{base_name}_segment_max_bytes", 0
),
segment_max_age=getattr(
config.liftbridge, f"stream_{base_name}_segment_max_age", 0
),
auto_pause_time=getattr(
config.liftbridge, f"stream_{base_name}_auto_pause_time", 0
),
auto_pause_disable_if_subscribers=getattr(
config.liftbridge,
f"stream_{base_name}_auto_pause_disable_if_subscribers",
False,
),
)
run_sync(wrapper)
def alter_stream(
current_meta: StreamMetadata, new_partitions: int, replication_factor: int
):
name = current_meta.name
old_partitions = len(current_meta.partitions)
n_msg: Dict[int, int] = {} # partition -> copied messages
async def get_partition_meta(stream, partition):
async with LiftBridgeClient() as client:
return await client.fetch_partition_metadata(stream, partition)
async def wrapper():
self.print("Altering stream %s" % name)
async with LiftBridgeClient() as client:
# Create temporary stream with same structure, as original one
tmp_stream = "__tmp-%s" % name
self.print("Creating temporary stream %s" % tmp_stream)
await client.create_stream(
subject=tmp_stream,
name=tmp_stream,
partitions=old_partitions,
replication_factor=replication_factor,
)
# Copy all unread data to temporary stream as is
for partition in range(old_partitions):
self.print(
"Copying partition %s:%s to %s:%s"
% (name, partition, tmp_stream, partition)
)
n_msg[partition] = 0
# Get current offset
p_meta = run_sync(functools.partial(get_partition_meta, stream, partition))
newest_offset = p_meta.newest_offset or 0
# Fetch cursor
current_offset = await client.fetch_cursor(
stream=stream,
partition=partition,
cursor_id=self.CURSOR_STREAM[name.split(".")[0]],
)
if current_offset > newest_offset:
# Fix if cursor not set properly
current_offset = newest_offset
self.print(
"Start copying from current_offset: %s to newest offset: %s"
% (current_offset, newest_offset)
)
if current_offset < newest_offset:
async for msg in client.subscribe(
stream=name, partition=partition, start_offset=current_offset
):
await client.publish(
msg.value,
stream=tmp_stream,
partition=partition,
)
n_msg[partition] += 1
if msg.offset == newest_offset:
break
if n_msg[partition]:
self.print(" %d messages has been copied" % n_msg[partition])
else:
self.print(" nothing to copy")
# Drop original stream
self.print("Dropping original stream %s" % name)
await client.delete_stream(name)
# Create new stream with required structure
self.print("Creating stream %s" % name)
await client.create_stream(
subject=name,
name=name,
partitions=new_partitions,
replication_factor=replication_factor,
)
# Copy data from temporary stream to a new one
for partition in range(old_partitions):
self.print(
"Restoring partition %s:%s to %s"
% (tmp_stream, partition, new_partitions)
)
# Re-route dropped partitions to partition 0
dest_partition = partition if partition < new_partitions else 0
n = n_msg[partition]
if n > 0:
async for msg in client.subscribe(
stream=tmp_stream,
partition=partition,
start_position=StartPosition.EARLIEST,
):
await client.publish(
msg.value, stream=name, partition=dest_partition
)
n -= 1
if not n:
break
self.print(" %d messages restored" % n_msg[partition])
else:
self.print(" nothing to restore")
# Drop temporary stream
self.print("Dropping temporary stream %s" % tmp_stream)
await client.delete_stream(tmp_stream)
# Uh-oh
self.print("Stream %s has been altered" % name)
run_sync(wrapper)
stream_meta = None
for m in meta.metadata:
if m.name == stream:
stream_meta = m
break
# Check if stream is configured properly
if stream_meta and len(stream_meta.partitions) == partitions:
return False
# Check if stream must be altered
if stream_meta:
self.print(
"Altering stream %s due to partition/replication factor mismatch (%d -> %d)"
% (
stream,
len(stream_meta.partitions),
partitions,
)
)
alter_stream(stream_meta, partitions, rf)
return True
# Create stream
self.print("Creating stream %s with %d partitions" % (stream, partitions))
create_stream(stream, partitions, rf)
return True
if __name__ == "__main__":
Command().run()