Created
April 26, 2012 18:29
-
-
Save davie/2501687 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.apache.log4j.ConsoleAppender; | |
import org.apache.log4j.Level; | |
import org.apache.log4j.Logger; | |
import org.apache.log4j.PatternLayout; | |
import org.elasticsearch.action.admin.indices.refresh.RefreshRequest; | |
import org.elasticsearch.action.bulk.BulkRequestBuilder; | |
import org.elasticsearch.action.bulk.BulkResponse; | |
import org.elasticsearch.action.search.SearchRequestBuilder; | |
import org.elasticsearch.action.search.SearchResponse; | |
import org.elasticsearch.client.Requests; | |
import org.elasticsearch.client.transport.TransportClient; | |
import org.elasticsearch.common.settings.ImmutableSettings; | |
import org.elasticsearch.common.settings.Settings; | |
import org.elasticsearch.common.transport.InetSocketTransportAddress; | |
import org.elasticsearch.common.xcontent.XContentBuilder; | |
import org.elasticsearch.common.xcontent.XContentFactory; | |
import org.elasticsearch.index.query.QueryBuilders; | |
import org.elasticsearch.index.query.QueryStringQueryBuilder; | |
import org.elasticsearch.search.SearchHits; | |
import org.junit.Before; | |
import org.junit.Test; | |
import java.io.IOException; | |
public class EsTimeoutTest { | |
public static final String INDEX_NAME = "slowindex"; | |
@Before | |
public void setUp() throws Exception { | |
Logger rootLogger = Logger.getRootLogger(); | |
rootLogger.setLevel(Level.DEBUG); | |
PatternLayout layout = new PatternLayout(PatternLayout.TTCC_CONVERSION_PATTERN); | |
rootLogger.addAppender(new ConsoleAppender(layout)); | |
} | |
@Test | |
public void reproduceTimeout() throws Exception { | |
Settings settings = ImmutableSettings | |
.settingsBuilder() | |
// setting the timeout to a higher number works. | |
//.put("client.transport.ping_timeout", "100s") | |
.build(); | |
TransportClient transportClient = | |
new TransportClient(settings) | |
.addTransportAddress(new InetSocketTransportAddress("localhost", 9300)); | |
for (int i = 0; i < 10; i++) { | |
indexABigDocument(transportClient); | |
} | |
transportClient.admin().indices().refresh(new RefreshRequest(INDEX_NAME)).actionGet(); | |
SearchRequestBuilder builder = transportClient.prepareSearch(INDEX_NAME); | |
QueryStringQueryBuilder value = QueryBuilders.queryString("John"); | |
long start = System.currentTimeMillis(); | |
// This query times out, and the node gets disconnected | |
SearchResponse searchResponse = builder.setQuery(value).setSize(100).execute().actionGet(); | |
SearchHits hits = searchResponse.getHits(); | |
System.out.println("searchResponse.getHits().getTotalHits() = " + hits.getTotalHits()); | |
long end = System.currentTimeMillis(); | |
System.out.println("request took: " + (end - start) + " ms"); | |
} | |
private void indexABigDocument(TransportClient client) { | |
try { | |
BulkRequestBuilder request = client.prepareBulk(); | |
for (int i = 0; i < 1; i++) { | |
XContentBuilder builder = XContentFactory.jsonBuilder().startObject(); | |
builder.field("firstname", "John"); | |
builder.field("lastname", "Lee"); | |
builder.field("data", aReallyLongString()); | |
builder.endObject(); | |
request.add(Requests.indexRequest(INDEX_NAME).type("my_type") | |
.source(builder)); | |
} | |
BulkResponse response = request.execute().actionGet(); | |
if (response.hasFailures()) { | |
System.out.println("Bulk request failed"); | |
} | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
private String aReallyLongString() throws IOException { | |
StringBuilder stringBuilder = new StringBuilder(); | |
for (int j = 0; j < 1000000; j++) { | |
stringBuilder.append("some more stuff"); | |
} | |
System.out.println("stringBuilder.toString().getBytes().length = " + stringBuilder.toString().getBytes().length); | |
return stringBuilder.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment