|
| 1 | +package com.netflix.hollow.core.write; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertTrue; |
| 5 | + |
| 6 | +import com.netflix.hollow.api.consumer.HollowConsumer; |
| 7 | +import com.netflix.hollow.api.producer.HollowProducer; |
| 8 | +import com.netflix.hollow.api.producer.fs.HollowInMemoryBlobStager; |
| 9 | +import com.netflix.hollow.test.InMemoryBlobStore; |
| 10 | +import java.util.Arrays; |
| 11 | +import java.util.HashMap; |
| 12 | +import java.util.HashSet; |
| 13 | +import java.util.List; |
| 14 | +import java.util.Map; |
| 15 | +import java.util.Set; |
| 16 | +import org.junit.Test; |
| 17 | + |
| 18 | +public class HollowTypeWriteStateTest { |
| 19 | + |
| 20 | + @Test |
| 21 | + public void testReverseDeltaNumShardsWhenNewTypes() { |
| 22 | + InMemoryBlobStore blobStore = new InMemoryBlobStore(); |
| 23 | + HollowInMemoryBlobStager blobStager = new HollowInMemoryBlobStager(); |
| 24 | + |
| 25 | + HollowProducer p1 = HollowProducer.withPublisher(blobStore).withBlobStager(blobStager).build(); |
| 26 | + long v1 = p1.runCycle(ws -> ws.add("s1")); |
| 27 | + |
| 28 | + // add a new object type and all collection types to data model |
| 29 | + HollowProducer p2 = HollowProducer.withPublisher(blobStore).withBlobStager(blobStager).build(); |
| 30 | + p2.initializeDataModel(HasAllTypes.class); |
| 31 | + p2.restore(v1, blobStore); |
| 32 | + long v2 = p2.runCycle(state -> { |
| 33 | + HasAllTypes o1 = new HasAllTypes( |
| 34 | + new CustomReferenceType(5l), |
| 35 | + new HashSet<>(Arrays.asList("e1")), |
| 36 | + Arrays.asList(1, 2, 3), |
| 37 | + new HashMap<String, Long>(){{put("k1", 1L);}} |
| 38 | + ); |
| 39 | + state.add(o1); |
| 40 | + }); |
| 41 | + |
| 42 | + HollowConsumer consumer = HollowConsumer.withBlobRetriever(blobStore) |
| 43 | + .withDoubleSnapshotConfig(new HollowConsumer.DoubleSnapshotConfig() { |
| 44 | + @Override |
| 45 | + public boolean allowDoubleSnapshot() { |
| 46 | + return false; |
| 47 | + } |
| 48 | + @Override |
| 49 | + public int maxDeltasBeforeDoubleSnapshot() { |
| 50 | + return Integer.MAX_VALUE; |
| 51 | + } |
| 52 | + }) |
| 53 | + .build(); |
| 54 | + consumer.triggerRefreshTo(v2); |
| 55 | + |
| 56 | + int numShardsObject = consumer.getStateEngine().getTypeState("CustomReferenceType").numShards(); |
| 57 | + assertTrue(numShardsObject > 0); |
| 58 | + int numShardsList = consumer.getStateEngine().getTypeState("ListOfInteger").numShards(); |
| 59 | + assertTrue(numShardsList > 0); |
| 60 | + int numShardsSet = consumer.getStateEngine().getTypeState("SetOfString").numShards(); |
| 61 | + assertTrue(numShardsSet > 0); |
| 62 | + int numShardsMap = consumer.getStateEngine().getTypeState("MapOfStringToLong").numShards(); |
| 63 | + assertTrue(numShardsMap > 0); |
| 64 | + |
| 65 | + consumer.triggerRefreshTo(v1); |
| 66 | + assertEquals(v1, consumer.getCurrentVersionId()); |
| 67 | + assertEquals(numShardsObject, consumer.getStateEngine().getTypeState("CustomReferenceType").numShards()); |
| 68 | + assertEquals(numShardsList, consumer.getStateEngine().getTypeState("ListOfInteger").numShards()); |
| 69 | + assertEquals(numShardsSet, consumer.getStateEngine().getTypeState("SetOfString").numShards()); |
| 70 | + assertEquals(numShardsMap, consumer.getStateEngine().getTypeState("MapOfStringToLong").numShards()); |
| 71 | + } |
| 72 | + |
| 73 | + private class HasAllTypes { |
| 74 | + CustomReferenceType customReferenceType; |
| 75 | + Set<String> setOfStrings; |
| 76 | + List<Integer> listOfInt; |
| 77 | + Map<String, Long> mapOfStringToLong; |
| 78 | + |
| 79 | + private HasAllTypes(CustomReferenceType customReferenceType, Set<String> setOfStrings, List<Integer> listOfInt, Map<String, Long> mapOfStringToLong) { |
| 80 | + this.customReferenceType = customReferenceType; |
| 81 | + this.setOfStrings = setOfStrings; |
| 82 | + this.listOfInt = listOfInt; |
| 83 | + this.mapOfStringToLong = mapOfStringToLong; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + private class CustomReferenceType { |
| 88 | + long id; |
| 89 | + private CustomReferenceType(long id) { |
| 90 | + this.id = id; |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments