Skip to content

Commit

Permalink
feat: setup momento-local (wip)
Browse files Browse the repository at this point in the history
  • Loading branch information
rishtigupta committed Oct 2, 2024
1 parent ff54fea commit 5831322
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 2 deletions.
7 changes: 7 additions & 0 deletions momento-sdk/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,10 @@ tasks.named("analyzeIntTestClassesDependencies").configure {
tasks.named("analyzeTestClassesDependencies").configure {
enabled = false
}

tasks.register<JavaExec>("run") {
group = "application"
description = "Runs the Program class"
mainClass.set("momento.sdk.Program") // Fully qualified class name
classpath = sourceSets["main"].runtimeClasspath
}
44 changes: 44 additions & 0 deletions momento-sdk/src/intTest/java/momento/sdk/CacheGetBatchTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package momento.sdk;

import java.time.Duration;
import momento.sdk.auth.CredentialProvider;
import momento.sdk.config.Configurations;
import momento.sdk.responses.cache.control.CacheCreateResponse;
import org.junit.jupiter.api.Test;

final class CacheGetBatchTest {
@Test
public void getBatchSetBatchHappyPath() {
CredentialProvider credentialProvider =
CredentialProvider.withMomentoLocal(System.getenv("MOMENTO_API_KEY"));
CacheClient cacheClient =
new CacheClientBuilder(
credentialProvider, Configurations.Laptop.latest(), Duration.ofMinutes(1))
.build();

CacheCreateResponse resp = cacheClient.createCache("cache").join();
System.out.println(resp);
// SetResponse setCacheResponse = cacheClient.set("cache", "key1", "val1",
// Duration.ofMinutes(1)).join();
// System.out.println(setCacheResponse);

// final Map<String, String> items = new HashMap<>();
// items.put("key1", "val1");
// items.put("key2", "val2");
// items.put("key3", "val3");
// final SetBatchResponse setBatchResponse =
// cacheClient.setBatch("cache", items, Duration.ofMinutes(1)).join();
// assertThat(setBatchResponse).isInstanceOf(SetBatchResponse.Success.class);
// for (SetResponse setResponse :
// ((SetBatchResponse.Success) setBatchResponse).results().values()) {
// assertThat(setResponse).isInstanceOf(SetResponse.Success.class);
// }
//
// final GetBatchResponse getBatchResponse = cacheClient.getBatch("cache",
// items.keySet()).join();
//
// assertThat(getBatchResponse).isInstanceOf(GetBatchResponse.Success.class);
// assertThat(((GetBatchResponse.Success) getBatchResponse).valueMapStringString())
// .containsExactlyEntriesOf(items);
}
}
26 changes: 26 additions & 0 deletions momento-sdk/src/main/java/momento/sdk/Program.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package momento.sdk;

import java.time.Duration;
import momento.sdk.auth.CredentialProvider;
import momento.sdk.config.Configurations;
import momento.sdk.responses.cache.control.CacheCreateResponse;

public class Program {
public static void main(String[] args) {
CredentialProvider credentialProvider =
CredentialProvider.withMomentoLocal(System.getenv("MOMENTO_API_KEY"));
CacheClient cacheClient =
new CacheClientBuilder(
credentialProvider, Configurations.Laptop.latest(), Duration.ofMinutes(1))
.build();

CacheCreateResponse createResponse = cacheClient.createCache("cache").join();
if (createResponse instanceof CacheCreateResponse.Success) {
System.out.println("Cache created successfully");
} else {
System.out.println("Failed to create cache" + createResponse.toString());
CacheCreateResponse.Error error = (CacheCreateResponse.Error) createResponse;
System.out.println("Failed to create cache: " + error.getErrorCode());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ final class ScsControlGrpcStubsManager implements AutoCloseable {
private static ManagedChannel setupConnection(
CredentialProvider credentialProvider, Configuration configuration) {
final NettyChannelBuilder channelBuilder =
NettyChannelBuilder.forAddress(credentialProvider.getControlEndpoint(), 443);
NettyChannelBuilder.forAddress(credentialProvider.getControlEndpoint(), 8080)
.usePlaintext();

// Override grpc config to disable keepalive for control clients
final GrpcConfiguration controlConfig =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ private static void eagerlyConnect(
private ManagedChannel setupChannel(
CredentialProvider credentialProvider, Configuration configuration) {
final NettyChannelBuilder channelBuilder =
NettyChannelBuilder.forAddress(credentialProvider.getCacheEndpoint(), 443);
NettyChannelBuilder.forAddress(credentialProvider.getCacheEndpoint(), 8080).usePlaintext();

// set additional channel options (message size, keepalive, auth, etc)
GrpcChannelOptions.applyGrpcConfigurationToChannelBuilder(
Expand Down
10 changes: 10 additions & 0 deletions momento-sdk/src/main/java/momento/sdk/auth/CredentialProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ public static CredentialProvider fromEnvVar(@Nonnull String envVar) {
return new EnvVarCredentialProvider(envVar);
}

public static CredentialProvider withMomentoLocal(@Nonnull String authToken) {
String momentoLocalOverride = "127.0.0.1";
return new StringCredentialProvider(
authToken,
momentoLocalOverride,
momentoLocalOverride,
momentoLocalOverride,
momentoLocalOverride);
}

/**
* Gets the token used to authenticate to Momento.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,20 @@ public void testCredentialProviderV1MissingApiKey() {
.isThrownBy(() -> new StringCredentialProvider(TEST_V1_MISSING_API_KEY))
.withMessageContaining("parse auth token");
}

// @Test
// public void testCredentialProviderWithMomentoLocal() {
// String apiKey =
// "eyJlbmRwb2ludCI6ImNlbGwtYWxwaGEtZGV2LnByZXByb2QuYS5tb21lbnRvaHEuY29tIiwiYXBpX2tleSI6ImV5SmhiR2NpT2lKSVV6STFOaUo5LmV5SnpkV0lpT2lKeWFYTm9kR2xBYlc5dFpXNTBiMmh4TG1OdmJTSXNJblpsY2lJNk1Td2ljQ0k2SWtOQlFUMGlMQ0psZUhBaU9qRTNNekEwTURZMU9ESjkuTHJmNTNMNEg2bDlVY05XNGJMLXJtRm96elluQm1RUjFSaUUxZzdGZktiWSJ9";
// assertThat(CredentialProvider.withMomentoLocal(apiKey))
// .satisfies(
// provider -> {
//// assertThat(provider.getAuthToken()).isEqualTo(apiKey);
//
// assertThat(provider.getControlEndpoint()).isEqualTo("127.0.0.1:8080");
// assertThat(provider.getCacheEndpoint()).isEqualTo("127.0.0.1:8080");
//
// assertThat(provider.getStorageEndpoint()).isEqualTo("127.0.0.1:8080");
// });
// }
}

0 comments on commit 5831322

Please sign in to comment.