Search in sources :

Example 1 with StorageException

use of org.apache.skywalking.oap.server.core.storage.StorageException in project skywalking by apache.

the class InfluxStorageProvider method start.

@Override
public void start() throws ServiceNotProvidedException, ModuleStartException {
    MetricsCreator metricCreator = getManager().find(TelemetryModule.NAME).provider().getService(MetricsCreator.class);
    HealthCheckMetrics healthChecker = metricCreator.createHealthCheckerGauge("storage_influxdb", MetricsTag.EMPTY_KEY, MetricsTag.EMPTY_VALUE);
    client.registerChecker(healthChecker);
    try {
        client.connect();
        InfluxTableInstaller installer = new InfluxTableInstaller(client, getManager());
        getManager().find(CoreModule.NAME).provider().getService(ModelCreator.class).addModelListener(installer);
    } catch (StorageException e) {
        throw new ModuleStartException(e.getMessage(), e);
    }
}
Also used : HealthCheckMetrics(org.apache.skywalking.oap.server.telemetry.api.HealthCheckMetrics) ModuleStartException(org.apache.skywalking.oap.server.library.module.ModuleStartException) MetricsCreator(org.apache.skywalking.oap.server.telemetry.api.MetricsCreator) ModelCreator(org.apache.skywalking.oap.server.core.storage.model.ModelCreator) StorageException(org.apache.skywalking.oap.server.core.storage.StorageException)

Example 2 with StorageException

use of org.apache.skywalking.oap.server.core.storage.StorageException in project incubator-skywalking by apache.

the class MeterSystem method create.

/**
 * Create streaming calculation of the given metrics name. This methods is synchronized due to heavy implementation
 * including creating dynamic class. Don't use this in concurrency runtime.
 *
 * @param metricsName  The name used as the storage eneity and in the query stage.
 * @param functionName The function provided through {@link MeterFunction}.
 * @throws IllegalArgumentException if the parameter can't match the expectation.
 * @throws UnexpectedException      if binary code manipulation fails or stream core failure.
 */
public synchronized <T> void create(String metricsName, String functionName, ScopeType type, Class<T> dataType) throws IllegalArgumentException {
    /**
     * Create a new meter class dynamically.
     */
    final Class<? extends AcceptableValue> meterFunction = functionRegister.get(functionName);
    if (meterFunction == null) {
        throw new IllegalArgumentException("Function " + functionName + " can't be found.");
    }
    boolean foundDataType = false;
    String acceptance = null;
    for (final Type genericInterface : meterFunction.getGenericInterfaces()) {
        if (genericInterface instanceof ParameterizedType) {
            ParameterizedType parameterizedType = (ParameterizedType) genericInterface;
            if (parameterizedType.getRawType().getTypeName().equals(AcceptableValue.class.getName())) {
                Type[] arguments = parameterizedType.getActualTypeArguments();
                if (arguments[0].equals(dataType)) {
                    foundDataType = true;
                } else {
                    acceptance = arguments[0].getTypeName();
                }
            }
            if (foundDataType) {
                break;
            }
        }
    }
    if (!foundDataType) {
        throw new IllegalArgumentException("Function " + functionName + " requires <" + acceptance + "> in AcceptableValue" + " but using " + dataType.getName() + " in the creation");
    }
    final CtClass parentClass;
    try {
        parentClass = classPool.get(meterFunction.getCanonicalName());
        if (!Metrics.class.isAssignableFrom(meterFunction)) {
            throw new IllegalArgumentException("Function " + functionName + " doesn't inherit from Metrics.");
        }
    } catch (NotFoundException e) {
        throw new IllegalArgumentException("Function " + functionName + " can't be found by javaassist.");
    }
    final String className = formatName(metricsName);
    /**
     * Check whether the metrics class is already defined or not
     */
    try {
        CtClass existingMetric = classPool.get(METER_CLASS_PACKAGE + className);
        if (existingMetric.getSuperclass() != parentClass || type != meterPrototypes.get(metricsName).getScopeType()) {
            throw new IllegalArgumentException(metricsName + " has been defined, but calculate function or/are scope type is/are different.");
        }
        log.info("Metric {} is already defined, so skip the metric creation.", metricsName);
        return;
    } catch (NotFoundException e) {
    }
    CtClass metricsClass = classPool.makeClass(METER_CLASS_PACKAGE + className, parentClass);
    /**
     * Create empty construct
     */
    try {
        CtConstructor defaultConstructor = CtNewConstructor.make("public " + className + "() {}", metricsClass);
        metricsClass.addConstructor(defaultConstructor);
    } catch (CannotCompileException e) {
        log.error("Can't add empty constructor in " + className + ".", e);
        throw new UnexpectedException(e.getMessage(), e);
    }
    /**
     * Generate `AcceptableValue<T> createNew()` method.
     */
    try {
        metricsClass.addMethod(CtNewMethod.make("" + "public org.apache.skywalking.oap.server.core.analysis.meter.function.AcceptableValue createNew() {" + "    org.apache.skywalking.oap.server.core.analysis.meter.function.AcceptableValue meterVar = new " + METER_CLASS_PACKAGE + className + "();" + "    ((org.apache.skywalking.oap.server.core.analysis.meter.Meter)meterVar).initMeta(\"" + metricsName + "\", " + type.getScopeId() + ");" + "    return meterVar;" + " }", metricsClass));
    } catch (CannotCompileException e) {
        log.error("Can't generate createNew method for " + className + ".", e);
        throw new UnexpectedException(e.getMessage(), e);
    }
    Class targetClass;
    try {
        if (SystemUtils.isJavaVersionAtMost(JavaVersion.JAVA_1_8)) {
            targetClass = metricsClass.toClass(MeterSystem.class.getClassLoader(), null);
        } else {
            targetClass = metricsClass.toClass(MeterClassPackageHolder.class);
        }
        AcceptableValue prototype = (AcceptableValue) targetClass.newInstance();
        meterPrototypes.put(metricsName, new MeterDefinition(type, prototype, dataType));
        log.debug("Generate metrics class, " + metricsClass.getName());
        MetricsStreamProcessor.getInstance().create(manager, new StreamDefinition(metricsName, type.getScopeId(), prototype.builder(), MetricsStreamProcessor.class), targetClass);
    } catch (CannotCompileException | IllegalAccessException | InstantiationException | StorageException e) {
        log.error("Can't compile/load/init " + className + ".", e);
        throw new UnexpectedException(e.getMessage(), e);
    }
}
Also used : UnexpectedException(org.apache.skywalking.oap.server.core.UnexpectedException) StreamDefinition(org.apache.skywalking.oap.server.core.analysis.StreamDefinition) NotFoundException(javassist.NotFoundException) CannotCompileException(javassist.CannotCompileException) MetricsStreamProcessor(org.apache.skywalking.oap.server.core.analysis.worker.MetricsStreamProcessor) CtConstructor(javassist.CtConstructor) ParameterizedType(java.lang.reflect.ParameterizedType) CtClass(javassist.CtClass) MeterClassPackageHolder(org.apache.skywalking.oap.server.core.analysis.meter.dynamic.MeterClassPackageHolder) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) Metrics(org.apache.skywalking.oap.server.core.analysis.metrics.Metrics) AcceptableValue(org.apache.skywalking.oap.server.core.analysis.meter.function.AcceptableValue) CtClass(javassist.CtClass) StorageException(org.apache.skywalking.oap.server.core.storage.StorageException)

