Skip to content

Commit

Permalink
Fix issue with the parallel execution of the `ConnectedAasManager::cr…
Browse files Browse the repository at this point in the history
…eateSubmodelInAas` method. (#547)

* test: failing test for ConnectedAasManager parallel exec

* refactor: desc factory classes

* test: add aas refId assertion

* fix: instances of leg. usage of *DescFactory Api

* fix: conflicts while adding smRefs to shells in parallel

* fix: other sync issues in InMemorySubmodelService

* fix: add Sync Lock to InMemorySubmodelService

* chore: add missing license header
  • Loading branch information
mateusmolina-iese authored Nov 28, 2024
1 parent 4d1a643 commit fa33413
Show file tree
Hide file tree
Showing 10 changed files with 280 additions and 122 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ static ObjectMapper buildObjectMapper() {
static AasDescriptorFactory buildAasDescriptorFactory(String... aasRepositoryBaseUrls) {
AttributeMapper attributeMapper = new AttributeMapper(objectMapper);

return new AasDescriptorFactory(null, List.of(aasRepositoryBaseUrls), attributeMapper);
return new AasDescriptorFactory(List.of(aasRepositoryBaseUrls), attributeMapper);
}

static SubmodelDescriptorFactory buildSmDescriptorFactory(String... aasRepositoryBaseUrls) {
org.eclipse.digitaltwin.basyx.submodelregistry.client.mapper.AttributeMapper attributeMapperSm = new org.eclipse.digitaltwin.basyx.submodelregistry.client.mapper.AttributeMapper(
objectMapper);
return new SubmodelDescriptorFactory(null, List.of(aasRepositoryBaseUrls), attributeMapperSm);
return new SubmodelDescriptorFactory(List.of(aasRepositoryBaseUrls), attributeMapperSm);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
/*******************************************************************************
* Copyright (C) 2024 the Eclipse BaSyx Authors
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* SPDX-License-Identifier: MIT
******************************************************************************/

package org.eclipse.digitaltwin.basyx.aasenvironment.client;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.util.Collection;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.stream.IntStream;

import org.eclipse.digitaltwin.aas4j.v3.model.AssetAdministrationShell;
import org.eclipse.digitaltwin.aas4j.v3.model.Key;
import org.eclipse.digitaltwin.aas4j.v3.model.Reference;
import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultAssetAdministrationShell;
import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultSubmodel;
import org.eclipse.digitaltwin.basyx.aasregistry.client.api.RegistryAndDiscoveryInterfaceApi;
import org.eclipse.digitaltwin.basyx.aasrepository.AasRepository;
import org.eclipse.digitaltwin.basyx.aasrepository.client.ConnectedAasRepository;
import org.eclipse.digitaltwin.basyx.core.pagination.PaginationInfo;
import org.eclipse.digitaltwin.basyx.submodelregistry.client.api.SubmodelRegistryApi;
import org.eclipse.digitaltwin.basyx.submodelrepository.SubmodelRepository;
import org.eclipse.digitaltwin.basyx.submodelrepository.client.ConnectedSubmodelRepository;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ConfigurableApplicationContext;

public class TestConnectedAasManagerMultithreading {
static final String AAS_REPOSITORY_BASE_PATH = "http://localhost:8081";
static final String SM_REPOSITORY_BASE_PATH = "http://localhost:8081";
static final String AAS_REGISTRY_BASE_PATH = "http://localhost:8050";
static final String SM_REGISTRY_BASE_PATH = "http://localhost:8060";
static final int N_THREADS = 20;

static ConfigurableApplicationContext appContext;
static AasRepository aasRepository;
static SubmodelRepository smRepository;

static ConnectedAasRepository connectedAasRepository;
static ConnectedSubmodelRepository connectedSmRepository;
static RegistryAndDiscoveryInterfaceApi aasRegistryApi;
static SubmodelRegistryApi smRegistryApi;

static ConnectedAasManager aasManager;

@BeforeClass
public static void setupRepositories() {
appContext = new SpringApplication(DummyAasEnvironmentComponent.class).run(new String[] {});

connectedAasRepository = new ConnectedAasRepository(AAS_REPOSITORY_BASE_PATH);
connectedSmRepository = new ConnectedSubmodelRepository(SM_REPOSITORY_BASE_PATH);
aasRegistryApi = new RegistryAndDiscoveryInterfaceApi(AAS_REGISTRY_BASE_PATH);
smRegistryApi = new SubmodelRegistryApi(SM_REGISTRY_BASE_PATH);
aasManager = new ConnectedAasManager(AAS_REGISTRY_BASE_PATH, AAS_REPOSITORY_BASE_PATH, SM_REGISTRY_BASE_PATH, SM_REPOSITORY_BASE_PATH);

cleanUpRegistries();
}

@After
public void cleanUpComponents() {
cleanUpRegistries();
}

@AfterClass
public static void stopContext() {
appContext.close();
}

@Test
public void testParallelSubmodelCreation() throws ExecutionException, InterruptedException {
AssetAdministrationShell shell = createShell();

ExecutorService executorService = Executors.newFixedThreadPool(N_THREADS);
ConcurrentLinkedDeque<String> createdSubmodelIds = new ConcurrentLinkedDeque<>();

List<Future<Boolean>> futures = IntStream.range(0, N_THREADS).mapToObj(i -> executorService.submit(() -> createdSubmodelIds.add(createSubmodel(shell.getId(), i)))).toList();

try {
for (int i = 0; i < N_THREADS; i++) {
futures.get(i).get();
}
} finally {
executorService.shutdown();
}

createdSubmodelIds.forEach(submodelId -> assertSubmodelWasCreatedAndRegistered(shell.getId(), submodelId));
}

static void assertSubmodelWasCreatedAndRegistered(String shellId, String submodelId) {
assertEquals(submodelId, aasManager.getSubmodelService(submodelId).getSubmodel().getId());
assertTrue(connectedAasRepository.getSubmodelReferences(shellId, PaginationInfo.NO_LIMIT).getResult().stream().map(Reference::getKeys).flatMap(Collection::stream).map(Key::getValue).anyMatch(submodelId::equals));
}


private static void cleanUpRegistries() {
try {
aasRegistryApi.deleteAllShellDescriptors();
} catch (Exception e) {
System.out.println(e.getMessage());
}
try {
smRegistryApi.deleteAllSubmodelDescriptors();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}

private static AssetAdministrationShell createShell() {
String id = UUID.randomUUID().toString();
DefaultAssetAdministrationShell shell = new DefaultAssetAdministrationShell.Builder().id(id).build();
aasManager.createAas(shell);
return aasManager.getAasService(id).getAAS();
}

private static String createSubmodel(String aasId, int threadId) {
try {
String id = aasId + "-thread" + threadId;
DefaultSubmodel submodel = new DefaultSubmodel.Builder().id(id).build();
aasManager.createSubmodelInAas(aasId, submodel);
return id;
} catch (Exception e) {
throw new RuntimeException("Failed at thread " + threadId, e);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -53,31 +53,28 @@ public class AasDescriptorFactory {
private static final String AAS_INTERFACE = "AAS-3.0";
private static final String AAS_REPOSITORY_PATH = "shells";

private AssetAdministrationShell shell;
private List<String> aasRepositoryURLs;
private final List<String> aasRepositoryURLs;
private static AttributeMapper attributeMapper;

private AttributeMapper attributeMapper;

public AasDescriptorFactory(AssetAdministrationShell shell, List<String> aasRepositoryBaseURLs, AttributeMapper attributeMapper) {
this.shell = shell;
public AasDescriptorFactory(List<String> aasRepositoryBaseURLs, AttributeMapper attributeMapper) {
this.aasRepositoryURLs = createAasRepositoryUrls(aasRepositoryBaseURLs);
this.attributeMapper = attributeMapper;
AasDescriptorFactory.attributeMapper = attributeMapper;
}

/**
* Creates {@link AssetAdministrationShellDescriptor}
*
* @return the created AssetAdministrationShellDescriptor
*/
public AssetAdministrationShellDescriptor create() {
public AssetAdministrationShellDescriptor create(AssetAdministrationShell shell) {

AssetAdministrationShellDescriptor descriptor = new AssetAdministrationShellDescriptor();

setId(shell.getId(), descriptor);

setIdShort(shell.getIdShort(), descriptor);

setEndpointItem(shell.getId(), descriptor);
setEndpointItem(shell.getId(), descriptor, aasRepositoryURLs);

setDescription(shell.getDescription(), descriptor);

Expand All @@ -96,68 +93,63 @@ public AssetAdministrationShellDescriptor create() {
return descriptor;
}

public AssetAdministrationShellDescriptor create(AssetAdministrationShell shell) {
this.shell = shell;
return create();
}

private void setDescription(List<LangStringTextType> descriptions, AssetAdministrationShellDescriptor descriptor) {
private static void setDescription(List<LangStringTextType> descriptions, AssetAdministrationShellDescriptor descriptor) {

if (descriptions == null || descriptions.isEmpty())
return;

descriptor.setDescription(attributeMapper.mapDescription(descriptions));
}

private void setDisplayName(List<LangStringNameType> displayNames, AssetAdministrationShellDescriptor descriptor) {
private static void setDisplayName(List<LangStringNameType> displayNames, AssetAdministrationShellDescriptor descriptor) {

if (displayNames == null || displayNames.isEmpty())
return;

descriptor.setDisplayName(attributeMapper.mapDisplayName(displayNames));
}

private void setExtensions(List<Extension> extensions, AssetAdministrationShellDescriptor descriptor) {
private static void setExtensions(List<Extension> extensions, AssetAdministrationShellDescriptor descriptor) {

if (extensions == null || extensions.isEmpty())
return;

descriptor.setExtensions(attributeMapper.mapExtensions(extensions));
}

private void setAdministration(AdministrativeInformation administration, AssetAdministrationShellDescriptor descriptor) {
private static void setAdministration(AdministrativeInformation administration, AssetAdministrationShellDescriptor descriptor) {

if (administration == null)
return;

descriptor.setAdministration(attributeMapper.mapAdministration(administration));
}

private void setAssetKind(AssetInformation assetInformation, AssetAdministrationShellDescriptor descriptor) {
private static void setAssetKind(AssetInformation assetInformation, AssetAdministrationShellDescriptor descriptor) {

if (assetInformation == null || assetInformation.getAssetKind() == null)
return;

descriptor.setAssetKind(attributeMapper.mapAssetKind(assetInformation.getAssetKind()));
}

private void setAssetType(AssetInformation assetInformation, AssetAdministrationShellDescriptor descriptor) {
private static void setAssetType(AssetInformation assetInformation, AssetAdministrationShellDescriptor descriptor) {

if (assetInformation == null || assetInformation.getAssetType() == null)
return;

descriptor.setAssetType(assetInformation.getAssetType());
}

private void setGlobalAssetId(AssetInformation assetInformation, AssetAdministrationShellDescriptor descriptor) {
private static void setGlobalAssetId(AssetInformation assetInformation, AssetAdministrationShellDescriptor descriptor) {

if (assetInformation == null || assetInformation.getGlobalAssetId() == null)
return;

descriptor.setGlobalAssetId(assetInformation.getGlobalAssetId());
}

private void setEndpointItem(String shellId, AssetAdministrationShellDescriptor descriptor) {
private static void setEndpointItem(String shellId, AssetAdministrationShellDescriptor descriptor, List<String> aasRepositoryURLs) {
for (String eachUrl : aasRepositoryURLs) {
Endpoint endpoint = new Endpoint();
endpoint.setInterface(AAS_INTERFACE);
Expand All @@ -168,7 +160,7 @@ private void setEndpointItem(String shellId, AssetAdministrationShellDescriptor
}
}

private ProtocolInformation createProtocolInformation(String shellId, String url) {
private static ProtocolInformation createProtocolInformation(String shellId, String url) {
String href = String.format("%s/%s", url, Base64UrlEncodedIdentifier.encodeIdentifier(shellId));

ProtocolInformation protocolInformation = new ProtocolInformation();
Expand All @@ -178,23 +170,23 @@ private ProtocolInformation createProtocolInformation(String shellId, String url
return protocolInformation;
}

private void setIdShort(String idShort, AssetAdministrationShellDescriptor descriptor) {
private static void setIdShort(String idShort, AssetAdministrationShellDescriptor descriptor) {
descriptor.setIdShort(idShort);
}

private void setId(String shellId, AssetAdministrationShellDescriptor descriptor) {
private static void setId(String shellId, AssetAdministrationShellDescriptor descriptor) {
descriptor.setId(shellId);
}

private String getProtocol(String endpoint) {
private static String getProtocol(String endpoint) {
try {
return new URL(endpoint).getProtocol();
} catch (MalformedURLException e) {
throw new RuntimeException();
}
}

private List<String> createAasRepositoryUrls(List<String> aasRepositoryBaseURLs) {
private static List<String> createAasRepositoryUrls(List<String> aasRepositoryBaseURLs) {
List<String> toReturn = new ArrayList<>(aasRepositoryBaseURLs.size());
for (String eachUrl : aasRepositoryBaseURLs) {
toReturn.add(RepositoryUrlHelper.createRepositoryUrl(eachUrl, AAS_REPOSITORY_PATH));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public AssetAdministrationShell getAas(String shellId) throws ElementDoesNotExis

@Override
public void createAas(AssetAdministrationShell shell) throws CollidingIdentifierException {
AssetAdministrationShellDescriptor descriptor = new AasDescriptorFactory(shell, aasRepositoryRegistryLink.getAasRepositoryBaseURLs(), attributeMapper).create();
AssetAdministrationShellDescriptor descriptor = new AasDescriptorFactory(aasRepositoryRegistryLink.getAasRepositoryBaseURLs(), attributeMapper).create(shell);

decorated.createAas(shell);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ public void testExternalUrl() {
private AssetAdministrationShellDescriptor createAndRetrieveDescriptor(AssetAdministrationShell shell) {
registryIntegrationAasRepository.createAas(shell);

AasDescriptorFactory descriptorFactory = new AasDescriptorFactory(shell, mockedRegistryLink.getAasRepositoryBaseURLs(), mockedAttributeMapper);
return descriptorFactory.create();
AasDescriptorFactory descriptorFactory = new AasDescriptorFactory(mockedRegistryLink.getAasRepositoryBaseURLs(), mockedAttributeMapper);
return descriptorFactory.create(shell);
}

private AssetAdministrationShell createDummyAas() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,19 @@ public CursorResult<List<Reference>> getSubmodelReferences(PaginationInfo pInfo)

@Override
public void addSubmodelReference(Reference submodelReference) {
throwExceptionIfReferenceIsAlreadyPresent(submodelReference);

aas.getSubmodels().add(submodelReference);
List<Reference> submodelsRefs = aas.getSubmodels();
synchronized (submodelsRefs) {
throwExceptionIfReferenceIsAlreadyPresent(submodelReference);
submodelsRefs.add(submodelReference);
}
}

@Override
public void removeSubmodelReference(String submodelId) {
Reference specificSubmodelReference = getSubmodelReferenceById(submodelId);

aas.getSubmodels().remove(specificSubmodelReference);
List<Reference> submodelsRefs = aas.getSubmodels();
synchronized (submodelsRefs) {
submodelsRefs.remove(getSubmodelReferenceById(submodelId));
}
}

@Override
Expand Down
Loading

0 comments on commit fa33413

Please sign in to comment.