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

[Backport 1.3] [Backport 1.x] Bug Fix, return default ID when log4j ThreadContext is empty #543

Merged
merged 1 commit into from
Apr 8, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
21 changes: 21 additions & 0 deletions integ-test/src/test/java/org/opensearch/sql/legacy/JdbcTestIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;

import java.io.IOException;
import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.Assert;
Expand Down Expand Up @@ -40,6 +41,26 @@ public void testPercentilesQuery() {
assertThat(percentileRow.getDouble("99.9"), equalTo(39.0));
}

// https://github.com/opensearch-project/sql/issues/537
@Test
public void testSlowQuery() throws IOException {
// set slow log threshold = 0s
updateClusterSettings(new ClusterSetting(PERSISTENT, "plugins.sql.slowlog", "0"));

JSONObject response = executeJdbcRequest(
"SELECT percentiles(age, 25.0, 50.0, 75.0, 99.9) age_percentiles " +
"FROM opensearch-sql_test_index_people");

assertThat(response.getJSONArray("datarows").length(), equalTo(1));
JSONObject percentileRow = (JSONObject) response.query("/datarows/0/0");
assertThat(percentileRow.getDouble("25.0"), equalTo(31.5));
assertThat(percentileRow.getDouble("50.0"), equalTo(33.5));
assertThat(percentileRow.getDouble("75.0"), equalTo(36.5));
assertThat(percentileRow.getDouble("99.9"), equalTo(39.0));

wipeAllClusterSettings();
}

@Ignore("flaky test, trigger resource not enough exception. "
+ "ORDER BY date_format(insert_time, 'dd-MM-YYYY') can't be pushed down ")
public void testDateTimeInQuery() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package org.opensearch.sql.legacy.utils;

import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import org.apache.logging.log4j.ThreadContext;

Expand All @@ -20,6 +21,8 @@ public class LogUtils {
*/
private static final String REQUEST_ID_KEY = "request_id";

private static final String EMPTY_ID = "ID";

/**
* Generates a random UUID and adds to the {@link ThreadContext} as the request id.
* <p>
Expand All @@ -38,13 +41,7 @@ public static void addRequestId() {
* @return the current request id from {@link ThreadContext}
*/
public static String getRequestId() {

final String requestId = ThreadContext.get(REQUEST_ID_KEY);
if (requestId == null) {
throw new IllegalStateException("Request id not present in current context");
}

return requestId;
return Optional.ofNullable(ThreadContext.get(REQUEST_ID_KEY)).orElseGet(() -> EMPTY_ID);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertEquals;

import org.apache.logging.log4j.ThreadContext;
import org.junit.After;
Expand Down Expand Up @@ -44,10 +45,9 @@ public void addRequestId_alreadyExists() {
Assert.assertThat(requestId2, not(equalTo(requestId)));
}

@Test(expected = IllegalStateException.class)
@Test
public void getRequestId_doesNotExist() {

LogUtils.getRequestId();
assertEquals("ID", LogUtils.getRequestId());
}

@Test
Expand Down