Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cache invalidation for create and update operations (issue #70) #72

Open
wants to merge 1 commit into
base: branch-3.4.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.amazonaws.glue.catalog.metastore;

import com.amazonaws.services.glue.model.Database;
import com.amazonaws.services.glue.model.DatabaseInput;
import com.amazonaws.services.glue.model.Table;
import com.amazonaws.services.glue.model.TableInput;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
Expand Down Expand Up @@ -122,6 +124,57 @@ public Table getTable(String dbName, String tableName) {
return result;
}

@Override
public void createDatabase(DatabaseInput databaseInput) {
invalidateDatabaseCache(databaseInput.getName());
super.createDatabase(databaseInput);
}

@Override
public void updateDatabase(String databaseName, DatabaseInput databaseInput) {
invalidateDatabaseCache(databaseName);
super.updateDatabase(databaseName, databaseInput);
}

@Override
public void deleteDatabase(String dbName) {
invalidateDatabaseCache(dbName);
super.deleteDatabase(dbName);
}

@Override
public void createTable(String dbName, TableInput tableInput) {
invalidateTableCache(dbName, tableInput.getName());
super.createTable(dbName, tableInput);
}

@Override
public void updateTable(String dbName, TableInput tableInput) {
invalidateTableCache(dbName, tableInput.getName());
super.updateTable(dbName, tableInput);
}

@Override
public void deleteTable(String dbName, String tableName) {
invalidateTableCache(dbName, tableName);
super.deleteTable(dbName, tableName);
}

protected void invalidateDatabaseCache(String dbName) {
if (databaseCacheEnabled && databaseCache != null) {
logger.info("Invalidated cache entry for database: " + dbName);
databaseCache.invalidate(dbName);
}
}

protected void invalidateTableCache(String dbName, String tableName) {
if (tableCacheEnabled && tableCache != null) {
TableIdentifier key = new TableIdentifier(dbName, tableName);
logger.info("Invalidated cache entry for table: " + key);
tableCache.invalidate(key);
}
}

static class TableIdentifier {
private final String dbName;
private final String tableName;
Expand Down Expand Up @@ -161,4 +214,4 @@ public int hashCode() {
return Objects.hash(dbName, tableName);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.amazonaws.services.glue.model.Database;
import com.amazonaws.services.glue.model.Table;
import com.amazonaws.services.glue.model.DatabaseInput;
import com.amazonaws.services.glue.model.TableInput;
import com.google.common.cache.Cache;
import org.apache.hadoop.hive.conf.HiveConf;
import org.junit.Before;
Expand All @@ -16,7 +18,6 @@
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.junit.Assert.assertTrue;

import static com.amazonaws.glue.catalog.util.AWSGlueConfig.AWS_GLUE_DB_CACHE_ENABLE;
import static com.amazonaws.glue.catalog.util.AWSGlueConfig.AWS_GLUE_TABLE_CACHE_ENABLE;
Expand Down Expand Up @@ -45,7 +46,6 @@ public void setUp() {
when(hiveConf.getInt(AWS_GLUE_TABLE_CACHE_TTL_MINS, 0)).thenReturn(100);
when(hiveConf.getInt(AWS_GLUE_DB_CACHE_SIZE, 0)).thenReturn(100);
when(hiveConf.getInt(AWS_GLUE_DB_CACHE_TTL_MINS, 0)).thenReturn(100);

}

@Test(expected = NullPointerException.class)
Expand Down Expand Up @@ -79,12 +79,12 @@ public void testConstructorWithInvalidDbCacheTtl() {

@Test
public void testGetDatabaseWhenCacheDisabled() {
//disable cache
when(hiveConf.getBoolean(AWS_GLUE_DB_CACHE_ENABLE, false)).thenReturn(false);
Database db = new Database();
AWSGlueMetastoreCacheDecorator cacheDecorator =
new AWSGlueMetastoreCacheDecorator(hiveConf, glueMetastore);
when(glueMetastore.getDatabase(DB_NAME)).thenReturn(db);

assertEquals(db, cacheDecorator.getDatabase(DB_NAME));
assertNull(cacheDecorator.databaseCache);
verify(glueMetastore, times(1)).getDatabase(DB_NAME);
Expand Down Expand Up @@ -126,14 +126,48 @@ public void testGetDatabaseWhenCacheEnabledAndCacheHit() {
verify(dbCache, times(1)).getIfPresent(DB_NAME);
}

@Test
public void testCreateDatabase() {
DatabaseInput databaseInput = new DatabaseInput().withName(DB_NAME);
AWSGlueMetastoreCacheDecorator cacheDecorator =
spy(new AWSGlueMetastoreCacheDecorator(hiveConf, glueMetastore));
cacheDecorator.createDatabase(databaseInput);

verify(glueMetastore, times(1)).createDatabase(databaseInput);
verify(cacheDecorator, times(1)).invalidateDatabaseCache(DB_NAME);
}

@Test
public void testUpdateDatabase() {
DatabaseInput databaseInput = new DatabaseInput().withName(DB_NAME);
AWSGlueMetastoreCacheDecorator cacheDecorator =
spy(new AWSGlueMetastoreCacheDecorator(hiveConf, glueMetastore));

cacheDecorator.updateDatabase(DB_NAME, databaseInput);

verify(glueMetastore, times(1)).updateDatabase(DB_NAME, databaseInput);
verify(cacheDecorator, times(1)).invalidateDatabaseCache(DB_NAME);
}

@Test
public void testDeleteDatabase() {
AWSGlueMetastoreCacheDecorator cacheDecorator =
spy(new AWSGlueMetastoreCacheDecorator(hiveConf, glueMetastore));

cacheDecorator.deleteDatabase(DB_NAME);

verify(glueMetastore, times(1)).deleteDatabase(DB_NAME);
verify(cacheDecorator, times(1)).invalidateDatabaseCache(DB_NAME);
}

@Test
public void testGetTableWhenCacheDisabled() {
//disable cache
when(hiveConf.getBoolean(AWS_GLUE_TABLE_CACHE_ENABLE, false)).thenReturn(false);
Table table = new Table();
AWSGlueMetastoreCacheDecorator cacheDecorator =
new AWSGlueMetastoreCacheDecorator(hiveConf, glueMetastore);
when(glueMetastore.getTable(DB_NAME, TABLE_NAME)).thenReturn(table);

assertEquals(table, cacheDecorator.getTable(DB_NAME, TABLE_NAME));
assertNull(cacheDecorator.tableCache);
verify(glueMetastore, times(1)).getTable(DB_NAME, TABLE_NAME);
Expand Down Expand Up @@ -175,4 +209,38 @@ public void testGetTableWhenCacheEnabledAndCacheHit() {
verify(tableCache, times(1)).getIfPresent(TABLE_IDENTIFIER);
}

}
@Test
public void testCreateTable() {
TableInput tableInput = new TableInput().withName(TABLE_NAME);
AWSGlueMetastoreCacheDecorator cacheDecorator =
spy(new AWSGlueMetastoreCacheDecorator(hiveConf, glueMetastore));

cacheDecorator.createTable(DB_NAME, tableInput);

verify(glueMetastore, times(1)).createTable(DB_NAME, tableInput);
verify(cacheDecorator, times(1)).invalidateTableCache(DB_NAME, TABLE_NAME);
}

@Test
public void testUpdateTable() {
TableInput tableInput = new TableInput().withName(TABLE_NAME);
AWSGlueMetastoreCacheDecorator cacheDecorator =
spy(new AWSGlueMetastoreCacheDecorator(hiveConf, glueMetastore));

cacheDecorator.updateTable(DB_NAME, tableInput);

verify(glueMetastore, times(1)).updateTable(DB_NAME, tableInput);
verify(cacheDecorator, times(1)).invalidateTableCache(DB_NAME, TABLE_NAME);
}

@Test
public void testDeleteTable() {
AWSGlueMetastoreCacheDecorator cacheDecorator =
spy(new AWSGlueMetastoreCacheDecorator(hiveConf, glueMetastore));

cacheDecorator.deleteTable(DB_NAME, TABLE_NAME);

verify(glueMetastore, times(1)).deleteTable(DB_NAME, TABLE_NAME);
verify(cacheDecorator, times(1)).invalidateTableCache(DB_NAME, TABLE_NAME);
}
}