-
Notifications
You must be signed in to change notification settings - Fork 97
/
KafkaBoltFluxComponent.java
186 lines (171 loc) · 10.2 KB
/
KafkaBoltFluxComponent.java
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
/**
* Copyright 2017 Hortonworks.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
package com.hortonworks.streamline.streams.layout.storm;
import com.hortonworks.streamline.streams.layout.TopologyLayoutConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.List;
/**
* Implementation for KafkaBolt
*/
public class KafkaBoltFluxComponent extends AbstractFluxComponent {
private static final Logger LOG = LoggerFactory.getLogger(KafkaBoltFluxComponent.class);
public KafkaBoltFluxComponent () {}
@Override
protected void generateComponent() {
validateSSLConfig();
setSaslJaasConfig();
String boltId = "kafkaBolt" + UUID_FOR_COMPONENTS;
String boltClassName = "org.apache.storm.kafka.bolt.KafkaBolt";
List<Object> configMethods = new ArrayList<>();
String[] configMethodNames = {"setFireAndForget", "setAsync"};
String[] configKeys = {"fireAndForget", "async"};
configMethods.addAll(getConfigMethodsYaml(configMethodNames, configKeys));
String[] moreConfigMethodNames = {
"withTupleToKafkaMapper",
"withTopicSelector",
"withProducerProperties"
};
String[] configMethodArgRefs = new String[moreConfigMethodNames.length];
configMethodArgRefs[0] = addTupleToKafkaMapper();
configMethodArgRefs[1] = addTopicSelector();
configMethodArgRefs[2] = addProducerProperties();
configMethods.addAll(getConfigMethodWithRefArg(moreConfigMethodNames, configMethodArgRefs));
component = createComponent(boltId, boltClassName, null, null, configMethods);
addParallelismToComponent();
}
private String addTupleToKafkaMapper () {
String mapperComponentId = "tupleToKafkaMapper" + UUID_FOR_COMPONENTS;
String mapperClassName = "com.hortonworks.streamline.streams.runtime.storm.bolt.kafka.StreamlineEventToKafkaMapper";
String[] constructorArgNames = { "keyField" };
List<Object> constructorArgs = getConstructorArgsYaml(constructorArgNames);
if (constructorArgs.isEmpty()) {
constructorArgs.add("");
}
addToComponents(createComponent(mapperComponentId, mapperClassName, null, constructorArgs, null));
return mapperComponentId;
}
private String addTopicSelector () {
String topicSelectorComponentId = "topicSelector" + UUID_FOR_COMPONENTS;
String topicSelectorClassName = "org.apache.storm.kafka.bolt.selector.DefaultTopicSelector";
String[] constructorArgNames = { "topic" };
List<Object> constructorArgs = getConstructorArgsYaml(constructorArgNames);
addToComponents(createComponent(topicSelectorComponentId, topicSelectorClassName, null, constructorArgs, null));
return topicSelectorComponentId;
}
private String addProducerProperties () {
String producerPropertiesComponentId = "producerProperties" + UUID_FOR_COMPONENTS;
String producerPropertiesClassName = "java.util.Properties";
String methodName = "put";
String[] specialPropertyNames = {
"key.serializer", "value.serializer", "acks"
};
//fieldNames and propertyNames arrays should be of same length
String[] propertyNames = {
"bootstrap.servers", "buffer.memory", "compression.type", "retries", "batch.size", "client.id", "connections.max.idle.ms",
"linger.ms", "max.block.ms", "max.request.size", "receive.buffer.bytes", "request.timeout.ms", "security.protocol", "send.buffer.bytes",
"timeout.ms", "block.on.buffer.full", "max.in.flight.requests.per.connection", "metadata.fetch.timeout.ms", "metadata.max.age.ms",
"reconnect.backoff.ms", "retry.backoff.ms", "schema.registry.url", "serdes.protocol.version", "writer.schema.version", "sasl.kerberos.service.name", "sasl.jaas.config",
"ssl.keystore.location", "ssl.keystore.password", "ssl.key.password", "ssl.truststore.location", "ssl.truststore.password", "ssl.enabled.protocols",
"ssl.keystore.type", "ssl.truststore.type", "ssl.protocol", "ssl.provider", "ssl.cipher.suites", "ssl.endpoint.identification.algorithm",
"ssl.keymanager.algorithm", "ssl.secure.random.implementation"
};
String[] fieldNames = {
"bootstrapServers", "bufferMemory", "compressionType", "retries", "batchSize", "clientId", "maxConnectionIdle",
"lingerTime", "maxBlock", "maxRequestSize", "receiveBufferSize", "requestTimeout", "securityProtocol", "sendBufferSize",
"timeout", "blocKOnBufferFull", "maxInflighRequests", "metadataFetchTimeout", "metadataMaxAge", "reconnectBackoff", "retryBackoff",
TopologyLayoutConstants.SCHEMA_REGISTRY_URL,"serProtocolVersion", "writerSchemaVersion", KafkaSpoutFluxComponent.SASL_KERBEROS_SERVICE_NAME,
KafkaSpoutFluxComponent.SASL_JAAS_CONFIG_KEY, "sslKeystoreLocation", "sslKeystorePassword", "sslKeyPassword", "sslTruststoreLocation",
"sslTruststorePassword", "sslEnabledProtocols", "sslKeystoreType", "sslTruststoreType", "sslProtocol", "sslProvider", "sslCipherSuites",
"sslEndpointIdAlgo", "sslKeyManagerAlgo", "sslSecureRandomImpl", "sslTrustManagerAlgo"
};
List<String> methodNames = new ArrayList<>();
List<Object> args = new ArrayList<>();
methodNames.add(methodName);
args.add(new String[]{specialPropertyNames[0], getKeySerializer()});
methodNames.add(methodName);
args.add(new String[]{specialPropertyNames[1], "com.hortonworks.streamline.streams.runtime.storm.bolt.kafka.StreamlineEventSerializer"});
methodNames.add(methodName);
args.add(new String[]{specialPropertyNames[2], getAckMode()});
for (int j = 0; j < propertyNames.length; ++j) {
if (conf.get(fieldNames[j]) != null) {
methodNames.add(methodName);
args.add(new Object[]{propertyNames[j], conf.get(fieldNames[j])});
}
}
addToComponents(createComponent(producerPropertiesComponentId, producerPropertiesClassName, null, null, getConfigMethodsYaml(methodNames.toArray(new String[methodNames.size()]), args.toArray())));
return producerPropertiesComponentId;
}
private String getKeySerializer () {
String keySerializer = (String) conf.get("keySerializer");
if ((keySerializer == null) || "ByteArray".equals(keySerializer)) {
return "org.apache.kafka.common.serialization.ByteArraySerializer";
} else if ("String".equals(keySerializer)) {
return "org.apache.kafka.common.serialization.StringSerializer";
} else if ("Integer".equals(keySerializer)) {
return "org.apache.kafka.common.serialization.IntegerSerializer";
} else if ("Long".equals(keySerializer)) {
return "org.apache.kafka.common.serialization.LongSerializer";
} else {
throw new IllegalArgumentException("Key serializer for kafka sink is not supported: " + keySerializer);
}
}
private String getAckMode () {
String ackMode = (String) conf.get("ackMode");
if ("None".equals(ackMode)) {
return "0";
} else if ("Leader".equals(ackMode) || (ackMode == null)) {
return "1";
} else if ("All".equals(ackMode)) {
return "all";
} else {
throw new IllegalArgumentException("Ack mode for kafka sink is not supported: " + ackMode);
}
}
private void setSaslJaasConfig () {
String securityProtocol = (String) conf.get("securityProtocol");
if (securityProtocol != null && !securityProtocol.isEmpty() && securityProtocol.startsWith("SASL")) {
StringBuilder saslConfigStrBuilder = new StringBuilder();
String kafkaServiceName = (String) conf.get(KafkaSpoutFluxComponent.SASL_KERBEROS_SERVICE_NAME);
String principal = (String) conf.get("principal");
String keytab = (String) conf.get("keytab");
if (kafkaServiceName == null || kafkaServiceName.isEmpty()) {
throw new IllegalArgumentException("Kafka service name must be provided for SASL GSSAPI Kerberos");
}
if (principal == null || principal.isEmpty()) {
throw new IllegalArgumentException("Kafka client principal must be provided for SASL GSSAPI Kerberos");
}
if (keytab == null || keytab.isEmpty()) {
throw new IllegalArgumentException("Kafka client principal keytab must be provided for SASL GSSAPI Kerberos");
}
saslConfigStrBuilder.append("com.sun.security.auth.module.Krb5LoginModule required useKeyTab=true storeKey=true keyTab=\"");
saslConfigStrBuilder.append(keytab).append("\" principal=\"").append(principal).append("\";");
conf.put(KafkaSpoutFluxComponent.SASL_JAAS_CONFIG_KEY, saslConfigStrBuilder.toString());
}
}
private void validateSSLConfig () {
String securityProtocol = (String) conf.get("securityProtocol");
if (securityProtocol != null && !securityProtocol.isEmpty() && securityProtocol.endsWith("SSL")) {
String truststoreLocation = (String) conf.get("sslTruststoreLocation");
String truststorePassword = (String) conf.get("sslTruststorePassword");
if (truststoreLocation == null || truststoreLocation.isEmpty()) {
throw new IllegalArgumentException("Truststore location must be provided for SSL");
}
if (truststorePassword == null || truststorePassword.isEmpty()) {
throw new IllegalArgumentException("Truststore password must be provided for SSL");
}
}
}
}