Skip to content

Commit 5e4cb58

Browse files
committed
change consumer to read metadata in blob header for version number
1 parent e7ce7db commit 5e4cb58

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

hollow/src/main/java/com/netflix/hollow/api/client/HollowDataHolder.java

+19-1
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,12 @@
3333
import com.netflix.hollow.tools.history.HollowHistoricalStateDataAccess;
3434
import java.io.IOException;
3535
import java.lang.ref.WeakReference;
36+
import java.util.Optional;
37+
import java.util.OptionalLong;
3638
import java.util.logging.Logger;
3739

40+
import static com.netflix.hollow.core.HollowStateEngine.HEADER_TAG_PRODUCER_TO_VERSION;
41+
3842
/**
3943
* A class comprising much of the internal state of a {@link HollowConsumer}. Not intended for external consumption.
4044
*/
@@ -177,7 +181,21 @@ private void applyStateEngineTransition(HollowBlobInput in, OptionalBlobPartInpu
177181
reader.applyDelta(in, optionalPartIn);
178182
}
179183

180-
setVersion(transition.getToVersion());
184+
long expectedToVersion = transition.getToVersion();
185+
Long actualToVersion = null;
186+
String actualToVersionStr = stateEngine.getHeaderTag(HEADER_TAG_PRODUCER_TO_VERSION);
187+
if (actualToVersionStr != null) {
188+
actualToVersion = Long.valueOf(actualToVersionStr);
189+
}
190+
191+
if (actualToVersion != null) {
192+
if (actualToVersion.longValue() != expectedToVersion) {
193+
LOG.warning("toVersion in blob didn't match toVersion seen in metadata; actualToVersion=" + actualToVersion + ", expectedToVersion=" + expectedToVersion);
194+
}
195+
setVersion(actualToVersion.longValue());
196+
} else {
197+
setVersion(transition.getToVersion());
198+
}
181199

182200
for(HollowConsumer.RefreshListener refreshListener : refreshListeners)
183201
refreshListener.blobLoaded(transition);

hollow/src/test/java/com/netflix/hollow/api/client/HollowClientUpdaterTest.java

+24
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import static com.netflix.hollow.core.HollowConstants.VERSION_LATEST;
2020
import static com.netflix.hollow.core.HollowConstants.VERSION_NONE;
21+
import static com.netflix.hollow.core.HollowStateEngine.HEADER_TAG_PRODUCER_TO_VERSION;
2122
import static com.netflix.hollow.core.HollowStateEngine.HEADER_TAG_SCHEMA_HASH;
2223
import static java.util.Collections.emptyList;
2324
import static org.junit.Assert.assertEquals;
@@ -46,6 +47,7 @@
4647
import com.netflix.hollow.test.consumer.TestHollowConsumer;
4748
import java.io.ByteArrayInputStream;
4849
import java.io.ByteArrayOutputStream;
50+
import java.io.IOException;
4951
import java.util.HashMap;
5052
import java.util.Map;
5153
import java.util.Optional;
@@ -55,6 +57,8 @@
5557
import java.util.logging.LogManager;
5658
import java.util.logging.LogRecord;
5759
import java.util.logging.Logger;
60+
61+
import org.junit.Assert;
5862
import org.junit.Before;
5963
import org.junit.Rule;
6064
import org.junit.Test;
@@ -98,6 +102,26 @@ public void testUpdateTo_noVersions() throws Throwable {
98102
assertTrue("Should still have no types", readStateEngine.getAllTypes().isEmpty());
99103
}
100104

105+
@Test
106+
public void testUpdateTo_updateToLatestButBlobOnDifferentVersion() throws Throwable {
107+
long updateToVersion = 1234;
108+
long blobToVersion = 1235; //this needs to be different from updateToVersion
109+
HollowWriteStateEngine writeStateEngine = new HollowWriteStateEngine();
110+
writeStateEngine.addHeaderTag(HEADER_TAG_PRODUCER_TO_VERSION, String.valueOf(blobToVersion));
111+
ByteArrayOutputStream os = new ByteArrayOutputStream();
112+
new HollowBlobWriter(writeStateEngine).writeSnapshot(os);
113+
ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
114+
HollowConsumer.Blob blob = mock(HollowConsumer.Blob.class);
115+
when(blob.isSnapshot())
116+
.thenReturn(true);
117+
when(blob.getInputStream())
118+
.thenReturn(is);
119+
when(retriever.retrieveSnapshotBlob(anyLong()))
120+
.thenReturn(blob);
121+
subject.updateTo(updateToVersion);
122+
Assert.assertEquals(subject.getCurrentVersionId(), blobToVersion);
123+
}
124+
101125
@Rule
102126
public ExpectedException expectedException = ExpectedException.none();
103127

0 commit comments

Comments
 (0)