CoreModuleProvider.java 16.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * 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.skywalking.oap.server.core;

21
import java.io.IOException;
22
import java.nio.file.Paths;
23
import org.apache.skywalking.oap.server.configuration.api.ConfigurationModule;
G
Gao Hongtao 已提交
24
import org.apache.skywalking.oap.server.configuration.api.DynamicConfigurationService;
25 26 27
import org.apache.skywalking.oap.server.core.analysis.ApdexThresholdConfig;
import org.apache.skywalking.oap.server.core.analysis.DisableRegister;
import org.apache.skywalking.oap.server.core.analysis.StreamAnnotationListener;
G
Gao Hongtao 已提交
28
import org.apache.skywalking.oap.server.core.analysis.metrics.ApdexMetrics;
29
import org.apache.skywalking.oap.server.core.analysis.worker.MetricsStreamProcessor;
30
import org.apache.skywalking.oap.server.core.analysis.worker.TopNStreamProcessor;
31
import org.apache.skywalking.oap.server.core.annotation.AnnotationScan;
32 33 34 35 36 37 38 39
import org.apache.skywalking.oap.server.core.cache.CacheUpdateTimer;
import org.apache.skywalking.oap.server.core.cache.NetworkAddressInventoryCache;
import org.apache.skywalking.oap.server.core.cache.ProfileTaskCache;
import org.apache.skywalking.oap.server.core.cache.ServiceInstanceInventoryCache;
import org.apache.skywalking.oap.server.core.cache.ServiceInventoryCache;
import org.apache.skywalking.oap.server.core.cluster.ClusterModule;
import org.apache.skywalking.oap.server.core.cluster.ClusterRegister;
import org.apache.skywalking.oap.server.core.cluster.RemoteInstance;
40
import org.apache.skywalking.oap.server.core.command.CommandService;
41 42 43 44 45 46
import org.apache.skywalking.oap.server.core.config.ComponentLibraryCatalogService;
import org.apache.skywalking.oap.server.core.config.ConfigService;
import org.apache.skywalking.oap.server.core.config.DownsamplingConfigService;
import org.apache.skywalking.oap.server.core.config.IComponentLibraryCatalogService;
import org.apache.skywalking.oap.server.core.oal.rt.OALEngine;
import org.apache.skywalking.oap.server.core.oal.rt.OALEngineLoader;
47
import org.apache.skywalking.oap.server.core.profile.ProfileTaskMutationService;
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
import org.apache.skywalking.oap.server.core.query.AggregationQueryService;
import org.apache.skywalking.oap.server.core.query.AlarmQueryService;
import org.apache.skywalking.oap.server.core.query.LogQueryService;
import org.apache.skywalking.oap.server.core.query.MetadataQueryService;
import org.apache.skywalking.oap.server.core.query.MetricQueryService;
import org.apache.skywalking.oap.server.core.query.ProfileTaskQueryService;
import org.apache.skywalking.oap.server.core.query.TopNRecordsQueryService;
import org.apache.skywalking.oap.server.core.query.TopologyQueryService;
import org.apache.skywalking.oap.server.core.query.TraceQueryService;
import org.apache.skywalking.oap.server.core.register.service.INetworkAddressInventoryRegister;
import org.apache.skywalking.oap.server.core.register.service.IServiceInstanceInventoryRegister;
import org.apache.skywalking.oap.server.core.register.service.IServiceInventoryRegister;
import org.apache.skywalking.oap.server.core.register.service.NetworkAddressInventoryRegister;
import org.apache.skywalking.oap.server.core.register.service.ServiceInstanceInventoryRegister;
import org.apache.skywalking.oap.server.core.register.service.ServiceInventoryRegister;
import org.apache.skywalking.oap.server.core.remote.RemoteSenderService;
import org.apache.skywalking.oap.server.core.remote.RemoteServiceHandler;
import org.apache.skywalking.oap.server.core.remote.client.Address;
import org.apache.skywalking.oap.server.core.remote.client.RemoteClientManager;
67
import org.apache.skywalking.oap.server.core.remote.health.HealthCheckServiceHandler;
68 69 70 71 72 73 74
import org.apache.skywalking.oap.server.core.server.GRPCHandlerRegister;
import org.apache.skywalking.oap.server.core.server.GRPCHandlerRegisterImpl;
import org.apache.skywalking.oap.server.core.server.JettyHandlerRegister;
import org.apache.skywalking.oap.server.core.server.JettyHandlerRegisterImpl;
import org.apache.skywalking.oap.server.core.source.DefaultScopeDefine;
import org.apache.skywalking.oap.server.core.source.SourceReceiver;
import org.apache.skywalking.oap.server.core.source.SourceReceiverImpl;
75
import org.apache.skywalking.oap.server.core.storage.PersistenceTimer;
76 77 78 79
import org.apache.skywalking.oap.server.core.storage.model.IModelGetter;
import org.apache.skywalking.oap.server.core.storage.model.IModelOverride;
import org.apache.skywalking.oap.server.core.storage.model.IModelSetter;
import org.apache.skywalking.oap.server.core.storage.model.StorageModels;
80
import org.apache.skywalking.oap.server.core.storage.ttl.DataTTLKeeperTimer;
81 82 83 84 85 86 87 88
import org.apache.skywalking.oap.server.core.worker.IWorkerInstanceGetter;
import org.apache.skywalking.oap.server.core.worker.IWorkerInstanceSetter;
import org.apache.skywalking.oap.server.core.worker.WorkerInstancesService;
import org.apache.skywalking.oap.server.library.module.ModuleConfig;
import org.apache.skywalking.oap.server.library.module.ModuleDefine;
import org.apache.skywalking.oap.server.library.module.ModuleProvider;
import org.apache.skywalking.oap.server.library.module.ModuleStartException;
import org.apache.skywalking.oap.server.library.module.ServiceNotProvidedException;
89 90 91
import org.apache.skywalking.oap.server.library.server.ServerException;
import org.apache.skywalking.oap.server.library.server.grpc.GRPCServer;
import org.apache.skywalking.oap.server.library.server.jetty.JettyServer;
wu-sheng's avatar
wu-sheng 已提交
92
import org.apache.skywalking.oap.server.telemetry.TelemetryModule;
93

