Browse Source

Entity Export refactoring

pull/6730/head
Andrii Shvaika 4 years ago
parent
commit
b55dbdf050
  1. 2
      application/src/main/java/org/thingsboard/server/service/sync/ie/DefaultEntitiesExportImportService.java
  2. 2
      application/src/main/java/org/thingsboard/server/service/sync/ie/EntitiesExportImportService.java
  3. 2
      application/src/main/java/org/thingsboard/server/service/sync/ie/exporting/EntityExportService.java
  4. 2
      application/src/main/java/org/thingsboard/server/service/sync/ie/exporting/impl/BaseEntityExportService.java
  5. 8
      application/src/main/java/org/thingsboard/server/service/sync/ie/exporting/impl/DefaultEntityExportService.java
  6. 26
      application/src/main/java/org/thingsboard/server/service/sync/vc/DefaultEntitiesVersionControlService.java
  7. 20
      application/src/main/java/org/thingsboard/server/service/sync/vc/data/ComplexEntitiesExportCtx.java
  8. 35
      application/src/main/java/org/thingsboard/server/service/sync/vc/data/EntitiesExportCtx.java
  9. 46
      application/src/main/java/org/thingsboard/server/service/sync/vc/data/EntityTypeExportCtx.java
  10. 16
      application/src/main/java/org/thingsboard/server/service/sync/vc/data/SimpleEntitiesExportCtx.java

2
application/src/main/java/org/thingsboard/server/service/sync/ie/DefaultEntitiesExportImportService.java

