|
|
@ -15,18 +15,31 @@ |
|
|
*/ |
|
|
*/ |
|
|
package org.thingsboard.server.service.install; |
|
|
package org.thingsboard.server.service.install; |
|
|
|
|
|
|
|
|
|
|
|
import com.fasterxml.jackson.databind.JsonNode; |
|
|
|
|
|
import com.fasterxml.jackson.databind.node.ObjectNode; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
|
|
import org.apache.commons.collections.CollectionUtils; |
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
import org.springframework.beans.factory.annotation.Value; |
|
|
import org.springframework.beans.factory.annotation.Value; |
|
|
import org.springframework.context.annotation.Profile; |
|
|
import org.springframework.context.annotation.Profile; |
|
|
import org.springframework.stereotype.Service; |
|
|
import org.springframework.stereotype.Service; |
|
|
import org.thingsboard.server.common.data.EntitySubtype; |
|
|
import org.thingsboard.server.common.data.EntitySubtype; |
|
|
import org.thingsboard.server.common.data.Tenant; |
|
|
import org.thingsboard.server.common.data.Tenant; |
|
|
|
|
|
import org.thingsboard.server.common.data.id.QueueId; |
|
|
|
|
|
import org.thingsboard.server.common.data.id.TenantId; |
|
|
import org.thingsboard.server.common.data.page.PageData; |
|
|
import org.thingsboard.server.common.data.page.PageData; |
|
|
import org.thingsboard.server.common.data.page.PageLink; |
|
|
import org.thingsboard.server.common.data.page.PageLink; |
|
|
|
|
|
import org.thingsboard.server.common.data.queue.ProcessingStrategy; |
|
|
|
|
|
import org.thingsboard.server.common.data.queue.ProcessingStrategyType; |
|
|
|
|
|
import org.thingsboard.server.common.data.queue.Queue; |
|
|
|
|
|
import org.thingsboard.server.common.data.queue.SubmitStrategy; |
|
|
|
|
|
import org.thingsboard.server.common.data.queue.SubmitStrategyType; |
|
|
|
|
|
import org.thingsboard.server.common.data.rule.RuleNode; |
|
|
import org.thingsboard.server.dao.dashboard.DashboardService; |
|
|
import org.thingsboard.server.dao.dashboard.DashboardService; |
|
|
import org.thingsboard.server.dao.device.DeviceProfileService; |
|
|
import org.thingsboard.server.dao.device.DeviceProfileService; |
|
|
import org.thingsboard.server.dao.device.DeviceService; |
|
|
import org.thingsboard.server.dao.device.DeviceService; |
|
|
|
|
|
import org.thingsboard.server.dao.queue.QueueService; |
|
|
|
|
|
import org.thingsboard.server.dao.rule.RuleChainService; |
|
|
import org.thingsboard.server.dao.tenant.TenantService; |
|
|
import org.thingsboard.server.dao.tenant.TenantService; |
|
|
import org.thingsboard.server.dao.usagerecord.ApiUsageStateService; |
|
|
import org.thingsboard.server.dao.usagerecord.ApiUsageStateService; |
|
|
import org.thingsboard.server.service.install.sql.SqlDbHelper; |
|
|
import org.thingsboard.server.service.install.sql.SqlDbHelper; |
|
|
@ -43,7 +56,9 @@ import java.sql.SQLSyntaxErrorException; |
|
|
import java.sql.SQLWarning; |
|
|
import java.sql.SQLWarning; |
|
|
import java.sql.Statement; |
|
|
import java.sql.Statement; |
|
|
import java.util.List; |
|
|
import java.util.List; |
|
|
|
|
|
import java.util.Map; |
|
|
import java.util.concurrent.TimeUnit; |
|
|
import java.util.concurrent.TimeUnit; |
|
|
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
|
import static org.thingsboard.server.service.install.DatabaseHelper.ADDITIONAL_INFO; |
|
|
import static org.thingsboard.server.service.install.DatabaseHelper.ADDITIONAL_INFO; |
|
|
import static org.thingsboard.server.service.install.DatabaseHelper.ASSIGNED_CUSTOMERS; |
|
|
import static org.thingsboard.server.service.install.DatabaseHelper.ASSIGNED_CUSTOMERS; |
|
|
@ -101,6 +116,14 @@ public class SqlDatabaseUpgradeService implements DatabaseEntitiesUpgradeService |
|
|
@Autowired |
|
|
@Autowired |
|
|
private ApiUsageStateService apiUsageStateService; |
|
|
private ApiUsageStateService apiUsageStateService; |
|
|
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
|
|
private QueueService queueService; |
|
|
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
|
|
private TbRuleEngineQueueConfigService queueConfig; |
|
|
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
|
|
private RuleChainService ruleChainService; |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public void upgradeDatabase(String fromVersion) throws Exception { |
|
|
public void upgradeDatabase(String fromVersion) throws Exception { |
|
|
@ -534,6 +557,81 @@ public class SqlDatabaseUpgradeService implements DatabaseEntitiesUpgradeService |
|
|
log.error("Failed updating schema!!!", e); |
|
|
log.error("Failed updating schema!!!", e); |
|
|
} |
|
|
} |
|
|
break; |
|
|
break; |
|
|
|
|
|
case "3.3.4": |
|
|
|
|
|
try (Connection conn = DriverManager.getConnection(dbUrl, dbUserName, dbPassword)) { |
|
|
|
|
|
log.info("Updating schema ..."); |
|
|
|
|
|
schemaUpdateFile = Paths.get(installScripts.getDataDir(), "upgrade", "3.3.4", SCHEMA_UPDATE_SQL); |
|
|
|
|
|
loadSql(schemaUpdateFile, conn); |
|
|
|
|
|
|
|
|
|
|
|
log.info("Loading queues..."); |
|
|
|
|
|
try { |
|
|
|
|
|
if (!CollectionUtils.isEmpty(queueConfig.getQueues())) { |
|
|
|
|
|
queueConfig.getQueues().forEach(queueSettings -> { |
|
|
|
|
|
Queue queue = new Queue(); |
|
|
|
|
|
queue.setTenantId(TenantId.SYS_TENANT_ID); |
|
|
|
|
|
queue.setName(queueSettings.getName()); |
|
|
|
|
|
queue.setTopic(queueSettings.getTopic()); |
|
|
|
|
|
queue.setPollInterval(queueSettings.getPollInterval()); |
|
|
|
|
|
queue.setPartitions(queueSettings.getPartitions()); |
|
|
|
|
|
queue.setPackProcessingTimeout(queueSettings.getPackProcessingTimeout()); |
|
|
|
|
|
SubmitStrategy submitStrategy = new SubmitStrategy(); |
|
|
|
|
|
submitStrategy.setBatchSize(queueSettings.getSubmitStrategy().getBatchSize()); |
|
|
|
|
|
submitStrategy.setType(SubmitStrategyType.valueOf(queueSettings.getSubmitStrategy().getType())); |
|
|
|
|
|
queue.setSubmitStrategy(submitStrategy); |
|
|
|
|
|
ProcessingStrategy processingStrategy = new ProcessingStrategy(); |
|
|
|
|
|
processingStrategy.setType(ProcessingStrategyType.valueOf(queueSettings.getProcessingStrategy().getType())); |
|
|
|
|
|
processingStrategy.setRetries(queueSettings.getProcessingStrategy().getRetries()); |
|
|
|
|
|
processingStrategy.setFailurePercentage(queueSettings.getProcessingStrategy().getFailurePercentage()); |
|
|
|
|
|
processingStrategy.setPauseBetweenRetries(queueSettings.getProcessingStrategy().getPauseBetweenRetries()); |
|
|
|
|
|
processingStrategy.setMaxPauseBetweenRetries(queueSettings.getProcessingStrategy().getMaxPauseBetweenRetries()); |
|
|
|
|
|
queue.setProcessingStrategy(processingStrategy); |
|
|
|
|
|
queue.setConsumerPerPartition(queueSettings.isConsumerPerPartition()); |
|
|
|
|
|
queueService.saveQueue(queue); |
|
|
|
|
|
}); |
|
|
|
|
|
} else { |
|
|
|
|
|
systemDataLoaderService.createQueues(); |
|
|
|
|
|
} |
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
log.info("Updating device profiles..."); |
|
|
|
|
|
schemaUpdateFile = Paths.get(installScripts.getDataDir(), "upgrade", "3.3.4", "schema_update_device_profile.sql"); |
|
|
|
|
|
loadSql(schemaUpdateFile, conn); |
|
|
|
|
|
|
|
|
|
|
|
log.info("Updating checkpoint rule nodes..."); |
|
|
|
|
|
PageLink pageLink = new PageLink(100); |
|
|
|
|
|
PageData<Tenant> pageData; |
|
|
|
|
|
do { |
|
|
|
|
|
pageData = tenantService.findTenants(pageLink); |
|
|
|
|
|
for (Tenant tenant : pageData.getData()) { |
|
|
|
|
|
TenantId tenantId = tenant.getId(); |
|
|
|
|
|
Map<String, QueueId> queues = |
|
|
|
|
|
queueService.findQueuesByTenantId(tenantId).stream().collect(Collectors.toMap(Queue::getName, Queue::getId)); |
|
|
|
|
|
try { |
|
|
|
|
|
List<RuleNode> checkpointNodes = |
|
|
|
|
|
ruleChainService.findRuleNodesByTenantIdAndType(tenantId, "org.thingsboard.rule.engine.flow.TbCheckpointNode"); |
|
|
|
|
|
checkpointNodes.forEach(node -> { |
|
|
|
|
|
ObjectNode configuration = (ObjectNode) node.getConfiguration(); |
|
|
|
|
|
JsonNode queueNameNode = configuration.remove("queueName"); |
|
|
|
|
|
if (queueNameNode != null) { |
|
|
|
|
|
String queueName = queueNameNode.asText(); |
|
|
|
|
|
configuration.put("queueId", queues.get(queueName).toString()); |
|
|
|
|
|
ruleChainService.saveRuleNode(tenantId, node); |
|
|
|
|
|
} |
|
|
|
|
|
}); |
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
pageLink = pageLink.nextPageLink(); |
|
|
|
|
|
} while (pageData.hasNext()); |
|
|
|
|
|
|
|
|
|
|
|
log.info("Updating schema settings..."); |
|
|
|
|
|
conn.createStatement().execute("UPDATE tb_schema_settings SET schema_version = 3004000;"); |
|
|
|
|
|
log.info("Schema updated."); |
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
log.error("Failed updating schema!!!", e); |
|
|
|
|
|
} |
|
|
|
|
|
break; |
|
|
default: |
|
|
default: |
|
|
throw new RuntimeException("Unable to upgrade SQL database, unsupported fromVersion: " + fromVersion); |
|
|
throw new RuntimeException("Unable to upgrade SQL database, unsupported fromVersion: " + fromVersion); |
|
|
} |
|
|
} |
|
|
|