Commit 8edefd49758acec9f9a3a2fa84071e1683c6e2f7

Authored by Igor Kulikov
Committed by GitHub
2 parents 4aede40c 35c8ebab

Merge pull request #3238 from YevhenBondarenko/ts-latest-migration-improvements

Cassandra ts latest migration improvements
@@ -29,7 +29,8 @@ import org.thingsboard.server.dao.model.sqlts.dictionary.TsKvDictionaryComposite @@ -29,7 +29,8 @@ import org.thingsboard.server.dao.model.sqlts.dictionary.TsKvDictionaryComposite
29 import org.thingsboard.server.dao.model.sqlts.latest.TsKvLatestEntity; 29 import org.thingsboard.server.dao.model.sqlts.latest.TsKvLatestEntity;
30 import org.thingsboard.server.dao.sqlts.dictionary.TsKvDictionaryRepository; 30 import org.thingsboard.server.dao.sqlts.dictionary.TsKvDictionaryRepository;
31 import org.thingsboard.server.dao.sqlts.insert.latest.InsertLatestTsRepository; 31 import org.thingsboard.server.dao.sqlts.insert.latest.InsertLatestTsRepository;
32 -import org.thingsboard.server.dao.util.NoSqlAnyDao; 32 +import org.thingsboard.server.dao.util.NoSqlTsDao;
  33 +import org.thingsboard.server.dao.util.SqlTsLatestDao;
33 import org.thingsboard.server.service.install.EntityDatabaseSchemaService; 34 import org.thingsboard.server.service.install.EntityDatabaseSchemaService;
34 35
35 import java.sql.Connection; 36 import java.sql.Connection;
@@ -51,11 +52,13 @@ import static org.thingsboard.server.service.install.migrate.CassandraToSqlColum @@ -51,11 +52,13 @@ import static org.thingsboard.server.service.install.migrate.CassandraToSqlColum
51 52
52 @Service 53 @Service
53 @Profile("install") 54 @Profile("install")
54 -@NoSqlAnyDao 55 +@NoSqlTsDao
  56 +@SqlTsLatestDao
55 @Slf4j 57 @Slf4j
56 public class CassandraTsLatestToSqlMigrateService implements TsLatestMigrateService { 58 public class CassandraTsLatestToSqlMigrateService implements TsLatestMigrateService {
57 59
58 - private static final int LATEST_KEY_LENGTH = 255; 60 + private static final int MAX_KEY_LENGTH = 255;
  61 + private static final int MAX_STR_V_LENGTH = 10000000;
59 62
60 @Autowired 63 @Autowired
61 private EntityDatabaseSchemaService entityDatabaseSchemaService; 64 private EntityDatabaseSchemaService entityDatabaseSchemaService;
@@ -95,11 +98,10 @@ public class CassandraTsLatestToSqlMigrateService implements TsLatestMigrateServ @@ -95,11 +98,10 @@ public class CassandraTsLatestToSqlMigrateService implements TsLatestMigrateServ
95 log.error("Unexpected error during ThingsBoard entities data migration!", e); 98 log.error("Unexpected error during ThingsBoard entities data migration!", e);
96 throw e; 99 throw e;
97 } 100 }
98 - entityDatabaseSchemaService.createDatabaseIndexes();  
99 } 101 }
100 102
101 private List<CassandraToSqlTable> tables = Arrays.asList( 103 private List<CassandraToSqlTable> tables = Arrays.asList(
102 - new CassandraToSqlTable("ts_kv_latest_cf", 104 + new CassandraToSqlTable("ts_kv_latest_cf", "ts_kv_latest",
103 idColumn("entity_id"), 105 idColumn("entity_id"),
104 stringColumn("key"), 106 stringColumn("key"),
105 bigintColumn("ts"), 107 bigintColumn("ts"),
@@ -129,6 +131,12 @@ public class CassandraTsLatestToSqlMigrateService implements TsLatestMigrateServ @@ -129,6 +131,12 @@ public class CassandraTsLatestToSqlMigrateService implements TsLatestMigrateServ
129 131
130 String strV = data[4].getValue(); 132 String strV = data[4].getValue();
131 if (strV != null) { 133 if (strV != null) {
  134 + if (strV.length() > MAX_STR_V_LENGTH) {
  135 + log.warn("[ts_kv_latest] Value size [{}] exceeds maximum size [{}] of column [str_v] and will be truncated!",
  136 + strV.length(), MAX_STR_V_LENGTH);
  137 + log.warn("Affected data:\n{}", strV);
  138 + strV = strV.substring(0, MAX_STR_V_LENGTH);
  139 + }
132 latestEntity.setStrValue(strV); 140 latestEntity.setStrValue(strV);
133 } else { 141 } else {
134 Long longV = null; 142 Long longV = null;
@@ -170,9 +178,11 @@ public class CassandraTsLatestToSqlMigrateService implements TsLatestMigrateServ @@ -170,9 +178,11 @@ public class CassandraTsLatestToSqlMigrateService implements TsLatestMigrateServ
170 } 178 }
171 179
172 protected Integer getOrSaveKeyId(String strKey) { 180 protected Integer getOrSaveKeyId(String strKey) {
173 - if (strKey.length() > LATEST_KEY_LENGTH) {  
174 - log.warn("Key is long. Max key length is 255\n{}", strKey);  
175 - strKey = strKey.substring(0, LATEST_KEY_LENGTH); 181 + if (strKey.length() > MAX_KEY_LENGTH) {
  182 + log.warn("[ts_kv_latest] Value size [{}] exceeds maximum size [{}] of column [key] and will be truncated!",
  183 + strKey.length(), MAX_KEY_LENGTH);
  184 + log.warn("Affected data:\n{}", strKey);
  185 + strKey = strKey.substring(0, MAX_KEY_LENGTH);
176 } 186 }
177 187
178 Integer keyId = tsKvDictionaryMap.get(strKey); 188 Integer keyId = tsKvDictionaryMap.get(strKey);