Skip to content

Commit e527c6e

Browse files
committed
ZKMetadataStore: don't react on the root creation
There is a race condition when we add a listener to the cache and then immediately create a root for the tree: this event may be caught or not. Either way we don't to react for such an event
1 parent abd94bd commit e527c6e

File tree

1 file changed

+20
-38
lines changed

1 file changed

+20
-38
lines changed

spring-integration-zookeeper/src/main/java/org/springframework/integration/zookeeper/metadata/ZookeeperMetadataStore.java

Lines changed: 20 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,12 @@ public String putIfAbsent(String key, String value) {
118118
createNode(key, value);
119119
return null;
120120
}
121-
catch (KeeperException.NodeExistsException e) {
121+
catch (KeeperException.NodeExistsException ex) {
122122
// so the data actually exists, we can read it
123123
return get(key);
124124
}
125-
catch (Exception e) {
126-
throw new ZookeeperMetadataStoreException("Error while trying to set '" + key + "':", e);
125+
catch (Exception ex) {
126+
throw new ZookeeperMetadataStoreException("Error while trying to set '" + key + "':", ex);
127127
}
128128
}
129129
}
@@ -142,13 +142,13 @@ public boolean replace(String key, String oldValue, String newValue) {
142142
}
143143
return true;
144144
}
145-
catch (KeeperException.NoNodeException | KeeperException.BadVersionException e) {
145+
catch (KeeperException.NoNodeException | KeeperException.BadVersionException ex) {
146146
// ignore, the node doesn't exist there's nothing to replace
147147
return false;
148148
}
149149
// ignore
150-
catch (Exception e) {
151-
throw new ZookeeperMetadataStoreException("Cannot replace value", e);
150+
catch (Exception ex) {
151+
throw new ZookeeperMetadataStoreException("Cannot replace value", ex);
152152
}
153153
}
154154
}
@@ -183,8 +183,8 @@ public void put(String key, String value) {
183183
updateNode(key, value, -1);
184184
}
185185
}
186-
catch (Exception e) {
187-
throw new ZookeeperMetadataStoreException("Error while setting value for key '" + key + "':", e);
186+
catch (Exception ex) {
187+
throw new ZookeeperMetadataStoreException("Error while setting value for key '" + key + "':", ex);
188188
}
189189
}
190190
}
@@ -198,17 +198,17 @@ public String get(String key) {
198198
.map(currentData -> {
199199
// our version is more recent than the cache
200200
if (this.updateMap.containsKey(key) &&
201-
this.updateMap.get(key).getVersion() >= currentData.getStat().getVersion()) {
201+
this.updateMap.get(key).version() >= currentData.getStat().getVersion()) {
202202

203-
return this.updateMap.get(key).getValue();
203+
return this.updateMap.get(key).value();
204204
}
205205
return IntegrationUtils.bytesToString(currentData.getData(), this.encoding);
206206
})
207207
.orElseGet(() -> {
208208
if (this.updateMap.containsKey(key)) {
209209
// we have saved the value, but the cache hasn't updated yet
210210
// if the value had changed via replication, we would have been notified by the listener
211-
return this.updateMap.get(key).getValue();
211+
return this.updateMap.get(key).value();
212212
}
213213
else {
214214
// the value just doesn't exist
@@ -229,12 +229,12 @@ public String remove(String key) {
229229
this.updateMap.put(key, new LocalChildData(null, Integer.MAX_VALUE));
230230
return IntegrationUtils.bytesToString(bytes, this.encoding);
231231
}
232-
catch (KeeperException.NoNodeException e) {
232+
catch (KeeperException.NoNodeException ex) {
233233
// ignore - the node doesn't exist
234234
return null;
235235
}
236-
catch (Exception e) {
237-
throw new ZookeeperMetadataStoreException("Exception while deleting key '" + key + "'", e);
236+
catch (Exception ex) {
237+
throw new ZookeeperMetadataStoreException("Exception while deleting key '" + key + "'", ex);
238238
}
239239
}
240240
}
@@ -254,7 +254,6 @@ public String getPath(String key) {
254254
return "".equals(key) ? this.root : this.root + '/' + key;
255255
}
256256

257-
258257
@Override
259258
public boolean isAutoStartup() {
260259
return this.autoStartup;
@@ -268,14 +267,14 @@ public synchronized void start() {
268267
.creatingParentContainersIfNeeded()
269268
.forPath(this.root);
270269

270+
this.client.createContainers(this.root);
271271
this.cache = CuratorCache.builder(this.client, this.root).build();
272272
this.cache.listenable().addListener(new MetadataStoreCacheListener());
273-
this.client.createContainers(this.root);
274273
this.cache.start();
275274
this.running = true;
276275
}
277-
catch (Exception e) {
278-
throw new ZookeeperMetadataStoreException("Exception while starting bean", e);
276+
catch (Exception ex) {
277+
throw new ZookeeperMetadataStoreException("Exception while starting bean", ex);
279278
}
280279
}
281280
}
@@ -306,24 +305,7 @@ private String getKey(String path) {
306305
}
307306

308307

309-
private static final class LocalChildData {
310-
311-
private final String value;
312-
313-
private final int version;
314-
315-
LocalChildData(String value, int version) {
316-
this.value = value;
317-
this.version = version;
318-
}
319-
320-
private String getValue() {
321-
return this.value;
322-
}
323-
324-
private int getVersion() {
325-
return this.version;
326-
}
308+
private record LocalChildData(String value, int version) {
327309

328310
}
329311

@@ -343,7 +325,7 @@ public void event(Type type, ChildData oldData, ChildData newData) {
343325
case NODE_CREATED:
344326
if (ZookeeperMetadataStore.this.updateMap.containsKey(eventKey) &&
345327
data.getStat().getVersion() >=
346-
ZookeeperMetadataStore.this.updateMap.get(eventKey).getVersion()) {
328+
ZookeeperMetadataStore.this.updateMap.get(eventKey).version()) {
347329

348330
ZookeeperMetadataStore.this.updateMap.remove(eventPath);
349331
}
@@ -352,7 +334,7 @@ public void event(Type type, ChildData oldData, ChildData newData) {
352334
case NODE_CHANGED:
353335
if (ZookeeperMetadataStore.this.updateMap.containsKey(eventKey) &&
354336
data.getStat().getVersion() >=
355-
ZookeeperMetadataStore.this.updateMap.get(eventKey).getVersion()) {
337+
ZookeeperMetadataStore.this.updateMap.get(eventKey).version()) {
356338

357339
ZookeeperMetadataStore.this.updateMap.remove(eventPath);
358340
}

0 commit comments

Comments
 (0)