Skip to content

Commit

Permalink
Wait for rules to be refreshed in BaseCaseInsensitiveMappingTest
Browse files Browse the repository at this point in the history
A refresh period of 1ms doesn't guarantee that rules will be refreshed
before they are used. Since the rules are refreshed synchronously we
sleep for 2*refresh period to guarantee that rules are refreshed.
  • Loading branch information
hashhar committed Mar 16, 2022
1 parent 2605f0c commit 8d818e7
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,31 @@
package io.trino.plugin.jdbc.mapping;

import com.google.common.collect.ImmutableList;
import io.airlift.units.Duration;

import java.io.IOException;
import java.nio.file.Path;
import java.util.List;

import static io.airlift.json.JsonCodec.jsonCodec;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.nio.file.Files.createTempFile;
import static java.nio.file.Files.write;
import static java.util.concurrent.TimeUnit.MILLISECONDS;

public final class RuleBasedIdentifierMappingUtils
{
public static final Duration REFRESH_PERIOD_DURATION = Duration.succinctDuration(1, MILLISECONDS);

private RuleBasedIdentifierMappingUtils() {}

public static Path createRuleBasedIdentifierMappingFile()
throws IOException
throws Exception
{
return createRuleBasedIdentifierMappingFile(ImmutableList.of(), ImmutableList.of());
}

public static Path createRuleBasedIdentifierMappingFile(List<SchemaMappingRule> schemas, List<TableMappingRule> tables)
throws IOException
throws Exception
{
Path file = createTempFile("identifier-mapping-", ".json");
file.toFile().deleteOnExit();
Expand All @@ -44,13 +47,15 @@ public static Path createRuleBasedIdentifierMappingFile(List<SchemaMappingRule>
}

public static Path updateRuleBasedIdentifierMappingFile(Path file, List<SchemaMappingRule> schemas, List<TableMappingRule> tables)
throws IOException
throws Exception
{
IdentifierMappingRules mapping = new IdentifierMappingRules(schemas, tables);

String json = jsonCodec(IdentifierMappingRules.class).toJson(mapping);

write(file, json.getBytes(UTF_8));
// Wait to ensure rules have been reloaded
Thread.sleep(2 * REFRESH_PERIOD_DURATION.toMillis());
return file;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import java.nio.file.Path;

import static io.trino.plugin.jdbc.mapping.RuleBasedIdentifierMappingUtils.REFRESH_PERIOD_DURATION;
import static io.trino.plugin.jdbc.mapping.RuleBasedIdentifierMappingUtils.createRuleBasedIdentifierMappingFile;
import static io.trino.plugin.mysql.MySqlQueryRunner.createMySqlQueryRunner;
import static java.util.Objects.requireNonNull;
Expand All @@ -47,7 +48,7 @@ protected QueryRunner createQueryRunner()
ImmutableMap.<String, String>builder()
.put("case-insensitive-name-matching", "true")
.put("case-insensitive-name-matching.config-file", mappingFile.toFile().getAbsolutePath())
.put("case-insensitive-name-matching.config-file.refresh-period", "1ms") // ~always refresh
.put("case-insensitive-name-matching.config-file.refresh-period", REFRESH_PERIOD_DURATION.toString())
.buildOrThrow(),
ImmutableList.of());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.nio.file.Path;
import java.util.Optional;

import static io.trino.plugin.jdbc.mapping.RuleBasedIdentifierMappingUtils.REFRESH_PERIOD_DURATION;
import static io.trino.plugin.jdbc.mapping.RuleBasedIdentifierMappingUtils.createRuleBasedIdentifierMappingFile;
import static io.trino.plugin.oracle.OracleQueryRunner.createOracleQueryRunner;
import static io.trino.plugin.oracle.TestingOracleServer.TEST_USER;
Expand Down Expand Up @@ -51,7 +52,7 @@ protected QueryRunner createQueryRunner()
.putAll(OracleQueryRunner.connectionProperties(oracleServer))
.put("case-insensitive-name-matching", "true")
.put("case-insensitive-name-matching.config-file", mappingFile.toFile().getAbsolutePath())
.put("case-insensitive-name-matching.config-file.refresh-period", "1ms") // ~always refresh
.put("case-insensitive-name-matching.config-file.refresh-period", REFRESH_PERIOD_DURATION.toString())
.buildOrThrow(),
ImmutableList.of());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import java.nio.file.Path;

import static io.trino.plugin.jdbc.mapping.RuleBasedIdentifierMappingUtils.REFRESH_PERIOD_DURATION;
import static io.trino.plugin.jdbc.mapping.RuleBasedIdentifierMappingUtils.createRuleBasedIdentifierMappingFile;
import static java.util.Objects.requireNonNull;

Expand All @@ -46,7 +47,7 @@ protected QueryRunner createQueryRunner()
ImmutableMap.<String, String>builder()
.put("case-insensitive-name-matching", "true")
.put("case-insensitive-name-matching.config-file", mappingFile.toFile().getAbsolutePath())
.put("case-insensitive-name-matching.config-file.refresh-period", "1ms") // ~always refresh
.put("case-insensitive-name-matching.config-file.refresh-period", REFRESH_PERIOD_DURATION.toString())
.buildOrThrow(),
ImmutableSet.of());
}
Expand Down
5 changes: 5 additions & 0 deletions plugin/trino-sqlserver/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@
<artifactId>log-manager</artifactId>
</dependency>

<dependency>
<groupId>io.airlift</groupId>
<artifactId>units</artifactId>
</dependency>

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.sql.ResultSet;
import java.sql.Statement;

import static io.trino.plugin.jdbc.mapping.RuleBasedIdentifierMappingUtils.REFRESH_PERIOD_DURATION;
import static io.trino.plugin.jdbc.mapping.RuleBasedIdentifierMappingUtils.createRuleBasedIdentifierMappingFile;
import static io.trino.plugin.sqlserver.SqlServerQueryRunner.createSqlServerQueryRunner;
import static java.util.Objects.requireNonNull;
Expand All @@ -51,7 +52,7 @@ protected QueryRunner createQueryRunner()
ImmutableMap.<String, String>builder()
.put("case-insensitive-name-matching", "true")
.put("case-insensitive-name-matching.config-file", mappingFile.toFile().getAbsolutePath())
.put("case-insensitive-name-matching.config-file.refresh-period", "1ms") // ~always refresh
.put("case-insensitive-name-matching.config-file.refresh-period", REFRESH_PERIOD_DURATION.toString())
.buildOrThrow(),
ImmutableList.of());
}
Expand Down

0 comments on commit 8d818e7

Please sign in to comment.