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

[client-v2] Added setting log_comment #1864

Merged
merged 5 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -224,4 +224,23 @@ public InsertSettings setDBRoles(Collection<String> dbRoles) {
public Collection<String> getDBRoles() {
return (Collection<String>) rawSettings.get(ClientSettings.SESSION_DB_ROLES);
}

/**
* Sets the comment that will be added to the query log record associated with the query.
* @param logComment - comment to be added to the log
* @return same instance of the builder
*/
public InsertSettings logComment(String logComment) {
this.logComment = logComment;
if (logComment != null && !logComment.isEmpty()) {
rawSettings.put(ClientSettings.SERVER_SETTING_PREFIX + "log_comment", logComment);
}
return this;
}

private String logComment = null;

public String getLogComment() {
return logComment;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,29 +54,22 @@

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLException;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ConnectException;
import java.net.InetSocketAddress;
import java.net.NoRouteToHostException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import java.util.Collection;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,4 +240,23 @@ public QuerySettings setDBRoles(Collection<String> dbRoles) {
public Collection<String> getDBRoles() {
return (Collection<String>) rawSettings.get(ClientSettings.SESSION_DB_ROLES);
}

/**
* Sets the comment that will be added to the query log record associated with the query.
* @param logComment - comment to be added to the log
* @return same instance of the builder
*/
public QuerySettings logComment(String logComment) {
this.logComment = logComment;
if (logComment != null && !logComment.isEmpty()) {
rawSettings.put(ClientSettings.SERVER_SETTING_PREFIX + "log_comment", logComment);
}
return this;
}

private String logComment = null;

public String getLogComment() {
return logComment;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.clickhouse.client.ClickHouseProtocol;
import com.clickhouse.client.api.Client;
import com.clickhouse.client.api.ClientException;
import com.clickhouse.client.api.command.CommandResponse;
import com.clickhouse.client.api.data_formats.ClickHouseBinaryFormatReader;
import com.clickhouse.client.api.enums.Protocol;
import com.clickhouse.client.api.insert.InsertResponse;
Expand All @@ -18,17 +19,12 @@
import com.clickhouse.client.api.metrics.ServerMetrics;
import com.clickhouse.client.api.query.GenericRecord;
import com.clickhouse.client.api.query.QueryResponse;
import com.clickhouse.client.config.ClickHouseClientOption;
import com.clickhouse.data.ClickHouseFormat;
import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.client.WireMock;
import com.github.tomakehurst.wiremock.common.ConsoleNotifier;
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
import com.github.tomakehurst.wiremock.http.Fault;
import org.testcontainers.shaded.org.apache.commons.lang3.RandomStringUtils;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.io.ByteArrayInputStream;
Expand All @@ -41,7 +37,6 @@
import java.util.UUID;
import java.util.concurrent.TimeUnit;

import static com.github.tomakehurst.wiremock.stubbing.Scenario.STARTED;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;
Expand Down Expand Up @@ -294,4 +289,44 @@ public void testInsertSettingsAddDatabase() throws Exception {
List<GenericRecord> records = client.queryAll("SELECT * FROM " + new_database + "." + tableName);
assertEquals(records.size(), 1000);
}

@Test(groups = {"integration"}, dataProviderClass = InsertTests.class, dataProvider = "logCommentDataProvider")
public void testLogComment(String logComment) throws Exception {

InsertSettings settings = new InsertSettings()
.setQueryId(UUID.randomUUID().toString())
.logComment(logComment);

final String tableName = "single_pojo_table";
final String createSQL = SamplePOJO.generateTableCreateSQL(tableName);
final SamplePOJO pojo = new SamplePOJO();

dropTable(tableName);
createTable(createSQL);
client.register(SamplePOJO.class, client.getTableSchema(tableName, "default"));

try (InsertResponse response = client.insert(tableName, Collections.singletonList(pojo), settings).get(30, TimeUnit.SECONDS)) {
Assert.assertEquals(response.getWrittenRows(), 1);
}

try (CommandResponse resp = client.execute("SYSTEM FLUSH LOGS").get()) {
}

List<GenericRecord> logRecords = client.queryAll("SELECT query_id, log_comment FROM system.query_log WHERE query_id = '" + settings.getQueryId() + "'");
Assert.assertEquals(logRecords.get(0).getString("query_id"), settings.getQueryId());
Assert.assertEquals(logRecords.get(0).getString("log_comment"), logComment == null ? "" : logComment);
}

@DataProvider( name = "logCommentDataProvider")
public static Object[] logCommentDataProvider() {
return new Object[][] {
{ "Test log comment" },
{ "Another log comment?" },
{ "Log comment with special characters: !@#$%^&*()" },
{ "Log comment with unicode: 你好" },
{ "", },
{ " "},
{ null }
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1743,6 +1743,26 @@ public void testClientCustomRoles(String[] roles) throws Exception {
}


@Test(groups = {"integration"})
public void testLogComment() throws Exception {

String logComment = "Test log comment";
QuerySettings settings = new QuerySettings()
.setQueryId(UUID.randomUUID().toString())
.logComment(logComment);
try (QueryResponse response = client.query("SELECT 1", settings).get()) {
Assert.assertNotNull(response.getQueryId());
Assert.assertTrue(response.getQueryId().startsWith(settings.getQueryId()));
}

try (CommandResponse resp = client.execute("SYSTEM FLUSH LOGS").get()) {
}

List<GenericRecord> logRecords = client.queryAll("SELECT query_id, log_comment FROM system.query_log WHERE query_id = '" + settings.getQueryId() + "'");
Assert.assertEquals(logRecords.get(0).getString("query_id"), settings.getQueryId());
Assert.assertEquals(logRecords.get(0).getString("log_comment"), logComment);
}

protected Client.Builder newClient() {
ClickHouseNode node = getServer(ClickHouseProtocol.HTTP);
return new Client.Builder()
Expand Down
Loading