Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor configs and disable collections and configure tests #77

Merged
merged 10 commits into from
Mar 12, 2024
12 changes: 1 addition & 11 deletions src/integration/java/io/pinecone/helpers/IndexManager.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package io.pinecone.helpers;

import io.pinecone.clients.PineconeControlPlaneClient;
import io.pinecone.configs.PineconeClient;
import io.pinecone.configs.PineconeClientConfig;
import io.pinecone.configs.PineconeConnection;
import io.pinecone.configs.PineconeConnectionConfig;
import io.pinecone.exceptions.PineconeException;
import org.openapitools.client.model.*;
import org.slf4j.Logger;
Expand Down Expand Up @@ -117,14 +114,7 @@ public static PineconeConnection createNewIndexAndConnect(PineconeControlPlaneCl
// wait a bit more before we connect...
Thread.sleep(15000);

String host = controlPlaneClient.describeIndex(indexName).getHost();

PineconeClientConfig specificConfig = new PineconeClientConfig().withApiKey(System.getenv("PINECONE_API_KEY"));
PineconeClient dataPlaneClient = new PineconeClient(specificConfig);

return dataPlaneClient.connect(
new PineconeConnectionConfig()
.withConnectionUrl("https://" + host));
return new PineconeConnection(apiKey, indexName);
}

public static CollectionModel createCollection(PineconeControlPlaneClient controlPlaneClient, String collectionName, String indexName, boolean waitUntilReady) throws InterruptedException {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package io.pinecone.integration.controlPlane.pod;

import io.pinecone.clients.PineconeControlPlaneClient;
import io.pinecone.configs.PineconeClient;
import io.pinecone.configs.PineconeClientConfig;
import io.pinecone.configs.PineconeConnection;
import io.pinecone.configs.PineconeConnectionConfig;
import io.pinecone.configs.*;
import io.pinecone.helpers.RandomStringBuilder;
import io.pinecone.proto.*;
import org.junit.jupiter.api.AfterAll;
Expand Down Expand Up @@ -125,9 +122,8 @@ public void testIndexToCollectionHappyPath() throws InterruptedException {
assertEquals(indexDescription.getStatus().getReady(), true);

// Set up new index data plane connection
PineconeClient newIndexClient = new PineconeClient(new PineconeClientConfig().withApiKey(apiKey).withEnvironment(environment));
PineconeConnection newIndexDataPlaneClient = newIndexClient.connect(new PineconeConnectionConfig().withConnectionUrl("https://" + indexDescription.getHost()));
VectorServiceGrpc.VectorServiceBlockingStub newIndexBlockingStub = newIndexDataPlaneClient.getBlockingStub();
PineconeConnection connection = new PineconeConnection(apiKey, indexName);
VectorServiceGrpc.VectorServiceBlockingStub newIndexBlockingStub = connection.getBlockingStub();
DescribeIndexStatsResponse describeResponse = newIndexBlockingStub.describeIndexStats(DescribeIndexStatsRequest.newBuilder().build());

// Verify stats reflect the vectors in the collection
Expand Down Expand Up @@ -162,7 +158,7 @@ public void testIndexToCollectionHappyPath() throws InterruptedException {
}
}

newIndexDataPlaneClient.close();
connection.close();
}

@Test
Expand Down
46 changes: 0 additions & 46 deletions src/main/java/io/pinecone/configs/PineconeClient.java

This file was deleted.

119 changes: 0 additions & 119 deletions src/main/java/io/pinecone/configs/PineconeClientConfig.java

This file was deleted.

63 changes: 63 additions & 0 deletions src/main/java/io/pinecone/configs/PineconeConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package io.pinecone.configs;

import io.grpc.ManagedChannel;
import io.pinecone.exceptions.PineconeValidationException;

