forked from apache/pulsar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CmdSources.java
755 lines (668 loc) · 32.9 KB
/
CmdSources.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
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.pulsar.admin.cli;
import static org.apache.commons.lang3.StringUtils.isBlank;
import static org.apache.commons.lang3.StringUtils.isNotBlank;
import static org.apache.pulsar.common.naming.TopicName.DEFAULT_NAMESPACE;
import static org.apache.pulsar.common.naming.TopicName.PUBLIC_TENANT;
import com.beust.jcommander.Parameter;
import com.beust.jcommander.ParameterException;
import com.beust.jcommander.Parameters;
import com.beust.jcommander.converters.StringConverter;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.text.WordUtils;
import org.apache.pulsar.admin.cli.utils.CmdUtils;
import org.apache.pulsar.client.admin.PulsarAdmin;
import org.apache.pulsar.client.admin.PulsarAdminException;
import org.apache.pulsar.common.functions.FunctionConfig;
import org.apache.pulsar.common.functions.ProducerConfig;
import org.apache.pulsar.common.functions.Resources;
import org.apache.pulsar.common.functions.UpdateOptionsImpl;
import org.apache.pulsar.common.functions.Utils;
import org.apache.pulsar.common.io.BatchSourceConfig;
import org.apache.pulsar.common.io.ConnectorDefinition;
import org.apache.pulsar.common.io.SourceConfig;
import org.apache.pulsar.common.util.ObjectMapperFactory;
@Getter
@Parameters(commandDescription = "Interface for managing Pulsar IO Sources (ingress data into Pulsar)")
@Slf4j
public class CmdSources extends CmdBase {
private final CreateSource createSource;
private final DeleteSource deleteSource;
private final GetSource getSource;
private final GetSourceStatus getSourceStatus;
private final ListSources listSources;
private final UpdateSource updateSource;
private final RestartSource restartSource;
private final StopSource stopSource;
private final StartSource startSource;
private final LocalSourceRunner localSourceRunner;
public CmdSources(Supplier<PulsarAdmin> admin) {
super("sources", admin);
createSource = new CreateSource();
updateSource = new UpdateSource();
deleteSource = new DeleteSource();
listSources = new ListSources();
getSource = new GetSource();
getSourceStatus = new GetSourceStatus();
restartSource = new RestartSource();
stopSource = new StopSource();
startSource = new StartSource();
localSourceRunner = new LocalSourceRunner();
jcommander.addCommand("create", createSource);
jcommander.addCommand("update", updateSource);
jcommander.addCommand("delete", deleteSource);
jcommander.addCommand("get", getSource);
// TODO depecreate getstatus
jcommander.addCommand("status", getSourceStatus, "getstatus");
jcommander.addCommand("list", listSources);
jcommander.addCommand("stop", stopSource);
jcommander.addCommand("start", startSource);
jcommander.addCommand("restart", restartSource);
jcommander.addCommand("localrun", localSourceRunner);
jcommander.addCommand("available-sources", new ListBuiltInSources());
jcommander.addCommand("reload", new ReloadBuiltInSources());
}
/**
* Base command.
*/
@Getter
abstract class BaseCommand extends CliCommand {
@Override
void run() throws Exception {
try {
processArguments();
} catch (Exception e) {
String chosenCommand = jcommander.getParsedCommand();
getUsageFormatter().usage(chosenCommand);
throw e;
}
runCmd();
}
void processArguments() throws Exception {
}
abstract void runCmd() throws Exception;
}
@Parameters(commandDescription = "Run a Pulsar IO source connector locally "
+ "(rather than deploying it to the Pulsar cluster)")
protected class LocalSourceRunner extends CreateSource {
@Parameter(names = "--state-storage-service-url",
description = "The URL for the state storage service (the default is Apache BookKeeper)")
protected String stateStorageServiceUrl;
@Parameter(names = "--brokerServiceUrl", description = "The URL for the Pulsar broker", hidden = true)
protected String deprecatedBrokerServiceUrl;
@Parameter(names = "--broker-service-url", description = "The URL for the Pulsar broker")
protected String brokerServiceUrl;
@Parameter(names = "--clientAuthPlugin",
description = "Client authentication plugin using which function-process can connect to broker",
hidden = true)
protected String deprecatedClientAuthPlugin;
@Parameter(names = "--client-auth-plugin",
description = "Client authentication plugin using which function-process can connect to broker")
protected String clientAuthPlugin;
@Parameter(names = "--clientAuthParams", description = "Client authentication param", hidden = true)
protected String deprecatedClientAuthParams;
@Parameter(names = "--client-auth-params", description = "Client authentication param")
protected String clientAuthParams;
@Parameter(names = "--use_tls", description = "Use tls connection", hidden = true)
protected Boolean deprecatedUseTls;
@Parameter(names = "--use-tls", description = "Use tls connection")
protected boolean useTls;
@Parameter(names = "--tls_allow_insecure", description = "Allow insecure tls connection", hidden = true)
protected Boolean deprecatedTlsAllowInsecureConnection;
@Parameter(names = "--tls-allow-insecure", description = "Allow insecure tls connection")
protected boolean tlsAllowInsecureConnection;
@Parameter(names = "--hostname_verification_enabled",
description = "Enable hostname verification", hidden = true)
protected Boolean deprecatedTlsHostNameVerificationEnabled;
@Parameter(names = "--hostname-verification-enabled", description = "Enable hostname verification")
protected boolean tlsHostNameVerificationEnabled;
@Parameter(names = "--tls_trust_cert_path", description = "tls trust cert file path", hidden = true)
protected String deprecatedTlsTrustCertFilePath;
@Parameter(names = "--tls-trust-cert-path", description = "tls trust cert file path")
protected String tlsTrustCertFilePath;
@Parameter(names = "--secrets-provider-classname", description = "Whats the classname for secrets provider")
protected String secretsProviderClassName;
@Parameter(names = "--secrets-provider-config",
description = "Config that needs to be passed to secrets provider")
protected String secretsProviderConfig;
@Parameter(names = "--metrics-port-start", description = "The starting port range for metrics server")
protected String metricsPortStart;
private void mergeArgs() {
if (isBlank(brokerServiceUrl) && !isBlank(deprecatedBrokerServiceUrl)) {
brokerServiceUrl = deprecatedBrokerServiceUrl;
}
if (isBlank(clientAuthPlugin) && !isBlank(deprecatedClientAuthPlugin)) {
clientAuthPlugin = deprecatedClientAuthPlugin;
}
if (isBlank(clientAuthParams) && !isBlank(deprecatedClientAuthParams)) {
clientAuthParams = deprecatedClientAuthParams;
}
if (!useTls && deprecatedUseTls != null) {
useTls = deprecatedUseTls;
}
if (!tlsAllowInsecureConnection && deprecatedTlsAllowInsecureConnection != null) {
tlsAllowInsecureConnection = deprecatedTlsAllowInsecureConnection;
}
if (!tlsHostNameVerificationEnabled && deprecatedTlsHostNameVerificationEnabled != null) {
tlsHostNameVerificationEnabled = deprecatedTlsHostNameVerificationEnabled;
}
if (isBlank(tlsTrustCertFilePath) && !isBlank(deprecatedTlsTrustCertFilePath)) {
tlsTrustCertFilePath = deprecatedTlsTrustCertFilePath;
}
}
@Override
public void runCmd() throws Exception {
// merge deprecated args with new args
mergeArgs();
List<String> localRunArgs = new LinkedList<>();
localRunArgs.add(System.getenv("PULSAR_HOME") + "/bin/function-localrunner");
localRunArgs.add("--sourceConfig");
localRunArgs.add(new Gson().toJson(sourceConfig));
for (Field field : this.getClass().getDeclaredFields()) {
if (field.getName().startsWith("DEPRECATED")) {
continue;
}
if (field.getName().contains("$")) {
continue;
}
Object value = field.get(this);
if (value != null) {
localRunArgs.add("--" + field.getName());
localRunArgs.add(value.toString());
}
}
ProcessBuilder processBuilder = new ProcessBuilder(localRunArgs).inheritIO();
Process process = processBuilder.start();
process.waitFor();
}
@Override
protected String validateSourceType(String sourceType) {
return sourceType;
}
}
@Parameters(commandDescription = "Submit a Pulsar IO source connector to run in a Pulsar cluster")
protected class CreateSource extends SourceDetailsCommand {
@Override
void runCmd() throws Exception {
if (Utils.isFunctionPackageUrlSupported(this.sourceConfig.getArchive())) {
getAdmin().sources().createSourceWithUrl(sourceConfig, sourceConfig.getArchive());
} else {
getAdmin().sources().createSource(sourceConfig, sourceConfig.getArchive());
}
print("Created successfully");
}
}
@Parameters(commandDescription = "Update a Pulsar IO source connector")
protected class UpdateSource extends SourceDetailsCommand {
@Parameter(names = "--update-auth-data", description = "Whether or not to update the auth data")
protected boolean updateAuthData;
@Override
void runCmd() throws Exception {
UpdateOptionsImpl updateOptions = new UpdateOptionsImpl();
updateOptions.setUpdateAuthData(updateAuthData);
if (Utils.isFunctionPackageUrlSupported(sourceConfig.getArchive())) {
getAdmin().sources().updateSourceWithUrl(sourceConfig, sourceConfig.getArchive(), updateOptions);
} else {
getAdmin().sources().updateSource(sourceConfig, sourceConfig.getArchive(), updateOptions);
}
print("Updated successfully");
}
protected void validateSourceConfigs(SourceConfig sourceConfig) {
if (sourceConfig.getTenant() == null) {
sourceConfig.setTenant(PUBLIC_TENANT);
}
if (sourceConfig.getNamespace() == null) {
sourceConfig.setNamespace(DEFAULT_NAMESPACE);
}
}
}
abstract class SourceDetailsCommand extends BaseCommand {
@Parameter(names = "--tenant", description = "The source's tenant")
protected String tenant;
@Parameter(names = "--namespace", description = "The source's namespace")
protected String namespace;
@Parameter(names = "--name", description = "The source's name")
protected String name;
@Parameter(names = { "-t", "--source-type" }, description = "The source's connector provider")
protected String sourceType;
@Parameter(names = "--processingGuarantees",
description = "The processing guarantees (aka delivery semantics) applied to the Source", hidden = true)
protected FunctionConfig.ProcessingGuarantees deprecatedProcessingGuarantees;
@Parameter(names = "--processing-guarantees",
description = "The processing guarantees (as known as delivery semantics) applied to the source."
+ " A source connector receives messages from external system and writes messages to a Pulsar"
+ " topic. The '--processing-guarantees' is used to ensure the processing guarantees for writing"
+ " messages to the Pulsar topic. The available values are `ATLEAST_ONCE`, `ATMOST_ONCE`,"
+ " `EFFECTIVELY_ONCE`. If it is not specified, `ATLEAST_ONCE` delivery guarantee is used.")
protected FunctionConfig.ProcessingGuarantees processingGuarantees;
@Parameter(names = { "-o", "--destinationTopicName" },
description = "The Pulsar topic to which data is sent", hidden = true)
protected String deprecatedDestinationTopicName;
@Parameter(names = "--destination-topic-name", description = "The Pulsar topic to which data is sent")
protected String destinationTopicName;
@Parameter(names = "--producer-config", description = "The custom producer configuration (as a JSON string)")
protected String producerConfig;
@Parameter(names = "--batch-builder", description = "BatchBuilder provides two types of "
+ "batch construction methods, DEFAULT and KEY_BASED. The default value is: DEFAULT")
protected String batchBuilder;
@Parameter(names = "--deserializationClassName",
description = "The SerDe classname for the source", hidden = true)
protected String deprecatedDeserializationClassName;
@Parameter(names = "--deserialization-classname", description = "The SerDe classname for the source")
protected String deserializationClassName;
@Parameter(names = { "-st",
"--schema-type" }, description = "The schema type (either a builtin schema like 'avro', 'json', etc.."
+ " or custom Schema class name to be used to encode messages emitted from the source")
protected String schemaType;
@Parameter(names = "--parallelism",
description = "The source's parallelism factor (i.e. the number of source instances to run)")
protected Integer parallelism;
@Parameter(names = { "-a", "--archive" },
description = "The path to the NAR archive for the Source. It also supports url-path "
+ "[http/https/file (file protocol assumes that file already exists on worker host)] "
+ "from which worker can download the package.", listConverter = StringConverter.class)
protected String archive;
@Parameter(names = "--className",
description = "The source's class name if archive is file-url-path (file://)", hidden = true)
protected String deprecatedClassName;
@Parameter(names = "--classname", description = "The source's class name if archive is file-url-path (file://)")
protected String className;
@Parameter(names = "--sourceConfigFile", description = "The path to a YAML config file specifying the "
+ "source's configuration", hidden = true)
protected String deprecatedSourceConfigFile;
@Parameter(names = "--source-config-file", description = "The path to a YAML config file specifying the "
+ "source's configuration")
protected String sourceConfigFile;
@Parameter(names = "--cpu", description = "The CPU (in cores) that needs to be allocated "
+ "per source instance (applicable only to Docker runtime)")
protected Double cpu;
@Parameter(names = "--ram", description = "The RAM (in bytes) that need to be allocated "
+ "per source instance (applicable only to the process and Docker runtimes)")
protected Long ram;
@Parameter(names = "--disk", description = "The disk (in bytes) that need to be allocated "
+ "per source instance (applicable only to Docker runtime)")
protected Long disk;
@Parameter(names = "--sourceConfig", description = "Source config key/values", hidden = true)
protected String deprecatedSourceConfigString;
@Parameter(names = "--source-config", description = "Source config key/values")
protected String sourceConfigString;
@Parameter(names = "--batch-source-config", description = "Batch source config key/values")
protected String batchSourceConfigString;
@Parameter(names = "--custom-runtime-options", description = "A string that encodes options to "
+ "customize the runtime, see docs for configured runtime for details")
protected String customRuntimeOptions;
@Parameter(names = "--secrets", description = "The map of secretName to an object that encapsulates "
+ "how the secret is fetched by the underlying secrets provider")
protected String secretsString;
@Parameter(names = "--log-topic", description = "The topic to which the logs of a Pulsar Sink are produced")
protected String logTopic;
protected SourceConfig sourceConfig;
private void mergeArgs() {
if (processingGuarantees == null && deprecatedProcessingGuarantees != null) {
processingGuarantees = deprecatedProcessingGuarantees;
}
if (isBlank(destinationTopicName) && !isBlank(deprecatedDestinationTopicName)) {
destinationTopicName = deprecatedDestinationTopicName;
}
if (isBlank(deserializationClassName) && !isBlank(deprecatedDeserializationClassName)) {
deserializationClassName = deprecatedDeserializationClassName;
}
if (isBlank(className) && !isBlank(deprecatedClassName)) {
className = deprecatedClassName;
}
if (isBlank(sourceConfigFile) && !isBlank(deprecatedSourceConfigFile)) {
sourceConfigFile = deprecatedSourceConfigFile;
}
if (isBlank(sourceConfigString) && !isBlank(deprecatedSourceConfigString)) {
sourceConfigString = deprecatedSourceConfigString;
}
}
@Override
void processArguments() throws Exception {
super.processArguments();
// merge deprecated args with new args
mergeArgs();
if (null != sourceConfigFile) {
this.sourceConfig = CmdUtils.loadConfig(sourceConfigFile, SourceConfig.class);
} else {
this.sourceConfig = new SourceConfig();
}
if (null != tenant) {
sourceConfig.setTenant(tenant);
}
if (null != namespace) {
sourceConfig.setNamespace(namespace);
}
if (null != name) {
sourceConfig.setName(name);
}
if (null != className) {
this.sourceConfig.setClassName(className);
}
if (null != destinationTopicName) {
sourceConfig.setTopicName(destinationTopicName);
}
if (null != producerConfig) {
Type type = new TypeToken<ProducerConfig>() {}.getType();
sourceConfig.setProducerConfig(new Gson().fromJson(producerConfig, type));
}
if (null != deserializationClassName) {
sourceConfig.setSerdeClassName(deserializationClassName);
}
if (null != schemaType) {
sourceConfig.setSchemaType(schemaType);
}
if (null != batchBuilder) {
sourceConfig.setBatchBuilder(batchBuilder);
}
if (null != processingGuarantees) {
sourceConfig.setProcessingGuarantees(processingGuarantees);
}
if (parallelism != null) {
sourceConfig.setParallelism(parallelism);
}
if (archive != null && (sourceType != null || sourceConfig.getSourceType() != null)) {
throw new ParameterException("Cannot specify both archive and source-type");
}
if (archive != null) {
sourceConfig.setArchive(archive);
}
if (sourceType != null) {
sourceConfig.setArchive(validateSourceType(sourceType));
} else if (sourceConfig.getSourceType() != null) {
sourceConfig.setArchive(validateSourceType(sourceConfig.getSourceType()));
}
Resources resources = sourceConfig.getResources();
if (cpu != null) {
if (resources == null) {
resources = new Resources();
}
resources.setCpu(cpu);
}
if (ram != null) {
if (resources == null) {
resources = new Resources();
}
resources.setRam(ram);
}
if (disk != null) {
if (resources == null) {
resources = new Resources();
}
resources.setDisk(disk);
}
if (resources != null) {
sourceConfig.setResources(resources);
}
try {
if (null != sourceConfigString) {
sourceConfig.setConfigs(parseConfigs(sourceConfigString));
}
} catch (Exception ex) {
throw new ParameterException("Cannot parse source-config", ex);
}
if (null != batchSourceConfigString) {
sourceConfig.setBatchSourceConfig(parseBatchSourceConfigs(batchSourceConfigString));
}
if (customRuntimeOptions != null) {
sourceConfig.setCustomRuntimeOptions(customRuntimeOptions);
}
if (secretsString != null) {
Type type = new TypeToken<Map<String, Object>>() {}.getType();
Map<String, Object> secretsMap = new Gson().fromJson(secretsString, type);
if (secretsMap == null) {
secretsMap = Collections.emptyMap();
}
sourceConfig.setSecrets(secretsMap);
}
if (null != logTopic) {
sourceConfig.setLogTopic(logTopic);
}
// check if source configs are valid
validateSourceConfigs(sourceConfig);
}
protected Map<String, Object> parseConfigs(String str) throws JsonProcessingException {
ObjectMapper mapper = ObjectMapperFactory.getMapper().getObjectMapper();
TypeReference<HashMap<String, Object>> typeRef = new TypeReference<HashMap<String, Object>>() {};
return mapper.readValue(str, typeRef);
}
protected BatchSourceConfig parseBatchSourceConfigs(String str) {
return new Gson().fromJson(str, BatchSourceConfig.class);
}
protected void validateSourceConfigs(SourceConfig sourceConfig) {
if (isBlank(sourceConfig.getArchive())) {
throw new ParameterException("Source archive not specified");
}
org.apache.pulsar.common.functions.Utils.inferMissingArguments(sourceConfig);
if (!Utils.isFunctionPackageUrlSupported(sourceConfig.getArchive())
&& !sourceConfig.getArchive().startsWith(Utils.BUILTIN)) {
if (!new File(sourceConfig.getArchive()).exists()) {
throw new IllegalArgumentException(String.format("Source Archive %s does not exist",
sourceConfig.getArchive()));
}
}
if (isBlank(sourceConfig.getName())) {
throw new IllegalArgumentException("Source name not specified");
}
if (sourceConfig.getBatchSourceConfig() != null) {
validateBatchSourceConfigs(sourceConfig.getBatchSourceConfig());
}
}
protected void validateBatchSourceConfigs(BatchSourceConfig batchSourceConfig) {
if (isBlank(batchSourceConfig.getDiscoveryTriggererClassName())) {
throw new IllegalArgumentException("Discovery Triggerer not specified");
}
}
protected String validateSourceType(String sourceType) throws IOException {
Set<String> availableSources;
try {
availableSources = getAdmin().sources().getBuiltInSources().stream()
.map(ConnectorDefinition::getName).collect(Collectors.toSet());
} catch (PulsarAdminException e) {
throw new IOException(e);
}
if (!availableSources.contains(sourceType)) {
throw new ParameterException(
"Invalid source type '" + sourceType + "' -- Available sources are: " + availableSources);
}
// Source type is a valid built-in connector type
return "builtin://" + sourceType;
}
}
/**
* Function level command.
*/
@Getter
abstract class SourceCommand extends BaseCommand {
@Parameter(names = "--tenant", description = "The source's tenant")
protected String tenant;
@Parameter(names = "--namespace", description = "The source's namespace")
protected String namespace;
@Parameter(names = "--name", description = "The source's name")
protected String sourceName;
@Override
void processArguments() throws Exception {
super.processArguments();
if (tenant == null) {
tenant = PUBLIC_TENANT;
}
if (namespace == null) {
namespace = DEFAULT_NAMESPACE;
}
if (null == sourceName) {
throw new RuntimeException(
"You must specify a name for the source");
}
}
}
@Parameters(commandDescription = "Stops a Pulsar IO source connector")
protected class DeleteSource extends SourceCommand {
@Override
void runCmd() throws Exception {
getAdmin().sources().deleteSource(tenant, namespace, sourceName);
print("Delete source successfully");
}
}
@Parameters(commandDescription = "Gets the information about a Pulsar IO source connector")
protected class GetSource extends SourceCommand {
@Override
void runCmd() throws Exception {
SourceConfig sourceConfig = getAdmin().sources().getSource(tenant, namespace, sourceName);
Gson gson = new GsonBuilder().setPrettyPrinting().create();
System.out.println(gson.toJson(sourceConfig));
}
}
/**
* List Sources command.
*/
@Parameters(commandDescription = "List all running Pulsar IO source connectors")
protected class ListSources extends BaseCommand {
@Parameter(names = "--tenant", description = "The source's tenant")
protected String tenant;
@Parameter(names = "--namespace", description = "The source's namespace")
protected String namespace;
@Override
public void processArguments() {
if (tenant == null) {
tenant = PUBLIC_TENANT;
}
if (namespace == null) {
namespace = DEFAULT_NAMESPACE;
}
}
@Override
void runCmd() throws Exception {
List<String> sources = getAdmin().sources().listSources(tenant, namespace);
Gson gson = new GsonBuilder().setPrettyPrinting().create();
System.out.println(gson.toJson(sources));
}
}
@Parameters(commandDescription = "Check the current status of a Pulsar Source")
class GetSourceStatus extends SourceCommand {
@Parameter(names = "--instance-id",
description = "The source instanceId (Get-status of all instances if instance-id is not provided")
protected String instanceId;
@Override
void runCmd() throws Exception {
if (isBlank(instanceId)) {
print(getAdmin().sources().getSourceStatus(tenant, namespace, sourceName));
} else {
print(getAdmin().sources()
.getSourceStatus(tenant, namespace, sourceName, Integer.parseInt(instanceId)));
}
}
}
@Parameters(commandDescription = "Restart source instance")
class RestartSource extends SourceCommand {
@Parameter(names = "--instance-id",
description = "The source instanceId (restart all instances if instance-id is not provided")
protected String instanceId;
@Override
void runCmd() throws Exception {
if (isNotBlank(instanceId)) {
try {
getAdmin().sources().restartSource(tenant, namespace, sourceName, Integer.parseInt(instanceId));
} catch (NumberFormatException e) {
System.err.println("instance-id must be a number");
}
} else {
getAdmin().sources().restartSource(tenant, namespace, sourceName);
}
System.out.println("Restarted successfully");
}
}
@Parameters(commandDescription = "Stop source instance")
class StopSource extends SourceCommand {
@Parameter(names = "--instance-id",
description = "The source instanceId (stop all instances if instance-id is not provided")
protected String instanceId;
@Override
void runCmd() throws Exception {
if (isNotBlank(instanceId)) {
try {
getAdmin().sources().stopSource(tenant, namespace, sourceName, Integer.parseInt(instanceId));
} catch (NumberFormatException e) {
System.err.println("instance-id must be a number");
}
} else {
getAdmin().sources().stopSource(tenant, namespace, sourceName);
}
System.out.println("Stopped successfully");
}
}
@Parameters(commandDescription = "Start source instance")
class StartSource extends SourceCommand {
@Parameter(names = "--instance-id",
description = "The source instanceId (start all instances if instance-id is not provided")
protected String instanceId;
@Override
void runCmd() throws Exception {
if (isNotBlank(instanceId)) {
try {
getAdmin().sources().startSource(tenant, namespace, sourceName, Integer.parseInt(instanceId));
} catch (NumberFormatException e) {
System.err.println("instance-id must be a number");
}
} else {
getAdmin().sources().startSource(tenant, namespace, sourceName);
}
System.out.println("Started successfully");
}
}
@Parameters(commandDescription = "Get the list of Pulsar IO connector sources supported by Pulsar cluster")
public class ListBuiltInSources extends BaseCommand {
@Override
void runCmd() throws Exception {
getAdmin().sources().getBuiltInSources().stream().filter(x -> !StringUtils.isEmpty(x.getSourceClass()))
.forEach(connector -> {
System.out.println(connector.getName());
System.out.println(WordUtils.wrap(connector.getDescription(), 80));
System.out.println("----------------------------------------");
});
}
}
@Parameters(commandDescription = "Reload the available built-in connectors")
public class ReloadBuiltInSources extends BaseCommand {
@Override
void runCmd() throws Exception {
getAdmin().sources().reloadBuiltInSources();
}
}
}