48
48
import java .util .concurrent .atomic .AtomicInteger ;
49
49
import java .util .stream .Collectors ;
50
50
51
+ import static java .util .Collections .singletonList ;
51
52
import static org .elasticsearch .cluster .metadata .DataStreamTestHelper .createBackingIndex ;
52
53
import static org .elasticsearch .cluster .metadata .DataStreamTestHelper .createFirstBackingIndex ;
53
54
import static org .elasticsearch .cluster .metadata .DataStreamTestHelper .createTimestampField ;
@@ -1231,9 +1232,7 @@ public void testOverlappingDataStreamNamesWithBackingIndexDatePattern() {
1231
1232
.numberOfReplicas (1 )
1232
1233
.build ();
1233
1234
b .put (ds2Index1 , false );
1234
- b .put (
1235
- new DataStream (dataStreamName2 , createTimestampField ("@timestamp" ), Collections .singletonList (ds2Index1 .getIndex ()), 1 , null )
1236
- );
1235
+ b .put (new DataStream (dataStreamName2 , createTimestampField ("@timestamp" ), singletonList (ds2Index1 .getIndex ()), 1 , null ));
1237
1236
1238
1237
Metadata metadata = b .build ();
1239
1238
assertThat (metadata .dataStreams ().size (), equalTo (2 ));
@@ -1314,6 +1313,74 @@ public void testBuildIndicesLookupForDataStreamAliases() {
1314
1313
assertThat (value .getAliases (), nullValue ());
1315
1314
}
1316
1315
1316
+ public void testDataStreamAliasValidation () {
1317
+ Metadata .Builder b = Metadata .builder ();
1318
+ addDataStream ("my-alias" , b );
1319
+ b .put ("my-alias" , "my-alias" , null , null );
1320
+ Exception e = expectThrows (IllegalStateException .class , b ::build );
1321
+ assertThat (e .getMessage (), containsString ("data stream alias and data stream have the same name (my-alias)" ));
1322
+
1323
+ b = Metadata .builder ();
1324
+ addDataStream ("d1" , b );
1325
+ addDataStream ("my-alias" , b );
1326
+ b .put ("my-alias" , "d1" , null , null );
1327
+ e = expectThrows (IllegalStateException .class , b ::build );
1328
+ assertThat (e .getMessage (), containsString ("data stream alias and data stream have the same name (my-alias)" ));
1329
+
1330
+ b = Metadata .builder ();
1331
+ b .put (
1332
+ IndexMetadata .builder ("index1" )
1333
+ .settings (
1334
+ Settings .builder ()
1335
+ .put (IndexMetadata .SETTING_VERSION_CREATED , Version .CURRENT )
1336
+ .put (IndexMetadata .SETTING_NUMBER_OF_SHARDS , 1 )
1337
+ .put (IndexMetadata .SETTING_NUMBER_OF_REPLICAS , 0 )
1338
+ )
1339
+ .putAlias (new AliasMetadata .Builder ("my-alias" ))
1340
+ );
1341
+
1342
+ addDataStream ("d1" , b );
1343
+ b .put ("my-alias" , "d1" , null , null );
1344
+ e = expectThrows (IllegalStateException .class , b ::build );
1345
+ assertThat (e .getMessage (), containsString ("data stream alias and indices alias have the same name (my-alias)" ));
1346
+ }
1347
+
1348
+ public void testDataStreamAliasValidationRestoreScenario () {
1349
+ Metadata .Builder b = Metadata .builder ();
1350
+ b .dataStreams (
1351
+ org .elasticsearch .core .Map .of ("my-alias" , createDataStream ("my-alias" )),
1352
+ org .elasticsearch .core .Map .of ("my-alias" , new DataStreamAlias ("my-alias" , singletonList ("my-alias" ), null , null ))
1353
+ );
1354
+ Exception e = expectThrows (IllegalStateException .class , b ::build );
1355
+ assertThat (e .getMessage (), containsString ("data stream alias and data stream have the same name (my-alias)" ));
1356
+
1357
+ b = Metadata .builder ();
1358
+ b .dataStreams (
1359
+ org .elasticsearch .core .Map .of ("d1" , createDataStream ("d1" ), "my-alias" , createDataStream ("my-alias" )),
1360
+ org .elasticsearch .core .Map .of ("my-alias" , new DataStreamAlias ("my-alias" , singletonList ("d1" ), null , null ))
1361
+ );
1362
+ e = expectThrows (IllegalStateException .class , b ::build );
1363
+ assertThat (e .getMessage (), containsString ("data stream alias and data stream have the same name (my-alias)" ));
1364
+
1365
+ b = Metadata .builder ();
1366
+ b .put (
1367
+ IndexMetadata .builder ("index1" )
1368
+ .settings (
1369
+ Settings .builder ()
1370
+ .put (IndexMetadata .SETTING_VERSION_CREATED , Version .CURRENT )
1371
+ .put (IndexMetadata .SETTING_NUMBER_OF_SHARDS , 1 )
1372
+ .put (IndexMetadata .SETTING_NUMBER_OF_REPLICAS , 0 )
1373
+ )
1374
+ .putAlias (new AliasMetadata .Builder ("my-alias" ))
1375
+ );
1376
+ b .dataStreams (
1377
+ org .elasticsearch .core .Map .of ("d1" , createDataStream ("d1" )),
1378
+ org .elasticsearch .core .Map .of ("my-alias" , new DataStreamAlias ("my-alias" , singletonList ("d1" ), null , null ))
1379
+ );
1380
+ e = expectThrows (IllegalStateException .class , b ::build );
1381
+ assertThat (e .getMessage (), containsString ("data stream alias and indices alias have the same name (my-alias)" ));
1382
+ }
1383
+
1317
1384
private void addDataStream (String name , Metadata .Builder b ) {
1318
1385
int numBackingIndices = randomIntBetween (1 , 4 );
1319
1386
List <Index > indices = new ArrayList <>(numBackingIndices );
@@ -1325,6 +1392,16 @@ private void addDataStream(String name, Metadata.Builder b) {
1325
1392
b .put (new DataStream (name , createTimestampField ("@timestamp" ), indices ));
1326
1393
}
1327
1394
1395
+ private DataStream createDataStream (String name ) {
1396
+ int numBackingIndices = randomIntBetween (1 , 4 );
1397
+ List <Index > indices = new ArrayList <>(numBackingIndices );
1398
+ for (int j = 1 ; j <= numBackingIndices ; j ++) {
1399
+ IndexMetadata idx = createBackingIndex (name , j ).build ();
1400
+ indices .add (idx .getIndex ());
1401
+ }
1402
+ return new DataStream (name , createTimestampField ("@timestamp" ), indices );
1403
+ }
1404
+
1328
1405
public void testIndicesLookupRecordsDataStreamForBackingIndices () {
1329
1406
final int numIndices = randomIntBetween (2 , 5 );
1330
1407
final int numBackingIndices = randomIntBetween (2 , 5 );
@@ -1772,7 +1849,7 @@ public void testReuseIndicesLookup() {
1772
1849
DataStream dataStream = new DataStream (
1773
1850
dataStreamName ,
1774
1851
new DataStream .TimestampField ("@timestamp" ),
1775
- Collections . singletonList (idx .getIndex ())
1852
+ singletonList (idx .getIndex ())
1776
1853
);
1777
1854
builder .put (dataStream );
1778
1855
Metadata metadata = builder .build ();
0 commit comments