use of org.teiid.net.HostInfo in project teiid by teiid.
the class TestSocketServerConnection method testIsSameInstance.
@Test
public void testIsSameInstance() throws Exception {
// $NON-NLS-1$
SocketServerConnection conn = createConnection(null, new HostInfo("0.0.0.0", 1), new Properties());
// $NON-NLS-1$
SocketServerConnection conn1 = createConnection(null, new HostInfo("0.0.0.1", 1), new Properties());
assertFalse(conn.isSameInstance(conn1));
assertTrue(conn.isSameInstance(conn));
}
use of org.teiid.net.HostInfo in project teiid by teiid.
the class TestSocketServerConnection method createConnection.
private SocketServerConnection createConnection(final Throwable t, final HostInfo hostInfo, Properties p) throws CommunicationException, ConnectionException {
ServerDiscovery discovery = new UrlServerDiscovery(new TeiidURL(hostInfo.getHostName(), hostInfo.getPortNumber(), false));
SocketServerInstanceFactory instanceFactory = new SocketServerInstanceFactory() {
FakeILogon logon = new FakeILogon(t);
@Override
public SocketServerInstance getServerInstance(HostInfo info) throws CommunicationException, IOException {
SocketServerInstance instance = Mockito.mock(SocketServerInstance.class);
Mockito.stub(instance.getCryptor()).toReturn(new NullCryptor());
Mockito.stub(instance.getHostInfo()).toReturn(hostInfo);
Mockito.stub(instance.getService(ILogon.class)).toReturn(logon);
Mockito.stub(instance.getServerVersion()).toReturn("07.03");
if (t != null) {
try {
Mockito.doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
if (logon.t == null) {
return null;
}
throw logon.t;
}
}).when(instance).send((Message) Mockito.anyObject(), (ResultsReceiver<Object>) Mockito.anyObject(), (Serializable) Mockito.anyObject());
} catch (Exception e) {
}
}
Mockito.stub(instance.isOpen()).toReturn(true);
return instance;
}
@Override
public void connected(SocketServerInstance instance, SessionToken session) {
}
@Override
public void disconnected(SocketServerInstance instance, SessionToken session) {
}
};
SocketServerConnection connection = new SocketServerConnection(instanceFactory, false, discovery, p);
return connection;
}
use of org.teiid.net.HostInfo in project teiid by teiid.
the class TestSocketServerInstanceImpl method createInstance.
private SocketServerInstanceImpl createInstance(ObjectChannelFactory channelFactory) throws CommunicationException, IOException {
HostInfo info = new HostInfo("0.0.0.0", 1);
info.getInetAddress();
SocketServerInstanceImpl ssii = new SocketServerInstanceImpl(info, 1, 1);
ssii.connect(channelFactory);
return ssii;
}
use of org.teiid.net.HostInfo in project teiid by teiid.
the class SocketServerConnection method selectServerInstance.
/**
* Implements a sticky random selection policy
* TODO: make this customizable
* TODO: put more information on hostinfo as to process response time, last successful connect, etc.
* @throws ConnectionException
*/
public synchronized SocketServerInstance selectServerInstance(boolean logoff) throws CommunicationException, ConnectionException {
if (closed) {
throw new CommunicationException(JDBCPlugin.Event.TEIID20016, JDBCPlugin.Util.gs(JDBCPlugin.Event.TEIID20016));
}
if (this.serverInstance != null && (!failOver || this.serverInstance.isOpen())) {
return this.serverInstance;
}
List<HostInfo> hostKeys = new ArrayList<HostInfo>(this.serverDiscovery.getKnownHosts(logonResult, null));
boolean discoverHosts = true;
closeServerInstance();
List<HostInfo> hostCopy = new ArrayList<HostInfo>(hostKeys);
int knownHosts = hostKeys.size();
while (hostKeys.size() > 0) {
HostInfo hostInfo = this.serverDiscovery.selectNextInstance(hostKeys);
Exception ex = null;
try {
if (!hostInfo.isResolved()) {
InetAddress inetAddress = hostInfo.getInetAddress();
if (!hostInfo.isResolved()) {
// create a resolved version
hostInfo = new HostInfo(hostInfo.getHostName(), new InetSocketAddress(inetAddress, hostInfo.getPortNumber()));
}
}
ILogon newLogon = connect(hostInfo);
if (this.logonResult == null) {
try {
logon(newLogon, logoff);
this.serverDiscovery.connectionSuccessful(hostInfo);
if (discoverHosts) {
List<HostInfo> updatedHosts = this.serverDiscovery.getKnownHosts(logonResult, this.serverInstance);
if (updatedHosts.size() > 1 && new HashSet<HostInfo>(updatedHosts).size() > new HashSet<HostInfo>(hostCopy).size()) {
hostKeys = updatedHosts;
closeServerInstance();
discoverHosts = false;
continue;
}
}
} catch (LogonException e) {
// to give to the user
throw new ConnectionException(e);
} catch (TeiidComponentException e) {
if (e.getCause() instanceof CommunicationException) {
throw (CommunicationException) e.getCause();
}
throw new CommunicationException(JDBCPlugin.Event.TEIID20018, e, JDBCPlugin.Util.gs(JDBCPlugin.Event.TEIID20018));
}
}
return this.serverInstance;
} catch (IOException e) {
ex = e;
} catch (SingleInstanceCommunicationException e) {
ex = e;
}
this.serverDiscovery.markInstanceAsBad(hostInfo);
if (knownHosts == 1) {
// just a single host, use the exception
if (ex instanceof UnknownHostException) {
throw new SingleInstanceCommunicationException(JDBCPlugin.Event.TEIID20019, ex, JDBCPlugin.Util.gs(JDBCPlugin.Event.TEIID20019, hostInfo.getHostName()));
}
throw new SingleInstanceCommunicationException(JDBCPlugin.Event.TEIID20020, ex, JDBCPlugin.Util.gs(JDBCPlugin.Event.TEIID20020, hostInfo.getHostName(), String.valueOf(hostInfo.getPortNumber()), ex.getMessage()));
}
// $NON-NLS-1$
log.log(Level.FINE, "Unable to connect to host", ex);
}
throw new CommunicationException(JDBCPlugin.Event.TEIID20021, JDBCPlugin.Util.gs(JDBCPlugin.Event.TEIID20021, hostCopy.toString()));
}
use of org.teiid.net.HostInfo in project teiid by teiid.
the class TestFailover method helpEstablishConnection.
private SocketServerConnection helpEstablishConnection(boolean clientSecure, SSLConfiguration config, Properties socketConfig) throws CommunicationException, ConnectionException {
listener = createListener(addr, config);
listener1 = createListener(addr, config);
listener1.stop();
Properties p = new Properties();
TeiidURL teiidUrl = new TeiidURL(addr.getHostName(), listener.getPort(), clientSecure);
teiidUrl.getHostInfo().add(new HostInfo(addr.getHostName(), listener1.getPort()));
String url = teiidUrl.getAppServerURL();
p.setProperty(TeiidURL.CONNECTION.SERVER_URL, url);
p.setProperty(TeiidURL.CONNECTION.DISCOVERY_STRATEGY, UrlServerDiscovery.class.getName());
p.setProperty(TeiidURL.CONNECTION.AUTO_FAILOVER, Boolean.TRUE.toString());
if (sscf == null) {
sscf = new SocketServerConnectionFactory();
sscf.initialize(socketConfig);
}
return sscf.getConnection(p);
}
Aggregations