21
21
import org .apache .lucene .store .IndexOutput ;
22
22
import org .apache .lucene .store .OutputStreamIndexOutput ;
23
23
import org .apache .lucene .tests .util .LuceneTestCase ;
24
+ import org .apache .lucene .util .Version ;
24
25
import org .junit .After ;
25
26
import org .junit .Before ;
26
27
import org .mockito .Mockito ;
@@ -124,14 +125,42 @@ public void testUploadedSegmentMetadataToString() {
124
125
"123456" ,
125
126
1234
126
127
);
127
- assertEquals ("abc::pqr::123456::1234" , metadata .toString ());
128
+ metadata .setWrittenByMajor (Version .LATEST .major );
129
+ assertEquals ("abc::pqr::123456::1234::" + Version .LATEST .major , metadata .toString ());
130
+ }
131
+
132
+ public void testUploadedSegmentMetadataToStringExceptionTooNew () {
133
+ RemoteSegmentStoreDirectory .UploadedSegmentMetadata metadata = new RemoteSegmentStoreDirectory .UploadedSegmentMetadata (
134
+ "abc" ,
135
+ "pqr" ,
136
+ "123456" ,
137
+ 1234
138
+ );
139
+ assertThrows (IllegalArgumentException .class , () -> metadata .setWrittenByMajor (Version .LATEST .major + 1 ));
140
+ }
141
+
142
+ public void testUploadedSegmentMetadataToStringExceptionTooOld () {
143
+ RemoteSegmentStoreDirectory .UploadedSegmentMetadata metadata = new RemoteSegmentStoreDirectory .UploadedSegmentMetadata (
144
+ "abc" ,
145
+ "pqr" ,
146
+ "123456" ,
147
+ 1234
148
+ );
149
+ assertThrows (IllegalArgumentException .class , () -> metadata .setWrittenByMajor (Version .LATEST .major - 2 ));
128
150
}
129
151
130
152
public void testUploadedSegmentMetadataFromString () {
131
153
RemoteSegmentStoreDirectory .UploadedSegmentMetadata metadata = RemoteSegmentStoreDirectory .UploadedSegmentMetadata .fromString (
132
- "_0.cfe::_0.cfe__uuidxyz::4567::372000"
154
+ "_0.cfe::_0.cfe__uuidxyz::4567::372000::" + Version .LATEST .major
155
+ );
156
+ assertEquals ("_0.cfe::_0.cfe__uuidxyz::4567::372000::" + Version .LATEST .major , metadata .toString ());
157
+ }
158
+
159
+ public void testUploadedSegmentMetadataFromStringException () {
160
+ assertThrows (
161
+ ArrayIndexOutOfBoundsException .class ,
162
+ () -> RemoteSegmentStoreDirectory .UploadedSegmentMetadata .fromString ("_0.cfe::_0.cfe__uuidxyz::4567::372000" )
133
163
);
134
- assertEquals ("_0.cfe::_0.cfe__uuidxyz::4567::372000" , metadata .toString ());
135
164
}
136
165
137
166
public void testGetPrimaryTermGenerationUuid () {
@@ -176,6 +205,8 @@ private Map<String, String> getDummyMetadata(String prefix, int commitGeneration
176
205
+ randomIntBetween (1000 , 5000 )
177
206
+ "::"
178
207
+ randomIntBetween (512000 , 1024000 )
208
+ + "::"
209
+ + Version .MIN_SUPPORTED_MAJOR
179
210
);
180
211
metadata .put (
181
212
prefix + ".cfs" ,
@@ -188,6 +219,8 @@ private Map<String, String> getDummyMetadata(String prefix, int commitGeneration
188
219
+ randomIntBetween (1000 , 5000 )
189
220
+ "::"
190
221
+ randomIntBetween (512000 , 1024000 )
222
+ + "::"
223
+ + Version .MIN_SUPPORTED_MAJOR
191
224
);
192
225
metadata .put (
193
226
prefix + ".si" ,
@@ -200,6 +233,8 @@ private Map<String, String> getDummyMetadata(String prefix, int commitGeneration
200
233
+ randomIntBetween (1000 , 5000 )
201
234
+ "::"
202
235
+ randomIntBetween (512000 , 1024000 )
236
+ + "::"
237
+ + Version .LATEST .major
203
238
);
204
239
metadata .put (
205
240
"segments_" + commitGeneration ,
@@ -213,6 +248,8 @@ private Map<String, String> getDummyMetadata(String prefix, int commitGeneration
213
248
+ randomIntBetween (1000 , 5000 )
214
249
+ "::"
215
250
+ randomIntBetween (1024 , 5120 )
251
+ + "::"
252
+ + Version .LATEST .major
216
253
);
217
254
return metadata ;
218
255
}
@@ -611,8 +648,8 @@ public void testContainsFile() throws IOException {
611
648
).thenReturn (metadataFiles );
612
649
613
650
Map <String , String > metadata = new HashMap <>();
614
- metadata .put ("_0.cfe" , "_0.cfe::_0.cfe__" + UUIDs .base64UUID () + "::1234::512" );
615
- metadata .put ("_0.cfs" , "_0.cfs::_0.cfs__" + UUIDs .base64UUID () + "::2345::1024" );
651
+ metadata .put ("_0.cfe" , "_0.cfe::_0.cfe__" + UUIDs .base64UUID () + "::1234::512::" + Version . LATEST . major );
652
+ metadata .put ("_0.cfs" , "_0.cfs::_0.cfs__" + UUIDs .base64UUID () + "::2345::1024::" + Version . LATEST . major );
616
653
617
654
when (remoteMetadataDirectory .openInput (metadataFilename , IOContext .DEFAULT )).thenReturn (createMetadataFileBytes (metadata , 1 , 5 ));
618
655
@@ -641,37 +678,53 @@ public void testUploadMetadataEmpty() throws IOException {
641
678
IndexOutput indexOutput = mock (IndexOutput .class );
642
679
when (storeDirectory .createOutput (startsWith ("metadata__12__o" ), eq (IOContext .DEFAULT ))).thenReturn (indexOutput );
643
680
644
- Collection <String > segmentFiles = List .of ("s1 " , "s2 " , "s3 " );
681
+ Collection <String > segmentFiles = List .of ("_s1.si " , "_s1.cfe " , "_s3.cfs " );
645
682
assertThrows (
646
683
NoSuchFileException .class ,
647
684
() -> remoteSegmentStoreDirectory .uploadMetadata (segmentFiles , segmentInfos , storeDirectory , 12L , 34L )
648
685
);
649
686
}
650
687
651
688
public void testUploadMetadataNonEmpty () throws IOException {
652
- populateMetadata ();
689
+ indexDocs (142364 , 5 );
690
+ flushShard (indexShard , true );
691
+ SegmentInfos segInfos = indexShard .store ().readLastCommittedSegmentsInfo ();
692
+ long primaryTerm = 12 ;
693
+ String primaryTermLong = RemoteStoreUtils .invertLong (primaryTerm );
694
+ long generation = segInfos .getGeneration ();
695
+ String generationLong = RemoteStoreUtils .invertLong (generation );
696
+ String latestMetadataFileName = "metadata__" + primaryTermLong + "__" + generationLong + "__abc" ;
697
+ List <String > metadataFiles = List .of (latestMetadataFileName );
698
+ when (
699
+ remoteMetadataDirectory .listFilesByPrefixInLexicographicOrder (
700
+ RemoteSegmentStoreDirectory .MetadataFilenameUtils .METADATA_PREFIX ,
701
+ 1
702
+ )
703
+ ).thenReturn (metadataFiles );
704
+ Map <String , Map <String , String >> metadataFilenameContentMapping = Map .of (
705
+ latestMetadataFileName ,
706
+ getDummyMetadata ("_0" , (int ) generation )
707
+ );
708
+ when (remoteMetadataDirectory .openInput (latestMetadataFileName , IOContext .DEFAULT )).thenReturn (
709
+ createMetadataFileBytes (metadataFilenameContentMapping .get (latestMetadataFileName ), generation , primaryTerm )
710
+ );
711
+
653
712
remoteSegmentStoreDirectory .init ();
654
713
655
714
Directory storeDirectory = mock (Directory .class );
656
715
BytesStreamOutput output = new BytesStreamOutput ();
657
716
IndexOutput indexOutput = new OutputStreamIndexOutput ("segment metadata" , "metadata output stream" , output , 4096 );
717
+ when (storeDirectory .createOutput (startsWith ("metadata__" + primaryTermLong + "__" + generationLong ), eq (IOContext .DEFAULT )))
718
+ .thenReturn (indexOutput );
658
719
659
- String generation = RemoteStoreUtils .invertLong (segmentInfos .getGeneration ());
660
- String primaryTerm = RemoteStoreUtils .invertLong (12 );
661
- when (storeDirectory .createOutput (startsWith ("metadata__" + primaryTerm + "__" + generation ), eq (IOContext .DEFAULT ))).thenReturn (
662
- indexOutput
663
- );
664
-
665
- Collection <String > segmentFiles = List .of ("_0.si" , "_0.cfe" , "_0.cfs" , "segments_1" );
666
- remoteSegmentStoreDirectory .uploadMetadata (segmentFiles , segmentInfos , storeDirectory , 12L , 34L );
720
+ remoteSegmentStoreDirectory .uploadMetadata (segInfos .files (true ), segInfos , storeDirectory , primaryTerm , generation );
667
721
668
722
verify (remoteMetadataDirectory ).copyFrom (
669
723
eq (storeDirectory ),
670
- startsWith ("metadata__" + primaryTerm + "__" + generation ),
671
- startsWith ("metadata__" + primaryTerm + "__" + generation ),
724
+ startsWith ("metadata__" + primaryTermLong + "__" + generationLong ),
725
+ startsWith ("metadata__" + primaryTermLong + "__" + generationLong ),
672
726
eq (IOContext .DEFAULT )
673
727
);
674
-
675
728
VersionedCodecStreamWrapper <RemoteSegmentMetadata > streamWrapper = new VersionedCodecStreamWrapper <>(
676
729
new RemoteSegmentMetadataHandler (),
677
730
RemoteSegmentMetadata .CURRENT_VERSION ,
@@ -680,16 +733,25 @@ public void testUploadMetadataNonEmpty() throws IOException {
680
733
RemoteSegmentMetadata remoteSegmentMetadata = streamWrapper .readStream (
681
734
new ByteArrayIndexInput ("expected" , BytesReference .toBytes (output .bytes ()))
682
735
);
683
-
684
736
Map <String , RemoteSegmentStoreDirectory .UploadedSegmentMetadata > actual = remoteSegmentStoreDirectory
685
737
.getSegmentsUploadedToRemoteStore ();
686
738
Map <String , RemoteSegmentStoreDirectory .UploadedSegmentMetadata > expected = remoteSegmentMetadata .getMetadata ();
687
-
688
739
for (String filename : expected .keySet ()) {
689
740
assertEquals (expected .get (filename ).toString (), actual .get (filename ).toString ());
690
741
}
691
742
}
692
743
744
+ public void testUploadMetadataNoSegmentCommitInfos () throws IOException {
745
+ SegmentInfos segInfos = indexShard .store ().readLastCommittedSegmentsInfo ();
746
+ int numSegCommitInfos = segInfos .size ();
747
+ assertEquals (
748
+ "For a fresh index, the number of SegmentCommitInfo instances associated with the SegmentInfos instance should be 0, but were found to be "
749
+ + numSegCommitInfos ,
750
+ 0 ,
751
+ numSegCommitInfos
752
+ );
753
+ }
754
+
693
755
public void testNoMetadataHeaderCorruptIndexException () throws IOException {
694
756
List <String > metadataFiles = List .of (metadataFilename );
695
757
when (
@@ -700,8 +762,8 @@ public void testNoMetadataHeaderCorruptIndexException() throws IOException {
700
762
).thenReturn (metadataFiles );
701
763
702
764
Map <String , String > metadata = new HashMap <>();
703
- metadata .put ("_0.cfe" , "_0.cfe::_0.cfe__" + UUIDs .base64UUID () + "::1234" );
704
- metadata .put ("_0.cfs" , "_0.cfs::_0.cfs__" + UUIDs .base64UUID () + "::2345" );
765
+ metadata .put ("_0.cfe" , "_0.cfe::_0.cfe__" + UUIDs .base64UUID () + "::1234::" + Version . LATEST . major );
766
+ metadata .put ("_0.cfs" , "_0.cfs::_0.cfs__" + UUIDs .base64UUID () + "::2345::" + Version . LATEST . major );
705
767
706
768
BytesStreamOutput output = new BytesStreamOutput ();
707
769
OutputStreamIndexOutput indexOutput = new OutputStreamIndexOutput ("segment metadata" , "metadata output stream" , output , 4096 );
@@ -723,8 +785,8 @@ public void testInvalidCodecHeaderCorruptIndexException() throws IOException {
723
785
).thenReturn (metadataFiles );
724
786
725
787
Map <String , String > metadata = new HashMap <>();
726
- metadata .put ("_0.cfe" , "_0.cfe::_0.cfe__" + UUIDs .base64UUID () + "::1234" );
727
- metadata .put ("_0.cfs" , "_0.cfs::_0.cfs__" + UUIDs .base64UUID () + "::2345" );
788
+ metadata .put ("_0.cfe" , "_0.cfe::_0.cfe__" + UUIDs .base64UUID () + "::1234::" + Version . LATEST . major );
789
+ metadata .put ("_0.cfs" , "_0.cfs::_0.cfs__" + UUIDs .base64UUID () + "::2345::" + Version . LATEST . major );
728
790
729
791
BytesStreamOutput output = new BytesStreamOutput ();
730
792
OutputStreamIndexOutput indexOutput = new OutputStreamIndexOutput ("segment metadata" , "metadata output stream" , output , 4096 );
@@ -748,8 +810,8 @@ public void testHeaderMinVersionCorruptIndexException() throws IOException {
748
810
).thenReturn (metadataFiles );
749
811
750
812
Map <String , String > metadata = new HashMap <>();
751
- metadata .put ("_0.cfe" , "_0.cfe::_0.cfe__" + UUIDs .base64UUID () + "::1234" );
752
- metadata .put ("_0.cfs" , "_0.cfs::_0.cfs__" + UUIDs .base64UUID () + "::2345" );
813
+ metadata .put ("_0.cfe" , "_0.cfe::_0.cfe__" + UUIDs .base64UUID () + "::1234::" + Version . LATEST . major );
814
+ metadata .put ("_0.cfs" , "_0.cfs::_0.cfs__" + UUIDs .base64UUID () + "::2345::" + Version . LATEST . major );
753
815
754
816
BytesStreamOutput output = new BytesStreamOutput ();
755
817
OutputStreamIndexOutput indexOutput = new OutputStreamIndexOutput ("segment metadata" , "metadata output stream" , output , 4096 );
@@ -773,8 +835,8 @@ public void testHeaderMaxVersionCorruptIndexException() throws IOException {
773
835
).thenReturn (metadataFiles );
774
836
775
837
Map <String , String > metadata = new HashMap <>();
776
- metadata .put ("_0.cfe" , "_0.cfe::_0.cfe__" + UUIDs .base64UUID () + "::1234" );
777
- metadata .put ("_0.cfs" , "_0.cfs::_0.cfs__" + UUIDs .base64UUID () + "::2345" );
838
+ metadata .put ("_0.cfe" , "_0.cfe::_0.cfe__" + UUIDs .base64UUID () + "::1234::" + Version . LATEST . major );
839
+ metadata .put ("_0.cfs" , "_0.cfs::_0.cfs__" + UUIDs .base64UUID () + "::2345::" + Version . LATEST . major );
778
840
779
841
BytesStreamOutput output = new BytesStreamOutput ();
780
842
OutputStreamIndexOutput indexOutput = new OutputStreamIndexOutput ("segment metadata" , "metadata output stream" , output , 4096 );
@@ -798,8 +860,8 @@ public void testIncorrectChecksumCorruptIndexException() throws IOException {
798
860
).thenReturn (metadataFiles );
799
861
800
862
Map <String , String > metadata = new HashMap <>();
801
- metadata .put ("_0.cfe" , "_0.cfe::_0.cfe__" + UUIDs .base64UUID () + "::1234::512" );
802
- metadata .put ("_0.cfs" , "_0.cfs::_0.cfs__" + UUIDs .base64UUID () + "::2345::1024" );
863
+ metadata .put ("_0.cfe" , "_0.cfe::_0.cfe__" + UUIDs .base64UUID () + "::1234::512::" + Version . LATEST . major );
864
+ metadata .put ("_0.cfs" , "_0.cfs::_0.cfs__" + UUIDs .base64UUID () + "::2345::1024::" + Version . LATEST . major );
803
865
804
866
BytesStreamOutput output = new BytesStreamOutput ();
805
867
IndexOutput indexOutput = new OutputStreamIndexOutput ("segment metadata" , "metadata output stream" , output , 4096 );
@@ -947,6 +1009,12 @@ public void testSegmentMetadataCurrentVersion() {
947
1009
assertEquals (RemoteSegmentMetadata .CURRENT_VERSION , 1 );
948
1010
}
949
1011
1012
+ private void indexDocs (int startDocId , int numberOfDocs ) throws IOException {
1013
+ for (int i = startDocId ; i < startDocId + numberOfDocs ; i ++) {
1014
+ indexDoc (indexShard , "_doc" , Integer .toString (i ));
1015
+ }
1016
+ }
1017
+
950
1018
public void testMetadataFileNameOrder () {
951
1019
String file1 = RemoteSegmentStoreDirectory .MetadataFilenameUtils .getMetadataFilename (15 , 21 , 23 , 1 , 1 );
952
1020
String file2 = RemoteSegmentStoreDirectory .MetadataFilenameUtils .getMetadataFilename (15 , 38 , 38 , 1 , 1 );
0 commit comments