Search in sources :

Example 1 with Connection

use of org.voltcore.network.Connection in project voltdb by VoltDB.

the class TestClientInterfaceHandleManager method testGetRemoveThenFind.

@Test
public void testGetRemoveThenFind() throws Exception {
    Connection mockConnection = mock(Connection.class);
    doReturn(mock(org.voltcore.network.WriteStream.class)).when(mockConnection).writeStream();
    ClientInterfaceHandleManager dut = new ClientInterfaceHandleManager(false, mockConnection, null, AdmissionControlGroup.getDummy());
    List<Long> handles = new ArrayList<Long>();
    // Add 10 handles
    for (int i = 0; i < 10; i++) {
        handles.add(dut.getHandle(true, 7, 31337 + i, 10, 10l, "yankeefoo", 0, i % 2 == 0 ? true : false, false));
    }
    // remove handle 6
    ClientInterfaceHandleManager.Iv2InFlight six = dut.removeHandle(handles.get(6));
    assertEquals(31337 + 6, six.m_clientHandle);
    // make sure that 0-5, 7-9 still are found.
    for (int i = 0; i < 10; i++) {
        ClientInterfaceHandleManager.Iv2InFlight inf = dut.findHandle(handles.get(i));
        if (i == 6) {
            assertTrue(inf == null);
            continue;
        }
        assertEquals(31337 + i, inf.m_clientHandle);
    }
}
Also used : Connection(org.voltcore.network.Connection) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 2 with Connection

use of org.voltcore.network.Connection in project voltdb by VoltDB.

the class TestClientInterfaceHandleManager method testGetSkipMissingHandles.

@Test
public void testGetSkipMissingHandles() throws Exception {
    Connection mockConnection = mock(Connection.class);
    doReturn(mock(org.voltcore.network.WriteStream.class)).when(mockConnection).writeStream();
    ClientInterfaceHandleManager dut = new ClientInterfaceHandleManager(false, mockConnection, null, AdmissionControlGroup.getDummy());
    List<Long> handles = new ArrayList<Long>();
    for (int i = 0; i < 10; i++) {
        handles.add(dut.getHandle(true, 7, 31337 + i, 10, 10l, "yankeefoo", 0, i % 2 == 0 ? true : false, false));
    }
    // pretend handles 0-4 were lost
    for (int i = 5; i < 10; i++) {
        ClientInterfaceHandleManager.Iv2InFlight inflight = dut.findHandle(handles.get(i));
        assertEquals((long) handles.get(i), i % 2 == 0 ? inflight.m_ciHandle | ClientInterfaceHandleManager.READ_BIT : inflight.m_ciHandle);
        assertEquals(31337 + i, inflight.m_clientHandle);
    }
}
Also used : Connection(org.voltcore.network.Connection) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 3 with Connection

use of org.voltcore.network.Connection in project voltdb by VoltDB.

the class TestClientInterfaceHandleManager method testGetAndFind.

@Test
public void testGetAndFind() throws Exception {
    Connection mockConnection = mock(Connection.class);
    ClientInterfaceHandleManager dut = new ClientInterfaceHandleManager(false, mockConnection, null, AdmissionControlGroup.getDummy());
    long handle = dut.getHandle(true, 7, 31337, 10, 10l, "foo", 0, false, false);
    assertEquals(7, ClientInterfaceHandleManager.getPartIdFromHandle(handle));
    assertEquals(0, ClientInterfaceHandleManager.getSeqNumFromHandle(handle));
    ClientInterfaceHandleManager.Iv2InFlight inflight = dut.findHandle(handle);
    assertEquals(handle, inflight.m_ciHandle);
    assertEquals(31337, inflight.m_clientHandle);
    handle = dut.getHandle(false, 12, 31338, 10, 10l, "yankees", 0, true, false);
    assertEquals(ClientInterfaceHandleManager.MP_PART_ID, ClientInterfaceHandleManager.getPartIdFromHandle(handle));
    assertEquals(0, ClientInterfaceHandleManager.getSeqNumFromHandle(handle));
    inflight = dut.findHandle(handle);
    assertEquals(handle, inflight.m_ciHandle | ClientInterfaceHandleManager.READ_BIT);
    assertEquals(31338, inflight.m_clientHandle);
}
Also used : Connection(org.voltcore.network.Connection) Test(org.junit.Test)

Example 4 with Connection

use of org.voltcore.network.Connection in project voltdb by VoltDB.

the class ClientInterface method checkForDeadConnections.

/**
     * Check for dead connections by providing each connection with the current
     * time so it can calculate the delta between now and the time the oldest message was
     * queued for sending.
     * @param now Current time in milliseconds
     */
