This repository has been archived by the owner on Mar 27, 2021. It is now read-only.
/
HeroicCore.java
1141 lines (965 loc) · 40.2 KB
/
HeroicCore.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
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2015 Spotify AB.
*
* 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 com.spotify.heroic;
import static com.google.common.base.Preconditions.checkNotNull;
import static java.util.Optional.empty;
import static java.util.Optional.of;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Stopwatch;
import com.google.common.collect.ImmutableList;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.lightstep.opencensus.exporter.LightStepExporterHandler;
import com.lightstep.tracer.jre.JRETracer;
import com.lightstep.tracer.shared.Options;
import com.spotify.heroic.analytics.AnalyticsComponent;
import com.spotify.heroic.cache.CacheComponent;
import com.spotify.heroic.cluster.CoreClusterComponent;
import com.spotify.heroic.cluster.DaggerCoreClusterComponent;
import com.spotify.heroic.common.Duration;
import com.spotify.heroic.common.Optionals;
import com.spotify.heroic.common.TypeNameMixin;
import com.spotify.heroic.consumer.ConsumersComponent;
import com.spotify.heroic.consumer.CoreConsumersModule;
import com.spotify.heroic.consumer.DaggerCoreConsumersComponent;
import com.spotify.heroic.dagger.CoreComponent;
import com.spotify.heroic.dagger.CoreEarlyComponent;
import com.spotify.heroic.dagger.CoreLoadingComponent;
import com.spotify.heroic.dagger.CorePrimaryComponent;
import com.spotify.heroic.dagger.DaggerCoreComponent;
import com.spotify.heroic.dagger.DaggerCoreEarlyComponent;
import com.spotify.heroic.dagger.DaggerCoreLoadingComponent;
import com.spotify.heroic.dagger.DaggerCorePrimaryComponent;
import com.spotify.heroic.dagger.DaggerStartupPingerComponent;
import com.spotify.heroic.dagger.EarlyModule;
import com.spotify.heroic.dagger.LoadingComponent;
import com.spotify.heroic.dagger.LoadingModule;
import com.spotify.heroic.dagger.PrimaryModule;
import com.spotify.heroic.dagger.StartupPingerComponent;
import com.spotify.heroic.dagger.StartupPingerModule;
import com.spotify.heroic.generator.GeneratorComponent;
import com.spotify.heroic.http.DaggerHttpServerComponent;
import com.spotify.heroic.http.HttpServer;
import com.spotify.heroic.http.HttpServerComponent;
import com.spotify.heroic.http.HttpServerModule;
import com.spotify.heroic.ingestion.IngestionComponent;
import com.spotify.heroic.jetty.JettyConnectionFactory;
import com.spotify.heroic.lifecycle.CoreLifeCycleRegistry;
import com.spotify.heroic.lifecycle.LifeCycle;
import com.spotify.heroic.lifecycle.LifeCycleHook;
import com.spotify.heroic.lifecycle.LifeCycleNamedHook;
import com.spotify.heroic.metadata.CoreMetadataComponent;
import com.spotify.heroic.metadata.DaggerCoreMetadataComponent;
import com.spotify.heroic.metric.CoreMetricComponent;
import com.spotify.heroic.metric.DaggerCoreMetricComponent;
import com.spotify.heroic.querylogging.QueryLoggingComponent;
import com.spotify.heroic.shell.DaggerShellServerComponent;
import com.spotify.heroic.shell.ShellServerComponent;
import com.spotify.heroic.shell.ShellServerModule;
import com.spotify.heroic.statistics.HeroicReporter;
import com.spotify.heroic.statistics.StatisticsComponent;
import com.spotify.heroic.suggest.CoreSuggestComponent;
import com.spotify.heroic.suggest.DaggerCoreSuggestComponent;
import com.spotify.heroic.tracing.TracingConfig;
import com.spotify.heroic.usagetracking.UsageTrackingComponent;
import com.spotify.tracing.SquashingTraceExporter;
import eu.toolchain.async.AsyncFramework;
import eu.toolchain.async.AsyncFuture;
import eu.toolchain.async.ResolvableFuture;
import io.opencensus.contrib.zpages.ZPageHandlers;
import io.opencensus.trace.Tracing;
import io.opencensus.trace.config.TraceConfig;
import io.opencensus.trace.samplers.Samplers;
import io.sentry.Sentry;
import java.io.IOException;
import java.io.InputStream;
import java.lang.Thread.UncaughtExceptionHandler;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function;
import java.util.function.Supplier;
import org.apache.commons.lang3.tuple.Pair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Configure and bootstrap a Heroic application.
* <p>
* All public methods are thread-safe.
* <p>
* All fields are non-null.
*
* @author udoprog
*/
public class HeroicCore implements HeroicConfiguration {
private static final Logger log = LoggerFactory.getLogger(HeroicCore.class);
private static final String DEFAULT_HOST = "0.0.0.0";
private static final int DEFAULT_PORT = 8080;
private static final boolean DEFAULT_SETUP_SERVICE = true;
private static final boolean DEFAULT_ONESHOT = false;
private static final boolean DEFAULT_DISABLE_BACKENDS = false;
private static final boolean DEFAULT_SETUP_SHELL_SERVER = true;
private static final UncaughtExceptionHandler uncaughtExceptionHandler =
(Thread t, Throwable e) -> {
//noinspection finally
try {
System.out.print("The Heroic is dying heroically now!\n");
System.out.flush();
System.err.println(
String.format("Uncaught exception caught in thread %s, exiting...", t));
e.printStackTrace(System.err);
} finally {
System.exit(1);
}
};
/**
* Built-in modules that should always be loaded.
*/
// @formatter:off
private static final HeroicModule[] BUILTIN_MODULES = new HeroicModule[] {
new com.spotify.heroic.aggregation.Module(),
new com.spotify.heroic.http.Module(),
new com.spotify.heroic.jetty.Module(),
new com.spotify.heroic.ws.Module(),
new com.spotify.heroic.cache.Module(),
new com.spotify.heroic.generator.Module()
};
// @formatter:on
private final Optional<String> id;
private final Optional<String> host;
private final Optional<Integer> port;
private final Optional<Supplier<InputStream>> configStream;
private final Optional<Path> configPath;
private final Optional<URI> startupPing;
private final Optional<String> startupId;
/**
* Additional dynamic parameters to pass into the configuration of a profile. These are
* typically extracted from the commandline, or a properties file.
*/
private final ExtraParameters params;
/* flags */
private final boolean setupService;
private final boolean oneshot;
private final boolean disableBackends;
private final boolean setupShellServer;
/* extensions */
private final List<HeroicModule> modules;
private final List<HeroicProfile> profiles;
private final List<HeroicBootstrap> early;
private final List<HeroicBootstrap> late;
private final List<HeroicConfig.Builder> configFragments;
private final Optional<ExecutorService> executor;
protected HeroicCore(
Optional<String> id,
Optional<String> host,
Optional<Integer> port,
Optional<Supplier<InputStream>> configStream,
Optional<Path> configPath,
Optional<URI> startupPing,
Optional<String> startupId,
ExtraParameters params,
boolean setupService,
boolean oneshot,
boolean disableBackends,
boolean setupShellServer,
List<HeroicModule> modules,
List<HeroicProfile> profiles,
List<HeroicBootstrap> early,
List<HeroicBootstrap> late,
List<HeroicConfig.Builder> configFragments,
Optional<ExecutorService> executor
) {
this.id = id;
this.host = host;
this.port = port;
this.configStream = configStream;
this.configPath = configPath;
this.startupPing = startupPing;
this.startupId = startupId;
this.params = params;
this.setupService = setupService;
this.oneshot = oneshot;
this.disableBackends = disableBackends;
this.setupShellServer = setupShellServer;
this.modules = modules;
this.profiles = profiles;
this.early = early;
this.late = late;
this.configFragments = configFragments;
this.executor = executor;
}
@Override
public boolean isDisableLocal() {
return disableBackends;
}
@Override
public boolean isOneshot() {
return oneshot;
}
/**
* Start the Heroic core, step by step
* <p>
* <p>
* It sets up the early injector which is responsible for loading all the necessary components
* to parse a configuration file.
* <p>
* <p>
* Load all the external modules, which are configured in {@link #modules}.
* <p>
* <p>
* Load and build the configuration using the early injector
* <p>
* <p>
* Setup the primary injector which will provide the dependencies to the entire application
* <p>
* <p>
* Run all bootstraps that are configured in {@link #late}
* <p>
* <p>
* Start all the external modules. {@link #startLifeCycles}
*/
public HeroicCoreInstance newInstance() throws Exception {
final CoreLoadingComponent loading = loadingInjector();
loadModules(loading);
final HeroicConfig config = config(loading);
String sentryDsn = config.sentryDsn();
if (sentryDsn != null) {
Sentry.init(sentryDsn);
}
enableTracing(config.tracing());
final CoreEarlyComponent early = earlyInjector(loading, config);
runBootstrappers(early, this.early);
// Initialize the instance injector with access to early components.
final AtomicReference<CoreComponent> injector = new AtomicReference<>();
final HeroicCoreInstance instance =
new Instance(loading.async(), injector, early, config, this.late);
final CoreComponent primary = primaryInjector(early, config, instance);
primary.loadingLifeCycle().install();
primary.internalLifeCycleRegistry().scoped("startup future").start(() -> {
primary.context().resolveStartedFuture();
primary.usageTracking().reportStartup();
return primary.async().resolved(null);
});
// Update the instance injector, giving dynamic components initialized after this point
// access to the primary
// injector.
injector.set(primary);
return instance;
}
private void enableTracing(TracingConfig config) throws IOException {
final TracingConfig.Lightstep lightstep = config.getLightstep();
if (!lightstep.getCollectorHost().isBlank()) {
final Options.OptionsBuilder optionsBuilder = new Options.OptionsBuilder()
.withAccessToken(lightstep.getAccessToken())
.withMaxReportingIntervalMillis(lightstep.getReportingIntervalMs())
.withMaxBufferedSpans(lightstep.getMaxBufferedSpans())
.withCollectorProtocol("http")
.withResetClient(lightstep.getResetClient())
.withComponentName(lightstep.getComponentName())
.withCollectorHost(lightstep.getCollectorHost())
.withCollectorPort(lightstep.getCollectorPort());
final Options options;
try {
options = optionsBuilder.build();
} catch (MalformedURLException e) {
throw new IllegalArgumentException("Malformed configuration for LightStep.", e);
}
log.info("Creating LightStep tracing exporter");
LightStepExporterHandler handler =
new LightStepExporterHandler(new JRETracer(options));
log.info("Registering LightStep exporter as the SquashingTraceExporter delegate");
SquashingTraceExporter.createAndRegister(
handler,
config.getSquash().getThreshold(),
config.getSquash().getWhitelist()
);
log.info("Setting global trace probability to {}", config.getProbability());
TraceConfig traceConfig = Tracing.getTraceConfig();
traceConfig.updateActiveTraceParams(
traceConfig
.getActiveTraceParams()
.toBuilder()
.setSampler(Samplers.probabilitySampler(config.getProbability()))
.build());
}
if (config.getZpagesPort() > 0) {
log.info("Starting zpage handlers on port {}", config.getZpagesPort());
// Start a web server for tracing data
ZPageHandlers.startHttpServerAndRegisterAll(config.getZpagesPort());
}
}
/**
* This method basically goes through the list of bootstrappers registered by modules and runs
* them.
*
* @param early Injector to inject boostrappers using.
* @param bootstrappers Bootstraps to run.
*/
static void runBootstrappers(
final CoreEarlyComponent early, final List<HeroicBootstrap> bootstrappers
) throws Exception {
for (final HeroicBootstrap bootstrap : bootstrappers) {
try {
bootstrap.run(early);
} catch (Exception e) {
throw new Exception("Failed to run bootstrapper " + bootstrap, e);
}
}
}
/**
* Setup early injector, which is responsible for sufficiently providing dependencies to runtime
* components.
*/
private CoreLoadingComponent loadingInjector() {
log.info("Building Loading Injector");
final ExecutorService executor;
final boolean managedExecutor;
if (this.executor.isPresent()) {
executor = this.executor.get();
managedExecutor = false;
} else {
executor = setupExecutor(Runtime.getRuntime().availableProcessors() * 2);
managedExecutor = true;
}
return DaggerCoreLoadingComponent
.builder()
.loadingModule(new LoadingModule(executor, managedExecutor, this, params))
.build();
}
private CoreEarlyComponent earlyInjector(
final CoreLoadingComponent loading, final HeroicConfig config
) {
final Optional<String> id = Optionals.firstPresent(this.id, config.id());
return DaggerCoreEarlyComponent
.builder()
.coreLoadingComponent(loading)
.earlyModule(new EarlyModule(config, id))
.build();
}
/**
* Setup a fixed thread pool executor that correctly handles unhandled exceptions.
*
* @param threads Number of threads to configure.
*/
private ExecutorService setupExecutor(final int threads) {
return new ThreadPoolExecutor(threads, threads, 0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<>(), new ThreadFactoryBuilder()
.setNameFormat("heroic-core-%d")
.setUncaughtExceptionHandler(uncaughtExceptionHandler)
.build()) {
@Override
protected void afterExecute(Runnable r, Throwable t) {
super.afterExecute(r, t);
if (t == null && (r instanceof Future<?>)) {
try {
((Future<?>) r).get();
} catch (CancellationException e) {
t = e;
} catch (ExecutionException e) {
t = e.getCause();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
if (t != null) {
if (log.isErrorEnabled()) {
log.error("Unhandled exception caught in core executor", t);
log.error("Exiting (code=2)");
} else {
System.err.println("Unhandled exception caught in core executor");
System.err.println("Exiting (code=2)");
t.printStackTrace(System.err);
}
System.exit(2);
}
}
};
}
/**
* Setup primary injector, which will provide dependencies to the entire application.
*
* @param config The loaded configuration file.
* @param early The early injector, which will act as a parent to the primary injector to bridge
* all it's provided components.
* @return The primary guice injector.
*/
private CoreComponent primaryInjector(
final CoreEarlyComponent early, final HeroicConfig config, final HeroicCoreInstance instance
) {
log.info("Building Primary Injector");
final List<LifeCycle> life = new ArrayList<>();
final StatisticsComponent statistics = config.statistics().module(early);
life.add(statistics.life());
final HeroicReporter reporter = statistics.reporter();
// Register root components.
final CorePrimaryComponent primary = DaggerCorePrimaryComponent
.builder()
.coreEarlyComponent(early)
.primaryModule(new PrimaryModule(
instance,
config.features(),
reporter,
config.conditionalFeature(),
config.tracing()))
.build();
final QueryLoggingComponent queryLogging = config.queryLogging().component(primary);
final UsageTrackingComponent usageTracking = config.usageTracking().module(primary);
final Optional<HttpServer> server = setupServer(config, life, primary);
final CacheComponent cache = config.cache().module(primary);
life.add(cache.cacheLife());
final AnalyticsComponent analytics = config.analytics().module(primary);
life.add(analytics.analyticsLife());
final CoreMetadataComponent metadata = DaggerCoreMetadataComponent
.builder()
.primaryComponent(primary)
.metadataManagerModule(config.metadata())
.build();
life.add(metadata.metadataLife());
final CoreSuggestComponent suggest = DaggerCoreSuggestComponent
.builder()
.primaryComponent(primary)
.suggestManagerModule(config.suggest())
.build();
life.add(suggest.suggestLife());
final CoreMetricComponent metric = DaggerCoreMetricComponent
.builder()
.corePrimaryComponent(primary)
.metadataComponent(metadata)
.analyticsComponent(analytics)
.metricManagerModule(config.metric())
.queryLoggingComponent(queryLogging)
.build();
life.add(metric.metricLife());
final IngestionComponent ingestion =
config.ingestion().module(primary, suggest, metadata, metric);
life.add(ingestion.ingestionLife());
buildShellServer(config).ifPresent(shellServerModule -> {
final ShellServerComponent shellServer = DaggerShellServerComponent
.builder()
.primaryComponent(primary)
.shellServerModule(shellServerModule)
.build();
life.add(shellServer.shellServerLife());
});
final CoreClusterComponent cluster = DaggerCoreClusterComponent
.builder()
.primaryComponent(primary)
.metricComponent(metric)
.metadataComponent(metadata)
.suggestComponent(suggest)
.clusterManagerModule(config.cluster())
.clusterDiscoveryComponent(config.cluster().getDiscovery().module(primary))
.usageTrackingComponent(usageTracking)
.build();
life.add(cluster.clusterLife());
if (startupPing.isPresent() && startupId.isPresent()) {
final StartupPingerComponent pinger = DaggerStartupPingerComponent
.builder()
.corePrimaryComponent(primary)
.clusterComponent(cluster)
.startupPingerModule(
new StartupPingerModule(startupPing.get(), startupId.get(), server))
.build();
life.add(pinger.startupPingerLife());
}
final ConsumersComponent consumer = DaggerCoreConsumersComponent
.builder()
.coreConsumersModule(
new CoreConsumersModule(reporter, config.consumers(), primary, ingestion))
.corePrimaryComponent(primary)
.build();
life.add(consumer.consumersLife());
final QueryComponent query = DaggerCoreQueryComponent
.builder()
.queryModule(new QueryModule(config.metric().groupLimit(),
config.metric().smallQueryThreshold()))
.corePrimaryComponent(primary)
.clusterComponent(cluster)
.cacheComponent(cache)
.queryLoggingComponent(queryLogging)
.build();
life.add(query.queryLife());
final GeneratorComponent generator = config.generator().module(primary);
life.add(generator.generatorLife());
// install all lifecycles
final LifeCycle combined = LifeCycle.combined(life);
combined.install();
return DaggerCoreComponent
.builder()
.primaryComponent(primary)
.analyticsComponent(analytics)
.consumersComponent(consumer)
.metricComponent(metric)
.metadataComponent(metadata)
.suggestComponent(suggest)
.queryComponent(query)
.queryLoggingComponent(queryLogging)
.ingestionComponent(ingestion)
.clusterComponent(cluster)
.generatorComponent(generator)
.usageTrackingComponent(usageTracking)
.build();
}
private Optional<HttpServer> setupServer(
final HeroicConfig config, final List<LifeCycle> life, final CorePrimaryComponent primary
) {
if (!setupService) {
return Optional.empty();
}
final InetSocketAddress bindAddress = setupBindAddress(config);
final HttpServerComponent serverComponent = DaggerHttpServerComponent
.builder()
.primaryComponent(primary)
.httpServerModule(new HttpServerModule(
bindAddress,
config.enableCors(),
config.corsAllowOrigin(),
config.connectors(),
config.tracing()
))
.build();
// Trigger life cycle registration
life.add(serverComponent.life());
return Optional.of(serverComponent.server());
}
private Optional<ShellServerModule> buildShellServer(final HeroicConfig config) {
final Optional<ShellServerModule> shellServer = config.shellServer();
// a shell server is configured.
if (shellServer.isPresent()) {
return shellServer;
}
// must have a shell server
if (setupShellServer) {
return of(ShellServerModule.builder().build());
}
return empty();
}
private InetSocketAddress setupBindAddress(HeroicConfig config) {
final String host =
Optionals.pickOptional(config.host(), this.host).orElse(DEFAULT_HOST);
final int port = Optionals.pickOptional(config.port(), this.port).orElse(DEFAULT_PORT);
return new InetSocketAddress(host, port);
}
static void startLifeCycles(
CoreLifeCycleRegistry registry, CoreComponent primary, HeroicConfig config
) throws Exception {
if (!awaitLifeCycles("start", primary, config.startTimeout(), registry.starters())) {
throw new Exception("Failed to start all life cycles");
}
}
static void stopLifeCycles(
CoreLifeCycleRegistry registry, CoreComponent primary, HeroicConfig config
) throws Exception {
if (!awaitLifeCycles("stop", primary, config.stopTimeout(), registry.stoppers())) {
log.warn("Failed to stop all life cycles");
}
}
static boolean awaitLifeCycles(
final String op, final CoreComponent primary, final Duration await,
final List<LifeCycleNamedHook<AsyncFuture<Void>>> hooks
) throws InterruptedException, ExecutionException {
log.info("[{}] {} lifecycle(s)...", op, hooks.size());
final AsyncFramework async = primary.async();
final List<AsyncFuture<Void>> futures = new ArrayList<>();
final List<Pair<AsyncFuture<Void>, LifeCycleHook<AsyncFuture<Void>>>> pairs =
new ArrayList<>();
for (final LifeCycleNamedHook<AsyncFuture<Void>> hook : hooks) {
log.trace("[{}] {}", op, hook.id());
final AsyncFuture<Void> future;
try {
future = hook.get();
} catch (Exception e) {
futures.add(async.failed(e));
break;
}
if (log.isTraceEnabled()) {
final Stopwatch w = Stopwatch.createStarted();
future.onFinished(() -> {
log.trace("[{}] {}, took {}us", op, hook.id(),
w.elapsed(TimeUnit.MICROSECONDS));
});
}
futures.add(future);
pairs.add(Pair.of(future, hook));
}
try {
async.collect(futures).get(await.getDuration(), await.getUnit());
} catch (final TimeoutException e) {
log.error("Operation timed out");
for (final Pair<AsyncFuture<Void>, LifeCycleHook<AsyncFuture<Void>>> pair : pairs) {
if (!pair.getLeft().isDone()) {
log.error("{}: did not finish in time: {}", op, pair.getRight());
}
}
return false;
}
log.info("[{}] {} lifecycle(s) done", op, hooks.size());
return true;
}
private HeroicConfig config(LoadingComponent loading) throws Exception {
HeroicConfig.Builder builder = HeroicConfig.builder();
for (final HeroicProfile profile : profiles) {
log.info("Loading profile '{}' (params: {})", profile.description(), params);
final ExtraParameters p = profile.scope().map(params::scope).orElse(params);
builder = builder.merge(profile.build(p));
}
for (final HeroicConfig.Builder fragment : configFragments) {
builder = builder.merge(fragment);
}
final ObjectMapper mapper = loading.configMapper().copy();
// TODO: figure out where to put this
mapper.addMixIn(JettyConnectionFactory.Builder.class, TypeNameMixin.class);
if (configPath.isPresent()) {
builder = HeroicConfig
.loadConfig(mapper, configPath.get())
.map(builder::merge)
.orElse(builder);
}
if (configStream.isPresent()) {
builder = HeroicConfig
.loadConfigStream(mapper, configStream.get().get())
.map(builder::merge)
.orElse(builder);
}
return builder.build();
}
/**
* Load modules from the specified modules configuration file and wire up those components with
* early injection.
*
* @param loading Injector to wire up modules using.
*/
private void loadModules(final CoreLoadingComponent loading) {
final List<HeroicModule> modules = new ArrayList<>();
for (final HeroicModule builtin : BUILTIN_MODULES) {
modules.add(builtin);
}
modules.addAll(this.modules);
for (final HeroicModule module : modules) {
// inject members of an entry point and run them.
module.setup(loading).run();
}
log.info("Loaded {} module(s)", modules.size());
}
public static Builder builder() {
return new Builder();
}
public static final class Builder {
private Optional<String> id = empty();
private Optional<String> host = empty();
private Optional<Integer> port = empty();
private Optional<Supplier<InputStream>> configStream = empty();
private Optional<Path> configPath = empty();
private Optional<ExtraParameters> params = empty();
private Optional<HeroicReporter> reporter = empty();
private Optional<URI> startupPing = empty();
private Optional<String> startupId = empty();
/* flags */
private Optional<Boolean> setupService = empty();
private Optional<Boolean> oneshot = empty();
private Optional<Boolean> disableBackends = empty();
private Optional<Boolean> setupShellServer = empty();
/* extensions */
private final ImmutableList.Builder<HeroicModule> modules = ImmutableList.builder();
private final ImmutableList.Builder<HeroicProfile> profiles = ImmutableList.builder();
private final ImmutableList.Builder<HeroicBootstrap> early = ImmutableList.builder();
private final ImmutableList.Builder<HeroicBootstrap> late = ImmutableList.builder();
/* configuration fragments */
private final ImmutableList.Builder<HeroicConfig.Builder> configFragments =
ImmutableList.builder();
private Optional<ExecutorService> executor = empty();
public Builder executor(final ExecutorService executor) {
this.executor = of(executor);
return this;
}
/**
* Register a specific ID for this heroic instance.
*
* @param id Id of the instance.
* @return This builder.
*/
public Builder id(final String id) {
this.id = of(id);
return this;
}
/**
* If a shell server is not configured, setup the default shell server.
*/
public Builder setupShellServer(boolean setupShellServer) {
this.setupShellServer = of(setupShellServer);
return this;
}
/**
* Port to bind service to.
*/
public Builder port(final int port) {
this.port = of(port);
return this;
}
/**
* Host to bind service to.
*/
public Builder host(final String host) {
checkNotNull(host, "host");
this.host = of(host);
return this;
}
public Builder configStream(final Supplier<InputStream> configStream) {
checkNotNull(configStream, "configStream");
this.configStream = of(configStream);
return this;
}
public Builder configPath(final String configPath) {
checkNotNull(configPath, "configPath");
return configPath(Paths.get(configPath));
}
public Builder configPath(final Path configPath) {
checkNotNull(configPath, "configPath");
if (!Files.isReadable(configPath)) {
throw new IllegalArgumentException(
"Configuration is not readable: " + configPath.toAbsolutePath());
}
this.configPath = of(configPath);
return this;
}
/**
* Programmatically add a configuration fragment.
*
* @param config Configuration fragment to add.
* @return This builder.
*/
public Builder configFragment(final HeroicConfig.Builder config) {
checkNotNull(config, "config");
this.configFragments.add(config);
return this;
}
public Builder startupPing(final String startupPing) {
checkNotNull(startupPing, "startupPing");
this.startupPing = of(URI.create(startupPing));
return this;
}
public Builder startupId(final String startupId) {
checkNotNull(startupId, "startupId");
this.startupId = of(startupId);
return this;
}
public Builder parameters(final ExtraParameters params) {
checkNotNull(params, "params");
this.params = of(params);
return this;
}
public Builder reporter(final HeroicReporter reporter) {
checkNotNull(reporter, "reporter");
this.reporter = of(reporter);
return this;
}
/**
* Configure setup of the server component of heroic or not.
*/
public Builder setupService(final boolean setupService) {
this.setupService = of(setupService);
return this;
}
/**
* Do not perform any scheduled tasks, only perform then once during startup.
*/
public Builder oneshot(final boolean oneshot) {
this.oneshot = of(oneshot);
return this;
}
/**
* Disable local backends.
*/
public Builder disableBackends(final boolean disableBackends) {
this.disableBackends = of(disableBackends);
return this;
}
public Builder modules(final List<HeroicModule> modules) {
checkNotNull(modules, "modules");
this.modules.addAll(modules);
return this;
}
public Builder module(final HeroicModule module) {
checkNotNull(module, "module");
this.modules.add(module);
return this;
}
public Builder profiles(final List<HeroicProfile> profiles) {
checkNotNull(profiles, "profiles");
this.profiles.addAll(profiles);
return this;
}
public Builder profile(final HeroicProfile profile) {
checkNotNull(profile, "profile");
this.profiles.add(profile);
return this;
}
public Builder earlyBootstrap(final HeroicBootstrap bootstrap) {
checkNotNull(bootstrap, "bootstrap");
this.early.add(bootstrap);
return this;
}
public Builder bootstrappers(final List<HeroicBootstrap> bootstrappers) {
checkNotNull(bootstrappers, "bootstrappers");
this.late.addAll(bootstrappers);
return this;
}
public Builder bootstrap(final HeroicBootstrap bootstrap) {
checkNotNull(bootstrap, "bootstrap");
this.late.add(bootstrap);
return this;
}
public HeroicCore build() {
// @formatter:off
return new HeroicCore(
id,
host,
port,
configStream,
configPath,
startupPing,
startupId,
params.orElseGet(ExtraParameters::empty),