-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: vamsi-amazon <reddyvam@amazon.com>
- Loading branch information
1 parent
23cc0f6
commit e0dd50c
Showing
30 changed files
with
1,368 additions
and
131 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
core/src/main/java/org/opensearch/sql/datasource/DataSourceLoaderCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package org.opensearch.sql.datasource; | ||
|
||
import org.opensearch.sql.datasource.model.DataSource; | ||
import org.opensearch.sql.datasource.model.DataSourceMetadata; | ||
|
||
/** | ||
* Interface for DataSourceLoaderCache which provides methods for | ||
* fetch, loading and invalidating DataSource cache. | ||
*/ | ||
public interface DataSourceLoaderCache { | ||
|
||
/** | ||
* Returns cached datasource object or loads a new one if not present. | ||
* | ||
* @param dataSourceMetadata {@link DataSourceMetadata}. | ||
* @return {@link DataSource} | ||
*/ | ||
DataSource getOrLoadDataSource(DataSourceMetadata dataSourceMetadata); | ||
|
||
} |
50 changes: 50 additions & 0 deletions
50
core/src/main/java/org/opensearch/sql/datasource/DataSourceLoaderCacheImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package org.opensearch.sql.datasource; | ||
|
||
import com.google.common.cache.Cache; | ||
import com.google.common.cache.CacheBuilder; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.stream.Collectors; | ||
import org.opensearch.sql.datasource.model.DataSource; | ||
import org.opensearch.sql.datasource.model.DataSourceMetadata; | ||
import org.opensearch.sql.datasource.model.DataSourceType; | ||
import org.opensearch.sql.storage.DataSourceFactory; | ||
|
||
/** | ||
* Default implementation of DataSourceLoaderCache. This implementation | ||
* utilizes Google Guava Cache {@link Cache} for caching DataSource objects | ||
* against {@link DataSourceMetadata}. Expires the cache objects every 24 hrs after | ||
* the last access. | ||
*/ | ||
public class DataSourceLoaderCacheImpl implements DataSourceLoaderCache { | ||
private final Map<DataSourceType, DataSourceFactory> dataSourceFactoryMap; | ||
private final Cache<DataSourceMetadata, DataSource> dataSourceCache; | ||
|
||
/** | ||
* DataSourceLoaderCacheImpl constructor. | ||
* | ||
* @param dataSourceFactorySet set of {@link DataSourceFactory}. | ||
*/ | ||
public DataSourceLoaderCacheImpl(Set<DataSourceFactory> dataSourceFactorySet) { | ||
this.dataSourceFactoryMap = dataSourceFactorySet.stream() | ||
.collect(Collectors.toMap(DataSourceFactory::getDataSourceType, f -> f)); | ||
this.dataSourceCache = CacheBuilder.newBuilder() | ||
.maximumSize(1000) | ||
.expireAfterAccess(24, TimeUnit.HOURS) | ||
.build(); | ||
} | ||
|
||
@Override | ||
public DataSource getOrLoadDataSource(DataSourceMetadata dataSourceMetadata) { | ||
DataSource dataSource = this.dataSourceCache.getIfPresent(dataSourceMetadata); | ||
if (dataSource == null) { | ||
dataSource = this.dataSourceFactoryMap.get(dataSourceMetadata.getConnector()) | ||
.createDataSource(dataSourceMetadata); | ||
this.dataSourceCache.put(dataSourceMetadata, dataSource); | ||
return dataSource; | ||
} | ||
return dataSource; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
core/src/main/java/org/opensearch/sql/datasource/exceptions/DataSourceNotFoundException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* | ||
* | ||
* * Copyright OpenSearch Contributors | ||
* * SPDX-License-Identifier: Apache-2.0 | ||
* | ||
*/ | ||
|
||
package org.opensearch.sql.datasource.exceptions; | ||
|
||
/** | ||
* DataSourceNotFoundException. | ||
*/ | ||
public class DataSourceNotFoundException extends RuntimeException { | ||
public DataSourceNotFoundException(String message) { | ||
super(message); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
core/src/test/java/org/opensearch/sql/datasource/DataSourceLoaderCacheImplTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package org.opensearch.sql.datasource; | ||
|
||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.lenient; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.verifyNoMoreInteractions; | ||
import static org.mockito.Mockito.when; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.opensearch.sql.datasource.model.DataSource; | ||
import org.opensearch.sql.datasource.model.DataSourceMetadata; | ||
import org.opensearch.sql.datasource.model.DataSourceType; | ||
import org.opensearch.sql.storage.DataSourceFactory; | ||
import org.opensearch.sql.storage.StorageEngine; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class DataSourceLoaderCacheImplTest { | ||
|
||
@Mock | ||
private DataSourceFactory dataSourceFactory; | ||
|
||
@Mock | ||
private StorageEngine storageEngine; | ||
|
||
@BeforeEach | ||
public void setup() { | ||
lenient() | ||
.doAnswer( | ||
invocation -> { | ||
DataSourceMetadata metadata = invocation.getArgument(0); | ||
return new DataSource(metadata.getName(), metadata.getConnector(), storageEngine); | ||
}) | ||
.when(dataSourceFactory) | ||
.createDataSource(any()); | ||
when(dataSourceFactory.getDataSourceType()).thenReturn(DataSourceType.OPENSEARCH); | ||
} | ||
|
||
@Test | ||
void testGetOrLoadDataSource() { | ||
DataSourceLoaderCache dataSourceLoaderCache = | ||
new DataSourceLoaderCacheImpl(Collections.singleton(dataSourceFactory)); | ||
DataSourceMetadata dataSourceMetadata = new DataSourceMetadata(); | ||
dataSourceMetadata.setName("testDS"); | ||
dataSourceMetadata.setConnector(DataSourceType.OPENSEARCH); | ||
dataSourceMetadata.setAllowedRoles(Collections.emptyList()); | ||
dataSourceMetadata.setProperties(ImmutableMap.of()); | ||
DataSource dataSource = dataSourceLoaderCache.getOrLoadDataSource(dataSourceMetadata); | ||
verify(dataSourceFactory, times(1)).createDataSource(dataSourceMetadata); | ||
Assertions.assertEquals(dataSource, | ||
dataSourceLoaderCache.getOrLoadDataSource(dataSourceMetadata)); | ||
verifyNoMoreInteractions(dataSourceFactory); | ||
} | ||
|
||
@Test | ||
void testGetOrLoadDataSourceWithMetadataUpdate() { | ||
DataSourceLoaderCache dataSourceLoaderCache = | ||
new DataSourceLoaderCacheImpl(Collections.singleton(dataSourceFactory)); | ||
DataSourceMetadata dataSourceMetadata = getMetadata(); | ||
dataSourceLoaderCache.getOrLoadDataSource(dataSourceMetadata); | ||
dataSourceLoaderCache.getOrLoadDataSource(dataSourceMetadata); | ||
dataSourceMetadata.setAllowedRoles(List.of("testDS_access")); | ||
dataSourceLoaderCache.getOrLoadDataSource(dataSourceMetadata); | ||
dataSourceLoaderCache.getOrLoadDataSource(dataSourceMetadata); | ||
verify(dataSourceFactory, times(2)).createDataSource(dataSourceMetadata); | ||
} | ||
|
||
private DataSourceMetadata getMetadata() { | ||
DataSourceMetadata dataSourceMetadata = new DataSourceMetadata(); | ||
dataSourceMetadata.setName("testDS"); | ||
dataSourceMetadata.setConnector(DataSourceType.OPENSEARCH); | ||
dataSourceMetadata.setAllowedRoles(Collections.emptyList()); | ||
dataSourceMetadata.setProperties(ImmutableMap.of()); | ||
return dataSourceMetadata; | ||
} | ||
|
||
} |
Oops, something went wrong.