Search in sources :

Example 1 with MySqlMetrics

use of com.github.ambry.mysql.MySqlMetrics in project ambry by linkedin.

the class MySqlAccountServiceIntegrationTest method testFailover.

@Test
public void testFailover() throws Exception {
    String dbInfoJsonString = mySqlConfigProps.getProperty(DB_INFO);
    JSONArray dbInfo = new JSONArray(dbInfoJsonString);
    JSONObject entry = dbInfo.getJSONObject(0);
    DbEndpoint origEndpoint = DbEndpoint.fromJson(entry);
    // Make two endpoints, one with bad url, writeable, remote; second with good url, writeable, local
    String localDc = "local", remoteDc = "remote";
    String badUrl = "jdbc:mysql://badhost/AccountMetadata";
    DbEndpoint localGoodEndpoint = new DbEndpoint(origEndpoint.getUrl(), localDc, false, origEndpoint.getUsername(), origEndpoint.getPassword());
    DbEndpoint remoteBadEndpoint = new DbEndpoint(badUrl, remoteDc, true, origEndpoint.getUsername(), origEndpoint.getPassword());
    JSONArray endpointsJson = new JSONArray().put(localGoodEndpoint.toJson()).put(remoteBadEndpoint.toJson());
    mySqlConfigProps.setProperty(DB_INFO, endpointsJson.toString());
    mySqlConfigProps.setProperty(ClusterMapConfig.CLUSTERMAP_DATACENTER_NAME, localDc);
    mySqlAccountStore = spy(new MySqlAccountStoreFactory(new VerifiableProperties(mySqlConfigProps), new MetricRegistry()).getMySqlAccountStore());
    when(mockMySqlAccountStoreFactory.getMySqlAccountStore()).thenReturn(mySqlAccountStore);
    // constructor does initial fetch which will fail on first endpoint and succeed on second
    AccountServiceMetrics accountServiceMetrics = new AccountServiceMetrics(new MetricRegistry());
    mySqlAccountService = new MySqlAccountService(accountServiceMetrics, accountServiceConfig, mockMySqlAccountStoreFactory, mockNotifier);
    MySqlMetrics storeMetrics = mySqlAccountStore.getMySqlDataAccessor().getMetrics();
    // At this point, should have at least one connection failure (bad endpoint) and one success
    long expectedConnectionFail = storeMetrics.connectionFailureCount.getCount();
    long expectedConnectionSuccess = storeMetrics.connectionSuccessCount.getCount();
    assertTrue(expectedConnectionFail > 0);
    assertTrue(expectedConnectionSuccess > 0);
    // Try to update, should fail to get connection
    Account account = makeTestAccountWithContainer();
    try {
        mySqlAccountService.updateAccounts(Collections.singletonList(account));
        fail("Expected failure due to no writeable accounts");
    } catch (AccountServiceException ase) {
        assertEquals(AccountServiceErrorCode.InternalError, ase.getErrorCode());
    }
    expectedConnectionFail++;
    assertEquals(1, accountServiceMetrics.updateAccountErrorCount.getCount());
    assertEquals(expectedConnectionFail, storeMetrics.connectionFailureCount.getCount());
    assertEquals(expectedConnectionSuccess, storeMetrics.connectionSuccessCount.getCount());
    mySqlAccountService.fetchAndUpdateCache();
}
Also used : VerifiableProperties(com.github.ambry.config.VerifiableProperties) MetricRegistry(com.codahale.metrics.MetricRegistry) JSONArray(org.json.JSONArray) MySqlAccountStoreFactory(com.github.ambry.account.mysql.MySqlAccountStoreFactory) JSONObject(org.json.JSONObject) MySqlMetrics(com.github.ambry.mysql.MySqlMetrics) Test(org.junit.Test)

Example 2 with MySqlMetrics

use of com.github.ambry.mysql.MySqlMetrics in project ambry by linkedin.

the class MySqlAccountServiceIntegrationTest method testBadCredentials.

@Test
public void testBadCredentials() throws Exception {
    DbEndpoint endpoint = new DbEndpoint("jdbc:mysql://localhost/AccountMetadata", "dc1", true, "baduser", "badpassword");
    try {
        new MySqlAccountStore(Collections.singletonList(endpoint), endpoint.getDatacenter(), new MySqlMetrics(MySqlAccountStore.class, new MetricRegistry()), accountServiceConfig);
        fail("Store creation should fail with bad credentials");
    } catch (SQLException e) {
        assertTrue(MySqlDataAccessor.isCredentialError(e));
    }
}
Also used : SQLException(java.sql.SQLException) MetricRegistry(com.codahale.metrics.MetricRegistry) MySqlMetrics(com.github.ambry.mysql.MySqlMetrics) MySqlAccountStore(com.github.ambry.account.mysql.MySqlAccountStore) Test(org.junit.Test)

Aggregations

MetricRegistry (com.codahale.metrics.MetricRegistry)2 MySqlMetrics (com.github.ambry.mysql.MySqlMetrics)2 Test (org.junit.Test)2 MySqlAccountStore (com.github.ambry.account.mysql.MySqlAccountStore)1 MySqlAccountStoreFactory (com.github.ambry.account.mysql.MySqlAccountStoreFactory)1 VerifiableProperties (com.github.ambry.config.VerifiableProperties)1 SQLException (java.sql.SQLException)1 JSONArray (org.json.JSONArray)1 JSONObject (org.json.JSONObject)1