Skip to content

Commit

Permalink
Some refactoring for the implementations of DistributedStorageAdmin (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
brfrn169 authored Jul 14, 2021
1 parent d91b02c commit 28ee011
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.scalar.db.api.TableMetadata;
import com.scalar.db.config.DatabaseConfig;
import com.scalar.db.exception.storage.ExecutionException;
import com.scalar.db.exception.storage.StorageRuntimeException;
import java.util.Map;
import java.util.Optional;
import javax.annotation.concurrent.ThreadSafe;
Expand Down Expand Up @@ -51,7 +52,11 @@ public void truncateTable(String namespace, String table) throws ExecutionExcept

@Override
public TableMetadata getTableMetadata(String namespace, String table) throws ExecutionException {
return metadataManager.getTableMetadata(fullNamespace(namespace), table);
try {
return metadataManager.getTableMetadata(fullNamespace(namespace), table);
} catch (StorageRuntimeException e) {
throw new ExecutionException("getting a table metadata failed", e);
}
}

private String fullNamespace(String namespace) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.scalar.db.api.Scan;
import com.scalar.db.api.TableMetadata;
import com.scalar.db.api.TableMetadata.Builder;
import com.scalar.db.exception.storage.ConnectionException;
import com.scalar.db.exception.storage.StorageRuntimeException;
import com.scalar.db.exception.storage.UnsupportedTypeException;
import com.scalar.db.io.DataType;
Expand Down Expand Up @@ -37,10 +38,12 @@ public TableMetadata getTableMetadata(String namespace, String table) {
try {
com.datastax.driver.core.TableMetadata metadata =
clusterManager.getMetadata(namespace, table);
if (metadata == null) {
return null;
}
tableMetadataMap.put(fullName, createTableMetadata(metadata));
} catch (StorageRuntimeException e) {
// The specified table is not found
return null;
} catch (ConnectionException e) {
throw new StorageRuntimeException("Failed to read the table metadata", e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
import com.google.common.annotations.VisibleForTesting;
import com.scalar.db.config.DatabaseConfig;
import com.scalar.db.exception.storage.ConnectionException;
import com.scalar.db.exception.storage.StorageRuntimeException;
import javax.annotation.Nonnull;
import javax.annotation.concurrent.ThreadSafe;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -71,7 +69,6 @@ public synchronized Session getSession() {
* @param table table name
* @return {@code TableMetadata}
*/
@Nonnull
public TableMetadata getMetadata(String keyspace, String table) {
KeyspaceMetadata metadata;
try {
Expand All @@ -80,7 +77,7 @@ public TableMetadata getMetadata(String keyspace, String table) {
throw new ConnectionException("can't get metadata from the cluster", e);
}
if (metadata == null || metadata.getTable(table) == null) {
throw new StorageRuntimeException("no table information found");
return null;
}
return metadata.getTable(table);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.scalar.db.api.TableMetadata;
import com.scalar.db.config.DatabaseConfig;
import com.scalar.db.exception.storage.ExecutionException;
import com.scalar.db.exception.storage.StorageRuntimeException;
import java.util.Map;
import java.util.Optional;
import javax.annotation.concurrent.ThreadSafe;
Expand Down Expand Up @@ -59,7 +60,11 @@ public void truncateTable(String namespace, String table) throws ExecutionExcept

@Override
public TableMetadata getTableMetadata(String namespace, String table) throws ExecutionException {
return metadataManager.getTableMetadata(fullNamespace(namespace), table);
try {
return metadataManager.getTableMetadata(fullNamespace(namespace), table);
} catch (StorageRuntimeException e) {
throw new ExecutionException("getting a table metadata failed", e);
}
}

private String fullNamespace(String namespace) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.scalar.db.api.DistributedStorageAdmin;
import com.scalar.db.api.TableMetadata;
import com.scalar.db.exception.storage.ExecutionException;
import com.scalar.db.exception.storage.StorageRuntimeException;
import java.net.URI;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -64,7 +65,11 @@ public void truncateTable(String namespace, String table) throws ExecutionExcept

@Override
public TableMetadata getTableMetadata(String namespace, String table) throws ExecutionException {
return metadataManager.getTableMetadata(fullNamespace(namespace), table);
try {
return metadataManager.getTableMetadata(fullNamespace(namespace), table);
} catch (StorageRuntimeException e) {
throw new ExecutionException("getting a table metadata failed", e);
}
}

private String fullNamespace(String namespace) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.scalar.db.api.DistributedStorageAdmin;
import com.scalar.db.api.TableMetadata;
import com.scalar.db.exception.storage.ExecutionException;
import com.scalar.db.exception.storage.StorageRuntimeException;
import java.sql.SQLException;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -56,7 +57,11 @@ public void truncateTable(String namespace, String table) throws ExecutionExcept

@Override
public TableMetadata getTableMetadata(String namespace, String table) throws ExecutionException {
return metadataManager.getTableMetadata(fullNamespace(namespace), table);
try {
return metadataManager.getTableMetadata(fullNamespace(namespace), table);
} catch (StorageRuntimeException e) {
throw new ExecutionException("getting a table metadata failed", e);
}
}

private String fullNamespace(String namespace) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,17 @@ public MultiStorageAdmin(MultiStorageConfig config) {
public void createTable(
String namespace, String table, TableMetadata metadata, Map<String, String> options)
throws ExecutionException {
throw new UnsupportedOperationException("implement later");
getAdmin(namespace, table).createTable(namespace, table, metadata, options);
}

@Override
public void dropTable(String namespace, String table) throws ExecutionException {
throw new UnsupportedOperationException("implement later");
getAdmin(namespace, table).dropTable(namespace, table);
}

@Override
public void truncateTable(String namespace, String table) throws ExecutionException {
throw new UnsupportedOperationException("implement later");
getAdmin(namespace, table).truncateTable(namespace, table);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import com.datastax.driver.core.exceptions.NoHostAvailableException;
import com.scalar.db.config.DatabaseConfig;
import com.scalar.db.exception.storage.ConnectionException;
import com.scalar.db.exception.storage.StorageRuntimeException;
import java.util.Properties;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -187,23 +186,22 @@ public void getMetadata_NoHostAvailable_ShouldThrowConnectionException() {
}

@Test
public void getMetadata_KeyspaceNotExists_ShouldThrowStorageRuntimeException() {
public void getMetadata_KeyspaceNotExists_ShouldReturnNull() {
// Arrange
manager.getSession();
Metadata metadata = mock(Metadata.class);
when(cluster.getMetadata()).thenReturn(metadata);
when(metadata.getKeyspace(anyString())).thenReturn(null);

// Act Assert
assertThatThrownBy(
() -> {
manager.getMetadata(ANY_KEYSPACE_NAME, ANY_TABLE_NAME);
})
.isInstanceOf(StorageRuntimeException.class);
// Act
TableMetadata actual = manager.getMetadata(ANY_KEYSPACE_NAME, ANY_TABLE_NAME);

// Assert
assertThat(actual).isNull();
}

@Test
public void getMetadata_TableNotExists_ShouldThrowStorageRuntimeException() {
public void getMetadata_TableNotExists_ShouldReturnNull() {
// Arrange
manager.getSession();
Metadata metadata = mock(Metadata.class);
Expand All @@ -213,10 +211,9 @@ public void getMetadata_TableNotExists_ShouldThrowStorageRuntimeException() {
when(keyspaceMetadata.getTable(anyString())).thenReturn(null);

// Act
assertThatThrownBy(
() -> {
manager.getMetadata(ANY_KEYSPACE_NAME, ANY_TABLE_NAME);
})
.isInstanceOf(StorageRuntimeException.class);
TableMetadata actual = manager.getMetadata(ANY_KEYSPACE_NAME, ANY_TABLE_NAME);

// Assert
assertThat(actual).isNull();
}
}

0 comments on commit 28ee011

Please sign in to comment.