@ -72,7 +72,7 @@ public class DefaultEntitiesExportImportService implements EntitiesExportImportS
@Override @Override
public <E extends ExportableEntity<I>, I extends EntityId> EntityExportData<E> exportEntity(EntitiesExportCtx ctx, I entityId, EntityExportSettings exportSettings) throws ThingsboardException { public <E extends ExportableEntity<I>, I extends EntityId> EntityExportData<E> exportEntity(EntitiesExportCtx<?> ctx, I entityId, EntityExportSettings exportSettings) throws ThingsboardException {
if (!rateLimitService.checkEntityExportLimit(ctx.getTenantId())) { if (!rateLimitService.checkEntityExportLimit(ctx.getTenantId())) {
throw new ThingsboardException("Rate limit for entities export is exceeded", ThingsboardErrorCode.TOO_MANY_REQUESTS); throw new ThingsboardException("Rate limit for entities export is exceeded", ThingsboardErrorCode.TOO_MANY_REQUESTS);
} }

2
application/src/main/java/org/thingsboard/server/service/sync/ie/EntitiesExportImportService.java

@ -31,7 +31,7 @@ import java.util.Comparator;
public interface EntitiesExportImportService { public interface EntitiesExportImportService {
<E extends ExportableEntity<I>, I extends EntityId> EntityExportData<E> exportEntity(EntitiesExportCtx ctx, I entityId, EntityExportSettings exportSettings) throws ThingsboardException; <E extends ExportableEntity<I>, I extends EntityId> EntityExportData<E> exportEntity(EntitiesExportCtx<?> ctx, I entityId, EntityExportSettings exportSettings) throws ThingsboardException;
<E extends ExportableEntity<I>, I extends EntityId> EntityImportResult<E> importEntity(EntitiesImportCtx ctx, EntityExportData<E> exportData) throws ThingsboardException; <E extends ExportableEntity<I>, I extends EntityId> EntityImportResult<E> importEntity(EntitiesImportCtx ctx, EntityExportData<E> exportData) throws ThingsboardException;

2
application/src/main/java/org/thingsboard/server/service/sync/ie/exporting/EntityExportService.java

@ -25,6 +25,6 @@ import org.thingsboard.server.service.sync.vc.data.EntitiesExportCtx;
public interface EntityExportService<I extends EntityId, E extends ExportableEntity<I>, D extends EntityExportData<E>> { public interface EntityExportService<I extends EntityId, E extends ExportableEntity<I>, D extends EntityExportData<E>> {
D getExportData(EntitiesExportCtx ctx, I entityId, EntityExportSettings exportSettings) throws ThingsboardException; D getExportData(EntitiesExportCtx<?> ctx, I entityId, EntityExportSettings exportSettings) throws ThingsboardException;
} }

2
application/src/main/java/org/thingsboard/server/service/sync/ie/exporting/impl/BaseEntityExportService.java

@ -30,7 +30,7 @@ import java.util.Set;
public abstract class BaseEntityExportService<I extends EntityId, E extends ExportableEntity<I>, D extends EntityExportData<E>> extends DefaultEntityExportService<I, E, D> { public abstract class BaseEntityExportService<I extends EntityId, E extends ExportableEntity<I>, D extends EntityExportData<E>> extends DefaultEntityExportService<I, E, D> {
@Override @Override
protected void setAdditionalExportData(EntitiesExportCtx ctx, E entity, D exportData, EntityExportSettings exportSettings) throws ThingsboardException { protected void setAdditionalExportData(EntitiesExportCtx<?> ctx, E entity, D exportData, EntityExportSettings exportSettings) throws ThingsboardException {
setRelatedEntities(ctx.getTenantId(), entity, (D) exportData, exportSettings); setRelatedEntities(ctx.getTenantId(), entity, (D) exportData, exportSettings);
super.setAdditionalExportData(ctx, entity, exportData, exportSettings); super.setAdditionalExportData(ctx, entity, exportData, exportSettings);
} }

8
application/src/main/java/org/thingsboard/server/service/sync/ie/exporting/impl/DefaultEntityExportService.java

@ -60,7 +60,7 @@ public class DefaultEntityExportService<I extends EntityId, E extends Exportable
private AttributesService attributesService; private AttributesService attributesService;
@Override @Override
public final D getExportData(EntitiesExportCtx ctx, I entityId, EntityExportSettings exportSettings) throws ThingsboardException { public final D getExportData(EntitiesExportCtx<?> ctx, I entityId, EntityExportSettings exportSettings) throws ThingsboardException {
D exportData = newExportData(); D exportData = newExportData();
E entity = exportableEntitiesService.findEntityByTenantIdAndId(ctx.getTenantId(), entityId); E entity = exportableEntitiesService.findEntityByTenantIdAndId(ctx.getTenantId(), entityId);
@ -75,7 +75,7 @@ public class DefaultEntityExportService<I extends EntityId, E extends Exportable
return exportData; return exportData;
} }
protected void setAdditionalExportData(EntitiesExportCtx ctx, E entity, D exportData, EntityExportSettings exportSettings) throws ThingsboardException { protected void setAdditionalExportData(EntitiesExportCtx<?> ctx, E entity, D exportData, EntityExportSettings exportSettings) throws ThingsboardException {
if (exportSettings.isExportRelations()) { if (exportSettings.isExportRelations()) {
List<EntityRelation> relations = exportRelations(ctx, entity); List<EntityRelation> relations = exportRelations(ctx, entity);
exportData.setRelations(relations); exportData.setRelations(relations);
@ -86,7 +86,7 @@ public class DefaultEntityExportService<I extends EntityId, E extends Exportable
} }
} }
private List<EntityRelation> exportRelations(EntitiesExportCtx ctx, E entity) throws ThingsboardException { private List<EntityRelation> exportRelations(EntitiesExportCtx<?> ctx, E entity) throws ThingsboardException {
List<EntityRelation> relations = new ArrayList<>(); List<EntityRelation> relations = new ArrayList<>();
List<EntityRelation> inboundRelations = relationService.findByTo(ctx.getTenantId(), entity.getId(), RelationTypeGroup.COMMON); List<EntityRelation> inboundRelations = relationService.findByTo(ctx.getTenantId(), entity.getId(), RelationTypeGroup.COMMON);
@ -97,7 +97,7 @@ public class DefaultEntityExportService<I extends EntityId, E extends Exportable
return relations; return relations;
} }
private Map<String, List<AttributeExportData>> exportAttributes(EntitiesExportCtx ctx, E entity) throws ThingsboardException { private Map<String, List<AttributeExportData>> exportAttributes(EntitiesExportCtx<?> ctx, E entity) throws ThingsboardException {
List<String> scopes; List<String> scopes;
if (entity.getId().getEntityType() == EntityType.DEVICE) { if (entity.getId().getEntityType() == EntityType.DEVICE) {
scopes = List.of(DataConstants.SERVER_SCOPE, DataConstants.SHARED_SCOPE); scopes = List.of(DataConstants.SERVER_SCOPE, DataConstants.SHARED_SCOPE);

26
application/src/main/java/org/thingsboard/server/service/sync/vc/DefaultEntitiesVersionControlService.java

@ -5,7 +5,7 @@
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
@ -22,7 +22,6 @@ import com.google.common.util.concurrent.MoreExecutors;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.support.TransactionTemplate; import org.springframework.transaction.support.TransactionTemplate;
@ -60,9 +59,7 @@ import org.thingsboard.server.common.data.sync.vc.request.create.ComplexVersionC
import org.thingsboard.server.common.data.sync.vc.request.create.EntityTypeVersionCreateConfig; import org.thingsboard.server.common.data.sync.vc.request.create.EntityTypeVersionCreateConfig;
import org.thingsboard.server.common.data.sync.vc.request.create.SingleEntityVersionCreateRequest; import org.thingsboard.server.common.data.sync.vc.request.create.SingleEntityVersionCreateRequest;
import org.thingsboard.server.common.data.sync.vc.request.create.SyncStrategy; import org.thingsboard.server.common.data.sync.vc.request.create.SyncStrategy;
import org.thingsboard.server.common.data.sync.vc.request.create.VersionCreateConfig;
import org.thingsboard.server.common.data.sync.vc.request.create.VersionCreateRequest; import org.thingsboard.server.common.data.sync.vc.request.create.VersionCreateRequest;
import org.thingsboard.server.common.data.sync.vc.request.load.EntityTypeVersionLoadConfig;
import org.thingsboard.server.common.data.sync.vc.request.load.EntityTypeVersionLoadRequest; import org.thingsboard.server.common.data.sync.vc.request.load.EntityTypeVersionLoadRequest;
import org.thingsboard.server.common.data.sync.vc.request.load.SingleEntityVersionLoadRequest; import org.thingsboard.server.common.data.sync.vc.request.load.SingleEntityVersionLoadRequest;
import org.thingsboard.server.common.data.sync.vc.request.load.VersionLoadConfig; import org.thingsboard.server.common.data.sync.vc.request.load.VersionLoadConfig;
@ -76,10 +73,10 @@ import org.thingsboard.server.service.sync.ie.EntitiesExportImportService;
import org.thingsboard.server.service.sync.ie.exporting.ExportableEntitiesService; import org.thingsboard.server.service.sync.ie.exporting.ExportableEntitiesService;
import org.thingsboard.server.service.sync.ie.importing.impl.MissingEntityException; import org.thingsboard.server.service.sync.ie.importing.impl.MissingEntityException;
import org.thingsboard.server.service.sync.vc.autocommit.TbAutoCommitSettingsService; import org.thingsboard.server.service.sync.vc.autocommit.TbAutoCommitSettingsService;
import org.thingsboard.server.service.sync.vc.data.CommitGitRequest;
import org.thingsboard.server.service.sync.vc.data.ComplexEntitiesExportCtx; import org.thingsboard.server.service.sync.vc.data.ComplexEntitiesExportCtx;
import org.thingsboard.server.service.sync.vc.data.EntitiesExportCtx; import org.thingsboard.server.service.sync.vc.data.EntitiesExportCtx;
import org.thingsboard.server.service.sync.vc.data.EntitiesImportCtx; import org.thingsboard.server.service.sync.vc.data.EntitiesImportCtx;
import org.thingsboard.server.service.sync.vc.data.EntityTypeExportCtx;
import org.thingsboard.server.service.sync.vc.data.SimpleEntitiesExportCtx; import org.thingsboard.server.service.sync.vc.data.SimpleEntitiesExportCtx;
import org.thingsboard.server.service.sync.vc.repository.TbRepositorySettingsService; import org.thingsboard.server.service.sync.vc.repository.TbRepositorySettingsService;
@ -152,21 +149,22 @@ public class DefaultEntitiesVersionControlService implements EntitiesVersionCont
} }
private void handleSingleEntityRequest(SimpleEntitiesExportCtx ctx) throws Exception { private void handleSingleEntityRequest(SimpleEntitiesExportCtx ctx) throws Exception {
ctx.add(saveEntityData(ctx, ctx.getRequest().getEntityId(), ctx.getSettings())); ctx.add(saveEntityData(ctx, ctx.getRequest().getEntityId()));
} }
private void handleComplexRequest(ComplexEntitiesExportCtx ctx) { private void handleComplexRequest(ComplexEntitiesExportCtx parentCtx) {
ctx.getRequest().getEntityTypes().forEach((entityType, config) -> { ComplexVersionCreateRequest request = parentCtx.getRequest();
if (ObjectUtils.defaultIfNull(config.getSyncStrategy(), ctx.getRequest().getSyncStrategy()) == SyncStrategy.OVERWRITE) { request.getEntityTypes().forEach((entityType, config) -> {
EntityTypeExportCtx ctx = new EntityTypeExportCtx(parentCtx, config, request.getSyncStrategy(), entityType);
if (ctx.isOverwrite()) {
ctx.add(gitServiceQueue.deleteAll(ctx.getCommit(), entityType)); ctx.add(gitServiceQueue.deleteAll(ctx.getCommit(), entityType));
} }
EntityExportSettings settings = ctx.getSettings(entityType);
if (config.isAllEntities()) { if (config.isAllEntities()) {
DaoUtil.processInBatches(pageLink -> exportableEntitiesService.findEntitiesByTenantId(ctx.getTenantId(), entityType, pageLink) DaoUtil.processInBatches(pageLink -> exportableEntitiesService.findEntitiesByTenantId(ctx.getTenantId(), entityType, pageLink)
, 100, entity -> { , 100, entity -> {
try { try {
ctx.add(saveEntityData(ctx, entity.getId(), settings)); ctx.add(saveEntityData(ctx, entity.getId()));
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
@ -174,7 +172,7 @@ public class DefaultEntitiesVersionControlService implements EntitiesVersionCont
} else { } else {
for (UUID entityId : config.getEntityIds()) { for (UUID entityId : config.getEntityIds()) {
try { try {
ctx.add(saveEntityData(ctx, EntityIdFactory.getByTypeAndUuid(entityType, entityId), settings)); ctx.add(saveEntityData(ctx, EntityIdFactory.getByTypeAndUuid(entityType, entityId)));
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
@ -183,8 +181,8 @@ public class DefaultEntitiesVersionControlService implements EntitiesVersionCont
}); });
} }
private ListenableFuture<Void> saveEntityData(EntitiesExportCtx ctx, EntityId entityId, EntityExportSettings settings) throws Exception { private ListenableFuture<Void> saveEntityData(EntitiesExportCtx<?> ctx, EntityId entityId) throws Exception {
EntityExportData<ExportableEntity<EntityId>> entityData = exportImportService.exportEntity(ctx, entityId, settings); EntityExportData<ExportableEntity<EntityId>> entityData = exportImportService.exportEntity(ctx, entityId, ctx.getSettings());
return gitServiceQueue.addToCommit(ctx.getCommit(), entityData); return gitServiceQueue.addToCommit(ctx.getCommit(), entityData);
} }

20
application/src/main/java/org/thingsboard/server/service/sync/vc/data/ComplexEntitiesExportCtx.java

@ -1,3 +1,18 @@
/**
* Copyright © 2016-2022 The Thingsboard Authors
*
* Licensed 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.thingsboard.server.service.sync.vc.data; package org.thingsboard.server.service.sync.vc.data;
import org.thingsboard.server.common.data.EntityType; import org.thingsboard.server.common.data.EntityType;
@ -20,4 +35,9 @@ public class ComplexEntitiesExportCtx extends EntitiesExportCtx<ComplexVersionCr
public EntityExportSettings getSettings(EntityType entityType) { public EntityExportSettings getSettings(EntityType entityType) {
return settings.get(entityType); return settings.get(entityType);
} }
@Override
public EntityExportSettings getSettings() {
throw new RuntimeException("Not implemented. Use EntityTypeExportCtx instead!");
}
} }

35
application/src/main/java/org/thingsboard/server/service/sync/vc/data/EntitiesExportCtx.java

@ -1,3 +1,18 @@
/**
* Copyright © 2016-2022 The Thingsboard Authors
*
* Licensed 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.thingsboard.server.service.sync.vc.data; package org.thingsboard.server.service.sync.vc.data;
import com.google.common.util.concurrent.ListenableFuture; import com.google.common.util.concurrent.ListenableFuture;
@ -12,12 +27,26 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
@Data @Data
public class EntitiesExportCtx<R extends VersionCreateRequest> { public abstract class EntitiesExportCtx<R extends VersionCreateRequest> {
protected final SecurityUser user; protected final SecurityUser user;
protected final CommitGitRequest commit; protected final CommitGitRequest commit;
protected final R request; protected final R request;
private final List<ListenableFuture<Void>> futures = new ArrayList<>(); private final List<ListenableFuture<Void>> futures;
public EntitiesExportCtx(SecurityUser user, CommitGitRequest commit, R request) {
this.user = user;
this.commit = commit;
this.request = request;
this.futures = new ArrayList<>();
}
public <T extends R> EntitiesExportCtx(EntitiesExportCtx<T> other) {
this.user = other.getUser();
this.commit = other.getCommit();
this.request = other.getRequest();
this.futures = other.getFutures();
}
public void add(ListenableFuture<Void> future) { public void add(ListenableFuture<Void> future) {
futures.add(future); futures.add(future);
@ -34,4 +63,6 @@ public class EntitiesExportCtx<R extends VersionCreateRequest> {
.exportCredentials(config.isSaveCredentials()) .exportCredentials(config.isSaveCredentials())
.build(); .build();
} }
public abstract EntityExportSettings getSettings();
} }

46
application/src/main/java/org/thingsboard/server/service/sync/vc/data/EntityTypeExportCtx.java

@ -0,0 +1,46 @@
/**
* Copyright © 2016-2022 The Thingsboard Authors
*
* Licensed 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.thingsboard.server.service.sync.vc.data;
import lombok.Getter;
import org.apache.commons.lang3.ObjectUtils;
import org.thingsboard.server.common.data.EntityType;
import org.thingsboard.server.common.data.sync.ie.EntityExportSettings;
import org.thingsboard.server.common.data.sync.vc.request.create.EntityTypeVersionCreateConfig;
import org.thingsboard.server.common.data.sync.vc.request.create.SyncStrategy;
import org.thingsboard.server.common.data.sync.vc.request.create.VersionCreateRequest;
public class EntityTypeExportCtx extends EntitiesExportCtx<VersionCreateRequest> {
@Getter
private final EntityType entityType;
@Getter
private final boolean overwrite;
@Getter
private final EntityExportSettings settings;
public EntityTypeExportCtx(EntitiesExportCtx<?> parent, EntityTypeVersionCreateConfig config, SyncStrategy defaultSyncStrategy, EntityType entityType) {
super(parent);
this.entityType = entityType;
this.settings = EntityExportSettings.builder()
.exportRelations(config.isSaveRelations())
.exportAttributes(config.isSaveAttributes())
.exportCredentials(config.isSaveCredentials())
.build();
this.overwrite = ObjectUtils.defaultIfNull(config.getSyncStrategy(), defaultSyncStrategy) == SyncStrategy.OVERWRITE;
}
}

16
application/src/main/java/org/thingsboard/server/service/sync/vc/data/SimpleEntitiesExportCtx.java

@ -1,3 +1,18 @@
/**
* Copyright © 2016-2022 The Thingsboard Authors
*
* Licensed 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.thingsboard.server.service.sync.vc.data; package org.thingsboard.server.service.sync.vc.data;
import lombok.Getter; import lombok.Getter;
@ -14,4 +29,5 @@ public class SimpleEntitiesExportCtx extends EntitiesExportCtx<SingleEntityVersi
super(user, commit, request); super(user, commit, request);
this.settings = request != null ? buildExportSettings(request.getConfig()) : null; this.settings = request != null ? buildExportSettings(request.getConfig()) : null;
} }
} }

Loading…
Cancel
Save