Example 3 with StorageException

use of org.apache.skywalking.oap.server.core.storage.StorageException in project incubator-skywalking by apache.

the class H2StorageProvider method start.

@Override
public void start() throws ServiceNotProvidedException, ModuleStartException {
    final ConfigService configService = getManager().find(CoreModule.NAME).provider().getService(ConfigService.class);
    MetricsCreator metricCreator = getManager().find(TelemetryModule.NAME).provider().getService(MetricsCreator.class);
    HealthCheckMetrics healthChecker = metricCreator.createHealthCheckerGauge("storage_h2", MetricsTag.EMPTY_KEY, MetricsTag.EMPTY_VALUE);
    h2Client.registerChecker(healthChecker);
    try {
        h2Client.connect();
        H2TableInstaller installer = new H2TableInstaller(h2Client, getManager());
        getManager().find(CoreModule.NAME).provider().getService(ModelCreator.class).addModelListener(installer);
    } catch (StorageException e) {
        throw new ModuleStartException(e.getMessage(), e);
    }
}
Also used : ConfigService(org.apache.skywalking.oap.server.core.config.ConfigService) H2TableInstaller(org.apache.skywalking.oap.server.storage.plugin.jdbc.h2.dao.H2TableInstaller) HealthCheckMetrics(org.apache.skywalking.oap.server.telemetry.api.HealthCheckMetrics) ModuleStartException(org.apache.skywalking.oap.server.library.module.ModuleStartException) MetricsCreator(org.apache.skywalking.oap.server.telemetry.api.MetricsCreator) ModelCreator(org.apache.skywalking.oap.server.core.storage.model.ModelCreator) StorageException(org.apache.skywalking.oap.server.core.storage.StorageException)

Example 4 with StorageException

use of org.apache.skywalking.oap.server.core.storage.StorageException in project incubator-skywalking by apache.

the class StorageEsInstaller method createTimeSeriesTable.

private void createTimeSeriesTable(Model model) throws StorageException {
    ElasticSearchClient esClient = (ElasticSearchClient) client;
    String tableName = IndexController.INSTANCE.getTableName(model);
    Map<String, Object> settings = createSetting(model);
    Mappings mapping = createMapping(model);
    String indexName = TimeSeriesUtils.latestWriteIndexName(model);
    try {
        boolean shouldUpdateTemplate = !esClient.isExistsTemplate(tableName);
        shouldUpdateTemplate = shouldUpdateTemplate || !structures.containsStructure(tableName, mapping);
        if (shouldUpdateTemplate) {
            structures.putStructure(tableName, mapping);
            boolean isAcknowledged = esClient.createOrUpdateTemplate(tableName, settings, structures.getMapping(tableName), config.getIndexTemplateOrder());
            log.info("create {} index template finished, isAcknowledged: {}", tableName, isAcknowledged);
            if (!isAcknowledged) {
                throw new IOException("create " + tableName + " index template failure, ");
            }
        }
        if (esClient.isExistsIndex(indexName)) {
            Mappings historyMapping = esClient.getIndex(indexName).map(Index::getMappings).orElseGet(Mappings::new);
            Mappings appendMapping = structures.diffStructure(tableName, historyMapping);
            if (appendMapping.getProperties() != null && !appendMapping.getProperties().isEmpty()) {
                boolean isAcknowledged = esClient.updateIndexMapping(indexName, appendMapping);
                log.info("update {} index finished, isAcknowledged: {}, append mappings: {}", indexName, isAcknowledged, appendMapping);
                if (!isAcknowledged) {
                    throw new StorageException("update " + indexName + " time series index failure");
                }
            }
        } else {
            boolean isAcknowledged = esClient.createIndex(indexName);
            log.info("create {} index finished, isAcknowledged: {}", indexName, isAcknowledged);
            if (!isAcknowledged) {
                throw new StorageException("create " + indexName + " time series index failure");
            }
        }
    } catch (IOException e) {
        throw new StorageException("cannot create " + tableName + " index template", e);
    }
}
Also used : Mappings(org.apache.skywalking.library.elasticsearch.response.Mappings) ElasticSearchClient(org.apache.skywalking.oap.server.library.client.elasticsearch.ElasticSearchClient) IOException(java.io.IOException) StorageException(org.apache.skywalking.oap.server.core.storage.StorageException)

Example 5 with StorageException

use of org.apache.skywalking.oap.server.core.storage.StorageException in project skywalking by apache.

the class MySQLTableInstaller method isExists.

@Override
protected boolean isExists(Model model) throws StorageException {
    TableMetaInfo.addModel(model);
    JDBCHikariCPClient h2Client = (JDBCHikariCPClient) client;
    try (Connection conn = h2Client.getConnection()) {
        try (ResultSet rset = conn.getMetaData().getTables(conn.getCatalog(), null, model.getName(), null)) {
            if (rset.next()) {
                return true;
            }
        }
    } catch (SQLException | JDBCClientException e) {
        throw new StorageException(e.getMessage(), e);
    }
    return false;
}
Also used : JDBCClientException(org.apache.skywalking.oap.server.library.client.jdbc.JDBCClientException) SQLException(java.sql.SQLException) JDBCHikariCPClient(org.apache.skywalking.oap.server.library.client.jdbc.hikaricp.JDBCHikariCPClient) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) StorageException(org.apache.skywalking.oap.server.core.storage.StorageException)

Aggregations

StorageException (org.apache.skywalking.oap.server.core.storage.StorageException)30 ModuleStartException (org.apache.skywalking.oap.server.library.module.ModuleStartException)16 ModelCreator (org.apache.skywalking.oap.server.core.storage.model.ModelCreator)12 ConfigService (org.apache.skywalking.oap.server.core.config.ConfigService)10 IOException (java.io.IOException)8 HealthCheckMetrics (org.apache.skywalking.oap.server.telemetry.api.HealthCheckMetrics)6 MetricsCreator (org.apache.skywalking.oap.server.telemetry.api.MetricsCreator)6 Connection (java.sql.Connection)4 SQLException (java.sql.SQLException)4 Mappings (org.apache.skywalking.library.elasticsearch.response.Mappings)4 MetricsStreamProcessor (org.apache.skywalking.oap.server.core.analysis.worker.MetricsStreamProcessor)4 ElasticSearchClient (org.apache.skywalking.oap.server.library.client.elasticsearch.ElasticSearchClient)4 JDBCClientException (org.apache.skywalking.oap.server.library.client.jdbc.JDBCClientException)4 JDBCHikariCPClient (org.apache.skywalking.oap.server.library.client.jdbc.hikaricp.JDBCHikariCPClient)4 MySQLTableInstaller (org.apache.skywalking.oap.server.storage.plugin.jdbc.mysql.MySQLTableInstaller)3 FileNotFoundException (java.io.FileNotFoundException)2 ParameterizedType (java.lang.reflect.ParameterizedType)2 Type (java.lang.reflect.Type)2 ResultSet (java.sql.ResultSet)2 CannotCompileException (javassist.CannotCompileException)2