-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
kafka.py
431 lines (382 loc) · 16.9 KB
/
kafka.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
import concurrent.futures
import json
import logging
from dataclasses import dataclass, field
from enum import Enum
from typing import Any, Dict, Iterable, List, Optional, Type
import confluent_kafka
import confluent_kafka.admin
import pydantic
from confluent_kafka.admin import (
AdminClient,
ConfigEntry,
ConfigResource,
TopicMetadata,
)
from datahub.configuration.common import AllowDenyPattern
from datahub.configuration.kafka import KafkaConsumerConnectionConfig
from datahub.configuration.source_common import DatasetSourceConfigBase
from datahub.emitter.mce_builder import (
make_data_platform_urn,
make_dataplatform_instance_urn,
make_dataset_urn_with_platform_instance,
make_domain_urn,
)
from datahub.emitter.mcp import MetadataChangeProposalWrapper
from datahub.emitter.mcp_builder import add_domain_to_entity_wu
from datahub.ingestion.api.common import PipelineContext
from datahub.ingestion.api.decorators import (
SupportStatus,
capability,
config_class,
platform_name,
support_status,
)
from datahub.ingestion.api.registry import import_path
from datahub.ingestion.api.source import SourceCapability
from datahub.ingestion.api.workunit import MetadataWorkUnit
from datahub.ingestion.source.common.subtypes import DatasetSubTypes
from datahub.ingestion.source.kafka_schema_registry_base import KafkaSchemaRegistryBase
from datahub.ingestion.source.state.entity_removal_state import GenericCheckpointState
from datahub.ingestion.source.state.stale_entity_removal_handler import (
StaleEntityRemovalHandler,
StaleEntityRemovalSourceReport,
StatefulStaleMetadataRemovalConfig,
)
from datahub.ingestion.source.state.stateful_ingestion_base import (
StatefulIngestionConfigBase,
StatefulIngestionSourceBase,
)
from datahub.metadata.com.linkedin.pegasus2avro.common import Status
from datahub.metadata.com.linkedin.pegasus2avro.metadata.snapshot import DatasetSnapshot
from datahub.metadata.com.linkedin.pegasus2avro.mxe import MetadataChangeEvent
from datahub.metadata.schema_classes import (
BrowsePathsClass,
DataPlatformInstanceClass,
DatasetPropertiesClass,
SubTypesClass,
)
from datahub.utilities.registries.domain_registry import DomainRegistry
from datahub.utilities.source_helpers import (
auto_stale_entity_removal,
auto_status_aspect,
)
logger = logging.getLogger(__name__)
class KafkaTopicConfigKeys(str, Enum):
MIN_INSYNC_REPLICAS_CONFIG = "min.insync.replicas"
RETENTION_SIZE_CONFIG = "retention.bytes"
RETENTION_TIME_CONFIG = "retention.ms"
CLEANUP_POLICY_CONFIG = "cleanup.policy"
MAX_MESSAGE_SIZE_CONFIG = "max.message.bytes"
UNCLEAN_LEADER_ELECTION_CONFIG = "unclean.leader.election.enable"
class KafkaSourceConfig(StatefulIngestionConfigBase, DatasetSourceConfigBase):
connection: KafkaConsumerConnectionConfig = KafkaConsumerConnectionConfig()
topic_patterns: AllowDenyPattern = AllowDenyPattern(allow=[".*"], deny=["^_.*"])
domain: Dict[str, AllowDenyPattern] = pydantic.Field(
default={},
description="A map of domain names to allow deny patterns. Domains can be urn-based (`urn:li:domain:13ae4d85-d955-49fc-8474-9004c663a810`) or bare (`13ae4d85-d955-49fc-8474-9004c663a810`).",
)
topic_subject_map: Dict[str, str] = pydantic.Field(
default={},
description="Provides the mapping for the `key` and the `value` schemas of a topic to the corresponding schema registry subject name. Each entry of this map has the form `<topic_name>-key`:`<schema_registry_subject_name_for_key_schema>` and `<topic_name>-value`:`<schema_registry_subject_name_for_value_schema>` for the key and the value schemas associated with the topic, respectively. This parameter is mandatory when the [RecordNameStrategy](https://docs.confluent.io/platform/current/schema-registry/serdes-develop/index.html#how-the-naming-strategies-work) is used as the subject naming strategy in the kafka schema registry. NOTE: When provided, this overrides the default subject name resolution even when the `TopicNameStrategy` or the `TopicRecordNameStrategy` are used.",
)
stateful_ingestion: Optional[StatefulStaleMetadataRemovalConfig] = None
schema_registry_class: str = pydantic.Field(
default="datahub.ingestion.source.confluent_schema_registry.ConfluentSchemaRegistry",
description="The fully qualified implementation class(custom) that implements the KafkaSchemaRegistryBase interface.",
)
ignore_warnings_on_schema_type: bool = pydantic.Field(
default=False,
description="Disables warnings reported for non-AVRO/Protobuf value or key schemas if set.",
)
@dataclass
class KafkaSourceReport(StaleEntityRemovalSourceReport):
topics_scanned: int = 0
filtered: List[str] = field(default_factory=list)
def report_topic_scanned(self, topic: str) -> None:
self.topics_scanned += 1
def report_dropped(self, topic: str) -> None:
self.filtered.append(topic)
@platform_name("Kafka")
@config_class(KafkaSourceConfig)
@support_status(SupportStatus.CERTIFIED)
@capability(
SourceCapability.PLATFORM_INSTANCE,
"For multiple Kafka clusters, use the platform_instance configuration",
)
@capability(
SourceCapability.SCHEMA_METADATA,
"Schemas associated with each topic are extracted from the schema registry. Avro and Protobuf (certified), JSON (incubating). Schema references are supported.",
)
class KafkaSource(StatefulIngestionSourceBase):
"""
This plugin extracts the following:
- Topics from the Kafka broker
- Schemas associated with each topic from the schema registry (Avro, Protobuf and JSON schemas are supported)
"""
platform: str = "kafka"
@classmethod
def create_schema_registry(
cls, config: KafkaSourceConfig, report: KafkaSourceReport
) -> KafkaSchemaRegistryBase:
try:
schema_registry_class: Type = import_path(config.schema_registry_class)
return schema_registry_class.create(config, report)
except (ImportError, AttributeError):
raise ImportError(config.schema_registry_class)
def __init__(self, config: KafkaSourceConfig, ctx: PipelineContext):
super().__init__(config, ctx)
self.source_config: KafkaSourceConfig = config
self.consumer: confluent_kafka.Consumer = confluent_kafka.Consumer(
{
"group.id": "test",
"bootstrap.servers": self.source_config.connection.bootstrap,
**self.source_config.connection.consumer_config,
}
)
self.init_kafka_admin_client()
self.report: KafkaSourceReport = KafkaSourceReport()
self.schema_registry_client: KafkaSchemaRegistryBase = (
KafkaSource.create_schema_registry(config, self.report)
)
if self.source_config.domain:
self.domain_registry = DomainRegistry(
cached_domains=[k for k in self.source_config.domain],
graph=self.ctx.graph,
)
# Create and register the stateful ingestion use-case handlers.
self.stale_entity_removal_handler = StaleEntityRemovalHandler(
source=self,
config=self.source_config,
state_type_class=GenericCheckpointState,
pipeline_name=self.ctx.pipeline_name,
run_id=self.ctx.run_id,
)
def init_kafka_admin_client(self) -> None:
try:
# TODO: Do we require separate config than existing consumer_config ?
self.admin_client = AdminClient(
{
"group.id": "test",
"bootstrap.servers": self.source_config.connection.bootstrap,
**self.source_config.connection.consumer_config,
}
)
except Exception as e:
logger.debug(e, exc_info=e)
self.report.report_warning(
"kafka-admin-client",
f"Failed to create Kafka Admin Client due to error {e}.",
)
def get_platform_instance_id(self) -> Optional[str]:
return self.source_config.platform_instance
@classmethod
def create(cls, config_dict: Dict, ctx: PipelineContext) -> "KafkaSource":
config: KafkaSourceConfig = KafkaSourceConfig.parse_obj(config_dict)
return cls(config, ctx)
def get_workunits(self) -> Iterable[MetadataWorkUnit]:
return auto_stale_entity_removal(
self.stale_entity_removal_handler,
auto_status_aspect(self.get_workunits_internal()),
)
def get_workunits_internal(self) -> Iterable[MetadataWorkUnit]:
topics = self.consumer.list_topics(
timeout=self.source_config.connection.client_timeout_seconds
).topics
extra_topic_details = self.fetch_extra_topic_details(topics.keys())
for t, t_detail in topics.items():
self.report.report_topic_scanned(t)
if self.source_config.topic_patterns.allowed(t):
yield from self._extract_record(t, t_detail, extra_topic_details.get(t))
else:
self.report.report_dropped(t)
def _extract_record(
self,
topic: str,
topic_detail: Optional[TopicMetadata],
extra_topic_config: Optional[Dict[str, ConfigEntry]],
) -> Iterable[MetadataWorkUnit]:
logger.debug(f"topic = {topic}")
# 1. Create the default dataset snapshot for the topic.
dataset_name = topic
platform_urn = make_data_platform_urn(self.platform)
dataset_urn = make_dataset_urn_with_platform_instance(
platform=self.platform,
name=dataset_name,
platform_instance=self.source_config.platform_instance,
env=self.source_config.env,
)
dataset_snapshot = DatasetSnapshot(
urn=dataset_urn,
aspects=[Status(removed=False)], # we append to this list later on
)
# 2. Attach schemaMetadata aspect (pass control to SchemaRegistry)
schema_metadata = self.schema_registry_client.get_schema_metadata(
topic, platform_urn
)
if schema_metadata is not None:
dataset_snapshot.aspects.append(schema_metadata)
# 3. Attach browsePaths aspect
browse_path_str = f"/{self.source_config.env.lower()}/{self.platform}"
if self.source_config.platform_instance:
browse_path_str += f"/{self.source_config.platform_instance}"
browse_path = BrowsePathsClass([browse_path_str])
dataset_snapshot.aspects.append(browse_path)
custom_props = self.build_custom_properties(
topic, topic_detail, extra_topic_config
)
dataset_properties = DatasetPropertiesClass(
name=topic,
customProperties=custom_props,
)
dataset_snapshot.aspects.append(dataset_properties)
# 4. Attach dataPlatformInstance aspect.
if self.source_config.platform_instance:
dataset_snapshot.aspects.append(
DataPlatformInstanceClass(
platform=platform_urn,
instance=make_dataplatform_instance_urn(
self.platform, self.source_config.platform_instance
),
)
)
# 5. Emit the datasetSnapshot MCE
mce = MetadataChangeEvent(proposedSnapshot=dataset_snapshot)
wu = MetadataWorkUnit(id=f"kafka-{topic}", mce=mce)
self.report.report_workunit(wu)
yield wu
# 5. Add the subtype aspect marking this as a "topic"
subtype_wu = MetadataWorkUnit(
id=f"{topic}-subtype",
mcp=MetadataChangeProposalWrapper(
entityUrn=dataset_urn,
aspect=SubTypesClass(typeNames=[DatasetSubTypes.TOPIC]),
),
)
self.report.report_workunit(subtype_wu)
yield subtype_wu
domain_urn: Optional[str] = None
# 6. Emit domains aspect MCPW
for domain, pattern in self.source_config.domain.items():
if pattern.allowed(dataset_name):
domain_urn = make_domain_urn(
self.domain_registry.get_domain_urn(domain)
)
if domain_urn:
wus = add_domain_to_entity_wu(
entity_urn=dataset_urn,
domain_urn=domain_urn,
)
for wu in wus:
self.report.report_workunit(wu)
yield wu
def build_custom_properties(
self,
topic: str,
topic_detail: Optional[TopicMetadata],
extra_topic_config: Optional[Dict[str, ConfigEntry]],
) -> Dict[str, str]:
custom_props: Dict[str, str] = {}
self.update_custom_props_with_topic_details(topic, topic_detail, custom_props)
self.update_custom_props_with_topic_config(
topic, extra_topic_config, custom_props
)
return custom_props
def update_custom_props_with_topic_details(
self,
topic: str,
topic_detail: Optional[TopicMetadata],
custom_props: Dict[str, str],
) -> None:
if topic_detail is None or topic_detail.partitions is None:
logger.info(
f"Partitions and Replication Factor not available for topic {topic}"
)
return
custom_props["Partitions"] = str(len(topic_detail.partitions))
replication_factor: Optional[int] = None
for _, p_meta in topic_detail.partitions.items():
if replication_factor is None or len(p_meta.replicas) > replication_factor:
replication_factor = len(p_meta.replicas)
if replication_factor is not None:
custom_props["Replication Factor"] = str(replication_factor)
def update_custom_props_with_topic_config(
self,
topic: str,
topic_config: Optional[Dict[str, ConfigEntry]],
custom_props: Dict[str, str],
) -> None:
if topic_config is None:
return
for config_key in KafkaTopicConfigKeys:
try:
if (
config_key in topic_config.keys()
and topic_config[config_key] is not None
):
config_value = topic_config[config_key].value
custom_props[config_key] = (
config_value
if isinstance(config_value, str)
else json.dumps(config_value)
)
except Exception as e:
logger.info(f"{config_key} is not available for topic due to error {e}")
def get_report(self) -> KafkaSourceReport:
return self.report
def close(self) -> None:
if self.consumer:
self.consumer.close()
super().close()
def _get_config_value_if_present(
self, config_dict: Dict[str, ConfigEntry], key: str
) -> Any:
return
def fetch_extra_topic_details(self, topics: List[str]) -> Dict[str, dict]:
extra_topic_details = {}
if not hasattr(self, "admin_client"):
logger.debug(
"Kafka Admin Client missing. Not fetching config details for topics."
)
else:
try:
extra_topic_details = self.fetch_topic_configurations(topics)
except Exception as e:
logger.debug(e, exc_info=e)
logger.warning(f"Failed to fetch config details due to error {e}.")
return extra_topic_details
def fetch_topic_configurations(self, topics: List[str]) -> Dict[str, dict]:
logger.info("Fetching config details for all topics")
configs: Dict[
ConfigResource, concurrent.futures.Future
] = self.admin_client.describe_configs(
resources=[ConfigResource(ConfigResource.Type.TOPIC, t) for t in topics],
request_timeout=self.source_config.connection.client_timeout_seconds,
)
logger.debug("Waiting for config details futures to complete")
concurrent.futures.wait(configs.values())
logger.debug("Config details futures completed")
topic_configurations: Dict[str, dict] = {}
for config_resource, config_result_future in configs.items():
self.process_topic_config_result(
config_resource, config_result_future, topic_configurations
)
return topic_configurations
def process_topic_config_result(
self,
config_resource: ConfigResource,
config_result_future: concurrent.futures.Future,
topic_configurations: dict,
) -> None:
try:
assert config_result_future.done()
assert config_result_future.exception() is None
topic_configurations[config_resource.name] = config_result_future.result()
except Exception as e:
logger.warning(
f"Config details for topic {config_resource.name} not fetched due to error {e}"
)
else:
logger.info(
f"Config details for topic {config_resource.name} fetched successfully"
)