1818 */
1919package org .elasticsearch .gateway ;
2020
21+ import org .elasticsearch .Version ;
2122import org .elasticsearch .cluster .metadata .IndexMetadata ;
2223import org .elasticsearch .cluster .routing .RoutingNodes ;
2324import org .elasticsearch .cluster .routing .ShardRouting ;
3536import java .util .Locale ;
3637import java .util .Map ;
3738
39+ import static org .hamcrest .Matchers .greaterThan ;
3840import static org .mockito .Mockito .mock ;
3941
4042public class PriorityComparatorTests extends ESTestCase {
@@ -52,15 +54,17 @@ public void testPreferNewIndices() {
5254 }
5355 shards .sort (new PriorityComparator () {
5456 @ Override
55- protected Settings getIndexSettings (Index index ) {
57+ protected IndexMetadata getMetadata (Index index ) {
58+ Settings settings ;
5659 if ("oldest" .equals (index .getName ())) {
57- return Settings .builder ().put (IndexMetadata .SETTING_CREATION_DATE , 10 )
58- .put (IndexMetadata .SETTING_PRIORITY , 1 ).build ();
60+ settings = buildSettings (10 , 1 );
5961 } else if ("newest" .equals (index .getName ())) {
60- return Settings .builder ().put (IndexMetadata .SETTING_CREATION_DATE , 100 )
61- .put (IndexMetadata .SETTING_PRIORITY , 1 ).build ();
62+ settings = buildSettings (100 , 1 );
63+ } else {
64+ settings = Settings .EMPTY ;
6265 }
63- return Settings .EMPTY ;
66+
67+ return IndexMetadata .builder (index .getName ()).settings (settings ).build ();
6468 }
6569 });
6670 RoutingNodes .UnassignedShards .UnassignedIterator iterator = shards .iterator ();
@@ -84,15 +88,53 @@ public void testPreferPriorityIndices() {
8488 }
8589 shards .sort (new PriorityComparator () {
8690 @ Override
87- protected Settings getIndexSettings (Index index ) {
91+ protected IndexMetadata getMetadata (Index index ) {
92+ Settings settings ;
93+ if ("oldest" .equals (index .getName ())) {
94+ settings = buildSettings (10 , 100 );
95+ } else if ("newest" .equals (index .getName ())) {
96+ settings = buildSettings (100 , 1 );
97+ } else {
98+ settings = Settings .EMPTY ;
99+ }
100+
101+ return IndexMetadata .builder (index .getName ()).settings (settings ).build ();
102+ }
103+ });
104+ RoutingNodes .UnassignedShards .UnassignedIterator iterator = shards .iterator ();
105+ ShardRouting next = iterator .next ();
106+ assertEquals ("oldest" , next .getIndexName ());
107+ next = iterator .next ();
108+ assertEquals ("newest" , next .getIndexName ());
109+ assertFalse (iterator .hasNext ());
110+ }
111+
112+ public void testPreferSystemIndices () {
113+ RoutingNodes .UnassignedShards shards = new RoutingNodes .UnassignedShards (mock (RoutingNodes .class ));
114+ List <ShardRouting > shardRoutings = Arrays .asList (
115+ TestShardRouting .newShardRouting ("oldest" , 0 , null , null ,
116+ randomBoolean (), ShardRoutingState .UNASSIGNED , new UnassignedInfo (randomFrom (UnassignedInfo .Reason .values ()), "foobar" )),
117+ TestShardRouting .newShardRouting ("newest" , 0 , null , null ,
118+ randomBoolean (), ShardRoutingState .UNASSIGNED , new UnassignedInfo (randomFrom (UnassignedInfo .Reason .values ()), "foobar" )));
119+ Collections .shuffle (shardRoutings , random ());
120+ for (ShardRouting routing : shardRoutings ) {
121+ shards .add (routing );
122+ }
123+ shards .sort (new PriorityComparator () {
124+ @ Override
125+ protected IndexMetadata getMetadata (Index index ) {
126+ Settings settings ;
127+ boolean isSystem = false ;
88128 if ("oldest" .equals (index .getName ())) {
89- return Settings . builder (). put ( IndexMetadata . SETTING_CREATION_DATE , 10 )
90- . put ( IndexMetadata . SETTING_PRIORITY , 100 ). build () ;
129+ settings = buildSettings ( 10 , 100 );
130+ isSystem = true ;
91131 } else if ("newest" .equals (index .getName ())) {
92- return Settings .builder ().put (IndexMetadata .SETTING_CREATION_DATE , 100 )
93- .put (IndexMetadata .SETTING_PRIORITY , 1 ).build ();
132+ settings = buildSettings (100 , 1 );
133+ } else {
134+ settings = Settings .EMPTY ;
94135 }
95- return Settings .EMPTY ;
136+
137+ return IndexMetadata .builder (index .getName ()).system (isSystem ).settings (settings ).build ();
96138 }
97139 });
98140 RoutingNodes .UnassignedShards .UnassignedIterator iterator = shards .iterator ();
@@ -106,75 +148,99 @@ protected Settings getIndexSettings(Index index) {
106148 public void testPriorityComparatorSort () {
107149 RoutingNodes .UnassignedShards shards = new RoutingNodes .UnassignedShards (mock (RoutingNodes .class ));
108150 int numIndices = randomIntBetween (3 , 99 );
109- IndexMeta [] indices = new IndexMeta [numIndices ];
110- final Map <String , IndexMeta > map = new HashMap <>();
151+ IndexMetadata [] indices = new IndexMetadata [numIndices ];
152+ final Map <String , IndexMetadata > map = new HashMap <>();
111153
112154 for (int i = 0 ; i < indices .length ; i ++) {
155+ int priority = 0 ;
156+ int creationDate = 0 ;
157+ boolean isSystem = false ;
158+
113159 if (frequently ()) {
114- indices [i ] = new IndexMeta ("idx_2015_04_" + String .format (Locale .ROOT , "%02d" , i ), randomIntBetween (1 , 1000 ),
115- randomIntBetween (1 , 10000 ));
116- } else { // sometimes just use defaults
117- indices [i ] = new IndexMeta ("idx_2015_04_" + String .format (Locale .ROOT , "%02d" , i ));
160+ priority = randomIntBetween (1 , 1000 );
161+ creationDate = randomIntBetween (1 , 10000 );
118162 }
119- map .put (indices [i ].name , indices [i ]);
163+ if (rarely ()) {
164+ isSystem = true ;
165+ }
166+ // else sometimes just use the defaults
167+
168+ indices [i ] = IndexMetadata .builder (String .format (Locale .ROOT , "idx_%04d" , i ))
169+ .system (isSystem )
170+ .settings (buildSettings (creationDate , priority ))
171+ .build ();
172+
173+ map .put (indices [i ].getIndex ().getName (), indices [i ]);
120174 }
121175 int numShards = randomIntBetween (10 , 100 );
122176 for (int i = 0 ; i < numShards ; i ++) {
123- IndexMeta indexMeta = randomFrom (indices );
124- shards .add (TestShardRouting .newShardRouting (indexMeta .name , randomIntBetween (1 , 5 ), null , null ,
177+ IndexMetadata indexMeta = randomFrom (indices );
178+ shards .add (TestShardRouting .newShardRouting (indexMeta .getIndex (). getName () , randomIntBetween (1 , 5 ), null , null ,
125179 randomBoolean (), ShardRoutingState .UNASSIGNED , new UnassignedInfo (randomFrom (UnassignedInfo .Reason .values ()),
126180 "foobar" )));
127181 }
128182 shards .sort (new PriorityComparator () {
129183 @ Override
130- protected Settings getIndexSettings (Index index ) {
131- IndexMeta indexMeta = map .get (index .getName ());
132- return indexMeta .settings ;
184+ protected IndexMetadata getMetadata (Index index ) {
185+ return map .get (index .getName ());
133186 }
134187 });
135188 ShardRouting previous = null ;
136189 for (ShardRouting routing : shards ) {
137190 if (previous != null ) {
138- IndexMeta prevMeta = map .get (previous .getIndexName ());
139- IndexMeta currentMeta = map .get (routing .getIndexName ());
140- if (prevMeta .priority == currentMeta .priority ) {
141- if (prevMeta .creationDate == currentMeta .creationDate ) {
142- if (prevMeta .name .equals (currentMeta .name ) == false ) {
143- assertTrue ("indexName mismatch, expected:" + currentMeta .name + " after " + prevMeta .name + " " +
144- prevMeta .name .compareTo (currentMeta .name ), prevMeta .name .compareTo (currentMeta .name ) > 0 );
191+ IndexMetadata prevMeta = map .get (previous .getIndexName ());
192+ IndexMetadata currentMeta = map .get (routing .getIndexName ());
193+
194+ if (prevMeta .isSystem () == currentMeta .isSystem ()) {
195+ final int prevPriority = prevMeta .getSettings ().getAsInt (IndexMetadata .SETTING_PRIORITY , -1 );
196+ final int currentPriority = currentMeta .getSettings ().getAsInt (IndexMetadata .SETTING_PRIORITY , -1 );
197+
198+ if (prevPriority == currentPriority ) {
199+ final int prevCreationDate = prevMeta .getSettings ().getAsInt (IndexMetadata .SETTING_CREATION_DATE , -1 );
200+ final int currentCreationDate = currentMeta .getSettings ().getAsInt (IndexMetadata .SETTING_CREATION_DATE , -1 );
201+
202+ if (prevCreationDate == currentCreationDate ) {
203+ final String prevName = prevMeta .getIndex ().getName ();
204+ final String currentName = currentMeta .getIndex ().getName ();
205+
206+ if (prevName .equals (currentName ) == false ) {
207+ assertThat (
208+ "indexName mismatch, expected:" + currentName + " after " + prevName ,
209+ prevName ,
210+ greaterThan (currentName )
211+ );
212+ }
213+ } else {
214+ assertThat (
215+ "creationDate mismatch, expected:" + currentCreationDate + " after " + prevCreationDate ,
216+ prevCreationDate , greaterThan (currentCreationDate )
217+ );
145218 }
146219 } else {
147- assertTrue ("creationDate mismatch, expected:" + currentMeta .creationDate + " after " + prevMeta .creationDate ,
148- prevMeta .creationDate > currentMeta .creationDate );
220+ assertThat (
221+ "priority mismatch, expected:" + currentPriority + " after " + prevPriority ,
222+ prevPriority , greaterThan (currentPriority )
223+ );
149224 }
150225 } else {
151- assertTrue ("priority mismatch, expected:" + currentMeta .priority + " after " + prevMeta .priority ,
152- prevMeta .priority > currentMeta .priority );
226+ assertThat (
227+ "system mismatch, expected:" + currentMeta .isSystem () + " after " + prevMeta .isSystem (),
228+ prevMeta .isSystem (),
229+ greaterThan (currentMeta .isSystem ())
230+ );
153231 }
154232 }
155233 previous = routing ;
156234 }
157235 }
158236
159- private static class IndexMeta {
160- final String name ;
161- final int priority ;
162- final long creationDate ;
163- final Settings settings ;
164-
165- private IndexMeta (String name ) { // default
166- this .name = name ;
167- this .priority = 1 ;
168- this .creationDate = -1 ;
169- this .settings = Settings .EMPTY ;
170- }
171-
172- private IndexMeta (String name , int priority , long creationDate ) {
173- this .name = name ;
174- this .priority = priority ;
175- this .creationDate = creationDate ;
176- this .settings = Settings .builder ().put (IndexMetadata .SETTING_CREATION_DATE , creationDate )
177- .put (IndexMetadata .SETTING_PRIORITY , priority ).build ();
178- }
237+ private static Settings buildSettings (int creationDate , int priority ) {
238+ return Settings .builder ()
239+ .put (IndexMetadata .SETTING_CREATION_DATE , creationDate )
240+ .put (IndexMetadata .SETTING_PRIORITY , priority )
241+ .put (IndexMetadata .SETTING_VERSION_CREATED , Version .CURRENT )
242+ .put (IndexMetadata .SETTING_NUMBER_OF_SHARDS , 1 )
243+ .put (IndexMetadata .SETTING_NUMBER_OF_REPLICAS , 0 )
244+ .build ();
179245 }
180246}
0 commit comments