kezhenxu94 commented on code in PR #10544:
URL: https://github.com/apache/skywalking/pull/10544#discussion_r1139805415


##########
oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/TableHelper.java:
##########
@@ -0,0 +1,141 @@
+/*
+ * 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.common;
+
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.skywalking.oap.server.core.Const;
+import org.apache.skywalking.oap.server.core.CoreModule;
+import org.apache.skywalking.oap.server.core.analysis.DownSampling;
+import org.apache.skywalking.oap.server.core.analysis.FunctionCategory;
+import org.apache.skywalking.oap.server.core.analysis.TimeBucket;
+import org.apache.skywalking.oap.server.core.config.ConfigService;
+import org.apache.skywalking.oap.server.core.storage.model.Model;
+import 
org.apache.skywalking.oap.server.library.client.jdbc.hikaricp.JDBCClient;
+import org.apache.skywalking.oap.server.library.module.ModuleManager;
+import org.apache.skywalking.oap.server.library.util.StringUtil;
+import org.apache.skywalking.oap.server.storage.plugin.jdbc.TableMetaInfo;
+
+import java.sql.SQLException;
+import java.util.Collections;
+import java.util.List;
+import java.util.concurrent.TimeUnit;
+import java.util.stream.Collectors;
+import java.util.stream.LongStream;
+
+/**
+ * Utility class to get table name for a given model.
+ */
+@Slf4j
+@RequiredArgsConstructor
+public class TableHelper {
+    private final ModuleManager moduleManager;
+    private final JDBCClient jdbcClient;
+
+    public static String getTableName(Model model) {
+        final var aggFuncName = 
FunctionCategory.uniqueFunctionName(model.getStreamClass()).replaceAll("-", 
"_");
+        return StringUtil.isNotBlank(aggFuncName) ? aggFuncName : 
model.getName();
+    }
+
+    public static String getTableForWrite(Model model) {
+        final var tableName = getTableName(model);
+
+        if (!model.isTimeSeries()) {
+            return tableName;
+        }
+
+        final var dayTimeBucket = 
TimeBucket.getTimeBucket(System.currentTimeMillis(), DownSampling.Day);
+        return tableName + Const.UNDERSCORE + dayTimeBucket;
+    }
+
+    public static String getTable(Model model, long timeBucket) {
+        final var tableName = getTableName(model);
+        if (timeBucket == 0) {
+            timeBucket = TimeBucket.getTimeBucket(System.currentTimeMillis(), 
DownSampling.Day);
+        }
+
+        if (!model.isTimeSeries()) {
+            return tableName;
+        }
+
+        return tableName + Const.UNDERSCORE + 
TimeBucket.getTimeBucket(TimeBucket.getTimestamp(timeBucket), DownSampling.Day);
+    }
+
+    public List<String> getTablesForRead(String modelName, long 
timeBucketStart, long timeBucketEnd) {
+        final var model = TableMetaInfo.get(modelName);
+        final var tableName = getTableName(model);
+
+        if (!model.isTimeSeries()) {
+            return Collections.singletonList(tableName);
+        }
+
+        timeBucketStart = 
TimeBucket.getTimeBucket(TimeBucket.getTimestamp(timeBucketStart), 
DownSampling.Day);
+        timeBucketEnd = 
TimeBucket.getTimeBucket(TimeBucket.getTimestamp(timeBucketEnd), 
DownSampling.Day);
+
+        return LongStream
+            .rangeClosed(timeBucketStart, timeBucketEnd)
+            .distinct()
+            .mapToObj(it -> tableName + "_" + it)
+            .filter(table -> {
+                try {
+                    return jdbcClient.tableExists(table);

Review Comment:
   > I think failing to read from Database in sync may be not worth it.
   > 
   > 1. It could be slow
   > 2. If this kind of query keeps happening, we cost a lot of resources in 
the database.
   > 
   > Instead, moving the table cache from `DeleteDao` to JDBC global scope 
should help. After all, it only read once period.
   
   Caching these brings other problems, when the tables are deleted in another 
OAP node, we don't get notified, and still consider this table exists, then we 
might send queries to an inexistent table, which causes errors.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to