private final void checkForDeadConnections(final long now) {
    final ArrayList<Pair<Connection, Integer>> connectionsToRemove = new ArrayList<Pair<Connection, Integer>>();
    for (final ClientInterfaceHandleManager cihm : m_cihm.values()) {
        // Internal connections don't implement calculatePendingWriteDelta(), so check for real connection first
        if (VoltPort.class == cihm.connection.getClass()) {
            final int delta = cihm.connection.writeStream().calculatePendingWriteDelta(now);
            if (delta > CLIENT_HANGUP_TIMEOUT) {
                connectionsToRemove.add(Pair.of(cihm.connection, delta));
            }
        }
    }
    for (final Pair<Connection, Integer> p : connectionsToRemove) {
        Connection c = p.getFirst();
        networkLog.warn("Closing connection to " + c + " because it hasn't read a response that was pending for " + p.getSecond() + " milliseconds");
        c.unregister();
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) Connection(org.voltcore.network.Connection) Pair(org.voltcore.utils.Pair)

Example 5 with Connection

use of org.voltcore.network.Connection in project voltdb by VoltDB.

the class Distributer method createConnectionWithHashedCredentials.

void createConnectionWithHashedCredentials(String host, String program, byte[] hashedPassword, int port, ClientAuthScheme scheme) throws UnknownHostException, IOException {
    SSLEngine sslEngine = null;
    if (m_sslContext != null) {
        sslEngine = m_sslContext.createSSLEngine("client", port);
        sslEngine.setUseClientMode(true);
        Set<String> enabled = ImmutableSet.copyOf(sslEngine.getEnabledCipherSuites());
        Set<String> intersection = Sets.intersection(SSLConfiguration.GCM_CIPHERS, enabled);
        if (intersection.isEmpty()) {
            intersection = Sets.intersection(SSLConfiguration.PREFERRED_CIPHERS, enabled);
        }
        if (intersection.isEmpty()) {
            intersection = enabled;
        }
        sslEngine.setEnabledCipherSuites(intersection.toArray(new String[0]));
    }
    final Object[] socketChannelAndInstanceIdAndBuildString = ConnectionUtil.getAuthenticatedConnection(host, program, hashedPassword, port, m_subject, scheme, sslEngine);
    final SocketChannel aChannel = (SocketChannel) socketChannelAndInstanceIdAndBuildString[0];
    final long[] instanceIdWhichIsTimestampAndLeaderIp = (long[]) socketChannelAndInstanceIdAndBuildString[1];
    final int hostId = (int) instanceIdWhichIsTimestampAndLeaderIp[0];
    NodeConnection cxn = new NodeConnection(instanceIdWhichIsTimestampAndLeaderIp);
    Connection c = null;
    try {
        if (aChannel != null) {
            c = m_network.registerChannel(aChannel, cxn, m_cipherService, sslEngine);
        }
    } catch (Exception e) {
        // Need to clean up the socket if there was any failure
        try {
            aChannel.close();
        } catch (IOException e1) {
        //Don't care connection is already lost anyways
        }
        Throwables.propagate(e);
    }
    cxn.m_connection = c;
    synchronized (this) {
        // Careful, this is slightly less safe than the previous behavior.
        if (m_connections.size() == 0) {
            m_clusterInstanceId = null;
        }
        if (m_clusterInstanceId == null) {
            long timestamp = instanceIdWhichIsTimestampAndLeaderIp[2];
            int addr = (int) instanceIdWhichIsTimestampAndLeaderIp[3];
            m_clusterInstanceId = new Object[] { timestamp, addr };
        } else {
            if (!(((Long) m_clusterInstanceId[0]).longValue() == instanceIdWhichIsTimestampAndLeaderIp[2]) || !(((Integer) m_clusterInstanceId[1]).longValue() == instanceIdWhichIsTimestampAndLeaderIp[3])) {
                // clean up the pre-registered voltnetwork connection/channel
                c.unregister();
                throw new IOException("Cluster instance id mismatch. Current is " + m_clusterInstanceId[0] + "," + m_clusterInstanceId[1] + " and server's was " + instanceIdWhichIsTimestampAndLeaderIp[2] + "," + instanceIdWhichIsTimestampAndLeaderIp[3]);
            }
        }
        m_buildString = (String) socketChannelAndInstanceIdAndBuildString[2];
        m_connections.add(cxn);
    }
    if (m_useClientAffinity) {
        synchronized (this) {
            m_hostIdToConnection.put(hostId, cxn);
        }
        if (m_subscribedConnection == null) {
            subscribeToNewNode();
        }
    }
}
Also used : SocketChannel(java.nio.channels.SocketChannel) SSLEngine(javax.net.ssl.SSLEngine) Connection(org.voltcore.network.Connection) IOException(java.io.IOException) TimeoutException(java.util.concurrent.TimeoutException) JSONException(org.json_voltpatches.JSONException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicLong(java.util.concurrent.atomic.AtomicLong) JSONObject(org.json_voltpatches.JSONObject)

Aggregations

Connection (org.voltcore.network.Connection)5 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 IOException (java.io.IOException)1 UnknownHostException (java.net.UnknownHostException)1 SocketChannel (java.nio.channels.SocketChannel)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 SSLEngine (javax.net.ssl.SSLEngine)1 JSONException (org.json_voltpatches.JSONException)1 JSONObject (org.json_voltpatches.JSONObject)1 Pair (org.voltcore.utils.Pair)1