CoreModuleProvider.java 17.4 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 40
import org.apache.skywalking.oap.server.core.cache.CacheUpdateTimer;
import org.apache.skywalking.oap.server.core.cache.EndpointInventoryCache;
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;
41
import org.apache.skywalking.oap.server.core.command.CommandService;
42 43 44 45 46 47
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;
48
import org.apache.skywalking.oap.server.core.profile.ProfileTaskMutationService;
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
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.EndpointInventoryRegister;
import org.apache.skywalking.oap.server.core.register.service.IEndpointInventoryRegister;
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;
70
import org.apache.skywalking.oap.server.core.remote.health.HealthCheckServiceHandler;
71 72 73 74 75 76 77
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;
78
import org.apache.skywalking.oap.server.core.storage.PersistenceTimer;
79 80 81 82
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;
83
import org.apache.skywalking.oap.server.core.storage.ttl.DataTTLKeeperTimer;
84 85 86 87 88 89 90 91
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;
92 93 94
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 已提交
95
import org.apache.skywalking.oap.server.telemetry.TelemetryModule;
96

97 98 99 100 101 102 103 104
/**
 * 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.
 */
105 106 107 108 109
public class CoreModuleProvider extends ModuleProvider {

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

    public CoreModuleProvider() {
        super();
        this.moduleConfig = new CoreModuleConfig();
120
        this.annotationScan = new AnnotationScan();
121
        this.storageModels = new StorageModels();
122
        this.receiver = new SourceReceiverImpl();
123 124
    }

125 126
    @Override
    public String name() {
127 128 129
        return "default";
    }

130 131
    @Override
    public Class<? extends ModuleDefine> module() {
132 133 134
        return CoreModule.class;
    }

135 136
    @Override
    public ModuleConfig createConfigBeanIfAbsent() {
137 138 139
        return moduleConfig;
    }

140 141
    @Override
    public void prepare() throws ServiceNotProvidedException, ModuleStartException {
142 143 144 145
        if (moduleConfig.isActiveExtraModelColumns()) {
            DefaultScopeDefine.activeExtraModelColumns();
        }

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

wu-sheng's avatar
wu-sheng 已提交
148 149 150
        AnnotationScan scopeScan = new AnnotationScan();
        scopeScan.registerListener(new DefaultScopeDefine.Listener());
        try {
wu-sheng's avatar
wu-sheng 已提交
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
            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 已提交
166 167 168 169
        } catch (IOException e) {
            throw new ModuleStartException(e.getMessage(), e);
        }

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

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

196
        this.registerServiceImplementation(ConfigService.class, new ConfigService(moduleConfig));
197 198
        this.registerServiceImplementation(
            DownsamplingConfigService.class, new DownsamplingConfigService(moduleConfig.getDownsampling()));
199

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

203 204
        this.registerServiceImplementation(IComponentLibraryCatalogService.class, new ComponentLibraryCatalogService());

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

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

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

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

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

225 226 227 228
        this.registerServiceImplementation(
            EndpointInventoryCache.class, new EndpointInventoryCache(getManager(), moduleConfig));
        this.registerServiceImplementation(
            IEndpointInventoryRegister.class, new EndpointInventoryRegister(getManager()));
229

230 231 232 233
        this.registerServiceImplementation(
            NetworkAddressInventoryCache.class, new NetworkAddressInventoryCache(getManager(), moduleConfig));
        this.registerServiceImplementation(
            INetworkAddressInventoryRegister.class, new NetworkAddressInventoryRegister(getManager()));
234

235
        this.registerServiceImplementation(TopologyQueryService.class, new TopologyQueryService(getManager()));
彭勇升 pengys 已提交
236 237
        this.registerServiceImplementation(MetricQueryService.class, new MetricQueryService(getManager()));
        this.registerServiceImplementation(TraceQueryService.class, new TraceQueryService(getManager()));
238
        this.registerServiceImplementation(LogQueryService.class, new LogQueryService(getManager()));
239
        this.registerServiceImplementation(MetadataQueryService.class, new MetadataQueryService(getManager()));
240
        this.registerServiceImplementation(AggregationQueryService.class, new AggregationQueryService(getManager()));
241
        this.registerServiceImplementation(AlarmQueryService.class, new AlarmQueryService(getManager()));
wu-sheng's avatar
wu-sheng 已提交
242
        this.registerServiceImplementation(TopNRecordsQueryService.class, new TopNRecordsQueryService(getManager()));
243

244
        // add profile service implementations
245 246 247 248
        this.registerServiceImplementation(
            ProfileTaskMutationService.class, new ProfileTaskMutationService(getManager()));
        this.registerServiceImplementation(
            ProfileTaskQueryService.class, new ProfileTaskQueryService(getManager(), moduleConfig));
249
        this.registerServiceImplementation(ProfileTaskCache.class, new ProfileTaskCache(getManager(), moduleConfig));
250

251 252
        this.registerServiceImplementation(CommandService.class, new CommandService(getManager()));

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

255 256 257 258 259 260
        if (moduleConfig.isGRPCSslEnabled()) {
            this.remoteClientManager = new RemoteClientManager(getManager(), moduleConfig.getRemoteTimeout(),
                                                               Paths.get(moduleConfig.getGRPCSslTrustedCAPath()).toFile());
        } else {
            this.remoteClientManager = new RemoteClientManager(getManager(), moduleConfig.getRemoteTimeout());
        }
261
        this.registerServiceImplementation(RemoteClientManager.class, remoteClientManager);
262 263

        MetricsStreamProcessor.getInstance().setEnableDatabaseSession(moduleConfig.isEnableDatabaseSession());
264
        TopNStreamProcessor.getInstance().setTopNWorkerReportCycle(moduleConfig.getTopNReportPeriod());
G
Gao Hongtao 已提交
265 266
        apdexThresholdConfig = new ApdexThresholdConfig(this);
        ApdexMetrics.setDICT(apdexThresholdConfig);
267 268
    }

269 270
    @Override
    public void start() throws ModuleStartException {
G
Gao Hongtao 已提交
271

彭勇升 pengys 已提交
272
        grpcServer.addHandler(new RemoteServiceHandler(getManager()));
273
        grpcServer.addHandler(new HealthCheckServiceHandler());
274
        remoteClientManager.start();
275

彭勇升 pengys 已提交
276
        try {
277
            receiver.scan();
wu-sheng's avatar
wu-sheng 已提交
278 279 280
            annotationScan.scan();

            oalEngine.notifyAllListeners();
281
        } catch (IOException | IllegalAccessException | InstantiationException e) {
彭勇升 pengys 已提交
282 283
            throw new ModuleStartException(e.getMessage(), e);
        }
284

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

300 301
        DynamicConfigurationService dynamicConfigurationService = getManager().find(ConfigurationModule.NAME)
                                                                              .provider()
302 303
                                                                              .getService(
                                                                                  DynamicConfigurationService.class);
G
Gao Hongtao 已提交
304
        dynamicConfigurationService.registerConfigChangeWatcher(apdexThresholdConfig);
305 306
    }

307 308
    @Override
    public void notifyAfterCompleted() throws ModuleStartException {
309 310 311 312 313 314 315
        try {
            grpcServer.start();
            jettyServer.start();
        } catch (ServerException e) {
            throw new ModuleStartException(e.getMessage(), e);
        }

316
        PersistenceTimer.INSTANCE.start(getManager(), moduleConfig);
317

318
        if (moduleConfig.isEnableDataKeeperExecutor()) {
K
kezhenxu94 已提交
319
            DataTTLKeeperTimer.INSTANCE.start(getManager(), moduleConfig);
320
        }
321 322

        CacheUpdateTimer.INSTANCE.start(getManager());
323
    }
wu-sheng's avatar
wu-sheng 已提交
324 325 326

    @Override
    public String[] requiredModules() {
327 328 329 330
        return new String[] {
            TelemetryModule.NAME,
            ConfigurationModule.NAME
        };
wu-sheng's avatar
wu-sheng 已提交
331
    }
332
}