<commit_before>/*
 * Copyright (c) 2008-2013, Hazelcast, Inc. All Rights Reserved.
 *
 * Licensed 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 com.hazelcast.map;

import com.hazelcast.config.Config;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.IMap;
import com.hazelcast.core.LifecycleEvent;
import com.hazelcast.core.LifecycleListener;
import com.hazelcast.instance.GroupProperties;
import com.hazelcast.instance.TestUtil;
import com.hazelcast.monitor.LocalMapStats;
import com.hazelcast.test.HazelcastSerialClassRunner;
import com.hazelcast.test.HazelcastTestSupport;
import com.hazelcast.test.TestHazelcastInstanceFactory;
import com.hazelcast.test.annotation.SlowTest;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;

import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReferenceArray;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

@RunWith(HazelcastSerialClassRunner.class)
@Category(SlowTest.class)
public class BackupTest extends HazelcastTestSupport {

    private static final String MAP_NAME = "default";

    @Before
    public void gc() {
        Runtime.getRuntime().gc();
    }

    @Test
    public void testGracefulShutdown() throws Exception {
        int size = 250000;
        TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(4);
        final Config config = new Config();
        config.setProperty(GroupProperties.PROP_PARTITION_COUNT, "1111");

        HazelcastInstance h1 = nodeFactory.newHazelcastInstance(config);
        IMap m1 = h1.getMap(MAP_NAME);
        for (int i = 0; i < size; i++) {
            m1.put(i, i);
        }

        HazelcastInstance h2 = nodeFactory.newHazelcastInstance(config);
        IMap m2 = h2.getMap(MAP_NAME);
        h1.getLifecycleService().shutdown();
        assertEquals(size, m2.size());

        HazelcastInstance h3 = nodeFactory.newHazelcastInstance(config);
        IMap m3 = h3.getMap(MAP_NAME);
        h2.getLifecycleService().shutdown();
        assertEquals(size, m3.size());

        HazelcastInstance h4 = nodeFactory.newHazelcastInstance(config);
        IMap m4 = h4.getMap(MAP_NAME);
        h3.getLifecycleService().shutdown();
        assertEquals(size, m4.size());
    }

    @Test
    public void testGracefulShutdown2() throws Exception {
        Config config = new Config();
        config.getMapConfig(MAP_NAME).setBackupCount(2).setStatisticsEnabled(true);
        config.setProperty(GroupProperties.PROP_PARTITION_COUNT, "1111");

        TestHazelcastInstanceFactory f = createHazelcastInstanceFactory(6);
        final HazelcastInstance hz = f.newHazelcastInstance(config);

        final IMap<Object, Object> map = hz.getMap(MAP_NAME);
        final int size = 200000;
        for (int i = 0; i < size; i++) {
            map.put(i, i);
        }

        final HazelcastInstance hz2 = f.newHazelcastInstance(config);
        final IMap<Object, Object> map2 = hz2.getMap(MAP_NAME);

        Assert.assertEquals(size, map2.size());

        final HazelcastInstance hz3 = f.newHazelcastInstance(config);
        final IMap<Object, Object> map3 = hz3.getMap(MAP_NAME);

        final HazelcastInstance hz4 = f.newHazelcastInstance(config);
        final IMap<Object, Object> map4 = hz4.getMap(MAP_NAME);

        Assert.assertEquals(size, map3.size());
        Assert.assertEquals(size, map4.size());

        final HazelcastInstance hz5 = f.newHazelcastInstance(config);
        final IMap<Object, Object> map5 = hz5.getMap(MAP_NAME);

        final HazelcastInstance hz6 = f.newHazelcastInstance(config);
        final IMap<Object, Object> map6 = hz6.getMap(MAP_NAME);

        Assert.assertEquals(size, map5.size());
        Assert.assertEquals(size, map6.size());

        hz.getLifecycleService().shutdown();
        hz2.getLifecycleService().shutdown();

        Assert.assertEquals(size, map3.size());
        Assert.assertEquals(size, map4.size());
        Assert.assertEquals(size, map5.size());
        Assert.assertEquals(size, map6.size());

        hz3.getLifecycleService().shutdown();
        hz4.getLifecycleService().shutdown();

        Assert.assertEquals(size, map5.size());
        Assert.assertEquals(size, map6.size());
    }

    @Test
    public void testGracefulShutdown3() throws Exception {
        Config config = new Config();
        config.getMapConfig(MAP_NAME).setBackupCount(2).setStatisticsEnabled(true);
        config.setProperty(GroupProperties.PROP_PARTITION_COUNT, "1111");

        TestHazelcastInstanceFactory f = createHazelcastInstanceFactory(6);
        final HazelcastInstance hz = f.newHazelcastInstance(config);

        final IMap<Object, Object> map = hz.getMap(MAP_NAME);
        final int size = 200000;
        for (int i = 0; i < size; i++) {
            map.put(i, i);
        }

        final HazelcastInstance hz2 = f.newHazelcastInstance(config);
        final IMap<Object, Object> map2 = hz2.getMap(MAP_NAME);

        final HazelcastInstance hz3 = f.newHazelcastInstance(config);
        final IMap<Object, Object> map3 = hz3.getMap(MAP_NAME);

        final HazelcastInstance hz4 = f.newHazelcastInstance(config);
        final IMap<Object, Object> map4 = hz4.getMap(MAP_NAME);

        final HazelcastInstance hz5 = f.newHazelcastInstance(config);
        final IMap<Object, Object> map5 = hz5.getMap(MAP_NAME);

        final HazelcastInstance hz6 = f.newHazelcastInstance(config);
        final IMap<Object, Object> map6 = hz6.getMap(MAP_NAME);

        Assert.assertEquals(size, map2.size());
        Assert.assertEquals(size, map3.size());
        Assert.assertEquals(size, map4.size());
        Assert.assertEquals(size, map5.size());
        Assert.assertEquals(size, map6.size());

        hz6.getLifecycleService().shutdown();
        Assert.assertEquals(size, map.size());
        Assert.assertEquals(size, map2.size());
        Assert.assertEquals(size, map3.size());
        Assert.assertEquals(size, map4.size());
        Assert.assertEquals(size, map5.size());

        hz2.getLifecycleService().shutdown();
        Assert.assertEquals(size, map.size());
        Assert.assertEquals(size, map3.size());
        Assert.assertEquals(size, map4.size());
        Assert.assertEquals(size, map5.size());

        hz5.getLifecycleService().shutdown();
        Assert.assertEquals(size, map.size());
        Assert.assertEquals(size, map3.size());
        Assert.assertEquals(size, map4.size());

        hz3.getLifecycleService().shutdown();
        Assert.assertEquals(size, map.size());
        Assert.assertEquals(size, map4.size());

        hz4.getLifecycleService().shutdown();
        Assert.assertEquals(size, map.size());
    }

    /**
     * Fix for the issue 275.
     */
    @Test(timeout = 300 * 1000)
    public void testBackupMigrationAndRecovery() throws Exception {
        testBackupMigrationAndRecovery(4, 1, 50000);
    }<commit_msg>add listener and latch wait<commit_after>/*
 * Copyright (c) 2008-2013, Hazelcast, Inc. All Rights Reserved.
 *
 * Licensed 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 com.hazelcast.map;

import com.hazelcast.config.Config;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.IMap;
import com.hazelcast.core.LifecycleEvent;
import com.hazelcast.core.LifecycleListener;
import com.hazelcast.instance.GroupProperties;
import com.hazelcast.instance.TestUtil;
import com.hazelcast.monitor.LocalMapStats;
import com.hazelcast.test.HazelcastSerialClassRunner;
import com.hazelcast.test.HazelcastTestSupport;
import com.hazelcast.test.TestHazelcastInstanceFactory;
import com.hazelcast.test.annotation.SlowTest;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;

import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReferenceArray;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

@RunWith(HazelcastSerialClassRunner.class)
@Category(SlowTest.class)
public class BackupTest extends HazelcastTestSupport {

    private static final String MAP_NAME = "default";

    @Before
    public void gc() {
        Runtime.getRuntime().gc();
    }

    @Test
    public void testGracefulShutdown() throws Exception {
        int size = 250000;
        TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(4);
        final Config config = new Config();
        config.setProperty(GroupProperties.PROP_PARTITION_COUNT, "1111");

        HazelcastInstance h1 = nodeFactory.newHazelcastInstance(config);
        IMap m1 = h1.getMap(MAP_NAME);
        for (int i = 0; i < size; i++) {
            m1.put(i, i);
        }

        HazelcastInstance h2 = nodeFactory.newHazelcastInstance(config);
        IMap m2 = h2.getMap(MAP_NAME);
        h1.getLifecycleService().shutdown();
        assertEquals(size, m2.size());

        HazelcastInstance h3 = nodeFactory.newHazelcastInstance(config);
        IMap m3 = h3.getMap(MAP_NAME);
        h2.getLifecycleService().shutdown();
        assertEquals(size, m3.size());

        HazelcastInstance h4 = nodeFactory.newHazelcastInstance(config);
        IMap m4 = h4.getMap(MAP_NAME);
        h3.getLifecycleService().shutdown();
        assertEquals(size, m4.size());
    }

    @Test
    public void testGracefulShutdown2() throws Exception {
        Config config = new Config();
        config.getMapConfig(MAP_NAME).setBackupCount(2).setStatisticsEnabled(true);
        config.setProperty(GroupProperties.PROP_PARTITION_COUNT, "1111");

        TestHazelcastInstanceFactory f = createHazelcastInstanceFactory(6);
        final HazelcastInstance hz = f.newHazelcastInstance(config);

        final IMap<Object, Object> map = hz.getMap(MAP_NAME);
        final int size = 200000;
        for (int i = 0; i < size; i++) {
            map.put(i, i);
        }

        final HazelcastInstance hz2 = f.newHazelcastInstance(config);
        final IMap<Object, Object> map2 = hz2.getMap(MAP_NAME);

        Assert.assertEquals(size, map2.size());

        final HazelcastInstance hz3 = f.newHazelcastInstance(config);
        final IMap<Object, Object> map3 = hz3.getMap(MAP_NAME);

        final HazelcastInstance hz4 = f.newHazelcastInstance(config);
        final IMap<Object, Object> map4 = hz4.getMap(MAP_NAME);

        Assert.assertEquals(size, map3.size());
        Assert.assertEquals(size, map4.size());

        final HazelcastInstance hz5 = f.newHazelcastInstance(config);
        final IMap<Object, Object> map5 = hz5.getMap(MAP_NAME);

        final HazelcastInstance hz6 = f.newHazelcastInstance(config);
        final IMap<Object, Object> map6 = hz6.getMap(MAP_NAME);

        Assert.assertEquals(size, map5.size());
        Assert.assertEquals(size, map6.size());

        hz.getLifecycleService().shutdown();
        hz2.getLifecycleService().shutdown();

        Assert.assertEquals(size, map3.size());
        Assert.assertEquals(size, map4.size());
        Assert.assertEquals(size, map5.size());
        Assert.assertEquals(size, map6.size());

        hz3.getLifecycleService().shutdown();
        hz4.getLifecycleService().shutdown();

        Assert.assertEquals(size, map5.size());
        Assert.assertEquals(size, map6.size());
    }

    @Test
    public void testGracefulShutdown3() throws Exception {
        Config config = new Config();
        config.getMapConfig(MAP_NAME).setBackupCount(2).setStatisticsEnabled(true);
        config.setProperty(GroupProperties.PROP_PARTITION_COUNT, "1111");

        TestHazelcastInstanceFactory f = createHazelcastInstanceFactory(6);
        final HazelcastInstance hz = f.newHazelcastInstance(config);

        final IMap<Object, Object> map = hz.getMap(MAP_NAME);
        final int size = 200000;
        for (int i = 0; i < size; i++) {
            map.put(i, i);
        }

        final HazelcastInstance hz2 = f.newHazelcastInstance(config);
        final IMap<Object, Object> map2 = hz2.getMap(MAP_NAME);

        final HazelcastInstance hz3 = f.newHazelcastInstance(config);
        final IMap<Object, Object> map3 = hz3.getMap(MAP_NAME);

        final HazelcastInstance hz4 = f.newHazelcastInstance(config);
        final IMap<Object, Object> map4 = hz4.getMap(MAP_NAME);

        final HazelcastInstance hz5 = f.newHazelcastInstance(config);
        final IMap<Object, Object> map5 = hz5.getMap(MAP_NAME);

        final HazelcastInstance hz6 = f.newHazelcastInstance(config);
        final IMap<Object, Object> map6 = hz6.getMap(MAP_NAME);

        Assert.assertEquals(size, map2.size());
        Assert.assertEquals(size, map3.size());
        Assert.assertEquals(size, map4.size());
        Assert.assertEquals(size, map5.size());
        Assert.assertEquals(size, map6.size());

        addListener(hz6).await(3, TimeUnit.SECONDS);
        hz6.getLifecycleService().shutdown();
        Assert.assertEquals(size, map.size());
        Assert.assertEquals(size, map2.size());
        Assert.assertEquals(size, map3.size());
        Assert.assertEquals(size, map4.size());
        Assert.assertEquals(size, map5.size());


        addListener(hz2).await(3, TimeUnit.SECONDS);
        hz2.getLifecycleService().shutdown();
        Assert.assertEquals(size, map.size());
        Assert.assertEquals(size, map3.size());
        Assert.assertEquals(size, map4.size());
        Assert.assertEquals(size, map5.size());


        addListener(hz5).await(3, TimeUnit.SECONDS);
        hz5.getLifecycleService().shutdown();
        Assert.assertEquals(size, map.size());
        Assert.assertEquals(size, map3.size());
        Assert.assertEquals(size, map4.size());

        addListener(hz3).await(3, TimeUnit.SECONDS);
        hz3.getLifecycleService().shutdown();
        Assert.assertEquals(size, map.size());
        Assert.assertEquals(size, map4.size());

        addListener(hz4).await(3, TimeUnit.SECONDS);
        hz4.getLifecycleService().shutdown();
        Assert.assertEquals(size, map.size());
    }

    private CountDownLatch addListener(HazelcastInstance hz) {
        final CountDownLatch latch = new CountDownLatch(1);
        // add listener
        hz.getLifecycleService().addLifecycleListener(new LifecycleListener() {
            @Override
            public void stateChanged(LifecycleEvent event) {
                if (event.getState().equals(LifecycleEvent.LifecycleState.SHUTDOWN)) {
                    latch.countDown();
                }
            }
        });

        return latch;
    }

    /**
     * Fix for the issue 275.
     */
    @Test(timeout = 300 * 1000)
    public void testBackupMigrationAndRecovery() throws Exception {
        testBackupMigrationAndRecovery(4, 1, 50000);
    }
{'fraction_non_alphanumeric': 0.11400530831430158, 'fraction_numerical': 0.019443244244182457, 'mean_word_length': 3.296473084062583, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 27, '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': '6436999', 'n_tokens_mistral': 5038, 'n_tokens_neox': 4870, 'n_words': 983}