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

Maven-like version range support #739

Merged
merged 3 commits into from
Oct 10, 2021
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
5 changes: 5 additions & 0 deletions clickhouse-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
</exclusions>
</dependency>

<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>dnsjava</groupId>
<artifactId>dnsjava</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.clickhouse.client;

import java.util.function.Function;

import com.clickhouse.client.cache.CaffeineCache;
import com.clickhouse.client.cache.JdkLruCache;

/**
* Wrapper interface depicts essential methods required by a client-side cache.
*/
public interface ClickHouseCache<K, V> {
/**
* Default cache size.
*/
static final int DEFAULT_CACHE_SIZE = 50;

/**
* Creates a cache with specific capacity and load function.
*
* @param <K> type of key
* @param <V> type of value
* @param capacity capacity of the cache, zero or negative number will be
* treated as {@link #DEFAULT_CACHE_SIZE}
* @param expireSeconds seconds to expire after access
* @param loadFunc non-null load function
* @return cache
*/
static <K, V> ClickHouseCache<K, V> create(int capacity, long expireSeconds, Function<K, V> loadFunc) {
ClickHouseCache<K, V> cache;

try {
cache = CaffeineCache.create(capacity, expireSeconds, loadFunc);
} catch (Throwable e) {
// ignore
cache = JdkLruCache.create(capacity, loadFunc);
}
return cache;
}

/**
* Gets value from cache if it exists.
*
* @param key key, in genernal should NOT be null
* @return non-null value in general
*/
V get(K key);

/**
* Gets inner cache object to gain more access.
*
* @param <T> type of the cache
* @param clazz non-null class of the cache
* @return inner cache object
* @throws NullPointerException
*/
<T> T unwrap(Class<T> clazz);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

/**
* This defines a data processor for dealing with one or multiple
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
package com.clickhouse.client;

import java.net.InetSocketAddress;

import com.clickhouse.client.config.ClickHouseDefaults;
import com.clickhouse.client.logging.Logger;
import com.clickhouse.client.logging.LoggerFactory;
import org.xbill.DNS.Lookup;
import org.xbill.DNS.Record;
import org.xbill.DNS.SRVRecord;
import org.xbill.DNS.TextParseException;
import org.xbill.DNS.Type;
import com.clickhouse.client.naming.SrvResolver;

/**
* Default DNS resolver. It tries to look up service record (SRV record) when
* {@link com.clickhouse.client.config.ClickHouseDefaults#DNS_RESOLVE} is set to
* {@link com.clickhouse.client.config.ClickHouseDefaults#SRV_RESOLVE} is set to
* {@code true}.
*/
public class ClickHouseDnsResolver {
Expand All @@ -21,57 +18,25 @@ public class ClickHouseDnsResolver {
private static final ClickHouseDnsResolver instance = ClickHouseUtils.getService(ClickHouseDnsResolver.class,
new ClickHouseDnsResolver());

public static ClickHouseDnsResolver getInstance() {
return instance;
}

protected ClickHouseDnsResolver() {
}
protected static ClickHouseDnsResolver newInstance() {
ClickHouseDnsResolver resolver = null;

public InetSocketAddress resolve(ClickHouseProtocol protocol, String host, int port) {
if (protocol == null || host == null) {
throw new IllegalArgumentException("Non-null protocol and host are required");
}

if ((boolean) ClickHouseDefaults.DNS_RESOLVE.getEffectiveDefaultValue()) {
SRVRecord r = resolve(host, false);
if (r != null) {
host = r.getName().canonicalize().toString(true);
port = r.getPort();
if ((boolean) ClickHouseDefaults.SRV_RESOLVE.getEffectiveDefaultValue()) {
try {
resolver = new SrvResolver();
} catch (Throwable e) {
log.warn("Failed to enable SRV resolver due to:", e);
}
} else {
host = ClickHouseDefaults.HOST.getEffectiveValue(host);
port = port <= 0 ? protocol.getDefaultPort() : port;
}

return new InetSocketAddress(host, port);
return resolver == null ? new ClickHouseDnsResolver() : resolver;
}

// TODO register a callback for DNS change?

protected SRVRecord resolve(String srvDns, boolean basedOnWeight) {
Record[] records = null;
try {
records = new Lookup(srvDns, Type.SRV).run();
} catch (TextParseException e) {
// fallback to a cached entry?
log.warn("Not able to resolve given DNS query: [%s]", srvDns, e);
}

SRVRecord record = null;
if (records != null) {
if (basedOnWeight) {
for (int i = 0; i < records.length; i++) {
SRVRecord rec = (SRVRecord) records[i];
if (record == null || record.getWeight() > rec.getWeight()) {
record = rec;
}
}
} else {
record = (SRVRecord) records[0];
}
}
public static ClickHouseDnsResolver getInstance() {
return instance;
}

return record;
public InetSocketAddress resolve(ClickHouseProtocol protocol, String host, int port) {
return new InetSocketAddress(host, port);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.io.Serializable;
import java.net.InetSocketAddress;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
Expand Down
Loading