H2StorageProvider.java 8.9 KB
Newer Older
wu-sheng's avatar
wu-sheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * 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.storage.plugin.jdbc.h2;

import java.util.Properties;
wu-sheng's avatar
wu-sheng 已提交
22
import org.apache.skywalking.oap.server.core.CoreModule;
23 24 25 26 27 28 29 30 31
import org.apache.skywalking.oap.server.core.storage.IBatchDAO;
import org.apache.skywalking.oap.server.core.storage.IHistoryDeleteDAO;
import org.apache.skywalking.oap.server.core.storage.IRegisterLockDAO;
import org.apache.skywalking.oap.server.core.storage.StorageDAO;
import org.apache.skywalking.oap.server.core.storage.StorageException;
import org.apache.skywalking.oap.server.core.storage.StorageModule;
import org.apache.skywalking.oap.server.core.storage.cache.INetworkAddressInventoryCacheDAO;
import org.apache.skywalking.oap.server.core.storage.cache.IServiceInstanceInventoryCacheDAO;
import org.apache.skywalking.oap.server.core.storage.cache.IServiceInventoryCacheDAO;
32
import org.apache.skywalking.oap.server.core.storage.profile.IProfileTaskLogQueryDAO;
33
import org.apache.skywalking.oap.server.core.storage.profile.IProfileTaskQueryDAO;
34
import org.apache.skywalking.oap.server.core.storage.profile.IProfileThreadSnapshotQueryDAO;
35 36 37 38 39 40 41 42
import org.apache.skywalking.oap.server.core.storage.query.IAggregationQueryDAO;
import org.apache.skywalking.oap.server.core.storage.query.IAlarmQueryDAO;
import org.apache.skywalking.oap.server.core.storage.query.ILogQueryDAO;
import org.apache.skywalking.oap.server.core.storage.query.IMetadataQueryDAO;
import org.apache.skywalking.oap.server.core.storage.query.IMetricsQueryDAO;
import org.apache.skywalking.oap.server.core.storage.query.ITopNRecordsQueryDAO;
import org.apache.skywalking.oap.server.core.storage.query.ITopologyQueryDAO;
import org.apache.skywalking.oap.server.core.storage.query.ITraceQueryDAO;
43
import org.apache.skywalking.oap.server.core.storage.ttl.GeneralStorageTTL;
wu-sheng's avatar
wu-sheng 已提交
44
import org.apache.skywalking.oap.server.library.client.jdbc.hikaricp.JDBCHikariCPClient;
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
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;
import org.apache.skywalking.oap.server.storage.plugin.jdbc.h2.dao.H2AggregationQueryDAO;
import org.apache.skywalking.oap.server.storage.plugin.jdbc.h2.dao.H2AlarmQueryDAO;
import org.apache.skywalking.oap.server.storage.plugin.jdbc.h2.dao.H2BatchDAO;
import org.apache.skywalking.oap.server.storage.plugin.jdbc.h2.dao.H2HistoryDeleteDAO;
import org.apache.skywalking.oap.server.storage.plugin.jdbc.h2.dao.H2LogQueryDAO;
import org.apache.skywalking.oap.server.storage.plugin.jdbc.h2.dao.H2MetadataQueryDAO;
import org.apache.skywalking.oap.server.storage.plugin.jdbc.h2.dao.H2MetricsQueryDAO;
import org.apache.skywalking.oap.server.storage.plugin.jdbc.h2.dao.H2NetworkAddressInventoryCacheDAO;
import org.apache.skywalking.oap.server.storage.plugin.jdbc.h2.dao.H2ProfileTaskLogQueryDAO;
import org.apache.skywalking.oap.server.storage.plugin.jdbc.h2.dao.H2ProfileTaskQueryDAO;
import org.apache.skywalking.oap.server.storage.plugin.jdbc.h2.dao.H2ProfileThreadSnapshotQueryDAO;
import org.apache.skywalking.oap.server.storage.plugin.jdbc.h2.dao.H2RegisterLockDAO;
import org.apache.skywalking.oap.server.storage.plugin.jdbc.h2.dao.H2RegisterLockInstaller;
import org.apache.skywalking.oap.server.storage.plugin.jdbc.h2.dao.H2ServiceInstanceInventoryCacheDAO;
import org.apache.skywalking.oap.server.storage.plugin.jdbc.h2.dao.H2ServiceInventoryCacheDAO;
import org.apache.skywalking.oap.server.storage.plugin.jdbc.h2.dao.H2StorageDAO;
import org.apache.skywalking.oap.server.storage.plugin.jdbc.h2.dao.H2TableInstaller;
import org.apache.skywalking.oap.server.storage.plugin.jdbc.h2.dao.H2TopNRecordsQueryDAO;
import org.apache.skywalking.oap.server.storage.plugin.jdbc.h2.dao.H2TopologyQueryDAO;
import org.apache.skywalking.oap.server.storage.plugin.jdbc.h2.dao.H2TraceQueryDAO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
wu-sheng's avatar
wu-sheng 已提交
72 73

/**
74 75
 * H2 Storage provider is for demonstration and preview only. I will find that haven't implemented several interfaces,
 * because not necessary, and don't consider about performance very much.
76
 * <p>
wu-sheng's avatar
wu-sheng 已提交
77 78 79 80 81 82 83 84
 * If someone wants to implement SQL-style database as storage, please just refer the logic.
 */
public class H2StorageProvider extends ModuleProvider {

    private static final Logger logger = LoggerFactory.getLogger(H2StorageProvider.class);

    private H2StorageConfig config;
    private JDBCHikariCPClient h2Client;
85
    private H2RegisterLockDAO lockDAO;
wu-sheng's avatar
wu-sheng 已提交
86 87 88 89 90

    public H2StorageProvider() {
        config = new H2StorageConfig();
    }

91 92
    @Override
    public String name() {
wu-sheng's avatar
wu-sheng 已提交
93 94 95
        return "h2";
    }

96 97
    @Override
    public Class<? extends ModuleDefine> module() {
wu-sheng's avatar
wu-sheng 已提交
98 99 100
        return StorageModule.class;
    }

101 102
    @Override
    public ModuleConfig createConfigBeanIfAbsent() {
wu-sheng's avatar
wu-sheng 已提交
103 104 105
        return config;
    }

106 107
    @Override
    public void prepare() throws ServiceNotProvidedException, ModuleStartException {
wu-sheng's avatar
wu-sheng 已提交
108 109 110 111 112 113 114 115 116
        Properties settings = new Properties();
        settings.setProperty("dataSourceClassName", config.getDriver());
        settings.setProperty("dataSource.url", config.getUrl());
        settings.setProperty("dataSource.user", config.getUser());
        settings.setProperty("dataSource.password", config.getPassword());
        h2Client = new JDBCHikariCPClient(settings);

        this.registerServiceImplementation(IBatchDAO.class, new H2BatchDAO(h2Client));
        this.registerServiceImplementation(StorageDAO.class, new H2StorageDAO(h2Client));
117 118 119

        lockDAO = new H2RegisterLockDAO(h2Client);
        this.registerServiceImplementation(IRegisterLockDAO.class, lockDAO);
wu-sheng's avatar
wu-sheng 已提交
120 121

        this.registerServiceImplementation(IServiceInventoryCacheDAO.class, new H2ServiceInventoryCacheDAO(h2Client));
122 123 124 125
        this.registerServiceImplementation(
            IServiceInstanceInventoryCacheDAO.class, new H2ServiceInstanceInventoryCacheDAO(h2Client));
        this.registerServiceImplementation(
            INetworkAddressInventoryCacheDAO.class, new H2NetworkAddressInventoryCacheDAO(h2Client));
wu-sheng's avatar
wu-sheng 已提交
126 127

        this.registerServiceImplementation(ITopologyQueryDAO.class, new H2TopologyQueryDAO(h2Client));
128
        this.registerServiceImplementation(IMetricsQueryDAO.class, new H2MetricsQueryDAO(h2Client));
wu-sheng's avatar
wu-sheng 已提交
129
        this.registerServiceImplementation(ITraceQueryDAO.class, new H2TraceQueryDAO(h2Client));
130 131
        this.registerServiceImplementation(
            IMetadataQueryDAO.class, new H2MetadataQueryDAO(h2Client, config.getMetadataQueryMaxSize()));
wu-sheng's avatar
wu-sheng 已提交
132
        this.registerServiceImplementation(IAggregationQueryDAO.class, new H2AggregationQueryDAO(h2Client));
133
        this.registerServiceImplementation(IAlarmQueryDAO.class, new H2AlarmQueryDAO(h2Client));
134 135
        this.registerServiceImplementation(
            IHistoryDeleteDAO.class, new H2HistoryDeleteDAO(getManager(), h2Client, new GeneralStorageTTL()));
wu-sheng's avatar
wu-sheng 已提交
136
        this.registerServiceImplementation(ITopNRecordsQueryDAO.class, new H2TopNRecordsQueryDAO(h2Client));
137
        this.registerServiceImplementation(ILogQueryDAO.class, new H2LogQueryDAO(h2Client));
138 139

        this.registerServiceImplementation(IProfileTaskQueryDAO.class, new H2ProfileTaskQueryDAO(h2Client));
140
        this.registerServiceImplementation(IProfileTaskLogQueryDAO.class, new H2ProfileTaskLogQueryDAO(h2Client));
141 142
        this.registerServiceImplementation(
            IProfileThreadSnapshotQueryDAO.class, new H2ProfileThreadSnapshotQueryDAO(h2Client));
wu-sheng's avatar
wu-sheng 已提交
143 144
    }

145 146
    @Override
    public void start() throws ServiceNotProvidedException, ModuleStartException {
wu-sheng's avatar
wu-sheng 已提交
147
        try {
148
            h2Client.connect();
wu-sheng's avatar
wu-sheng 已提交
149 150 151

            H2TableInstaller installer = new H2TableInstaller(getManager());
            installer.install(h2Client);
152 153

            new H2RegisterLockInstaller().install(h2Client, lockDAO);
wu-sheng's avatar
wu-sheng 已提交
154 155 156 157 158
        } catch (StorageException e) {
            throw new ModuleStartException(e.getMessage(), e);
        }
    }

159 160
    @Override
    public void notifyAfterCompleted() throws ServiceNotProvidedException, ModuleStartException {
wu-sheng's avatar
wu-sheng 已提交
161 162 163

    }

164 165
    @Override
    public String[] requiredModules() {
wu-sheng's avatar
wu-sheng 已提交
166
        return new String[] {CoreModule.NAME};
wu-sheng's avatar
wu-sheng 已提交
167 168
    }
}