![JAR search and dependency download from the Maven repository](/logo.png)
com.arangodb.internal.net.ExtendedHostResolver Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of core Show documentation
Show all versions of core Show documentation
Core module for ArangoDB Java Driver
/*
* DISCLAIMER
*
* Copyright 2017 ArangoDB GmbH, Cologne, Germany
*
* 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.
*
* Copyright holder is ArangoDB GmbH, Cologne, Germany
*/
package com.arangodb.internal.net;
import com.arangodb.ArangoDBException;
import com.arangodb.config.HostDescription;
import com.arangodb.internal.ArangoExecutorSync;
import com.arangodb.internal.ArangoRequestParam;
import com.arangodb.internal.config.ArangoConfig;
import com.arangodb.internal.serde.InternalSerde;
import com.arangodb.internal.util.HostUtils;
import com.arangodb.internal.InternalRequest;
import com.arangodb.internal.RequestType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.*;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import static com.arangodb.internal.serde.SerdeUtils.constructParametricType;
/**
* @author Mark Vollmary
*/
public class ExtendedHostResolver implements HostResolver {
private static final Logger LOGGER = LoggerFactory.getLogger(ExtendedHostResolver.class);
private final HostSet hosts;
private final ArangoConfig config;
private final ConnectionFactory connectionFactory;
private final Integer acquireHostListInterval;
private final ScheduledExecutorService scheduler;
private ArangoExecutorSync executor;
private InternalSerde arangoSerialization;
private ScheduledFuture> schedule;
public ExtendedHostResolver(final List hosts, final ArangoConfig config,
final ConnectionFactory connectionFactory, Integer acquireHostListInterval) {
this.acquireHostListInterval = acquireHostListInterval;
this.hosts = new HostSet(hosts);
this.config = config;
this.connectionFactory = connectionFactory;
scheduler = Executors.newSingleThreadScheduledExecutor(r -> {
Thread t = Executors.defaultThreadFactory().newThread(r);
t.setDaemon(true);
return t;
}
);
}
@Override
public void init(ArangoExecutorSync executor, InternalSerde arangoSerialization) {
this.executor = executor;
this.arangoSerialization = arangoSerialization;
resolve();
schedule = scheduler.scheduleAtFixedRate(this::resolve, acquireHostListInterval, acquireHostListInterval, TimeUnit.MILLISECONDS);
}
@Override
public void close() {
schedule.cancel(false);
scheduler.shutdown();
}
@Override
public HostSet getHosts() {
return hosts;
}
private void resolve() {
final Collection endpoints = resolveFromServer();
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Resolve {} Endpoints", endpoints.size());
LOGGER.debug("Endpoints {}", Arrays.deepToString(endpoints.toArray()));
}
if (!endpoints.isEmpty()) {
hosts.markAllForDeletion();
}
for (final String endpoint : endpoints) {
LOGGER.debug("Create HOST from {}", endpoint);
if (endpoint.matches(".*://.+:[0-9]+")) {
final String[] s = endpoint.replaceAll(".*://", "").split(":");
if (s.length == 2) {
final HostDescription description = new HostDescription(s[0], Integer.parseInt(s[1]));
hosts.addHost(HostUtils.createHost(description, config, connectionFactory));
} else if (s.length == 4) {
// IPV6 Address - TODO: we need a proper function to resolve AND support IPV4 & IPV6 functions
// globally
final HostDescription description = new HostDescription("127.0.0.1", Integer.parseInt(s[3]));
hosts.addHost(HostUtils.createHost(description, config, connectionFactory));
} else {
LOGGER.warn("Skip Endpoint (Missing Port) {}", endpoint);
}
} else {
LOGGER.warn("Skip Endpoint (Format) {}", endpoint);
}
}
hosts.clearAllMarkedForDeletion();
}
private Collection resolveFromServer() {
Collection response;
try {
response = executor.execute(
new InternalRequest(ArangoRequestParam.SYSTEM, RequestType.GET, "/_api/cluster/endpoints"),
(r) -> {
final List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy