<commit_before>/*
 * Licensed to ElasticSearch and <NAME> under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. ElasticSearch 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.index.engine.robin;

import com.google.common.base.Predicate;
import org.elasticsearch.action.admin.cluster.node.info.NodeInfo;
import org.elasticsearch.action.admin.cluster.node.info.NodesInfoResponse;
import org.elasticsearch.action.admin.indices.segments.IndexSegments;
import org.elasticsearch.action.admin.indices.segments.IndexShardSegments;
import org.elasticsearch.action.admin.indices.segments.IndicesSegmentResponse;
import org.elasticsearch.action.admin.indices.segments.ShardSegments;
import org.elasticsearch.action.admin.indices.stats.IndicesStatsResponse;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.unit.ByteSizeUnit;
import org.elasticsearch.common.unit.ByteSizeValue;
import org.elasticsearch.common.util.BloomFilter;
import org.elasticsearch.index.codec.CodecService;
import org.elasticsearch.index.engine.Segment;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.test.ElasticsearchIntegrationTest;
import org.hamcrest.Matchers;
import org.junit.Test;

import java.lang.reflect.Field;
import java.util.Collection;

import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount;

public class RobinEngineIntegrationTest extends ElasticsearchIntegrationTest {

    @Test
    public void testSettingLoadBloomFilterDefaultTrue() throws Exception {
        Field allowRamBytesUsed = RobinEngine.class.getDeclaredField("allowRamBytesUsed");
        allowRamBytesUsed.setAccessible(true);
        allowRamBytesUsed.set(RobinEngine.class, Boolean.TRUE);

        try {
            client().admin().indices().prepareCreate("test").setSettings(ImmutableSettings.builder().put("number_of_replicas", 0).put("number_of_shards", 1)).get();
            client().prepareIndex("test", "foo").setSource("field", "foo").get();
            ensureGreen();
            refresh();
            IndicesStatsResponse stats = client().admin().indices().prepareStats().setSegments(true).get();
            final long segmentsMemoryWithBloom = stats.getTotal().getSegments().getMemoryInBytes();
            logger.info("segments with bloom: {}", segmentsMemoryWithBloom);

            logger.info("updating the setting to unload bloom filters");
            client().admin().indices().prepareUpdateSettings("test").setSettings(ImmutableSettings.builder().put(CodecService.INDEX_CODEC_BLOOM_LOAD, false)).get();
            logger.info("waiting for memory to match without blooms");
            awaitBusy(new Predicate<Object>() {
                public boolean apply(Object o) {
                    IndicesStatsResponse stats = client().admin().indices().prepareStats().setSegments(true).get();
                    long segmentsMemoryWithoutBloom = stats.getTotal().getSegments().getMemoryInBytes();
                    logger.info("trying segments without bloom: {}", segmentsMemoryWithoutBloom);
                    return segmentsMemoryWithoutBloom == (segmentsMemoryWithBloom - BloomFilter.Factory.DEFAULT.createFilter(1).getSizeInBytes());
                }
            });

            logger.info("updating the setting to load bloom filters");
            client().admin().indices().prepareUpdateSettings("test").setSettings(ImmutableSettings.builder().put(CodecService.INDEX_CODEC_BLOOM_LOAD, true)).get();
            logger.info("waiting for memory to match with blooms");
            awaitBusy(new Predicate<Object>() {
                public boolean apply(Object o) {
                    IndicesStatsResponse stats = client().admin().indices().prepareStats().setSegments(true).get();
                    long newSegmentsMemoryWithBloom = stats.getTotal().getSegments().getMemoryInBytes();
                    logger.info("trying segments with bloom: {}", newSegmentsMemoryWithBloom);
                    return newSegmentsMemoryWithBloom == segmentsMemoryWithBloom;
                }
            });
        } finally {
            allowRamBytesUsed.set(RobinEngine.class, Boolean.FALSE);
        }
    }

    @Test
    public void testSettingLoadBloomFilterDefaultFalse() throws Exception {
        Field allowRamBytesUsed = RobinEngine.class.getDeclaredField("allowRamBytesUsed");
        allowRamBytesUsed.setAccessible(true);
        allowRamBytesUsed.set(RobinEngine.class, Boolean.TRUE);
        try {
            client().admin().indices().prepareCreate("test").setSettings(ImmutableSettings.builder().put("number_of_replicas", 0).put("number_of_shards", 1).put(CodecService.INDEX_CODEC_BLOOM_LOAD, false)).get();
            client().prepareIndex("test", "foo").setSource("field", "foo").get();
            ensureGreen();
            refresh();

            IndicesStatsResponse stats = client().admin().indices().prepareStats().setSegments(true).get();
            final long segmentsMemoryWithoutBloom = stats.getTotal().getSegments().getMemoryInBytes();
            logger.info("segments without bloom: {}", segmentsMemoryWithoutBloom);

            logger.info("updating the setting to load bloom filters");<commit_msg>Add @Slow annotation to bad apples<commit_after>/*
 * Licensed to ElasticSearch and <NAME> under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. ElasticSearch 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.index.engine.robin;

import com.google.common.base.Predicate;
import org.apache.lucene.util.LuceneTestCase.Slow;
import org.elasticsearch.action.admin.cluster.node.info.NodeInfo;
import org.elasticsearch.action.admin.cluster.node.info.NodesInfoResponse;
import org.elasticsearch.action.admin.indices.segments.IndexSegments;
import org.elasticsearch.action.admin.indices.segments.IndexShardSegments;
import org.elasticsearch.action.admin.indices.segments.IndicesSegmentResponse;
import org.elasticsearch.action.admin.indices.segments.ShardSegments;
import org.elasticsearch.action.admin.indices.stats.IndicesStatsResponse;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.unit.ByteSizeUnit;
import org.elasticsearch.common.unit.ByteSizeValue;
import org.elasticsearch.common.util.BloomFilter;
import org.elasticsearch.index.codec.CodecService;
import org.elasticsearch.index.engine.Segment;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.test.ElasticsearchIntegrationTest;
import org.hamcrest.Matchers;
import org.junit.Test;

import java.lang.reflect.Field;
import java.util.Collection;

import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount;

public class RobinEngineIntegrationTest extends ElasticsearchIntegrationTest {

    @Test
    @Slow
    public void testSettingLoadBloomFilterDefaultTrue() throws Exception {
        Field allowRamBytesUsed = RobinEngine.class.getDeclaredField("allowRamBytesUsed");
        allowRamBytesUsed.setAccessible(true);
        allowRamBytesUsed.set(RobinEngine.class, Boolean.TRUE);

        try {
            client().admin().indices().prepareCreate("test").setSettings(ImmutableSettings.builder().put("number_of_replicas", 0).put("number_of_shards", 1)).get();
            client().prepareIndex("test", "foo").setSource("field", "foo").get();
            ensureGreen();
            refresh();
            IndicesStatsResponse stats = client().admin().indices().prepareStats().setSegments(true).get();
            final long segmentsMemoryWithBloom = stats.getTotal().getSegments().getMemoryInBytes();
            logger.info("segments with bloom: {}", segmentsMemoryWithBloom);

            logger.info("updating the setting to unload bloom filters");
            client().admin().indices().prepareUpdateSettings("test").setSettings(ImmutableSettings.builder().put(CodecService.INDEX_CODEC_BLOOM_LOAD, false)).get();
            logger.info("waiting for memory to match without blooms");
            awaitBusy(new Predicate<Object>() {
                public boolean apply(Object o) {
                    IndicesStatsResponse stats = client().admin().indices().prepareStats().setSegments(true).get();
                    long segmentsMemoryWithoutBloom = stats.getTotal().getSegments().getMemoryInBytes();
                    logger.info("trying segments without bloom: {}", segmentsMemoryWithoutBloom);
                    return segmentsMemoryWithoutBloom == (segmentsMemoryWithBloom - BloomFilter.Factory.DEFAULT.createFilter(1).getSizeInBytes());
                }
            });

            logger.info("updating the setting to load bloom filters");
            client().admin().indices().prepareUpdateSettings("test").setSettings(ImmutableSettings.builder().put(CodecService.INDEX_CODEC_BLOOM_LOAD, true)).get();
            logger.info("waiting for memory to match with blooms");
            awaitBusy(new Predicate<Object>() {
                public boolean apply(Object o) {
                    IndicesStatsResponse stats = client().admin().indices().prepareStats().setSegments(true).get();
                    long newSegmentsMemoryWithBloom = stats.getTotal().getSegments().getMemoryInBytes();
                    logger.info("trying segments with bloom: {}", newSegmentsMemoryWithBloom);
                    return newSegmentsMemoryWithBloom == segmentsMemoryWithBloom;
                }
            });
        } finally {
            allowRamBytesUsed.set(RobinEngine.class, Boolean.FALSE);
        }
    }

    @Test
    @Slow
    public void testSettingLoadBloomFilterDefaultFalse() throws Exception {
        Field allowRamBytesUsed = RobinEngine.class.getDeclaredField("allowRamBytesUsed");
        allowRamBytesUsed.setAccessible(true);
        allowRamBytesUsed.set(RobinEngine.class, Boolean.TRUE);
        try {
            client().admin().indices().prepareCreate("test").setSettings(ImmutableSettings.builder().put("number_of_replicas", 0).put("number_of_shards", 1).put(CodecService.INDEX_CODEC_BLOOM_LOAD, false)).get();
            client().prepareIndex("test", "foo").setSource("field", "foo").get();
            ensureGreen();
            refresh();

            IndicesStatsResponse stats = client().admin().indices().prepareStats().setSegments(true).get();
            final long segmentsMemoryWithoutBloom = stats.getTotal().getSegments().getMemoryInBytes();
            logger.info("segments without bloom: {}", segmentsMemoryWithoutBloom);

            logger.info("updating the setting to load bloom filters");
{'fraction_non_alphanumeric': 0.1099916736053289, 'fraction_numerical': 0.001498751040799334, 'mean_word_length': 3.7568316831683166, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '6544739', 'n_tokens_mistral': 3063, 'n_tokens_neox': 3043, 'n_words': 630}