77 */
88package org .elasticsearch .cluster .routing ;
99
10+ import org .apache .lucene .util .BytesRef ;
11+ import org .apache .lucene .util .StringHelper ;
1012import org .elasticsearch .Version ;
1113import org .elasticsearch .action .RoutingMissingException ;
1214import org .elasticsearch .cluster .metadata .IndexMetadata ;
@@ -513,7 +515,7 @@ public void testRoutingPathOneSub() throws IOException {
513515 assertIndexShard (
514516 routing ,
515517 Map .of ("foo" , Map .of ("bar" , "cat" ), "baz" , "dog" ),
516- Math .floorMod (hash (List .of ("foo" , List . of ( " bar" , "cat" ) )), shards )
518+ Math .floorMod (hash (List .of ("foo. bar" , "cat" )), shards )
517519 );
518520 }
519521
@@ -523,10 +525,16 @@ public void testRoutingPathManySubs() throws IOException {
523525 assertIndexShard (
524526 routing ,
525527 Map .of ("foo" , Map .of ("a" , "cat" ), "bar" , Map .of ("thing" , "yay" , "this" , "too" )),
526- Math .floorMod (hash (List .of ("bar" , List . of ( " thing" , "yay" , "this" , "too" ) , "foo" , List . of ( " a" , "cat" ) )), shards )
528+ Math .floorMod (hash (List .of ("bar. thing" , "yay" , "bar. this" , "too" , "foo. a" , "cat" )), shards )
527529 );
528530 }
529531
532+ public void testRoutingPathDotInName () throws IOException {
533+ int shards = between (2 , 1000 );
534+ IndexRouting routing = indexRoutingForPath (shards , "foo.bar" );
535+ assertIndexShard (routing , Map .of ("foo.bar" , "cat" , "baz" , "dog" ), Math .floorMod (hash (List .of ("foo.bar" , "cat" )), shards ));
536+ }
537+
530538 public void testRoutingPathBwc () throws IOException {
531539 Version version = VersionUtils .randomIndexCompatibleVersion (random ());
532540 IndexRouting routing = indexRoutingForPath (version , 8 , "dim.*,other.*,top" );
@@ -538,12 +546,13 @@ public void testRoutingPathBwc() throws IOException {
538546 * versions of Elasticsearch must continue to route based on the
539547 * version on the index.
540548 */
541- assertIndexShard (routing , Map .of ("dim" , Map .of ("a" , "a" )), 0 );
549+ assertIndexShard (routing , Map .of ("dim" , Map .of ("a" , "a" )), 4 );
542550 assertIndexShard (routing , Map .of ("dim" , Map .of ("a" , "b" )), 5 );
543551 assertIndexShard (routing , Map .of ("dim" , Map .of ("c" , "d" )), 4 );
544- assertIndexShard (routing , Map .of ("other" , Map .of ("a" , "a" )), 5 );
545- assertIndexShard (routing , Map .of ("top" , "a" ), 3 );
546- assertIndexShard (routing , Map .of ("dim" , Map .of ("c" , "d" ), "top" , "b" ), 2 );
552+ assertIndexShard (routing , Map .of ("other" , Map .of ("a" , "a" )), 7 );
553+ assertIndexShard (routing , Map .of ("top" , "a" ), 5 );
554+ assertIndexShard (routing , Map .of ("dim" , Map .of ("c" , "d" ), "top" , "b" ), 0 );
555+ assertIndexShard (routing , Map .of ("dim.a" , "a" ), 4 );
547556 }
548557
549558 private IndexRouting indexRoutingForPath (int shards , String path ) {
@@ -560,8 +569,8 @@ private IndexRouting indexRoutingForPath(Version createdVersion, int shards, Str
560569 );
561570 }
562571
563- private void assertIndexShard (IndexRouting routing , Map <String , Object > source , int id ) throws IOException {
564- assertThat (routing .indexShard (randomAlphaOfLength (5 ), null , XContentType .JSON , source (source )), equalTo (id ));
572+ private void assertIndexShard (IndexRouting routing , Map <String , Object > source , int expected ) throws IOException {
573+ assertThat (routing .indexShard (randomAlphaOfLength (5 ), null , XContentType .JSON , source (source )), equalTo (expected ));
565574 }
566575
567576 private BytesReference source (Map <String , Object > doc ) throws IOException {
@@ -581,24 +590,14 @@ private BytesReference source(Map<String, Object> doc) throws IOException {
581590 /**
582591 * Build the hash we expect from the extracter.
583592 */
584- private int hash (List <? > keysAndValues ) {
593+ private int hash (List <String > keysAndValues ) {
585594 assertThat (keysAndValues .size () % 2 , equalTo (0 ));
586595 int hash = 0 ;
587596 for (int i = 0 ; i < keysAndValues .size (); i += 2 ) {
588- int thisHash = Murmur3HashFunction .hash (keysAndValues .get (i ).toString ()) ^ expectedValueHash (keysAndValues .get (i + 1 ));
589- hash = hash * 31 + thisHash ;
597+ int keyHash = StringHelper .murmurhash3_x86_32 (new BytesRef (keysAndValues .get (i )), 0 );
598+ int valueHash = StringHelper .murmurhash3_x86_32 (new BytesRef (keysAndValues .get (i + 1 )), 0 );
599+ hash = hash * 31 + (keyHash ^ valueHash );
590600 }
591601 return hash ;
592602 }
593-
594- private int expectedValueHash (Object value ) {
595- if (value instanceof List ) {
596- return hash ((List <?>) value );
597- }
598- if (value instanceof String ) {
599- return Murmur3HashFunction .hash ((String ) value );
600- }
601- throw new IllegalArgumentException ("Unsupported value: " + value );
602- }
603-
604603}
0 commit comments