Skip to content

Commit 5426722

Browse files
authored
Refactor configure index test (#50)
## Problem After recent changes in integration test to use the existing index if it exists, the configure index test was required to be updated to consume less resources. ## Solution 1. Refactored checkIfIndexExists code: a. To support both data and control plane integration tests b. Check if any index matches the criteria compared to old approach of checking the first index of the list and otherwise create a new index. 2. For configure test: a. Updated the beforeAll logic to check if an index exist b. Deleted the afterEach block to delete each index and save them for future test ## Type of Change - [ ] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) - [ ] This change requires a documentation update - [ ] Infrastructure change (CI configs, etc) - [ ] Non-code change (docs, etc) - [X] None of the above: (explain here) Added Integration tests ## Test Plan Ran integration tests locally.
1 parent f952fad commit 5426722

File tree

5 files changed

+65
-67
lines changed

5 files changed

+65
-67
lines changed

src/integration/java/io/pinecone/PineconeIndexOperationsClientIntegrationTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public void setUp() {
2525
}
2626

2727
@Test
28-
public void createAndDelete() throws IOException {
28+
public void createAndDelete() throws IOException, InterruptedException {
2929
String indexName = RandomStringBuilder.build("index-name", 8);
3030

3131
// Create an index
@@ -48,5 +48,6 @@ public void createAndDelete() throws IOException {
4848

4949
// Cleanup
5050
pinecone.deleteIndex(indexName);
51+
Thread.sleep(3500);
5152
}
5253
}

src/integration/java/io/pinecone/helpers/IndexManager.java

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,15 @@
88
import java.util.List;
99

1010
public class IndexManager {
11-
public PineconeConnection createIndexIfNotExists(int dimension) throws IOException, InterruptedException {
12-
boolean createNewIndex = false;
13-
String indexName = "";
11+
public static PineconeConnection createIndexIfNotExistsDataPlane(int dimension) throws IOException, InterruptedException {
1412
PineconeClientConfig config = new PineconeClientConfig()
1513
.withApiKey(System.getenv("PINECONE_API_KEY"))
1614
.withEnvironment(System.getenv("PINECONE_ENVIRONMENT"));
1715
PineconeIndexOperationClient controlPlaneClient = new PineconeIndexOperationClient(config);
1816
List<String> indexList = controlPlaneClient.listIndexes();
1917

20-
if (!indexList.isEmpty()) {
21-
indexName = indexList.get(0);
22-
IndexMeta indexMeta = isIndexReady(indexName, controlPlaneClient);
23-
if (indexMeta.getDatabase().getDimension() != dimension) {
24-
createNewIndex = true;
25-
}
26-
}
27-
28-
if (createNewIndex) {
29-
indexName = RandomStringBuilder.build("index-name", 8);
30-
CreateIndexRequest createIndexRequest = new CreateIndexRequest()
31-
.withIndexName(indexName)
32-
.withDimension(dimension)
33-
.withMetric("euclidean");
34-
controlPlaneClient.createIndex(createIndexRequest);
35-
}
18+
String indexName = findIndexWithDimensionAndPodType(indexList, dimension, controlPlaneClient);
19+
if(indexName.isEmpty()) indexName = createNewIndex(dimension, controlPlaneClient);
3620

3721
PineconeClient dataPlaneClient = new PineconeClient(config);
3822
IndexMeta indexMeta = controlPlaneClient.describeIndex(indexName);
@@ -43,6 +27,40 @@ public PineconeConnection createIndexIfNotExists(int dimension) throws IOExcepti
4327
.withConnectionUrl("https://" + host));
4428
}
4529

30+
public static String createIndexIfNotExistsControlPlane(PineconeClientConfig config, int dimension) throws IOException, InterruptedException {
31+
PineconeIndexOperationClient controlPlaneClient = new PineconeIndexOperationClient(config);
32+
List<String> indexList = controlPlaneClient.listIndexes();
33+
String indexName = findIndexWithDimensionAndPodType(indexList, dimension, controlPlaneClient);
34+
35+
return (indexName.isEmpty()) ? createNewIndex(dimension, controlPlaneClient) : indexName;
36+
}
37+
38+
private static String findIndexWithDimensionAndPodType(List<String> indexList, int dimension, PineconeIndexOperationClient controlPlaneClient)
39+
throws IOException, InterruptedException {
40+
int i = 0;
41+
while (i < indexList.size()) {
42+
IndexMeta indexMeta = isIndexReady(indexList.get(i), controlPlaneClient);
43+
if (indexMeta.getDatabase().getDimension() == dimension
44+
&& indexMeta.getDatabase().getPodType().equals("p1.x1")) {
45+
return indexList.get(i);
46+
}
47+
i++;
48+
}
49+
return "";
50+
}
51+
52+
private static String createNewIndex(int dimension, PineconeIndexOperationClient controlPlaneClient) throws IOException {
53+
String indexName = RandomStringBuilder.build("index-name", 8);
54+
CreateIndexRequest createIndexRequest = new CreateIndexRequest()
55+
.withIndexName(indexName)
56+
.withDimension(dimension)
57+
.withMetric("euclidean")
58+
.withPodType("p1.x1");
59+
controlPlaneClient.createIndex(createIndexRequest);
60+
61+
return indexName;
62+
}
63+
4664
public static IndexMeta isIndexReady(String indexName, PineconeIndexOperationClient indexOperationClient)
4765
throws IOException, InterruptedException {
4866
IndexMeta indexMeta;
@@ -51,9 +69,9 @@ public static IndexMeta isIndexReady(String indexName, PineconeIndexOperationCli
5169
if (indexMeta.getStatus().getState().equalsIgnoreCase("ready")) {
5270
break;
5371
}
54-
5572
Thread.sleep(1000);
5673
}
74+
5775
return indexMeta;
5876
}
5977
}

src/integration/java/io/pinecone/integration/ConfigureIndexTest.java renamed to src/integration/java/io/pinecone/integration/controlPlane/ConfigureIndexTest.java

Lines changed: 21 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,39 @@
1-
package io.pinecone.integration;
1+
package io.pinecone.integration.controlPlane;
22

33
import io.pinecone.PineconeClientConfig;
44
import io.pinecone.PineconeClientLiveIntegTest;
55
import io.pinecone.PineconeIndexOperationClient;
66
import io.pinecone.exceptions.PineconeBadRequestException;
77
import io.pinecone.exceptions.PineconeNotFoundException;
8-
import io.pinecone.helpers.RandomStringBuilder;
98
import io.pinecone.model.ConfigureIndexRequest;
10-
import io.pinecone.model.CreateIndexRequest;
119
import io.pinecone.model.IndexMeta;
1210
import org.junit.jupiter.api.*;
1311
import org.slf4j.Logger;
1412
import org.slf4j.LoggerFactory;
1513

1614
import java.io.IOException;
1715

16+
import static io.pinecone.helpers.IndexManager.createIndexIfNotExistsControlPlane;
1817
import static io.pinecone.helpers.IndexManager.isIndexReady;
1918
import static org.junit.jupiter.api.Assertions.assertEquals;
2019

2120
public class ConfigureIndexTest {
21+
private static PineconeClientConfig config;
2222
private PineconeIndexOperationClient indexOperationClient;
2323
private String indexName;
2424
private static final Logger logger = LoggerFactory.getLogger(PineconeClientLiveIntegTest.class);
2525

26-
@BeforeEach
27-
public void setUp() throws IOException {
28-
indexName = RandomStringBuilder.build("index-name", 8);
29-
PineconeClientConfig config = new PineconeClientConfig()
26+
@BeforeAll
27+
public static void defineConfig() {
28+
config = new PineconeClientConfig()
3029
.withApiKey(System.getenv("PINECONE_API_KEY"))
3130
.withEnvironment(System.getenv("PINECONE_ENVIRONMENT"));
32-
indexOperationClient = new PineconeIndexOperationClient(config);
33-
34-
// Create an index
35-
CreateIndexRequest request = new CreateIndexRequest()
36-
.withIndexName(indexName)
37-
.withDimension(5)
38-
.withMetric("euclidean");
39-
indexOperationClient.createIndex(request);
4031
}
4132

42-
@AfterEach
43-
public void cleanUp() throws IOException, InterruptedException {
44-
indexOperationClient.deleteIndex(indexName);
45-
Thread.sleep(3500);
33+
@BeforeEach
34+
public void setUp() throws IOException, InterruptedException {
35+
indexName = createIndexIfNotExistsControlPlane(config, 5);
36+
indexOperationClient = new PineconeIndexOperationClient(config);
4637
}
4738

4839
@Test
@@ -76,28 +67,7 @@ public void configureIndexExceedingQuota() {
7667
}
7768

7869
@Test
79-
public void scaleUp() {
80-
try{
81-
// Verify the starting state
82-
IndexMeta indexMeta = isIndexReady(indexName, indexOperationClient);
83-
assertEquals(1, indexMeta.getDatabase().getReplicas());
84-
85-
// Configure the index
86-
ConfigureIndexRequest configureIndexRequest = new ConfigureIndexRequest()
87-
.withReplicas(2);
88-
isIndexReady(indexName, indexOperationClient);
89-
indexOperationClient.configureIndex(indexName, configureIndexRequest);
90-
91-
// Verify replicas were scaled up
92-
indexMeta = indexOperationClient.describeIndex(indexName);
93-
assertEquals(2, indexMeta.getDatabase().getReplicas());
94-
} catch (Exception exception) {
95-
logger.error(exception.toString());
96-
}
97-
}
98-
99-
@Test
100-
public void scaleDown() {
70+
public void scaleUpAndDown() {
10171
try {
10272
// Verify the starting state
10373
IndexMeta indexMeta = isIndexReady(indexName, indexOperationClient);
@@ -163,13 +133,17 @@ public void sizeIncrease() {
163133
// Get the index description to verify the new pod type
164134
indexMeta = indexOperationClient.describeIndex(indexName);
165135
assertEquals("p1.x2", indexMeta.getDatabase().getPodType());
136+
137+
// Delete this index since it'll be unused for future tests
138+
indexOperationClient.deleteIndex(indexName);
139+
Thread.sleep(3500);
166140
} catch (Exception exception) {
167141
logger.error(exception.getLocalizedMessage());
168142
}
169143
}
170144

171145
@Test
172-
public void sizeDown() {
146+
public void sizeDown() throws IOException, InterruptedException {
173147
try {
174148
// Verify the starting state
175149
IndexMeta indexMeta = isIndexReady(indexName, indexOperationClient);
@@ -189,9 +163,14 @@ public void sizeDown() {
189163
configureIndexRequest = new ConfigureIndexRequest()
190164
.withPodType("p1.x1");
191165
indexOperationClient.configureIndex(indexName, configureIndexRequest);
166+
Thread.sleep(3500);
192167
} catch (Exception exception) {
193168
assertEquals(exception.getClass(), PineconeBadRequestException.class);
194169
assertEquals(exception.getMessage(), "scaling down pod type is not supported");
170+
} finally {
171+
// Delete this index since it'll be unused for other tests
172+
indexOperationClient.deleteIndex(indexName);
173+
Thread.sleep(3500);
195174
}
196175
}
197176
}

src/integration/java/io/pinecone/integration/dataplane/UpsertAndDeleteTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import com.google.protobuf.Struct;
44
import com.google.protobuf.Value;
55
import io.pinecone.PineconeConnection;
6-
import io.pinecone.helpers.IndexManager;
76
import io.pinecone.helpers.RandomStringBuilder;
87
import io.pinecone.proto.*;
98
import org.junit.jupiter.api.BeforeAll;
@@ -15,6 +14,7 @@
1514
import java.util.concurrent.ExecutionException;
1615

1716
import static io.pinecone.helpers.BuildUpsertRequest.*;
17+
import static io.pinecone.helpers.IndexManager.createIndexIfNotExistsDataPlane;
1818
import static org.junit.jupiter.api.Assertions.assertEquals;
1919
import static org.junit.jupiter.api.Assertions.assertThrows;
2020

@@ -25,7 +25,7 @@ public class UpsertAndDeleteTest {
2525

2626
@BeforeAll
2727
public static void setUp() throws IOException, InterruptedException {
28-
PineconeConnection connection = new IndexManager().createIndexIfNotExists(dimension);
28+
PineconeConnection connection = createIndexIfNotExistsDataPlane(dimension);
2929
blockingStub = connection.getBlockingStub();
3030
futureStub = connection.getFutureStub();
3131
}

src/integration/java/io/pinecone/integration/dataplane/UpsertAndDescribeIndexStatsTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package io.pinecone.integration.dataplane;
22

33
import io.pinecone.*;
4-
import io.pinecone.helpers.IndexManager;
54
import io.pinecone.proto.*;
65
import org.junit.jupiter.api.*;
76

87
import static io.pinecone.helpers.BuildUpsertRequest.*;
8+
import static io.pinecone.helpers.IndexManager.createIndexIfNotExistsDataPlane;
99
import static org.junit.jupiter.api.Assertions.assertEquals;
1010

1111
import java.io.IOException;
@@ -18,7 +18,7 @@ public class UpsertAndDescribeIndexStatsTest {
1818

1919
@BeforeAll
2020
public static void setUp() throws IOException, InterruptedException {
21-
PineconeConnection connection = new IndexManager().createIndexIfNotExists(dimension);
21+
PineconeConnection connection = createIndexIfNotExistsDataPlane(dimension);
2222
blockingStub = connection.getBlockingStub();
2323
futureStub = connection.getFutureStub();
2424
}

0 commit comments

Comments
 (0)