Skip to content

Commit 38a1348

Browse files
authored
fix: update data model for infinispan (#2156)
* update data model for infinispan Signed-off-by: achmelo <a.chmelo@gmail.com> * client cert for profiling Signed-off-by: achmelo <a.chmelo@gmail.com> * storage tests Signed-off-by: achmelo <a.chmelo@gmail.com> * fix code smells Signed-off-by: achmelo <a.chmelo@gmail.com>
1 parent 52df447 commit 38a1348

File tree

3 files changed

+82
-62
lines changed

3 files changed

+82
-62
lines changed

caching-service/src/main/java/org/zowe/apiml/caching/service/infinispan/storage/InfinispanStorage.java

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,71 +10,66 @@
1010
package org.zowe.apiml.caching.service.infinispan.storage;
1111

1212
import lombok.extern.slf4j.Slf4j;
13-
import org.infinispan.Cache;
1413
import org.zowe.apiml.caching.model.KeyValue;
1514
import org.zowe.apiml.caching.service.Messages;
1615
import org.zowe.apiml.caching.service.Storage;
1716
import org.zowe.apiml.caching.service.StorageException;
1817

1918
import java.util.HashMap;
2019
import java.util.Map;
20+
import java.util.concurrent.ConcurrentMap;
2121

2222
@Slf4j
2323
public class InfinispanStorage implements Storage {
2424

2525

26-
private final Cache<String, Map<String, KeyValue>> cache;
26+
private final ConcurrentMap<String, KeyValue> cache;
2727

28-
public InfinispanStorage(Cache<String, Map<String, KeyValue>> cache) {
28+
public InfinispanStorage(ConcurrentMap<String, KeyValue> cache) {
2929
this.cache = cache;
3030
}
3131

3232
@Override
3333
public KeyValue create(String serviceId, KeyValue toCreate) {
34+
toCreate.setServiceId(serviceId);
3435
log.info("Writing record: {}|{}|{}", serviceId, toCreate.getKey(), toCreate.getValue());
3536

36-
Map<String, KeyValue> serviceCache = cache.computeIfAbsent(serviceId, k -> new HashMap<>());
37+
KeyValue serviceCache = cache.putIfAbsent(serviceId + toCreate.getKey(), toCreate);
3738

38-
if (serviceCache.containsKey(toCreate.getKey())) {
39+
if (serviceCache != null) {
3940
throw new StorageException(Messages.DUPLICATE_KEY.getKey(), Messages.DUPLICATE_KEY.getStatus(), toCreate.getKey());
4041
}
41-
KeyValue entry = serviceCache.put(toCreate.getKey(), toCreate);
42-
cache.put(serviceId, serviceCache);
43-
return entry;
42+
return null;
4443
}
4544

4645
@Override
4746
public KeyValue read(String serviceId, String key) {
4847
log.info("Reading record for service {} under key {}", serviceId, key);
49-
Map<String, KeyValue> serviceCache = cache.get(serviceId);
50-
if (serviceCache != null && serviceCache.containsKey(key)) {
51-
return serviceCache.get(key);
48+
KeyValue serviceCache = cache.get(serviceId + key);
49+
if (serviceCache != null) {
50+
return serviceCache;
5251
} else {
5352
throw new StorageException(Messages.KEY_NOT_IN_CACHE.getKey(), Messages.KEY_NOT_IN_CACHE.getStatus(), key, serviceId);
5453
}
5554
}
5655

5756
@Override
5857
public KeyValue update(String serviceId, KeyValue toUpdate) {
58+
toUpdate.setServiceId(serviceId);
5959
log.info("Updating record for service {} under key {}", serviceId, toUpdate);
60-
Map<String, KeyValue> serviceCache = cache.get(serviceId);
61-
if (serviceCache == null || !serviceCache.containsKey(toUpdate.getKey())) {
60+
KeyValue serviceCache = cache.put(serviceId + toUpdate.getKey(), toUpdate);
61+
if (serviceCache == null) {
6262
throw new StorageException(Messages.KEY_NOT_IN_CACHE.getKey(), Messages.KEY_NOT_IN_CACHE.getStatus(), toUpdate.getKey(), serviceId);
6363
}
64-
serviceCache.put(toUpdate.getKey(), toUpdate);
65-
cache.put(serviceId, serviceCache);
6664
return toUpdate;
6765

6866
}
6967

7068
@Override
7169
public KeyValue delete(String serviceId, String toDelete) {
7270
log.info("Removing record for service {} under key {}", serviceId, toDelete);
73-
Map<String, KeyValue> serviceCache = cache.get(serviceId);
74-
KeyValue entry;
75-
if (serviceCache.containsKey(toDelete)) {
76-
entry = serviceCache.remove(toDelete);
77-
cache.put(serviceId, serviceCache);
71+
KeyValue entry = cache.remove(serviceId + toDelete);
72+
if (entry != null) {
7873
return entry;
7974
} else {
8075
throw new StorageException(Messages.KEY_NOT_IN_CACHE.getKey(), Messages.KEY_NOT_IN_CACHE.getStatus(), toDelete, serviceId);
@@ -84,12 +79,22 @@ public KeyValue delete(String serviceId, String toDelete) {
8479
@Override
8580
public Map<String, KeyValue> readForService(String serviceId) {
8681
log.info("Reading all records for service {} ", serviceId);
87-
return cache.get(serviceId);
82+
Map<String, KeyValue> result = new HashMap<>();
83+
cache.forEach((key, value) -> {
84+
if (serviceId.equals(value.getServiceId())) {
85+
result.put(value.getKey(), value);
86+
}
87+
});
88+
return result;
8889
}
8990

9091
@Override
9192
public void deleteForService(String serviceId) {
9293
log.info("Removing all records for service {} ", serviceId);
93-
cache.remove(serviceId);
94+
cache.forEach((key, value) -> {
95+
if (value.getServiceId().equals(serviceId)) {
96+
cache.remove(key);
97+
}
98+
});
9499
}
95100
}

caching-service/src/test/java/org/zowe/apiml/caching/service/infinispan/storage/InfinispanStorageTest.java

Lines changed: 54 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
import org.zowe.apiml.caching.model.KeyValue;
1717
import org.zowe.apiml.caching.service.StorageException;
1818

19-
import java.util.HashMap;
20-
import java.util.Map;
19+
import java.util.concurrent.ConcurrentHashMap;
20+
import java.util.concurrent.ConcurrentMap;
2121

2222
import static org.junit.jupiter.api.Assertions.*;
2323
import static org.mockito.Mockito.*;
@@ -26,7 +26,7 @@ class InfinispanStorageTest {
2626

2727
public static final KeyValue TO_CREATE = new KeyValue("key1", "val1");
2828
public static final KeyValue TO_UPDATE = new KeyValue("key1", "val2");
29-
Cache<String, Map<String, KeyValue>> cache;
29+
Cache<String, KeyValue> cache;
3030
InfinispanStorage storage;
3131
String serviceId1 = "service1";
3232

@@ -39,86 +39,101 @@ void setup() {
3939
@Nested
4040
class WhenEntryDoesntExist {
4141

42-
Map<String, KeyValue> serviceStore;
42+
KeyValue keyValue;
4343

4444
@BeforeEach
4545
void createEmptyStore() {
46-
serviceStore = new HashMap<>();
47-
}
48-
49-
@Test
50-
void whenCreate_thenCacheIsUpdated() {
51-
when(cache.computeIfAbsent(any(), any())).thenReturn(serviceStore);
52-
storage.create(serviceId1, TO_CREATE);
53-
verify(cache, times(1)).put(serviceId1, serviceStore);
46+
keyValue = null;
5447
}
5548

5649
@Test
5750
void whenRead_thenExceptionIsThrown() {
5851
String key = TO_CREATE.getKey();
59-
when(cache.get(serviceId1)).thenReturn(serviceStore);
52+
when(cache.get(serviceId1)).thenReturn(keyValue);
6053
assertThrows(StorageException.class, () -> storage.read(serviceId1, key));
6154
}
6255

6356
@Test
64-
void whenDelete_thenExceptionIsThrown() {
57+
void whenUpdate_thenExceptionIsThrown() {
58+
KeyValue entry = new KeyValue("key", "value");
59+
when(cache.get(serviceId1)).thenReturn(keyValue);
60+
assertThrows(StorageException.class, () -> storage.update(serviceId1, entry));
61+
}
6562

66-
String key = TO_CREATE.getKey();
67-
when(cache.get(serviceId1)).thenReturn(serviceStore);
68-
assertThrows(StorageException.class, () -> storage.delete(serviceId1, key));
69-
verify(cache, times(0)).put(serviceId1, serviceStore);
63+
@Test
64+
void whenAddNew_returnNull() {
65+
keyValue = new KeyValue("key", "value");
66+
assertNull(storage.create(serviceId1, keyValue));
7067
}
7168

7269
@Test
73-
void whenUpdate_thenCacheIsUpdated() {
70+
void whenDelete_thenExceptionIsThrown() {
7471

75-
when(cache.get(serviceId1)).thenReturn(serviceStore);
76-
assertThrows(StorageException.class, () -> storage.update(serviceId1, TO_UPDATE));
77-
verify(cache, times(0)).put(serviceId1, serviceStore);
72+
String key = TO_CREATE.getKey();
73+
when(cache.remove(serviceId1 + key)).thenReturn(null);
74+
assertThrows(StorageException.class, () -> storage.delete(serviceId1, key));
7875
}
76+
7977
}
8078

8179

8280
@Nested
8381
class WhenEntryExists {
84-
Map<String, KeyValue> serviceStore;
82+
KeyValue keyValue;
8583

8684
@BeforeEach
8785
void createStoreWithEntry() {
88-
serviceStore = new HashMap<>();
89-
serviceStore.put(TO_CREATE.getKey(), TO_CREATE);
86+
keyValue = TO_CREATE;
9087
}
9188

9289
@Test
93-
void whenCreate_thenExceptionIsThrown() {
94-
when(cache.computeIfAbsent(any(), any())).thenReturn(serviceStore);
90+
void exceptionIsThrown() {
91+
when(cache.putIfAbsent(any(), any())).thenReturn(keyValue);
9592
assertThrows(StorageException.class, () -> storage.create(serviceId1, TO_CREATE));
9693
}
9794

9895
@Test
99-
void whenRead_thenEntryIsReturned() {
100-
when(cache.get(serviceId1)).thenReturn(serviceStore);
96+
void entryIsReturned() {
97+
when(cache.get(serviceId1 + TO_CREATE.getKey())).thenReturn(TO_CREATE);
10198
KeyValue result = storage.read(serviceId1, TO_CREATE.getKey());
10299
assertEquals(TO_CREATE.getValue(), result.getValue());
103100
}
104101

105102
@Test
106-
void whenUpdate_thenCacheIsUpdated() {
103+
void cacheIsUpdated() {
107104

108-
when(cache.get(serviceId1)).thenReturn(serviceStore);
105+
when(cache.put(serviceId1 + TO_UPDATE.getKey(), TO_UPDATE)).thenReturn(TO_UPDATE);
109106
storage.update(serviceId1, TO_UPDATE);
110-
verify(cache, times(1)).put(serviceId1, serviceStore);
111-
assertEquals("val2", serviceStore.get(TO_CREATE.getKey()).getValue());
107+
verify(cache, times(1)).put(serviceId1 + TO_UPDATE.getKey(), TO_UPDATE);
108+
assertEquals("val2", TO_UPDATE.getValue());
112109
}
113110

114111
@Test
115-
void whenDelete_thenCacheIsUpdated() {
112+
void itemIsDeleted() {
113+
ConcurrentMap<String, KeyValue> cache = new ConcurrentHashMap<>();
114+
InfinispanStorage storage = new InfinispanStorage(cache);
115+
assertNull(storage.create(serviceId1, TO_CREATE));
116+
assertEquals(TO_CREATE, storage.delete(serviceId1, TO_CREATE.getKey()));
117+
}
116118

117-
when(cache.get(serviceId1)).thenReturn(serviceStore);
118-
KeyValue result = storage.delete(serviceId1, TO_CREATE.getKey());
119-
verify(cache, times(1)).put(serviceId1, serviceStore);
120-
assertEquals(TO_CREATE.getValue(), result.getValue());
121-
assertNull(serviceStore.get(TO_CREATE.getKey()));
119+
@Test
120+
void returnAll() {
121+
ConcurrentMap<String, KeyValue> cache = new ConcurrentHashMap<>();
122+
InfinispanStorage storage = new InfinispanStorage(cache);
123+
storage.create(serviceId1, new KeyValue("key", "value"));
124+
storage.create(serviceId1, new KeyValue("key2", "value2"));
125+
assertEquals(2, storage.readForService(serviceId1).size());
126+
}
127+
128+
@Test
129+
void removeAll() {
130+
ConcurrentMap<String, KeyValue> cache = new ConcurrentHashMap<>();
131+
InfinispanStorage storage = new InfinispanStorage(cache);
132+
storage.create(serviceId1, new KeyValue("key", "value"));
133+
storage.create(serviceId1, new KeyValue("key2", "value2"));
134+
assertEquals(2, storage.readForService(serviceId1).size());
135+
storage.deleteForService(serviceId1);
136+
assertEquals(0, storage.readForService(serviceId1).size());
122137
}
123138

124139
}

config/profiling/run-profiling.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,4 @@ then
7676
fi
7777
fi
7878

79-
jmeter -Jhost=$host -Jport=$port -Jthreads=$threads -Jdataset=$dataset -Jjmeter.reportgenerator.overall_granularity=1000 -n -t caching-profiling-parametrized.jmx -l $dir/result -e -o $dir/test-results -j $dir/result.log
79+
jmeter -D javax.net.ssl.keyStore=../../keystore/client_cert/client-certs.p12 -D javax.net.ssl.keyStorePassword=password -Jhost=$host -Jport=$port -Jthreads=$threads -Jdataset=$dataset -Jjmeter.reportgenerator.overall_granularity=1000 -n -t caching-profiling-parametrized.jmx -l $dir/result -e -o $dir/test-results -j $dir/result.log

0 commit comments

Comments
 (0)