@@ -614,5 +614,113 @@ func TestSmallBatchSize(t *testing.T, dbProvider statedb.VersionedDBProvider) {
614
614
615
615
vv , _ = db .GetState ("ns1" , "key11" )
616
616
testutil .AssertEquals (t , vv .Value , jsonValue11 )
617
+ }
618
+
619
+ // TestBatchWithIndividualRetry tests a single failure in a batch
620
+ func TestBatchWithIndividualRetry (t * testing.T , dbProvider statedb.VersionedDBProvider ) {
621
+
622
+ db , err := dbProvider .GetDBHandle ("testbatchretry" )
623
+ testutil .AssertNoError (t , err , "" )
624
+
625
+ batch := statedb .NewUpdateBatch ()
626
+ vv1 := statedb.VersionedValue {Value : []byte ("value1" ), Version : version .NewHeight (1 , 1 )}
627
+ vv2 := statedb.VersionedValue {Value : []byte ("value2" ), Version : version .NewHeight (1 , 2 )}
628
+ vv3 := statedb.VersionedValue {Value : []byte ("value3" ), Version : version .NewHeight (1 , 3 )}
629
+ vv4 := statedb.VersionedValue {Value : []byte ("value4" ), Version : version .NewHeight (1 , 4 )}
630
+
631
+ batch .Put ("ns" , "key1" , vv1 .Value , vv1 .Version )
632
+ batch .Put ("ns" , "key2" , vv2 .Value , vv2 .Version )
633
+ batch .Put ("ns" , "key3" , vv3 .Value , vv3 .Version )
634
+ batch .Put ("ns" , "key4" , vv4 .Value , vv4 .Version )
635
+ savePoint := version .NewHeight (1 , 5 )
636
+ err = db .ApplyUpdates (batch , savePoint )
637
+ testutil .AssertNoError (t , err , "" )
638
+
639
+ // Clear the cache for the next batch, in place of simulation
640
+ if bulkdb , ok := db .(statedb.BulkOptimizable ); ok {
641
+ //clear the cached versions, this will force a read when getVerion is called
642
+ bulkdb .ClearCachedVersions ()
643
+ }
644
+
645
+ batch = statedb .NewUpdateBatch ()
646
+ batch .Put ("ns" , "key1" , vv1 .Value , vv1 .Version )
647
+ batch .Put ("ns" , "key2" , vv2 .Value , vv2 .Version )
648
+ batch .Put ("ns" , "key3" , vv3 .Value , vv3 .Version )
649
+ batch .Put ("ns" , "key4" , vv4 .Value , vv4 .Version )
650
+ savePoint = version .NewHeight (1 , 6 )
651
+ err = db .ApplyUpdates (batch , savePoint )
652
+ testutil .AssertNoError (t , err , "" )
653
+
654
+ // Update document key3
655
+ batch = statedb .NewUpdateBatch ()
656
+ batch .Delete ("ns" , "key2" , vv2 .Version )
657
+ batch .Put ("ns" , "key3" , vv3 .Value , vv3 .Version )
658
+ savePoint = version .NewHeight (1 , 7 )
659
+ err = db .ApplyUpdates (batch , savePoint )
660
+ testutil .AssertNoError (t , err , "" )
661
+
662
+ // This should force a retry for couchdb revision conflict for both delete and update
663
+ // Retry logic should correct the update and prevent delete from throwing an error
664
+ batch = statedb .NewUpdateBatch ()
665
+ batch .Delete ("ns" , "key2" , vv2 .Version )
666
+ batch .Put ("ns" , "key3" , vv3 .Value , vv3 .Version )
667
+ savePoint = version .NewHeight (1 , 8 )
668
+ err = db .ApplyUpdates (batch , savePoint )
669
+ testutil .AssertNoError (t , err , "" )
670
+
671
+ //Create a new set of values that use JSONs instead of binary
672
+ jsonValue5 := []byte (`{"asset_name": "marble5","color": "blue","size": 5,"owner": "fred"}` )
673
+ jsonValue6 := []byte (`{"asset_name": "marble6","color": "blue","size": 6,"owner": "elaine"}` )
674
+ jsonValue7 := []byte (`{"asset_name": "marble7","color": "blue","size": 7,"owner": "fred"}` )
675
+ jsonValue8 := []byte (`{"asset_name": "marble8","color": "blue","size": 8,"owner": "elaine"}` )
676
+
677
+ // Clear the cache for the next batch, in place of simulation
678
+ if bulkdb , ok := db .(statedb.BulkOptimizable ); ok {
679
+ //clear the cached versions, this will force a read when getVersion is called
680
+ bulkdb .ClearCachedVersions ()
681
+ }
682
+
683
+ batch = statedb .NewUpdateBatch ()
684
+ batch .Put ("ns1" , "key5" , jsonValue5 , version .NewHeight (1 , 9 ))
685
+ batch .Put ("ns1" , "key6" , jsonValue6 , version .NewHeight (1 , 10 ))
686
+ batch .Put ("ns1" , "key7" , jsonValue7 , version .NewHeight (1 , 11 ))
687
+ batch .Put ("ns1" , "key8" , jsonValue8 , version .NewHeight (1 , 12 ))
688
+ savePoint = version .NewHeight (1 , 6 )
689
+ err = db .ApplyUpdates (batch , savePoint )
690
+ testutil .AssertNoError (t , err , "" )
691
+
692
+ // Clear the cache for the next batch, in place of simulation
693
+ if bulkdb , ok := db .(statedb.BulkOptimizable ); ok {
694
+ //clear the cached versions, this will force a read when getVersion is called
695
+ bulkdb .ClearCachedVersions ()
696
+ }
697
+
698
+ //Send the batch through again to test updates
699
+ batch = statedb .NewUpdateBatch ()
700
+ batch .Put ("ns1" , "key5" , jsonValue5 , version .NewHeight (1 , 9 ))
701
+ batch .Put ("ns1" , "key6" , jsonValue6 , version .NewHeight (1 , 10 ))
702
+ batch .Put ("ns1" , "key7" , jsonValue7 , version .NewHeight (1 , 11 ))
703
+ batch .Put ("ns1" , "key8" , jsonValue8 , version .NewHeight (1 , 12 ))
704
+ savePoint = version .NewHeight (1 , 6 )
705
+ err = db .ApplyUpdates (batch , savePoint )
706
+ testutil .AssertNoError (t , err , "" )
707
+
708
+ // Update document key3
709
+ // this will cause an inconsistent cache entry for connection db2
710
+ batch = statedb .NewUpdateBatch ()
711
+ batch .Delete ("ns1" , "key6" , version .NewHeight (1 , 13 ))
712
+ batch .Put ("ns1" , "key7" , jsonValue7 , version .NewHeight (1 , 14 ))
713
+ savePoint = version .NewHeight (1 , 15 )
714
+ err = db .ApplyUpdates (batch , savePoint )
715
+ testutil .AssertNoError (t , err , "" )
716
+
717
+ // This should force a retry for couchdb revision conflict for both delete and update
718
+ // Retry logic should correct the update and prevent delete from throwing an error
719
+ batch = statedb .NewUpdateBatch ()
720
+ batch .Delete ("ns1" , "key6" , version .NewHeight (1 , 16 ))
721
+ batch .Put ("ns1" , "key7" , jsonValue7 , version .NewHeight (1 , 17 ))
722
+ savePoint = version .NewHeight (1 , 18 )
723
+ err = db .ApplyUpdates (batch , savePoint )
724
+ testutil .AssertNoError (t , err , "" )
617
725
618
726
}
0 commit comments