94 95 96 97 98 99 100 101
/**
 * Core module provider includes the recommended and default implementations of {@link CoreModule#services()}. All
 * services with these default implementations are widely used including data receiver, data analysis, streaming
 * process, storage and query.
 *
 * NOTICE. In our experiences, no one should re-implement the core module service implementations, unless we are very
 * familiar with all mechanisms of SkyWalking.
 */
102 103 104 105 106
public class CoreModuleProvider extends ModuleProvider {

    private final CoreModuleConfig moduleConfig;
    private GRPCServer grpcServer;
    private JettyServer jettyServer;
107
    private RemoteClientManager remoteClientManager;
108
    private final AnnotationScan annotationScan;
109
    private final StorageModels storageModels;
110
    private final SourceReceiverImpl receiver;
wu-sheng's avatar
wu-sheng 已提交
111
    private OALEngine oalEngine;
G
Gao Hongtao 已提交
112
    private ApdexThresholdConfig apdexThresholdConfig;
113 114 115 116

    public CoreModuleProvider() {
        super();
        this.moduleConfig = new CoreModuleConfig();
117
        this.annotationScan = new AnnotationScan();
118
        this.storageModels = new StorageModels();
119
        this.receiver = new SourceReceiverImpl();
120 121
    }

122 123
    @Override
    public String name() {
124 125 126
        return "default";
    }

127 128
    @Override
    public Class<? extends ModuleDefine> module() {
129 130 131
        return CoreModule.class;
    }

132 133
    @Override
    public ModuleConfig createConfigBeanIfAbsent() {
134 135 136
        return moduleConfig;
    }

137 138
    @Override
    public void prepare() throws ServiceNotProvidedException, ModuleStartException {
139 140 141 142
        if (moduleConfig.isActiveExtraModelColumns()) {
            DefaultScopeDefine.activeExtraModelColumns();
        }

143
        StreamAnnotationListener streamAnnotationListener = new StreamAnnotationListener(getManager());
wu-sheng's avatar
wu-sheng 已提交
144

wu-sheng's avatar
wu-sheng 已提交
145 146 147
        AnnotationScan scopeScan = new AnnotationScan();
        scopeScan.registerListener(new DefaultScopeDefine.Listener());
        try {
wu-sheng's avatar
wu-sheng 已提交
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
            scopeScan.scan();

            oalEngine = OALEngineLoader.get();
            oalEngine.setStreamListener(streamAnnotationListener);
            oalEngine.setDispatcherListener(receiver.getDispatcherManager());
            oalEngine.start(getClass().getClassLoader());
        } catch (Exception e) {
            throw new ModuleStartException(e.getMessage(), e);
        }

        AnnotationScan oalDisable = new AnnotationScan();
        oalDisable.registerListener(DisableRegister.INSTANCE);
        oalDisable.registerListener(new DisableRegister.SingleDisableScanListener());
        try {
            oalDisable.scan();
wu-sheng's avatar
wu-sheng 已提交
163 164 165 166
        } catch (IOException e) {
            throw new ModuleStartException(e.getMessage(), e);
        }

167 168 169
        if (moduleConfig.isGRPCSslEnabled()) {
            grpcServer = new GRPCServer(moduleConfig.getGRPCHost(), moduleConfig.getGRPCPort(),
                                        Paths.get(moduleConfig.getGRPCSslCertChainPath()).toFile(),
170 171
                                        Paths.get(moduleConfig.getGRPCSslKeyPath()).toFile()
            );
172 173 174
        } else {
            grpcServer = new GRPCServer(moduleConfig.getGRPCHost(), moduleConfig.getGRPCPort());
        }
175 176 177 178 179 180
        if (moduleConfig.getMaxConcurrentCallsPerConnection() > 0) {
            grpcServer.setMaxConcurrentCallsPerConnection(moduleConfig.getMaxConcurrentCallsPerConnection());
        }
        if (moduleConfig.getMaxMessageSize() > 0) {
            grpcServer.setMaxMessageSize(moduleConfig.getMaxMessageSize());
        }
181 182 183 184 185 186
        if (moduleConfig.getGRPCThreadPoolQueueSize() > 0) {
            grpcServer.setThreadPoolQueueSize(moduleConfig.getGRPCThreadPoolQueueSize());
        }
        if (moduleConfig.getGRPCThreadPoolSize() > 0) {
            grpcServer.setThreadPoolSize(moduleConfig.getGRPCThreadPoolSize());
        }
187 188
        grpcServer.initialize();

189 190
        jettyServer = new JettyServer(
            moduleConfig.getRestHost(), moduleConfig.getRestPort(), moduleConfig.getRestContextPath(), moduleConfig
191
            .getJettySelectors());
192 193
        jettyServer.initialize();

194
        this.registerServiceImplementation(ConfigService.class, new ConfigService(moduleConfig));
195 196
        this.registerServiceImplementation(
            DownsamplingConfigService.class, new DownsamplingConfigService(moduleConfig.getDownsampling()));
197

198 199 200
        this.registerServiceImplementation(GRPCHandlerRegister.class, new GRPCHandlerRegisterImpl(grpcServer));
        this.registerServiceImplementation(JettyHandlerRegister.class, new JettyHandlerRegisterImpl(jettyServer));

201 202
        this.registerServiceImplementation(IComponentLibraryCatalogService.class, new ComponentLibraryCatalogService());

203
        this.registerServiceImplementation(SourceReceiver.class, receiver);
彭勇升 pengys 已提交
204

205 206 207 208
        WorkerInstancesService instancesService = new WorkerInstancesService();
        this.registerServiceImplementation(IWorkerInstanceGetter.class, instancesService);
        this.registerServiceImplementation(IWorkerInstanceSetter.class, instancesService);

彭勇升 pengys 已提交
209
        this.registerServiceImplementation(RemoteSenderService.class, new RemoteSenderService(getManager()));
210 211 212
        this.registerServiceImplementation(IModelSetter.class, storageModels);
        this.registerServiceImplementation(IModelGetter.class, storageModels);
        this.registerServiceImplementation(IModelOverride.class, storageModels);
213

214 215
        this.registerServiceImplementation(
            ServiceInventoryCache.class, new ServiceInventoryCache(getManager(), moduleConfig));
216 217
        this.registerServiceImplementation(IServiceInventoryRegister.class, new ServiceInventoryRegister(getManager()));

218 219 220 221
        this.registerServiceImplementation(
            ServiceInstanceInventoryCache.class, new ServiceInstanceInventoryCache(getManager(), moduleConfig));
        this.registerServiceImplementation(
            IServiceInstanceInventoryRegister.class, new ServiceInstanceInventoryRegister(getManager()));
222

223 224 225 226
        this.registerServiceImplementation(
            NetworkAddressInventoryCache.class, new NetworkAddressInventoryCache(getManager(), moduleConfig));
        this.registerServiceImplementation(
            INetworkAddressInventoryRegister.class, new NetworkAddressInventoryRegister(getManager()));
227

228
        this.registerServiceImplementation(TopologyQueryService.class, new TopologyQueryService(getManager()));
彭勇升 pengys 已提交
229 230
        this.registerServiceImplementation(MetricQueryService.class, new MetricQueryService(getManager()));
        this.registerServiceImplementation(TraceQueryService.class, new TraceQueryService(getManager()));
231
        this.registerServiceImplementation(LogQueryService.class, new LogQueryService(getManager()));
232
        this.registerServiceImplementation(MetadataQueryService.class, new MetadataQueryService(getManager()));
233
        this.registerServiceImplementation(AggregationQueryService.class, new AggregationQueryService(getManager()));
234
        this.registerServiceImplementation(AlarmQueryService.class, new AlarmQueryService(getManager()));
wu-sheng's avatar
wu-sheng 已提交
235
        this.registerServiceImplementation(TopNRecordsQueryService.class, new TopNRecordsQueryService(getManager()));
236

237
        // add profile service implementations
238 239 240 241
        this.registerServiceImplementation(
            ProfileTaskMutationService.class, new ProfileTaskMutationService(getManager()));
        this.registerServiceImplementation(
            ProfileTaskQueryService.class, new ProfileTaskQueryService(getManager(), moduleConfig));
242
        this.registerServiceImplementation(ProfileTaskCache.class, new ProfileTaskCache(getManager(), moduleConfig));
243

244 245
        this.registerServiceImplementation(CommandService.class, new CommandService(getManager()));

wu-sheng's avatar
wu-sheng 已提交
246
        annotationScan.registerListener(streamAnnotationListener);
247

248 249
        if (moduleConfig.isGRPCSslEnabled()) {
            this.remoteClientManager = new RemoteClientManager(getManager(), moduleConfig.getRemoteTimeout(),
250 251 252
                                                               Paths.get(moduleConfig.getGRPCSslTrustedCAPath())
                                                                    .toFile()
            );
253 254 255
        } else {
            this.remoteClientManager = new RemoteClientManager(getManager(), moduleConfig.getRemoteTimeout());
        }
256
        this.registerServiceImplementation(RemoteClientManager.class, remoteClientManager);
257 258

        MetricsStreamProcessor.getInstance().setEnableDatabaseSession(moduleConfig.isEnableDatabaseSession());
259
        TopNStreamProcessor.getInstance().setTopNWorkerReportCycle(moduleConfig.getTopNReportPeriod());
G
Gao Hongtao 已提交
260 261
        apdexThresholdConfig = new ApdexThresholdConfig(this);
        ApdexMetrics.setDICT(apdexThresholdConfig);
262 263
    }

264 265
    @Override
    public void start() throws ModuleStartException {
G
Gao Hongtao 已提交
266

彭勇升 pengys 已提交
267
        grpcServer.addHandler(new RemoteServiceHandler(getManager()));
268
        grpcServer.addHandler(new HealthCheckServiceHandler());
269
        remoteClientManager.start();
270

彭勇升 pengys 已提交
271
        try {
272
            receiver.scan();
wu-sheng's avatar
wu-sheng 已提交
273 274 275
            annotationScan.scan();

            oalEngine.notifyAllListeners();
276
        } catch (IOException | IllegalAccessException | InstantiationException e) {
彭勇升 pengys 已提交
277 278
            throw new ModuleStartException(e.getMessage(), e);
        }
279

280
        if (CoreModuleConfig.Role.Mixed.name()
281
                                       .equalsIgnoreCase(
282 283 284 285
                                           moduleConfig.getRole())
            || CoreModuleConfig.Role.Aggregator.name()
                                               .equalsIgnoreCase(
                                                   moduleConfig.getRole())) {
286 287
            RemoteInstance gRPCServerInstance = new RemoteInstance(
                new Address(moduleConfig.getGRPCHost(), moduleConfig.getGRPCPort(), true));
288 289 290 291 292
            this.getManager()
                .find(ClusterModule.NAME)
                .provider()
                .getService(ClusterRegister.class)
                .registerRemote(gRPCServerInstance);
293 294
        }

295 296
        DynamicConfigurationService dynamicConfigurationService = getManager().find(ConfigurationModule.NAME)
                                                                              .provider()
297 298
                                                                              .getService(
                                                                                  DynamicConfigurationService.class);
G
Gao Hongtao 已提交
299
        dynamicConfigurationService.registerConfigChangeWatcher(apdexThresholdConfig);
300 301
    }

302 303
    @Override
    public void notifyAfterCompleted() throws ModuleStartException {
304 305 306 307 308 309 310
        try {
            grpcServer.start();
            jettyServer.start();
        } catch (ServerException e) {
            throw new ModuleStartException(e.getMessage(), e);
        }

311
        PersistenceTimer.INSTANCE.start(getManager(), moduleConfig);
312

313
        if (moduleConfig.isEnableDataKeeperExecutor()) {
K
kezhenxu94 已提交
314
            DataTTLKeeperTimer.INSTANCE.start(getManager(), moduleConfig);
315
        }
316 317

        CacheUpdateTimer.INSTANCE.start(getManager());
318
    }
wu-sheng's avatar
wu-sheng 已提交
319 320 321

    @Override
    public String[] requiredModules() {
322 323 324 325
        return new String[] {
            TelemetryModule.NAME,
            ConfigurationModule.NAME
        };
wu-sheng's avatar
wu-sheng 已提交
326
    }
327
}