2424import org .elasticsearch .client .Client ;
2525import org .elasticsearch .common .UUIDs ;
2626import org .elasticsearch .common .bytes .BytesReference ;
27- import org .elasticsearch .common .collect .Tuple ;
2827import org .elasticsearch .common .io .stream .BytesStreamOutput ;
2928import org .elasticsearch .common .io .stream .OutputStreamStreamOutput ;
3029import org .elasticsearch .common .io .stream .StreamOutput ;
4645import java .util .stream .Collectors ;
4746
4847import static org .elasticsearch .repositories .blobstore .BlobStoreRepository .blobId ;
49- import static org .elasticsearch .repositories .blobstore .BlobStoreRepository .parseNameUUIDFromBlobName ;
5048import static org .hamcrest .Matchers .equalTo ;
5149
5250/**
@@ -108,53 +106,57 @@ public void testRetrieveSnapshots() throws Exception {
108106 assertThat (snapshotIds , equalTo (originalSnapshots ));
109107 }
110108
111- public void testSnapshotIndexFile () throws Exception {
112- final Client client = client ();
113- final Path location = ESIntegTestCase .randomRepoPath (node ().settings ());
114- final String repositoryName = "test-repo" ;
115-
116- PutRepositoryResponse putRepositoryResponse =
117- client .admin ().cluster ().preparePutRepository (repositoryName )
118- .setType ("fs" )
119- .setSettings (Settings .builder ().put (node ().settings ()).put ("location" , location ))
120- .get ();
121- assertThat (putRepositoryResponse .isAcknowledged (), equalTo (true ));
122-
123- final RepositoriesService repositoriesService = getInstanceFromNode (RepositoriesService .class );
124- @ SuppressWarnings ("unchecked" ) final BlobStoreRepository repository =
125- (BlobStoreRepository ) repositoriesService .repository (repositoryName );
109+ public void testReadAndWriteSnapshotsThroughIndexFile () throws Exception {
110+ final BlobStoreRepository repository = setupRepo ();
126111
127112 // write to and read from a snapshot file with no entries
128- repository .writeSnapshotList ( Collections . emptyList ( ));
129- List < SnapshotId > readSnapshotIds = repository .readSnapshotList ( );
130- assertThat (readSnapshotIds .size (), equalTo (0 ));
113+ assertThat ( repository .snapshots (). size (), equalTo ( 0 ));
114+ repository .writeSnapshotsToIndexGen ( Collections . emptyList () );
115+ assertThat (repository . snapshots () .size (), equalTo (0 ));
131116
132117 // write to and read from a snapshot file with a random number of entries
133118 final int numSnapshots = randomIntBetween (1 , 1000 );
134119 final List <SnapshotId > snapshotIds = new ArrayList <>(numSnapshots );
135120 for (int i = 0 ; i < numSnapshots ; i ++) {
136121 snapshotIds .add (new SnapshotId (randomAsciiOfLength (8 ), UUIDs .randomBase64UUID ()));
137122 }
138- repository .writeSnapshotList (snapshotIds );
139- readSnapshotIds = repository .readSnapshotList ();
140- assertThat (readSnapshotIds , equalTo (snapshotIds ));
123+ repository .writeSnapshotsToIndexGen (snapshotIds );
124+ assertThat (repository .snapshots (), equalTo (snapshotIds ));
141125 }
142126
143- public void testOldIndexFileFormat () throws Exception {
144- final Client client = client ();
145- final Path location = ESIntegTestCase .randomRepoPath (node ().settings ());
146- final String repositoryName = "test-repo" ;
127+ public void testIndexGenerationalFiles () throws Exception {
128+ final BlobStoreRepository repository = setupRepo ();
147129
148- PutRepositoryResponse putRepositoryResponse =
149- client .admin ().cluster ().preparePutRepository (repositoryName )
150- .setType ("fs" )
151- .setSettings (Settings .builder ().put (node ().settings ()).put ("location" , location ))
152- .get ();
153- assertThat (putRepositoryResponse .isAcknowledged (), equalTo (true ));
130+ // write to index generational file
131+ final int numSnapshots = randomIntBetween (1 , 1000 );
132+ final List <SnapshotId > snapshotIds = new ArrayList <>(numSnapshots );
133+ for (int i = 0 ; i < numSnapshots ; i ++) {
134+ snapshotIds .add (new SnapshotId (randomAsciiOfLength (8 ), UUIDs .randomBase64UUID ()));
135+ }
136+ repository .writeSnapshotsToIndexGen (snapshotIds );
137+ assertThat (Sets .newHashSet (repository .readSnapshotsFromIndex ()), equalTo (Sets .newHashSet (snapshotIds )));
138+ assertThat (repository .latestIndexBlobId (), equalTo (0L ));
139+ assertThat (repository .readSnapshotIndexLatestBlob (), equalTo (0L ));
154140
155- final RepositoriesService repositoriesService = getInstanceFromNode (RepositoriesService .class );
156- @ SuppressWarnings ("unchecked" ) final BlobStoreRepository repository =
157- (BlobStoreRepository ) repositoriesService .repository (repositoryName );
141+ // adding more and writing to a new index generational file
142+ for (int i = 0 ; i < 10 ; i ++) {
143+ snapshotIds .add (new SnapshotId (randomAsciiOfLength (8 ), UUIDs .randomBase64UUID ()));
144+ }
145+ repository .writeSnapshotsToIndexGen (snapshotIds );
146+ assertThat (Sets .newHashSet (repository .readSnapshotsFromIndex ()), equalTo (Sets .newHashSet (snapshotIds )));
147+ assertThat (repository .latestIndexBlobId (), equalTo (1L ));
148+ assertThat (repository .readSnapshotIndexLatestBlob (), equalTo (1L ));
149+
150+ // removing a snapshot adn writing to a new index generational file
151+ snapshotIds .remove (0 );
152+ repository .writeSnapshotsToIndexGen (snapshotIds );
153+ assertThat (Sets .newHashSet (repository .readSnapshotsFromIndex ()), equalTo (Sets .newHashSet (snapshotIds )));
154+ assertThat (repository .latestIndexBlobId (), equalTo (2L ));
155+ assertThat (repository .readSnapshotIndexLatestBlob (), equalTo (2L ));
156+ }
157+
158+ public void testOldIndexFileFormat () throws Exception {
159+ final BlobStoreRepository repository = setupRepo ();
158160
159161 // write old index file format
160162 final int numOldSnapshots = randomIntBetween (1 , 50 );
@@ -163,36 +165,15 @@ public void testOldIndexFileFormat() throws Exception {
163165 snapshotIds .add (new SnapshotId (randomAsciiOfLength (8 ), SnapshotId .UNASSIGNED_UUID ));
164166 }
165167 writeOldFormat (repository , snapshotIds .stream ().map (SnapshotId ::getName ).collect (Collectors .toList ()));
166- List <SnapshotId > readSnapshotIds = repository .readSnapshotList ();
167- assertThat (Sets .newHashSet (readSnapshotIds ), equalTo (Sets .newHashSet (snapshotIds )));
168+ assertThat (Sets .newHashSet (repository .snapshots ()), equalTo (Sets .newHashSet (snapshotIds )));
168169
169170 // write to and read from a snapshot file with a random number of new entries added
170171 final int numSnapshots = randomIntBetween (1 , 1000 );
171172 for (int i = 0 ; i < numSnapshots ; i ++) {
172173 snapshotIds .add (new SnapshotId (randomAsciiOfLength (8 ), UUIDs .randomBase64UUID ()));
173174 }
174- repository .writeSnapshotList (snapshotIds );
175- readSnapshotIds = repository .readSnapshotList ();
176- assertThat (Sets .newHashSet (readSnapshotIds ), equalTo (Sets .newHashSet (snapshotIds )));
177- }
178-
179- public void testParseUUIDFromBlobName () {
180- String blobStr = "abc123" ;
181- Tuple <String , String > pair = parseNameUUIDFromBlobName (blobStr );
182- assertThat (pair .v1 (), equalTo (blobStr )); // snapshot name
183- assertThat (pair .v2 (), equalTo (SnapshotId .UNASSIGNED_UUID )); // snapshot uuid
184- blobStr = "abcefghijklmnopqrstuvwxyz" ;
185- pair = parseNameUUIDFromBlobName (blobStr );
186- assertThat (pair .v1 (), equalTo (blobStr ));
187- assertThat (pair .v2 (), equalTo (SnapshotId .UNASSIGNED_UUID ));
188- blobStr = "abc123-xyz" ; // not enough characters after '-' to have a uuid
189- pair = parseNameUUIDFromBlobName (blobStr );
190- assertThat (pair .v1 (), equalTo (blobStr ));
191- assertThat (pair .v2 (), equalTo (SnapshotId .UNASSIGNED_UUID ));
192- blobStr = "abc123-a1b2c3d4e5f6g7h8i9j0k1" ;
193- pair = parseNameUUIDFromBlobName (blobStr );
194- assertThat (pair .v1 (), equalTo ("abc123" ));
195- assertThat (pair .v2 (), equalTo ("a1b2c3d4e5f6g7h8i9j0k1" ));
175+ repository .writeSnapshotsToIndexGen (snapshotIds );
176+ assertThat (Sets .newHashSet (repository .snapshots ()), equalTo (Sets .newHashSet (snapshotIds )));
196177 }
197178
198179 public void testBlobId () {
@@ -208,6 +189,24 @@ public void testBlobId() {
208189 assertThat (blobId (snapshotId ), equalTo ("abc-123-" + uuid )); // snapshot name + '-' + uuid
209190 }
210191
192+ private BlobStoreRepository setupRepo () {
193+ final Client client = client ();
194+ final Path location = ESIntegTestCase .randomRepoPath (node ().settings ());
195+ final String repositoryName = "test-repo" ;
196+
197+ PutRepositoryResponse putRepositoryResponse =
198+ client .admin ().cluster ().preparePutRepository (repositoryName )
199+ .setType ("fs" )
200+ .setSettings (Settings .builder ().put (node ().settings ()).put ("location" , location ))
201+ .get ();
202+ assertThat (putRepositoryResponse .isAcknowledged (), equalTo (true ));
203+
204+ final RepositoriesService repositoriesService = getInstanceFromNode (RepositoriesService .class );
205+ @ SuppressWarnings ("unchecked" ) final BlobStoreRepository repository =
206+ (BlobStoreRepository ) repositoriesService .repository (repositoryName );
207+ return repository ;
208+ }
209+
211210 private void writeOldFormat (final BlobStoreRepository repository , final List <String > snapshotNames ) throws Exception {
212211 final BytesReference bRef ;
213212 try (BytesStreamOutput bStream = new BytesStreamOutput ()) {
0 commit comments