diff --git a/elasticsearch5/pom.xml b/elasticsearch5/pom.xml index 54280ea6cd7bff5bfb80257f403d8a9be6252993..0370be1f02ffcbeceba165dd387c1d622e4bc953 100644 --- a/elasticsearch5/pom.xml +++ b/elasticsearch5/pom.xml @@ -65,37 +65,29 @@ LICENSE file. </executions> </plugin> <plugin> - <groupId>org.apache.maven.plugins</groupId> - <artifactId>maven-antrun-plugin</artifactId> - <version>1.8</version> + <groupId>com.github.alexcojocaru</groupId> + <artifactId>elasticsearch-maven-plugin</artifactId> + <version>5.9</version> + <configuration> + <version>${elasticsearch5-version}</version> + <clusterName>test</clusterName> + <httpPort>9200</httpPort> + <transportPort>9300</transportPort> + </configuration> <executions> - <!-- start up external cluster --> <execution> - <id>integ-setup</id> + <id>start-elasticsearch</id> <phase>pre-integration-test</phase> <goals> - <goal>run</goal> + <goal>runforked</goal> </goals> - <configuration> - <skip>${skipTests}</skip> - <target> - <ant antfile="src/test/ant/integration-tests.xml" target="start-external-cluster"/> - </target> - </configuration> </execution> - <!-- shut down external cluster --> <execution> - <id>integ-teardown</id> + <id>stop-elasticsearch</id> <phase>post-integration-test</phase> <goals> - <goal>run</goal> + <goal>stop</goal> </goals> - <configuration> - <skip>${skipTests}</skip> - <target> - <ant antfile="src/test/ant/integration-tests.xml" target="stop-external-cluster"/> - </target> - </configuration> </execution> </executions> </plugin> diff --git a/elasticsearch5/src/main/java/com/yahoo/ycsb/db/elasticsearch5/ElasticsearchClient.java b/elasticsearch5/src/main/java/com/yahoo/ycsb/db/elasticsearch5/ElasticsearchClient.java index cf8100dc84e73ed025afcaa92ab6b5d857614cc5..bce0689c953b68a6dcd08a10ea755fa34ffeaf71 100644 --- a/elasticsearch5/src/main/java/com/yahoo/ycsb/db/elasticsearch5/ElasticsearchClient.java +++ b/elasticsearch5/src/main/java/com/yahoo/ycsb/db/elasticsearch5/ElasticsearchClient.java @@ -22,9 +22,12 @@ import com.yahoo.ycsb.DB; import com.yahoo.ycsb.DBException; import com.yahoo.ycsb.Status; import com.yahoo.ycsb.StringByteIterator; +import org.elasticsearch.action.DocWriteResponse; import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest; import org.elasticsearch.action.admin.indices.create.CreateIndexRequest; +import org.elasticsearch.action.admin.indices.refresh.RefreshRequest; import org.elasticsearch.action.delete.DeleteResponse; +import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.client.Requests; import org.elasticsearch.client.transport.TransportClient; @@ -33,9 +36,7 @@ import org.elasticsearch.common.transport.InetSocketTransportAddress; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.index.query.RangeQueryBuilder; import org.elasticsearch.index.query.TermQueryBuilder; -import org.elasticsearch.rest.RestStatus; import org.elasticsearch.search.SearchHit; -import org.elasticsearch.search.SearchHitField; import org.elasticsearch.transport.client.PreBuiltTransportClient; import java.net.InetAddress; @@ -148,6 +149,8 @@ public class ElasticsearchClient extends DB { } } + private volatile boolean isRefreshNeeded = false; + @Override public Status insert(String table, String key, Map<String, ByteIterator> values) { try { @@ -160,7 +163,16 @@ public class ElasticsearchClient extends DB { doc.field(KEY, key); doc.endObject(); - client.prepareIndex(indexKey, table).setSource(doc).execute().actionGet(); + final IndexResponse indexResponse = client.prepareIndex(indexKey, table).setSource(doc).get(); + if (indexResponse.getResult() != DocWriteResponse.Result.CREATED) { + return Status.ERROR; + } + + if (!isRefreshNeeded) { + synchronized (this) { + isRefreshNeeded = true; + } + } return Status.OK; } catch (final Exception e) { @@ -179,11 +191,17 @@ public class ElasticsearchClient extends DB { final String id = searchResponse.getHits().getAt(0).getId(); - final DeleteResponse deleteResponse = client.prepareDelete(indexKey, table, id).execute().actionGet(); - if (deleteResponse.status().equals(RestStatus.NOT_FOUND)) { + final DeleteResponse deleteResponse = client.prepareDelete(indexKey, table, id).get(); + if (deleteResponse.getResult() == DocWriteResponse.Result.NOT_FOUND) { return Status.NOT_FOUND; } + if (!isRefreshNeeded) { + synchronized (this) { + isRefreshNeeded = true; + } + } + return Status.OK; } catch (final Exception e) { e.printStackTrace(); @@ -206,15 +224,14 @@ public class ElasticsearchClient extends DB { final SearchHit hit = searchResponse.getHits().getAt(0); if (fields != null) { for (final String field : fields) { - result.put(field, new StringByteIterator( - (String) hit.getField(field).getValue())); + result.put(field, new StringByteIterator((String) hit.getSource().get(field))); } } else { - for (final Map.Entry<String, SearchHitField> e : hit.getFields().entrySet()) { + for (final Map.Entry<String, Object> e : hit.getSource().entrySet()) { if (KEY.equals(e.getKey())) { continue; } - result.put(e.getKey(), new StringByteIterator((String) e.getValue().getValue())); + result.put(e.getKey(), new StringByteIterator((String) e.getValue())); } } @@ -238,7 +255,18 @@ public class ElasticsearchClient extends DB { hit.getSource().put(entry.getKey(), entry.getValue()); } - client.prepareIndex(indexKey, table, hit.getId()).setSource(hit.getSource()).get(); + final IndexResponse indexResponse = + client.prepareIndex(indexKey, table, hit.getId()).setSource(hit.getSource()).get(); + + if (indexResponse.getResult() != DocWriteResponse.Result.UPDATED) { + return Status.ERROR; + } + + if (!isRefreshNeeded) { + synchronized (this) { + isRefreshNeeded = true; + } + } return Status.OK; } catch (final Exception e) { @@ -255,6 +283,7 @@ public class ElasticsearchClient extends DB { final Set<String> fields, final Vector<HashMap<String, ByteIterator>> result) { try { + refreshIfNeeded(); final RangeQueryBuilder query = new RangeQueryBuilder(KEY).gte(startkey); final SearchResponse response = client.prepareSearch(indexKey).setQuery(query).setSize(recordcount).get(); @@ -266,12 +295,12 @@ public class ElasticsearchClient extends DB { entry.put(field, new StringByteIterator((String) hit.getSource().get(field))); } } else { - entry = new HashMap<>(hit.getFields().size()); - for (final Map.Entry<String, SearchHitField> field : hit.getFields().entrySet()) { + entry = new HashMap<>(hit.getSource().size()); + for (final Map.Entry<String, Object> field : hit.getSource().entrySet()) { if (KEY.equals(field.getKey())) { continue; } - entry.put(field.getKey(), new StringByteIterator((String) field.getValue().getValue())); + entry.put(field.getKey(), new StringByteIterator((String) field.getValue())); } } result.add(entry); @@ -283,8 +312,25 @@ public class ElasticsearchClient extends DB { } } + private void refreshIfNeeded() { + if (isRefreshNeeded) { + final boolean refresh; + synchronized (this) { + if (isRefreshNeeded) { + refresh = true; + isRefreshNeeded = false; + } else { + refresh = false; + } + } + if (refresh) { + client.admin().indices().refresh(new RefreshRequest()).actionGet(); + } + } + } private SearchResponse search(final String table, final String key) { + refreshIfNeeded(); return client.prepareSearch(indexKey).setTypes(table).setQuery(new TermQueryBuilder(KEY, key)).get(); } diff --git a/elasticsearch5/src/main/java/com/yahoo/ycsb/db/elasticsearch5/ElasticsearchRestClient.java b/elasticsearch5/src/main/java/com/yahoo/ycsb/db/elasticsearch5/ElasticsearchRestClient.java index 07952c1020fe786d185eba3e457734ae97a9f7bb..b0c904f4030de244e7916b6669873759a782eb9a 100644 --- a/elasticsearch5/src/main/java/com/yahoo/ycsb/db/elasticsearch5/ElasticsearchRestClient.java +++ b/elasticsearch5/src/main/java/com/yahoo/ycsb/db/elasticsearch5/ElasticsearchRestClient.java @@ -55,7 +55,6 @@ import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; */ public class ElasticsearchRestClient extends DB { - private static final String DEFAULT_CLUSTER_NAME = "es.ycsb.cluster"; private static final String DEFAULT_INDEX_KEY = "es.ycsb"; private static final String DEFAULT_REMOTE_HOST = "localhost:9200"; private static final int NUMBER_OF_SHARDS = 1; @@ -183,6 +182,8 @@ public class ElasticsearchRestClient extends DB { } } } + + private volatile boolean isRefreshNeeded = false; @Override public Status insert(final String table, final String key, final Map<String, ByteIterator> values) { @@ -196,11 +197,17 @@ public class ElasticsearchRestClient extends DB { Collections.<String, String>emptyMap(), new NStringEntity(new ObjectMapper().writeValueAsString(data), ContentType.APPLICATION_JSON)); - if (response.getStatusLine().getStatusCode() == 201) { - return Status.OK; - } else { + if (response.getStatusLine().getStatusCode() != 201) { return Status.ERROR; } + + if (!isRefreshNeeded) { + synchronized (this) { + isRefreshNeeded = true; + } + } + + return Status.OK; } catch (final Exception e) { e.printStackTrace(); return Status.ERROR; @@ -226,13 +233,18 @@ public class ElasticsearchRestClient extends DB { } @SuppressWarnings("unchecked") final Map<String, Object> hit = (Map<String, Object>)((List<Object>)hits.get("hits")).get(0); - @SuppressWarnings("unchecked") final Map<String, Object> source = (Map<String, Object>)hit.get("_source"); final Response deleteResponse = - restClient.performRequest("DELETE", "/" + indexKey + "/" + table + "/" + source.get("_id")); + restClient.performRequest("DELETE", "/" + indexKey + "/" + table + "/" + hit.get("_id")); if (deleteResponse.getStatusLine().getStatusCode() != 200) { return Status.ERROR; } + if (!isRefreshNeeded) { + synchronized (this) { + isRefreshNeeded = true; + } + } + return Status.OK; } catch (final Exception e) { e.printStackTrace(); @@ -310,12 +322,19 @@ public class ElasticsearchRestClient extends DB { final Map<String, String> params = emptyMap(); final Response response = restClient.performRequest( "PUT", - "/" + indexKey + "/" + table + "/" + source.get("_id"), + "/" + indexKey + "/" + table + "/" + hit.get("_id"), params, new NStringEntity(new ObjectMapper().writeValueAsString(source), ContentType.APPLICATION_JSON)); if (response.getStatusLine().getStatusCode() != 200) { return Status.ERROR; } + + if (!isRefreshNeeded) { + synchronized (this) { + isRefreshNeeded = true; + } + } + return Status.OK; } catch (final Exception e) { e.printStackTrace(); @@ -376,6 +395,24 @@ public class ElasticsearchRestClient extends DB { } } + private void refreshIfNeeded() throws IOException { + if (isRefreshNeeded) { + final boolean refresh; + synchronized (this) { + if (isRefreshNeeded) { + refresh = true; + isRefreshNeeded = false; + } else { + refresh = false; + } + } + if (refresh) { + restClient.performRequest("POST", "/" + indexKey + "/_refresh"); + } + } + } + + private Response search(final String table, final String key) throws IOException { try (XContentBuilder builder = jsonBuilder()) { builder.startObject(); @@ -390,6 +427,7 @@ public class ElasticsearchRestClient extends DB { } private Response search(final String table, final XContentBuilder builder) throws IOException { + refreshIfNeeded(); final Map<String, String> params = emptyMap(); final StringEntity entity = new StringEntity(builder.string()); final Header header = new BasicHeader("content-type", ContentType.APPLICATION_JSON.toString()); diff --git a/elasticsearch5/src/test/ant/integration-tests.xml b/elasticsearch5/src/test/ant/integration-tests.xml deleted file mode 100644 index 59675ccebc08a8d296237000b7a8f6f2f8d046d8..0000000000000000000000000000000000000000 --- a/elasticsearch5/src/test/ant/integration-tests.xml +++ /dev/null @@ -1,282 +0,0 @@ -<?xml version="1.0"?> -<project name="integration-tests" xmlns:if="ant:if" xmlns:unless="ant:unless"> - <!-- our pid file for easy cleanup --> - <property name="integ.pidfile" location="${project.build.directory}/integration-tests/run/es.pid"/> - - <!-- if this exists, ES is running (maybe) --> - <available property="integ.pidfile.exists" file="${integ.pidfile}"/> - - <!-- name of our cluster, maybe needs changing --> - <property name="integ.cluster.name" value="elasticsearch_integration"/> - - <!-- runs an OS script --> - <macrodef name="run-script"> - <attribute name="script"/> - <attribute name="spawn" default="false"/> - <element name="nested" optional="true"/> - <sequential> - <local name="failonerror"/> - <condition property="failonerror"> - <isfalse value="@{spawn}"/> - </condition> - - <!-- create a temp CWD, to enforce that commands don't rely on CWD --> - <local name="temp.cwd"/> - <tempfile property="temp.cwd" destDir="${project.build.directory}/integration-tests/run/tmp" deleteonexit="true"/> - <mkdir dir="${temp.cwd}"/> - - <!-- print commands we run --> - <local name="script.base"/> - <basename file="@{script}" property="script.base"/> - <!-- crappy way to output, but we need it. make it nice later --> - <echoxml><exec script="${script.base}"><nested/></exec></echoxml> - <exec executable="cmd" osfamily="winnt" dir="${temp.cwd}" failonerror="${failonerror}" spawn="@{spawn}" taskname="${script.base}"> - <arg value="/c"/> - <arg value="""/> - <arg value="@{script}.bat"/> - <nested/> - <arg value="""/> - </exec> - - <exec executable="bash" osfamily="unix" dir="${temp.cwd}" failonerror="${failonerror}" spawn="@{spawn}" taskname="${script.base}"> - <arg value="@{script}"/> - <nested/> - </exec> - </sequential> - </macrodef> - - <!-- extracts PID from file --> - <macrodef name="extract-pid"> - <attribute name="file"/> - <attribute name="property"/> - <sequential> - <loadfile srcFile="@{file}" property="@{property}"> - <filterchain> - <striplinebreaks/> - </filterchain> - </loadfile> - </sequential> - </macrodef> - - <!-- applies transformations to src and stores in dst --> - <macrodef name="filter-property"> - <attribute name="src"/> - <attribute name="dest"/> - <element name="chain"/> - <sequential> - <loadresource property="@{dest}"> - <propertyresource name="@{src}"/> - <filterchain> - <tokenfilter> - <chain/> - </tokenfilter> - </filterchain> - </loadresource> - </sequential> - </macrodef> - - <!-- waits for elasticsearch to start --> - <macrodef name="waitfor-elasticsearch"> - <attribute name="port"/> - <attribute name="timeoutproperty"/> - <sequential> - <echo>Waiting for elasticsearch to become available on port @{port}...</echo> - <waitfor maxwait="30" maxwaitunit="second" - checkevery="500" checkeveryunit="millisecond" - timeoutproperty="@{timeoutproperty}"> - <http url="http://localhost:@{port}"/> - </waitfor> - </sequential> - </macrodef> - - <scriptdef name="isGreater" language="javascript"> - <attribute name="v1"/> - <attribute name="v2"/> - <![CDATA[ - - var i, l, d, s = false; - - a = attributes.get("v1").split('.'); - b = attributes.get("v2").split('.'); - l = Math.min(a.length, b.length); - - for (i=0; i<l; i++) { - d = parseInt(a[i], 10) - parseInt(b[i], 10); - if (d !== 0) { - project.setProperty("compare-result", d > 0); - s = true; - break; - } - } - - if(!s){ - d = a.length - b.length; - project.setProperty("compare-result", d >= 0); - } - - ]]> - </scriptdef> - - <!-- start elasticsearch and wait until its ready --> - <macrodef name="startup-elasticsearch"> - <attribute name="home" default="${project.build.directory}/integration-tests/run/elasticsearch-${elasticsearch5-version}"/> - <attribute name="spawn" default="true"/> - <attribute name="es.cluster.name" default="${integ.cluster.name}"/> - <attribute name="es.http.port" default="${integ.http.port}"/> - <attribute name="es.transport.tcp.port" default="${integ.transport.port}"/> - <attribute name="es.pidfile" default="${integ.pidfile}"/> - <element name="additional-args" optional="true"/> - <sequential> - <!-- make sure no elasticsearch instance is currently running and listening on the port we need --> - <fail message="This test expects port @{es.http.port} to be free but an elasticsearch instance is already running and listening on that port. - Maybe the last test run did not manage to shut down the node correctly? - You must kill it before tests can run."> - <condition> - <socket server="localhost" port="@{es.http.port}"></socket> - </condition> - </fail> - <!-- run bin/elasticsearch with args --> - <echo>Starting up external cluster...</echo> - <isGreater v1="${elasticsearch5-version}" v2="5.0.0" /> - - <echo if:true="${compare-result}">running Elasticsearch 5.0.0 or superior</echo> - <echo unless:true="${compare-result}">running Elasticsearch < 5.0.0</echo> - - <run-script script="@{home}/bin/elasticsearch" - spawn="@{spawn}"> - <nested> - <arg value="-Des.pidfile=@{es.pidfile}" unless:true="${compare-result}"/> - <arg value="-Des.cluster.name=@{es.cluster.name}" unless:true="${compare-result}"/> - <arg value="-Des.http.port=@{es.http.port}" unless:true="${compare-result}"/> - <arg value="-Des.transport.tcp.port=@{es.transport.tcp.port}" unless:true="${compare-result}"/> - <arg value="-Des.network.host=127.0.0.1" unless:true="${compare-result}"/> - <arg value="-Epidfile=@{es.pidfile}" if:true="${compare-result}"/> - <arg value="-Ecluster.name=@{es.cluster.name}" if:true="${compare-result}"/> - <arg value="-Ehttp.port=@{es.http.port}" if:true="${compare-result}"/> - <arg value="-Etransport.tcp.port=@{es.transport.tcp.port}" if:true="${compare-result}"/> - <arg value="-Enetwork.host=127.0.0.1" if:true="${compare-result}"/> - <additional-args/> - </nested> - </run-script> - - <!-- wait for startup --> - <local name="failed.to.start"/> - <waitfor-elasticsearch port="@{es.http.port}" - timeoutproperty="failed.to.start"/> - - <!-- best effort, print console log. useful if it fails especially --> - <local name="log.contents"/> - <loadfile srcFile="@{home}/logs/@{es.cluster.name}.log" - property="log.contents" - failonerror="false"/> - <echo message="${log.contents}" taskname="elasticsearch"/> - - <fail message="ES instance did not start" if="failed.to.start"/> - - <local name="integ.pid"/> - <extract-pid file="@{es.pidfile}" property="integ.pid"/> - <echo>External node started PID ${integ.pid}</echo> - </sequential> - </macrodef> - - <macrodef name="stop-node"> - <attribute name="es.pidfile" default="${integ.pidfile}"/> - <sequential> - <local name="integ.pid"/> - - <extract-pid file="@{es.pidfile}" property="integ.pid"/> - <echo>Shutting down external node PID ${integ.pid}</echo> - <!-- verify with jps that this actually is the correct pid. - See if we can find the line "pid org.elasticsearch.bootstrap.Elasticsearch" in the output of jps -l.--> - <local name="jps.pidline"/> - <local name="jps.executable"/> - <local name="environment"/> - <property environment="environment"/> - <property name="jps.executable" location="${environment.JAVA_HOME}/bin/jps"/> - <exec executable="${jps.executable}" failonerror="true"> - <arg value="-l"/> - <redirector outputproperty="jps.pidline"> - <outputfilterchain> - <linecontains> - <contains value="${integ.pid} org.elasticsearch.bootstrap.Elasticsearch"/> - </linecontains> - </outputfilterchain> - </redirector> - </exec> - <fail - message="pid file at @{es.pidfile} is ${integ.pid} but jps -l did not report any process with org.elasticsearch.bootstrap.Elasticsearch and this pid. - Did you run mvn clean? Maybe an old pid file is still lying around."> - <condition> - <equals arg1="${jps.pidline}" arg2=""/> - </condition> - </fail> - - <exec executable="taskkill" failonerror="true" osfamily="winnt"> - <arg value="/F"/> - <arg value="/PID"/> - <arg value="${integ.pid}"/> - </exec> - <exec executable="kill" failonerror="true" osfamily="unix"> - <arg value="-9"/> - <arg value="${integ.pid}"/> - </exec> - <delete file="@{es.pidfile}"/> - </sequential> - </macrodef> - - <target name="stop-external-cluster" if="integ.pidfile.exists"> - <stop-node/> - </target> - - <target name="setup-workspace" depends="stop-external-cluster"> - <sequential> - <delete dir="${project.build.directory}/integration-tests/run"/> - <unzip src="${project.build.directory}/integration-tests/binaries/elasticsearch-${elasticsearch5-version}.zip" - dest="${project.build.directory}/integration-tests/run"/> - </sequential> - </target> - - <target name="start-external-cluster" depends="setup-workspace"> - <startup-elasticsearch/> - </target> - - <!-- unzip integ test artifact, install plugin, then start ES --> - <target name="start-external-cluster-with-plugin" depends="setup-workspace"> - <install-plugin name="${project.artifactId}" file="${project.build.directory}/releases/${project.artifactId}-${project.version}.zip"/> - <startup-elasticsearch/> - </target> - - <!-- installs a plugin into elasticsearch --> - <macrodef name="install-plugin"> - <attribute name="home" default="${project.build.directory}/integration-tests/run/elasticsearch-${elasticsearch5-version}"/> - <attribute name="name"/> - <attribute name="file"/> - <sequential> - <local name="url"/> - <makeurl property="url" file="@{file}"/> - - <isGreater v1="${elasticsearch5-version}" v2="5.0.0" /> - <property name="commandline" value="@{home}/bin/plugin" unless:true="${compare-result}"/> - <property name="commandline" value="@{home}/bin/elasticsearch-plugin" if:true="${compare-result}"/> - - <!-- install plugin --> - <echo>Installing plugin @{name}...</echo> - <run-script script="${commandline}"> - <nested> - <arg value="install"/> - <arg value="${url}"/> - </nested> - </run-script> - - <fail message="did not find plugin installed as @{name}"> - <condition> - <not> - <resourceexists> - <file file="@{home}/plugins/@{name}"/> - </resourceexists> - </not> - </condition> - </fail> - </sequential> - </macrodef> -</project> \ No newline at end of file diff --git a/elasticsearch5/src/test/java/com/yahoo/ycsb/db/elasticsearch5/ElasticsearchClientTest.java b/elasticsearch5/src/test/java/com/yahoo/ycsb/db/elasticsearch5/ElasticsearchClientIT.java similarity index 56% rename from elasticsearch5/src/test/java/com/yahoo/ycsb/db/elasticsearch5/ElasticsearchClientTest.java rename to elasticsearch5/src/test/java/com/yahoo/ycsb/db/elasticsearch5/ElasticsearchClientIT.java index 174c42ceab43acaef18dc3e8e773c91801f98104..1e7ac3f04d305aea48525d85b275e8a436a68f81 100644 --- a/elasticsearch5/src/test/java/com/yahoo/ycsb/db/elasticsearch5/ElasticsearchClientTest.java +++ b/elasticsearch5/src/test/java/com/yahoo/ycsb/db/elasticsearch5/ElasticsearchClientIT.java @@ -22,12 +22,8 @@ import com.yahoo.ycsb.DBException; import com.yahoo.ycsb.Status; import com.yahoo.ycsb.StringByteIterator; import org.junit.After; -import org.junit.AfterClass; import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.ClassRule; import org.junit.Test; -import org.junit.rules.TemporaryFolder; import java.util.HashMap; import java.util.Properties; @@ -36,15 +32,11 @@ import java.util.Vector; import static org.junit.Assert.assertEquals; -public class ElasticsearchClientTest { +public class ElasticsearchClientIT { - @ClassRule public final static TemporaryFolder temp = new TemporaryFolder(); - private final static ElasticsearchClient instance = new ElasticsearchClient(); + private final ElasticsearchClient instance = new ElasticsearchClient(); private final static HashMap<String, ByteIterator> MOCK_DATA; private final static String MOCK_TABLE = "MOCK_TABLE"; - private final static String MOCK_KEY0 = "0"; - private final static String MOCK_KEY1 = "1"; - private final static String MOCK_KEY2 = "2"; static { MOCK_DATA = new HashMap<>(10); @@ -53,29 +45,21 @@ public class ElasticsearchClientTest { } } - @BeforeClass - public static void setUpClass() throws DBException { + @Before + public void setUp() throws DBException { final Properties props = new Properties(); - props.put("path.home", temp.getRoot().toString()); + props.put("es.newdb", "true"); + props.put("es.setting.cluster.name", "test"); instance.setProperties(props); instance.init(); - } - - @AfterClass - public static void tearDownClass() throws DBException { - instance.cleanup(); - } - - @Before - public void setUp() { - instance.insert(MOCK_TABLE, MOCK_KEY1, MOCK_DATA); - instance.insert(MOCK_TABLE, MOCK_KEY2, MOCK_DATA); + for (int i = 0; i < 16; i++) { + instance.insert(MOCK_TABLE, Integer.toString(i), MOCK_DATA); + } } @After - public void tearDown() { - instance.delete(MOCK_TABLE, MOCK_KEY1); - instance.delete(MOCK_TABLE, MOCK_KEY2); + public void tearDown() throws DBException { + instance.cleanup(); } /** @@ -83,7 +67,7 @@ public class ElasticsearchClientTest { */ @Test public void testInsert() { - Status result = instance.insert(MOCK_TABLE, MOCK_KEY0, MOCK_DATA); + final Status result = instance.insert(MOCK_TABLE, "0", MOCK_DATA); assertEquals(Status.OK, result); } @@ -92,7 +76,7 @@ public class ElasticsearchClientTest { */ @Test public void testDelete() { - Status result = instance.delete(MOCK_TABLE, MOCK_KEY1); + final Status result = instance.delete(MOCK_TABLE, "1"); assertEquals(Status.OK, result); } @@ -101,9 +85,9 @@ public class ElasticsearchClientTest { */ @Test public void testRead() { - Set<String> fields = MOCK_DATA.keySet(); - HashMap<String, ByteIterator> resultParam = new HashMap<>(10); - Status result = instance.read(MOCK_TABLE, MOCK_KEY1, fields, resultParam); + final Set<String> fields = MOCK_DATA.keySet(); + final HashMap<String, ByteIterator> resultParam = new HashMap<>(10); + final Status result = instance.read(MOCK_TABLE, "1", fields, resultParam); assertEquals(Status.OK, result); } @@ -112,21 +96,21 @@ public class ElasticsearchClientTest { */ @Test public void testUpdate() { - int i; - HashMap<String, ByteIterator> newValues = new HashMap<>(10); + final HashMap<String, ByteIterator> newValues = new HashMap<>(10); - for (i = 1; i <= 10; i++) { + for (int i = 1; i <= 10; i++) { newValues.put("field" + i, new StringByteIterator("newvalue" + i)); } - Status result = instance.update(MOCK_TABLE, MOCK_KEY1, newValues); - assertEquals(Status.OK, result); + final Status updateResult = instance.update(MOCK_TABLE, "1", newValues); + assertEquals(Status.OK, updateResult); - //validate that the values changed - HashMap<String, ByteIterator> resultParam = new HashMap<>(10); - instance.read(MOCK_TABLE, MOCK_KEY1, MOCK_DATA.keySet(), resultParam); + // validate that the values changed + final HashMap<String, ByteIterator> resultParam = new HashMap<>(10); + final Status readResult = instance.read(MOCK_TABLE, "1", MOCK_DATA.keySet(), resultParam); + assertEquals(Status.OK, readResult); - for (i = 1; i <= 10; i++) { + for (int i = 1; i <= 10; i++) { assertEquals("newvalue" + i, resultParam.get("field" + i).toString()); } @@ -137,10 +121,12 @@ public class ElasticsearchClientTest { */ @Test public void testScan() { - int recordcount = 10; - Set<String> fields = MOCK_DATA.keySet(); - Vector<HashMap<String, ByteIterator>> resultParam = new Vector<>(10); - Status result = instance.scan(MOCK_TABLE, MOCK_KEY1, recordcount, fields, resultParam); - assertEquals(Status.NOT_IMPLEMENTED, result); + final int recordcount = 10; + final Set<String> fields = MOCK_DATA.keySet(); + final Vector<HashMap<String, ByteIterator>> resultParam = new Vector<>(10); + final Status result = instance.scan(MOCK_TABLE, "1", recordcount, fields, resultParam); + assertEquals(Status.OK, result); + + assertEquals(10, resultParam.size()); } } diff --git a/elasticsearch5/src/test/java/com/yahoo/ycsb/db/elasticsearch5/ElasticsearchRestClientTestIT.java b/elasticsearch5/src/test/java/com/yahoo/ycsb/db/elasticsearch5/ElasticsearchRestClientIT.java similarity index 51% rename from elasticsearch5/src/test/java/com/yahoo/ycsb/db/elasticsearch5/ElasticsearchRestClientTestIT.java rename to elasticsearch5/src/test/java/com/yahoo/ycsb/db/elasticsearch5/ElasticsearchRestClientIT.java index fac85fd7ba26e1eb74a64d15a059fe9f31dfbed9..20aeb6a7379a0e01a65c5c687627c45e87424bf9 100644 --- a/elasticsearch5/src/test/java/com/yahoo/ycsb/db/elasticsearch5/ElasticsearchRestClientTestIT.java +++ b/elasticsearch5/src/test/java/com/yahoo/ycsb/db/elasticsearch5/ElasticsearchRestClientIT.java @@ -1,12 +1,12 @@ /** * Copyright (c) 2017 YCSB contributors. All rights reserved. - * <p> + * * 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 - * <p> + * * http://www.apache.org/licenses/LICENSE-2.0 - * <p> + * * 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 @@ -21,8 +21,11 @@ import com.yahoo.ycsb.ByteIterator; import com.yahoo.ycsb.DBException; import com.yahoo.ycsb.Status; import com.yahoo.ycsb.StringByteIterator; -import org.junit.*; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import java.io.IOException; import java.util.HashMap; import java.util.Properties; import java.util.Set; @@ -30,14 +33,11 @@ import java.util.Vector; import static org.junit.Assert.assertEquals; -public class ElasticsearchRestClientTestIT { - private final static String TEST_HOST = "localhost:9400"; - private final static ElasticsearchRestClient instance = new ElasticsearchRestClient(); +public class ElasticsearchRestClientIT { + + private final ElasticsearchRestClient instance = new ElasticsearchRestClient(); private final static HashMap<String, ByteIterator> MOCK_DATA; private final static String MOCK_TABLE = "MOCK_TABLE"; - private final static String MOCK_KEY0 = "0"; - private final static String MOCK_KEY1 = "1"; - private final static String MOCK_KEY2 = "2"; static { MOCK_DATA = new HashMap<>(10); @@ -46,79 +46,87 @@ public class ElasticsearchRestClientTestIT { } } - @BeforeClass - public static void setUpClass() throws DBException { + @Before + public void setUp() throws DBException, IOException { final Properties props = new Properties(); - props.setProperty("es.hosts.list", TEST_HOST); + props.put("es.newdb", "true"); instance.setProperties(props); instance.init(); - } - - @AfterClass - public static void tearDownClass() throws DBException { - instance.cleanup(); - } - - @Before - public void setUp() { - instance.insert(MOCK_TABLE, MOCK_KEY1, MOCK_DATA); - instance.insert(MOCK_TABLE, MOCK_KEY2, MOCK_DATA); + for (int i = 0; i < 16; i++) { + instance.insert(MOCK_TABLE, Integer.toString(i), MOCK_DATA); + } } @After - public void tearDown() { - instance.delete(MOCK_TABLE, MOCK_KEY1); - instance.delete(MOCK_TABLE, MOCK_KEY2); + public void tearDown() throws DBException { + instance.cleanup(); } + /** + * Test of insert method, of class ElasticsearchClient. + */ @Test public void testInsert() { - Status result = instance.insert(MOCK_TABLE, MOCK_KEY0, MOCK_DATA); + final Status result = instance.insert(MOCK_TABLE, "0", MOCK_DATA); assertEquals(Status.OK, result); } + /** + * Test of delete method, of class ElasticsearchClient. + */ @Test public void testDelete() { - Status result = instance.delete(MOCK_TABLE, MOCK_KEY1); + final Status result = instance.delete(MOCK_TABLE, "1"); assertEquals(Status.OK, result); } + /** + * Test of read method, of class ElasticsearchClient. + */ @Test public void testRead() { - Set<String> fields = MOCK_DATA.keySet(); - HashMap<String, ByteIterator> resultParam = new HashMap<>(10); - Status result = instance.read(MOCK_TABLE, MOCK_KEY1, fields, resultParam); + final Set<String> fields = MOCK_DATA.keySet(); + final HashMap<String, ByteIterator> resultParam = new HashMap<>(10); + final Status result = instance.read(MOCK_TABLE, "1", fields, resultParam); assertEquals(Status.OK, result); } + /** + * Test of update method, of class ElasticsearchClient. + */ @Test public void testUpdate() { - int i; - HashMap<String, ByteIterator> newValues = new HashMap<>(10); + final HashMap<String, ByteIterator> newValues = new HashMap<>(10); - for (i = 1; i <= 10; i++) { + for (int i = 1; i <= 10; i++) { newValues.put("field" + i, new StringByteIterator("newvalue" + i)); } - Status result = instance.update(MOCK_TABLE, MOCK_KEY1, newValues); - assertEquals(Status.OK, result); + final Status updateResult = instance.update(MOCK_TABLE, "1", newValues); + assertEquals(Status.OK, updateResult); - //validate that the values changed - HashMap<String, ByteIterator> resultParam = new HashMap<>(10); - instance.read(MOCK_TABLE, MOCK_KEY1, MOCK_DATA.keySet(), resultParam); + // validate that the values changed + final HashMap<String, ByteIterator> resultParam = new HashMap<>(10); + final Status readResult = instance.read(MOCK_TABLE, "1", MOCK_DATA.keySet(), resultParam); + assertEquals(Status.OK, readResult); - for (i = 1; i <= 10; i++) { + for (int i = 1; i <= 10; i++) { assertEquals("newvalue" + i, resultParam.get("field" + i).toString()); } } - + + /** + * Test of scan method, of class ElasticsearchClient. + */ @Test public void testScan() { - int recordcount = 10; - Set<String> fields = MOCK_DATA.keySet(); - Vector<HashMap<String, ByteIterator>> resultParam = new Vector<>(10); - Status result = instance.scan(MOCK_TABLE, MOCK_KEY1, recordcount, fields, resultParam); - assertEquals(Status.NOT_IMPLEMENTED, result); + final int recordcount = 10; + final Set<String> fields = MOCK_DATA.keySet(); + final Vector<HashMap<String, ByteIterator>> resultParam = new Vector<>(10); + final Status result = instance.scan(MOCK_TABLE, "1", recordcount, fields, resultParam); + assertEquals(Status.OK, result); + + assertEquals(10, resultParam.size()); } }