Skip to content

Instantly share code, notes, and snippets.

@peschlowp
Created January 9, 2014 09:24
Show Gist options
  • Save peschlowp/8331596 to your computer and use it in GitHub Desktop.
Save peschlowp/8331596 to your computer and use it in GitHub Desktop.
Java program that demoes a possible bug in ElasticSearch 0.90.9 leading to an update request blocking forever on the client. Steps needed to run the program: 1. Set the cluster name of your choice and start up an ElasticSearch server. The index and mapping will be created dynamically. 2. Put ElasticSearch 0.90.9 library on the classpath.
package ppe.test.elasticsearch;
import java.io.IOException;
import org.elasticsearch.action.ActionFuture;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.node.Node;
import org.elasticsearch.node.NodeBuilder;
public class Experiment {
public static void main(String[] args) {
new Experiment().run();
}
private static final String CLUSTER_NAME = "my-cluster";
private static final String INDEX_NAME = "my-test-index";
private static final String TYPE_NAME = "my-test-type";
// set to 0 and you will get VersionConflictEngineException, with the program completing normally
private static final int NUM_RETRIES_ON_VERSION_CONFLICT = 1;
// if false the wait on future completion will hang the program forever, if true a timeout of 10 seconds is used
private static final boolean USE_TIMEOUT_FOR_FUTURE_GET = true;
private static final int NUM_TEST_DOCS = 100;
private Node node;
private Client client;
private void init() {
node = NodeBuilder.nodeBuilder().clusterName(CLUSTER_NAME).client(true).node();
client = node.client();
}
private void release() {
client.close();
node.close();
}
private void run() {
init();
insertDocuments();
Thread updater = new Thread(new Runnable() {
@Override
public void run() {
updateDocuments();
}
}, "updater");
Thread deleter = new Thread(new Runnable() {
@Override
public void run() {
deleteDocuments();
}
}, "deleter");
updater.start();
deleter.start();
try {
updater.join();
deleter.join();
} catch (InterruptedException e) {
// Not of concern for this test program.
}
release();
}
private void insertDocuments() {
System.out.println("Inserting " + NUM_TEST_DOCS + " documents");
for (int i = 0; i < NUM_TEST_DOCS; i++) {
String id = String.valueOf(i);
System.out.println("Inserting document " + id);
try {
add(id);
} catch (Exception e) {
System.err.println("Exception during insert: " + e);
}
}
}
private void updateDocuments() {
System.out.println("Updating " + NUM_TEST_DOCS + " documents");
for (int i = 0; i < NUM_TEST_DOCS; i++) {
String id = String.valueOf(i);
System.out.println("Updating document " + id);
try {
update(id);
Thread.sleep(10);
} catch (Exception e) {
System.err.println("Exception during update: " + e);
}
}
}
private void deleteDocuments() {
System.out.println("Deleting " + NUM_TEST_DOCS + " documents");
for (int i = 0; i < NUM_TEST_DOCS; i++) {
String id = String.valueOf(i);
System.out.println("Deleting document " + id);
try {
delete(id);
Thread.sleep(10);
} catch (Exception e) {
System.err.println("Exception during delete: " + e);
}
}
}
private void add(String id) throws IOException {
waitUntilCompletes(client.prepareIndex(INDEX_NAME, TYPE_NAME, id).setSource(buildJson(id))
.setRefresh(true).execute());
}
private void update(String id) throws IOException {
waitUntilCompletes(client.prepareUpdate(INDEX_NAME, TYPE_NAME, id).setDoc(buildJson(id))
.setRetryOnConflict(NUM_RETRIES_ON_VERSION_CONFLICT).setRefresh(true).execute());
}
private void delete(String id) {
waitUntilCompletes(client.prepareDelete(INDEX_NAME, TYPE_NAME, id).setRefresh(true)
.execute());
}
private XContentBuilder buildJson(String id) throws IOException {
return XContentFactory.jsonBuilder().startObject().field("id", id).endObject();
}
private void waitUntilCompletes(ActionFuture<?> future) {
if (USE_TIMEOUT_FOR_FUTURE_GET) {
future.actionGet(10000);
} else {
future.actionGet();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment