-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor async executor service dependencies using guice framework
Signed-off-by: Vamsi Manohar <[email protected]>
- Loading branch information
Showing
19 changed files
with
547 additions
and
209 deletions.
There are no files selected for viewing
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
12 changes: 12 additions & 0 deletions
12
spark/src/main/java/org/opensearch/sql/spark/client/EMRServerlessClientFactory.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,12 @@ | ||
package org.opensearch.sql.spark.client; | ||
|
||
/** Factory interface for creating instances of {@link EMRServerlessClient}. */ | ||
public interface EMRServerlessClientFactory { | ||
|
||
/** | ||
* Gets an instance of {@link EMRServerlessClient}. | ||
* | ||
* @return An {@link EMRServerlessClient} instance. | ||
*/ | ||
EMRServerlessClient getClient(); | ||
} |
66 changes: 66 additions & 0 deletions
66
spark/src/main/java/org/opensearch/sql/spark/client/EMRServerlessClientFactoryImpl.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,66 @@ | ||
package org.opensearch.sql.spark.client; | ||
|
||
import static org.opensearch.sql.common.setting.Settings.Key.SPARK_EXECUTION_ENGINE_CONFIG; | ||
|
||
import com.amazonaws.auth.DefaultAWSCredentialsProviderChain; | ||
import com.amazonaws.services.emrserverless.AWSEMRServerless; | ||
import com.amazonaws.services.emrserverless.AWSEMRServerlessClientBuilder; | ||
import java.security.AccessController; | ||
import java.security.PrivilegedAction; | ||
import lombok.RequiredArgsConstructor; | ||
import org.opensearch.sql.spark.config.SparkExecutionEngineConfig; | ||
import org.opensearch.sql.spark.config.SparkExecutionEngineConfigSupplier; | ||
|
||
/** Implementation of {@link EMRServerlessClientFactory}. */ | ||
@RequiredArgsConstructor | ||
public class EMRServerlessClientFactoryImpl implements EMRServerlessClientFactory { | ||
|
||
private final SparkExecutionEngineConfigSupplier sparkExecutionEngineConfigSupplier; | ||
private EMRServerlessClient emrServerlessClient; | ||
private String region; | ||
|
||
/** | ||
* Gets an instance of {@link EMRServerlessClient}. | ||
* | ||
* @return An {@link EMRServerlessClient} instance. | ||
*/ | ||
@Override | ||
public EMRServerlessClient getClient() { | ||
SparkExecutionEngineConfig sparkExecutionEngineConfig = | ||
this.sparkExecutionEngineConfigSupplier.getSparkExecutionEngineConfig(); | ||
validateSparkExecutionEngineConfig(sparkExecutionEngineConfig); | ||
if (isNewClientCreationRequired(sparkExecutionEngineConfig.getRegion())) { | ||
region = sparkExecutionEngineConfig.getRegion(); | ||
this.emrServerlessClient = createEMRServerlessClient(this.region); | ||
} | ||
return this.emrServerlessClient; | ||
} | ||
|
||
private boolean isNewClientCreationRequired(String region) { | ||
return !region.equals(this.region); | ||
} | ||
|
||
private void validateSparkExecutionEngineConfig( | ||
SparkExecutionEngineConfig sparkExecutionEngineConfig) { | ||
if (sparkExecutionEngineConfig == null || sparkExecutionEngineConfig.getRegion() == null) { | ||
throw new IllegalArgumentException( | ||
String.format( | ||
"Async Query APIs are disabled as %s is not configured in cluster settings. Please" | ||
+ " configure the setting and restart the domain to enable Async Query APIs", | ||
SPARK_EXECUTION_ENGINE_CONFIG.getKeyValue())); | ||
} | ||
} | ||
|
||
private EMRServerlessClient createEMRServerlessClient(String awsRegion) { | ||
return AccessController.doPrivileged( | ||
(PrivilegedAction<EMRServerlessClient>) | ||
() -> { | ||
AWSEMRServerless awsemrServerless = | ||
AWSEMRServerlessClientBuilder.standard() | ||
.withRegion(awsRegion) | ||
.withCredentials(new DefaultAWSCredentialsProviderChain()) | ||
.build(); | ||
return new EmrServerlessClientImpl(awsemrServerless); | ||
}); | ||
} | ||
} |
Oops, something went wrong.