Skip to content

Commit 2bc72a3

Browse files
authored
Merge pull request #340 from levzem/perf-fix
CC-6187: Cache already existing index in JestElasticsearchClient
2 parents e9eec5b + 2813897 commit 2bc72a3

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/main/java/io/confluent/connect/elasticsearch/jest/JestElasticsearchClient.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,11 @@ private boolean indexExists(String index) {
195195
Action action = new IndicesExists.Builder(index).build();
196196
try {
197197
JestResult result = client.execute(action);
198-
return result.isSucceeded();
198+
boolean exists = result.isSucceeded();
199+
if (exists) {
200+
indexCache.add(index);
201+
}
202+
return exists;
199203
} catch (IOException e) {
200204
throw new ConnectException(e);
201205
}

src/test/java/io/confluent/connect/elasticsearch/jest/JestElasticsearchClientTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import io.searchbox.indices.IndicesExists;
3737
import io.searchbox.indices.mapping.GetMapping;
3838
import io.searchbox.indices.mapping.PutMapping;
39+
import java.util.Collections;
3940
import org.apache.kafka.connect.data.Schema;
4041
import org.apache.kafka.connect.errors.ConnectException;
4142
import org.junit.Before;
@@ -56,6 +57,7 @@
5657
import static org.mockito.Mockito.inOrder;
5758
import static org.mockito.Mockito.mock;
5859
import static org.mockito.Mockito.verify;
60+
import static org.mockito.Mockito.verifyNoMoreInteractions;
5961
import static org.mockito.Mockito.when;
6062

6163
public class JestElasticsearchClientTest {
@@ -89,6 +91,23 @@ public void getsVersion() {
8991
assertThat(client.getVersion(), is(equalTo(ElasticsearchClient.Version.ES_V1)));
9092
}
9193

94+
@Test
95+
public void attemptToCreateExistingIndex() throws Exception {
96+
JestElasticsearchClient client = new JestElasticsearchClient(jestClient);
97+
JestResult success = new JestResult(new Gson());
98+
success.setSucceeded(true);
99+
IndicesExists indicesExists = new IndicesExists.Builder(INDEX).build();
100+
when(jestClient.execute(indicesExists)).thenReturn(success);
101+
when(jestClient.execute(argThat(isCreateIndexForTestIndex()))).thenReturn(success);
102+
103+
client.createIndices(Collections.singleton(INDEX));
104+
InOrder inOrder = inOrder(jestClient);
105+
inOrder.verify(jestClient).execute(info);
106+
inOrder.verify(jestClient).execute(indicesExists);
107+
108+
verifyNoMoreInteractions(jestClient);
109+
}
110+
92111
@Test
93112
public void createsIndices() throws Exception {
94113
JestElasticsearchClient client = new JestElasticsearchClient(jestClient);

0 commit comments

Comments
 (0)