From 594994ffaec99d3a18225426b932feefb256fd53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BD=AD=E5=8B=87=E5=8D=87=20pengys?= Date: Sun, 21 Jul 2019 13:56:12 +0800 Subject: [PATCH] 1. Provide InsertRequest and UpdateRequest interface for prepare persistence. (#3131) 2. Implement the ids query for H2 metrics DAO. --- .../worker/MetricsPersistentWorker.java | 2 +- .../oap/server/core/storage/IMetricsDAO.java | 7 ++-- .../oap/server/core/storage/StorageDAO.java | 4 +- .../elasticsearch/ElasticSearchClient.java | 8 ++-- .../ElasticSearchInsertRequest.java | 37 ++++++++++++++++++ .../ElasticSearchUpdateRequest.java | 36 ++++++++++++++++++ .../jdbc/hikaricp/JDBCHikariCPClient.java | 2 +- .../library/client/request/InsertRequest.java | 24 ++++++++++++ .../client/request/PrepareRequest.java | 24 ++++++++++++ .../library/client/request/UpdateRequest.java | 24 ++++++++++++ .../elasticsearch/base/MetricsEsDAO.java | 10 ++--- .../elasticsearch/base/StorageEsDAO.java | 6 +-- .../plugin/jdbc/ArrayParamBuilder.java | 35 +++++++++++++++++ .../storage/plugin/jdbc/SQLExecutor.java | 13 +++---- .../plugin/jdbc/h2/dao/H2MetricsDAO.java | 19 ++++++++-- .../plugin/jdbc/h2/dao/H2SQLExecutor.java | 33 ++++++++++++---- .../plugin/jdbc/h2/dao/H2StorageDAO.java | 7 ++-- .../jdbc/ArrayParamBuilderTestCase.java | 38 +++++++++++++++++++ 18 files changed, 286 insertions(+), 43 deletions(-) create mode 100644 oap-server/server-library/library-client/src/main/java/org/apache/skywalking/oap/server/library/client/elasticsearch/ElasticSearchInsertRequest.java create mode 100644 oap-server/server-library/library-client/src/main/java/org/apache/skywalking/oap/server/library/client/elasticsearch/ElasticSearchUpdateRequest.java create mode 100644 oap-server/server-library/library-client/src/main/java/org/apache/skywalking/oap/server/library/client/request/InsertRequest.java create mode 100644 oap-server/server-library/library-client/src/main/java/org/apache/skywalking/oap/server/library/client/request/PrepareRequest.java create mode 100644 oap-server/server-library/library-client/src/main/java/org/apache/skywalking/oap/server/library/client/request/UpdateRequest.java create mode 100644 oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/ArrayParamBuilder.java create mode 100644 oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/ArrayParamBuilderTestCase.java diff --git a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/analysis/worker/MetricsPersistentWorker.java b/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/analysis/worker/MetricsPersistentWorker.java index 066f70fdb..14135dc73 100644 --- a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/analysis/worker/MetricsPersistentWorker.java +++ b/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/analysis/worker/MetricsPersistentWorker.java @@ -40,7 +40,7 @@ public class MetricsPersistentWorker extends PersistenceWorker mergeDataCache; - private final IMetricsDAO metricsDAO; + private final IMetricsDAO metricsDAO; private final AbstractWorker nextAlarmWorker; private final AbstractWorker nextExportWorker; private final DataCarrier dataCarrier; diff --git a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/storage/IMetricsDAO.java b/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/storage/IMetricsDAO.java index 5c2e2462f..e67dfa80b 100644 --- a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/storage/IMetricsDAO.java +++ b/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/storage/IMetricsDAO.java @@ -22,15 +22,16 @@ import java.io.IOException; import java.util.Map; import org.apache.skywalking.oap.server.core.analysis.metrics.Metrics; import org.apache.skywalking.oap.server.core.storage.model.Model; +import org.apache.skywalking.oap.server.library.client.request.*; /** * @author peng-yongsheng */ -public interface IMetricsDAO extends DAO { +public interface IMetricsDAO extends DAO { Map get(Model model, Metrics[] metrics) throws IOException; - INSERT prepareBatchInsert(Model model, Metrics metrics) throws IOException; + InsertRequest prepareBatchInsert(Model model, Metrics metrics) throws IOException; - UPDATE prepareBatchUpdate(Model model, Metrics metrics) throws IOException; + UpdateRequest prepareBatchUpdate(Model model, Metrics metrics) throws IOException; } diff --git a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/storage/StorageDAO.java b/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/storage/StorageDAO.java index 35d178d1e..4d3f26d60 100644 --- a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/storage/StorageDAO.java +++ b/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/storage/StorageDAO.java @@ -26,9 +26,9 @@ import org.apache.skywalking.oap.server.library.module.Service; /** * @author peng-yongsheng */ -public interface StorageDAO extends Service { +public interface StorageDAO extends Service { - IMetricsDAO newMetricsDao(StorageBuilder storageBuilder); + IMetricsDAO newMetricsDao(StorageBuilder storageBuilder); IRegisterDAO newRegisterDao(StorageBuilder storageBuilder); diff --git a/oap-server/server-library/library-client/src/main/java/org/apache/skywalking/oap/server/library/client/elasticsearch/ElasticSearchClient.java b/oap-server/server-library/library-client/src/main/java/org/apache/skywalking/oap/server/library/client/elasticsearch/ElasticSearchClient.java index cbee9e2e6..ef9f4049b 100644 --- a/oap-server/server-library/library-client/src/main/java/org/apache/skywalking/oap/server/library/client/elasticsearch/ElasticSearchClient.java +++ b/oap-server/server-library/library-client/src/main/java/org/apache/skywalking/oap/server/library/client/elasticsearch/ElasticSearchClient.java @@ -254,14 +254,14 @@ public class ElasticSearchClient implements Client { client.update(request); } - public IndexRequest prepareInsert(String indexName, String id, XContentBuilder source) { + public ElasticSearchInsertRequest prepareInsert(String indexName, String id, XContentBuilder source) { indexName = formatIndexName(indexName); - return new IndexRequest(indexName, TYPE, id).source(source); + return new ElasticSearchInsertRequest(indexName, TYPE, id).source(source); } - public UpdateRequest prepareUpdate(String indexName, String id, XContentBuilder source) { + public ElasticSearchUpdateRequest prepareUpdate(String indexName, String id, XContentBuilder source) { indexName = formatIndexName(indexName); - return new UpdateRequest(indexName, TYPE, id).doc(source); + return new ElasticSearchUpdateRequest(indexName, TYPE, id).doc(source); } public int delete(String indexName, String timeBucketColumnName, long endTimeBucket) throws IOException { diff --git a/oap-server/server-library/library-client/src/main/java/org/apache/skywalking/oap/server/library/client/elasticsearch/ElasticSearchInsertRequest.java b/oap-server/server-library/library-client/src/main/java/org/apache/skywalking/oap/server/library/client/elasticsearch/ElasticSearchInsertRequest.java new file mode 100644 index 000000000..19e974ce9 --- /dev/null +++ b/oap-server/server-library/library-client/src/main/java/org/apache/skywalking/oap/server/library/client/elasticsearch/ElasticSearchInsertRequest.java @@ -0,0 +1,37 @@ +/* + * 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.library.client.elasticsearch; + +import org.apache.skywalking.oap.server.library.client.request.InsertRequest; +import org.elasticsearch.action.index.IndexRequest; +import org.elasticsearch.common.xcontent.XContentBuilder; + +/** + * @author peng-yongsheng + */ +public class ElasticSearchInsertRequest extends IndexRequest implements InsertRequest { + + public ElasticSearchInsertRequest(String index, String type, String id) { + super(index, type, id); + } + + @Override public ElasticSearchInsertRequest source(XContentBuilder sourceBuilder) { + super.source(sourceBuilder); + return this; + } +} diff --git a/oap-server/server-library/library-client/src/main/java/org/apache/skywalking/oap/server/library/client/elasticsearch/ElasticSearchUpdateRequest.java b/oap-server/server-library/library-client/src/main/java/org/apache/skywalking/oap/server/library/client/elasticsearch/ElasticSearchUpdateRequest.java new file mode 100644 index 000000000..9429d93d8 --- /dev/null +++ b/oap-server/server-library/library-client/src/main/java/org/apache/skywalking/oap/server/library/client/elasticsearch/ElasticSearchUpdateRequest.java @@ -0,0 +1,36 @@ +/* + * 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.library.client.elasticsearch; + +import org.elasticsearch.action.update.UpdateRequest; +import org.elasticsearch.common.xcontent.XContentBuilder; + +/** + * @author peng-yongsheng + */ +public class ElasticSearchUpdateRequest extends UpdateRequest implements org.apache.skywalking.oap.server.library.client.request.UpdateRequest { + + public ElasticSearchUpdateRequest(String index, String type, String id) { + super(index, type, id); + } + + @Override public ElasticSearchUpdateRequest doc(XContentBuilder source) { + super.doc(source); + return this; + } +} diff --git a/oap-server/server-library/library-client/src/main/java/org/apache/skywalking/oap/server/library/client/jdbc/hikaricp/JDBCHikariCPClient.java b/oap-server/server-library/library-client/src/main/java/org/apache/skywalking/oap/server/library/client/jdbc/hikaricp/JDBCHikariCPClient.java index fcdeb119b..158e9185e 100644 --- a/oap-server/server-library/library-client/src/main/java/org/apache/skywalking/oap/server/library/client/jdbc/hikaricp/JDBCHikariCPClient.java +++ b/oap-server/server-library/library-client/src/main/java/org/apache/skywalking/oap/server/library/client/jdbc/hikaricp/JDBCHikariCPClient.java @@ -31,7 +31,7 @@ import org.slf4j.*; * @author wusheng */ public class JDBCHikariCPClient implements Client { - private final Logger logger = LoggerFactory.getLogger(JDBCHikariCPClient.class); + private static final Logger logger = LoggerFactory.getLogger(JDBCHikariCPClient.class); private HikariDataSource dataSource; private HikariConfig hikariConfig; diff --git a/oap-server/server-library/library-client/src/main/java/org/apache/skywalking/oap/server/library/client/request/InsertRequest.java b/oap-server/server-library/library-client/src/main/java/org/apache/skywalking/oap/server/library/client/request/InsertRequest.java new file mode 100644 index 000000000..c3308d330 --- /dev/null +++ b/oap-server/server-library/library-client/src/main/java/org/apache/skywalking/oap/server/library/client/request/InsertRequest.java @@ -0,0 +1,24 @@ +/* + * 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.library.client.request; + +/** + * @author peng-yongsheng + */ +public interface InsertRequest extends PrepareRequest { +} diff --git a/oap-server/server-library/library-client/src/main/java/org/apache/skywalking/oap/server/library/client/request/PrepareRequest.java b/oap-server/server-library/library-client/src/main/java/org/apache/skywalking/oap/server/library/client/request/PrepareRequest.java new file mode 100644 index 000000000..05fbd949c --- /dev/null +++ b/oap-server/server-library/library-client/src/main/java/org/apache/skywalking/oap/server/library/client/request/PrepareRequest.java @@ -0,0 +1,24 @@ +/* + * 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.library.client.request; + +/** + * @author peng-yongsheng + */ +public interface PrepareRequest { +} diff --git a/oap-server/server-library/library-client/src/main/java/org/apache/skywalking/oap/server/library/client/request/UpdateRequest.java b/oap-server/server-library/library-client/src/main/java/org/apache/skywalking/oap/server/library/client/request/UpdateRequest.java new file mode 100644 index 000000000..209cc43ed --- /dev/null +++ b/oap-server/server-library/library-client/src/main/java/org/apache/skywalking/oap/server/library/client/request/UpdateRequest.java @@ -0,0 +1,24 @@ +/* + * 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.library.client.request; + +/** + * @author peng-yongsheng + */ +public interface UpdateRequest extends PrepareRequest { +} diff --git a/oap-server/server-storage-plugin/storage-elasticsearch-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/elasticsearch/base/MetricsEsDAO.java b/oap-server/server-storage-plugin/storage-elasticsearch-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/elasticsearch/base/MetricsEsDAO.java index a0c8212c2..e0403c735 100644 --- a/oap-server/server-storage-plugin/storage-elasticsearch-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/elasticsearch/base/MetricsEsDAO.java +++ b/oap-server/server-storage-plugin/storage-elasticsearch-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/elasticsearch/base/MetricsEsDAO.java @@ -23,16 +23,14 @@ import java.util.*; import org.apache.skywalking.oap.server.core.analysis.metrics.Metrics; import org.apache.skywalking.oap.server.core.storage.*; import org.apache.skywalking.oap.server.core.storage.model.Model; -import org.apache.skywalking.oap.server.library.client.elasticsearch.ElasticSearchClient; -import org.elasticsearch.action.index.IndexRequest; +import org.apache.skywalking.oap.server.library.client.elasticsearch.*; import org.elasticsearch.action.search.SearchResponse; -import org.elasticsearch.action.update.UpdateRequest; import org.elasticsearch.common.xcontent.XContentBuilder; /** * @author peng-yongsheng */ -public class MetricsEsDAO extends EsDAO implements IMetricsDAO { +public class MetricsEsDAO extends EsDAO implements IMetricsDAO { private final StorageBuilder storageBuilder; @@ -57,13 +55,13 @@ public class MetricsEsDAO extends EsDAO implements IMetricsDAO { +public class StorageEsDAO extends EsDAO implements StorageDAO { public StorageEsDAO(ElasticSearchClient client) { super(client); } - @Override public IMetricsDAO newMetricsDao(StorageBuilder storageBuilder) { + @Override public IMetricsDAO newMetricsDao(StorageBuilder storageBuilder) { return new MetricsEsDAO(getClient(), storageBuilder); } diff --git a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/ArrayParamBuilder.java b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/ArrayParamBuilder.java new file mode 100644 index 000000000..78da5fc89 --- /dev/null +++ b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/ArrayParamBuilder.java @@ -0,0 +1,35 @@ +/* + * 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; + +/** + * @author peng-yongsheng + */ +public class ArrayParamBuilder { + + public static String build(String[] values) { + StringBuilder param = new StringBuilder(); + for (int i = 0; i < values.length; i++) { + param.append("'").append(values[i]).append("'"); + if (i < values.length - 1) { + param.append(","); + } + } + return param.toString(); + } +} diff --git a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/SQLExecutor.java b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/SQLExecutor.java index 54b11928e..d2d7cc25a 100644 --- a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/SQLExecutor.java +++ b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/SQLExecutor.java @@ -18,20 +18,19 @@ package org.apache.skywalking.oap.server.storage.plugin.jdbc; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.SQLException; +import java.sql.*; import java.util.List; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.apache.skywalking.oap.server.library.client.request.*; +import org.slf4j.*; /** * A SQL executor. * * @author wusheng */ -public class SQLExecutor { - private final Logger logger = LoggerFactory.getLogger(SQLExecutor.class); +public class SQLExecutor implements InsertRequest, UpdateRequest { + + private static final Logger logger = LoggerFactory.getLogger(SQLExecutor.class); private String sql; private List param; diff --git a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/h2/dao/H2MetricsDAO.java b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/h2/dao/H2MetricsDAO.java index 1943c9428..7f18b4c2f 100644 --- a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/h2/dao/H2MetricsDAO.java +++ b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/h2/dao/H2MetricsDAO.java @@ -19,7 +19,7 @@ package org.apache.skywalking.oap.server.storage.plugin.jdbc.h2.dao; import java.io.IOException; -import java.util.Map; +import java.util.*; import org.apache.skywalking.oap.server.core.analysis.metrics.Metrics; import org.apache.skywalking.oap.server.core.storage.*; import org.apache.skywalking.oap.server.core.storage.model.Model; @@ -29,7 +29,7 @@ import org.apache.skywalking.oap.server.storage.plugin.jdbc.SQLExecutor; /** * @author wusheng */ -public class H2MetricsDAO extends H2SQLExecutor implements IMetricsDAO { +public class H2MetricsDAO extends H2SQLExecutor implements IMetricsDAO { private JDBCHikariCPClient h2Client; private StorageBuilder storageBuilder; @@ -40,8 +40,19 @@ public class H2MetricsDAO extends H2SQLExecutor implements IMetricsDAO get(Model model, Metrics[] metrics) throws IOException { - // return (Metrics)getByID(h2Client, model.getName(), metrics.id(), storageBuilder); - return null; + Map result = new HashMap<>(); + + String[] ids = new String[metrics.length]; + for (int i = 0; i < metrics.length; i++) { + ids[i] = metrics[i].id(); + } + + List storageDataList = getByIDs(h2Client, model.getName(), ids, storageBuilder); + + for (StorageData storageData : storageDataList) { + result.put(storageData.id(), (Metrics)storageData); + } + return result; } @Override public SQLExecutor prepareBatchInsert(Model model, Metrics metrics) throws IOException { diff --git a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/h2/dao/H2SQLExecutor.java b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/h2/dao/H2SQLExecutor.java index 3ab7d291d..049bee376 100644 --- a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/h2/dao/H2SQLExecutor.java +++ b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/h2/dao/H2SQLExecutor.java @@ -32,11 +32,33 @@ import org.apache.skywalking.oap.server.storage.plugin.jdbc.*; import org.slf4j.*; /** - * @author wusheng + * @author wusheng, peng-yongsheng */ public class H2SQLExecutor { private static final Logger logger = LoggerFactory.getLogger(H2SQLExecutor.class); + protected List getByIDs(JDBCHikariCPClient h2Client, String modelName, String[] ids, + StorageBuilder storageBuilder) throws IOException { + + try (Connection connection = h2Client.getConnection()) { + /* + * Although H2 database or other database support createArrayOf and setArray operate. + * But Mysql 5.1.44 driver doesn't. + */ + String param = ArrayParamBuilder.build(ids); + + try (ResultSet rs = h2Client.executeQuery(connection, "SELECT * FROM " + modelName + " WHERE id in (" + param + ")")) { + List storageDataList = new ArrayList<>(); + while (rs.next()) { + storageDataList.add(toStorageData(rs, modelName, storageBuilder)); + } + return storageDataList; + } + } catch (SQLException | JDBCClientException e) { + throw new IOException(e.getMessage(), e); + } + } + protected StorageData getByID(JDBCHikariCPClient h2Client, String modelName, String id, StorageBuilder storageBuilder) throws IOException { try (Connection connection = h2Client.getConnection()) { @@ -59,8 +81,7 @@ public class H2SQLExecutor { } } - protected StorageData toStorageData(ResultSet rs, String modelName, - StorageBuilder storageBuilder) throws SQLException { + protected StorageData toStorageData(ResultSet rs, String modelName, StorageBuilder storageBuilder) throws SQLException { if (rs.next()) { Map data = new HashMap(); List columns = TableMetaInfo.get(modelName).getColumns(); @@ -85,8 +106,7 @@ public class H2SQLExecutor { return Const.NONE; } - protected SQLExecutor getInsertExecutor(String modelName, StorageData metrics, - StorageBuilder storageBuilder) throws IOException { + protected SQLExecutor getInsertExecutor(String modelName, StorageData metrics, StorageBuilder storageBuilder) throws IOException { Map objectMap = storageBuilder.data2Map(metrics); SQLBuilder sqlBuilder = new SQLBuilder("INSERT INTO " + modelName + " VALUES"); @@ -113,8 +133,7 @@ public class H2SQLExecutor { return new SQLExecutor(sqlBuilder.toString(), param); } - protected SQLExecutor getUpdateExecutor(String modelName, StorageData metrics, - StorageBuilder storageBuilder) throws IOException { + protected SQLExecutor getUpdateExecutor(String modelName, StorageData metrics, StorageBuilder storageBuilder) throws IOException { Map objectMap = storageBuilder.data2Map(metrics); SQLBuilder sqlBuilder = new SQLBuilder("UPDATE " + modelName + " SET "); diff --git a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/h2/dao/H2StorageDAO.java b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/h2/dao/H2StorageDAO.java index d8ef8398a..1d9588134 100644 --- a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/h2/dao/H2StorageDAO.java +++ b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/h2/dao/H2StorageDAO.java @@ -23,12 +23,11 @@ import org.apache.skywalking.oap.server.core.analysis.record.Record; import org.apache.skywalking.oap.server.core.register.RegisterSource; import org.apache.skywalking.oap.server.core.storage.*; import org.apache.skywalking.oap.server.library.client.jdbc.hikaricp.JDBCHikariCPClient; -import org.apache.skywalking.oap.server.storage.plugin.jdbc.SQLExecutor; /** - * @author wusheng + * @author wusheng, peng-yongsheng */ -public class H2StorageDAO implements StorageDAO { +public class H2StorageDAO implements StorageDAO { private JDBCHikariCPClient h2Client; @@ -36,7 +35,7 @@ public class H2StorageDAO implements StorageDAO { this.h2Client = h2Client; } - @Override public IMetricsDAO newMetricsDao(StorageBuilder storageBuilder) { + @Override public IMetricsDAO newMetricsDao(StorageBuilder storageBuilder) { return new H2MetricsDAO(h2Client, storageBuilder); } diff --git a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/ArrayParamBuilderTestCase.java b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/ArrayParamBuilderTestCase.java new file mode 100644 index 000000000..8d120a5f8 --- /dev/null +++ b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/ArrayParamBuilderTestCase.java @@ -0,0 +1,38 @@ +/* + * 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; + +import org.junit.*; + +/** + * @author peng-yongsheng + */ +public class ArrayParamBuilderTestCase { + + @Test + public void testBuild() { + String param = ArrayParamBuilder.build(new String[] {"1"}); + Assert.assertEquals("'1'", param); + + param = ArrayParamBuilder.build(new String[] {"1", "2"}); + Assert.assertEquals("'1','2'", param); + + param = ArrayParamBuilder.build(new String[] {"1", "2", "3"}); + Assert.assertEquals("'1','2','3'", param); + } +}