All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.hekate.cluster.seed.StaticSeedNodeProvider Maven / Gradle / Ivy

There is a newer version: 4.1.3
Show newest version
/*
 * Copyright 2020 The Hekate Project
 *
 * The Hekate Project licenses this file to you 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.
 */

package io.hekate.cluster.seed;

import io.hekate.cluster.ClusterServiceFactory;
import io.hekate.core.HekateException;
import io.hekate.core.internal.util.AddressUtils;
import io.hekate.core.internal.util.ConfigCheck;
import io.hekate.core.report.ConfigReportSupport;
import io.hekate.core.report.ConfigReporter;
import io.hekate.util.format.ToString;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import static java.util.Collections.emptyList;
import static java.util.Collections.unmodifiableList;

/**
 * Seed node provider with a pre-configured static list of seed node addresses.
 *
 * 

* This provider uses a fixed set of addresses that can be specified at construction time via {@link StaticSeedNodeProviderConfig} * configuration object and can't be changed at runtime. Addresses can be specified via * {@link StaticSeedNodeProviderConfig#setAddresses(List)} method that accepts a list of strings in a form of {@code :} * (f.e. {@code 192.168.39.41:10012}). *

* *

* Below is the configuration example: * ${source: cluster/seed/StaticSeedNodeProviderJavadocTest.java#configuration} *

* * @see ClusterServiceFactory#setSeedNodeProvider(SeedNodeProvider) * @see SeedNodeProvider */ public class StaticSeedNodeProvider implements SeedNodeProvider, ConfigReportSupport { private final List addresses; /** * Constructs new instance. * * @param cfg Configuration. * * @throws UnknownHostException If one of the configured addresses couldn't be resolved. */ public StaticSeedNodeProvider(StaticSeedNodeProviderConfig cfg) throws UnknownHostException { ConfigCheck.get(StaticSeedNodeProvider.class).notNull(cfg, "configuration"); this.addresses = parseAddresses(cfg.getAddresses()); } @Override public void report(ConfigReporter report) { report.section("static", r -> r.value("addresses", addresses) ); } @Override public List findSeedNodes(String cluster) throws HekateException { return addresses; } @Override public void startDiscovery(String cluster, InetSocketAddress node) throws HekateException { // No-op. } @Override public void suspendDiscovery() throws HekateException { // No-op. } @Override public void stopDiscovery(String cluster, InetSocketAddress node) throws HekateException { // No-op. } @Override public long cleanupInterval() { return 0; } @Override public void registerRemote(String cluster, InetSocketAddress node) throws HekateException { // No-op. } @Override public void unregisterRemote(String cluster, InetSocketAddress node) throws HekateException { // No-op. } /** * Returns the unmodifiable list of seed node addresses (see {@link StaticSeedNodeProviderConfig#setAddresses(List)}). * * @return Unmodifiable list of seed node addresses. */ public List getAddresses() { return addresses; } private List parseAddresses(Collection addresses) throws UnknownHostException { if (addresses != null && !addresses.isEmpty()) { ArrayList result = new ArrayList<>(); for (String address : addresses) { if (address != null) { InetSocketAddress socketAddress = AddressUtils.parse(address, ConfigCheck.get(StaticSeedNodeProviderConfig.class)); if (socketAddress != null) { result.add(socketAddress); } } } result.trimToSize(); return unmodifiableList(result); } return emptyList(); } @Override public String toString() { return ToString.format(this); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy