<commit_before>/*
 * Licensed to Elastic Search and <NAME> under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. Elastic Search licenses this 
 * file to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.elasticsearch.cluster.routing;

import org.elasticsearch.index.shard.ShardId;
import org.elasticsearch.util.collect.ImmutableList;
import org.elasticsearch.util.collect.UnmodifiableIterator;
import org.elasticsearch.util.concurrent.jsr166y.ThreadLocalRandom;
import org.elasticsearch.util.io.stream.StreamInput;
import org.elasticsearch.util.io.stream.StreamOutput;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.concurrent.atomic.AtomicInteger;

import static org.elasticsearch.util.collect.Lists.*;

/**
 * @author kimchy (<NAME>)
 */
public class IndexShardRoutingTable implements Iterable<ShardRouting> {

    private final ShardId shardId;

    private final ImmutableList<ShardRouting> shards;

    private final AtomicInteger counter;

    IndexShardRoutingTable(ShardId shardId, ImmutableList<ShardRouting> shards) {
        this.shardId = shardId;
        this.shards = shards;
        this.counter = new AtomicInteger(ThreadLocalRandom.current().nextInt(shards.size()));
    }

    public ShardId shardId() {
        return shardId;
    }

    public ShardId getShardId() {
        return shardId();
    }

    @Override public UnmodifiableIterator<ShardRouting> iterator() {
        return shards.iterator();
    }

    public int size() {
        return shards.size();
    }

    public int getSize() {
        return size();
    }

    public ImmutableList<ShardRouting> shards() {
        return shards;
    }

    public ImmutableList<ShardRouting> getShards() {
        return shards();
    }

    public ShardsIterator shardsIt() {
        return new IndexShardsIterator(0);
    }

    public ShardsIterator shardsRandomIt() {
        return new IndexShardsIterator(nextCounter());
    }

    public ShardRouting primaryShard() {
        for (ShardRouting shardRouting : this) {
            if (shardRouting.primary()) {
                return shardRouting;
            }
        }
        return null;
    }

    public List<ShardRouting> backupsShards() {
        List<ShardRouting> backupShards = newArrayListWithExpectedSize(2);
        for (ShardRouting shardRouting : this) {
            if (!shardRouting.primary()) {
                backupShards.add(shardRouting);
            }
        }
        return backupShards;
    }

    int nextCounter() {
        return counter.getAndIncrement();
    }

    ShardRouting shardModulo(int shardId) {
        return shards.get((Math.abs(shardId) % size()));
    }

    /**
     * <p>The class can be used from different threads, though not designed to be used concurrently
     * from different threads.
     */
    class IndexShardsIterator implements ShardsIterator, Iterator<ShardRouting> {

        private final int origIndex;

        private volatile int index;

        private volatile int counter = 0;

        private IndexShardsIterator(int index) {
            this.origIndex = index;
            this.index = index;
        }

        @Override public Iterator<ShardRouting> iterator() {
            return this;
        }

        @Override public ShardsIterator reset() {
            counter = 0;
            index = origIndex;
            return this;
        }

        @Override public boolean hasNext() {
            return counter < size();
        }

        @Override public ShardRouting next() throws NoSuchElementException {
            if (!hasNext()) {
                throw new NoSuchElementException("No shard found");
            }
            counter++;
            return shardModulo(index++);
        }

        @Override public boolean hasNextActive() {
            int counter = this.counter;
            int index = this.index;
            while (counter++ < size()) {
                ShardRouting shardRouting = shardModulo(index++);
                if (shardRouting.active()) {
                    return true;
                }
            }
            return false;
        }

        @Override public ShardRouting nextActive() throws NoSuchElementException {
            ShardRouting shardRouting = nextActiveOrNull();
            if (shardRouting == null) {
                throw new NoSuchElementException("No active shard found");
            }
            return shardRouting;
        }

        @Override public ShardRouting nextActiveOrNull() throws NoSuchElementException {
            while (counter++ < size()) {
                ShardRouting shardRouting = shardModulo(index++);
                if (shardRouting.active()) {
                    return shardRouting;
                }
            }
            return null;
        }

        @Override public void remove() {
            throw new UnsupportedOperationException();
        }

        @Override public int size() {
            return IndexShardRoutingTable.this.size();
        }

