Search in sources :

Example 6 with KnownKey

use of com.zimbra.common.localconfig.KnownKey in project zm-mailbox by Zimbra.

the class TestLdapConnection method onCheckoutHealthCheck.

@Test
public void onCheckoutHealthCheck() throws Exception {
    SKIP_FOR_INMEM_LDAP_SERVER(SkipTestReason.CONNECTION_POOL_HEALTH_CHECK);
    Map<KnownKey, String> lcKeysToModify = new HashMap<KnownKey, String>();
    lcKeysToModify.put(LC.ldap_connect_pool_health_check_on_checkout_enabled, "true");
    // lcKeysToModify.put(LC.ldap_connect_pool_health_check_after_exception_enabled, "false");
    // lcKeysToModify.put(LC.ldap_connect_pool_health_check_background_enabled, "false");
    Map<KnownKey, String> origLCKeyValues = setLocalConfig(lcKeysToModify);
    final int NUM_CONNS = 10;
    ExternalLdapConfig ldapConfig = new ExternalLdapConfig(LDAP_URL_ON_CHECKOUT, START_TLS_ENABLED, null, BIND_DN, BIND_PASSWORD, null, null);
    LDAPConnectionPool connPool = populateConnPool(ldapConfig, NUM_CONNS);
    // stop ldap server here
    System.out.println("Before health check, availConns = " + connPool.getCurrentAvailableConnections());
    stopLdap();
    // try to get a connection from the pool to trigger health check
    boolean caughtException = false;
    try {
        UBIDLdapContext zlc = getContext(ldapConfig);
    } catch (ServiceException e) {
        caughtException = true;
    }
    assertTrue(caughtException);
    System.out.println("After health check, availConns = " + connPool.getCurrentAvailableConnections());
    assertEquals(0, connPool.getCurrentAvailableConnections());
    // put the config key back
    setLocalConfig(origLCKeyValues);
    startLdap();
    // get a connection now, should be successful
    UBIDLdapContext zlc = getContext(ldapConfig);
    closeContext(zlc);
}
Also used : LDAPConnectionPool(com.unboundid.ldap.sdk.LDAPConnectionPool) KnownKey(com.zimbra.common.localconfig.KnownKey) ExternalLdapConfig(com.zimbra.cs.ldap.LdapServerConfig.ExternalLdapConfig) ServiceException(com.zimbra.common.service.ServiceException) HashMap(java.util.HashMap) UBIDLdapContext(com.zimbra.cs.ldap.unboundid.UBIDLdapContext) Test(org.junit.Test)

Example 7 with KnownKey

use of com.zimbra.common.localconfig.KnownKey in project zm-mailbox by Zimbra.

the class TestLdapConnection method afterExceptionHealthCheck.

@Test
// after-exception health check is not supported.
@Ignore
public void afterExceptionHealthCheck() throws Exception {
    Map<KnownKey, String> lcKeysToModify = new HashMap<KnownKey, String>();
    lcKeysToModify.put(LC.ldap_connect_pool_health_check_on_checkout_enabled, "false");
    // lcKeysToModify.put(LC.ldap_connect_pool_health_check_after_exception_enabled, "true");
    // lcKeysToModify.put(LC.ldap_connect_pool_health_check_background_enabled, "false");
    Map<KnownKey, String> origLCKeyValues = setLocalConfig(lcKeysToModify);
    final int NUM_CONNS = 10;
    ExternalLdapConfig ldapConfig = new ExternalLdapConfig(LDAP_URL_AFTER_EXCEPTION, START_TLS_ENABLED, null, BIND_DN, BIND_PASSWORD, null, null);
    LDAPConnectionPool connPool = populateConnPool(ldapConfig, NUM_CONNS);
    // stop ldap server here
    System.out.println("Before health check, availConns = " + connPool.getCurrentAvailableConnections());
    stopLdap();
    // try to get a connection from the pool
    // unlike on checkout health check, this will NOT trigger a health check
    // it will just return one connection from the pool
    UBIDLdapContext zlcTest = getContext(ldapConfig);
    // use the connection - now we should get an exception, and a health check
    // should be triggered.
    boolean caughtException = false;
    try {
        zlcTest.getAttributes(LdapConstants.DN_ROOT_DSE, null);
    } catch (ServiceException e) {
        caughtException = true;
    // e.printStackTrace();
    } finally {
    // if this is called, it somehow increments the CurrentAvailableConnections count
    // in the connection pool - it should not, because the connection is already defunced
    // (LDAPConnectionPool.releaseConnectionAfterException() was called).
    // 
    // The CurrentAvailableConnections count drop back to NUM_CONNS - 1 after one minute.
    // closeContext(zlcTest);
    }
    assertTrue(caughtException);
    System.out.println("After health check, availConns = " + connPool.getCurrentAvailableConnections());
    int secs = 0;
    while (true) {
        Thread.sleep(1000);
        secs++;
        int junk = connPool.getCurrentAvailableConnections();
        System.out.println("After health check, availConns = " + junk + " " + secs);
        if (junk < NUM_CONNS) {
            break;
        }
    }
    // unlink on-checkout and beckground modes, the after-exception mode removes only
    // the bad connection.   To support this, we need to call
    // LDAPConnectionPool.releaseConnectionAfterException(LDAPConnection connection, LDAPException exception)
    // after an exception is caught.
    // For some reason this only work in Eclipse because of timing issue.
    assertEquals(NUM_CONNS - 1, connPool.getCurrentAvailableConnections());
    // put the config key back
    setLocalConfig(origLCKeyValues);
    startLdap();
    // get a connection now, should be successful
    UBIDLdapContext zlc = getContext(ldapConfig);
    closeContext(zlc);
}
Also used : LDAPConnectionPool(com.unboundid.ldap.sdk.LDAPConnectionPool) KnownKey(com.zimbra.common.localconfig.KnownKey) ExternalLdapConfig(com.zimbra.cs.ldap.LdapServerConfig.ExternalLdapConfig) ServiceException(com.zimbra.common.service.ServiceException) HashMap(java.util.HashMap) UBIDLdapContext(com.zimbra.cs.ldap.unboundid.UBIDLdapContext) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

KnownKey (com.zimbra.common.localconfig.KnownKey)7 HashMap (java.util.HashMap)4 LDAPConnectionPool (com.unboundid.ldap.sdk.LDAPConnectionPool)3 ExternalLdapConfig (com.zimbra.cs.ldap.LdapServerConfig.ExternalLdapConfig)3 UBIDLdapContext (com.zimbra.cs.ldap.unboundid.UBIDLdapContext)3 Test (org.junit.Test)3 ServiceException (com.zimbra.common.service.ServiceException)2 Field (java.lang.reflect.Field)1 Map (java.util.Map)1 Ignore (org.junit.Ignore)1