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

Rename pattern from serverSetting. to querySetting for consistency #13

Merged
merged 2 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,12 @@ properties.put("clientSecret", "${clientSecret}");
### Connection settings

See this page on available [connection settings][connection settings].
These settings can be configured in properties by using the prefix `serverSetting.`
These settings can be configured in properties by using the prefix `querySetting.`

For example, to control locale set the following property:

```java
properties.put("serverSetting.lc_time", "en_US");
properties.put("querySetting.lc_time", "en_US");
```

---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,33 @@
package com.salesforce.datacloud.jdbc.core;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.stream.Collectors;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.val;

@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class HyperConnectionSettings {
private static final String HYPER_SETTING = "serverSetting.";
public class ConnectionQuerySettings {
private static final String HYPER_SETTING = "querySetting.";
private static final String HYPER_LEGACY_SETTING = "serverSetting.";
private final Map<String, String> settings;

public static HyperConnectionSettings of(Properties properties) {
val result = properties.entrySet().stream()
.filter(e -> e.getKey().toString().startsWith(HYPER_SETTING))
.collect(
Collectors.toMap(e -> e.getKey().toString().substring(HYPER_SETTING.length()), e -> e.getValue()
.toString()));
return new HyperConnectionSettings(result);
public static ConnectionQuerySettings of(Properties properties) {
Map<String, String> settings = new HashMap<>();
for (val e : properties.entrySet()) {
if (e.getKey().toString().startsWith(HYPER_SETTING)) {
settings.put(
e.getKey().toString().substring(HYPER_SETTING.length()),
e.getValue().toString());
} else if (e.getKey().toString().startsWith(HYPER_LEGACY_SETTING)) {
settings.put(
e.getKey().toString().substring(HYPER_LEGACY_SETTING.length()),
e.getValue().toString());
}
}
return new ConnectionQuerySettings(settings);
}

public Map<String, String> getSettings() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public static HyperGrpcClientExecutor of(@NonNull ManagedChannelBuilder<?> build
throws SQLException {
val client = HyperGrpcClientExecutor.builder();

val settings = HyperConnectionSettings.of(properties).getSettings();
val settings = ConnectionQuerySettings.of(properties).getSettings();
if (!settings.isEmpty()) {
client.settingsQueryParams(
QueryParam.newBuilder().putAllSettings(settings).build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,35 @@

import com.google.common.collect.Maps;
import com.salesforce.datacloud.jdbc.hyper.HyperTestBase;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import lombok.SneakyThrows;
import lombok.val;
import org.junit.jupiter.api.Test;

public class ConnectionSettingsTest extends HyperTestBase {
public class ConnectionQuerySettingsTest extends HyperTestBase {
@Test
@SneakyThrows
public void testHyperRespectsConnectionSetting() {
public void testLegacyQuerySetting() {
val settings = Maps.immutableEntry("serverSetting.date_style", "YMD");
val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

assertWithStatement(
statement -> {
val result = statement.executeQuery("SELECT CURRENT_DATE");
val result = statement.executeQuery("SHOW date_style");
result.next();
assertThat(result.getString(1)).isEqualTo("ISO, YMD");
},
settings);
}

val expected = LocalDate.parse(result.getDate(1).toString(), formatter);
val actual = result.getDate(1);
@Test
@SneakyThrows
public void testQuerySetting() {
val settings = Maps.immutableEntry("querySetting.date_style", "YMD");

assertThat(actual.toString()).isEqualTo(expected.toString());
assertWithStatement(
statement -> {
val result = statement.executeQuery("SHOW date_style");
result.next();
assertThat(result.getString(1)).isEqualTo("ISO, YMD");
},
settings);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,16 @@
import org.junit.jupiter.api.Test;

class HyperConnectionSettingsTest extends HyperGrpcTestBase {
private static final String HYPER_SETTING = "serverSetting.";
private static final String HYPER_SETTING = "querySetting.";

@Test
void testGetSettingWithCorrectPrefix() {
Map<String, String> expected = ImmutableMap.of("lc_time", "en_US");
Properties properties = new Properties();
properties.setProperty(HYPER_SETTING + "lc_time", "en_US");
properties.setProperty("username", "alice");
HyperConnectionSettings hyperConnectionSettings = HyperConnectionSettings.of(properties);
assertThat(hyperConnectionSettings.getSettings()).containsExactlyInAnyOrderEntriesOf(expected);
ConnectionQuerySettings connectionQuerySettings = ConnectionQuerySettings.of(properties);
assertThat(connectionQuerySettings.getSettings()).containsExactlyInAnyOrderEntriesOf(expected);
}

@Test
Expand All @@ -50,16 +50,16 @@ void testGetSettingReturnEmptyResultSet() {
Properties properties = new Properties();
properties.setProperty("c_time", "en_US");
properties.setProperty("username", "alice");
HyperConnectionSettings hyperConnectionSettings = HyperConnectionSettings.of(properties);
assertThat(hyperConnectionSettings.getSettings()).containsExactlyInAnyOrderEntriesOf(expected);
ConnectionQuerySettings connectionQuerySettings = ConnectionQuerySettings.of(properties);
assertThat(connectionQuerySettings.getSettings()).containsExactlyInAnyOrderEntriesOf(expected);
}

@Test
void testGetSettingWithEmptyProperties() {
Map<String, String> expected = ImmutableMap.of();
Properties properties = new Properties();
HyperConnectionSettings hyperConnectionSettings = HyperConnectionSettings.of(properties);
assertThat(hyperConnectionSettings.getSettings()).containsExactlyInAnyOrderEntriesOf(expected);
ConnectionQuerySettings connectionQuerySettings = ConnectionQuerySettings.of(properties);
assertThat(connectionQuerySettings.getSettings()).containsExactlyInAnyOrderEntriesOf(expected);
}

@SneakyThrows
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* Copyright (c) 2024, Salesforce, Inc.
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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 implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.salesforce.datacloud.jdbc.hyper;

import static java.util.Objects.requireNonNull;

import java.io.*;
import java.nio.file.Paths;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.regex.Pattern;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.junit.jupiter.api.Assertions;

@Slf4j
public class HyperServerProcess {
private static final Pattern PORT_PATTERN = Pattern.compile(".*gRPC listening on 127.0.0.1:([0-9]+).*");

private final Process hyperProcess;
private final ExecutorService hyperMonitors;
private Integer port;

@SneakyThrows
public HyperServerProcess() {
log.info("starting hyperd, this might take a few seconds");

val executable = new File("./target/hyper/hyperd");
val properties = Paths.get(requireNonNull(HyperTestBase.class.getResource("/hyper.yaml"))
.toURI())
.toFile();

if (!executable.exists()) {
Assertions.fail("hyperd executable couldn't be found, have you run mvn process-test-resources? expected="
+ executable.getAbsolutePath());
}

hyperProcess = new ProcessBuilder()
.command(executable.getAbsolutePath(), "--config", properties.getAbsolutePath(), "--no-password", "run")
.start();

// Wait until process is listening and extract port on which it is listening
val latch = new CountDownLatch(1);
hyperMonitors = Executors.newFixedThreadPool(2);
hyperMonitors.execute(() -> logStream(hyperProcess.getErrorStream(), log::error));
hyperMonitors.execute(() -> logStream(hyperProcess.getInputStream(), line -> {
log.info(line);
val matcher = PORT_PATTERN.matcher(line);
if (matcher.matches()) {
port = Integer.valueOf(matcher.group(1));
latch.countDown();
}
}));

if (!latch.await(30, TimeUnit.SECONDS)) {
Assertions.fail("failed to start instance of hyper within 30 seconds");
}
}

@SneakyThrows
void shutdown() throws InterruptedException {
if (hyperProcess != null && hyperProcess.isAlive()) {
log.info("destroy hyper process");
hyperProcess.destroy();
hyperProcess.waitFor();
}

log.info("shutdown hyper monitors");
hyperMonitors.shutdown();
}

int getPort() {
return port;
}

boolean isHealthy() {
return hyperProcess != null && hyperProcess.isAlive();
}

private static void logStream(InputStream inputStream, Consumer<String> consumer) {
try (val reader = new BufferedReader(new BufferedReader(new InputStreamReader(inputStream)))) {
String line;
while ((line = reader.readLine()) != null) {
consumer.accept("hyperd - " + line);
}
} catch (IOException e) {
log.warn("Caught exception while consuming log stream, it probably closed", e);
} catch (Exception e) {
log.error("Caught unexpected exception", e);
}
}
}
Loading