|
|
|
@ -25,6 +25,7 @@ import org.thingsboard.server.common.data.ObjectType; |
|
|
|
import org.thingsboard.server.common.data.edqs.AttributeKv; |
|
|
|
import org.thingsboard.server.common.data.edqs.EdqsEventType; |
|
|
|
import org.thingsboard.server.common.data.edqs.EdqsObject; |
|
|
|
import org.thingsboard.server.common.data.edqs.EdqsSyncRequest; |
|
|
|
import org.thingsboard.server.common.data.edqs.Entity; |
|
|
|
import org.thingsboard.server.common.data.edqs.LatestTsKv; |
|
|
|
import org.thingsboard.server.common.data.edqs.fields.EntityFields; |
|
|
|
@ -83,21 +84,38 @@ public abstract class EdqsSyncService { |
|
|
|
|
|
|
|
public abstract boolean isSyncNeeded(); |
|
|
|
|
|
|
|
public void sync() { |
|
|
|
public void sync(EdqsSyncRequest syncRequest) { |
|
|
|
log.info("Synchronizing data to EDQS"); |
|
|
|
long startTs = System.currentTimeMillis(); |
|
|
|
counters.clear(); |
|
|
|
|
|
|
|
syncTenantEntities(); |
|
|
|
syncRelations(); |
|
|
|
loadKeyDictionary(); |
|
|
|
syncAttributes(); |
|
|
|
syncLatestTimeseries(); |
|
|
|
if (syncRequest.getObjectTypes() != null && !syncRequest.getObjectTypes().isEmpty()) { |
|
|
|
log.info("Sync request for entity types: {}", syncRequest.getObjectTypes()); |
|
|
|
for (ObjectType objectType : syncRequest.getObjectTypes()) { |
|
|
|
syncObjectType(objectType); |
|
|
|
} |
|
|
|
} else { |
|
|
|
log.info("Sync request for all entity types"); |
|
|
|
syncTenantEntities(); |
|
|
|
syncRelations(); |
|
|
|
loadKeyDictionary(); |
|
|
|
syncAttributes(); |
|
|
|
syncLatestTimeseries(); |
|
|
|
} |
|
|
|
|
|
|
|
counters.clear(); |
|
|
|
log.info("Finished synchronizing data to EDQS in {} ms", (System.currentTimeMillis() - startTs)); |
|
|
|
} |
|
|
|
|
|
|
|
private void syncObjectType(ObjectType objectType) { |
|
|
|
switch (objectType) { |
|
|
|
case RELATION -> syncRelations(); |
|
|
|
case ATTRIBUTE_KV -> syncAttributes(); |
|
|
|
case LATEST_TS_KV -> syncLatestTimeseries(); |
|
|
|
default -> syncTenantEntity(objectType); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private void process(TenantId tenantId, ObjectType type, EdqsObject object) { |
|
|
|
AtomicInteger counter = counters.computeIfAbsent(type, t -> new AtomicInteger()); |
|
|
|
if (counter.incrementAndGet() % 10000 == 0) { |
|
|
|
@ -108,26 +126,30 @@ public abstract class EdqsSyncService { |
|
|
|
|
|
|
|
private void syncTenantEntities() { |
|
|
|
for (ObjectType type : edqsTenantTypes) { |
|
|
|
log.info("Synchronizing {} entities to EDQS", type); |
|
|
|
long ts = System.currentTimeMillis(); |
|
|
|
EntityType entityType = type.toEntityType(); |
|
|
|
Dao<?> dao = entityDaoRegistry.getDao(entityType); |
|
|
|
UUID lastId = UUID.fromString("00000000-0000-0000-0000-000000000000"); |
|
|
|
while (true) { |
|
|
|
var batch = dao.findNextBatch(lastId, entityBatchSize); |
|
|
|
if (batch.isEmpty()) { |
|
|
|
break; |
|
|
|
} |
|
|
|
for (EntityFields entityFields : batch) { |
|
|
|
TenantId tenantId = TenantId.fromUUID(entityFields.getTenantId()); |
|
|
|
entityInfoMap.put(entityFields.getId(), new EntityIdInfo(entityType, tenantId)); |
|
|
|
process(tenantId, type, new Entity(entityType, entityFields)); |
|
|
|
} |
|
|
|
EntityFields lastRecord = batch.get(batch.size() - 1); |
|
|
|
lastId = lastRecord.getId(); |
|
|
|
syncTenantEntity(type); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private void syncTenantEntity(ObjectType type) { |
|
|
|
log.info("Synchronizing {} entities to EDQS", type); |
|
|
|
long ts = System.currentTimeMillis(); |
|
|
|
EntityType entityType = type.toEntityType(); |
|
|
|
Dao<?> dao = entityDaoRegistry.getDao(entityType); |
|
|
|
UUID lastId = UUID.fromString("00000000-0000-0000-0000-000000000000"); |
|
|
|
while (true) { |
|
|
|
var batch = dao.findNextBatch(lastId, entityBatchSize); |
|
|
|
if (batch.isEmpty()) { |
|
|
|
break; |
|
|
|
} |
|
|
|
for (EntityFields entityFields : batch) { |
|
|
|
TenantId tenantId = TenantId.fromUUID(entityFields.getTenantId()); |
|
|
|
entityInfoMap.put(entityFields.getId(), new EntityIdInfo(entityType, tenantId)); |
|
|
|
process(tenantId, type, new Entity(entityType, entityFields)); |
|
|
|
} |
|
|
|
log.info("Finished synchronizing {} entities to EDQS in {} ms", type, (System.currentTimeMillis() - ts)); |
|
|
|
EntityFields lastRecord = batch.get(batch.size() - 1); |
|
|
|
lastId = lastRecord.getId(); |
|
|
|
} |
|
|
|
log.info("Finished synchronizing {} entities to EDQS in {} ms", type, (System.currentTimeMillis() - ts)); |
|
|
|
} |
|
|
|
|
|
|
|
private void syncRelations() { |
|
|
|
|