Explorar el Código

fix: 增加tablestore表检查

Marko552 hace 3 años
padre
commit
cabc08a485

+ 11 - 0
abi-cloud-qr-platform-common/src/main/java/com/abi/task/common/tablestore/TableStoreUtils.java

@@ -16,6 +16,7 @@ import com.alicloud.openservices.tablestore.model.search.query.TermsQuery;
 import com.alicloud.openservices.tablestore.model.search.sort.FieldSort;
 import com.alicloud.openservices.tablestore.model.search.sort.Sort;
 import com.alicloud.openservices.tablestore.model.search.sort.SortOrder;
+import com.google.common.collect.Maps;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Value;
@@ -23,6 +24,7 @@ import org.springframework.stereotype.Component;
 
 import java.time.LocalDateTime;
 import java.util.*;
+import java.util.stream.Collectors;
 
 /**
  * @author: fangxinjian
@@ -391,4 +393,13 @@ public class TableStoreUtils {
         updateTableRequest.setTableOptionsForUpdate(tableOptions);
         return client().updateTable(updateTableRequest);
     }
+
+
+    public Map<String,String> listTables() {
+        List<String> tableNames = client().listTable().getTableNames();
+        if (CollectionUtil.isNotEmpty(tableNames)){
+            return tableNames.stream().collect(Collectors.toMap(t->t,t->t));
+        }
+        return Maps.newHashMap();
+    }
 }

+ 89 - 0
abi-cloud-qr-platform-server/src/main/java/com/abi/qms/platform/runner/DoCheckTableStoreRunner.java

@@ -0,0 +1,89 @@
+package com.abi.qms.platform.runner;
+
+import com.abi.task.common.api.exception.BusinessException;
+import com.abi.task.common.tablestore.TableStoreUtils;
+import com.abi.task.common.tablestore.common.TableStore;
+import com.google.common.collect.Sets;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.ObjectUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.ApplicationArguments;
+import org.springframework.boot.ApplicationRunner;
+import org.springframework.core.io.Resource;
+import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
+import org.springframework.core.type.classreading.CachingMetadataReaderFactory;
+import org.springframework.core.type.classreading.MetadataReader;
+import org.springframework.stereotype.Component;
+
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.atomic.AtomicInteger;
+
+/**
+ * @className: com.abi.qms.platform.runner-> doCheckTableStore
+ * @description:
+ * @author: Marko.W
+ * @createDate: 2021-06-08 9:59
+ * @version: 1.0
+ * @todo:
+ */
+@Component
+@Slf4j
+public class DoCheckTableStoreRunner implements ApplicationRunner {
+
+    @Autowired
+    private TableStoreUtils tableStoreUtils;
+
+    private static AtomicInteger retryTimes = new AtomicInteger(0);
+    private static String RESOURCE_PATH = "classpath*:com/abi/qms/platform/dao/**/*.class";
+//    private static String RESOURCE_PATH = "classpath*:com/abi/task/common/tablestore/**/*.class";
+
+
+    @Override
+    public void run(ApplicationArguments args) throws Exception {
+        Set<TableStore> tableNames = getAllEntity();
+        Map<String,String> existTableNames = tableStoreUtils.listTables();
+        tableNames.stream()
+                .filter(o->!existTableNames.containsKey(o.tableName()))
+                .forEach(tableInfo->{
+                    log.info("检查到表名{}不存在,开始执行创建",tableInfo.tableName());
+                    long start = System.currentTimeMillis();
+                    tableStoreUtils.createTable(tableInfo.tableName(),tableInfo.primaryKeyName());
+                    log.info("表名{}创建成功,耗时{}ms",tableInfo.tableName(),(System.currentTimeMillis() -start));
+        });
+    }
+
+    private static Set<TableStore> getAllEntity(){
+        Set<TableStore> ret = Sets.newHashSet();
+        PathMatchingResourcePatternResolver pathMatchingResourcePatternResolver = new PathMatchingResourcePatternResolver();
+        CachingMetadataReaderFactory cachingMetadataReaderFactory = new CachingMetadataReaderFactory();
+        try {
+            Resource[] resources = pathMatchingResourcePatternResolver.getResources(RESOURCE_PATH);
+            ClassLoader loader = ClassLoader.getSystemClassLoader();
+            for (Resource resource : resources) {
+                MetadataReader reader = cachingMetadataReaderFactory.getMetadataReader(resource);
+                String className = reader.getClassMetadata().getClassName();
+                Class aClass = loader.loadClass(className);
+                TableStore annotation = (TableStore) aClass.getAnnotation(TableStore.class);
+                if (ObjectUtils.isNotEmpty(annotation)){
+                    ret.add(annotation);
+                }
+            }
+        } catch (Exception e) {
+            if (retryTimes.getAndIncrement()>2){
+                log.error("TableStore 初始化检查失败",e);
+                throw new BusinessException(500,"TableStore 初始化检查失败");
+            }
+            try {
+                Thread.sleep(retryTimes.get() * 1000);
+            } catch (InterruptedException interruptedException) {
+                // Do Nothing
+            }
+            //进行当前重试
+            log.info("当前开始重试,第{}次",retryTimes.get());
+            return getAllEntity();
+        }
+        return ret;
+    }
+
+}