public class PineconeConfig {

private String apiKey;
private String host;
private String usageContext;
rohanshah18 marked this conversation as resolved.
Show resolved Hide resolved
private ManagedChannel customChannelBuilder;

public PineconeConfig(String apiKey) {
this.apiKey = apiKey;
}

public String getApiKey() {
return apiKey;
}

public void setApiKey(String apiKey) {
this.apiKey = apiKey;
}

public String getHost() {
return host;
}

public void setHost(String host) {
this.host = host;
}

public String getUsageContext() {
return usageContext;
}

public void setUsageContext(String usageContext) {
this.usageContext = usageContext;
}

public ManagedChannel getCustomChannelBuilder() {
return this.customChannelBuilder;
}

public void setCustomChannelBuilder(ManagedChannel customChannelBuilder) {
this.customChannelBuilder = customChannelBuilder;
}

public interface CustomChannelBuilder {
ManagedChannel buildChannel();
}
rohanshah18 marked this conversation as resolved.
Show resolved Hide resolved

void validate() {
if (apiKey == null)
throw new PineconeValidationException("Invalid PineconeConfig: missing apiKey");
}

public String getUserAgent() {
String userAgentLanguage = "lang=java; pineconeClientVersion = v0.8.0";
jhamon marked this conversation as resolved.
Show resolved Hide resolved
return (this.getUsageContext() != null) ?
userAgentLanguage + "; usageContext=" + this.getUsageContext() : userAgentLanguage;
rohanshah18 marked this conversation as resolved.
Show resolved Hide resolved
}
}
56 changes: 18 additions & 38 deletions src/main/java/io/pinecone/configs/PineconeConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ public class PineconeConnection implements AutoCloseable {
private static final int DEFAULT_MAX_MESSAGE_SIZE = 64 * 1000 * 1000;

private static final Logger logger = LoggerFactory.getLogger(PineconeConnection.class);
private final PineconeConnectionConfig connectionConfig;
private final PineconeClientConfig clientConfig;
private final PineconeConfig config;
final ManagedChannel channel;

/**
Expand All @@ -38,27 +37,28 @@ public class PineconeConnection implements AutoCloseable {
private VectorServiceGrpc.VectorServiceFutureStub futureStub;

public PineconeConnection(String apiKey, String indexName) {
this.clientConfig = new PineconeClientConfig().withApiKey(apiKey);
this.config = new PineconeConfig(apiKey);
rohanshah18 marked this conversation as resolved.
Show resolved Hide resolved
String host = getHost(apiKey, indexName);
this.connectionConfig = new PineconeConnectionConfig().withConnectionUrl(host);
channel = buildChannel(host);
initialize();
}

public PineconeConnection(PineconeClientConfig clientConfig, PineconeConnectionConfig connectionConfig) {
this.connectionConfig = connectionConfig;
this.clientConfig = clientConfig;
validateConfigsAndGetHostIfEmpty();

channel = connectionConfig.getCustomChannelBuilder() != null
? connectionConfig.getCustomChannelBuilder().apply(clientConfig, connectionConfig)
: buildChannel(connectionConfig.getConnectionUrl());
public PineconeConnection(PineconeConfig config, String indexName) {
this.config = config;
rohanshah18 marked this conversation as resolved.
Show resolved Hide resolved
if (config.getCustomChannelBuilder() != null) {
channel = config.getCustomChannelBuilder();
} else {
if (config.getHost() == null || config.getHost().isEmpty()) {
config.setHost(getHost(config.getApiKey(), indexName));
}
channel = buildChannel(config.getHost());
}
initialize();
}

private void initialize() {
channel.notifyWhenStateChanged(channel.getState(false), this::onConnectivityStateChanged);
Metadata metadata = assembleMetadata(clientConfig);
Metadata metadata = assembleMetadata(config);
blockingStub = generateBlockingStub(metadata);
futureStub = generateFutureStub(metadata);
logger.debug("created new PineconeConnection for channel: {}", channel);
Expand Down Expand Up @@ -127,19 +127,18 @@ public static ManagedChannel buildChannel(String host) {
return builder.build();
}

private static Metadata assembleMetadata(PineconeClientConfig clientConfig) {
private static Metadata assembleMetadata(PineconeConfig config) {
Metadata metadata = new Metadata();
metadata.put(Metadata.Key.of("api-key",
Metadata.ASCII_STRING_MARSHALLER), clientConfig.getApiKey());
metadata.put(Metadata.Key.of("User-Agent", Metadata.ASCII_STRING_MARSHALLER), clientConfig.getUserAgent());
Metadata.ASCII_STRING_MARSHALLER), config.getApiKey());
metadata.put(Metadata.Key.of("User-Agent", Metadata.ASCII_STRING_MARSHALLER), config.getUserAgent());
return metadata;
}

public static String formatEndpoint(String host) {
if(host != null && !host.isEmpty()) {
if (host != null && !host.isEmpty()) {
return host.replaceFirst("https?://", "");
}
else {
} else {
throw new PineconeValidationException("Index host cannot be null or empty");
}
}
Expand All @@ -148,23 +147,4 @@ private static String getHost(String apiKey, String indexName) {
PineconeControlPlaneClient controlPlaneClient = new PineconeControlPlaneClient(apiKey);
return controlPlaneClient.describeIndex(indexName).getHost();
}

void validateConfigsAndGetHostIfEmpty() throws PineconeValidationException {
rohanshah18 marked this conversation as resolved.
Show resolved Hide resolved
if (this.clientConfig == null) {
throw new PineconeValidationException("PineconeClientConfiguration may not be null");
}

if (this.connectionConfig == null) {
throw new PineconeValidationException("PineconeConnectionConfig may not be null");
}

this.clientConfig.validate();
this.connectionConfig.validate();

if(connectionConfig.getIndexName() != null && !connectionConfig.getIndexName().isEmpty()
&& (connectionConfig.getConnectionUrl() == null || connectionConfig.getConnectionUrl().isEmpty())) {
String host = getHost(clientConfig.getApiKey(), connectionConfig.getIndexName());
connectionConfig.withConnectionUrl(host);
}
}
}
Loading
Loading