19
19
20
20
package org .elasticsearch .cluster .routing ;
21
21
22
- import org .elasticsearch .cluster .node .DiscoveryNode ;
23
22
import org .elasticsearch .cluster .node .DiscoveryNodes ;
24
23
import org .elasticsearch .common .Nullable ;
25
24
import org .elasticsearch .common .Randomness ;
26
25
import org .elasticsearch .common .io .stream .StreamInput ;
27
26
import org .elasticsearch .common .io .stream .StreamOutput ;
28
- import org .elasticsearch .common .util .Maps ;
29
27
import org .elasticsearch .common .util .set .Sets ;
30
28
import org .elasticsearch .index .Index ;
31
29
import org .elasticsearch .index .shard .ShardId ;
44
42
import java .util .Optional ;
45
43
import java .util .Set ;
46
44
47
- import static java .util .Collections .emptyMap ;
48
-
49
45
/**
50
46
* {@link IndexShardRoutingTable} encapsulates all instances of a single shard.
51
47
* Each Elasticsearch index consists of multiple shards, each shard encapsulates
@@ -67,10 +63,6 @@ public class IndexShardRoutingTable implements Iterable<ShardRouting> {
67
63
final Set <String > allAllocationIds ;
68
64
final boolean allShardsStarted ;
69
65
70
- private volatile Map <AttributesKey , AttributesRoutings > activeShardsByAttributes = emptyMap ();
71
- private volatile Map <AttributesKey , AttributesRoutings > initializingShardsByAttributes = emptyMap ();
72
- private final Object shardsByAttributeMutex = new Object ();
73
-
74
66
/**
75
67
* The initializing list, including ones that are initializing on a target node because of relocation.
76
68
* If we can come up with a better variable name, it would be nice...
@@ -549,86 +541,6 @@ public boolean equals(Object obj) {
549
541
}
550
542
}
551
543
552
- static class AttributesRoutings {
553
-
554
- public final List <ShardRouting > withSameAttribute ;
555
- public final List <ShardRouting > withoutSameAttribute ;
556
- public final int totalSize ;
557
-
558
- AttributesRoutings (List <ShardRouting > withSameAttribute , List <ShardRouting > withoutSameAttribute ) {
559
- this .withSameAttribute = withSameAttribute ;
560
- this .withoutSameAttribute = withoutSameAttribute ;
561
- this .totalSize = withoutSameAttribute .size () + withSameAttribute .size ();
562
- }
563
- }
564
-
565
- private AttributesRoutings getActiveAttribute (AttributesKey key , DiscoveryNodes nodes ) {
566
- AttributesRoutings shardRoutings = activeShardsByAttributes .get (key );
567
- if (shardRoutings == null ) {
568
- synchronized (shardsByAttributeMutex ) {
569
- ArrayList <ShardRouting > from = new ArrayList <>(activeShards );
570
- List <ShardRouting > to = collectAttributeShards (key , nodes , from );
571
-
572
- shardRoutings = new AttributesRoutings (to , Collections .unmodifiableList (from ));
573
- activeShardsByAttributes = Maps .copyMapWithAddedEntry (activeShardsByAttributes , key , shardRoutings );
574
- }
575
- }
576
- return shardRoutings ;
577
- }
578
-
579
- private AttributesRoutings getInitializingAttribute (AttributesKey key , DiscoveryNodes nodes ) {
580
- AttributesRoutings shardRoutings = initializingShardsByAttributes .get (key );
581
- if (shardRoutings == null ) {
582
- synchronized (shardsByAttributeMutex ) {
583
- ArrayList <ShardRouting > from = new ArrayList <>(allInitializingShards );
584
- List <ShardRouting > to = collectAttributeShards (key , nodes , from );
585
- shardRoutings = new AttributesRoutings (to , Collections .unmodifiableList (from ));
586
- initializingShardsByAttributes =
587
- Maps .copyMapWithAddedEntry (initializingShardsByAttributes , key , shardRoutings );
588
- }
589
- }
590
- return shardRoutings ;
591
- }
592
-
593
- private static List <ShardRouting > collectAttributeShards (AttributesKey key , DiscoveryNodes nodes , ArrayList <ShardRouting > from ) {
594
- final ArrayList <ShardRouting > to = new ArrayList <>();
595
- for (final String attribute : key .attributes ) {
596
- final String localAttributeValue = nodes .getLocalNode ().getAttributes ().get (attribute );
597
- if (localAttributeValue != null ) {
598
- for (Iterator <ShardRouting > iterator = from .iterator (); iterator .hasNext (); ) {
599
- ShardRouting fromShard = iterator .next ();
600
- final DiscoveryNode discoveryNode = nodes .get (fromShard .currentNodeId ());
601
- if (discoveryNode == null ) {
602
- iterator .remove (); // node is not present anymore - ignore shard
603
- } else if (localAttributeValue .equals (discoveryNode .getAttributes ().get (attribute ))) {
604
- iterator .remove ();
605
- to .add (fromShard );
606
- }
607
- }
608
- }
609
- }
610
- return Collections .unmodifiableList (to );
611
- }
612
-
613
- public ShardIterator preferAttributesActiveInitializingShardsIt (List <String > attributes , DiscoveryNodes nodes ) {
614
- return preferAttributesActiveInitializingShardsIt (attributes , nodes , shuffler .nextSeed ());
615
- }
616
-
617
- public ShardIterator preferAttributesActiveInitializingShardsIt (List <String > attributes , DiscoveryNodes nodes , int seed ) {
618
- AttributesKey key = new AttributesKey (attributes );
619
- AttributesRoutings activeRoutings = getActiveAttribute (key , nodes );
620
- AttributesRoutings initializingRoutings = getInitializingAttribute (key , nodes );
621
-
622
- // we now randomize, once between the ones that have the same attributes, and once for the ones that don't
623
- // we don't want to mix between the two!
624
- ArrayList <ShardRouting > ordered = new ArrayList <>(activeRoutings .totalSize + initializingRoutings .totalSize );
625
- ordered .addAll (shuffler .shuffle (activeRoutings .withSameAttribute , seed ));
626
- ordered .addAll (shuffler .shuffle (activeRoutings .withoutSameAttribute , seed ));
627
- ordered .addAll (shuffler .shuffle (initializingRoutings .withSameAttribute , seed ));
628
- ordered .addAll (shuffler .shuffle (initializingRoutings .withoutSameAttribute , seed ));
629
- return new PlainShardIterator (shardId , ordered );
630
- }
631
-
632
544
public ShardRouting primaryShard () {
633
545
return primary ;
634
546
}
0 commit comments