Skip to content

Commit fa33413

Browse files
authored
Fix issue with the parallel execution of the ConnectedAasManager::createSubmodelInAas 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
1 parent 4d1a643 commit fa33413

File tree

10 files changed

+280
-122
lines changed

10 files changed

+280
-122
lines changed

basyx.aasenvironment/basyx.aasenvironment-client/src/main/java/org/eclipse/digitaltwin/basyx/aasenvironment/client/ConnectedAasManagerHelper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,13 @@ static ObjectMapper buildObjectMapper() {
6060
static AasDescriptorFactory buildAasDescriptorFactory(String... aasRepositoryBaseUrls) {
6161
AttributeMapper attributeMapper = new AttributeMapper(objectMapper);
6262

63-
return new AasDescriptorFactory(null, List.of(aasRepositoryBaseUrls), attributeMapper);
63+
return new AasDescriptorFactory(List.of(aasRepositoryBaseUrls), attributeMapper);
6464
}
6565

6666
static SubmodelDescriptorFactory buildSmDescriptorFactory(String... aasRepositoryBaseUrls) {
6767
org.eclipse.digitaltwin.basyx.submodelregistry.client.mapper.AttributeMapper attributeMapperSm = new org.eclipse.digitaltwin.basyx.submodelregistry.client.mapper.AttributeMapper(
6868
objectMapper);
69-
return new SubmodelDescriptorFactory(null, List.of(aasRepositoryBaseUrls), attributeMapperSm);
69+
return new SubmodelDescriptorFactory(List.of(aasRepositoryBaseUrls), attributeMapperSm);
7070
}
7171

7272
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
/*******************************************************************************
2+
* Copyright (C) 2024 the Eclipse BaSyx Authors
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining
5+
* a copy of this software and associated documentation files (the
6+
* "Software"), to deal in the Software without restriction, including
7+
* without limitation the rights to use, copy, modify, merge, publish,
8+
* distribute, sublicense, and/or sell copies of the Software, and to
9+
* permit persons to whom the Software is furnished to do so, subject to
10+
* the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be
13+
* included in all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19+
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20+
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21+
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22+
*
23+
* SPDX-License-Identifier: MIT
24+
******************************************************************************/
25+
26+
package org.eclipse.digitaltwin.basyx.aasenvironment.client;
27+
28+
import static org.junit.Assert.assertEquals;
29+
import static org.junit.Assert.assertTrue;
30+
31+
import java.util.Collection;
32+
import java.util.List;
33+
import java.util.UUID;
34+
import java.util.concurrent.ConcurrentLinkedDeque;
35+
import java.util.concurrent.ExecutionException;
36+
import java.util.concurrent.ExecutorService;
37+
import java.util.concurrent.Executors;
38+
import java.util.concurrent.Future;
39+
import java.util.stream.IntStream;
40+
41+
import org.eclipse.digitaltwin.aas4j.v3.model.AssetAdministrationShell;
42+
import org.eclipse.digitaltwin.aas4j.v3.model.Key;
43+
import org.eclipse.digitaltwin.aas4j.v3.model.Reference;
44+
import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultAssetAdministrationShell;
45+
import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultSubmodel;
46+
import org.eclipse.digitaltwin.basyx.aasregistry.client.api.RegistryAndDiscoveryInterfaceApi;
47+
import org.eclipse.digitaltwin.basyx.aasrepository.AasRepository;
48+
import org.eclipse.digitaltwin.basyx.aasrepository.client.ConnectedAasRepository;
49+
import org.eclipse.digitaltwin.basyx.core.pagination.PaginationInfo;
50+
import org.eclipse.digitaltwin.basyx.submodelregistry.client.api.SubmodelRegistryApi;
51+
import org.eclipse.digitaltwin.basyx.submodelrepository.SubmodelRepository;
52+
import org.eclipse.digitaltwin.basyx.submodelrepository.client.ConnectedSubmodelRepository;
53+
import org.junit.After;
54+
import org.junit.AfterClass;
55+
import org.junit.BeforeClass;
56+
import org.junit.Test;
57+
import org.springframework.boot.SpringApplication;
58+
import org.springframework.context.ConfigurableApplicationContext;
59+
60+
public class TestConnectedAasManagerMultithreading {
61+
static final String AAS_REPOSITORY_BASE_PATH = "http://localhost:8081";
62+
static final String SM_REPOSITORY_BASE_PATH = "http://localhost:8081";
63+
static final String AAS_REGISTRY_BASE_PATH = "http://localhost:8050";
64+
static final String SM_REGISTRY_BASE_PATH = "http://localhost:8060";
65+
static final int N_THREADS = 20;
66+
67+
static ConfigurableApplicationContext appContext;
68+
static AasRepository aasRepository;
69+
static SubmodelRepository smRepository;
70+
71+
static ConnectedAasRepository connectedAasRepository;
72+
static ConnectedSubmodelRepository connectedSmRepository;
73+
static RegistryAndDiscoveryInterfaceApi aasRegistryApi;
74+
static SubmodelRegistryApi smRegistryApi;
75+
76+
static ConnectedAasManager aasManager;
77+
78+
@BeforeClass
79+
public static void setupRepositories() {
80+
appContext = new SpringApplication(DummyAasEnvironmentComponent.class).run(new String[] {});
81+
82+
connectedAasRepository = new ConnectedAasRepository(AAS_REPOSITORY_BASE_PATH);
83+
connectedSmRepository = new ConnectedSubmodelRepository(SM_REPOSITORY_BASE_PATH);
84+
aasRegistryApi = new RegistryAndDiscoveryInterfaceApi(AAS_REGISTRY_BASE_PATH);
85+
smRegistryApi = new SubmodelRegistryApi(SM_REGISTRY_BASE_PATH);
86+
aasManager = new ConnectedAasManager(AAS_REGISTRY_BASE_PATH, AAS_REPOSITORY_BASE_PATH, SM_REGISTRY_BASE_PATH, SM_REPOSITORY_BASE_PATH);
87+
88+
cleanUpRegistries();
89+
}
90+
91+
@After
92+
public void cleanUpComponents() {
93+
cleanUpRegistries();
94+
}
95+
96+
@AfterClass
97+
public static void stopContext() {
98+
appContext.close();
99+
}
100+
101+
@Test
102+
public void testParallelSubmodelCreation() throws ExecutionException, InterruptedException {
103+
AssetAdministrationShell shell = createShell();
104+
105+
ExecutorService executorService = Executors.newFixedThreadPool(N_THREADS);
106+
ConcurrentLinkedDeque<String> createdSubmodelIds = new ConcurrentLinkedDeque<>();
107+
108+
List<Future<Boolean>> futures = IntStream.range(0, N_THREADS).mapToObj(i -> executorService.submit(() -> createdSubmodelIds.add(createSubmodel(shell.getId(), i)))).toList();
109+
110+
try {
111+
for (int i = 0; i < N_THREADS; i++) {
112+
futures.get(i).get();
113+
}
114+
} finally {
115+
executorService.shutdown();
116+
}
117+
118+
createdSubmodelIds.forEach(submodelId -> assertSubmodelWasCreatedAndRegistered(shell.getId(), submodelId));
119+
}
120+
121+
static void assertSubmodelWasCreatedAndRegistered(String shellId, String submodelId) {
122+
assertEquals(submodelId, aasManager.getSubmodelService(submodelId).getSubmodel().getId());
123+
assertTrue(connectedAasRepository.getSubmodelReferences(shellId, PaginationInfo.NO_LIMIT).getResult().stream().map(Reference::getKeys).flatMap(Collection::stream).map(Key::getValue).anyMatch(submodelId::equals));
124+
}
125+
126+
127+
private static void cleanUpRegistries() {
128+
try {
129+
aasRegistryApi.deleteAllShellDescriptors();
130+
} catch (Exception e) {
131+
System.out.println(e.getMessage());
132+
}
133+
try {
134+
smRegistryApi.deleteAllSubmodelDescriptors();
135+
} catch (Exception e) {
136+
System.out.println(e.getMessage());
137+
}
138+
}
139+
140+
private static AssetAdministrationShell createShell() {
141+
String id = UUID.randomUUID().toString();
142+
DefaultAssetAdministrationShell shell = new DefaultAssetAdministrationShell.Builder().id(id).build();
143+
aasManager.createAas(shell);
144+
return aasManager.getAasService(id).getAAS();
145+
}
146+
147+
private static String createSubmodel(String aasId, int threadId) {
148+
try {
149+
String id = aasId + "-thread" + threadId;
150+
DefaultSubmodel submodel = new DefaultSubmodel.Builder().id(id).build();
151+
aasManager.createSubmodelInAas(aasId, submodel);
152+
return id;
153+
} catch (Exception e) {
154+
throw new RuntimeException("Failed at thread " + threadId, e);
155+
}
156+
}
157+
158+
}

basyx.aasregistry/basyx.aasregistry-client-native/src/main/java/org/eclipse/digitaltwin/basyx/aasregistry/main/client/factory/AasDescriptorFactory.java

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -53,31 +53,28 @@ public class AasDescriptorFactory {
5353
private static final String AAS_INTERFACE = "AAS-3.0";
5454
private static final String AAS_REPOSITORY_PATH = "shells";
5555

56-
private AssetAdministrationShell shell;
57-
private List<String> aasRepositoryURLs;
56+
private final List<String> aasRepositoryURLs;
57+
private static AttributeMapper attributeMapper;
5858

59-
private AttributeMapper attributeMapper;
60-
61-
public AasDescriptorFactory(AssetAdministrationShell shell, List<String> aasRepositoryBaseURLs, AttributeMapper attributeMapper) {
62-
this.shell = shell;
59+
public AasDescriptorFactory(List<String> aasRepositoryBaseURLs, AttributeMapper attributeMapper) {
6360
this.aasRepositoryURLs = createAasRepositoryUrls(aasRepositoryBaseURLs);
64-
this.attributeMapper = attributeMapper;
61+
AasDescriptorFactory.attributeMapper = attributeMapper;
6562
}
6663

6764
/**
6865
* Creates {@link AssetAdministrationShellDescriptor}
6966
*
7067
* @return the created AssetAdministrationShellDescriptor
7168
*/
72-
public AssetAdministrationShellDescriptor create() {
69+
public AssetAdministrationShellDescriptor create(AssetAdministrationShell shell) {
7370

7471
AssetAdministrationShellDescriptor descriptor = new AssetAdministrationShellDescriptor();
7572

7673
setId(shell.getId(), descriptor);
7774

7875
setIdShort(shell.getIdShort(), descriptor);
7976

80-
setEndpointItem(shell.getId(), descriptor);
77+
setEndpointItem(shell.getId(), descriptor, aasRepositoryURLs);
8178

8279
setDescription(shell.getDescription(), descriptor);
8380

@@ -96,68 +93,63 @@ public AssetAdministrationShellDescriptor create() {
9693
return descriptor;
9794
}
9895

99-
public AssetAdministrationShellDescriptor create(AssetAdministrationShell shell) {
100-
this.shell = shell;
101-
return create();
102-
}
103-
104-
private void setDescription(List<LangStringTextType> descriptions, AssetAdministrationShellDescriptor descriptor) {
96+
private static void setDescription(List<LangStringTextType> descriptions, AssetAdministrationShellDescriptor descriptor) {
10597

10698
if (descriptions == null || descriptions.isEmpty())
10799
return;
108100

109101
descriptor.setDescription(attributeMapper.mapDescription(descriptions));
110102
}
111103

112-
private void setDisplayName(List<LangStringNameType> displayNames, AssetAdministrationShellDescriptor descriptor) {
104+
private static void setDisplayName(List<LangStringNameType> displayNames, AssetAdministrationShellDescriptor descriptor) {
113105

114106
if (displayNames == null || displayNames.isEmpty())
115107
return;
116108

117109
descriptor.setDisplayName(attributeMapper.mapDisplayName(displayNames));
118110
}
119111

120-
private void setExtensions(List<Extension> extensions, AssetAdministrationShellDescriptor descriptor) {
112+
private static void setExtensions(List<Extension> extensions, AssetAdministrationShellDescriptor descriptor) {
121113

122114
if (extensions == null || extensions.isEmpty())
123115
return;
124116

125117
descriptor.setExtensions(attributeMapper.mapExtensions(extensions));
126118
}
127119

128-
private void setAdministration(AdministrativeInformation administration, AssetAdministrationShellDescriptor descriptor) {
120+
private static void setAdministration(AdministrativeInformation administration, AssetAdministrationShellDescriptor descriptor) {
129121

130122
if (administration == null)
131123
return;
132124

133125
descriptor.setAdministration(attributeMapper.mapAdministration(administration));
134126
}
135127

136-
private void setAssetKind(AssetInformation assetInformation, AssetAdministrationShellDescriptor descriptor) {
128+
private static void setAssetKind(AssetInformation assetInformation, AssetAdministrationShellDescriptor descriptor) {
137129

138130
if (assetInformation == null || assetInformation.getAssetKind() == null)
139131
return;
140132

141133
descriptor.setAssetKind(attributeMapper.mapAssetKind(assetInformation.getAssetKind()));
142134
}
143135

144-
private void setAssetType(AssetInformation assetInformation, AssetAdministrationShellDescriptor descriptor) {
136+
private static void setAssetType(AssetInformation assetInformation, AssetAdministrationShellDescriptor descriptor) {
145137

146138
if (assetInformation == null || assetInformation.getAssetType() == null)
147139
return;
148140

149141
descriptor.setAssetType(assetInformation.getAssetType());
150142
}
151143

152-
private void setGlobalAssetId(AssetInformation assetInformation, AssetAdministrationShellDescriptor descriptor) {
144+
private static void setGlobalAssetId(AssetInformation assetInformation, AssetAdministrationShellDescriptor descriptor) {
153145

154146
if (assetInformation == null || assetInformation.getGlobalAssetId() == null)
155147
return;
156148

157149
descriptor.setGlobalAssetId(assetInformation.getGlobalAssetId());
158150
}
159151

160-
private void setEndpointItem(String shellId, AssetAdministrationShellDescriptor descriptor) {
152+
private static void setEndpointItem(String shellId, AssetAdministrationShellDescriptor descriptor, List<String> aasRepositoryURLs) {
161153
for (String eachUrl : aasRepositoryURLs) {
162154
Endpoint endpoint = new Endpoint();
163155
endpoint.setInterface(AAS_INTERFACE);
@@ -168,7 +160,7 @@ private void setEndpointItem(String shellId, AssetAdministrationShellDescriptor
168160
}
169161
}
170162

171-
private ProtocolInformation createProtocolInformation(String shellId, String url) {
163+
private static ProtocolInformation createProtocolInformation(String shellId, String url) {
172164
String href = String.format("%s/%s", url, Base64UrlEncodedIdentifier.encodeIdentifier(shellId));
173165

174166
ProtocolInformation protocolInformation = new ProtocolInformation();
@@ -178,23 +170,23 @@ private ProtocolInformation createProtocolInformation(String shellId, String url
178170
return protocolInformation;
179171
}
180172

181-
private void setIdShort(String idShort, AssetAdministrationShellDescriptor descriptor) {
173+
private static void setIdShort(String idShort, AssetAdministrationShellDescriptor descriptor) {
182174
descriptor.setIdShort(idShort);
183175
}
184176

185-
private void setId(String shellId, AssetAdministrationShellDescriptor descriptor) {
177+
private static void setId(String shellId, AssetAdministrationShellDescriptor descriptor) {
186178
descriptor.setId(shellId);
187179
}
188180

189-
private String getProtocol(String endpoint) {
181+
private static String getProtocol(String endpoint) {
190182
try {
191183
return new URL(endpoint).getProtocol();
192184
} catch (MalformedURLException e) {
193185
throw new RuntimeException();
194186
}
195187
}
196188

197-
private List<String> createAasRepositoryUrls(List<String> aasRepositoryBaseURLs) {
189+
private static List<String> createAasRepositoryUrls(List<String> aasRepositoryBaseURLs) {
198190
List<String> toReturn = new ArrayList<>(aasRepositoryBaseURLs.size());
199191
for (String eachUrl : aasRepositoryBaseURLs) {
200192
toReturn.add(RepositoryUrlHelper.createRepositoryUrl(eachUrl, AAS_REPOSITORY_PATH));

basyx.aasrepository/basyx.aasrepository-feature-registry-integration/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/feature/registry/integration/RegistryIntegrationAasRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public AssetAdministrationShell getAas(String shellId) throws ElementDoesNotExis
7878

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

8383
decorated.createAas(shell);
8484

basyx.aasrepository/basyx.aasrepository-feature-registry-integration/src/test/java/org/eclipse/digitaltwin/basyx/aasrepository/feature/registry/integration/AasRepositoryRegistryLinkDescriptorGenerationTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ public void testExternalUrl() {
103103
private AssetAdministrationShellDescriptor createAndRetrieveDescriptor(AssetAdministrationShell shell) {
104104
registryIntegrationAasRepository.createAas(shell);
105105

106-
AasDescriptorFactory descriptorFactory = new AasDescriptorFactory(shell, mockedRegistryLink.getAasRepositoryBaseURLs(), mockedAttributeMapper);
107-
return descriptorFactory.create();
106+
AasDescriptorFactory descriptorFactory = new AasDescriptorFactory(mockedRegistryLink.getAasRepositoryBaseURLs(), mockedAttributeMapper);
107+
return descriptorFactory.create(shell);
108108
}
109109

110110
private AssetAdministrationShell createDummyAas() {

basyx.aasservice/basyx.aasservice-backend-inmemory/src/main/java/org/eclipse/digitaltwin/basyx/aasservice/backend/InMemoryAasService.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,16 +93,19 @@ public CursorResult<List<Reference>> getSubmodelReferences(PaginationInfo pInfo)
9393

9494
@Override
9595
public void addSubmodelReference(Reference submodelReference) {
96-
throwExceptionIfReferenceIsAlreadyPresent(submodelReference);
97-
98-
aas.getSubmodels().add(submodelReference);
96+
List<Reference> submodelsRefs = aas.getSubmodels();
97+
synchronized (submodelsRefs) {
98+
throwExceptionIfReferenceIsAlreadyPresent(submodelReference);
99+
submodelsRefs.add(submodelReference);
100+
}
99101
}
100102

101103
@Override
102104
public void removeSubmodelReference(String submodelId) {
103-
Reference specificSubmodelReference = getSubmodelReferenceById(submodelId);
104-
105-
aas.getSubmodels().remove(specificSubmodelReference);
105+
List<Reference> submodelsRefs = aas.getSubmodels();
106+
synchronized (submodelsRefs) {
107+
submodelsRefs.remove(getSubmodelReferenceById(submodelId));
108+
}
106109
}
107110

108111
@Override

0 commit comments

Comments
 (0)