/
ControllerStarter.java
274 lines (247 loc) · 11 KB
/
ControllerStarter.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
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
/**
* Copyright (C) 2015-2016 Uber Technology Inc. (streaming-core@uber.com)
*
* 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.uber.stream.kafka.mirrormaker.controller;
import com.uber.stream.kafka.mirrormaker.controller.core.AutoTopicWhitelistingManager;
import com.uber.stream.kafka.mirrormaker.controller.core.ClusterInfoBackupManager;
import com.uber.stream.kafka.mirrormaker.controller.core.FileBackUpHandler;
import com.uber.stream.kafka.mirrormaker.controller.core.GitBackUpHandler;
import com.uber.stream.kafka.mirrormaker.controller.core.HelixMirrorMakerManager;
import com.uber.stream.kafka.mirrormaker.controller.core.KafkaBrokerTopicObserver;
import com.uber.stream.kafka.mirrormaker.controller.reporter.HelixKafkaMirrorMakerMetricsReporter;
import com.uber.stream.kafka.mirrormaker.controller.rest.ControllerRestApplication;
import com.uber.stream.kafka.mirrormaker.controller.validation.SourceKafkaClusterValidationManager;
import com.uber.stream.kafka.mirrormaker.controller.validation.ValidationManager;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.restlet.Application;
import org.restlet.Component;
import org.restlet.Context;
import org.restlet.data.Protocol;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* The main entry point for everything.
*
* @author xiangfu
*/
public class ControllerStarter {
private static final String DEST_KAFKA_CLUSTER = "destKafkaCluster";
private static final String SRC_KAFKA_CLUSTER = "srcKafkaCluster";
private static final Logger LOGGER = LoggerFactory.getLogger(ControllerStarter.class);
private final ControllerConf _config;
private final Component _component;
private final Application _controllerRestApp;
private final HelixMirrorMakerManager _helixMirrorMakerManager;
private final ValidationManager _validationManager;
private final SourceKafkaClusterValidationManager _srcKafkaValidationManager;
private final AutoTopicWhitelistingManager _autoTopicWhitelistingManager;
private final ClusterInfoBackupManager _clusterInfoBackupManager;
private final Map<String, KafkaBrokerTopicObserver> _kafkaBrokerTopicObserverMap =
new HashMap<String, KafkaBrokerTopicObserver>();
public ControllerStarter(ControllerConf conf) {
LOGGER.info("Trying to init ControllerStarter with config: {}", conf);
_config = conf;
HelixKafkaMirrorMakerMetricsReporter.init(conf);
_component = new Component();
_controllerRestApp = new ControllerRestApplication(null);
_helixMirrorMakerManager = new HelixMirrorMakerManager(_config);
_validationManager = new ValidationManager(_helixMirrorMakerManager);
_srcKafkaValidationManager = getSourceKafkaClusterValidationManager();
_autoTopicWhitelistingManager = getAutoTopicWhitelistingManager();
if (_config.getBackUpToGit()) {
_clusterInfoBackupManager = new ClusterInfoBackupManager(_helixMirrorMakerManager,
new GitBackUpHandler(conf.getRemoteBackupRepo(), conf.getLocalGitRepoPath()), _config);
} else {
_clusterInfoBackupManager = new ClusterInfoBackupManager(_helixMirrorMakerManager,
new FileBackUpHandler(conf.getLocalBackupFilePath()), _config);
}
}
private SourceKafkaClusterValidationManager getSourceKafkaClusterValidationManager() {
if (_config.getEnableSrcKafkaValidation()) {
if (!_kafkaBrokerTopicObserverMap.containsKey(SRC_KAFKA_CLUSTER)) {
_kafkaBrokerTopicObserverMap.put(SRC_KAFKA_CLUSTER,
new KafkaBrokerTopicObserver(SRC_KAFKA_CLUSTER, _config.getSrcKafkaZkPath()));
}
return new SourceKafkaClusterValidationManager(
_kafkaBrokerTopicObserverMap.get(SRC_KAFKA_CLUSTER),
_helixMirrorMakerManager, _config.getEnableAutoTopicExpansion());
} else {
LOGGER.info("Not init SourceKafkaClusterValidationManager!");
return null;
}
}
private AutoTopicWhitelistingManager getAutoTopicWhitelistingManager() {
if (_config.getEnableAutoWhitelist()) {
if (!_kafkaBrokerTopicObserverMap.containsKey(SRC_KAFKA_CLUSTER)) {
_kafkaBrokerTopicObserverMap.put(SRC_KAFKA_CLUSTER,
new KafkaBrokerTopicObserver(SRC_KAFKA_CLUSTER, _config.getSrcKafkaZkPath()));
}
if (!_kafkaBrokerTopicObserverMap.containsKey(DEST_KAFKA_CLUSTER)) {
_kafkaBrokerTopicObserverMap.put(DEST_KAFKA_CLUSTER,
new KafkaBrokerTopicObserver(DEST_KAFKA_CLUSTER, _config.getDestKafkaZkPath()));
}
String patternToExcludeTopics = _config.getPatternToExcludeTopics();
if (patternToExcludeTopics != null && patternToExcludeTopics.trim().length() > 0) {
patternToExcludeTopics = patternToExcludeTopics.trim();
} else {
patternToExcludeTopics = "";
}
LOGGER.info("Pattern to exclude topics is " + patternToExcludeTopics);
return new AutoTopicWhitelistingManager(_kafkaBrokerTopicObserverMap.get(SRC_KAFKA_CLUSTER),
_kafkaBrokerTopicObserverMap.get(DEST_KAFKA_CLUSTER), _helixMirrorMakerManager,
patternToExcludeTopics, _config.getRefreshTimeInSeconds(), _config.getInitWaitTimeInSeconds());
} else {
LOGGER.info("Not init AutoTopicWhitelistingManager!");
return null;
}
}
public HelixMirrorMakerManager getHelixResourceManager() {
return _helixMirrorMakerManager;
}
public void start() throws Exception {
_component.getServers().add(Protocol.HTTP, Integer.parseInt(_config.getControllerPort()));
_component.getClients().add(Protocol.FILE);
_component.getClients().add(Protocol.JAR);
_component.getClients().add(Protocol.WAR);
final Context applicationContext = _component.getContext().createChildContext();
LOGGER.info("injecting conf and resource manager to the api context");
applicationContext.getAttributes().put(ControllerConf.class.toString(), _config);
applicationContext.getAttributes().put(HelixMirrorMakerManager.class.toString(),
_helixMirrorMakerManager);
applicationContext.getAttributes().put(ValidationManager.class.toString(), _validationManager);
if (_srcKafkaValidationManager != null) {
applicationContext.getAttributes().put(SourceKafkaClusterValidationManager.class.toString(),
_srcKafkaValidationManager);
applicationContext.getAttributes().put(KafkaBrokerTopicObserver.class.toString(),
_kafkaBrokerTopicObserverMap.get(SRC_KAFKA_CLUSTER));
}
if (_autoTopicWhitelistingManager != null) {
applicationContext.getAttributes().put(AutoTopicWhitelistingManager.class.toString(),
_autoTopicWhitelistingManager);
}
_controllerRestApp.setContext(applicationContext);
_component.getDefaultHost().attach(_controllerRestApp);
try {
LOGGER.info("starting helix mirror maker manager");
_helixMirrorMakerManager.start();
_validationManager.start();
if (_autoTopicWhitelistingManager != null) {
_autoTopicWhitelistingManager.start();
}
if (_srcKafkaValidationManager != null) {
_srcKafkaValidationManager.start();
}
if (_clusterInfoBackupManager != null) {
_clusterInfoBackupManager.start();
}
LOGGER.info("starting api component");
_component.start();
} catch (final Exception e) {
LOGGER.error("Caught exception while starting controller", e);
throw e;
}
}
public void stop() {
try {
LOGGER.info("stopping broker topic observers");
for (String key : _kafkaBrokerTopicObserverMap.keySet()) {
try {
KafkaBrokerTopicObserver observer = _kafkaBrokerTopicObserverMap.get(key);
observer.stop();
} catch (Exception e) {
LOGGER.error("Failed to stop KafkaBrokerTopicObserver: {}!", key);
}
}
LOGGER.info("stopping api component");
_component.stop();
LOGGER.info("stopping resource manager");
_helixMirrorMakerManager.stop();
} catch (final Exception e) {
LOGGER.error("Caught exception", e);
}
}
public static ControllerConf getDefaultConf() {
final ControllerConf conf = new ControllerConf();
conf.setControllerPort("9000");
conf.setZkStr("localhost:2181");
conf.setHelixClusterName("testMirrorMaker");
conf.setBackUpToGit("false");
conf.setAutoRebalanceDelayInSeconds("120");
conf.setLocalBackupFilePath("/var/log/kafka-mirror-maker-controller");
return conf;
}
public static ControllerConf getExampleConf() {
final ControllerConf conf = new ControllerConf();
conf.setControllerPort("9000");
conf.setZkStr("localhost:2181");
conf.setHelixClusterName("testMirrorMaker");
conf.setBackUpToGit("false");
conf.setAutoRebalanceDelayInSeconds("120");
conf.setLocalBackupFilePath("/var/log/kafka-mirror-maker-controller");
conf.setEnableAutoWhitelist("true");
conf.setEnableAutoTopicExpansion("true");
conf.setSrcKafkaZkPath("localhost:2181/cluster1");
conf.setDestKafkaZkPath("localhost:2181/cluster2");
conf.setInitWaitTimeInSeconds("10");
conf.setRefreshTimeInSeconds("20");
return conf;
}
public static ControllerStarter init(CommandLine cmd) {
ControllerConf conf = null;
if (cmd.hasOption("example1")) {
conf = getDefaultConf();
} else if (cmd.hasOption("example2")) {
conf = getExampleConf();
} else {
try {
conf = ControllerConf.getControllerConf(cmd);
} catch (Exception e) {
throw new RuntimeException("Not valid controller configurations!", e);
}
}
final ControllerStarter starter = new ControllerStarter(conf);
return starter;
}
public static void main(String[] args) throws Exception {
CommandLineParser parser = new DefaultParser();
CommandLine cmd = null;
cmd = parser.parse(ControllerConf.constructControllerOptions(), args);
if (cmd.getOptions().length == 0 || cmd.hasOption("help")) {
HelpFormatter f = new HelpFormatter();
f.printHelp("OptionsTip", ControllerConf.constructControllerOptions());
System.exit(0);
}
final ControllerStarter controllerStarter = ControllerStarter.init(cmd);
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
try {
controllerStarter.stop();
} catch (Exception e) {
LOGGER.error("Caught error during shutdown! ", e);
}
}
});
try {
controllerStarter.start();
} catch (Exception e) {
LOGGER.error("Cannot start Helix Mirror Maker Controller: ", e);
}
}
}