        @Override public int sizeActive() {
            int shardsActive = 0;
            for (ShardRouting shardRouting : IndexShardRoutingTable.this.shards()) {
                if (shardRouting.active()) {
                    shardsActive++;<commit_msg>better voaltile management<commit_after>/*
 * Licensed to Elastic Search and <NAME> under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. Elastic Search licenses this 
 * file to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.elasticsearch.cluster.routing;

import org.elasticsearch.index.shard.ShardId;
import org.elasticsearch.util.collect.ImmutableList;
import org.elasticsearch.util.collect.UnmodifiableIterator;
import org.elasticsearch.util.concurrent.jsr166y.ThreadLocalRandom;
import org.elasticsearch.util.io.stream.StreamInput;
import org.elasticsearch.util.io.stream.StreamOutput;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.concurrent.atomic.AtomicInteger;

import static org.elasticsearch.util.collect.Lists.*;

/**
 * @author kimchy (<NAME>)
 */
public class IndexShardRoutingTable implements Iterable<ShardRouting> {

    private final ShardId shardId;

    private final ImmutableList<ShardRouting> shards;

    private final AtomicInteger counter;

    IndexShardRoutingTable(ShardId shardId, ImmutableList<ShardRouting> shards) {
        this.shardId = shardId;
        this.shards = shards;
        this.counter = new AtomicInteger(ThreadLocalRandom.current().nextInt(shards.size()));
    }

    public ShardId shardId() {
        return shardId;
    }

    public ShardId getShardId() {
        return shardId();
    }

    @Override public UnmodifiableIterator<ShardRouting> iterator() {
        return shards.iterator();
    }

    public int size() {
        return shards.size();
    }

    public int getSize() {
        return size();
    }

    public ImmutableList<ShardRouting> shards() {
        return shards;
    }

    public ImmutableList<ShardRouting> getShards() {
        return shards();
    }

    public ShardsIterator shardsIt() {
        return new IndexShardsIterator(0);
    }

    public ShardsIterator shardsRandomIt() {
        return new IndexShardsIterator(nextCounter());
    }

    public ShardRouting primaryShard() {
        for (ShardRouting shardRouting : this) {
            if (shardRouting.primary()) {
                return shardRouting;
            }
        }
        return null;
    }

    public List<ShardRouting> backupsShards() {
        List<ShardRouting> backupShards = newArrayListWithExpectedSize(2);
        for (ShardRouting shardRouting : this) {
            if (!shardRouting.primary()) {
                backupShards.add(shardRouting);
            }
        }
        return backupShards;
    }

    int nextCounter() {
        return counter.getAndIncrement();
    }

    ShardRouting shardModulo(int shardId) {
        return shards.get((Math.abs(shardId) % size()));
    }

    /**
     * <p>The class can be used from different threads, though not designed to be used concurrently
     * from different threads.
     */
    class IndexShardsIterator implements ShardsIterator, Iterator<ShardRouting> {

        private final int origIndex;

        private volatile int index;

        private volatile int counter = 0;

        private IndexShardsIterator(int index) {
            this.origIndex = index;
            this.index = index;
        }

        @Override public Iterator<ShardRouting> iterator() {
            return this;
        }

        @Override public ShardsIterator reset() {
            counter = 0;
            index = origIndex;
            return this;
        }

        @Override public boolean hasNext() {
            return counter < size();
        }

        @Override public ShardRouting next() throws NoSuchElementException {
            if (!hasNext()) {
                throw new NoSuchElementException("No shard found");
            }
            counter++;
            return shardModulo(index++);
        }

        @Override public boolean hasNextActive() {
            int counter = this.counter;
            int index = this.index;
            while (counter++ < size()) {
                ShardRouting shardRouting = shardModulo(index++);
                if (shardRouting.active()) {
                    return true;
                }
            }
            return false;
        }

        @Override public ShardRouting nextActive() throws NoSuchElementException {
            ShardRouting shardRouting = nextActiveOrNull();
            if (shardRouting == null) {
                throw new NoSuchElementException("No active shard found");
            }
            return shardRouting;
        }

        @Override public ShardRouting nextActiveOrNull() throws NoSuchElementException {
            int counter = this.counter;
            int index = this.index;
            while (counter++ < size()) {
                ShardRouting shardRouting = shardModulo(index++);
                if (shardRouting.active()) {
                    this.counter = counter;
                    this.index = index;
                    return shardRouting;
                }
            }
            this.counter = counter;
            this.index = index;
            return null;
        }

        @Override public void remove() {
            throw new UnsupportedOperationException();
        }

        @Override public int size() {
            return IndexShardRoutingTable.this.size();
        }

        @Override public int sizeActive() {
            int shardsActive = 0;
            for (ShardRouting shardRouting : IndexShardRoutingTable.this.shards()) {
                if (shardRouting.active()) {
                    shardsActive++;
{'fraction_non_alphanumeric': 0.08515554088621173, 'fraction_numerical': 0.0019803614159584126, 'mean_word_length': 2.2345876701361087, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5456919', 'n_tokens_mistral': 3170, 'n_tokens_neox': 3146, 'n_words': 865}