Package org.elasticsearch.test.stress.mget

Source Code of org.elasticsearch.test.stress.mget.MGetStress1

/*
* Licensed to Elastic Search and Shay Banon 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.test.stress.mget;

import org.elasticsearch.action.get.MultiGetItemResponse;
import org.elasticsearch.action.get.MultiGetResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.collect.Sets;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.util.concurrent.jsr166y.ThreadLocalRandom;
import org.elasticsearch.node.Node;
import org.elasticsearch.node.NodeBuilder;

import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;

/**
*/
public class MGetStress1 {

    public static void main(String[] args) throws Exception {
        final int NUMBER_OF_NODES = 2;
        final int NUMBER_OF_DOCS = 50000;
        final int MGET_BATCH = 1000;

        Node[] nodes = new Node[NUMBER_OF_NODES];
        for (int i = 0; i < nodes.length; i++) {
            nodes[i] = NodeBuilder.nodeBuilder().node();
        }

        System.out.println("---> START Indexing initial data [" + NUMBER_OF_DOCS + "]");
        final Client client = nodes[0].client();
        for (int i = 0; i < NUMBER_OF_DOCS; i++) {
            client.prepareIndex("test", "type", Integer.toString(i)).setSource("field", "value").execute().actionGet();
        }
        System.out.println("---> DONE Indexing initial data [" + NUMBER_OF_DOCS + "]");

        final AtomicBoolean done = new AtomicBoolean();
        // start indexer
        Thread indexer = new Thread(new Runnable() {
            @Override public void run() {
                while (!done.get()) {
                    client.prepareIndex("test", "type", Integer.toString(ThreadLocalRandom.current().nextInt(NUMBER_OF_DOCS)))
                            .setSource("field", "value").execute().actionGet();
                }
            }
        });
        indexer.start();
        System.out.println("---> Starting indexer");

        // start the mget one
        Thread mget = new Thread(new Runnable() {
            @Override public void run() {
                while (!done.get()) {
                    Set<String> ids = Sets.newHashSet();
                    for (int i = 0; i < MGET_BATCH; i++) {
                        ids.add(Integer.toString(ThreadLocalRandom.current().nextInt(NUMBER_OF_DOCS)));
                    }
                    //System.out.println("---> mget for [" + ids.size() + "]");
                    MultiGetResponse response = client.prepareMultiGet().add("test", "type", ids).execute().actionGet();
                    int expected = ids.size();
                    int count = 0;
                    for (MultiGetItemResponse item : response) {
                        count++;
                        if (item.failed()) {
                            System.err.println("item failed... " + item.failure());
                        } else {
                            boolean removed = ids.remove(item.id());
                            if (!removed) {
                                System.err.println("got id twice " + item.id());
                            }
                        }
                    }
                    if (expected != count) {
                        System.err.println("Expected [" + expected + "], got back [" + count + "]");
                    }
                }
            }
        });
        mget.start();
        System.out.println("---> Starting mget");

        Thread.sleep(TimeValue.timeValueMinutes(10).millis());

        done.set(true);
    }
}
TOP

Related Classes of org.elasticsearch.test.stress.mget.MGetStress1

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.