com.sequoiadb.datasource.ConcreteLocalStrategy Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sequoiadb-driver Show documentation
Show all versions of sequoiadb-driver Show documentation
Java client driver for SequoiaDB
/*
* Copyright 2018 SequoiaDB Inc.
*
* 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.
*/
package com.sequoiadb.datasource;
import com.sequoiadb.exception.BaseException;
import com.sequoiadb.exception.SDBError;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.*;
class ConcreteLocalStrategy extends AbstractStrategy {
private Random _rand = new Random(47);
private List _localAddrs = new ArrayList();
private List _localIPs = new ArrayList();
@Override
public void init(List addressList, List _idleConnPairs, List _usedConnPairs) {
super.init(addressList, _idleConnPairs, _usedConnPairs);
_localIPs = getNetCardIPs();
_localAddrs = getLocalCoordIPs(_addrs, _localIPs);
}
@Override
public String getAddress() {
String addr = null;
_addrLock.lock();
try {
if (_localAddrs.size() > 0) {
addr = _localAddrs.get(_rand.nextInt(_localAddrs.size()));
} else {
if (_addrs.size() > 0) {
addr = _addrs.get(_rand.nextInt(_addrs.size()));
}
}
} finally {
_addrLock.unlock();
}
return addr;
}
public void addAddress(String addr) {
super.addAddress(addr);
_addrLock.lock();
try {
if (isLocalAddress(addr, _localIPs)) {
_localAddrs.add(addr);
}
} finally {
_addrLock.unlock();
}
}
public List removeAddress(String addr) {
List list = super.removeAddress(addr);
_addrLock.lock();
try {
if (isLocalAddress(addr, _localIPs)) {
_localAddrs.remove(addr);
}
} finally {
_addrLock.unlock();
}
return list;
}
static List getNetCardIPs() {
List localIPs = new ArrayList();
localIPs.add("127.0.0.1");
try {
Enumeration netcards = NetworkInterface.getNetworkInterfaces();
if (null == netcards) {
return localIPs;
}
for (NetworkInterface netcard : Collections.list(netcards)) {
if (null != netcard.getHardwareAddress()) {
List list = netcard.getInterfaceAddresses();
for (InterfaceAddress interfaceAddress : list) {
String addr = interfaceAddress.getAddress().toString();
if (addr.indexOf("/") >= 0) {// TODO: check in linux
localIPs.add(addr.split("/")[1]);
}
}
}
}
} catch (SocketException e) {
throw new BaseException(SDBError.SDB_SYS, "failed to get local ip address");
}
return localIPs;
}
static List getLocalCoordIPs(List urls, List localIPs) {
List localAddrs = new ArrayList();
if (localIPs.size() > 0) {
for (String url : urls) {
String ip = url.split(":")[0].trim();
if (localIPs.contains(ip))
localAddrs.add(url);
}
}
return localAddrs;
}
/**
* @return true or false
* @fn boolean isLocalAddress(String url)
* @bref Judge a coord address is in local or not
*/
static boolean isLocalAddress(String url, List localIPs) {
return localIPs.contains(url.split(":")[0].trim());
}
}