diff --git a/apm-collector/apm-collector-core/src/main/java/org/skywalking/apm/collector/core/data/CommonTable.java b/apm-collector/apm-collector-core/src/main/java/org/skywalking/apm/collector/core/data/CommonTable.java index 5a173026c..f62e96535 100644 --- a/apm-collector/apm-collector-core/src/main/java/org/skywalking/apm/collector/core/data/CommonTable.java +++ b/apm-collector/apm-collector-core/src/main/java/org/skywalking/apm/collector/core/data/CommonTable.java @@ -24,6 +24,5 @@ package org.skywalking.apm.collector.core.data; public abstract class CommonTable { public static final String TABLE_TYPE = "type"; public static final String COLUMN_ID = "id"; - public static final String COLUMN_AGG = "agg"; public static final String COLUMN_TIME_BUCKET = "time_bucket"; } diff --git a/apm-collector/apm-collector-storage/collector-storage-define/src/main/java/org/skywalking/apm/collector/storage/StorageModule.java b/apm-collector/apm-collector-storage/collector-storage-define/src/main/java/org/skywalking/apm/collector/storage/StorageModule.java index 99a1c5840..636901d99 100644 --- a/apm-collector/apm-collector-storage/collector-storage-define/src/main/java/org/skywalking/apm/collector/storage/StorageModule.java +++ b/apm-collector/apm-collector-storage/collector-storage-define/src/main/java/org/skywalking/apm/collector/storage/StorageModule.java @@ -22,6 +22,7 @@ import java.util.ArrayList; import java.util.List; import org.skywalking.apm.collector.core.module.Module; import org.skywalking.apm.collector.storage.base.dao.IBatchDAO; +import org.skywalking.apm.collector.storage.dao.IAlertingListPersistenceDAO; import org.skywalking.apm.collector.storage.dao.IApplicationCacheDAO; import org.skywalking.apm.collector.storage.dao.IApplicationComponentPersistenceDAO; import org.skywalking.apm.collector.storage.dao.IApplicationComponentUIDAO; @@ -78,6 +79,7 @@ public class StorageModule extends Module { addRegisterDAO(classes); addPersistenceDAO(classes); addUiDAO(classes); + addAlertingDAO(classes); return classes.toArray(new Class[] {}); } @@ -133,4 +135,8 @@ public class StorageModule extends Module { classes.add(IServiceEntryUIDAO.class); classes.add(IServiceReferenceUIDAO.class); } + + private void addAlertingDAO(List classes) { + classes.add(IAlertingListPersistenceDAO.class); + } } diff --git a/apm-collector/apm-collector-storage/collector-storage-define/src/main/java/org/skywalking/apm/collector/storage/dao/IAlertingListPersistenceDAO.java b/apm-collector/apm-collector-storage/collector-storage-define/src/main/java/org/skywalking/apm/collector/storage/dao/IAlertingListPersistenceDAO.java new file mode 100644 index 000000000..1001102d1 --- /dev/null +++ b/apm-collector/apm-collector-storage/collector-storage-define/src/main/java/org/skywalking/apm/collector/storage/dao/IAlertingListPersistenceDAO.java @@ -0,0 +1,28 @@ +/* + * Copyright 2017, OpenSkywalking Organization All rights reserved. + * + * Licensed 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. + * + * Project repository: https://github.com/OpenSkywalking/skywalking + */ + +package org.skywalking.apm.collector.storage.dao; + +import org.skywalking.apm.collector.core.data.Data; +import org.skywalking.apm.collector.storage.base.dao.IPersistenceDAO; + +/** + * @author peng-yongsheng + */ +public interface IAlertingListPersistenceDAO extends IPersistenceDAO { +} diff --git a/apm-collector/apm-collector-storage/collector-storage-define/src/main/java/org/skywalking/apm/collector/storage/table/alerting/AlertingList.java b/apm-collector/apm-collector-storage/collector-storage-define/src/main/java/org/skywalking/apm/collector/storage/table/alerting/AlertingList.java new file mode 100644 index 000000000..30c9a2d7b --- /dev/null +++ b/apm-collector/apm-collector-storage/collector-storage-define/src/main/java/org/skywalking/apm/collector/storage/table/alerting/AlertingList.java @@ -0,0 +1,113 @@ +/* + * Copyright 2017, OpenSkywalking Organization All rights reserved. + * + * Licensed 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. + * + * Project repository: https://github.com/OpenSkywalking/skywalking + */ + +package org.skywalking.apm.collector.storage.table.alerting; + +import org.skywalking.apm.collector.core.data.Column; +import org.skywalking.apm.collector.core.data.Data; +import org.skywalking.apm.collector.core.data.operator.CoverOperation; +import org.skywalking.apm.collector.core.data.operator.NonOperation; + +/** + * @author peng-yongsheng + */ +public class AlertingList extends Data { + + private static final Column[] STRING_COLUMNS = { + new Column(AlertingListTable.COLUMN_ID, new NonOperation()), + }; + + private static final Column[] LONG_COLUMNS = { + new Column(AlertingListTable.COLUMN_FIRST_TIME_BUCKET, new NonOperation()), + new Column(AlertingListTable.COLUMN_LAST_TIME_BUCKET, new CoverOperation()), + }; + private static final Column[] DOUBLE_COLUMNS = {}; + + private static final Column[] INTEGER_COLUMNS = { + new Column(AlertingListTable.COLUMN_LAYER, new CoverOperation()), + new Column(AlertingListTable.COLUMN_LAYER_ID, new CoverOperation()), + new Column(AlertingListTable.COLUMN_EXPECTED, new CoverOperation()), + new Column(AlertingListTable.COLUMN_ACTUAL, new CoverOperation()), + }; + + private static final Column[] BOOLEAN_COLUMNS = { + new Column(AlertingListTable.COLUMN_VALID, new CoverOperation()), + }; + + private static final Column[] BYTE_COLUMNS = {}; + + public AlertingList(String id) { + super(id, STRING_COLUMNS, LONG_COLUMNS, DOUBLE_COLUMNS, INTEGER_COLUMNS, BOOLEAN_COLUMNS, BYTE_COLUMNS); + } + + public Integer getLayer() { + return getDataInteger(0); + } + + public void setLayer(Integer layer) { + setDataInteger(0, layer); + } + + public Integer getLayerId() { + return getDataInteger(1); + } + + public void setLayerId(Integer layerId) { + setDataInteger(1, layerId); + } + + public Integer getExpected() { + return getDataInteger(2); + } + + public void setExpected(Integer expected) { + setDataInteger(2, expected); + } + + public Integer getActual() { + return getDataInteger(3); + } + + public void setActual(Integer actual) { + setDataInteger(3, actual); + } + + public Long getFirstTimeBucket() { + return getDataLong(0); + } + + public void setFirstTimeBucket(Long firstTimeBucket) { + setDataLong(0, firstTimeBucket); + } + + public Long getLastTimeBucket() { + return getDataLong(1); + } + + public void setLastTimeBucket(Long lastTimeBucket) { + setDataLong(1, lastTimeBucket); + } + + public Boolean getValid() { + return getDataBoolean(0); + } + + public void setValid(Boolean valid) { + setDataBoolean(0, valid); + } +} diff --git a/apm-collector/apm-collector-storage/collector-storage-define/src/main/java/org/skywalking/apm/collector/storage/table/alerting/AlertingListTable.java b/apm-collector/apm-collector-storage/collector-storage-define/src/main/java/org/skywalking/apm/collector/storage/table/alerting/AlertingListTable.java new file mode 100644 index 000000000..4ee1ab703 --- /dev/null +++ b/apm-collector/apm-collector-storage/collector-storage-define/src/main/java/org/skywalking/apm/collector/storage/table/alerting/AlertingListTable.java @@ -0,0 +1,35 @@ +/* + * Copyright 2017, OpenSkywalking Organization All rights reserved. + * + * Licensed 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. + * + * Project repository: https://github.com/OpenSkywalking/skywalking + */ + +package org.skywalking.apm.collector.storage.table.alerting; + +import org.skywalking.apm.collector.core.data.CommonTable; + +/** + * @author peng-yongsheng + */ +public class AlertingListTable extends CommonTable { + public static final String TABLE = "alerting_list"; + public static final String COLUMN_LAYER = "layer"; + public static final String COLUMN_LAYER_ID = "layer_id"; + public static final String COLUMN_FIRST_TIME_BUCKET = "first_time_bucket"; + public static final String COLUMN_LAST_TIME_BUCKET = "last_time_bucket"; + public static final String COLUMN_EXPECTED = "expected"; + public static final String COLUMN_ACTUAL = "actual"; + public static final String COLUMN_VALID = "valid"; +} diff --git a/apm-collector/apm-collector-storage/collector-storage-es-provider/src/main/java/org/skywalking/apm/collector/storage/es/StorageModuleEsProvider.java b/apm-collector/apm-collector-storage/collector-storage-es-provider/src/main/java/org/skywalking/apm/collector/storage/es/StorageModuleEsProvider.java index bcb6a87ba..d03b3d8b0 100644 --- a/apm-collector/apm-collector-storage/collector-storage-es-provider/src/main/java/org/skywalking/apm/collector/storage/es/StorageModuleEsProvider.java +++ b/apm-collector/apm-collector-storage/collector-storage-es-provider/src/main/java/org/skywalking/apm/collector/storage/es/StorageModuleEsProvider.java @@ -31,6 +31,7 @@ import org.skywalking.apm.collector.core.module.ServiceNotProvidedException; import org.skywalking.apm.collector.storage.StorageException; import org.skywalking.apm.collector.storage.StorageModule; import org.skywalking.apm.collector.storage.base.dao.IBatchDAO; +import org.skywalking.apm.collector.storage.dao.IAlertingListPersistenceDAO; import org.skywalking.apm.collector.storage.dao.IApplicationCacheDAO; import org.skywalking.apm.collector.storage.dao.IApplicationComponentPersistenceDAO; import org.skywalking.apm.collector.storage.dao.IApplicationComponentUIDAO; @@ -69,6 +70,7 @@ import org.skywalking.apm.collector.storage.dao.IServiceReferenceMetricPersisten import org.skywalking.apm.collector.storage.dao.IServiceReferenceUIDAO; import org.skywalking.apm.collector.storage.es.base.dao.BatchEsDAO; import org.skywalking.apm.collector.storage.es.base.define.ElasticSearchStorageInstaller; +import org.skywalking.apm.collector.storage.es.dao.AlertingListEsPersistenceDAO; import org.skywalking.apm.collector.storage.es.dao.ApplicationComponentEsPersistenceDAO; import org.skywalking.apm.collector.storage.es.dao.ApplicationComponentEsUIDAO; import org.skywalking.apm.collector.storage.es.dao.ApplicationEsCacheDAO; @@ -145,6 +147,7 @@ public class StorageModuleEsProvider extends ModuleProvider { registerRegisterDAO(); registerPersistenceDAO(); registerUiDAO(); + registerAlertingDAO(); } @Override public void start(Properties config) throws ServiceNotProvidedException { @@ -230,4 +233,8 @@ public class StorageModuleEsProvider extends ModuleProvider { this.registerServiceImplementation(IServiceEntryUIDAO.class, new ServiceEntryEsUIDAO(elasticSearchClient)); this.registerServiceImplementation(IServiceReferenceUIDAO.class, new ServiceReferenceEsUIDAO(elasticSearchClient)); } -} + + private void registerAlertingDAO() throws ServiceNotProvidedException { + this.registerServiceImplementation(IAlertingListPersistenceDAO.class, new AlertingListEsPersistenceDAO(elasticSearchClient)); + } +} \ No newline at end of file diff --git a/apm-collector/apm-collector-storage/collector-storage-es-provider/src/main/java/org/skywalking/apm/collector/storage/es/dao/AlertingListEsPersistenceDAO.java b/apm-collector/apm-collector-storage/collector-storage-es-provider/src/main/java/org/skywalking/apm/collector/storage/es/dao/AlertingListEsPersistenceDAO.java new file mode 100644 index 000000000..e5c870148 --- /dev/null +++ b/apm-collector/apm-collector-storage/collector-storage-es-provider/src/main/java/org/skywalking/apm/collector/storage/es/dao/AlertingListEsPersistenceDAO.java @@ -0,0 +1,103 @@ +/* + * Copyright 2017, OpenSkywalking Organization All rights reserved. + * + * Licensed 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. + * + * Project repository: https://github.com/OpenSkywalking/skywalking + */ + +package org.skywalking.apm.collector.storage.es.dao; + +import java.util.HashMap; +import java.util.Map; +import org.elasticsearch.action.get.GetResponse; +import org.elasticsearch.action.index.IndexRequestBuilder; +import org.elasticsearch.action.update.UpdateRequestBuilder; +import org.elasticsearch.index.query.QueryBuilders; +import org.elasticsearch.index.reindex.BulkByScrollResponse; +import org.skywalking.apm.collector.client.elasticsearch.ElasticSearchClient; +import org.skywalking.apm.collector.core.util.TimeBucketUtils; +import org.skywalking.apm.collector.storage.dao.IAlertingListPersistenceDAO; +import org.skywalking.apm.collector.storage.es.base.dao.EsDAO; +import org.skywalking.apm.collector.storage.table.alerting.AlertingList; +import org.skywalking.apm.collector.storage.table.alerting.AlertingListTable; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * @author peng-yongsheng + */ +public class AlertingListEsPersistenceDAO extends EsDAO implements IAlertingListPersistenceDAO { + + private final Logger logger = LoggerFactory.getLogger(AlertingListEsPersistenceDAO.class); + + public AlertingListEsPersistenceDAO(ElasticSearchClient client) { + super(client); + } + + @Override public AlertingList get(String id) { + GetResponse getResponse = getClient().prepareGet(AlertingListTable.TABLE, id).get(); + if (getResponse.isExists()) { + AlertingList alertingList = new AlertingList(id); + Map source = getResponse.getSource(); + alertingList.setLayer(((Number)source.get(AlertingListTable.COLUMN_LAYER)).intValue()); + alertingList.setLayerId(((Number)source.get(AlertingListTable.COLUMN_LAYER_ID)).intValue()); + alertingList.setFirstTimeBucket(((Number)source.get(AlertingListTable.COLUMN_FIRST_TIME_BUCKET)).longValue()); + alertingList.setLastTimeBucket(((Number)source.get(AlertingListTable.COLUMN_LAST_TIME_BUCKET)).longValue()); + alertingList.setExpected(((Number)source.get(AlertingListTable.COLUMN_EXPECTED)).intValue()); + alertingList.setActual(((Number)source.get(AlertingListTable.COLUMN_ACTUAL)).intValue()); + alertingList.setValid((Boolean)source.get(AlertingListTable.COLUMN_VALID)); + return alertingList; + } else { + return null; + } + } + + @Override public IndexRequestBuilder prepareBatchInsert(AlertingList data) { + Map source = new HashMap<>(); + source.put(AlertingListTable.COLUMN_LAYER, data.getLayer()); + source.put(AlertingListTable.COLUMN_LAYER_ID, data.getLayerId()); + source.put(AlertingListTable.COLUMN_FIRST_TIME_BUCKET, data.getFirstTimeBucket()); + source.put(AlertingListTable.COLUMN_LAST_TIME_BUCKET, data.getLastTimeBucket()); + source.put(AlertingListTable.COLUMN_EXPECTED, data.getExpected()); + source.put(AlertingListTable.COLUMN_ACTUAL, data.getActual()); + source.put(AlertingListTable.COLUMN_VALID, data.getValid()); + + return getClient().prepareIndex(AlertingListTable.TABLE, data.getId()).setSource(source); + } + + @Override public UpdateRequestBuilder prepareBatchUpdate(AlertingList data) { + Map source = new HashMap<>(); + source.put(AlertingListTable.COLUMN_LAYER, data.getLayer()); + source.put(AlertingListTable.COLUMN_LAYER_ID, data.getLayerId()); + source.put(AlertingListTable.COLUMN_FIRST_TIME_BUCKET, data.getFirstTimeBucket()); + source.put(AlertingListTable.COLUMN_LAST_TIME_BUCKET, data.getLastTimeBucket()); + source.put(AlertingListTable.COLUMN_EXPECTED, data.getExpected()); + source.put(AlertingListTable.COLUMN_ACTUAL, data.getActual()); + source.put(AlertingListTable.COLUMN_VALID, data.getValid()); + + return getClient().prepareUpdate(AlertingListTable.TABLE, data.getId()).setDoc(source); + } + + @Override public void deleteHistory(Long startTimestamp, Long endTimestamp) { + long startTimeBucket = TimeBucketUtils.INSTANCE.getMinuteTimeBucket(startTimestamp); + long endTimeBucket = TimeBucketUtils.INSTANCE.getMinuteTimeBucket(endTimestamp); + BulkByScrollResponse response = getClient().prepareDelete() + .filter(QueryBuilders.rangeQuery(AlertingListTable.COLUMN_TIME_BUCKET).gte(startTimeBucket).lte(endTimeBucket)) + .source(AlertingListTable.TABLE) + .get(); + + long deleted = response.getDeleted(); + logger.info("Delete {} rows history from {} index.", deleted, AlertingListTable.TABLE); + } +} diff --git a/apm-collector/apm-collector-storage/collector-storage-es-provider/src/main/java/org/skywalking/apm/collector/storage/es/define/AlertingListEsTableDefine.java b/apm-collector/apm-collector-storage/collector-storage-es-provider/src/main/java/org/skywalking/apm/collector/storage/es/define/AlertingListEsTableDefine.java new file mode 100644 index 000000000..303f18896 --- /dev/null +++ b/apm-collector/apm-collector-storage/collector-storage-es-provider/src/main/java/org/skywalking/apm/collector/storage/es/define/AlertingListEsTableDefine.java @@ -0,0 +1,47 @@ +/* + * Copyright 2017, OpenSkywalking Organization All rights reserved. + * + * Licensed 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. + * + * Project repository: https://github.com/OpenSkywalking/skywalking + */ + +package org.skywalking.apm.collector.storage.es.define; + +import org.skywalking.apm.collector.storage.es.base.define.ElasticSearchColumnDefine; +import org.skywalking.apm.collector.storage.es.base.define.ElasticSearchTableDefine; +import org.skywalking.apm.collector.storage.table.alerting.AlertingListTable; + +/** + * @author peng-yongsheng + */ +public class AlertingListEsTableDefine extends ElasticSearchTableDefine { + + public AlertingListEsTableDefine() { + super(AlertingListTable.TABLE); + } + + @Override public int refreshInterval() { + return 2; + } + + @Override public void initialize() { + addColumn(new ElasticSearchColumnDefine(AlertingListTable.COLUMN_LAYER, ElasticSearchColumnDefine.Type.Integer.name())); + addColumn(new ElasticSearchColumnDefine(AlertingListTable.COLUMN_LAYER_ID, ElasticSearchColumnDefine.Type.Integer.name())); + addColumn(new ElasticSearchColumnDefine(AlertingListTable.COLUMN_FIRST_TIME_BUCKET, ElasticSearchColumnDefine.Type.Long.name())); + addColumn(new ElasticSearchColumnDefine(AlertingListTable.COLUMN_LAST_TIME_BUCKET, ElasticSearchColumnDefine.Type.Long.name())); + addColumn(new ElasticSearchColumnDefine(AlertingListTable.COLUMN_EXPECTED, ElasticSearchColumnDefine.Type.Integer.name())); + addColumn(new ElasticSearchColumnDefine(AlertingListTable.COLUMN_ACTUAL, ElasticSearchColumnDefine.Type.Integer.name())); + addColumn(new ElasticSearchColumnDefine(AlertingListTable.COLUMN_VALID, ElasticSearchColumnDefine.Type.Boolean.name())); + } +} diff --git a/apm-collector/apm-collector-storage/collector-storage-es-provider/src/main/resources/META-INF/defines/storage.define b/apm-collector/apm-collector-storage/collector-storage-es-provider/src/main/resources/META-INF/defines/storage.define index 62d47b37e..22d0931be 100644 --- a/apm-collector/apm-collector-storage/collector-storage-es-provider/src/main/resources/META-INF/defines/storage.define +++ b/apm-collector/apm-collector-storage/collector-storage-es-provider/src/main/resources/META-INF/defines/storage.define @@ -14,4 +14,5 @@ org.skywalking.apm.collector.storage.es.define.SegmentCostEsTableDefine org.skywalking.apm.collector.storage.es.define.SegmentEsTableDefine org.skywalking.apm.collector.storage.es.define.ServiceEntryEsTableDefine org.skywalking.apm.collector.storage.es.define.ServiceMetricEsTableDefine -org.skywalking.apm.collector.storage.es.define.ServiceReferenceMetricEsTableDefine \ No newline at end of file +org.skywalking.apm.collector.storage.es.define.ServiceReferenceMetricEsTableDefine +org.skywalking.apm.collector.storage.es.define.AlertingListEsTableDefine \ No newline at end of file diff --git a/apm-collector/apm-collector-storage/collector-storage-h2-provider/src/main/java/org/skywalking/apm/collector/storage/h2/StorageModuleH2Provider.java b/apm-collector/apm-collector-storage/collector-storage-h2-provider/src/main/java/org/skywalking/apm/collector/storage/h2/StorageModuleH2Provider.java index 3f6467505..e2d41f367 100644 --- a/apm-collector/apm-collector-storage/collector-storage-h2-provider/src/main/java/org/skywalking/apm/collector/storage/h2/StorageModuleH2Provider.java +++ b/apm-collector/apm-collector-storage/collector-storage-h2-provider/src/main/java/org/skywalking/apm/collector/storage/h2/StorageModuleH2Provider.java @@ -26,6 +26,7 @@ import org.skywalking.apm.collector.core.module.ServiceNotProvidedException; import org.skywalking.apm.collector.storage.StorageException; import org.skywalking.apm.collector.storage.StorageModule; import org.skywalking.apm.collector.storage.base.dao.IBatchDAO; +import org.skywalking.apm.collector.storage.dao.IAlertingListPersistenceDAO; import org.skywalking.apm.collector.storage.dao.IApplicationCacheDAO; import org.skywalking.apm.collector.storage.dao.IApplicationComponentPersistenceDAO; import org.skywalking.apm.collector.storage.dao.IApplicationComponentUIDAO; @@ -64,6 +65,7 @@ import org.skywalking.apm.collector.storage.dao.IServiceReferenceMetricPersisten import org.skywalking.apm.collector.storage.dao.IServiceReferenceUIDAO; import org.skywalking.apm.collector.storage.h2.base.dao.BatchH2DAO; import org.skywalking.apm.collector.storage.h2.base.define.H2StorageInstaller; +import org.skywalking.apm.collector.storage.h2.dao.AlertingListH2PersistenceDAO; import org.skywalking.apm.collector.storage.h2.dao.ApplicationComponentH2PersistenceDAO; import org.skywalking.apm.collector.storage.h2.dao.ApplicationComponentH2UIDAO; import org.skywalking.apm.collector.storage.h2.dao.ApplicationH2CacheDAO; @@ -135,6 +137,7 @@ public class StorageModuleH2Provider extends ModuleProvider { registerRegisterDAO(); registerPersistenceDAO(); registerUiDAO(); + registerAlertingDAO(); } @Override public void start(Properties config) throws ServiceNotProvidedException { @@ -207,4 +210,8 @@ public class StorageModuleH2Provider extends ModuleProvider { this.registerServiceImplementation(IServiceEntryUIDAO.class, new ServiceEntryH2UIDAO(h2Client)); this.registerServiceImplementation(IServiceReferenceUIDAO.class, new ServiceReferenceH2UIDAO(h2Client)); } + + private void registerAlertingDAO() throws ServiceNotProvidedException { + this.registerServiceImplementation(IAlertingListPersistenceDAO.class, new AlertingListH2PersistenceDAO(h2Client)); + } } diff --git a/apm-collector/apm-collector-storage/collector-storage-h2-provider/src/main/java/org/skywalking/apm/collector/storage/h2/dao/AlertingListH2PersistenceDAO.java b/apm-collector/apm-collector-storage/collector-storage-h2-provider/src/main/java/org/skywalking/apm/collector/storage/h2/dao/AlertingListH2PersistenceDAO.java new file mode 100644 index 000000000..cb27c8a2a --- /dev/null +++ b/apm-collector/apm-collector-storage/collector-storage-h2-provider/src/main/java/org/skywalking/apm/collector/storage/h2/dao/AlertingListH2PersistenceDAO.java @@ -0,0 +1,112 @@ +/* + * Copyright 2017, OpenSkywalking Organization All rights reserved. + * + * Licensed 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. + * + * Project repository: https://github.com/OpenSkywalking/skywalking + */ + +package org.skywalking.apm.collector.storage.h2.dao; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.skywalking.apm.collector.client.h2.H2Client; +import org.skywalking.apm.collector.client.h2.H2ClientException; +import org.skywalking.apm.collector.storage.base.sql.SqlBuilder; +import org.skywalking.apm.collector.storage.dao.IAlertingListPersistenceDAO; +import org.skywalking.apm.collector.storage.h2.base.dao.H2DAO; +import org.skywalking.apm.collector.storage.h2.base.define.H2SqlEntity; +import org.skywalking.apm.collector.storage.table.alerting.AlertingList; +import org.skywalking.apm.collector.storage.table.alerting.AlertingListTable; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * @author peng-yongsheng + */ +public class AlertingListH2PersistenceDAO extends H2DAO implements IAlertingListPersistenceDAO { + + private final Logger logger = LoggerFactory.getLogger(AlertingListH2PersistenceDAO.class); + + private static final String GET_SQL = "select * from {0} where {1} = ?"; + + public AlertingListH2PersistenceDAO(H2Client client) { + super(client); + } + + @Override public AlertingList get(String id) { + H2Client client = getClient(); + String sql = SqlBuilder.buildSql(GET_SQL, AlertingListTable.TABLE, AlertingListTable.COLUMN_ID); + Object[] params = new Object[] {id}; + try (ResultSet rs = client.executeQuery(sql, params)) { + if (rs.next()) { + AlertingList alertingList = new AlertingList(id); + alertingList.setLayer(rs.getInt(AlertingListTable.COLUMN_LAYER)); + alertingList.setLayerId(rs.getInt(AlertingListTable.COLUMN_LAYER_ID)); + alertingList.setFirstTimeBucket(rs.getLong(AlertingListTable.COLUMN_FIRST_TIME_BUCKET)); + alertingList.setLastTimeBucket(rs.getLong(AlertingListTable.COLUMN_LAST_TIME_BUCKET)); + alertingList.setExpected(rs.getInt(AlertingListTable.COLUMN_EXPECTED)); + alertingList.setActual(rs.getInt(AlertingListTable.COLUMN_ACTUAL)); + alertingList.setValid(rs.getBoolean(AlertingListTable.COLUMN_VALID)); + + return alertingList; + } + } catch (SQLException | H2ClientException e) { + logger.error(e.getMessage(), e); + } + return null; + } + + @Override public H2SqlEntity prepareBatchInsert(AlertingList data) { + Map source = new HashMap<>(); + H2SqlEntity entity = new H2SqlEntity(); + source.put(AlertingListTable.COLUMN_LAYER, data.getLayer()); + source.put(AlertingListTable.COLUMN_LAYER_ID, data.getLayerId()); + source.put(AlertingListTable.COLUMN_FIRST_TIME_BUCKET, data.getFirstTimeBucket()); + source.put(AlertingListTable.COLUMN_LAST_TIME_BUCKET, data.getLastTimeBucket()); + source.put(AlertingListTable.COLUMN_EXPECTED, data.getExpected()); + source.put(AlertingListTable.COLUMN_ACTUAL, data.getActual()); + source.put(AlertingListTable.COLUMN_VALID, data.getValid()); + + String sql = SqlBuilder.buildBatchInsertSql(AlertingListTable.TABLE, source.keySet()); + entity.setSql(sql); + entity.setParams(source.values().toArray(new Object[0])); + return entity; + } + + @Override public H2SqlEntity prepareBatchUpdate(AlertingList data) { + Map source = new HashMap<>(); + H2SqlEntity entity = new H2SqlEntity(); + source.put(AlertingListTable.COLUMN_LAYER, data.getLayer()); + source.put(AlertingListTable.COLUMN_LAYER_ID, data.getLayerId()); + source.put(AlertingListTable.COLUMN_FIRST_TIME_BUCKET, data.getFirstTimeBucket()); + source.put(AlertingListTable.COLUMN_LAST_TIME_BUCKET, data.getLastTimeBucket()); + source.put(AlertingListTable.COLUMN_EXPECTED, data.getExpected()); + source.put(AlertingListTable.COLUMN_ACTUAL, data.getActual()); + source.put(AlertingListTable.COLUMN_VALID, data.getValid()); + String sql = SqlBuilder.buildBatchUpdateSql(AlertingListTable.TABLE, source.keySet(), AlertingListTable.COLUMN_ID); + entity.setSql(sql); + List values = new ArrayList<>(source.values()); + values.add(data.getId()); + entity.setParams(values.toArray(new Object[0])); + return entity; + } + + @Override public void deleteHistory(Long startTimestamp, Long endTimestamp) { + + } +} diff --git a/apm-collector/apm-collector-storage/collector-storage-h2-provider/src/main/java/org/skywalking/apm/collector/storage/h2/define/AlertingListH2TableDefine.java b/apm-collector/apm-collector-storage/collector-storage-h2-provider/src/main/java/org/skywalking/apm/collector/storage/h2/define/AlertingListH2TableDefine.java new file mode 100644 index 000000000..34f6db68b --- /dev/null +++ b/apm-collector/apm-collector-storage/collector-storage-h2-provider/src/main/java/org/skywalking/apm/collector/storage/h2/define/AlertingListH2TableDefine.java @@ -0,0 +1,46 @@ +/* + * Copyright 2017, OpenSkywalking Organization All rights reserved. + * + * Licensed 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. + * + * Project repository: https://github.com/OpenSkywalking/skywalking + */ + +package org.skywalking.apm.collector.storage.h2.define; + +import org.skywalking.apm.collector.storage.h2.base.define.H2ColumnDefine; +import org.skywalking.apm.collector.storage.h2.base.define.H2TableDefine; +import org.skywalking.apm.collector.storage.table.alerting.AlertingListTable; +import org.skywalking.apm.collector.storage.table.application.ApplicationComponentTable; + +/** + * @author peng-yongsheng + */ +public class AlertingListH2TableDefine extends H2TableDefine { + + public AlertingListH2TableDefine() { + super(AlertingListTable.TABLE); + } + + @Override public void initialize() { + addColumn(new H2ColumnDefine(ApplicationComponentTable.COLUMN_ID, H2ColumnDefine.Type.Varchar.name())); + + addColumn(new H2ColumnDefine(AlertingListTable.COLUMN_LAYER, H2ColumnDefine.Type.Int.name())); + addColumn(new H2ColumnDefine(AlertingListTable.COLUMN_LAYER_ID, H2ColumnDefine.Type.Int.name())); + addColumn(new H2ColumnDefine(AlertingListTable.COLUMN_FIRST_TIME_BUCKET, H2ColumnDefine.Type.Bigint.name())); + addColumn(new H2ColumnDefine(AlertingListTable.COLUMN_LAST_TIME_BUCKET, H2ColumnDefine.Type.Bigint.name())); + addColumn(new H2ColumnDefine(AlertingListTable.COLUMN_EXPECTED, H2ColumnDefine.Type.Int.name())); + addColumn(new H2ColumnDefine(AlertingListTable.COLUMN_ACTUAL, H2ColumnDefine.Type.Int.name())); + addColumn(new H2ColumnDefine(AlertingListTable.COLUMN_VALID, H2ColumnDefine.Type.Boolean.name())); + } +} diff --git a/apm-collector/apm-collector-storage/collector-storage-h2-provider/src/main/resources/META-INF/defines/storage.define b/apm-collector/apm-collector-storage/collector-storage-h2-provider/src/main/resources/META-INF/defines/storage.define index c9fa6e5be..833365ed3 100644 --- a/apm-collector/apm-collector-storage/collector-storage-h2-provider/src/main/resources/META-INF/defines/storage.define +++ b/apm-collector/apm-collector-storage/collector-storage-h2-provider/src/main/resources/META-INF/defines/storage.define @@ -14,4 +14,5 @@ org.skywalking.apm.collector.storage.h2.define.SegmentCostH2TableDefine org.skywalking.apm.collector.storage.h2.define.SegmentH2TableDefine org.skywalking.apm.collector.storage.h2.define.ServiceEntryH2TableDefine org.skywalking.apm.collector.storage.h2.define.ServiceMetricH2TableDefine -org.skywalking.apm.collector.storage.h2.define.ServiceReferenceMetricH2TableDefine \ No newline at end of file +org.skywalking.apm.collector.storage.h2.define.ServiceReferenceMetricH2TableDefine +org.skywalking.apm.collector.storage.h2.define.AlertingListH2TableDefine \ No newline at end of file