Skip to content

Commit add4e67

Browse files
committed
fix(spanner): skip empty async cache updates
1 parent 8883b89 commit add4e67

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

java-spanner/google-cloud-spanner/src/main/java/com/google/cloud/spanner/spi/v1/ChannelFinder.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,20 @@ private PendingCacheUpdate(CacheUpdate update) {
117117
}
118118
}
119119

120+
private boolean isMaterialUpdate(CacheUpdate update) {
121+
return update.getGroupCount() > 0
122+
|| update.getRangeCount() > 0
123+
|| (update.hasKeyRecipes() && update.getKeyRecipes().getRecipeCount() > 0);
124+
}
125+
126+
private boolean shouldProcessUpdate(CacheUpdate update) {
127+
if (isMaterialUpdate(update)) {
128+
return true;
129+
}
130+
long updateDatabaseId = update.getDatabaseId();
131+
return updateDatabaseId != 0 && databaseId.get() != updateDatabaseId;
132+
}
133+
120134
public void update(CacheUpdate update) {
121135
synchronized (updateLock) {
122136
long currentId = databaseId.get();
@@ -154,6 +168,9 @@ public void update(CacheUpdate update) {
154168
}
155169

156170
public void updateAsync(CacheUpdate update) {
171+
if (!shouldProcessUpdate(update)) {
172+
return;
173+
}
157174
pendingUpdates.add(new PendingCacheUpdate(update));
158175
if (drainScheduled.compareAndSet(false, true)) {
159176
java.util.concurrent.CountDownLatch latch = new java.util.concurrent.CountDownLatch(1);

java-spanner/google-cloud-spanner/src/test/java/com/google/cloud/spanner/spi/v1/ChannelFinderTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,30 @@ public void updateIgnoresZeroDatabaseIdAndKeepsExistingCache() throws Exception
8888
assertThat(rangeCache(finder).size()).isEqualTo(1);
8989
}
9090

91+
@Test
92+
public void updateAsyncSkipsTrulyEmptyUpdateForCurrentDatabase() throws Exception {
93+
ChannelFinder finder = new ChannelFinder(new FakeEndpointCache());
94+
finder.update(singleRangeUpdate(0));
95+
96+
finder.updateAsync(CacheUpdate.newBuilder().setDatabaseId(7L).build());
97+
finder.awaitPendingUpdates();
98+
99+
assertThat(databaseId(finder)).isEqualTo(7L);
100+
assertThat(rangeCache(finder).size()).isEqualTo(1);
101+
}
102+
103+
@Test
104+
public void updateAsyncProcessesDatabaseTransitionWithoutRangesOrGroups() throws Exception {
105+
ChannelFinder finder = new ChannelFinder(new FakeEndpointCache());
106+
finder.update(singleRangeUpdate(0));
107+
108+
finder.updateAsync(CacheUpdate.newBuilder().setDatabaseId(9L).build());
109+
finder.awaitPendingUpdates();
110+
111+
assertThat(databaseId(finder)).isEqualTo(9L);
112+
assertThat(rangeCache(finder).size()).isEqualTo(0);
113+
}
114+
91115
private static CacheUpdate singleRangeUpdate(int index) {
92116
String startKey = String.format("k%05d", index);
93117
String limitKey = String.format("k%05d", index + 1);

0 commit comments

Comments
 (0)